#include "db_config.h"
#include "db_int.h"
#include "dbinc/db_page.h"
#include "dbinc/db_am.h"
#include "dbinc/fop.h"
#include "dbinc/lock.h"
#include "dbinc/log.h"
#include "dbinc/mp.h"
#include "dbinc/txn.h"
static int __db_rename __P((DB *, DB_THREAD_INFO *,
DB_TXN *, const char *, const char *, const char *));
static int __db_subdb_rename __P((DB *, DB_THREAD_INFO *,
DB_TXN *, const char *, const char *, const char *));
int
__env_dbrename_pp(dbenv, txn, name, subdb, newname, flags)
DB_ENV *dbenv;
DB_TXN *txn;
const char *name, *subdb, *newname;
u_int32_t flags;
{
DB *dbp;
DB_THREAD_INFO *ip;
ENV *env;
int handle_check, ret, t_ret, txn_local;
env = dbenv->env;
dbp = NULL;
txn_local = 0;
ENV_ILLEGAL_BEFORE_OPEN(env, "DB_ENV->dbrename");
if ((ret = __db_fchk(env, "DB->rename", flags, DB_AUTO_COMMIT)) != 0)
return (ret);
ENV_ENTER(env, ip);
handle_check = IS_ENV_REPLICATED(env);
if (handle_check && (ret = __env_rep_enter(env, 1)) != 0) {
handle_check = 0;
goto err;
}
if (IS_ENV_AUTO_COMMIT(env, txn, flags)) {
if ((ret = __db_txn_auto_init(env, ip, &txn)) != 0)
goto err;
txn_local = 1;
} else
if (txn != NULL && !TXN_ON(env) &&
(!CDB_LOCKING(env) || !F_ISSET(txn, TXN_CDSGROUP))) {
ret = __db_not_txn_env(env);
goto err;
}
LF_CLR(DB_AUTO_COMMIT);
if ((ret = __db_create_internal(&dbp, env, 0)) != 0)
goto err;
ret = __db_rename_int(dbp, ip, txn, name, subdb, newname);
if (txn_local) {
LOCK_INIT(dbp->handle_lock);
dbp->locker = NULL;
} else if (txn != NULL) {
dbp->locker = NULL;
}
err: if (txn_local && (t_ret =
__db_txn_auto_resolve(env, txn, 0, ret)) != 0 && ret == 0)
ret = t_ret;
if (txn_local || txn == NULL) {
if (dbp != NULL &&
(t_ret = __db_close(dbp, NULL, DB_NOSYNC)) != 0 && ret == 0)
ret = t_ret;
} else {
if (dbp != NULL && (t_ret =
__txn_closeevent(env, txn, dbp)) != 0 && ret == 0)
ret = t_ret;
}
if (handle_check && (t_ret = __env_db_rep_exit(env)) != 0 && ret == 0)
ret = t_ret;
ENV_LEAVE(env, ip);
return (ret);
}
int
__db_rename_pp(dbp, name, subdb, newname, flags)
DB *dbp;
const char *name, *subdb, *newname;
u_int32_t flags;
{
DB_THREAD_INFO *ip;
ENV *env;
int handle_check, ret, t_ret;
env = dbp->env;
handle_check = 0;
if (F_ISSET(dbp, DB_AM_OPEN_CALLED))
return (__db_mi_open(env, "DB->rename", 1));
if ((ret = __db_fchk(env, "DB->rename", flags, 0)) != 0)
return (ret);
if ((ret = __db_check_txn(dbp, NULL, DB_LOCK_INVALIDID, 0)) != 0)
return (ret);
ENV_ENTER(env, ip);
handle_check = IS_ENV_REPLICATED(env);
if (handle_check && (ret = __db_rep_enter(dbp, 1, 1, 0)) != 0) {
handle_check = 0;
goto err;
}
ret = __db_rename(dbp, ip, NULL, name, subdb, newname);
if (handle_check && (t_ret = __env_db_rep_exit(env)) != 0 && ret == 0)
ret = t_ret;
err: ENV_LEAVE(env, ip);
return (ret);
}
static int
__db_rename(dbp, ip, txn, name, subdb, newname)
DB *dbp;
DB_THREAD_INFO *ip;
DB_TXN *txn;
const char *name, *subdb, *newname;
{
int ret, t_ret;
ret = __db_rename_int(dbp, ip, txn, name, subdb, newname);
if (txn == NULL) {
if ((t_ret = __db_close(dbp, txn, DB_NOSYNC)) != 0 && ret == 0)
ret = t_ret;
} else {
if ((t_ret =
__txn_closeevent(dbp->env, txn, dbp)) != 0 && ret == 0)
ret = t_ret;
}
return (ret);
}
int
__db_rename_int(dbp, ip, txn, name, subdb, newname)
DB *dbp;
DB_THREAD_INFO *ip;
DB_TXN *txn;
const char *name, *subdb, *newname;
{
ENV *env;
int ret;
char *old, *real_name;
env = dbp->env;
real_name = NULL;
DB_TEST_RECOVERY(dbp, DB_TEST_PREDESTROY, ret, name);
if (name == NULL && subdb == NULL) {
__db_errx(env, "Rename on temporary files invalid");
ret = EINVAL;
goto err;
}
if (name == NULL)
MAKE_INMEM(dbp);
else if (subdb != NULL) {
ret = __db_subdb_rename(dbp, ip, txn, name, subdb, newname);
goto err;
}
if (F_ISSET(dbp, DB_AM_INMEM)) {
old = (char *)subdb;
real_name = (char *)subdb;
} else {
if ((ret = __db_appname(env,
DB_APP_DATA, name, 0, NULL, &real_name)) != 0)
goto err;
old = (char *)name;
}
if ((ret = __fop_remove_setup(dbp, txn, real_name, 0)) != 0)
goto err;
if (dbp->db_am_rename != NULL &&
(ret = dbp->db_am_rename(dbp, txn, name, subdb, newname)) != 0)
goto err;
if (IS_REAL_TXN(txn)) {
if ((ret = __fop_dummy(dbp, txn, old, newname, 0)) != 0)
goto err;
} else {
if ((ret = __fop_dbrename(dbp, old, newname)) != 0)
goto err;
}
DB_ASSERT(env, dbp->log_filename == NULL ||
dbp->log_filename->id == DB_LOGFILEID_INVALID);
DB_TEST_RECOVERY(dbp, DB_TEST_POSTDESTROY, ret, newname);
DB_TEST_RECOVERY_LABEL
err: if (!F_ISSET(dbp, DB_AM_INMEM) && real_name != NULL)
__os_free(env, real_name);
return (ret);
}
static int
__db_subdb_rename(dbp, ip, txn, name, subdb, newname)
DB *dbp;
DB_THREAD_INFO *ip;
DB_TXN *txn;
const char *name, *subdb, *newname;
{
DB *mdbp;
ENV *env;
PAGE *meta;
int ret, t_ret;
mdbp = NULL;
meta = NULL;
env = dbp->env;
F_SET(dbp, DB_AM_SUBDB);
if ((ret = __db_master_open(dbp, ip, txn, name, 0, 0, &mdbp)) != 0)
goto err;
if ((ret = __db_master_update(mdbp, dbp, ip, txn, subdb, dbp->type,
MU_OPEN, NULL, 0)) != 0)
goto err;
if ((ret = __memp_fget(mdbp->mpf, &dbp->meta_pgno,
ip, txn, 0, &meta)) != 0)
goto err;
memcpy(dbp->fileid, ((DBMETA *)meta)->uid, DB_FILE_ID_LEN);
if ((ret = __fop_lock_handle(env,
dbp, mdbp->locker, DB_LOCK_WRITE, NULL, NOWAIT_FLAG(txn))) != 0)
goto err;
ret = __memp_fput(mdbp->mpf, ip, meta, dbp->priority);
meta = NULL;
if (ret != 0)
goto err;
if ((ret = __db_master_update(mdbp, dbp, ip, txn,
subdb, dbp->type, MU_RENAME, newname, 0)) != 0)
goto err;
DB_TEST_RECOVERY(dbp, DB_TEST_POSTDESTROY, ret, name);
DB_TEST_RECOVERY_LABEL
err:
if (meta != NULL && (t_ret =
__memp_fput(mdbp->mpf, ip, meta, dbp->priority)) != 0 && ret == 0)
ret = t_ret;
if (txn == NULL) {
if (mdbp != NULL &&
(t_ret = __db_close(mdbp, txn, DB_NOSYNC)) != 0 && ret == 0)
ret = t_ret;
} else {
if (mdbp != NULL && (t_ret =
__txn_closeevent(env, txn, mdbp)) != 0 && ret == 0)
ret = t_ret;
}
return (ret);
}