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