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_name(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 stat_database(struct rpmdbstate *state, struct stat *statbuf)
78 return stat_database_name(state, "Packages", statbuf, 1);
83 db2rpmdbid(unsigned char *db, int byteswapped)
85 #ifdef RPM5_BIG_ENDIAN_ID
86 return db[0] << 24 | db[1] << 16 | db[2] << 8 | db[3];
88 # if defined(WORDS_BIGENDIAN)
93 return db[0] << 24 | db[1] << 16 | db[2] << 8 | db[3];
95 return db[3] << 24 | db[2] << 16 | db[1] << 8 | db[0];
100 rpmdbid2db(unsigned char *db, Id id, int byteswapped)
102 #ifdef RPM5_BIG_ENDIAN_ID
103 db[0] = id >> 24, db[1] = id >> 16, db[2] = id >> 8, db[3] = id;
105 # if defined(WORDS_BIGENDIAN)
110 db[0] = id >> 24, db[1] = id >> 16, db[2] = id >> 8, db[3] = id;
112 db[3] = id >> 24, db[2] = id >> 16, db[1] = id >> 8, db[0] = id;
116 #if defined(FEDORA) || defined(MAGEIA)
118 serialize_dbenv_ops(struct rpmdbstate *state)
125 lpath = solv_dupjoin(state->rootdir, "/var/lib/rpm/.dbenv.lock", 0);
126 oldmask = umask(022);
127 fd = open(lpath, (O_RDWR|O_CREAT), 0644);
132 memset(&fl, 0, sizeof(fl));
134 fl.l_whence = SEEK_SET;
137 if (fcntl(fd, F_SETLKW, &fl) != -1)
148 /* should look in /usr/lib/rpm/macros instead, but we want speed... */
150 opendbenv(struct rpmdbstate *state)
152 const char *rootdir = state->rootdir;
157 if (db_env_create(&dbenv, 0))
158 return pool_error(state->pool, 0, "db_env_create: %s", strerror(errno));
159 #if (defined(FEDORA) || defined(MAGEIA)) && (DB_VERSION_MAJOR >= 5 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 5))
160 dbenv->set_thread_count(dbenv, 8);
162 dbpath = solv_dupjoin(rootdir, "/var/lib/rpm", 0);
163 if (access(dbpath, W_OK) == -1)
166 dbpath = solv_dupjoin(rootdir, "/usr/share/rpm/Packages", 0);
167 if (access(dbpath, R_OK) == 0)
168 state->is_ostree = 1;
170 dbpath = solv_dupjoin(rootdir, state->is_ostree ? "/usr/share/rpm" : "/var/lib/rpm", 0);
171 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
175 #if defined(FEDORA) || defined(MAGEIA)
176 int serialize_fd = serialize_dbenv_ops(state);
177 int eflags = DB_CREATE|DB_INIT_CDB|DB_INIT_MPOOL;
178 r = dbenv->open(dbenv, dbpath, eflags, 0644);
179 /* see rpm commit 2822ccbcdf3e898b960fafb23c4d571e26cef0a4 */
180 if (r == DB_VERSION_MISMATCH)
182 eflags |= DB_PRIVATE;
183 dbenv->errx(dbenv, "warning: DB_VERSION_MISMATCH, retrying with DB_PRIVATE");
184 r = dbenv->open(dbenv, dbpath, eflags, 0644);
186 if (serialize_fd >= 0)
189 r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
194 pool_error(state->pool, 0, "dbenv->open: %s", strerror(errno));
196 dbenv->close(dbenv, 0);
200 state->dbenv = dbenv;
201 state->dbenvopened = 1;
206 closedbenv(struct rpmdbstate *state)
208 #if defined(FEDORA) || defined(MAGEIA)
214 #if defined(FEDORA) || defined(MAGEIA)
215 (void)state->dbenv->get_open_flags(state->dbenv, &eflags);
216 if (!(eflags & DB_PRIVATE))
218 int serialize_fd = serialize_dbenv_ops(state);
219 state->dbenv->close(state->dbenv, 0);
220 if (serialize_fd >= 0)
224 state->dbenv->close(state->dbenv, 0);
226 state->dbenv->close(state->dbenv, 0);
229 state->dbenvopened = 0;
233 openpkgdb(struct rpmdbstate *state)
235 if (state->pkgdbopened)
236 return state->pkgdbopened > 0 ? 1 : 0;
237 state->pkgdbopened = -1;
238 if (state->dbenvopened != 1 && !opendbenv(state))
240 if (db_create(&state->db, state->dbenv, 0))
242 pool_error(state->pool, 0, "db_create: %s", strerror(errno));
247 if (state->db->open(state->db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
249 pool_error(state->pool, 0, "db->open Packages: %s", strerror(errno));
250 state->db->close(state->db, 0);
255 if (state->db->get_byteswapped(state->db, &state->byteswapped))
257 pool_error(state->pool, 0, "db->get_byteswapped: %s", strerror(errno));
258 state->db->close(state->db, 0);
263 state->pkgdbopened = 1;
268 closepkgdb(struct rpmdbstate *state)
272 state->db->close(state->db, 0);
274 state->pkgdbopened = 0;
277 /* get the rpmdbids of all installed packages from the Name index database.
278 * This is much faster then querying the big Packages database */
279 static struct rpmdbentry *
280 getinstalledrpmdbids(struct rpmdbstate *state, const char *index, const char *match, int *nentriesp, char **namedatap, int keep_gpg_pubkey)
294 struct rpmdbentry *entries = 0;
301 if (state->dbenvopened != 1 && !opendbenv(state))
303 dbenv = state->dbenv;
304 if (db_create(&db, dbenv, 0))
306 pool_error(state->pool, 0, "db_create: %s", strerror(errno));
309 if (db->open(db, 0, index, 0, DB_UNKNOWN, DB_RDONLY, 0664))
311 pool_error(state->pool, 0, "db->open %s: %s", index, strerror(errno));
315 if (db->get_byteswapped(db, &byteswapped))
317 pool_error(state->pool, 0, "db->get_byteswapped: %s", strerror(errno));
321 if (db->cursor(db, NULL, &dbc, 0))
323 pool_error(state->pool, 0, "db->cursor: %s", strerror(errno));
327 memset(&dbkey, 0, sizeof(dbkey));
328 memset(&dbdata, 0, sizeof(dbdata));
331 dbkey.data = (void *)match;
332 dbkey.size = strlen(match);
334 while (dbc->c_get(dbc, &dbkey, &dbdata, match ? DB_SET : DB_NEXT) == 0)
336 if (!match && !keep_gpg_pubkey && dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
343 namedata = solv_extend(namedata, namedatal, dbkey.size + 1, 1, NAMEDATA_BLOCK);
344 memcpy(namedata + namedatal, dbkey.data, dbkey.size);
345 namedata[namedatal + dbkey.size] = 0;
346 namedatal += dbkey.size + 1;
348 while(dl >= RPM_INDEX_SIZE)
350 entries = solv_extend(entries, nentries, 1, sizeof(*entries), ENTRIES_BLOCK);
351 entries[nentries].rpmdbid = db2rpmdbid(dp, byteswapped);
352 entries[nentries].nameoff = nameoff;
354 dp += RPM_INDEX_SIZE;
355 dl -= RPM_INDEX_SIZE;
362 /* make sure that enteries is != 0 if there was no error */
364 entries = solv_extend(entries, 1, 1, sizeof(*entries), ENTRIES_BLOCK);
365 *nentriesp = nentries;
367 *namedatap = namedata;
371 /* common code, return dbid on success, -1 on error */
373 getrpm_dbdata(struct rpmdbstate *state, DBT *dbdata, int dbid)
375 unsigned int dsize, cnt, l;
378 if (dbdata->size < 8)
379 return pool_error(state->pool, -1, "corrupt rpm database (size)");
380 cnt = getu32((const unsigned char *)dbdata->data);
381 dsize = getu32((const unsigned char *)dbdata->data + 4);
382 if (cnt >= MAX_HDR_CNT || dsize >= MAX_HDR_DSIZE)
383 return pool_error(state->pool, -1, "corrupt rpm database (cnt/dcnt)");
384 l = cnt * 16 + dsize;
385 if (8 + l > dbdata->size)
386 return pool_error(state->pool, -1, "corrupt rpm database (data size)");
387 if (l + 1 > state->rpmheadsize)
389 state->rpmheadsize = l + 128;
390 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*rpmhead) + state->rpmheadsize);
392 rpmhead = state->rpmhead;
394 rpmhead->dcnt = dsize;
395 memcpy(rpmhead->data, (unsigned char *)dbdata->data + 8, l);
396 rpmhead->data[l] = 0;
397 rpmhead->dp = rpmhead->data + cnt * 16;
401 /* retrive header by rpmdbid, returns 0 if not found, -1 on error */
403 getrpm_dbid(struct rpmdbstate *state, Id dbid)
405 unsigned char buf[4];
410 return pool_error(state->pool, -1, "illegal rpmdbid %d", dbid);
411 if (state->pkgdbopened != 1 && !openpkgdb(state))
413 rpmdbid2db(buf, dbid, state->byteswapped);
414 memset(&dbkey, 0, sizeof(dbkey));
415 memset(&dbdata, 0, sizeof(dbdata));
420 if (state->db->get(state->db, NULL, &dbkey, &dbdata, 0))
422 return getrpm_dbdata(state, &dbdata, dbid);
426 count_headers(struct rpmdbstate *state)
428 Pool *pool = state->pool;
436 if (stat_database_name(state, "Name", &statbuf, 0))
438 memset(&dbkey, 0, sizeof(dbkey));
439 memset(&dbdata, 0, sizeof(dbdata));
440 if (db_create(&db, state->dbenv, 0))
442 pool_error(pool, 0, "db_create: %s", strerror(errno));
445 if (db->open(db, 0, "Name", 0, DB_UNKNOWN, DB_RDONLY, 0664))
447 pool_error(pool, 0, "db->open Name: %s", strerror(errno));
451 if (db->cursor(db, NULL, &dbc, 0))
454 pool_error(pool, 0, "db->cursor: %s", strerror(errno));
457 while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
458 count += dbdata.size / RPM_INDEX_SIZE;
465 pkgdb_cursor_open(struct rpmdbstate *state)
467 if (state->db->cursor(state->db, NULL, &state->dbc, 0))
468 return pool_error(state->pool, -1, "db->cursor failed");
473 pkgdb_cursor_close(struct rpmdbstate *state)
475 state->dbc->c_close(state->dbc);
479 /* retrive header by berkeleydb cursor, returns 0 on EOF, -1 on error */
481 pkgdb_cursor_getrpm(struct rpmdbstate *state)
487 memset(&dbkey, 0, sizeof(dbkey));
488 memset(&dbdata, 0, sizeof(dbdata));
489 while (state->dbc->c_get(state->dbc, &dbkey, &dbdata, DB_NEXT) == 0)
492 return pool_error(state->pool, -1, "corrupt Packages database (key size)");
493 dbid = db2rpmdbid(dbkey.data, state->byteswapped);
494 if (dbid) /* ignore join key */
495 return getrpm_dbdata(state, &dbdata, dbid);
497 return 0; /* no more entries */