f0cb94f4a520d7b544f5f9df871a78f50f5746e8
[platform/upstream/rpm.git] / python / rpmfi-py.c
1 /** \ingroup py_c
2  * \file python/rpmfi-py.c
3  */
4
5 #include "system.h"
6
7 #include <rpmlib.h>
8
9 #include "header-py.h"
10 #include "rpmfi-py.h"
11
12 #include "debug.h"
13
14 /*@access rpmfi @*/
15
16 /*@null@*/
17 static PyObject *
18 rpmfi_Debug(/*@unused@*/ rpmfiObject * s, PyObject * args, PyObject * kwds)
19         /*@globals _Py_NoneStruct @*/
20         /*@modifies _Py_NoneStruct @*/
21 {
22     char * kwlist[] = {"debugLevel", NULL};
23
24     if (!PyArg_ParseTupleAndKeywords(args, kwds, "i", kwlist, &_rpmfi_debug))
25         return NULL;
26
27     Py_INCREF(Py_None);
28     return Py_None;
29 }
30
31 /*@null@*/
32 static PyObject *
33 rpmfi_FC(rpmfiObject * s)
34         /*@*/
35 {
36     return Py_BuildValue("i", rpmfiFC(s->fi));
37 }
38
39 /*@null@*/
40 static PyObject *
41 rpmfi_FX(rpmfiObject * s)
42         /*@*/
43 {
44     return Py_BuildValue("i", rpmfiFX(s->fi));
45 }
46
47 /*@null@*/
48 static PyObject *
49 rpmfi_DC(rpmfiObject * s)
50         /*@*/
51 {
52     return Py_BuildValue("i", rpmfiDC(s->fi));
53 }
54
55 /*@null@*/
56 static PyObject *
57 rpmfi_DX(rpmfiObject * s)
58         /*@*/
59 {
60     return Py_BuildValue("i", rpmfiDX(s->fi));
61 }
62
63 /*@null@*/
64 static PyObject *
65 rpmfi_BN(rpmfiObject * s)
66         /*@*/
67 {
68     return Py_BuildValue("s", xstrdup(rpmfiBN(s->fi)));
69 }
70
71 /*@null@*/
72 static PyObject *
73 rpmfi_DN(rpmfiObject * s)
74         /*@*/
75 {
76     return Py_BuildValue("s", xstrdup(rpmfiDN(s->fi)));
77 }
78
79 /*@null@*/
80 static PyObject *
81 rpmfi_FN(rpmfiObject * s)
82         /*@modifies s @*/
83 {
84     return Py_BuildValue("s", xstrdup(rpmfiFN(s->fi)));
85 }
86
87 /*@null@*/
88 static PyObject *
89 rpmfi_FFlags(rpmfiObject * s)
90         /*@*/
91 {
92     return Py_BuildValue("i", rpmfiFFlags(s->fi));
93 }
94
95 /*@null@*/
96 static PyObject *
97 rpmfi_VFlags(rpmfiObject * s)
98         /*@*/
99 {
100     return Py_BuildValue("i", rpmfiVFlags(s->fi));
101 }
102
103 /*@null@*/
104 static PyObject *
105 rpmfi_FMode(rpmfiObject * s)
106         /*@*/
107 {
108     return Py_BuildValue("i", rpmfiFMode(s->fi));
109 }
110
111 /*@null@*/
112 static PyObject *
113 rpmfi_FState(rpmfiObject * s)
114         /*@*/
115 {
116     return Py_BuildValue("i", rpmfiFState(s->fi));
117 }
118
119 /* XXX rpmfiMD5 */
120 /*@null@*/
121 static PyObject *
122 rpmfi_MD5(rpmfiObject * s)
123         /*@*/
124 {
125     const unsigned char * MD5;
126     char fmd5[33];
127     char * t;
128     int i;
129
130     MD5 = rpmfiMD5(s->fi);
131     t = fmd5;
132     if (MD5 != NULL)
133     for (i = 0; i < 16; i++, t += 2)
134         sprintf(t, "%02x", MD5[i]);
135     *t = '\0';
136     return Py_BuildValue("s", xstrdup(fmd5));
137 }
138
139 /*@null@*/
140 static PyObject *
141 rpmfi_FLink(rpmfiObject * s)
142         /*@*/
143 {
144     return Py_BuildValue("s", xstrdup(rpmfiFLink(s->fi)));
145 }
146
147 /*@null@*/
148 static PyObject *
149 rpmfi_FSize(rpmfiObject * s)
150         /*@*/
151 {
152     return Py_BuildValue("i", rpmfiFSize(s->fi));
153 }
154
155 /*@null@*/
156 static PyObject *
157 rpmfi_FRdev(rpmfiObject * s)
158         /*@*/
159 {
160     return Py_BuildValue("i", rpmfiFRdev(s->fi));
161 }
162
163 /*@null@*/
164 static PyObject *
165 rpmfi_FMtime(rpmfiObject * s)
166         /*@*/
167 {
168     return Py_BuildValue("i", rpmfiFMtime(s->fi));
169 }
170
171 /*@null@*/
172 static PyObject *
173 rpmfi_FUser(rpmfiObject * s)
174         /*@*/
175 {
176     return Py_BuildValue("s", xstrdup(rpmfiFUser(s->fi)));
177 }
178
179 /*@null@*/
180 static PyObject *
181 rpmfi_FGroup(rpmfiObject * s)
182         /*@*/
183 {
184     return Py_BuildValue("s", xstrdup(rpmfiFGroup(s->fi)));
185 }
186
187 /*@null@*/
188 static PyObject *
189 rpmfi_FColor(rpmfiObject * s)
190         /*@*/
191 {
192     return Py_BuildValue("i", rpmfiFColor(s->fi));
193 }
194
195 /*@null@*/
196 static PyObject *
197 rpmfi_FClass(rpmfiObject * s)
198         /*@*/
199 {
200     const char * FClass;
201
202     if ((FClass = rpmfiFClass(s->fi)) == NULL)
203         FClass = "";
204     return Py_BuildValue("s", xstrdup(FClass));
205 }
206
207 #if Py_TPFLAGS_HAVE_ITER
208 static PyObject *
209 rpmfi_iter(rpmfiObject * s)
210         /*@*/
211 {
212     Py_INCREF(s);
213     return (PyObject *)s;
214 }
215 #endif
216
217 /*@null@*/
218 static PyObject *
219 rpmfi_iternext(rpmfiObject * s)
220         /*@globals _Py_NoneStruct @*/
221         /*@modifies s, _Py_NoneStruct @*/
222 {
223     PyObject * result = NULL;
224
225     /* Reset loop indices on 1st entry. */
226     if (!s->active) {
227         s->fi = rpmfiInit(s->fi, 0);
228         s->active = 1;
229     }
230
231     /* If more to do, return the file tuple. */
232     if (rpmfiNext(s->fi) >= 0) {
233         const char * FN = rpmfiFN(s->fi);
234         int FSize = rpmfiFSize(s->fi);
235         int FMode = rpmfiFMode(s->fi);
236         int FMtime = rpmfiFMtime(s->fi);
237         int FFlags = rpmfiFFlags(s->fi);
238         int FRdev = rpmfiFRdev(s->fi);
239         int FInode = rpmfiFInode(s->fi);
240         int FNlink = rpmfiFNlink(s->fi);
241         int FState = rpmfiFState(s->fi);
242         int VFlags = rpmfiVFlags(s->fi);
243         const char * FUser = rpmfiFUser(s->fi);
244         const char * FGroup = rpmfiFGroup(s->fi);
245 /*@-shadow@*/
246         const unsigned char * MD5 = rpmfiMD5(s->fi), *s = MD5;
247 /*@=shadow@*/
248         char FMD5[2*16+1], *t = FMD5;
249         static const char hex[] = "0123456789abcdef";
250         int gotMD5, i;
251
252         gotMD5 = 0;
253         if (s)
254         for (i = 0; i < 16; i++) {
255             gotMD5 |= *s;
256             *t++ = hex[ (*s >> 4) & 0xf ];
257             *t++ = hex[ (*s++   ) & 0xf ];
258         }
259         *t = '\0';
260
261         result = PyTuple_New(13);
262         if (FN == NULL) {
263             Py_INCREF(Py_None);
264             PyTuple_SET_ITEM(result, 0, Py_None);
265         } else
266             PyTuple_SET_ITEM(result,  0, Py_BuildValue("s", FN));
267         PyTuple_SET_ITEM(result,  1, PyInt_FromLong(FSize));
268         PyTuple_SET_ITEM(result,  2, PyInt_FromLong(FMode));
269         PyTuple_SET_ITEM(result,  3, PyInt_FromLong(FMtime));
270         PyTuple_SET_ITEM(result,  4, PyInt_FromLong(FFlags));
271         PyTuple_SET_ITEM(result,  5, PyInt_FromLong(FRdev));
272         PyTuple_SET_ITEM(result,  6, PyInt_FromLong(FInode));
273         PyTuple_SET_ITEM(result,  7, PyInt_FromLong(FNlink));
274         PyTuple_SET_ITEM(result,  8, PyInt_FromLong(FState));
275         PyTuple_SET_ITEM(result,  9, PyInt_FromLong(VFlags));
276         if (FUser == NULL) {
277             Py_INCREF(Py_None);
278             PyTuple_SET_ITEM(result, 10, Py_None);
279         } else
280             PyTuple_SET_ITEM(result, 10, Py_BuildValue("s", FUser));
281         if (FGroup == NULL) {
282             Py_INCREF(Py_None);
283             PyTuple_SET_ITEM(result, 11, Py_None);
284         } else
285             PyTuple_SET_ITEM(result, 11, Py_BuildValue("s", FGroup));
286         if (!gotMD5) {
287             Py_INCREF(Py_None);
288             PyTuple_SET_ITEM(result, 12, Py_None);
289         } else
290             PyTuple_SET_ITEM(result, 12, Py_BuildValue("s", FMD5));
291
292     } else
293         s->active = 0;
294
295     return result;
296 }
297
298 static PyObject *
299 rpmfi_Next(rpmfiObject * s)
300         /*@globals _Py_NoneStruct @*/
301         /*@modifies s, _Py_NoneStruct @*/
302 {
303     PyObject * result = NULL;
304
305     result = rpmfi_iternext(s);
306
307     if (result == NULL) {
308         Py_INCREF(Py_None);
309         return Py_None;
310     }
311
312     return result;
313 }
314
315 #ifdef  NOTYET
316 /*@null@*/
317 static PyObject *
318 rpmfi_NextD(rpmfiObject * s)
319         /*@*/
320 {
321         Py_INCREF(Py_None);
322         return Py_None;
323 }
324
325 /*@null@*/
326 static PyObject *
327 rpmfi_InitD(rpmfiObject * s)
328         /*@*/
329 {
330         Py_INCREF(Py_None);
331         return Py_None;
332 }
333 #endif
334
335 /*@-fullinitblock@*/
336 /*@unchecked@*/ /*@observer@*/
337 static struct PyMethodDef rpmfi_methods[] = {
338  {"Debug",      (PyCFunction)rpmfi_Debug,       METH_VARARGS|METH_KEYWORDS,
339         NULL},
340  {"FC",         (PyCFunction)rpmfi_FC,          METH_NOARGS,
341         NULL},
342  {"FX",         (PyCFunction)rpmfi_FX,          METH_NOARGS,
343         NULL},
344  {"DC",         (PyCFunction)rpmfi_DC,          METH_NOARGS,
345         NULL},
346  {"DX",         (PyCFunction)rpmfi_DX,          METH_NOARGS,
347         NULL},
348  {"BN",         (PyCFunction)rpmfi_BN,          METH_NOARGS,
349         NULL},
350  {"DN",         (PyCFunction)rpmfi_DN,          METH_NOARGS,
351         NULL},
352  {"FN",         (PyCFunction)rpmfi_FN,          METH_NOARGS,
353         NULL},
354  {"FFlags",     (PyCFunction)rpmfi_FFlags,      METH_NOARGS,
355         NULL},
356  {"VFlags",     (PyCFunction)rpmfi_VFlags,      METH_NOARGS,
357         NULL},
358  {"FMode",      (PyCFunction)rpmfi_FMode,       METH_NOARGS,
359         NULL},
360  {"FState",     (PyCFunction)rpmfi_FState,      METH_NOARGS,
361         NULL},
362  {"MD5",        (PyCFunction)rpmfi_MD5,         METH_NOARGS,
363         NULL},
364  {"FLink",      (PyCFunction)rpmfi_FLink,       METH_NOARGS,
365         NULL},
366  {"FSize",      (PyCFunction)rpmfi_FSize,       METH_NOARGS,
367         NULL},
368  {"FRdev",      (PyCFunction)rpmfi_FRdev,       METH_NOARGS,
369         NULL},
370  {"FMtime",     (PyCFunction)rpmfi_FMtime,      METH_NOARGS,
371         NULL},
372  {"FUser",      (PyCFunction)rpmfi_FUser,       METH_NOARGS,
373         NULL},
374  {"FGroup",     (PyCFunction)rpmfi_FGroup,      METH_NOARGS,
375         NULL},
376  {"FColor",     (PyCFunction)rpmfi_FColor,      METH_NOARGS,
377         NULL},
378  {"FClass",     (PyCFunction)rpmfi_FClass,      METH_NOARGS,
379         NULL},
380  {"next",       (PyCFunction)rpmfi_Next,        METH_NOARGS,
381 "fi.next() -> (FN, FSize, FMode, FMtime, FFlags, FRdev, FInode, FNlink, FState, VFlags, FUser, FGroup, FMD5))\n\
382 - Retrieve next file info tuple.\n" },
383 #ifdef  NOTYET
384  {"NextD",      (PyCFunction)rpmfi_NextD,       METH_NOARGS,
385         NULL},
386  {"InitD",      (PyCFunction)rpmfi_InitD,       METH_NOARGS,
387         NULL},
388 #endif
389  {NULL,         NULL}           /* sentinel */
390 };
391 /*@=fullinitblock@*/
392
393 /* ---------- */
394
395 static void
396 rpmfi_dealloc(/*@only@*/ /*@null@*/ rpmfiObject * s)
397         /*@modifies s @*/
398 {
399     if (s) {
400         s->fi = rpmfiFree(s->fi);
401         PyObject_Del(s);
402     }
403 }
404
405 static int
406 rpmfi_print(rpmfiObject * s, FILE * fp, /*@unused@*/ int flags)
407         /*@globals fileSystem @*/
408         /*@modifies s, fp, fileSystem @*/
409 {
410     if (!(s && s->fi))
411         return -1;
412
413     s->fi = rpmfiInit(s->fi, 0);
414     while (rpmfiNext(s->fi) >= 0)
415         fprintf(fp, "%s\n", rpmfiFN(s->fi));
416     return 0;
417 }
418
419 static PyObject * rpmfi_getattro(PyObject * o, PyObject * n)
420         /*@*/
421 {
422     return PyObject_GenericGetAttr(o, n);
423 }
424
425 static int rpmfi_setattro(PyObject * o, PyObject * n, PyObject * v)
426         /*@*/
427 {
428     return PyObject_GenericSetAttr(o, n, v);
429 }
430
431 static int
432 rpmfi_length(rpmfiObject * s)
433         /*@*/
434 {
435     return rpmfiFC(s->fi);
436 }
437
438 /*@null@*/
439 static PyObject *
440 rpmfi_subscript(rpmfiObject * s, PyObject * key)
441         /*@modifies s @*/
442 {
443     int ix;
444
445     if (!PyInt_Check(key)) {
446         PyErr_SetString(PyExc_TypeError, "integer expected");
447         return NULL;
448     }
449
450     ix = (int) PyInt_AsLong(key);
451     rpmfiSetFX(s->fi, ix);
452     return Py_BuildValue("s", xstrdup(rpmfiFN(s->fi)));
453 }
454
455 /*@unchecked@*/ /*@observer@*/
456 static PyMappingMethods rpmfi_as_mapping = {
457         (inquiry) rpmfi_length,         /* mp_length */
458         (binaryfunc) rpmfi_subscript,   /* mp_subscript */
459         (objobjargproc)0,               /* mp_ass_subscript */
460 };
461
462 /** \ingroup py_c
463  */
464 static int rpmfi_init(rpmfiObject * s, PyObject *args, PyObject *kwds)
465         /*@globals rpmGlobalMacroContext @*/
466         /*@modifies s, rpmGlobalMacroContext @*/
467 {
468     hdrObject * ho = NULL;
469     PyObject * to = NULL;
470     rpmts ts = NULL;    /* XXX FIXME: fiFromHeader should be a ts method. */
471     int tagN = RPMTAG_BASENAMES;
472     int flags = 0;
473     char * kwlist[] = {"header", "tag", "flags", NULL};
474
475 if (_rpmfi_debug < 0)
476 fprintf(stderr, "*** rpmfi_init(%p,%p,%p)\n", s, args, kwds);
477
478     if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!|Oi:rpmfi_init", kwlist,
479             &hdr_Type, &ho, &to, &flags))
480         return -1;
481
482     if (to != NULL) {
483         tagN = tagNumFromPyObject(to);
484         if (tagN == -1) {
485             PyErr_SetString(PyExc_KeyError, "unknown header tag");
486             return -1;
487         }
488     }
489     s->fi = rpmfiNew(ts, hdrGetHeader(ho), tagN, flags);
490     s->active = 0;
491
492     return 0;
493 }
494
495 /** \ingroup py_c
496  */
497 static void rpmfi_free(/*@only@*/ rpmfiObject * s)
498         /*@modifies s @*/
499 {
500 if (_rpmfi_debug)
501 fprintf(stderr, "%p -- fi %p\n", s, s->fi);
502     s->fi = rpmfiFree(s->fi);
503
504     PyObject_Del((PyObject *)s);
505 }
506
507 /** \ingroup py_c
508  */
509 static PyObject * rpmfi_alloc(PyTypeObject * subtype, int nitems)
510         /*@*/
511 {
512     PyObject * s = PyType_GenericAlloc(subtype, nitems);
513
514 if (_rpmfi_debug < 0)
515 fprintf(stderr, "*** rpmfi_alloc(%p,%d) ret %p\n", subtype, nitems, s);
516     return s;
517 }
518
519 /** \ingroup py_c
520  */
521 /*@null@*/
522 static PyObject * rpmfi_new(PyTypeObject * subtype, PyObject *args, PyObject *kwds)
523         /*@globals rpmGlobalMacroContext @*/
524         /*@modifies rpmGlobalMacroContext @*/
525 {
526     rpmfiObject * s = (void *) PyObject_New(rpmfiObject, subtype);
527
528     /* Perform additional initialization. */
529     if (rpmfi_init(s, args, kwds) < 0) {
530         rpmfi_free(s);
531         return NULL;
532     }
533
534 if (_rpmfi_debug)
535 fprintf(stderr, "%p ++ fi %p\n", s, s->fi);
536
537     return (PyObject *)s;
538 }
539
540 /**
541  */
542 /*@unchecked@*/ /*@observer@*/
543 static char rpmfi_doc[] =
544 "";
545
546 /*@-fullinitblock@*/
547 PyTypeObject rpmfi_Type = {
548         PyObject_HEAD_INIT(&PyType_Type)
549         0,                              /* ob_size */
550         "rpm.fi",                       /* tp_name */
551         sizeof(rpmfiObject),            /* tp_basicsize */
552         0,                              /* tp_itemsize */
553         /* methods */
554         (destructor) rpmfi_dealloc,     /* tp_dealloc */
555         (printfunc) rpmfi_print,        /* tp_print */
556         (getattrfunc)0,                 /* tp_getattr */
557         (setattrfunc)0,                 /* tp_setattr */
558         (cmpfunc)0,                     /* tp_compare */
559         (reprfunc)0,                    /* tp_repr */
560         0,                              /* tp_as_number */
561         0,                              /* tp_as_sequence */
562         &rpmfi_as_mapping,              /* tp_as_mapping */
563         (hashfunc)0,                    /* tp_hash */
564         (ternaryfunc)0,                 /* tp_call */
565         (reprfunc)0,                    /* tp_str */
566         (getattrofunc) rpmfi_getattro,  /* tp_getattro */
567         (setattrofunc) rpmfi_setattro,  /* tp_setattro */
568         0,                              /* tp_as_buffer */
569         Py_TPFLAGS_DEFAULT,             /* tp_flags */
570         rpmfi_doc,                      /* tp_doc */
571 #if Py_TPFLAGS_HAVE_ITER
572         0,                              /* tp_traverse */
573         0,                              /* tp_clear */
574         0,                              /* tp_richcompare */
575         0,                              /* tp_weaklistoffset */
576         (getiterfunc) rpmfi_iter,       /* tp_iter */
577         (iternextfunc) rpmfi_iternext,  /* tp_iternext */
578         rpmfi_methods,                  /* tp_methods */
579         0,                              /* tp_members */
580         0,                              /* tp_getset */
581         0,                              /* tp_base */
582         0,                              /* tp_dict */
583         0,                              /* tp_descr_get */
584         0,                              /* tp_descr_set */
585         0,                              /* tp_dictoffset */
586         (initproc) rpmfi_init,          /* tp_init */
587         (allocfunc) rpmfi_alloc,        /* tp_alloc */
588         (newfunc) rpmfi_new,            /* tp_new */
589         (freefunc) rpmfi_free,          /* tp_free */
590         0,                              /* tp_is_gc */
591 #endif
592 };
593 /*@=fullinitblock@*/
594
595 /* ---------- */
596
597 rpmfi fiFromFi(rpmfiObject * s)
598 {
599     return s->fi;
600 }
601
602 rpmfiObject *
603 rpmfi_Wrap(rpmfi fi)
604 {
605     rpmfiObject *s = PyObject_New(rpmfiObject, &rpmfi_Type);
606
607     if (s == NULL)
608         return NULL;
609     s->fi = fi;
610     s->active = 0;
611     return s;
612 }
613
614 rpmfiObject *
615 hdr_fiFromHeader(PyObject * s, PyObject * args, PyObject * kwds)
616 {
617     hdrObject * ho = (hdrObject *)s;
618     PyObject * to = NULL;
619     rpmts ts = NULL;    /* XXX FIXME: fiFromHeader should be a ts method. */
620     rpmTag tagN = RPMTAG_BASENAMES;
621     int flags = 0;
622     char * kwlist[] = {"tag", "flags", NULL};
623
624     if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:fiFromHeader", kwlist,
625             &to, &flags))
626         return NULL;
627
628     if (to != NULL) {
629         tagN = tagNumFromPyObject(to);
630         if (tagN == -1) {
631             PyErr_SetString(PyExc_KeyError, "unknown header tag");
632             return NULL;
633         }
634     }
635     return rpmfi_Wrap( rpmfiNew(ts, hdrGetHeader(ho), tagN, flags) );
636 }