00001
00005 #include "system.h"
00006
00007 #include "Python.h"
00008 #ifdef __LCLINT__
00009 #undef PyObject_HEAD
00010 #define PyObject_HEAD int _PyObjectHead;
00011 #endif
00012
00013 #include <glob.h>
00014 #include <dirent.h>
00015 #include <rpmio_internal.h>
00016
00017 #include <rpmlib.h>
00018
00019 #include "header-py.h"
00020 #include "rpmfd-py.h"
00021
00022 #include "debug.h"
00023
00024
00025
00026 extern int _rpmio_debug;
00027
00034 static PyObject *
00035 rpmfd_Debug( rpmfdObject * s, PyObject * args)
00036
00037
00038 {
00039 if (!PyArg_ParseTuple(args, "i", &_rpmio_debug)) return NULL;
00040 Py_INCREF(Py_None);
00041 return Py_None;
00042 }
00043
00046 typedef struct FDlist_t FDlist;
00047
00050 struct FDlist_t {
00051 FILE * f;
00052 FD_t fd;
00053 const char * note;
00054 FDlist * next;
00055 } ;
00056
00059 static FDlist *fdhead = NULL;
00060
00063 static FDlist *fdtail = NULL;
00064
00067 static int closeCallback(FILE * f)
00068
00069
00070 {
00071 FDlist *node, *last;
00072
00073 node = fdhead;
00074 last = NULL;
00075 while (node) {
00076 if (node->f == f)
00077 break;
00078 last = node;
00079 node = node->next;
00080 }
00081 if (node) {
00082 if (last)
00083 last->next = node->next;
00084 else
00085 fdhead = node->next;
00086 node->note = _free (node->note);
00087 node->fd = fdLink(node->fd, "closeCallback");
00088 Fclose (node->fd);
00089 while (node->fd)
00090 node->fd = fdFree(node->fd, "closeCallback");
00091 node = _free (node);
00092 }
00093 return 0;
00094 }
00095
00098 static PyObject *
00099 rpmfd_Fopen( PyObject * self, PyObject * args)
00100
00101
00102 {
00103 char * path, * mode;
00104 FDlist *node;
00105
00106 if (!PyArg_ParseTuple(args, "ss", &path, &mode))
00107 return NULL;
00108
00109 node = xmalloc (sizeof(FDlist));
00110
00111 node->fd = Fopen(path, mode);
00112 node->fd = fdLink(node->fd, "doFopen");
00113 node->note = xstrdup (path);
00114
00115 if (!node->fd) {
00116 PyErr_SetFromErrno(pyrpmError);
00117 node = _free (node);
00118 return NULL;
00119 }
00120
00121 if (Ferror(node->fd)) {
00122 const char *err = Fstrerror(node->fd);
00123 node = _free(node);
00124 if (err) {
00125 PyErr_SetString(pyrpmError, err);
00126 return NULL;
00127 }
00128 }
00129 node->f = fdGetFp(node->fd);
00130 if (!node->f) {
00131 PyErr_SetString(pyrpmError, "FD_t has no FILE*");
00132 free(node);
00133 return NULL;
00134 }
00135
00136 node->next = NULL;
00137 if (!fdhead) {
00138 fdhead = fdtail = node;
00139 } else if (fdtail) {
00140 fdtail->next = node;
00141 } else {
00142 fdhead = node;
00143 }
00144 fdtail = node;
00145
00146 return PyFile_FromFile (node->f, path, mode, closeCallback);
00147 }
00148
00151
00152
00153 static struct PyMethodDef rpmfd_methods[] = {
00154 {"Debug", (PyCFunction)rpmfd_Debug, METH_VARARGS,
00155 NULL},
00156 {"Fopen", (PyCFunction)rpmfd_Fopen, METH_VARARGS,
00157 NULL},
00158 {NULL, NULL}
00159 };
00160
00161
00162
00163
00166 static PyObject * rpmfd_getattr(rpmfdObject * o, char * name)
00167
00168 {
00169 return Py_FindMethod(rpmfd_methods, (PyObject *) o, name);
00170 }
00171
00174
00175 static char rpmfd_doc[] =
00176 "";
00177
00180
00181 PyTypeObject rpmfd_Type = {
00182 PyObject_HEAD_INIT(NULL)
00183 0,
00184 "rpm.fd",
00185 sizeof(rpmfdObject),
00186 0,
00187 (destructor)0,
00188 0,
00189 (getattrfunc) rpmfd_getattr,
00190 (setattrfunc)0,
00191 0,
00192 0,
00193 0,
00194 0,
00195 0,
00196 0,
00197 0,
00198 0,
00199 0,
00200 0,
00201 0,
00202 Py_TPFLAGS_DEFAULT,
00203 rpmfd_doc,
00204 #if Py_TPFLAGS_HAVE_ITER
00205 0,
00206 0,
00207 0,
00208 0,
00209 0,
00210 0,
00211 rpmfd_methods,
00212 0,
00213 0,
00214 0,
00215 0,
00216 0,
00217 0,
00218 0,
00219 0,
00220 0,
00221 0,
00222 0,
00223 0,
00224 #endif
00225 };
00226
00227
00228 rpmfdObject * rpmfd_Wrap(FD_t fd)
00229 {
00230 rpmfdObject *s = PyObject_NEW(rpmfdObject, &rpmfd_Type);
00231 if (s == NULL)
00232 return NULL;
00233 s->fd = fd;
00234 return s;
00235 }