Main Page Modules Data Structures File List Data Fields Globals Related Pages

# python/rpmfd-py.c

Go to the documentation of this file.

    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>       /* XXX rpmio.h */
    00014 #include <dirent.h>     /* XXX rpmio.h */
    00015 #include <rpmio_internal.h>
    00016
    00017 #include <rpmlib.h>     /* XXX _free */
    00018
    00019 #include "header-py.h"  /* XXX pyrpmError */
    00020 #include "rpmfd-py.h"
    00021
    00022 #include "debug.h"
    00023
    00024 /*@access FD_t @*/
    00025
    00026 extern int _rpmio_debug;
    00027
    00034 static PyObject *
    00035 rpmfd_Debug(/*@unused@*/ rpmfdObject * s, PyObject * args)
    00036         /*@globals _Py_NoneStruct @*/
    00037         /*@modifies _Py_NoneStruct @*/
    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         /*@globals fdhead @*/
    00069         /*@modifies fdhead @*/
    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(/*@unused@*/ PyObject * self, PyObject * args)
    00100         /*@globals fdhead, fdtail @*/
    00101         /*@modifies fdhead, fdtail @*/
    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 /*@-fullinitblock@*/
    00152 /*@unchecked@*/ /*@observer@*/
    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}           /* sentinel */
    00159 };
    00160 /*@=fullinitblock@*/
    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 /*@unchecked@*/ /*@observer@*/
    00175 static char rpmfd_doc[] =
    00176 "";
    00177
    00180 /*@-fullinitblock@*/
    00181 PyTypeObject rpmfd_Type = {
    00182         PyObject_HEAD_INIT(NULL)
    00183         0,                              /* ob_size */
    00184         "rpm.fd",                       /* tp_name */
    00185         sizeof(rpmfdObject),            /* tp_size */
    00186         0,                              /* tp_itemsize */
    00187         (destructor)0,                  /* tp_dealloc */
    00188         0,                              /* tp_print */
    00189         (getattrfunc) rpmfd_getattr,    /* tp_getattr */
    00190         (setattrfunc)0,                 /* tp_setattr */
    00191         0,                              /* tp_compare */
    00192         0,                              /* tp_repr */
    00193         0,                              /* tp_as_number */
    00194         0,                              /* tp_as_sequence */
    00195         0,                              /* tp_as_mapping */
    00196         0,                              /* tp_hash */
    00197         0,                              /* tp_call */
    00198         0,                              /* tp_str */
    00199         0,                              /* tp_getattro */
    00200         0,                              /* tp_setattro */
    00201         0,                              /* tp_as_buffer */
    00202         Py_TPFLAGS_DEFAULT,             /* tp_flags */
    00203         rpmfd_doc,                      /* tp_doc */
    00204 #if Py_TPFLAGS_HAVE_ITER
    00205         0,                              /* tp_traverse */
    00206         0,                              /* tp_clear */
    00207         0,                              /* tp_richcompare */
    00208         0,                              /* tp_weaklistoffset */
    00209         0,                              /* tp_iter */
    00210         0,                              /* tp_iternext */
    00211         rpmfd_methods,                  /* tp_methods */
    00212         0,                              /* tp_members */
    00213         0,                              /* tp_getset */
    00214         0,                              /* tp_base */
    00215         0,                              /* tp_dict */
    00216         0,                              /* tp_descr_get */
    00217         0,                              /* tp_descr_set */
    00218         0,                              /* tp_dictoffset */
    00219         0,                              /* tp_init */
    00220         0,                              /* tp_alloc */
    00221         0,                              /* tp_new */
    00222         0,                              /* tp_free */
    00223         0,                              /* tp_is_gc */
    00224 #endif
    00225 };
    00226 /*@=fullinitblock@*/
    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 }