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