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

python/rpmfi-py.c

Go to the documentation of this file.
00001 
00005 #include "system.h"
00006 
00007 #include <rpmlib.h>
00008 
00009 #include "header-py.h"
00010 #include "rpmfi-py.h"
00011 
00012 #include "debug.h"
00013 
00014 /*@access rpmfi @*/
00015 
00016 #if Py_TPFLAGS_HAVE_ITER
00017 static PyObject *
00018 rpmfi_iter(rpmfiObject * s)
00019         /*@*/
00020 {
00021     Py_INCREF(s);
00022     return (PyObject *)s;
00023 }
00024 #endif
00025 
00026 /*@null@*/
00027 static PyObject *
00028 rpmfi_iternext(rpmfiObject * s)
00029         /*@globals _Py_NoneStruct @*/
00030         /*@modifies s, _Py_NoneStruct @*/
00031 {
00032     PyObject * result = NULL;
00033 
00034     /* Reset loop indices on 1st entry. */
00035     if (!s->active) {
00036         s->fi = rpmfiInit(s->fi, 0);
00037         s->active = 1;
00038     }
00039 
00040     /* If more to do, return the file tuple. */
00041     if (rpmfiNext(s->fi) >= 0) {
00042         const char * FN = rpmfiFN(s->fi);
00043         int FSize = rpmfiFSize(s->fi);
00044         int FMode = rpmfiFMode(s->fi);
00045         int FMtime = rpmfiFMtime(s->fi);
00046         int FFlags = rpmfiFFlags(s->fi);
00047         int FRdev = rpmfiFRdev(s->fi);
00048         int FInode = rpmfiFInode(s->fi);
00049         int FNlink = rpmfiFNlink(s->fi);
00050         int FState = rpmfiFState(s->fi);
00051         int VFlags = rpmfiVFlags(s->fi);
00052         const char * FUser = rpmfiFUser(s->fi);
00053         const char * FGroup = rpmfiFGroup(s->fi);
00054 /*@-shadow@*/
00055         int dalgo = 0;
00056         size_t dlen = 0;
00057         const unsigned char * digest = rpmfiDigest(s->fi, &dalgo, &dlen);
00058         const unsigned char * s = digest;
00059 /*@=shadow@*/
00060         const char * fdigest;
00061         char * t;
00062         static const char hex[] = "0123456789abcdef";
00063         int gotMD5, i;
00064 
00065         fdigest = t = memset(alloca(dlen), 0, dlen);
00066         gotMD5 = 0;
00067         if (s)
00068         for (i = 0; i < 16; i++) {
00069             gotMD5 |= *s;
00070             *t++ = hex[ (*s >> 4) & 0xf ];
00071             *t++ = hex[ (*s++   ) & 0xf ];
00072         }
00073         *t = '\0';
00074 
00075         result = PyTuple_New(13);
00076         if (FN == NULL) {
00077             Py_INCREF(Py_None);
00078             PyTuple_SET_ITEM(result, 0, Py_None);
00079         } else
00080             PyTuple_SET_ITEM(result,  0, Py_BuildValue("s", FN));
00081         PyTuple_SET_ITEM(result,  1, PyInt_FromLong(FSize));
00082         PyTuple_SET_ITEM(result,  2, PyInt_FromLong(FMode));
00083         PyTuple_SET_ITEM(result,  3, PyInt_FromLong(FMtime));
00084         PyTuple_SET_ITEM(result,  4, PyInt_FromLong(FFlags));
00085         PyTuple_SET_ITEM(result,  5, PyInt_FromLong(FRdev));
00086         PyTuple_SET_ITEM(result,  6, PyInt_FromLong(FInode));
00087         PyTuple_SET_ITEM(result,  7, PyInt_FromLong(FNlink));
00088         PyTuple_SET_ITEM(result,  8, PyInt_FromLong(FState));
00089         PyTuple_SET_ITEM(result,  9, PyInt_FromLong(VFlags));
00090         if (FUser == NULL) {
00091             Py_INCREF(Py_None);
00092             PyTuple_SET_ITEM(result, 10, Py_None);
00093         } else
00094             PyTuple_SET_ITEM(result, 10, Py_BuildValue("s", FUser));
00095         if (FGroup == NULL) {
00096             Py_INCREF(Py_None);
00097             PyTuple_SET_ITEM(result, 11, Py_None);
00098         } else
00099             PyTuple_SET_ITEM(result, 11, Py_BuildValue("s", FGroup));
00100         if (!gotMD5) {
00101             Py_INCREF(Py_None);
00102             PyTuple_SET_ITEM(result, 12, Py_None);
00103         } else
00104             PyTuple_SET_ITEM(result, 12, Py_BuildValue("s", fdigest));
00105 
00106     } else
00107         s->active = 0;
00108 
00109     return result;
00110 }
00111 
00116 
00117 static PyObject *
00118 rpmfi_Next(rpmfiObject * s)
00119         /*@globals _Py_NoneStruct @*/
00120         /*@modifies s, _Py_NoneStruct @*/
00121 {
00122     PyObject * result = NULL;
00123 
00124     result = rpmfi_iternext(s);
00125 
00126     if (result == NULL) {
00127         Py_INCREF(Py_None);
00128         return Py_None;
00129     }
00130 
00131     return result;
00132 }
00133 
00134 #ifdef  NOTYET
00135 /*@null@*/
00136 static PyObject *
00137 rpmfi_NextD(rpmfiObject * s)
00138         /*@*/
00139 {
00140         Py_INCREF(Py_None);
00141         return Py_None;
00142 }
00143 
00144 /*@null@*/
00145 static PyObject *
00146 rpmfi_InitD(rpmfiObject * s)
00147         /*@*/
00148 {
00149         Py_INCREF(Py_None);
00150         return Py_None;
00151 }
00152 #endif
00153 
00154 /*@null@*/
00155 static PyObject *
00156 rpmfi_Debug(/*@unused@*/ rpmfiObject * s, PyObject * args, PyObject * kwds)
00157         /*@globals _Py_NoneStruct @*/
00158         /*@modifies _Py_NoneStruct @*/
00159 {
00160     char * kwlist[] = {"debugLevel", NULL};
00161 
00162     if (!PyArg_ParseTupleAndKeywords(args, kwds, "i", kwlist, &_rpmfi_debug))
00163         return NULL;
00164 
00165     Py_INCREF(Py_None);
00166     return Py_None;
00167 }
00168 
00169 /*@null@*/
00170 static PyObject *
00171 rpmfi_FC(rpmfiObject * s)
00172         /*@*/
00173 {
00174     return Py_BuildValue("i", rpmfiFC(s->fi));
00175 }
00176 
00177 /*@null@*/
00178 static PyObject *
00179 rpmfi_FX(rpmfiObject * s)
00180         /*@*/
00181 {
00182     return Py_BuildValue("i", rpmfiFX(s->fi));
00183 }
00184 
00185 /*@null@*/
00186 static PyObject *
00187 rpmfi_DC(rpmfiObject * s)
00188         /*@*/
00189 {
00190     return Py_BuildValue("i", rpmfiDC(s->fi));
00191 }
00192 
00193 /*@null@*/
00194 static PyObject *
00195 rpmfi_DX(rpmfiObject * s)
00196         /*@*/
00197 {
00198     return Py_BuildValue("i", rpmfiDX(s->fi));
00199 }
00200 
00201 /*@null@*/
00202 static PyObject *
00203 rpmfi_BN(rpmfiObject * s)
00204         /*@*/
00205 {
00206     return Py_BuildValue("s", xstrdup(rpmfiBN(s->fi)));
00207 }
00208 
00209 /*@null@*/
00210 static PyObject *
00211 rpmfi_DN(rpmfiObject * s)
00212         /*@*/
00213 {
00214     return Py_BuildValue("s", xstrdup(rpmfiDN(s->fi)));
00215 }
00216 
00217 /*@null@*/
00218 static PyObject *
00219 rpmfi_FN(rpmfiObject * s)
00220         /*@modifies s @*/
00221 {
00222     return Py_BuildValue("s", xstrdup(rpmfiFN(s->fi)));
00223 }
00224 
00225 /*@null@*/
00226 static PyObject *
00227 rpmfi_FFlags(rpmfiObject * s)
00228         /*@*/
00229 {
00230     return Py_BuildValue("i", rpmfiFFlags(s->fi));
00231 }
00232 
00233 /*@null@*/
00234 static PyObject *
00235 rpmfi_VFlags(rpmfiObject * s)
00236         /*@*/
00237 {
00238     return Py_BuildValue("i", rpmfiVFlags(s->fi));
00239 }
00240 
00241 /*@null@*/
00242 static PyObject *
00243 rpmfi_FMode(rpmfiObject * s)
00244         /*@*/
00245 {
00246     return Py_BuildValue("i", rpmfiFMode(s->fi));
00247 }
00248 
00249 /*@null@*/
00250 static PyObject *
00251 rpmfi_FState(rpmfiObject * s)
00252         /*@*/
00253 {
00254     return Py_BuildValue("i", rpmfiFState(s->fi));
00255 }
00256 
00257 /* XXX rpmfiMD5 */
00258 /*@null@*/
00259 static PyObject *
00260 rpmfi_MD5(rpmfiObject * s)
00261         /*@*/
00262 {
00263     int dalgo = 0;
00264     size_t dlen = 0;
00265     const unsigned char * digest;
00266     const char * fdigest;
00267     char * t;
00268     int i;
00269 
00270     digest = rpmfiDigest(s->fi, &dalgo, &dlen);
00271     if (digest == NULL || dlen == 0)
00272         return NULL;
00273     fdigest = t = memset(alloca(dlen), 0, dlen);
00274     for (i = 0; i < dlen; i++, t += 2)
00275         sprintf(t, "%02x", digest[i]);
00276     *t = '\0';
00277     return Py_BuildValue("s", xstrdup(fdigest));
00278 }
00279 
00280 /*@null@*/
00281 static PyObject *
00282 rpmfi_FLink(rpmfiObject * s)
00283         /*@*/
00284 {
00285     return Py_BuildValue("s", xstrdup(rpmfiFLink(s->fi)));
00286 }
00287 
00288 /*@null@*/
00289 static PyObject *
00290 rpmfi_FSize(rpmfiObject * s)
00291         /*@*/
00292 {
00293     return Py_BuildValue("i", rpmfiFSize(s->fi));
00294 }
00295 
00296 /*@null@*/
00297 static PyObject *
00298 rpmfi_FRdev(rpmfiObject * s)
00299         /*@*/
00300 {
00301     return Py_BuildValue("i", rpmfiFRdev(s->fi));
00302 }
00303 
00304 /*@null@*/
00305 static PyObject *
00306 rpmfi_FMtime(rpmfiObject * s)
00307         /*@*/
00308 {
00309     return Py_BuildValue("i", rpmfiFMtime(s->fi));
00310 }
00311 
00312 /*@null@*/
00313 static PyObject *
00314 rpmfi_FUser(rpmfiObject * s)
00315         /*@*/
00316 {
00317     return Py_BuildValue("s", xstrdup(rpmfiFUser(s->fi)));
00318 }
00319 
00320 /*@null@*/
00321 static PyObject *
00322 rpmfi_FGroup(rpmfiObject * s)
00323         /*@*/
00324 {
00325     return Py_BuildValue("s", xstrdup(rpmfiFGroup(s->fi)));
00326 }
00327 
00328 /*@null@*/
00329 static PyObject *
00330 rpmfi_FColor(rpmfiObject * s)
00331         /*@*/
00332 {
00333     return Py_BuildValue("i", rpmfiFColor(s->fi));
00334 }
00335 
00336 /*@null@*/
00337 static PyObject *
00338 rpmfi_FClass(rpmfiObject * s)
00339         /*@*/
00340 {
00341     const char * FClass;
00342 
00343     if ((FClass = rpmfiFClass(s->fi)) == NULL)
00344         FClass = "";
00345     return Py_BuildValue("s", xstrdup(FClass));
00346 }
00347 
00350 /*@-fullinitblock@*/
00351 /*@unchecked@*/ /*@observer@*/
00352 static struct PyMethodDef rpmfi_methods[] = {
00353  {"Debug",      (PyCFunction)rpmfi_Debug,       METH_VARARGS|METH_KEYWORDS,
00354         NULL},
00355  {"FC",         (PyCFunction)rpmfi_FC,          METH_NOARGS,
00356         NULL},
00357  {"FX",         (PyCFunction)rpmfi_FX,          METH_NOARGS,
00358         NULL},
00359  {"DC",         (PyCFunction)rpmfi_DC,          METH_NOARGS,
00360         NULL},
00361  {"DX",         (PyCFunction)rpmfi_DX,          METH_NOARGS,
00362         NULL},
00363  {"BN",         (PyCFunction)rpmfi_BN,          METH_NOARGS,
00364         NULL},
00365  {"DN",         (PyCFunction)rpmfi_DN,          METH_NOARGS,
00366         NULL},
00367  {"FN",         (PyCFunction)rpmfi_FN,          METH_NOARGS,
00368         NULL},
00369  {"FFlags",     (PyCFunction)rpmfi_FFlags,      METH_NOARGS,
00370         NULL},
00371  {"VFlags",     (PyCFunction)rpmfi_VFlags,      METH_NOARGS,
00372         NULL},
00373  {"FMode",      (PyCFunction)rpmfi_FMode,       METH_NOARGS,
00374         NULL},
00375  {"FState",     (PyCFunction)rpmfi_FState,      METH_NOARGS,
00376         NULL},
00377  {"MD5",        (PyCFunction)rpmfi_MD5,         METH_NOARGS,
00378         NULL},
00379  {"FLink",      (PyCFunction)rpmfi_FLink,       METH_NOARGS,
00380         NULL},
00381  {"FSize",      (PyCFunction)rpmfi_FSize,       METH_NOARGS,
00382         NULL},
00383  {"FRdev",      (PyCFunction)rpmfi_FRdev,       METH_NOARGS,
00384         NULL},
00385  {"FMtime",     (PyCFunction)rpmfi_FMtime,      METH_NOARGS,
00386         NULL},
00387  {"FUser",      (PyCFunction)rpmfi_FUser,       METH_NOARGS,
00388         NULL},
00389  {"FGroup",     (PyCFunction)rpmfi_FGroup,      METH_NOARGS,
00390         NULL},
00391  {"FColor",     (PyCFunction)rpmfi_FColor,      METH_NOARGS,
00392         NULL},
00393  {"FClass",     (PyCFunction)rpmfi_FClass,      METH_NOARGS,
00394         NULL},
00395  {"next",       (PyCFunction)rpmfi_Next,        METH_NOARGS,
00396 "fi.next() -> (FN, FSize, FMode, FMtime, FFlags, FRdev, FInode, FNlink, FState, VFlags, FUser, FGroup, FMD5))\n\
00397 - Retrieve next file info tuple.\n" },
00398 #ifdef  NOTYET
00399  {"NextD",      (PyCFunction)rpmfi_NextD,       METH_NOARGS,
00400         NULL},
00401  {"InitD",      (PyCFunction)rpmfi_InitD,       METH_NOARGS,
00402         NULL},
00403 #endif
00404  {NULL,         NULL}           /* sentinel */
00405 };
00406 /*@=fullinitblock@*/
00407 
00408 /* ---------- */
00409 
00410 static void
00411 rpmfi_dealloc(/*@only@*/ /*@null@*/ rpmfiObject * s)
00412         /*@modifies s @*/
00413 {
00414     if (s) {
00415         s->fi = rpmfiFree(s->fi);
00416         PyObject_Del(s);
00417     }
00418 }
00419 
00420 static int
00421 rpmfi_print(rpmfiObject * s, FILE * fp, /*@unused@*/ int flags)
00422         /*@globals fileSystem @*/
00423         /*@modifies s, fp, fileSystem @*/
00424 {
00425     if (!(s && s->fi))
00426         return -1;
00427 
00428     s->fi = rpmfiInit(s->fi, 0);
00429     while (rpmfiNext(s->fi) >= 0)
00430         fprintf(fp, "%s\n", rpmfiFN(s->fi));
00431     return 0;
00432 }
00433 
00434 static PyObject * rpmfi_getattro(PyObject * o, PyObject * n)
00435         /*@*/
00436 {
00437     return PyObject_GenericGetAttr(o, n);
00438 }
00439 
00440 static int rpmfi_setattro(PyObject * o, PyObject * n, PyObject * v)
00441         /*@*/
00442 {
00443     return PyObject_GenericSetAttr(o, n, v);
00444 }
00445 
00446 static int
00447 rpmfi_length(rpmfiObject * s)
00448         /*@*/
00449 {
00450     return rpmfiFC(s->fi);
00451 }
00452 
00453 /*@null@*/
00454 static PyObject *
00455 rpmfi_subscript(rpmfiObject * s, PyObject * key)
00456         /*@modifies s @*/
00457 {
00458     int ix;
00459 
00460     if (!PyInt_Check(key)) {
00461         PyErr_SetString(PyExc_TypeError, "integer expected");
00462         return NULL;
00463     }
00464 
00465     ix = (int) PyInt_AsLong(key);
00466     rpmfiSetFX(s->fi, ix);
00467     return Py_BuildValue("s", xstrdup(rpmfiFN(s->fi)));
00468 }
00469 
00470 /*@unchecked@*/ /*@observer@*/
00471 static PyMappingMethods rpmfi_as_mapping = {
00472         (inquiry) rpmfi_length,         /* mp_length */
00473         (binaryfunc) rpmfi_subscript,   /* mp_subscript */
00474         (objobjargproc)0,               /* mp_ass_subscript */
00475 };
00476 
00479 static int rpmfi_init(rpmfiObject * s, PyObject *args, PyObject *kwds)
00480         /*@globals rpmGlobalMacroContext @*/
00481         /*@modifies s, rpmGlobalMacroContext @*/
00482 {
00483     hdrObject * ho = NULL;
00484     PyObject * to = NULL;
00485     rpmts ts = NULL;    /* XXX FIXME: fiFromHeader should be a ts method. */
00486     int tagN = RPMTAG_BASENAMES;
00487     int flags = 0;
00488     char * kwlist[] = {"header", "tag", "flags", NULL};
00489 
00490 if (_rpmfi_debug < 0)
00491 fprintf(stderr, "*** rpmfi_init(%p,%p,%p)\n", s, args, kwds);
00492 
00493     if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!|Oi:rpmfi_init", kwlist,
00494             &hdr_Type, &ho, &to, &flags))
00495         return -1;
00496 
00497     if (to != NULL) {
00498         tagN = tagNumFromPyObject(to);
00499         if (tagN == -1) {
00500             PyErr_SetString(PyExc_KeyError, "unknown header tag");
00501             return -1;
00502         }
00503     }
00504     s->fi = rpmfiNew(ts, hdrGetHeader(ho), tagN, flags);
00505     s->active = 0;
00506 
00507     return 0;
00508 }
00509 
00512 static void rpmfi_free(/*@only@*/ rpmfiObject * s)
00513         /*@modifies s @*/
00514 {
00515 if (_rpmfi_debug)
00516 fprintf(stderr, "%p -- fi %p\n", s, s->fi);
00517     s->fi = rpmfiFree(s->fi);
00518 
00519     PyObject_Del((PyObject *)s);
00520 }
00521 
00524 static PyObject * rpmfi_alloc(PyTypeObject * subtype, int nitems)
00525         /*@*/
00526 {
00527     PyObject * s = PyType_GenericAlloc(subtype, nitems);
00528 
00529 if (_rpmfi_debug < 0)
00530 fprintf(stderr, "*** rpmfi_alloc(%p,%d) ret %p\n", subtype, nitems, s);
00531     return s;
00532 }
00533 
00536 /*@null@*/
00537 static PyObject * rpmfi_new(PyTypeObject * subtype, PyObject *args, PyObject *kwds)
00538         /*@globals rpmGlobalMacroContext @*/
00539         /*@modifies rpmGlobalMacroContext @*/
00540 {
00541     rpmfiObject * s = (void *) PyObject_New(rpmfiObject, subtype);
00542 
00543     /* Perform additional initialization. */
00544     if (rpmfi_init(s, args, kwds) < 0) {
00545         rpmfi_free(s);
00546         return NULL;
00547     }
00548 
00549 if (_rpmfi_debug)
00550 fprintf(stderr, "%p ++ fi %p\n", s, s->fi);
00551 
00552     return (PyObject *)s;
00553 }
00554 
00557 /*@unchecked@*/ /*@observer@*/
00558 static char rpmfi_doc[] =
00559 "";
00560 
00561 /*@-fullinitblock@*/
00562 PyTypeObject rpmfi_Type = {
00563         PyObject_HEAD_INIT(&PyType_Type)
00564         0,                              /* ob_size */
00565         "rpm.fi",                       /* tp_name */
00566         sizeof(rpmfiObject),            /* tp_basicsize */
00567         0,                              /* tp_itemsize */
00568         /* methods */
00569         (destructor) rpmfi_dealloc,     /* tp_dealloc */
00570         (printfunc) rpmfi_print,        /* tp_print */
00571         (getattrfunc)0,                 /* tp_getattr */
00572         (setattrfunc)0,                 /* tp_setattr */
00573         (cmpfunc)0,                     /* tp_compare */
00574         (reprfunc)0,                    /* tp_repr */
00575         0,                              /* tp_as_number */
00576         0,                              /* tp_as_sequence */
00577         &rpmfi_as_mapping,              /* tp_as_mapping */
00578         (hashfunc)0,                    /* tp_hash */
00579         (ternaryfunc)0,                 /* tp_call */
00580         (reprfunc)0,                    /* tp_str */
00581         (getattrofunc) rpmfi_getattro,  /* tp_getattro */
00582         (setattrofunc) rpmfi_setattro,  /* tp_setattro */
00583         0,                              /* tp_as_buffer */
00584         Py_TPFLAGS_DEFAULT,             /* tp_flags */
00585         rpmfi_doc,                      /* tp_doc */
00586 #if Py_TPFLAGS_HAVE_ITER
00587         0,                              /* tp_traverse */
00588         0,                              /* tp_clear */
00589         0,                              /* tp_richcompare */
00590         0,                              /* tp_weaklistoffset */
00591         (getiterfunc) rpmfi_iter,       /* tp_iter */
00592         (iternextfunc) rpmfi_iternext,  /* tp_iternext */
00593         rpmfi_methods,                  /* tp_methods */
00594         0,                              /* tp_members */
00595         0,                              /* tp_getset */
00596         0,                              /* tp_base */
00597         0,                              /* tp_dict */
00598         0,                              /* tp_descr_get */
00599         0,                              /* tp_descr_set */
00600         0,                              /* tp_dictoffset */
00601         (initproc) rpmfi_init,          /* tp_init */
00602         (allocfunc) rpmfi_alloc,        /* tp_alloc */
00603         (newfunc) rpmfi_new,            /* tp_new */
00604         rpmfi_free,                     /* tp_free */
00605         0,                              /* tp_is_gc */
00606 #endif
00607 };
00608 /*@=fullinitblock@*/
00609 
00610 /* ---------- */
00611 
00612 rpmfi fiFromFi(rpmfiObject * s)
00613 {
00614     return s->fi;
00615 }
00616 
00617 rpmfiObject *
00618 rpmfi_Wrap(rpmfi fi)
00619 {
00620     rpmfiObject *s = PyObject_New(rpmfiObject, &rpmfi_Type);
00621 
00622     if (s == NULL)
00623         return NULL;
00624     s->fi = fi;
00625     s->active = 0;
00626     return s;
00627 }
00628 
00629 rpmfiObject *
00630 hdr_fiFromHeader(PyObject * s, PyObject * args, PyObject * kwds)
00631 {
00632     hdrObject * ho = (hdrObject *)s;
00633     PyObject * to = NULL;
00634     rpmts ts = NULL;    /* XXX FIXME: fiFromHeader should be a ts method. */
00635     rpmTag tagN = RPMTAG_BASENAMES;
00636     int flags = 0;
00637     char * kwlist[] = {"tag", "flags", NULL};
00638 
00639     if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:fiFromHeader", kwlist,
00640             &to, &flags))
00641         return NULL;
00642 
00643     if (to != NULL) {
00644         tagN = tagNumFromPyObject(to);
00645         if (tagN == -1) {
00646             PyErr_SetString(PyExc_KeyError, "unknown header tag");
00647             return NULL;
00648         }
00649     }
00650     return rpmfi_Wrap( rpmfiNew(ts, hdrGetHeader(ho), tagN, flags) );
00651 }

Generated on Tue Dec 27 22:20:13 2016 for rpm by  doxygen 1.4.4