Skip to content

Commit 10a1ecf

Browse files
committed
Put the path to the database in the state
This allows us to make the path configurable if we need to do this in the future. It also makes the ostree handling a bit easier.
1 parent 90fed1e commit 10a1ecf

2 files changed

Lines changed: 38 additions & 34 deletions

File tree

ext/repo_rpmdb_bdb.h

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ struct rpmdbstate {
4747

4848
int dbenvopened; /* database environment opened */
4949
int pkgdbopened; /* package database openend */
50-
int is_ostree; /* read-only db that lives in /usr/share/rpm */
50+
const char *dbpath; /* path to the database */
5151

5252
DB_ENV *dbenv; /* database environment */
5353
DB *db; /* packages database */
@@ -70,17 +70,20 @@ access_rootdir(struct rpmdbstate *state, const char *dir, int mode)
7070
}
7171

7272
static void
73-
detect_ostree(struct rpmdbstate *state)
73+
detect_dbpath(struct rpmdbstate *state)
7474
{
75-
state->is_ostree = access_rootdir(state, "/var/lib/rpm", W_OK) == -1 &&
76-
access_rootdir(state, "/usr/share/rpm/Packages", R_OK) == 0 ? 1 : -1;
75+
state->dbpath = access_rootdir(state, "/var/lib/rpm", W_OK) == -1
76+
&& access_rootdir(state, "/usr/share/rpm/Packages", R_OK) == 0
77+
? "/usr/share/rpm" : "/var/lib/rpm";
7778
}
7879

