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