* we're running with a newer version of db4 than the database was
* created with.
*/
-static int db4_upgradedb(struct onak_db4_dbctx *privctx)
+static int db4_upgradedb(struct onak_dbctx *dbctx)
{
+ struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
DB *curdb = NULL;
int ret;
int i;
struct stat statbuf;
ssize_t written;
- snprintf(buf, sizeof(buf) - 1, "%s/%s", config.db_dir,
+ snprintf(buf, sizeof(buf) - 1, "%s/%s", dbctx->config->location,
DB4_UPGRADE_FILE);
lockfile_fd = open(buf, O_RDWR | O_CREAT | O_EXCL, 0600);
if (lockfile_fd < 0) {
if (written != strlen(buf)) {
logthing(LOGTHING_CRITICAL, "Couldn't write PID to lockfile: "
"%s", strerror(errno));
- snprintf(buf, sizeof(buf) - 1, "%s/%s", config.db_dir,
+ snprintf(buf, sizeof(buf) - 1, "%s/%s", dbctx->config->location,
DB4_UPGRADE_FILE);
unlink(buf);
return -1;
ret = db_env_create(&privctx->dbenv, 0);
if (ret == 0) {
privctx->dbenv->set_errcall(privctx->dbenv, &db4_errfunc);
- privctx->dbenv->remove(privctx->dbenv, config.db_dir, 0);
+ privctx->dbenv->remove(privctx->dbenv, dbctx->config->location, 0);
privctx->dbenv = NULL;
}
for (i = 0; i < privctx->numdbs; i++) {
ret = db_create(&curdb, NULL, 0);
if (ret == 0) {
snprintf(buf, sizeof(buf) - 1, "%s/keydb.%d.db",
- config.db_dir, i);
+ dbctx->config->location, i);
logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
curdb->upgrade(curdb, buf, 0);
curdb->close(curdb, 0);
ret = db_create(&curdb, NULL, 0);
if (ret == 0) {
- snprintf(buf, sizeof(buf) - 1, "%s/worddb", config.db_dir);
+ snprintf(buf, sizeof(buf) - 1, "%s/worddb", dbctx->config->location);
logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
curdb->upgrade(curdb, buf, 0);
curdb->close(curdb, 0);
ret = db_create(&curdb, NULL, 0);
if (ret == 0) {
- snprintf(buf, sizeof(buf) - 1, "%s/id32db", config.db_dir);
+ snprintf(buf, sizeof(buf) - 1, "%s/id32db", dbctx->config->location);
logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
curdb->upgrade(curdb, buf, 0);
curdb->close(curdb, 0);
ret = db_create(&curdb, NULL, 0);
if (ret == 0) {
- snprintf(buf, sizeof(buf) - 1, "%s/id64db", config.db_dir);
+ snprintf(buf, sizeof(buf) - 1, "%s/id64db", dbctx->config->location);
logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
curdb->upgrade(curdb, buf, 0);
curdb->close(curdb, 0);
ret = db_create(&curdb, NULL, 0);
if (ret == 0) {
- snprintf(buf, sizeof(buf) - 1, "%s/skshashdb", config.db_dir);
+ snprintf(buf, sizeof(buf) - 1, "%s/skshashdb", dbctx->config->location);
logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
curdb->upgrade(curdb, buf, 0);
curdb->close(curdb, 0);
ret = db_create(&curdb, NULL, 0);
if (ret == 0) {
- snprintf(buf, sizeof(buf) - 1, "%s/subkeydb", config.db_dir);
+ snprintf(buf, sizeof(buf) - 1, "%s/subkeydb", dbctx->config->location);
logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
curdb->upgrade(curdb, buf, 0);
curdb->close(curdb, 0);
db_strerror(ret));
}
- snprintf(buf, sizeof(buf) - 1, "%s/%s", config.db_dir,
+ snprintf(buf, sizeof(buf) - 1, "%s/%s", dbctx->config->location,
DB4_UPGRADE_FILE);
unlink(buf);
* this file are called in order to allow the DB to be initialized ready
* for access.
*/
-struct onak_dbctx *keydb_db4_init(bool readonly)
+struct onak_dbctx *keydb_db4_init(struct onak_db_config *dbcfg, bool readonly)
{
char buf[1024];
FILE *numdb = NULL;
if (dbctx == NULL) {
return NULL;
}
+ dbctx->config = dbcfg;
dbctx->priv = privctx = calloc(1, sizeof(*privctx));
if (privctx == NULL) {
free(dbctx);
/* Default to 16 key data DBs */
privctx->numdbs = 16;
- snprintf(buf, sizeof(buf) - 1, "%s/%s", config.db_dir,
+ snprintf(buf, sizeof(buf) - 1, "%s/%s", dbcfg->location,
DB4_UPGRADE_FILE);
ret = stat(buf, &statbuf);
while ((ret == 0) || (errno != ENOENT)) {
}
ret = 0;
- snprintf(buf, sizeof(buf) - 1, "%s/num_keydb", config.db_dir);
+ snprintf(buf, sizeof(buf) - 1, "%s/num_keydb", dbcfg->location);
numdb = fopen(buf, "r");
if (numdb != NULL) {
if (fgets(buf, sizeof(buf), numdb) != NULL) {
* Up the number of locks we're allowed at once. We base this on
* the maximum number of keys we're going to return.
*/
- maxlocks = config.maxkeys * 16;
- if (maxlocks < 1000) {
- maxlocks = 1000;
+ if (ret == 0) {
+ maxlocks = config.maxkeys * 16;
+ if (maxlocks < 1000) {
+ maxlocks = 1000;
+ }
+ privctx->dbenv->set_lk_max_locks(privctx->dbenv, maxlocks);
+ privctx->dbenv->set_lk_max_objects(privctx->dbenv, maxlocks);
}
- privctx->dbenv->set_lk_max_locks(privctx->dbenv, maxlocks);
- privctx->dbenv->set_lk_max_objects(privctx->dbenv, maxlocks);
/*
* Enable deadlock detection so that we don't block indefinitely on
}
if (ret == 0) {
- ret = privctx->dbenv->open(privctx->dbenv, config.db_dir,
+ ret = privctx->dbenv->open(privctx->dbenv, dbcfg->location,
DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_LOCK |
DB_INIT_TXN |
DB_CREATE,
if (ret == DB_VERSION_MISMATCH) {
privctx->dbenv->close(privctx->dbenv, 0);
privctx->dbenv = NULL;
- ret = db4_upgradedb(privctx);
+ ret = db4_upgradedb(dbctx);
if (ret == 0) {
ret = db_env_create(&privctx->dbenv, 0);
}
privctx->dbenv->set_lk_detect(privctx->dbenv,
DB_LOCK_DEFAULT);
ret = privctx->dbenv->open(privctx->dbenv,
- config.db_dir,
+ dbcfg->location,
DB_INIT_LOG | DB_INIT_MPOOL |
DB_INIT_LOCK | DB_INIT_TXN |
DB_CREATE | DB_RECOVER,
if (ret != 0) {
logthing(LOGTHING_CRITICAL,
"Error opening db environment: %s (%s)",
- config.db_dir,
+ dbcfg->location,
db_strerror(ret));
if (privctx->dbenv != NULL) {
privctx->dbenv->close(privctx->dbenv, 0);