7980
static int
8081
stat_database_name(struct rpmdbstate *state, char *dbname, struct stat *statbuf, int seterror)
8182
{
8283
char *dbpath;
83-
dbpath = solv_dupjoin(state->rootdir, state->is_ostree > 0 ? "/usr/share/rpm/" : "/var/lib/rpm/", dbname);
84+
if (!state->dbpath)
85+
detect_dbpath(state);
86+
dbpath = solv_dupjoin(state->rootdir, state->dbpath, dbname);
8487
if (stat(dbpath, statbuf))
8588
{
8689
if (seterror)
@@ -95,9 +98,7 @@ stat_database_name(struct rpmdbstate *state, char *dbname, struct stat *statbuf,
9598
static int
9699
stat_database(struct rpmdbstate *state, struct stat *statbuf)
97100
{
98-
if (!state->is_ostree)
99-
detect_ostree(state);
100-
return stat_database_name(state, "Packages", statbuf, 1);
101+
return stat_database_name(state, "/Packages", statbuf, 1);
101102
}
102103

103104

@@ -171,7 +172,6 @@ serialize_dbenv_ops(struct rpmdbstate *state)
171172
static int
172173
opendbenv(struct rpmdbstate *state)
173174
{
174-
const char *rootdir = state->rootdir;
175175
char *dbpath;
176176
DB_ENV *dbenv = 0;
177177
int r;
@@ -181,16 +181,16 @@ opendbenv(struct rpmdbstate *state)
181181
#if (defined(FEDORA) || defined(MAGEIA)) && (DB_VERSION_MAJOR >= 5 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 5))
182182
dbenv->set_thread_count(dbenv, 8);
183183
#endif
184-
dbpath = solv_dupjoin(rootdir, "/var/lib/rpm", 0);
185-
state->is_ostree = -1;
184+
state->dbpath = "/var/lib/rpm";
185+
dbpath = solv_dupjoin(state->rootdir, state->dbpath, 0);
186186
if (access(dbpath, W_OK) == -1)
187187
{
188-
free(dbpath);
189-
dbpath = solv_dupjoin(rootdir, "/usr/share/rpm/Packages", 0);
190-
if (access(dbpath, R_OK) == 0)
191-
state->is_ostree = 1;
192-
free(dbpath);
193-
dbpath = solv_dupjoin(rootdir, state->is_ostree > 0 ? "/usr/share/rpm" : "/var/lib/rpm", 0);
188+
if (access_rootdir(state, "/usr/share/rpm/Packages", R_OK) == 0)
189+
{
190+
state->dbpath = "/usr/share/rpm";
191+
free(dbpath);
192+
dbpath = solv_dupjoin(state->rootdir, state->dbpath, 0);
193+
}
194194
r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
195195
}
196196
else
@@ -426,7 +426,7 @@ count_headers(struct rpmdbstate *state)
426426
DBT dbkey;
427427
DBT dbdata;
428428

429-
if (stat_database_name(state, "Name", &statbuf, 0))
429+
if (stat_database_name(state, "/Name", &statbuf, 0))
430430
return 0;
431431
memset(&dbkey, 0, sizeof(dbkey));
432432
memset(&dbdata, 0, sizeof(dbdata));
@@ -502,7 +502,9 @@ hash_name_index(struct rpmdbstate *state, Chksum *chk)
502502
int fd, l;
503503
char buf[4096];
504504

505-
dbpath = solv_dupjoin(state->rootdir, state->is_ostree > 0 ? "/usr/share/rpm/" : "/var/lib/rpm/", "Name");
505+
if (!state->dbpath)
506+
detect_dbpath(state);
507+
dbpath = solv_dupjoin(state->rootdir, state->dbpath, "/Name");
506508
if ((fd = open(dbpath, O_RDONLY)) < 0)
507509
return -1;
508510
while ((l = read(fd, buf, sizeof(buf))) > 0)

ext/repo_rpmdb_librpm.h

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct rpmdbstate {
2323
unsigned int rpmheadsize;
2424

2525
int dbenvopened; /* database environment opened */
26-
int is_ostree; /* read-only db that lives in /usr/share/rpm */
26+
const char *dbpath; /* path to the database */
2727

2828
rpmts ts;
2929
rpmdbMatchIterator mi; /* iterator over packages database */
@@ -43,21 +43,22 @@ access_rootdir(struct rpmdbstate *state, const char *dir, int mode)
4343
}
4444

4545
static void
46-
detect_ostree(struct rpmdbstate *state)
46+
detect_dbpath(struct rpmdbstate *state)
4747
{
48-
state->is_ostree = access_rootdir(state, "/var/lib/rpm", W_OK) == -1 &&
49-
access_rootdir(state, "/usr/share/rpm/Packages", R_OK) == 0 ? 1 : -1;
48+
state->dbpath = access_rootdir(state, "/var/lib/rpm", W_OK) == -1
49+
&& access_rootdir(state, "/usr/share/rpm/Packages", R_OK) == 0
50+
? "/usr/share/rpm" : "/var/lib/rpm";
5051
}
5152

5253
static int
5354
stat_database(struct rpmdbstate *state, struct stat *statbuf)
5455
{
5556
static const char *dbname[] = {
56-
"Packages",
57-
"Packages.db",
58-
"rpmdb.sqlite",
59-
"data.mdb",
60-
"Packages", /* for error reporting */
57+
"/Packages",
58+
"/Packages.db",
59+
"/rpmdb.sqlite",
60+
"/data.mdb",
61+
"/Packages", /* for error reporting */
6162
0,
6263
};
6364
int i;
@@ -66,11 +67,11 @@ stat_database(struct rpmdbstate *state, struct stat *statbuf)
6667
if (state->dbenvopened == 1)
6768
return rpmdbFStat(rpmtsGetRdb(state->ts), statbuf);
6869
#endif
69-
if (!state->is_ostree)
70-
detect_ostree(state);
70+
if (!state->dbpath)
71+
detect_dbpath(state);
7172
for (i = 0; ; i++)
7273
{
73-
char *dbpath = solv_dupjoin(state->rootdir, state->is_ostree > 0 ? "/usr/share/rpm/" : "/var/lib/rpm/", dbname[i]);
74+
char *dbpath = solv_dupjoin(state->rootdir, state->dbpath, dbname[i]);
7475
if (!stat(dbpath, statbuf))
7576
{
7677
free(dbpath);
@@ -92,11 +93,12 @@ stat_database(struct rpmdbstate *state, struct stat *statbuf)
9293
static int
9394
opendbenv(struct rpmdbstate *state)
9495
{
95-
const char *rootdir = state->rootdir;
9696
rpmts ts;
9797
char *dbpath;
98-
detect_ostree(state);
99-
dbpath = solv_dupjoin("_dbpath ", rootdir, state->is_ostree > 0 ? "/usr/share/rpm" : "/var/lib/rpm");
98+
99+
if (!state->dbpath)
100+
detect_dbpath(state);
101+
dbpath = solv_dupjoin("_dbpath ", state->rootdir, state->dbpath);
100102
rpmDefineMacro(NULL, dbpath, 0);
101103
solv_free(dbpath);
102104
ts = rpmtsCreate();

0 commit comments

Comments
 (0)