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