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
|
/*
* Copyright (c) 2018, SUSE Inc.
*
* This program is licensed under the BSD license, read LICENSE.BSD
* for further information
*/
/*
* repo_rpmdb_librpm.h
*
* Use librpm to access the rpm database
*
*/
#include <rpm/rpmts.h>
#include <rpm/rpmmacro.h>
struct rpmdbstate {
Pool *pool;
char *rootdir;
RpmHead *rpmhead; /* header storage space */
unsigned int rpmheadsize;
int dbenvopened; /* database environment opened */
const char *dbpath; /* path to the database */
rpmts ts;
rpmdbMatchIterator mi; /* iterator over packages database */
};
static inline int
access_rootdir(struct rpmdbstate *state, const char *dir, int mode)
{
if (state->rootdir)
{
char *path = solv_dupjoin(state->rootdir, dir, 0);
int r = access(path, mode);
free(path);
return r;
}
return access(dir, mode);
}
static void
detect_dbpath(struct rpmdbstate *state)
{
state->dbpath = access_rootdir(state, "/var/lib/rpm", W_OK) == -1
&& access_rootdir(state, "/usr/share/rpm/Packages", R_OK) == 0
? "/usr/share/rpm" : "/var/lib/rpm";
}
static int
stat_database(struct rpmdbstate *state, struct stat *statbuf)
{
static const char *dbname[] = {
"/Packages",
"/Packages.db",
"/rpmdb.sqlite",
"/data.mdb",
"/Packages", /* for error reporting */
0,
};
int i;
#ifdef HAVE_RPMDBFSTAT
if (state->dbenvopened == 1)
return rpmdbFStat(rpmtsGetRdb(state->ts), statbuf);
#endif
if (!state->dbpath)
detect_dbpath(state);
for (i = 0; ; i++)
{
char *dbpath = solv_dupjoin(state->rootdir, state->dbpath, dbname[i]);
if (!stat(dbpath, statbuf))
{
free(dbpath);
return 0;
}
if (errno != ENOENT || !dbname[i + 1])
{
int saved_errno = errno;
pool_error(state->pool, -1, "%s: %s", dbpath, strerror(errno));
solv_free(dbpath);
errno = saved_errno;
return -1;
}
solv_free(dbpath);
}
return 0;
}
static int
opendbenv(struct rpmdbstate *state)
{
rpmts ts;
char *dbpath;
if (!state->dbpath)
detect_dbpath(state);
dbpath = solv_dupjoin("_dbpath ", state->rootdir, state->dbpath);
rpmDefineMacro(NULL, dbpath, 0);
solv_free(dbpath);
ts = rpmtsCreate();
if (!ts)
{
pool_error(state->pool, 0, "rpmtsCreate failed");
delMacro(NULL, "_dbpath");
return 0;
}
if (rpmtsOpenDB(ts, O_RDONLY))
{
pool_error(state->pool, 0, "rpmtsOpenDB failed: %s", strerror(errno));
rpmtsFree(ts);
delMacro(NULL, "_dbpath");
return 0;
}
delMacro(NULL, "_dbpath");
rpmtsSetVSFlags(ts, _RPMVSF_NODIGESTS | _RPMVSF_NOSIGNATURES | _RPMVSF_NOHEADER);
state->ts = ts;
state->dbenvopened = 1;
return 1;
}
static void
closedbenv(struct rpmdbstate *state)
{
if (state->ts)
rpmtsFree(state->ts);
state->ts = 0;
state->dbenvopened = 0;
}
/* get the rpmdbids of all installed packages from the Name index database.
* This is much faster then querying the big Packages database */
static struct rpmdbentry *
getinstalledrpmdbids(struct rpmdbstate *state, const char *index, const char *match, int *nentriesp, char **namedatap, int keep_gpg_pubkey)
{
const void * key;
size_t keylen, matchl = 0;
Id nameoff;
char *namedata = 0;
int namedatal = 0;
struct rpmdbentry *entries = 0;
int nentries = 0;
rpmdbIndexIterator ii;
*nentriesp = 0;
if (namedatap)
*namedatap = 0;
if (state->dbenvopened != 1 && !opendbenv(state))
return 0;
if (match)
matchl = strlen(match);
ii = rpmdbIndexIteratorInit(rpmtsGetRdb(state->ts), RPMDBI_NAME);
while (rpmdbIndexIteratorNext(ii, &key, &keylen) == 0)
{
unsigned int i, npkgs;
if (match)
{
if (keylen != matchl || memcmp(key, match, keylen) != 0)
continue;
}
else if (!keep_gpg_pubkey && keylen == 10 && !memcmp(key, "gpg-pubkey", 10))
continue;
nameoff = namedatal;
if (namedatap)
{
namedata = solv_extend(namedata, namedatal, keylen + 1, 1, NAMEDATA_BLOCK);
memcpy(namedata + namedatal, key, keylen);
namedata[namedatal + keylen] = 0;
namedatal += keylen + 1;
}
npkgs = rpmdbIndexIteratorNumPkgs(ii);
for (i = 0; i < npkgs; i++)
{
entries = solv_extend(entries, nentries, 1, sizeof(*entries), ENTRIES_BLOCK);
entries[nentries].rpmdbid = rpmdbIndexIteratorPkgOffset(ii, i);
entries[nentries].nameoff = nameoff;
nentries++;
}
}
rpmdbIndexIteratorFree(ii);
/* make sure that enteries is != 0 if there was no error */
if (!entries)
entries = solv_extend(entries, 1, 1, sizeof(*entries), ENTRIES_BLOCK);
*nentriesp = nentries;
if (namedatap)
*namedatap = namedata;
return entries;
}
#if defined(HAVE_RPMDBNEXTITERATORHEADERBLOB) && !defined(ENABLE_RPMPKG_LIBRPM)
static int headfromhdrblob(struct rpmdbstate *state, const unsigned char *data, unsigned int size);
#endif
/* retrive header by rpmdbid, returns 0 if not found, -1 on error */
static int
getrpm_dbid(struct rpmdbstate *state, Id rpmdbid)
{
#if defined(HAVE_RPMDBNEXTITERATORHEADERBLOB) && !defined(ENABLE_RPMPKG_LIBRPM)
const unsigned char *uh;
unsigned int uhlen;
#else
Header h;
#endif
rpmdbMatchIterator mi;
unsigned int offset = rpmdbid;
if (rpmdbid <= 0)
return pool_error(state->pool, -1, "illegal rpmdbid %d", rpmdbid);
if (state->dbenvopened != 1 && !opendbenv(state))
return -1;
mi = rpmdbInitIterator(rpmtsGetRdb(state->ts), RPMDBI_PACKAGES, &offset, sizeof(offset));
#if defined(HAVE_RPMDBNEXTITERATORHEADERBLOB) && !defined(ENABLE_RPMPKG_LIBRPM)
uh = rpmdbNextIteratorHeaderBlob(mi, &uhlen);
if (!uh)
{
rpmdbFreeIterator(mi);
return 0;
}
if (!headfromhdrblob(state, uh, uhlen))
{
rpmdbFreeIterator(mi);
return -1;
}
#else
h = rpmdbNextIterator(mi);
if (!h)
{
rpmdbFreeIterator(mi);
return 0;
}
if (!rpm_byrpmh(state, h))
{
rpmdbFreeIterator(mi);
return -1;
}
#endif
mi = rpmdbFreeIterator(mi);
return rpmdbid;
}
static int
count_headers(struct rpmdbstate *state)
{
int count;
rpmdbMatchIterator mi;
if (state->dbenvopened != 1 && !opendbenv(state))
return 0;
mi = rpmdbInitIterator(rpmtsGetRdb(state->ts), RPMDBI_NAME, NULL, 0);
count = rpmdbGetIteratorCount(mi);
rpmdbFreeIterator(mi);
return count;
}
static int
pkgdb_cursor_open(struct rpmdbstate *state)
{
state->mi = rpmdbInitIterator(rpmtsGetRdb(state->ts), RPMDBI_PACKAGES, NULL, 0);
return 0;
}
static void
pkgdb_cursor_close(struct rpmdbstate *state)
{
rpmdbFreeIterator(state->mi);
state->mi = 0;
}
static Id
pkgdb_cursor_getrpm(struct rpmdbstate *state)
{
#if defined(HAVE_RPMDBNEXTITERATORHEADERBLOB) && !defined(ENABLE_RPMPKG_LIBRPM)
const unsigned char *uh;
unsigned int uhlen;
while ((uh = rpmdbNextIteratorHeaderBlob(state->mi, &uhlen)) != 0)
{
Id dbid = rpmdbGetIteratorOffset(state->mi);
if (!headfromhdrblob(state, uh, uhlen))
continue;
return dbid;
}
#else
Header h;
while ((h = rpmdbNextIterator(state->mi)))
{
Id dbid = rpmdbGetIteratorOffset(state->mi);
if (!rpm_byrpmh(state, h))
continue;
return dbid;
}
#endif
return 0;
}
static int
hash_name_index(struct rpmdbstate *state, Chksum *chk)
{
rpmdbIndexIterator ii;
const void *key;
size_t keylen;
if (state->dbenvopened != 1 && !opendbenv(state))
return -1;
ii = rpmdbIndexIteratorInit(rpmtsGetRdb(state->ts), RPMDBI_NAME);
if (!ii)
return -1;
while (rpmdbIndexIteratorNext(ii, &key, &keylen) == 0)
{
unsigned int i, npkgs = rpmdbIndexIteratorNumPkgs(ii);
solv_chksum_add(chk, key, (int)keylen);
for (i = 0; i < npkgs; i++)
{
unsigned int offset = rpmdbIndexIteratorPkgOffset(ii, i);
solv_chksum_add(chk, &offset, sizeof(offset));
}
}
rpmdbIndexIteratorFree(ii);
return 0;
}
|