- python: include Python.h before glibc features.h.
[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     if (MD5 != NULL)
140     for (i = 0, t = fmd5; i < 16; i++, t += 2)
141         sprintf(t, "%02x", MD5[i]);
142     *t = '\0';
143     return Py_BuildValue("s", xstrdup(fmd5));
144 }
145
146 /*@null@*/
147 static PyObject *
148 rpmfi_FLink(rpmfiObject * s, PyObject * args)
149         /*@*/
150 {
151     if (!PyArg_ParseTuple(args, ":FLink")) return NULL;
152     return Py_BuildValue("s", xstrdup(rpmfiFLink(s->fi)));
153 }
154
155 /*@null@*/
156 static PyObject *
157 rpmfi_FSize(rpmfiObject * s, PyObject * args)
158         /*@*/
159 {
160     if (!PyArg_ParseTuple(args, ":FSize")) return NULL;
161     return Py_BuildValue("i", rpmfiFSize(s->fi));
162 }
163
164 /*@null@*/
165 static PyObject *
166 rpmfi_FRdev(rpmfiObject * s, PyObject * args)
167         /*@*/
168 {
169     if (!PyArg_ParseTuple(args, ":FRdev")) return NULL;
170     return Py_BuildValue("i", rpmfiFRdev(s->fi));
171 }
172
173 /*@null@*/
174 static PyObject *
175 rpmfi_FMtime(rpmfiObject * s, PyObject * args)
176         /*@*/
177 {
178     if (!PyArg_ParseTuple(args, ":FMtime")) return NULL;
179     return Py_BuildValue("i", rpmfiFMtime(s->fi));
180 }
181
182 /*@null@*/
183 static PyObject *
184 rpmfi_FUser(rpmfiObject * s, PyObject * args)
185         /*@*/
186 {
187     if (!PyArg_ParseTuple(args, ":FUser")) return NULL;
188     return Py_BuildValue("s", xstrdup(rpmfiFUser(s->fi)));
189 }
190
191 /*@null@*/
192 static PyObject *
193 rpmfi_FGroup(rpmfiObject * s, PyObject * args)
194         /*@*/
195 {
196     if (!PyArg_ParseTuple(args, ":FGroup")) return NULL;
197     return Py_BuildValue("s", xstrdup(rpmfiFGroup(s->fi)));
198 }
199
200 /*@null@*/
201 static PyObject *
202 rpmfi_FColor(rpmfiObject * s, PyObject * args)
203         /*@*/
204 {
205     if (!PyArg_ParseTuple(args, ":FColor")) return NULL;
206     return Py_BuildValue("i", rpmfiFColor(s->fi));
207 }
208
209 /*@null@*/
210 static PyObject *
211 rpmfi_FClass(rpmfiObject * s, PyObject * args)
212         /*@*/
213 {
214     const char * FClass;
215
216     if (!PyArg_ParseTuple(args, ":FClass")) return NULL;
217     if ((FClass = rpmfiFClass(s->fi)) == NULL)
218         FClass = "";
219     return Py_BuildValue("s", xstrdup(FClass));
220 }
221
222 #if Py_TPFLAGS_HAVE_ITER
223 static PyObject *
224 rpmfi_iter(rpmfiObject * s, /*@unused@*/ PyObject * args)
225         /*@*/
226 {
227     Py_INCREF(s);
228     return (PyObject *)s;
229 }
230 #endif
231
232 /*@null@*/
233 static PyObject *
234 rpmfi_iternext(rpmfiObject * s)
235         /*@globals _Py_NoneStruct @*/
236         /*@modifies s, _Py_NoneStruct @*/
237 {
238     PyObject * result = NULL;
239
240     /* Reset loop indices on 1st entry. */
241     if (!s->active) {
242         s->fi = rpmfiInit(s->fi, 0);
243         s->active = 1;
244     }
245
246     /* If more to do, return the file tuple. */
247     if (rpmfiNext(s->fi) >= 0) {
248         const char * FN = rpmfiFN(s->fi);
249         int FSize = rpmfiFSize(s->fi);
250         int FMode = rpmfiFMode(s->fi);
251         int FMtime = rpmfiFMtime(s->fi);
252         int FFlags = rpmfiFFlags(s->fi);
253         int FRdev = rpmfiFRdev(s->fi);
254         int FInode = rpmfiFInode(s->fi);
255         int FNlink = rpmfiFNlink(s->fi);
256         int FState = rpmfiFState(s->fi);
257         int VFlags = rpmfiVFlags(s->fi);
258         const char * FUser = rpmfiFUser(s->fi);
259         const char * FGroup = rpmfiFGroup(s->fi);
260 /*@-shadow@*/
261         const unsigned char * MD5 = rpmfiMD5(s->fi), *s = MD5;
262 /*@=shadow@*/
263         char FMD5[2*16+1], *t = FMD5;
264         static const char hex[] = "0123456789abcdef";
265         int gotMD5, i;
266
267         gotMD5 = 0;
268         if (s)
269         for (i = 0; i < 16; i++) {
270             gotMD5 |= *s;
271             *t++ = hex[ (*s >> 4) & 0xf ];
272             *t++ = hex[ (*s++   ) & 0xf ];
273         }
274         *t = '\0';
275
276         result = PyTuple_New(13);
277         if (FN == NULL) {
278             Py_INCREF(Py_None);
279             PyTuple_SET_ITEM(result, 0, Py_None);
280         } else
281             PyTuple_SET_ITEM(result,  0, Py_BuildValue("s", FN));
282         PyTuple_SET_ITEM(result,  1, PyInt_FromLong(FSize));
283         PyTuple_SET_ITEM(result,  2, PyInt_FromLong(FMode));
284         PyTuple_SET_ITEM(result,  3, PyInt_FromLong(FMtime));
285         PyTuple_SET_ITEM(result,  4, PyInt_FromLong(FFlags));
286         PyTuple_SET_ITEM(result,  5, PyInt_FromLong(FRdev));
287         PyTuple_SET_ITEM(result,  6, PyInt_FromLong(FInode));
288         PyTuple_SET_ITEM(result,  7, PyInt_FromLong(FNlink));
289         PyTuple_SET_ITEM(result,  8, PyInt_FromLong(FState));
290         PyTuple_SET_ITEM(result,  9, PyInt_FromLong(VFlags));
291         if (FUser == NULL) {
292             Py_INCREF(Py_None);
293             PyTuple_SET_ITEM(result, 10, Py_None);
294         } else
295             PyTuple_SET_ITEM(result, 10, Py_BuildValue("s", FUser));
296         if (FGroup == NULL) {
297             Py_INCREF(Py_None);
298             PyTuple_SET_ITEM(result, 11, Py_None);
299         } else
300             PyTuple_SET_ITEM(result, 11, Py_BuildValue("s", FGroup));
301         if (!gotMD5) {
302             Py_INCREF(Py_None);
303             PyTuple_SET_ITEM(result, 12, Py_None);
304         } else
305             PyTuple_SET_ITEM(result, 12, Py_BuildValue("s", FMD5));
306
307     } else
308         s->active = 0;
309
310     return result;
311 }
312
313 static PyObject *
314 rpmfi_Next(rpmfiObject * s, /*@unused@*/ PyObject * args)
315         /*@globals _Py_NoneStruct @*/
316         /*@modifies s, _Py_NoneStruct @*/
317 {
318     PyObject * result = NULL;
319
320     result = rpmfi_iternext(s);
321
322     if (result == NULL) {
323         Py_INCREF(Py_None);
324         return Py_None;
325     }
326
327     return result;
328 }
329
330 #ifdef  NOTYET
331 /*@null@*/
332 static PyObject *
333 rpmfi_NextD(rpmfiObject * s, PyObject * args)
334         /*@*/
335 {
336         if (!PyArg_ParseTuple(args, ":NextD"))
337                 return NULL;
338         Py_INCREF(Py_None);
339         return Py_None;
340 }
341
342 /*@null@*/
343 static PyObject *
344 rpmfi_InitD(rpmfiObject * s, PyObject * args)
345         /*@*/
346 {
347         if (!PyArg_ParseTuple(args, ":InitD"))
348                 return NULL;
349         Py_INCREF(Py_None);
350         return Py_None;
351 }
352 #endif
353
354 /*@-fullinitblock@*/
355 /*@unchecked@*/ /*@observer@*/
356 static struct PyMethodDef rpmfi_methods[] = {
357  {"Debug",      (PyCFunction)rpmfi_Debug,       METH_VARARGS,
358         NULL},
359  {"FC",         (PyCFunction)rpmfi_FC,          METH_VARARGS,
360         NULL},
361  {"FX",         (PyCFunction)rpmfi_FX,          METH_VARARGS,
362         NULL},
363  {"DC",         (PyCFunction)rpmfi_DC,          METH_VARARGS,
364         NULL},
365  {"DX",         (PyCFunction)rpmfi_DX,          METH_VARARGS,
366         NULL},
367  {"BN",         (PyCFunction)rpmfi_BN,          METH_VARARGS,
368         NULL},
369  {"DN",         (PyCFunction)rpmfi_DN,          METH_VARARGS,
370         NULL},
371  {"FN",         (PyCFunction)rpmfi_FN,          METH_VARARGS,
372         NULL},
373  {"FFlags",     (PyCFunction)rpmfi_FFlags,      METH_VARARGS,
374         NULL},
375  {"VFlags",     (PyCFunction)rpmfi_VFlags,      METH_VARARGS,
376         NULL},
377  {"FMode",      (PyCFunction)rpmfi_FMode,       METH_VARARGS,
378         NULL},
379  {"FState",     (PyCFunction)rpmfi_FState,      METH_VARARGS,
380         NULL},
381  {"MD5",        (PyCFunction)rpmfi_MD5,         METH_VARARGS,
382         NULL},
383  {"FLink",      (PyCFunction)rpmfi_FLink,       METH_VARARGS,
384         NULL},
385  {"FSize",      (PyCFunction)rpmfi_FSize,       METH_VARARGS,
386         NULL},
387  {"FRdev",      (PyCFunction)rpmfi_FRdev,       METH_VARARGS,
388         NULL},
389  {"FMtime",     (PyCFunction)rpmfi_FMtime,      METH_VARARGS,
390         NULL},
391  {"FUser",      (PyCFunction)rpmfi_FUser,       METH_VARARGS,
392         NULL},
393  {"FGroup",     (PyCFunction)rpmfi_FGroup,      METH_VARARGS,
394         NULL},
395  {"FColor",     (PyCFunction)rpmfi_FColor,      METH_VARARGS,
396         NULL},
397  {"FClass",     (PyCFunction)rpmfi_FClass,      METH_VARARGS,
398         NULL},
399  {"next",       (PyCFunction)rpmfi_Next,        METH_VARARGS,
400 "fi.next() -> (FN, FSize, FMode, FMtime, FFlags, FRdev, FInode, FNlink, FState, VFlags, FUser, FGroup, FMD5))\n\
401 - Retrieve next file info tuple.\n" },
402 #ifdef  NOTYET
403  {"NextD",      (PyCFunction)rpmfi_NextD,       METH_VARARGS,
404         NULL},
405  {"InitD",      (PyCFunction)rpmfi_InitD,       METH_VARARGS,
406         NULL},
407 #endif
408  {NULL,         NULL}           /* sentinel */
409 };
410 /*@=fullinitblock@*/
411
412 /* ---------- */
413
414 static void
415 rpmfi_dealloc(/*@only@*/ /*@null@*/ rpmfiObject * s)
416         /*@modifies s @*/
417 {
418     if (s) {
419         s->fi = rpmfiFree(s->fi);
420         PyObject_Del(s);
421     }
422 }
423
424 static int
425 rpmfi_print(rpmfiObject * s, FILE * fp, /*@unused@*/ int flags)
426         /*@globals fileSystem @*/
427         /*@modifies s, fp, fileSystem @*/
428 {
429     if (!(s && s->fi))
430         return -1;
431
432     s->fi = rpmfiInit(s->fi, 0);
433     while (rpmfiNext(s->fi) >= 0)
434         fprintf(fp, "%s\n", rpmfiFN(s->fi));
435     return 0;
436 }
437
438 static PyObject * rpmfi_getattro(PyObject * o, PyObject * n)
439         /*@*/
440 {
441     return PyObject_GenericGetAttr(o, n);
442 }
443
444 static int rpmfi_setattro(PyObject * o, PyObject * n, PyObject * v)
445         /*@*/
446 {
447     return PyObject_GenericSetAttr(o, n, v);
448 }
449
450 static int
451 rpmfi_length(rpmfiObject * s)
452         /*@*/
453 {
454     return rpmfiFC(s->fi);
455 }
456
457 /*@null@*/
458 static PyObject *
459 rpmfi_subscript(rpmfiObject * s, PyObject * key)
460         /*@modifies s @*/
461 {
462     int ix;
463
464     if (!PyInt_Check(key)) {
465         PyErr_SetString(PyExc_TypeError, "integer expected");
466         return NULL;
467     }
468
469     ix = (int) PyInt_AsLong(key);
470     rpmfiSetFX(s->fi, ix);
471     return Py_BuildValue("s", xstrdup(rpmfiFN(s->fi)));
472 }
473
474 /*@unchecked@*/ /*@observer@*/
475 static PyMappingMethods rpmfi_as_mapping = {
476         (inquiry) rpmfi_length,         /* mp_length */
477         (binaryfunc) rpmfi_subscript,   /* mp_subscript */
478         (objobjargproc)0,               /* mp_ass_subscript */
479 };
480
481 /**
482  */
483 /*@unchecked@*/ /*@observer@*/
484 static char rpmfi_doc[] =
485 "";
486
487 /*@-fullinitblock@*/
488 PyTypeObject rpmfi_Type = {
489         PyObject_HEAD_INIT(&PyType_Type)
490         0,                              /* ob_size */
491         "rpm.fi",                       /* tp_name */
492         sizeof(rpmfiObject),            /* tp_basicsize */
493         0,                              /* tp_itemsize */
494         /* methods */
495         (destructor) rpmfi_dealloc,     /* tp_dealloc */
496         (printfunc) rpmfi_print,        /* tp_print */
497         (getattrfunc)0,                 /* tp_getattr */
498         (setattrfunc)0,                 /* tp_setattr */
499         (cmpfunc)0,                     /* tp_compare */
500         (reprfunc)0,                    /* tp_repr */
501         0,                              /* tp_as_number */
502         0,                              /* tp_as_sequence */
503         &rpmfi_as_mapping,              /* tp_as_mapping */
504         (hashfunc)0,                    /* tp_hash */
505         (ternaryfunc)0,                 /* tp_call */
506         (reprfunc)0,                    /* tp_str */
507         (getattrofunc) rpmfi_getattro,  /* tp_getattro */
508         (setattrofunc) rpmfi_setattro,  /* tp_setattro */
509         0,                              /* tp_as_buffer */
510         Py_TPFLAGS_DEFAULT,             /* tp_flags */
511         rpmfi_doc,                      /* tp_doc */
512 #if Py_TPFLAGS_HAVE_ITER
513         0,                              /* tp_traverse */
514         0,                              /* tp_clear */
515         0,                              /* tp_richcompare */
516         0,                              /* tp_weaklistoffset */
517         (getiterfunc) rpmfi_iter,       /* tp_iter */
518         (iternextfunc) rpmfi_iternext,  /* tp_iternext */
519         rpmfi_methods,                  /* tp_methods */
520         0,                              /* tp_members */
521         0,                              /* tp_getset */
522         0,                              /* tp_base */
523         0,                              /* tp_dict */
524         0,                              /* tp_descr_get */
525         0,                              /* tp_descr_set */
526         0,                              /* tp_dictoffset */
527         0,                              /* tp_init */
528         0,                              /* tp_alloc */
529         0,                              /* tp_new */
530         0,                              /* tp_free */
531         0,                              /* tp_is_gc */
532 #endif
533 };
534 /*@=fullinitblock@*/
535
536 /* ---------- */
537
538 rpmfi fiFromFi(rpmfiObject * s)
539 {
540     return s->fi;
541 }
542
543 rpmfiObject *
544 rpmfi_Wrap(rpmfi fi)
545 {
546     rpmfiObject *s = PyObject_New(rpmfiObject, &rpmfi_Type);
547
548     if (s == NULL)
549         return NULL;
550     s->fi = fi;
551     s->active = 0;
552     return s;
553 }
554
555 rpmfiObject *
556 hdr_fiFromHeader(PyObject * s, PyObject * args)
557 {
558     hdrObject * ho = (hdrObject *)s;
559     PyObject * to = NULL;
560     rpmts ts = NULL;    /* XXX FIXME: fiFromHeader should be a ts method. */
561     rpmTag tagN = RPMTAG_BASENAMES;
562     int scareMem = 0;
563
564     if (!PyArg_ParseTuple(args, "|O:fiFromHeader", &to))
565         return NULL;
566     if (to != NULL) {
567         tagN = tagNumFromPyObject(to);
568         if (tagN == -1) {
569             PyErr_SetString(PyExc_KeyError, "unknown header tag");
570             return NULL;
571         }
572     }
573     return rpmfi_Wrap( rpmfiNew(ts, hdrGetHeader(ho), tagN, scareMem) );
574 }