-
Notifications
You must be signed in to change notification settings - Fork 1
/
rpmdb-py.c
282 lines (250 loc) · 7.04 KB
/
rpmdb-py.c
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
/** \ingroup py_c
* \file python/rpmdb-py.c
*/
#include "system-py.h"
#include <rpmio.h>
#include <rpmcb.h> /* XXX fnpyKey */
#include <rpmtypes.h>
#include <rpmtag.h>
#include "rpmdb-py.h"
#include "rpmmi-py.h"
#include "header-py.h"
#include "debug.h"
/** \ingroup python
* \class Rpmdb
* \brief A python rpm.db object represents an RPM database.
*
* Instances of the rpmdb object provide access to the records of a
* RPM database. The records are accessed by index number. To
* retrieve the header data in the RPM database, the rpmdb object is
* subscripted as you would access members of a list.
*
* The rpmdb class contains the following methods:
*
* - firstkey() Returns the index of the first record in the database.
* deprecated Use mi = ts.dbMatch() (or db.match()) instead.
*
* - nextkey(index) Returns the index of the next record after "index" in the
* database.
* param index current rpmdb location
* deprecated Use hdr = mi.next() instead.
*
* - findbyfile(file) Returns a list of the indexes to records that own file
* "file".
* param file absolute path to file
* deprecated Use mi = ts.dbMatch('basename') instead.
*
* - findbyname(name) Returns a list of the indexes to records for packages
* named "name".
* param name package name
* deprecated Use mi = ts.dbMatch('name') instead.
*
* - findbyprovides(dep) Returns a list of the indexes to records for packages
* that provide "dep".
* param dep provided dependency string
* deprecated Use mi = ts.dbMmatch('providename') instead.
*
* To obtain a db object explicitly, the opendb function in the rpm module
* can be called. The opendb function takes two optional arguments.
* The first optional argument is a boolean flag that specifies if the
* database is to be opened for read/write access or read-only access.
* The second argument specifies an alternate root directory for RPM
* to use.
*
* Note that in most cases, one is interested in querying the default
* database in /var/lib/rpm and an rpm.mi match iterator derived from
* an implicit open of the database from an rpm.ts transaction set object:
* \code
* import rpm
* ts = rpm.TransactionSet()
* mi = ts.dbMatch()
* ...
* \endcode
* is simpler than explicitly opening a database object:
* \code
* import rpm
* db = rpm.opendb()
* mi = db.match()
* \endcode
*
* An example of opening a database and retrieving the first header in
* the database, then printing the name of the package that the header
* represents:
* \code
* import rpm
* db = rpm.opendb()
* mi = db.match()
* if mi:
* h = mi.next()
* if h:
* print h['name']
* \endcode
*
* To print all of the packages in the database that match a package
* name, the code will look like this:
* \code
* import rpm
* db = rpm.opendb()
* mi = db.match('name', "foo")
* while mi:
* h = mi.next()
* if not h:
* break
* print "%s-%s-%s" % (h['name'], h['version'], h['release'])
* \endcode
*/
struct rpmdbObject_s {
PyObject_HEAD
PyObject *md_dict; /*!< to look like PyModuleObject */
rpmdb db;
};
/** \ingroup python
* \name Class: Rpmdb
*/
/*@{*/
static PyObject *
rpmdb_Match (rpmdbObject * s, PyObject * args, PyObject * kwds)
{
PyObject * mio;
char *key = NULL;
int len = 0;
int tag = RPMDBI_PACKAGES;
char * kwlist[] = {"tag", "key", "len", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&zi", kwlist,
tagNumFromPyObject, &tag, &key, &len))
return NULL;
mio = rpmmi_Wrap(&rpmmi_Type, rpmmiInit(s->db, tag, key, len), (PyObject *)s);
return mio;
}
/*@}*/
static struct PyMethodDef rpmdb_methods[] = {
{"match", (PyCFunction) rpmdb_Match, METH_VARARGS|METH_KEYWORDS,
"db.match([TagN, [key, [len]]]) -> mi\n\
- Create an rpm db match iterator.\n" },
{NULL, NULL} /* sentinel */
};
/**
*/
static int
rpmdb_length(rpmdbObject * s)
{
rpmmi mi;
int count = 0;
mi = rpmmiInit(s->db, RPMDBI_PACKAGES, NULL, 0);
while (rpmmiNext(mi) != NULL)
count++;
mi = rpmmiFree(mi);
return count;
}
static hdrObject *
rpmdb_subscript(rpmdbObject * s, PyObject * key)
{
int offset;
hdrObject * ho;
Header h;
rpmmi mi;
if (!PyInt_Check(key)) {
PyErr_SetString(PyExc_TypeError, "integer expected");
return NULL;
}
offset = (int) PyInt_AsLong(key);
mi = rpmmiInit(s->db, RPMDBI_PACKAGES, &offset, sizeof(offset));
if (!(h = rpmmiNext(mi))) {
mi = rpmmiFree(mi);
PyErr_SetString(pyrpmError, "cannot read rpmdb entry");
return NULL;
}
ho = (hdrObject *) hdr_Wrap(&hdr_Type, h);
(void)headerFree(h);
h = NULL;
return ho;
}
static PyMappingMethods rpmdb_as_mapping = {
(lenfunc) rpmdb_length, /* mp_length */
(binaryfunc) rpmdb_subscript, /* mp_subscript */
(objobjargproc)0, /* mp_ass_subscript */
};
static void rpmdb_dealloc(rpmdbObject * s)
{
if (s->db != NULL)
rpmdbClose(s->db);
s->db = NULL;
Py_TYPE(s)->tp_free((PyObject *)s);
}
static char rpmdb_doc[] =
"";
PyTypeObject rpmdb_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
"rpm.db", /* tp_name */
sizeof(rpmdbObject), /* tp_size */
0, /* tp_itemsize */
(destructor) rpmdb_dealloc, /* tp_dealloc */
0, /* tp_print */
(getattrfunc)0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
&rpmdb_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 */
rpmdb_doc, /* tp_doc */
#if Py_TPFLAGS_HAVE_ITER
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
rpmdb_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 */
0, /* tp_new */
0, /* tp_free */
0, /* tp_is_gc */
#endif
};
#ifdef _LEGACY_BINDINGS_TOO
rpmdb dbFromDb(rpmdbObject * db)
{
return db->db;
}
rpmdbObject *
rpmOpenDB(PyObject * self, PyObject * args, PyObject * kwds) {
rpmdbObject * o;
char * root = "";
int forWrite = 0;
char * kwlist[] = {"forWrite", "rootdir", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|is", kwlist,
&forWrite, &root))
return NULL;
o = (rpmdbObject *) Py_TYPE(s)->tp_alloc(subtype, 0);
o->db = NULL;
if (rpmdbOpen(root, &o->db, forWrite ? O_RDWR | O_CREAT: O_RDONLY, (mode_t)0644)) {
char * errmsg = "cannot open database in %s";
char * errstr = NULL;
int errsize;
Py_XDECREF(o);
/* PyErr_SetString should take varargs... */
errsize = strlen(errmsg) + *root == '\0' ? 15 /* "/var/lib/rpm" */ : strlen(root);
errstr = alloca(errsize);
snprintf(errstr, errsize, errmsg, *root == '\0' ? "/var/lib/rpm" : root);
PyErr_SetString(pyrpmError, errstr);
return NULL;
}
return o;
}
#endif