summaryrefslogtreecommitdiff
path: root/ext/repo_rpmdb_bdb.h
diff options
context:
space:
mode:
Diffstat (limited to 'ext/repo_rpmdb_bdb.h')
-rw-r--r--ext/repo_rpmdb_bdb.h84
1 files changed, 31 insertions, 53 deletions
diff --git a/ext/repo_rpmdb_bdb.h b/ext/repo_rpmdb_bdb.h
index a8b8500..1c83317 100644
--- a/ext/repo_rpmdb_bdb.h
+++ b/ext/repo_rpmdb_bdb.h
@@ -43,11 +43,11 @@ struct rpmdbstate {
char *rootdir;
RpmHead *rpmhead; /* header storage space */
- int rpmheadsize;
+ unsigned int rpmheadsize;
int dbenvopened; /* database environment opened */
int pkgdbopened; /* package database openend */
- int is_ostree; /* read-only db that lives in /usr/share/rpm */
+ const char *dbpath; /* path to the database */
DB_ENV *dbenv; /* database environment */
DB *db; /* packages database */
@@ -70,17 +70,20 @@ access_rootdir(struct rpmdbstate *state, const char *dir, int mode)
}
static void
-detect_ostree(struct rpmdbstate *state)
+detect_dbpath(struct rpmdbstate *state)
{
- state->is_ostree = access_rootdir(state, "/var/lib/rpm", W_OK) == -1 &&
- access_rootdir(state, "/usr/share/rpm/Packages", R_OK) == 0 ? 1 : -1;
+ 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_name(struct rpmdbstate *state, char *dbname, struct stat *statbuf, int seterror)
{
char *dbpath;
- dbpath = solv_dupjoin(state->rootdir, state->is_ostree > 0 ? "/usr/share/rpm/" : "/var/lib/rpm/", dbname);
+ if (!state->dbpath)
+ detect_dbpath(state);
+ dbpath = solv_dupjoin(state->rootdir, state->dbpath, dbname);
if (stat(dbpath, statbuf))
{
if (seterror)
@@ -95,9 +98,7 @@ stat_database_name(struct rpmdbstate *state, char *dbname, struct stat *statbuf,
static int
stat_database(struct rpmdbstate *state, struct stat *statbuf)
{
- if (!state->is_ostree)
- detect_ostree(state);
- return stat_database_name(state, "Packages", statbuf, 1);
+ return stat_database_name(state, "/Packages", statbuf, 1);
}
@@ -171,7 +172,6 @@ serialize_dbenv_ops(struct rpmdbstate *state)
static int
opendbenv(struct rpmdbstate *state)
{
- const char *rootdir = state->rootdir;
char *dbpath;
DB_ENV *dbenv = 0;
int r;
@@ -181,16 +181,16 @@ opendbenv(struct rpmdbstate *state)
#if (defined(FEDORA) || defined(MAGEIA)) && (DB_VERSION_MAJOR >= 5 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 5))
dbenv->set_thread_count(dbenv, 8);
#endif
- dbpath = solv_dupjoin(rootdir, "/var/lib/rpm", 0);
- state->is_ostree = -1;
+ state->dbpath = "/var/lib/rpm";
+ dbpath = solv_dupjoin(state->rootdir, state->dbpath, 0);
if (access(dbpath, W_OK) == -1)
{
- free(dbpath);
- dbpath = solv_dupjoin(rootdir, "/usr/share/rpm/Packages", 0);
- if (access(dbpath, R_OK) == 0)
- state->is_ostree = 1;
- free(dbpath);
- dbpath = solv_dupjoin(rootdir, state->is_ostree > 0 ? "/usr/share/rpm" : "/var/lib/rpm", 0);
+ if (access_rootdir(state, "/usr/share/rpm/Packages", R_OK) == 0)
+ {
+ state->dbpath = "/usr/share/rpm";
+ free(dbpath);
+ dbpath = solv_dupjoin(state->rootdir, state->dbpath, 0);
+ }
r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
}
else
@@ -387,35 +387,7 @@ getinstalledrpmdbids(struct rpmdbstate *state, const char *index, const char *ma
return entries;
}
-/* common code, return dbid on success, -1 on error */
-static int
-getrpm_dbdata(struct rpmdbstate *state, DBT *dbdata, int dbid)
-{
- unsigned int dsize, cnt, l;
- RpmHead *rpmhead;
-
- if (dbdata->size < 8)
- return pool_error(state->pool, -1, "corrupt rpm database (size)");
- cnt = getu32((const unsigned char *)dbdata->data);
- dsize = getu32((const unsigned char *)dbdata->data + 4);
- if (cnt >= MAX_HDR_CNT || dsize >= MAX_HDR_DSIZE)
- return pool_error(state->pool, -1, "corrupt rpm database (cnt/dcnt)");
- l = cnt * 16 + dsize;
- if (8 + l > dbdata->size)
- return pool_error(state->pool, -1, "corrupt rpm database (data size)");
- if (l + 1 > state->rpmheadsize)
- {
- state->rpmheadsize = l + 128;
- state->rpmhead = solv_realloc(state->rpmhead, sizeof(*rpmhead) + state->rpmheadsize);
- }
- rpmhead = state->rpmhead;
- rpmhead->cnt = cnt;
- rpmhead->dcnt = dsize;
- memcpy(rpmhead->data, (unsigned char *)dbdata->data + 8, l);
- rpmhead->data[l] = 0;
- rpmhead->dp = rpmhead->data + cnt * 16;
- return dbid;
-}
+static int headfromhdrblob(struct rpmdbstate *state, const unsigned char *data, unsigned int size);
/* retrive header by rpmdbid, returns 0 if not found, -1 on error */
static int
@@ -438,7 +410,9 @@ getrpm_dbid(struct rpmdbstate *state, Id dbid)
dbdata.size = 0;
if (state->db->get(state->db, NULL, &dbkey, &dbdata, 0))
return 0;
- return getrpm_dbdata(state, &dbdata, dbid);
+ if (!headfromhdrblob(state, (const unsigned char *)dbdata.data, (unsigned int)dbdata.size))
+ return -1;
+ return dbid;
}
static int
@@ -452,7 +426,7 @@ count_headers(struct rpmdbstate *state)
DBT dbkey;
DBT dbdata;
- if (stat_database_name(state, "Name", &statbuf, 0))
+ if (stat_database_name(state, "/Name", &statbuf, 0))
return 0;
memset(&dbkey, 0, sizeof(dbkey));
memset(&dbdata, 0, sizeof(dbdata));
@@ -512,8 +486,11 @@ pkgdb_cursor_getrpm(struct rpmdbstate *state)
if (dbkey.size != 4)
return pool_error(state->pool, -1, "corrupt Packages database (key size)");
dbid = db2rpmdbid(dbkey.data, state->byteswapped);
- if (dbid) /* ignore join key */
- return getrpm_dbdata(state, &dbdata, dbid);
+ if (!dbid)
+ continue; /* ignore join key */
+ if (!headfromhdrblob(state, (const unsigned char *)dbdata.data, (unsigned int)dbdata.size))
+ return -1;
+ return dbid;
}
return 0; /* no more entries */
}
@@ -525,7 +502,9 @@ hash_name_index(struct rpmdbstate *state, Chksum *chk)
int fd, l;
char buf[4096];
- dbpath = solv_dupjoin(state->rootdir, state->is_ostree > 0 ? "/usr/share/rpm/" : "/var/lib/rpm/", "Name");
+ if (!state->dbpath)
+ detect_dbpath(state);
+ dbpath = solv_dupjoin(state->rootdir, state->dbpath, "/Name");
if ((fd = open(dbpath, O_RDONLY)) < 0)
return -1;
while ((l = read(fd, buf, sizeof(buf))) > 0)
@@ -534,4 +513,3 @@ hash_name_index(struct rpmdbstate *state, Chksum *chk)
return 0;
}
-