Commit 01c84b05 authored by Rich Prohaska's avatar Rich Prohaska Committed by Yoni Fogel

fix a couple of set_data_dir bugs [t:1985]

git-svn-id: file:///svn/toku/tokudb@14375 c7de825b-a66e-492c-adef-691d508d4ae1
parent 739e2dcc
...@@ -15,34 +15,39 @@ static void run_test (void) { ...@@ -15,34 +15,39 @@ static void run_test (void) {
toku_os_mkdir(ENVDIR, S_IRWXU+S_IRWXG+S_IRWXO); toku_os_mkdir(ENVDIR, S_IRWXU+S_IRWXG+S_IRWXO);
DB_ENV *env; DB_ENV *env;
r = db_env_create(&env, 0); CKERR(r); r = db_env_create(&env, 0); CKERR(r);
r = env->open(env, ENVDIR, envflags, S_IRWXU+S_IRWXG+S_IRWXO); CKERR(r); r = env->open(env, ENVDIR, envflags, S_IRWXU+S_IRWXG+S_IRWXO); CKERR(r);
DB *dba; DB *db;
r = db_create(&dba, env, 0); CKERR(r); r = db_create(&db, env, 0); CKERR(r);
r = dba->open(dba, NULL, "a.db", NULL, DB_BTREE, DB_AUTO_COMMIT|DB_CREATE, 0666); CKERR(r); r = db->open(db, NULL, "a.db", NULL, DB_BTREE, DB_AUTO_COMMIT|DB_CREATE, 0666); CKERR(r);
r = dba->close(dba, 0); CKERR(r); r = db->close(db, 0); CKERR(r);
DB *dbb; r = db_create(&db, env, 0); CKERR(r);
r = db_create(&dbb, env, 0); CKERR(r); r = db->open(db, NULL, "bdir/b.db", NULL, DB_BTREE, DB_AUTO_COMMIT|DB_CREATE, 0666); assert(r != 0);
r = dbb->open(dbb, NULL, "bdir/b.db", NULL, DB_BTREE, DB_AUTO_COMMIT|DB_CREATE, 0666); assert(r != 0);
r = toku_os_mkdir(ENVDIR "/bdir", 0777); assert(r == 0); r = toku_os_mkdir(ENVDIR "/bdir", 0777); assert(r == 0);
r = dbb->open(dbb, NULL, "bdir/b.db", NULL, DB_BTREE, DB_AUTO_COMMIT|DB_CREATE, 0666); CKERR(r); r = db->open(db, NULL, "bdir/b.db", NULL, DB_BTREE, DB_AUTO_COMMIT|DB_CREATE, 0666); CKERR(r);
r = dbb->close(dbb, 0); CKERR(r); r = db->close(db, 0); CKERR(r);
r = env->close(env, 0); CKERR(r); r = env->close(env, 0); CKERR(r);
r = toku_os_mkdir(ENVDIR "/cdir", 0777); assert(r == 0); r = toku_os_mkdir(ENVDIR "/cdir", 0777); assert(r == 0);
r = db_env_create(&env, 0); CKERR(r); r = db_env_create(&env, 0); CKERR(r);
r = env->set_data_dir(env, "cdir"); CKERR(r); r = env->set_data_dir(env, "cdir"); CKERR(r);
r = env->open(env, ENVDIR, envflags, S_IRWXU+S_IRWXG+S_IRWXO); CKERR(r); r = env->open(env, ENVDIR, envflags, S_IRWXU+S_IRWXG+S_IRWXO); CKERR(r);
DB *dbc; r = db_create(&db, env, 0); CKERR(r);
r = db_create(&dbc, env, 0); CKERR(r); r = db->open(db, NULL, "c.db", NULL, DB_BTREE, DB_AUTO_COMMIT|DB_CREATE, 0666); CKERR(r);
r = dbc->open(dbc, NULL, "c.db", NULL, DB_BTREE, DB_AUTO_COMMIT|DB_CREATE, 0666); CKERR(r); r = db->close(db, 0); CKERR(r);
r = dbc->close(dbc, 0); CKERR(r);
r = env->close(env, 0); CKERR(r); #if 0
// test fname with absolute path
r = db_create(&db, env, 0); CKERR(r);
r = db->open(db, NULL, "/tmp/d.db", NULL, DB_BTREE, DB_AUTO_COMMIT|DB_CREATE, 0666); CKERR(r);
r = db->close(db, 0); CKERR(r);
#endif
r = env->close(env, 0); CKERR(r);
} }
const char *cmd; const char *cmd;
......
...@@ -292,13 +292,13 @@ static int toku_c_count(DBC *cursor, db_recno_t *count, u_int32_t flags); ...@@ -292,13 +292,13 @@ static int toku_c_count(DBC *cursor, db_recno_t *count, u_int32_t flags);
static int toku_c_close(DBC * c); static int toku_c_close(DBC * c);
/* misc */ /* misc */
static char *construct_full_name(const char *dir, const char *fname); static char *construct_full_name(int count, ...);
static int delete_rolltmp_files(DB_ENV *env) { static int delete_rolltmp_files(DB_ENV *env) {
const char *datadir=env->i->dir; const char *datadir=env->i->dir;
char *logdir; char *logdir;
if (env->i->lg_dir) { if (env->i->lg_dir) {
logdir = construct_full_name(env->i->dir, env->i->lg_dir); logdir = construct_full_name(2, env->i->dir, env->i->lg_dir);
} else { } else {
logdir = toku_strdup(env->i->dir); logdir = toku_strdup(env->i->dir);
} }
...@@ -311,7 +311,7 @@ static int do_recovery (DB_ENV *env) { ...@@ -311,7 +311,7 @@ static int do_recovery (DB_ENV *env) {
const char *datadir=env->i->dir; const char *datadir=env->i->dir;
char *logdir; char *logdir;
if (env->i->lg_dir) { if (env->i->lg_dir) {
logdir = construct_full_name(env->i->dir, env->i->lg_dir); logdir = construct_full_name(2, env->i->dir, env->i->lg_dir);
} else { } else {
logdir = toku_strdup(env->i->dir); logdir = toku_strdup(env->i->dir);
} }
...@@ -323,7 +323,7 @@ static int do_recovery (DB_ENV *env) { ...@@ -323,7 +323,7 @@ static int do_recovery (DB_ENV *env) {
static int needs_recovery (DB_ENV *env) { static int needs_recovery (DB_ENV *env) {
char *logdir; char *logdir;
if (env->i->lg_dir) { if (env->i->lg_dir) {
logdir = construct_full_name(env->i->dir, env->i->lg_dir); logdir = construct_full_name(2, env->i->dir, env->i->lg_dir);
} else { } else {
logdir = toku_strdup(env->i->dir); logdir = toku_strdup(env->i->dir);
} }
...@@ -417,7 +417,7 @@ static int toku_env_open(DB_ENV * env, const char *home, u_int32_t flags, int mo ...@@ -417,7 +417,7 @@ static int toku_env_open(DB_ENV * env, const char *home, u_int32_t flags, int mo
if (flags & (DB_INIT_TXN | DB_INIT_LOG)) { if (flags & (DB_INIT_TXN | DB_INIT_LOG)) {
char* full_dir = NULL; char* full_dir = NULL;
if (env->i->lg_dir) full_dir = construct_full_name(env->i->dir, env->i->lg_dir); if (env->i->lg_dir) full_dir = construct_full_name(2, env->i->dir, env->i->lg_dir);
assert(env->i->logger); assert(env->i->logger);
toku_logger_write_log_files(env->i->logger, (flags & DB_INIT_LOG) != 0); toku_logger_write_log_files(env->i->logger, (flags & DB_INIT_LOG) != 0);
r = toku_logger_open(full_dir ? full_dir : env->i->dir, env->i->logger); r = toku_logger_open(full_dir ? full_dir : env->i->dir, env->i->logger);
...@@ -3006,47 +3006,28 @@ static int toku_db_key_range(DB * db, DB_TXN * txn, DBT * dbt, DB_KEY_RANGE * kr ...@@ -3006,47 +3006,28 @@ static int toku_db_key_range(DB * db, DB_TXN * txn, DBT * dbt, DB_KEY_RANGE * kr
} }
#endif #endif
static int construct_full_name_in_buf(const char *dir, const char *fname, char* full, int length) { static char *construct_full_name(int count, ...) {
int l; va_list ap;
char *name = NULL;
if (!full) return EINVAL; size_t n = 0;
if (toku_os_is_absolute_name(fname)) { int i;
l = 0; va_start(ap, count);
full[0] = '\0'; for (i=0; i<count; i++) {
} char *arg = va_arg(ap, char *);
else { if (arg) {
l = snprintf(full, length, "%s", dir); n += 1 + strlen(arg) + 1;
if (l >= length) return ENAMETOOLONG; char *newname = toku_malloc(n);
if (l == 0 || full[l - 1] != '/') { assert(newname);
if (l + 1 == length) return ENAMETOOLONG; if (name && !toku_os_is_absolute_name(arg))
snprintf(newname, n, "%s/%s", name, arg);
/* Didn't put a slash down. */ else
if (fname[0] != '/') { snprintf(newname, n, "%s", arg);
full[l++] = '/'; toku_free(name);
full[l] = 0; name = newname;
}
} }
} }
l += snprintf(full + l, length - l, "%s", fname);
if (l >= length) return ENAMETOOLONG;
return 0;
}
static char *construct_full_name(const char *dir, const char *fname) { return name;
if (toku_os_is_absolute_name(fname))
dir = "";
{
int dirlen = strlen(dir);
int fnamelen = strlen(fname);
int len = dirlen + fnamelen + 2; // One for the / between (which may not be there). One for the trailing null.
char *result = toku_malloc(len);
// printf("%s:%d len(%d)=%d+%d+2\n", __FILE__, __LINE__, len, dirlen, fnamelen);
if (construct_full_name_in_buf(dir, fname, result, len) != 0) {
toku_free(result);
result = NULL;
}
return result;
}
} }
static int find_db_file(DB_ENV* dbenv, const char *fname, char** full_name_out) { static int find_db_file(DB_ENV* dbenv, const char *fname, char** full_name_out) {
...@@ -3059,7 +3040,7 @@ static int find_db_file(DB_ENV* dbenv, const char *fname, char** full_name_out) ...@@ -3059,7 +3040,7 @@ static int find_db_file(DB_ENV* dbenv, const char *fname, char** full_name_out)
if (dbenv->i->data_dirs!=NULL) { if (dbenv->i->data_dirs!=NULL) {
assert(dbenv->i->n_data_dirs > 0); assert(dbenv->i->n_data_dirs > 0);
for (i = 0; i < dbenv->i->n_data_dirs; i++) { for (i = 0; i < dbenv->i->n_data_dirs; i++) {
full_name = construct_full_name(dbenv->i->data_dirs[0], fname); full_name = construct_full_name(3, dbenv->i->dir, dbenv->i->data_dirs[i], fname);
if (!full_name) return ENOMEM; if (!full_name) return ENOMEM;
r = toku_stat(full_name, &statbuf); r = toku_stat(full_name, &statbuf);
if (r == 0) goto finish; if (r == 0) goto finish;
...@@ -3070,11 +3051,11 @@ static int find_db_file(DB_ENV* dbenv, const char *fname, char** full_name_out) ...@@ -3070,11 +3051,11 @@ static int find_db_file(DB_ENV* dbenv, const char *fname, char** full_name_out)
} }
} }
//Did not find it at all. Return the first data dir. //Did not find it at all. Return the first data dir.
full_name = construct_full_name(dbenv->i->data_dirs[0], fname); full_name = construct_full_name(3, dbenv->i->dir, dbenv->i->data_dirs[0], fname);
goto finish; goto finish;
} }
//Default without data_dirs is the environment directory. //Default without data_dirs is the environment directory.
full_name = construct_full_name(dbenv->i->dir, fname); full_name = construct_full_name(2, dbenv->i->dir, fname);
goto finish; goto finish;
finish: finish:
...@@ -3249,7 +3230,6 @@ static int toku_db_open(DB * db, DB_TXN * txn, const char *fname, const char *db ...@@ -3249,7 +3230,6 @@ static int toku_db_open(DB * db, DB_TXN * txn, const char *fname, const char *db
db->i->open_flags = flags; db->i->open_flags = flags;
db->i->open_mode = mode; db->i->open_mode = mode;
r = toku_brt_open(db->i->brt, db->i->full_fname, fname, r = toku_brt_open(db->i->brt, db->i->full_fname, fname,
is_db_create, is_db_excl, is_db_create, is_db_excl,
db->dbenv->i->cachetable, db->dbenv->i->cachetable,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment