Merge changes from rpm-4.2.1 development.
[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 "Python.h"
8 #ifdef __LCLINT__
9 #undef  PyObject_HEAD
10 #define PyObject_HEAD   int _PyObjectHead;
11 #endif
12
13 #include <rpmlib.h>
14
15 #include "header-py.h"
16 #include "rpmfi-py.h"
17
18 #include "debug.h"
19
20 /*@access rpmfi @*/
21
22 static PyObject *
23 rpmfi_Debug(/*@unused@*/ rpmfiObject * s, PyObject * args)
24         /*@globals _Py_NoneStruct @*/
25         /*@modifies _Py_NoneStruct @*/
26 {
27     if (!PyArg_ParseTuple(args, "i", &_rpmfi_debug)) return NULL;
28     Py_INCREF(Py_None);
29     return Py_None;
30 }
31
32 static PyObject *
33 rpmfi_FC(rpmfiObject * s, PyObject * args)
34         /*@*/
35 {
36     if (!PyArg_ParseTuple(args, ":FC")) return NULL;
37     return Py_BuildValue("i", rpmfiFC(s->fi));
38 }
39
40 static PyObject *
41 rpmfi_FX(rpmfiObject * s, PyObject * args)
42         /*@*/
43 {
44     if (!PyArg_ParseTuple(args, ":FX")) return NULL;
45     return Py_BuildValue("i", rpmfiFX(s->fi));
46 }
47
48 static PyObject *
49 rpmfi_DC(rpmfiObject * s, PyObject * args)
50         /*@*/
51 {
52     if (!PyArg_ParseTuple(args, ":DC")) return NULL;
53     return Py_BuildValue("i", rpmfiDC(s->fi));
54 }
55
56 static PyObject *
57 rpmfi_DX(rpmfiObject * s, PyObject * args)
58         /*@*/
59 {
60     if (!PyArg_ParseTuple(args, ":DX")) return NULL;
61     return Py_BuildValue("i", rpmfiDX(s->fi));
62 }
63
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 static PyObject *
73 rpmfi_DN(rpmfiObject * s, PyObject * args)
74         /*@*/
75 {
76     if (!PyArg_ParseTuple(args, ":DN")) return NULL;
77     return Py_BuildValue("s", xstrdup(rpmfiDN(s->fi)));
78 }
79
80 static PyObject *
81 rpmfi_FN(rpmfiObject * s, PyObject * args)
82         /*@modifies s @*/
83 {
84     if (!PyArg_ParseTuple(args, ":FN")) return NULL;
85     return Py_BuildValue("s", xstrdup(rpmfiFN(s->fi)));
86 }
87
88 static PyObject *
89 rpmfi_FFlags(rpmfiObject * s, PyObject * args)
90         /*@*/
91 {
92     if (!PyArg_ParseTuple(args, ":FFlags")) return NULL;
93     return Py_BuildValue("i", rpmfiFFlags(s->fi));
94 }
95
96 static PyObject *
97 rpmfi_VFlags(rpmfiObject * s, PyObject * args)
98         /*@*/
99 {
100     if (!PyArg_ParseTuple(args, ":VFlags")) return NULL;
101     return Py_BuildValue("i", rpmfiVFlags(s->fi));
102 }
103
104 static PyObject *
105 rpmfi_FMode(rpmfiObject * s, PyObject * args)
106         /*@*/
107 {
108     if (!PyArg_ParseTuple(args, ":FMode")) return NULL;
109     return Py_BuildValue("i", rpmfiFMode(s->fi));
110 }
111
112 static PyObject *
113 rpmfi_FState(rpmfiObject * s, PyObject * args)
114         /*@*/
115 {
116     if (!PyArg_ParseTuple(args, ":FState")) return NULL;
117     return Py_BuildValue("i", rpmfiFState(s->fi));
118 }
119
120 /* XXX rpmfiMD5 */
121 static PyObject *
122 rpmfi_MD5(rpmfiObject * s, PyObject * args)
123         /*@*/
124 {
125     const unsigned char * MD5;
126     char fmd5[33];
127     char * t;
128     int i;
129
130     if (!PyArg_ParseTuple(args, ":MD5")) return NULL;
131     MD5 = rpmfiMD5(s->fi);
132     for (i = 0, t = fmd5; i < 16; i++, t += 2)
133         sprintf(t, "%02x", MD5[i]);
134     *t = '\0';
135     return Py_BuildValue("s", xstrdup(fmd5));
136 }
137
138 static PyObject *
139 rpmfi_FLink(rpmfiObject * s, PyObject * args)
140         /*@*/
141 {
142     if (!PyArg_ParseTuple(args, ":FLink")) return NULL;
143     return Py_BuildValue("s", xstrdup(rpmfiFLink(s->fi)));
144 }
145
146 static PyObject *
147 rpmfi_FSize(rpmfiObject * s, PyObject * args)
148         /*@*/
149 {
150     if (!PyArg_ParseTuple(args, ":FSize")) return NULL;
151     return Py_BuildValue("i", rpmfiFSize(s->fi));
152 }
153
154 static PyObject *
155 rpmfi_FRdev(rpmfiObject * s, PyObject * args)
156         /*@*/
157 {
158     if (!PyArg_ParseTuple(args, ":FRdev")) return NULL;
159     return Py_BuildValue("i", rpmfiFRdev(s->fi));
160 }
161
162 static PyObject *
163 rpmfi_FMtime(rpmfiObject * s, PyObject * args)
164         /*@*/
165 {
166     if (!PyArg_ParseTuple(args, ":FMtime")) return NULL;
167     return Py_BuildValue("i", rpmfiFMtime(s->fi));
168 }
169
170 static PyObject *
171 rpmfi_FUser(rpmfiObject * s, PyObject * args)
172         /*@*/
173 {
174     if (!PyArg_ParseTuple(args, ":FUser")) return NULL;
175     return Py_BuildValue("s", xstrdup(rpmfiFUser(s->fi)));
176 }
177
178 static PyObject *
179 rpmfi_FGroup(rpmfiObject * s, PyObject * args)
180         /*@*/
181 {
182     if (!PyArg_ParseTuple(args, ":FGroup")) return NULL;
183     return Py_BuildValue("s", xstrdup(rpmfiFGroup(s->fi)));
184 }
185
186 static PyObject *
187 rpmfi_FColor(rpmfiObject * s, PyObject * args)
188         /*@*/
189 {
190     if (!PyArg_ParseTuple(args, ":FColor")) return NULL;
191     return Py_BuildValue("i", rpmfiFColor(s->fi));
192 }
193
194 static PyObject *
195 rpmfi_FClass(rpmfiObject * s, PyObject * args)
196         /*@*/
197 {
198     const char * FClass;
199
200     if (!PyArg_ParseTuple(args, ":FClass")) return NULL;
201     if ((FClass = rpmfiFClass(s->fi)) == NULL)
202         FClass = "";
203     return Py_BuildValue("s", xstrdup(FClass));
204 }
205
206 #if Py_TPFLAGS_HAVE_ITER
207 static PyObject *
208 rpmfi_iter(rpmfiObject * s, /*@unused@*/ PyObject * args)
209         /*@*/
210 {
211     Py_INCREF(s);
212     return (PyObject *)s;
213 }
214 #endif
215
216 static PyObject *
217 rpmfi_iternext(rpmfiObject * s)
218         /*@globals _Py_NoneStruct @*/
219         /*@modifies s, _Py_NoneStruct @*/
220 {
221     PyObject * result = NULL;
222
223     /* Reset loop indices on 1st entry. */
224     if (!s->active) {
225         s->fi = rpmfiInit(s->fi, 0);
226         s->active = 1;
227     }
228
229     /* If more to do, return the file tuple. */
230     if (rpmfiNext(s->fi) >= 0) {
231         const char * FN = rpmfiFN(s->fi);
232         int FSize = rpmfiFSize(s->fi);
233         int FMode = rpmfiFMode(s->fi);
234         int FMtime = rpmfiFMtime(s->fi);
235         int FFlags = rpmfiFFlags(s->fi);
236         int FRdev = rpmfiFRdev(s->fi);
237         int FInode = rpmfiFInode(s->fi);
238         int FNlink = rpmfiFNlink(s->fi);
239         int FState = rpmfiFState(s->fi);
240         int VFlags = rpmfiVFlags(s->fi);
241         const char * FUser = rpmfiFUser(s->fi);
242         const char * FGroup = rpmfiFGroup(s->fi);
243 /*@-shadow@*/
244         const unsigned char * MD5 = rpmfiMD5(s->fi), *s = MD5;
245 /*@=shadow@*/
246         char FMD5[2*16+1], *t = FMD5;
247         static const char hex[] = "0123456789abcdef";
248         int gotMD5, i;
249
250         gotMD5 = 0;
251         if (s)
252         for (i = 0; i < 16; i++) {
253             gotMD5 |= *s;
254             *t++ = hex[ (*s >> 4) & 0xf ];
255             *t++ = hex[ (*s++   ) & 0xf ];
256         }
257         *t = '\0';
258
259         result = PyTuple_New(13);
260         if (FN == NULL) {
261             Py_INCREF(Py_None);
262             PyTuple_SET_ITEM(result, 0, Py_None);
263         } else
264             PyTuple_SET_ITEM(result,  0, Py_BuildValue("s", FN));
265         PyTuple_SET_ITEM(result,  1, PyInt_FromLong(FSize));
266         PyTuple_SET_ITEM(result,  2, PyInt_FromLong(FMode));
267         PyTuple_SET_ITEM(result,  3, PyInt_FromLong(FMtime));
268         PyTuple_SET_ITEM(result,  4, PyInt_FromLong(FFlags));
269         PyTuple_SET_ITEM(result,  5, PyInt_FromLong(FRdev));
270         PyTuple_SET_ITEM(result,  6, PyInt_FromLong(FInode));
271         PyTuple_SET_ITEM(result,  7, PyInt_FromLong(FNlink));
272         PyTuple_SET_ITEM(result,  8, PyInt_FromLong(FState));
273         PyTuple_SET_ITEM(result,  9, PyInt_FromLong(VFlags));
274         if (FUser == NULL) {
275             Py_INCREF(Py_None);
276             PyTuple_SET_ITEM(result, 10, Py_None);
277         } else
278             PyTuple_SET_ITEM(result, 10, Py_BuildValue("s", FUser));
279         if (FGroup == NULL) {
280             Py_INCREF(Py_None);
281             PyTuple_SET_ITEM(result, 11, Py_None);
282         } else
283             PyTuple_SET_ITEM(result, 11, Py_BuildValue("s", FGroup));
284         if (!gotMD5) {
285             Py_INCREF(Py_None);
286             PyTuple_SET_ITEM(result, 12, Py_None);
287         } else
288             PyTuple_SET_ITEM(result, 12, Py_BuildValue("s", FMD5));
289
290     } else
291         s->active = 0;
292
293     return result;
294 }
295
296 static PyObject *
297 rpmfi_Next(rpmfiObject * s, /*@unused@*/ PyObject * args)
298         /*@globals _Py_NoneStruct @*/
299         /*@modifies s, _Py_NoneStruct @*/
300 {
301     PyObject * result = NULL;
302
303     result = rpmfi_iternext(s);
304
305     if (result == NULL) {
306         Py_INCREF(Py_None);
307         return Py_None;
308     }
309
310     return result;
311 }
312
313 #ifdef  NOTYET
314 static PyObject *
315 rpmfi_NextD(rpmfiObject * s, PyObject * args)
316         /*@*/
317 {
318         if (!PyArg_ParseTuple(args, ":NextD"))
319                 return NULL;
320         Py_INCREF(Py_None);
321         return Py_None;
322 }
323
324 static PyObject *
325 rpmfi_InitD(rpmfiObject * s, PyObject * args)
326         /*@*/
327 {
328         if (!PyArg_ParseTuple(args, ":InitD"))
329                 return NULL;
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,
339         NULL},
340  {"FC",         (PyCFunction)rpmfi_FC,          METH_VARARGS,
341         NULL},
342  {"FX",         (PyCFunction)rpmfi_FX,          METH_VARARGS,
343         NULL},
344  {"DC",         (PyCFunction)rpmfi_DC,          METH_VARARGS,
345         NULL},
346  {"DX",         (PyCFunction)rpmfi_DX,          METH_VARARGS,
347         NULL},
348  {"BN",         (PyCFunction)rpmfi_BN,          METH_VARARGS,
349         NULL},
350  {"DN",         (PyCFunction)rpmfi_DN,          METH_VARARGS,
351         NULL},
352  {"FN",         (PyCFunction)rpmfi_FN,          METH_VARARGS,
353         NULL},
354  {"FFlags",     (PyCFunction)rpmfi_FFlags,      METH_VARARGS,
355         NULL},
356  {"VFlags",     (PyCFunction)rpmfi_VFlags,      METH_VARARGS,
357         NULL},
358  {"FMode",      (PyCFunction)rpmfi_FMode,       METH_VARARGS,
359         NULL},
360  {"FState",     (PyCFunction)rpmfi_FState,      METH_VARARGS,
361         NULL},
362  {"MD5",        (PyCFunction)rpmfi_MD5,         METH_VARARGS,
363         NULL},
364  {"FLink",      (PyCFunction)rpmfi_FLink,       METH_VARARGS,
365         NULL},
366  {"FSize",      (PyCFunction)rpmfi_FSize,       METH_VARARGS,
367         NULL},
368  {"FRdev",      (PyCFunction)rpmfi_FRdev,       METH_VARARGS,
369         NULL},
370  {"FMtime",     (PyCFunction)rpmfi_FMtime,      METH_VARARGS,
371         NULL},
372  {"FUser",      (PyCFunction)rpmfi_FUser,       METH_VARARGS,
373         NULL},
374  {"FGroup",     (PyCFunction)rpmfi_FGroup,      METH_VARARGS,
375         NULL},
376  {"FColor",     (PyCFunction)rpmfi_FColor,      METH_VARARGS,
377         NULL},
378  {"FClass",     (PyCFunction)rpmfi_FClass,      METH_VARARGS,
379         NULL},
380  {"next",       (PyCFunction)rpmfi_Next,        METH_VARARGS,
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_VARARGS,
385         NULL},
386  {"InitD",      (PyCFunction)rpmfi_InitD,       METH_VARARGS,
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 static PyObject *
439 rpmfi_subscript(rpmfiObject * s, PyObject * key)
440         /*@modifies s @*/
441 {
442     int ix;
443
444     if (!PyInt_Check(key)) {
445         PyErr_SetString(PyExc_TypeError, "integer expected");
446         return NULL;
447     }
448
449     ix = (int) PyInt_AsLong(key);
450     rpmfiSetFX(s->fi, ix);
451     return Py_BuildValue("s", xstrdup(rpmfiFN(s->fi)));
452 }
453
454 /*@unchecked@*/ /*@observer@*/
455 static PyMappingMethods rpmfi_as_mapping = {
456         (inquiry) rpmfi_length,         /* mp_length */
457         (binaryfunc) rpmfi_subscript,   /* mp_subscript */
458         (objobjargproc)0,               /* mp_ass_subscript */
459 };
460
461 /**
462  */
463 /*@unchecked@*/ /*@observer@*/
464 static char rpmfi_doc[] =
465 "";
466
467 /*@-fullinitblock@*/
468 PyTypeObject rpmfi_Type = {
469         PyObject_HEAD_INIT(&PyType_Type)
470         0,                              /* ob_size */
471         "rpm.fi",                       /* tp_name */
472         sizeof(rpmfiObject),            /* tp_basicsize */
473         0,                              /* tp_itemsize */
474         /* methods */
475         (destructor) rpmfi_dealloc,     /* tp_dealloc */
476         (printfunc) rpmfi_print,        /* tp_print */
477         (getattrfunc)0,                 /* tp_getattr */
478         (setattrfunc)0,                 /* tp_setattr */
479         (cmpfunc)0,                     /* tp_compare */
480         (reprfunc)0,                    /* tp_repr */
481         0,                              /* tp_as_number */
482         0,                              /* tp_as_sequence */
483         &rpmfi_as_mapping,              /* tp_as_mapping */
484         (hashfunc)0,                    /* tp_hash */
485         (ternaryfunc)0,                 /* tp_call */
486         (reprfunc)0,                    /* tp_str */
487         (getattrofunc) rpmfi_getattro,  /* tp_getattro */
488         (setattrofunc) rpmfi_setattro,  /* tp_setattro */
489         0,                              /* tp_as_buffer */
490         Py_TPFLAGS_DEFAULT,             /* tp_flags */
491         rpmfi_doc,                      /* tp_doc */
492 #if Py_TPFLAGS_HAVE_ITER
493         0,                              /* tp_traverse */
494         0,                              /* tp_clear */
495         0,                              /* tp_richcompare */
496         0,                              /* tp_weaklistoffset */
497         (getiterfunc) rpmfi_iter,       /* tp_iter */
498         (iternextfunc) rpmfi_iternext,  /* tp_iternext */
499         rpmfi_methods,                  /* tp_methods */
500         0,                              /* tp_members */
501         0,                              /* tp_getset */
502         0,                              /* tp_base */
503         0,                              /* tp_dict */
504         0,                              /* tp_descr_get */
505         0,                              /* tp_descr_set */
506         0,                              /* tp_dictoffset */
507         0,                              /* tp_init */
508         0,                              /* tp_alloc */
509         0,                              /* tp_new */
510         0,                              /* tp_free */
511         0,                              /* tp_is_gc */
512 #endif
513 };
514 /*@=fullinitblock@*/
515
516 /* ---------- */
517
518 rpmfi fiFromFi(rpmfiObject * s)
519 {
520     return s->fi;
521 }
522
523 rpmfiObject *
524 rpmfi_Wrap(rpmfi fi)
525 {
526     rpmfiObject *s = PyObject_New(rpmfiObject, &rpmfi_Type);
527
528     if (s == NULL)
529         return NULL;
530     s->fi = fi;
531     s->active = 0;
532     return s;
533 }
534
535 rpmfiObject *
536 hdr_fiFromHeader(PyObject * s, PyObject * args)
537 {
538     hdrObject * ho = (hdrObject *)s;
539     PyObject * to = NULL;
540     rpmts ts = NULL;    /* XXX FIXME: fiFromHeader should be a ts method. */
541     rpmTag tagN = RPMTAG_BASENAMES;
542     int scareMem = 0;
543
544     if (!PyArg_ParseTuple(args, "|O:fiFromHeader", &to))
545         return NULL;
546     if (to != NULL) {
547         tagN = tagNumFromPyObject(to);
548         if (tagN == -1) {
549             PyErr_SetString(PyExc_KeyError, "unknown header tag");
550             return NULL;
551         }
552     }
553     return rpmfi_Wrap( rpmfiNew(ts, hdrGetHeader(ho), tagN, scareMem) );
554 }