2 * Copyright (c) 2018 SUSE Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
11 * Use BerkeleyDB to access the rpm database
16 #if !defined(DB_CREATE) && !defined(ENABLE_RPMDB_LIBRPM)
17 # if defined(SUSE) || defined(HAVE_RPM_DB_H)
25 # include <rpm/rpmversion.h>
26 # if RPMLIB_VERSION < RPMLIB_VERSION_ENCODE(5,3,_,0,0,_)
27 # define RPM_INDEX_SIZE 8 /* rpmdbid + array index */
29 # define RPM_INDEX_SIZE 4 /* just the rpmdbid */
30 # define RPM5_BIG_ENDIAN_ID
33 # define RPM_INDEX_SIZE 8 /* rpmdbid + array index */
37 /******************************************************************/
45 RpmHead *rpmhead; /* header storage space */
48 int dbenvopened; /* database environment opened */
49 int pkgdbopened; /* package database openend */
50 int is_ostree; /* read-only db that lives in /usr/share/rpm */
52 DB_ENV *dbenv; /* database environment */
53 DB *db; /* packages database */
54 int byteswapped; /* endianess of packages database */
55 DBC *dbc; /* iterator over packages database */
60 stat_database(struct rpmdbstate *state, char *dbname, struct stat *statbuf, int seterror)
63 dbpath = solv_dupjoin(state->rootdir, state->is_ostree ? "/usr/share/rpm/" : "/var/lib/rpm/", dbname);
64 if (stat(dbpath, statbuf))
67 pool_error(state->pool, -1, "%s: %s", dbpath, strerror(errno));
76 db2rpmdbid(unsigned char *db, int byteswapped)
78 #ifdef RPM5_BIG_ENDIAN_ID
79 return db[0] << 24 | db[1] << 16 | db[2] << 8 | db[3];
81 # if defined(WORDS_BIGENDIAN)
86 return db[0] << 24 | db[1] << 16 | db[2] << 8 | db[3];
88 return db[3] << 24 | db[2] << 16 | db[1] << 8 | db[0];
93 rpmdbid2db(unsigned char *db, Id id, int byteswapped)
95 #ifdef RPM5_BIG_ENDIAN_ID
96 db[0] = id >> 24, db[1] = id >> 16, db[2] = id >> 8, db[3] = id;
98 # if defined(WORDS_BIGENDIAN)
103 db[0] = id >> 24, db[1] = id >> 16, db[2] = id >> 8, db[3] = id;
105 db[3] = id >> 24, db[2] = id >> 16, db[1] = id >> 8, db[0] = id;
109 #if defined(FEDORA) || defined(MAGEIA)
111 serialize_dbenv_ops(struct rpmdbstate *state)
118 lpath = solv_dupjoin(state->rootdir, "/var/lib/rpm/.dbenv.lock", 0);
119 oldmask = umask(022);
120 fd = open(lpath, (O_RDWR|O_CREAT), 0644);
125 memset(&fl, 0, sizeof(fl));
127 fl.l_whence = SEEK_SET;
130 if (fcntl(fd, F_SETLKW, &fl) != -1)
141 /* should look in /usr/lib/rpm/macros instead, but we want speed... */
143 opendbenv(struct rpmdbstate *state)
145 const char *rootdir = state->rootdir;
150 if (db_env_create(&dbenv, 0))
151 return pool_error(state->pool, 0, "db_env_create: %s", strerror(errno));
152 #if (defined(FEDORA) || defined(MAGEIA)) && (DB_VERSION_MAJOR >= 5 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 5))
153 dbenv->set_thread_count(dbenv, 8);
155 dbpath = solv_dupjoin(rootdir, "/var/lib/rpm", 0);
156 if (access(dbpath, W_OK) == -1)
159 dbpath = solv_dupjoin(rootdir, "/usr/share/rpm/Packages", 0);
160 if (access(dbpath, R_OK) == 0)
161 state->is_ostree = 1;
163 dbpath = solv_dupjoin(rootdir, state->is_ostree ? "/usr/share/rpm" : "/var/lib/rpm", 0);
164 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
168 #if defined(FEDORA) || defined(MAGEIA)
169 int serialize_fd = serialize_dbenv_ops(state);
170 int eflags = DB_CREATE|DB_INIT_CDB|DB_INIT_MPOOL;
171 r = dbenv->open(dbenv, dbpath, eflags, 0644);
172 /* see rpm commit 2822ccbcdf3e898b960fafb23c4d571e26cef0a4 */
173 if (r == DB_VERSION_MISMATCH)
175 eflags |= DB_PRIVATE;
176 dbenv->errx(dbenv, "warning: DB_VERSION_MISMATCH, retrying with DB_PRIVATE");
177 r = dbenv->open(dbenv, dbpath, eflags, 0644);
179 if (serialize_fd >= 0)
182 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
187 pool_error(state->pool, 0, "dbenv->open: %s", strerror(errno));
189 dbenv->close(dbenv, 0);
193 state->dbenv = dbenv;
194 state->dbenvopened = 1;
199 closedbenv(struct rpmdbstate *state)
201 #if defined(FEDORA) || defined(MAGEIA)
207 #if defined(FEDORA) || defined(MAGEIA)
208 (void)state->dbenv->get_open_flags(state->dbenv, &eflags);
209 if (!(eflags & DB_PRIVATE))
211 int serialize_fd = serialize_dbenv_ops(state);
212 state->dbenv->close(state->dbenv, 0);
213 if (serialize_fd >= 0)
217 state->dbenv->close(state->dbenv, 0);
219 state->dbenv->close(state->dbenv, 0);
222 state->dbenvopened = 0;
226 openpkgdb(struct rpmdbstate *state)
228 if (state->pkgdbopened)
229 return state->pkgdbopened > 0 ? 1 : 0;
230 state->pkgdbopened = -1;
231 if (state->dbenvopened != 1 && !opendbenv(state))
233 if (db_create(&state->db, state->dbenv, 0))
235 pool_error(state->pool, 0, "db_create: %s", strerror(errno));
240 if (state->db->open(state->db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
242 pool_error(state->pool, 0, "db->open Packages: %s", strerror(errno));
243 state->db->close(state->db, 0);
248 if (state->db->get_byteswapped(state->db, &state->byteswapped))
250 pool_error(state->pool, 0, "db->get_byteswapped: %s", strerror(errno));
251 state->db->close(state->db, 0);
256 state->pkgdbopened = 1;
261 closepkgdb(struct rpmdbstate *state)
265 state->db->close(state->db, 0);
267 state->pkgdbopened = 0;
270 /* get the rpmdbids of all installed packages from the Name index database.
271 * This is much faster then querying the big Packages database */
272 static struct rpmdbentry *
273 getinstalledrpmdbids(struct rpmdbstate *state, const char *index, const char *match, int *nentriesp, char **namedatap, int keep_gpg_pubkey)
287 struct rpmdbentry *entries = 0;
294 if (state->dbenvopened != 1 && !opendbenv(state))
296 dbenv = state->dbenv;
297 if (db_create(&db, dbenv, 0))
299 pool_error(state->pool, 0, "db_create: %s", strerror(errno));
302 if (db->open(db, 0, index, 0, DB_UNKNOWN, DB_RDONLY, 0664))
304 pool_error(state->pool, 0, "db->open %s: %s", index, strerror(errno));
308 if (db->get_byteswapped(db, &byteswapped))
310 pool_error(state->pool, 0, "db->get_byteswapped: %s", strerror(errno));
314 if (db->cursor(db, NULL, &dbc, 0))
316 pool_error(state->pool, 0, "db->cursor: %s", strerror(errno));
320 memset(&dbkey, 0, sizeof(dbkey));
321 memset(&dbdata, 0, sizeof(dbdata));
324 dbkey.data = (void *)match;
325 dbkey.size = strlen(match);
327 while (dbc->c_get(dbc, &dbkey, &dbdata, match ? DB_SET : DB_NEXT) == 0)
329 if (!match && !keep_gpg_pubkey && dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
336 namedata = solv_extend(namedata, namedatal, dbkey.size + 1, 1, NAMEDATA_BLOCK);
337 memcpy(namedata + namedatal, dbkey.data, dbkey.size);
338 namedata[namedatal + dbkey.size] = 0;
339 namedatal += dbkey.size + 1;
341 while(dl >= RPM_INDEX_SIZE)
343 entries = solv_extend(entries, nentries, 1, sizeof(*entries), ENTRIES_BLOCK);
344 entries[nentries].rpmdbid = db2rpmdbid(dp, byteswapped);
345 entries[nentries].nameoff = nameoff;
347 dp += RPM_INDEX_SIZE;
348 dl -= RPM_INDEX_SIZE;
355 /* make sure that enteries is != 0 if there was no error */
357 entries = solv_extend(entries, 1, 1, sizeof(*entries), ENTRIES_BLOCK);
358 *nentriesp = nentries;
360 *namedatap = namedata;
364 /* common code, return dbid on success, -1 on error */
366 getrpm_dbdata(struct rpmdbstate *state, DBT *dbdata, int dbid)
368 unsigned int dsize, cnt, l;
371 if (dbdata->size < 8)
372 return pool_error(state->pool, -1, "corrupt rpm database (size)");
373 cnt = getu32((const unsigned char *)dbdata->data);
374 dsize = getu32((const unsigned char *)dbdata->data + 4);
375 if (cnt >= MAX_HDR_CNT || dsize >= MAX_HDR_DSIZE)
376 return pool_error(state->pool, -1, "corrupt rpm database (cnt/dcnt)");
377 l = cnt * 16 + dsize;
378 if (8 + l > dbdata->size)
379 return pool_error(state->pool, -1, "corrupt rpm database (data size)");
380 if (l + 1 > state->rpmheadsize)
382 state->rpmheadsize = l + 128;
383 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*rpmhead) + state->rpmheadsize);
385 rpmhead = state->rpmhead;
387 rpmhead->dcnt = dsize;
388 memcpy(rpmhead->data, (unsigned char *)dbdata->data + 8, l);
389 rpmhead->data[l] = 0;
390 rpmhead->dp = rpmhead->data + cnt * 16;
394 /* retrive header by rpmdbid, returns 0 if not found, -1 on error */
396 getrpm_dbid(struct rpmdbstate *state, Id dbid)
398 unsigned char buf[4];
403 return pool_error(state->pool, -1, "illegal rpmdbid %d", dbid);
404 if (state->pkgdbopened != 1 && !openpkgdb(state))
406 rpmdbid2db(buf, dbid, state->byteswapped);
407 memset(&dbkey, 0, sizeof(dbkey));
408 memset(&dbdata, 0, sizeof(dbdata));
413 if (state->db->get(state->db, NULL, &dbkey, &dbdata, 0))
415 return getrpm_dbdata(state, &dbdata, dbid);
419 count_headers(struct rpmdbstate *state)
421 Pool *pool = state->pool;
429 if (stat_database(state, "Name", &statbuf, 0))
431 memset(&dbkey, 0, sizeof(dbkey));
432 memset(&dbdata, 0, sizeof(dbdata));
433 if (db_create(&db, state->dbenv, 0))
435 pool_error(pool, 0, "db_create: %s", strerror(errno));
438 if (db->open(db, 0, "Name", 0, DB_UNKNOWN, DB_RDONLY, 0664))
440 pool_error(pool, 0, "db->open Name: %s", strerror(errno));
444 if (db->cursor(db, NULL, &dbc, 0))
447 pool_error(pool, 0, "db->cursor: %s", strerror(errno));
450 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
451 count += dbdata.size / RPM_INDEX_SIZE;
458 pkgdb_cursor_open(struct rpmdbstate *state)
460 if (state->db->cursor(state->db, NULL, &state->dbc, 0))
461 return pool_error(state->pool, -1, "db->cursor failed");
466 pkgdb_cursor_close(struct rpmdbstate *state)
468 state->dbc->c_close(state->dbc);
472 /* retrive header by berkeleydb cursor, returns 0 on EOF, -1 on error */
474 pkgdb_cursor_getrpm(struct rpmdbstate *state)
480 memset(&dbkey, 0, sizeof(dbkey));
481 memset(&dbdata, 0, sizeof(dbdata));
482 while (state->dbc->c_get(state->dbc, &dbkey, &dbdata, DB_NEXT) == 0)
485 return pool_error(state->pool, -1, "corrupt Packages database (key size)");
486 dbid = db2rpmdbid(dbkey.data, state->byteswapped);
487 if (dbid) /* ignore join key */
488 return getrpm_dbdata(state, &dbdata, dbid);
490 return 0; /* no more entries */