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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
|
#include "rpmsystem-py.h"
#include <rpm/rpmtypes.h>
#include <rpm/rpmpgp.h>
#include "header-py.h"
#include "rpmfi-py.h"
#include "rpmfiles-py.h"
#include "rpmfd-py.h"
#include "rpmarchive-py.h"
#include "rpmstrpool-py.h"
/* A single file from rpmfiles set, can't be independently instanciated */
struct rpmfileObject_s {
PyObject_HEAD
PyObject *md_dict;
rpmfiles files; /* reference to rpmfiles */
int ix; /* index to rpmfiles */
};
static void rpmfile_dealloc(rpmfileObject * s)
{
s->files = rpmfilesFree(s->files);
Py_TYPE(s)->tp_free((PyObject *)s);
}
static char rpmfile_doc[] =
"Gives access to the meta data of a single file.\n\n"
"Instances of this class are only available through an rpm.files object.";
static PyObject *rpmfile_fx(rpmfileObject *s)
{
return Py_BuildValue("i", s->ix);
}
static PyObject *rpmfile_dx(rpmfileObject *s)
{
return Py_BuildValue("i", rpmfilesDI(s->files, s->ix));
}
static PyObject *rpmfile_name(rpmfileObject *s)
{
char * fn = rpmfilesFN(s->files, s->ix);
PyObject *o = Py_BuildValue("s", fn);
free(fn);
return o;
}
static PyObject *rpmfile_basename(rpmfileObject *s)
{
return Py_BuildValue("s", rpmfilesBN(s->files, s->ix));
}
static PyObject *rpmfile_dirname(rpmfileObject *s)
{
return Py_BuildValue("s", rpmfilesDN(s->files, rpmfilesDI(s->files, s->ix)));
}
static PyObject *rpmfile_orig_name(rpmfileObject *s)
{
char * fn = rpmfilesOFN(s->files, s->ix);
PyObject *o = Py_BuildValue("s", fn);
free(fn);
return o;
}
static PyObject *rpmfile_orig_basename(rpmfileObject *s)
{
return Py_BuildValue("s", rpmfilesOBN(s->files, s->ix));
}
static PyObject *rpmfile_orig_dirname(rpmfileObject *s)
{
return Py_BuildValue("s", rpmfilesODN(s->files, rpmfilesODI(s->files, s->ix)));
}
static PyObject *rpmfile_mode(rpmfileObject *s)
{
return Py_BuildValue("i", rpmfilesFMode(s->files, s->ix));
}
static PyObject *rpmfile_size(rpmfileObject *s)
{
return Py_BuildValue("L", rpmfilesFSize(s->files, s->ix));
}
static PyObject *rpmfile_mtime(rpmfileObject *s)
{
return Py_BuildValue("i", rpmfilesFMtime(s->files, s->ix));
}
static PyObject *rpmfile_rdev(rpmfileObject *s)
{
return Py_BuildValue("i", rpmfilesFRdev(s->files, s->ix));
}
static PyObject *rpmfile_inode(rpmfileObject *s)
{
return Py_BuildValue("i", rpmfilesFInode(s->files, s->ix));
}
static PyObject *rpmfile_nlink(rpmfileObject *s)
{
return Py_BuildValue("i", rpmfilesFNlink(s->files, s->ix));
}
static PyObject *rpmfile_linkto(rpmfileObject *s)
{
return Py_BuildValue("s", rpmfilesFLink(s->files, s->ix));
}
static PyObject *rpmfile_user(rpmfileObject *s)
{
return Py_BuildValue("s", rpmfilesFUser(s->files, s->ix));
}
static PyObject *rpmfile_group(rpmfileObject *s)
{
return Py_BuildValue("s", rpmfilesFGroup(s->files, s->ix));
}
static PyObject *rpmfile_fflags(rpmfileObject *s)
{
return Py_BuildValue("i", rpmfilesFFlags(s->files, s->ix));
}
static PyObject *rpmfile_vflags(rpmfileObject *s)
{
return Py_BuildValue("i", rpmfilesVFlags(s->files, s->ix));
}
static PyObject *rpmfile_color(rpmfileObject *s)
{
return Py_BuildValue("i", rpmfilesFColor(s->files, s->ix));
}
static PyObject *rpmfile_state(rpmfileObject *s)
{
return Py_BuildValue("i", rpmfilesFState(s->files, s->ix));
}
static PyObject *rpmfile_digest(rpmfileObject *s)
{
size_t diglen = 0;
const unsigned char *digest = rpmfilesFDigest(s->files, s->ix,
NULL, &diglen);
if (digest) {
char * hex = pgpHexStr(digest, diglen);
PyObject *o = Py_BuildValue("s", hex);
free(hex);
return o;
}
Py_RETURN_NONE;
}
static PyObject *rpmfile_class(rpmfileObject *s)
{
return Py_BuildValue("s", rpmfilesFClass(s->files, s->ix));
}
static PyObject *rpmfile_caps(rpmfileObject *s)
{
return Py_BuildValue("s", rpmfilesFCaps(s->files, s->ix));
}
static PyObject *rpmfile_langs(rpmfileObject *s)
{
return Py_BuildValue("s", rpmfilesFLangs(s->files, s->ix));
}
static PyObject *rpmfile_links(rpmfileObject *s)
{
PyObject *result = NULL;
const int * links = NULL;
uint32_t nlinks = rpmfilesFLinks(s->files, s->ix, &links);
if (nlinks == 0)
Py_RETURN_NONE;
else if (nlinks == 1)
links = &s->ix; /* file itself */
result = PyTuple_New(nlinks);
if (result) {
for (uint32_t i = 0; i < nlinks; i++) {
int lix = links[i];
PyObject * o;
if (lix == s->ix) {
/* file itself, return a reference instead of new object */
Py_INCREF(s);
o = (PyObject *) s;
} else {
o = rpmfile_Wrap(s->files, lix);
}
PyTuple_SET_ITEM(result, i, o);
}
}
return result;
}
/*
* Exported as "matches" instead of a rich comparison operator or such
* as this cannot be used for comparing file *object* equality,
* rpmfilesCompare() determines whether the file *contents* match.
*/
static PyObject *rpmfile_matches(rpmfileObject *s, PyObject *o)
{
PyObject *result = NULL;
if (rpmfileObject_Check(o)) {
rpmfileObject *of = (rpmfileObject *)o;
int rc = rpmfilesCompare(s->files, s->ix, of->files, of->ix);
result = PyBool_FromLong(rc == 0);
} else {
PyErr_SetObject(PyExc_TypeError, o);
}
return result;
}
static PyObject *rpmfile_verify(rpmfileObject *s, PyObject *args, PyObject *kwds)
{
static char *kwlist[] = { "omitMask", NULL };
rpmVerifyAttrs omitMask = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i", kwlist, &omitMask))
return NULL;
return Py_BuildValue("i", rpmfilesVerify(s->files, s->ix, omitMask));
}
static PyGetSetDef rpmfile_getseters[] = {
{ "fx", (getter) rpmfile_fx, NULL,
"index in header and rpm.files object" },
{ "dx", (getter) rpmfile_dx, NULL,
"index of dirname entry" },
{ "name", (getter) rpmfile_name, NULL,
"file name (path)" },
{ "basename", (getter) rpmfile_basename, NULL, NULL },
{ "dirname", (getter) rpmfile_dirname, NULL, NULL },
{ "orig_name", (getter) rpmfile_orig_name, NULL,
"original file name (may differ due to relocation)" },
{ "orig_basename", (getter) rpmfile_orig_basename, NULL,
"original base name (may differ due to relocation)" },
{ "orig_dirname", (getter) rpmfile_orig_dirname, NULL,
"original dir name (may differ due to relocation)" },
{ "mode", (getter) rpmfile_mode, NULL,
"mode flags / unix permissions" },
{ "mtime", (getter) rpmfile_mtime, NULL,
"modification time (in unix time)" },
{ "size", (getter) rpmfile_size, NULL,
"file size" },
{ "rdev", (getter) rpmfile_rdev, NULL,
"device number - for device files only" },
{ "inode", (getter) rpmfile_inode, NULL,
"inode number - contains fake, data used to identify hard liked files" },
{ "fflags", (getter) rpmfile_fflags, NULL,
"file flags - see RPMFILE_* constants" },
{ "vflags", (getter) rpmfile_vflags, NULL,
"verification flags - see RPMVERIFY_* (in rpmvf.h)" },
{ "linkto", (getter) rpmfile_linkto, NULL,
"link target - symlinks only" },
{ "color", (getter) rpmfile_color, NULL,
"file color - 2 for 64 bit binaries, 1 for 32 bit binaries, 0 else" },
{ "nlink", (getter) rpmfile_nlink, NULL,
"number of hardlinks pointing to the same content as this file" },
{ "links", (getter) rpmfile_links, NULL,
"list of file indexes that are hardlinked with this file" },
{ "user", (getter) rpmfile_user, NULL,
"user name owning this file" },
{ "group", (getter) rpmfile_group, NULL,
"group name owning this file" },
{ "digest", (getter) rpmfile_digest, NULL,
"check sum of file content" },
{ "class", (getter) rpmfile_class, NULL,
"classfication of file content based on libmagic/file(1)" },
{ "state", (getter) rpmfile_state, NULL,
"file state - see RPMFILE_STATE_* constants" },
{ "langs", (getter) rpmfile_langs, NULL,
"language the file provides (typically for doc files)" },
{ "caps", (getter) rpmfile_caps, NULL,
"file capabilities" },
{ NULL, NULL, NULL, NULL }
};
static struct PyMethodDef rpmfile_methods[] = {
{ "matches", (PyCFunction) rpmfile_matches, METH_O,
NULL },
{ "verify", (PyCFunction) rpmfile_verify, METH_VARARGS|METH_KEYWORDS,
NULL },
{ NULL, NULL, 0, NULL }
};
PyTypeObject rpmfile_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
"rpm.file", /* tp_name */
sizeof(rpmfileObject), /* tp_basicsize */
0, /* tp_itemsize */
/* methods */
(destructor) rpmfile_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
(reprfunc)rpmfile_name, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */
PyObject_GenericSetAttr, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
rpmfile_doc, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
rpmfile_methods, /* tp_methods */
0, /* tp_members */
rpmfile_getseters, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
0, /* tp_free */
0, /* tp_is_gc */
};
PyObject * rpmfile_Wrap(rpmfiles files, int ix)
{
rpmfileObject *s = PyObject_New(rpmfileObject, &rpmfile_Type);
if (s == NULL) return NULL;
s->files = rpmfilesLink(files);
s->ix = ix;
return (PyObject *) s;
}
/* The actual rpmfiles info set */
struct rpmfilesObject_s {
PyObject_HEAD
PyObject *md_dict; /*!< to look like PyModuleObject */
rpmfiles files;
};
static void rpmfiles_dealloc(rpmfilesObject * s)
{
s->files = rpmfilesFree(s->files);
Py_TYPE(s)->tp_free((PyObject *)s);
}
static PyObject * rpmfiles_new(PyTypeObject * subtype, PyObject *args, PyObject *kwds)
{
PyObject * to = NULL;
Header h = NULL;
rpmfiles files = NULL;
rpmTagVal tagN = RPMTAG_BASENAMES;
int flags = 0;
rpmstrPool pool = NULL;
char * kwlist[] = {"header", "tag", "flags", "pool", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&|OiO&:rpmfiles_new", kwlist,
hdrFromPyObject, &h, &to, &flags,
poolFromPyObject, &pool))
return NULL;
files = rpmfilesNew(pool, h, tagN, flags);
if (files == NULL) {
PyErr_SetString(PyExc_ValueError, "invalid file data in header");
return NULL;
}
return rpmfiles_Wrap(subtype, files);
}
static Py_ssize_t rpmfiles_length(rpmfilesObject *s)
{
return rpmfilesFC(s->files);
}
static PyObject * rpmfiles_getitem(rpmfilesObject *s, Py_ssize_t ix)
{
if (ix >= 0 && ix < rpmfilesFC(s->files))
return rpmfile_Wrap(s->files, ix);
PyErr_SetObject(PyExc_IndexError, Py_BuildValue("i", ix));
return NULL;
}
static int rpmfiles_contains(rpmfilesObject *s, PyObject *value)
{
const char *fn = NULL;
if (!PyArg_Parse(value, "s", &fn))
return -1;
return (rpmfilesFindFN(s->files, fn) >= 0) ? 1 : 0;
}
static PySequenceMethods rpmfiles_as_sequence = {
(lenfunc)rpmfiles_length, /* sq_length */
0, /* sq_concat */
0, /* sq_repeat */
(ssizeargfunc) rpmfiles_getitem, /* sq_item */
0, /* sq_slice */
0, /* sq_ass_item */
0, /* sq_ass_slice */
(objobjproc)rpmfiles_contains, /* sq_contains */
0, /* sq_inplace_concat */
0, /* sq_inplace_repeat */
};
static PyObject * rpmfiles_find(rpmfileObject *s,
PyObject *args, PyObject *kwds)
{
const char *fn = NULL;
int fx, orig = 0;
char * kwlist[] = {"filename", "orig", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|i", kwlist, &fn, &orig))
return NULL;
if (orig)
fx = rpmfilesFindOFN(s->files, fn);
else
fx = rpmfilesFindFN(s->files, fn);
if (fx >= 0)
return rpmfile_Wrap(s->files, fx);
Py_RETURN_NONE;
}
static PyObject *rpmfiles_archive(rpmfilesObject *s,
PyObject *args, PyObject *kwds)
{
char * kwlist[] = {"fd", "write", NULL};
rpmfdObject *fdo = NULL;
FD_t fd = NULL;
rpmfi archive = NULL;
int writer = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&|i", kwlist,
rpmfdFromPyObject, &fdo, &writer)) {
return NULL;
}
fd = rpmfdGetFd(fdo);
if (writer) {
archive = rpmfiNewArchiveWriter(fd, s->files);
} else {
archive = rpmfiNewArchiveReader(fd, s->files, RPMFI_ITER_READ_ARCHIVE);
}
return rpmarchive_Wrap(&rpmarchive_Type, s->files, archive);
}
static PyObject *rpmfiles_subscript(rpmfilesObject *s, PyObject *item)
{
PyObject *str = NULL;
/* treat numbers as sequence accesses */
if (PyInt_Check(item)) {
return rpmfiles_getitem(s, PyInt_AsSsize_t(item));
} else if (PyLong_Check(item)) {
return rpmfiles_getitem(s, PyLong_AsSsize_t(item));
}
/* handle slices by returning tuples of rpm.file items */
if (PySlice_Check(item)) {
Py_ssize_t start, stop, step, slicelength, i, cur;
PyObject * result;
if (PySlice_GetIndicesEx(
#if PY_MAJOR_VERSION < 3
(PySliceObject*)
#endif
item, rpmfiles_length(s),
&start, &stop, &step, &slicelength) < 0) {
return NULL;
}
result = PyTuple_New(slicelength);
if (result) {
for (cur = start, i = 0; i < slicelength; cur += step, i++) {
PyTuple_SET_ITEM(result, i, rpmfiles_getitem(s, cur));
}
}
return result;
}
/* ... and strings as mapping access */
if (utf8FromPyObject(item, &str)) {
int fx = rpmfilesFindFN(s->files, PyBytes_AsString(str));
Py_DECREF(str);
if (fx >= 0) {
return rpmfile_Wrap(s->files, fx);
} else {
PyErr_SetObject(PyExc_KeyError, item);
}
} else {
PyErr_SetObject(PyExc_TypeError, item);
}
return NULL;
}
static PyMappingMethods rpmfiles_as_mapping = {
(lenfunc) rpmfiles_length, /* mp_length */
(binaryfunc) rpmfiles_subscript, /* mp_subscript */
0, /* mp_ass_subscript */
};
static struct PyMethodDef rpmfiles_methods[] = {
{ "archive", (PyCFunction) rpmfiles_archive, METH_VARARGS|METH_KEYWORDS,
"files.archive(fd, write=False) -- Return a rpm.archive object\n\n"
"Args:\n"
" fd : File to read from or write to.\n"
" write : True to get an archive writer, False for an archive reader"},
{ "find", (PyCFunction) rpmfiles_find, METH_VARARGS|METH_KEYWORDS,
"files.find(filename, orig=False) -- Return index of given file name.\n\n"
" Return -1 if file is not found.\n"
" Leading \".\" in filename is ignored."},
{ NULL, NULL, 0, NULL }
};
static char rpmfiles_doc[] =
"rpm.files(hdr, tag=RPMTAG_BASENAMES, flags=None, pool=None)\n\n"
"Stores the meta data of a package's files.\n\n"
"Args:\n"
"\thdr: The header object to get the data from.\n"
"\tflags : Controls which data to store and whether to create\n\t\tcopies or use the data from the header.\n\t\tBy default all data is copied.\n\t\tSee RPMFI_* constants in rpmfiles.h.\n"
"\tpool : rpm.strpool object to store the strings in.\n\t\tLeave empty to use global pool.\n"
"\ttag : Obsolete. Leave alone!\n\n"
"rpm.files is basically a sequence of rpm.file objects.\nNote that this is a read only data structure. To write file data you\nhave to write it directly into aheader object.";
PyTypeObject rpmfiles_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
"rpm.files", /* tp_name */
sizeof(rpmfilesObject), /* tp_basicsize */
0, /* tp_itemsize */
/* methods */
(destructor) rpmfiles_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
&rpmfiles_as_sequence, /* tp_as_sequence */
&rpmfiles_as_mapping, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */
PyObject_GenericSetAttr, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
rpmfiles_doc, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
rpmfiles_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 */
0, /* tp_init */
0, /* tp_alloc */
(newfunc) rpmfiles_new, /* tp_new */
0, /* tp_free */
0, /* tp_is_gc */
};
PyObject * rpmfiles_Wrap(PyTypeObject *subtype, rpmfiles files)
{
rpmfilesObject *s = (rpmfilesObject *)subtype->tp_alloc(subtype, 0);
if (s == NULL) return NULL;
s->files = files;
return (PyObject *) s;
}
|