Exemple #1
0
int TestBackupMultiDataDir(CuTest *ct) {
	DB_ENV *dbenv;
	DB *dbp;
	DBTYPE dtype;
	struct handlers *info;
	int has_callback;
	u_int32_t flag;

	dtype = DB_BTREE;
	info = ct->context;
	has_callback = 0;
	flag = DB_BACKUP_CLEAN | DB_CREATE | DB_BACKUP_FILES;

	/* Step 1: set up directories and make DB_CONFIG. */
	CuAssert(ct, "setup_dir", setup_dir(2, data_dirs) == 0);
	CuAssert(ct, "make_dbconfig",
	    make_dbconfig("set_data_dir DATA1") == 0);

	/* Step 2: open db handle. */
	CuAssert(ct,"open_dbp", open_dbp(&dbenv, &dbp,
	    dtype, 2, data_dirs, data_dirs[0], NULL, 0, NULL) == 0);
	info->dbenvp = dbenv;
	info->dbp = dbp;

	/* Step 3: store records into db. */
	CuAssert(ct, "store_records", store_records(dbp, 1) == 0);
	CuAssert(ct, "DB->sync", dbp->sync(dbp, 0) == 0);

	/* Step 4: backup the whole environment without callbacks. */
	CuAssert(ct, "backup_env",
	    backup_env(ct, dbenv, flag, has_callback) == 0);	

	/*
	 * Step 5: check backup result.
	 * 5a: verify db files are in BACKUP/DATA1.
	 */
	CuAssert(ct, "verify_db_log",
	    verify_db_log(dtype, 0, 0, data_dirs[0], data_dirs[0]) == 0);

	/* 5b: verify that data_dirs are in backupdir. */
	CuAssert(ct, "__os_exist", __os_exists(NULL, "BACKUP/DATA1", 0) == 0);
	CuAssert(ct, "__os_exist", __os_exists(NULL, "BACKUP/DATA2", 0) == 0);

	/* 5c: verify that log files are in BACKUP_DIR. */
	CuAssert(ct, "verify_db_log",
	    verify_db_log(dtype, 0, 1, NULL, NULL) == 0);

	/* 5d: verify that DB_CONFIG is in BACKUP_DIR. */
	CuAssert(ct, "verify_dbconfig", verify_dbconfig(1) == 0);

	return (0);
}
int TestMutexAlignment(CuTest *ct) {
	int procs, threads, alignment, lockloop;
	int max_procs, max_threads;
	char *bin;
	char cmdstr[1000], cmd[1000];

	/* Step 1: Check required binary file existence, set args */
#ifdef DB_WIN32
#ifdef _WIN64
#ifdef DEBUG
	bin = "x64\\Debug\\test_mutex.exe";
#else
	bin = "x64\\Release\\test_mutex.exe";
#endif
#else
#ifdef DEBUG
	bin = "Win32\\Debug\\test_mutex.exe";
#else
	bin = "Win32\\Release\\test_mutex.exe";
#endif
#endif
	sprintf(cmdstr, "%s -p %%d -t %%d -a %%d -n %%d >/nul 2>&1", bin);
	lockloop = 100;
	max_procs = 2;
	max_threads = 2;
#else
	bin = "./test_mutex";
	sprintf(cmdstr, "%s -p %%d -t %%d -a %%d -n %%d >/dev/null 2>&1", bin);
	lockloop = 2000;
	max_procs = 4;
	max_threads = 4;
#endif

	if (__os_exists(NULL, bin, NULL) != 0) {
		printf("Error! Can not find %s. It need to be built in order to\
		    run this test.\n", bin);
		CuAssert(ct, bin, 0);
		return (EXIT_FAILURE);
	}
Exemple #3
0
/* Close or free all handles and commit or rollback the transaction. */
static int backupCleanup(sqlite3_backup *p)
{
	int rc, rc2, ret;
	void *app;
	DB *db;

	rc = rc2 = SQLITE_OK;

	if (!p || p->rc == SQLITE_OK)
		return rc;

	rc2 = sqlite3BtreeCloseCursor(&p->destCur);
	if (rc2 != SQLITE_OK)
		rc = rc2;
	if (p->srcCur) {
		db = p->srcCur->dbp;
		app = db->app_private;
		if ((ret = p->srcCur->close(p->srcCur)) == 0)
			ret = db->close(db, DB_NOSYNC);
		rc2 = dberr2sqlite(ret);
		/*
		 * The KeyInfo was allocated in btreeSetupIndex,
		 * so have to deallocate it here.
		 */
		if (app)
			sqlite3DbFree(p->pSrcDb, app);
	}
	if (rc2 != SQLITE_OK)
		rc = rc2;
	p->srcCur = 0;

	/*
	 * May retry on a locked or busy error, so keep
	 * these values.
	 */
	if (p->rc != SQLITE_LOCKED && p->rc != SQLITE_BUSY) {
		if (p->srcName)
			sqlite3_free(p->srcName);
		if (p->destName != 0)
			sqlite3_free(p->destName);
		p->srcName = p->destName = NULL;
	}

	if (p->tables != 0)
			sqlite3_free(p->tables);
	p->tables = NULL;

	if (p->pSrc->nBackup)
		p->pSrc->nBackup--;
	if (p->pDest != NULL && p->pDest->nBackup)
		p->pDest->nBackup--;
	if (p->srcTxn) {
		if (p->rc == SQLITE_DONE)
			ret = p->srcTxn->commit(p->srcTxn, 0);
		else
			ret = p->srcTxn->abort(p->srcTxn);
		rc2 = dberr2sqlite(ret);
	}
	p->srcTxn = 0;
	if (rc2 != SQLITE_OK && sqlite3BtreeIsInTrans(p->pDest)) {
		rc = rc2;
		if (p->rc == SQLITE_DONE)
			rc2 = sqlite3BtreeCommit(p->pDest);
		else
			rc2 = sqlite3BtreeRollback(p->pDest);
		if (rc2 != SQLITE_OK)
			rc = rc2;
	}

	if (p->pDest && p->openDest) {
		char path[512];

		/*
		 * If successfully done then delete the old backup, if
		 * an error then delete the current database and restore
		 * the old backup.
		 */
		sqlite3_snprintf(sizeof(path), path,
		    "%s%s", p->fullName, BACKUP_SUFFIX);
		if (p->rc == SQLITE_DONE) {
			rc2 = btreeDeleteEnvironment(p->pDest, path, 0);
		} else {
			rc2 = btreeDeleteEnvironment(p->pDest, p->fullName, 0);
			if (!__os_exists(NULL, path, 0))
				__os_rename(NULL, path, p->fullName, 0);
		}
		if (rc == SQLITE_OK)
			rc = rc2;
		if (rc == SQLITE_OK) {
			p->pDest = NULL;
			p->pDestDb->aDb[p->iDb].pBt = NULL;
			p->openDest = 0;
			rc = sqlite3BtreeOpen(p->fullName, p->pDestDb,
			    &p->pDest,
			    SQLITE_DEFAULT_CACHE_SIZE | SQLITE_OPEN_MAIN_DB,
			    p->pDestDb->openFlags);
			p->pDestDb->aDb[p->iDb].pBt = p->pDest;
			if (rc == SQLITE_OK) {
				p->pDestDb->aDb[p->iDb].pSchema =
				    sqlite3SchemaGet(p->pDestDb, p->pDest);
				if (!p->pDestDb->aDb[p->iDb].pSchema)
					p->rc = SQLITE_NOMEM;
			} else
				p->pDestDb->aDb[p->iDb].pSchema = NULL;
			if (rc == SQLITE_OK)
				p->pDest->pBt->db_oflags |= DB_CREATE;
			/*
			 * Have to delete the schema here on error to avoid
			 * assert failure.
			 */
			if (p->pDest == NULL &&
			    p->pDestDb->aDb[p->iDb].pSchema != NULL) {
				sqlite3SchemaClear(
				    p->pDestDb->aDb[p->iDb].pSchema);
				p->pDestDb->aDb[p->iDb].pSchema = NULL;
			}
#ifdef SQLITE_HAS_CODEC
			if (rc == SQLITE_OK) {
				if (p->iDb == 0)
					rc = sqlite3_key(p->pDestDb,
					    p->pSrc->pBt->encrypt_pwd,
					    p->pSrc->pBt->encrypt_pwd_len);
				else
					rc = sqlite3CodecAttach(p->pDestDb,
					    p->iDb, p->pSrc->pBt->encrypt_pwd,
					    p->pSrc->pBt->encrypt_pwd_len);
			}
#endif
		}
	}
	if (p->rc != SQLITE_LOCKED && p->rc != SQLITE_BUSY) {
		if (p->fullName != 0)
			sqlite3_free(p->fullName);
		p->fullName = NULL;
	}
	p->lastUpdate = p->pSrc->updateDuringBackup;
	return rc;
}
Exemple #4
0
/*
 * Deletes all data and environment files of the given Btree.  Requires
 * that there are no other handles using the BtShared when this function
 * is called.
 */
int btreeDeleteEnvironment(Btree *p, const char *home, int rename)
{
	BtShared *pBt;
	int rc, ret, iDb, storage;
	sqlite3 *db;
	DB_ENV *tmp_env;
	char path[512];
#ifdef BDBSQL_FILE_PER_TABLE
	int numFiles;
	char **files;
#endif

	rc = SQLITE_OK;
	ret = 0;
	tmp_env = NULL;

	if (p != NULL) {
		if ((rc = btreeUpdateBtShared(p, 1)) != SQLITE_OK)
			goto err;
		pBt = p->pBt;
		if (pBt->nRef > 1)
			return SQLITE_BUSY;

		storage = pBt->dbStorage;
		db = p->db;
		for (iDb = 0; iDb < db->nDb; iDb++) {
			if (db->aDb[iDb].pBt == p)
				break;
		}
		if ((rc = sqlite3BtreeClose(p)) != SQLITE_OK)
			goto err;
		pBt = NULL;
		p = NULL;
		db->aDb[iDb].pBt = NULL;
	}
	if (home == NULL)
		goto done;

	ret = btreeCleanupEnv(path);
	/* EFAULT can be returned on Windows when the file does not exist.*/
	if (ret == ENOENT || ret == EFAULT)
		ret = 0;
	else if (ret != 0)
		goto err;

	if ((ret = db_env_create(&tmp_env, 0)) != 0)
		goto err;

	if (rename) {
		if (!(ret = __os_exists(tmp_env->env, home, 0))) {
			sqlite3_snprintf(sizeof(path), path,
			    "%s%s", home, BACKUP_SUFFIX);
			ret = __os_rename(tmp_env->env, home, path, 0);
		}
	} else {
#ifdef BDBSQL_FILE_PER_TABLE
		ret = __os_dirlist(tmp_env->env, home, 0, &files, &numFiles);
		if (ret == 0) {
			int i, ret2;
			for (i = 0; i < numFiles; i++) {
				sqlite3_snprintf(sizeof(path), path, "%s/%s",
				    home, files[i]);
				if ((ret2 = __os_unlink (tmp_env->env, path, 0))
				    != 0)
					ret = ret2;
			}
		}
		__os_dirfree(tmp_env->env, files, numFiles);
		if (ret == 0)
			ret = __os_unlink(tmp_env->env, home, 0);

#else
		if (!(ret = __os_exists(tmp_env->env, home, 0)))
			ret = __os_unlink(tmp_env->env, home, 0);
#endif
	}
	/* EFAULT can be returned on Windows when the file does not exist.*/
	if (ret == ENOENT || ret == EFAULT)
		ret = 0;
	else if (ret != 0)
		goto err;

err:
done:	if (tmp_env != NULL)
		tmp_env->close(tmp_env, 0);

	return MAP_ERR(rc, ret);
}
Exemple #5
0
int TestBackupPartitionDB(CuTest *ct) {
	DB_ENV *dbenv;
	DB *dbp;
	DBT key1, key2, keys[2];
	DBTYPE dtype;
	struct handlers *info;
	int has_callback;
	u_int32_t flag, value1, value2;

	dtype = DB_BTREE;
	info = ct->context;
	has_callback = 0;
	flag = DB_BACKUP_CLEAN | DB_CREATE | DB_BACKUP_SINGLE_DIR;

	/* Step 1: set up directories and make DB_CONFIG. */
	CuAssert(ct, "setup_dir", setup_dir(1, data_dirs) == 0);
	CuAssert(ct, "make_dbconfig",
	    make_dbconfig("set_data_dir DATA1") == 0);

	/* Make the partition keys. */
	memset(&key1, 0, sizeof(DBT));
	memset(&key2, 0, sizeof(DBT));
	value1 = 8;
	key1.data = &value1;
	key1.size = sizeof(value1);
	value2 = 16;
	key2.data = &value2;
	key2.size = sizeof(value2);
	keys[0] = key1;
	keys[1] = key2;

	/* Step 2: open db handle. */
	CuAssert(ct,"open_dbp", open_dbp(&dbenv,
	    &dbp, dtype, 1, data_dirs, data_dirs[0], NULL, 3, keys) == 0);
	info->dbenvp = dbenv;
	info->dbp = dbp;

	/* Step 3: store records into db. */
	CuAssert(ct, "store_records", store_records(dbp, 1) == 0);
	CuAssert(ct, "DB->sync", dbp->sync(dbp, 0) == 0);

	/* Step 4: backup the whole environment into a single directory. */
	CuAssert(ct, "backup_env",
	    backup_env(ct, dbenv, flag, has_callback) == 0);

	/*
	 * Step 5: check backup result.
	 * 5a: verify db files are in BACKUP/DATA1.
	 */
	CuAssert(ct, "verify_db_log",
	    verify_db_log(dtype, 1, 0, data_dirs[0], NULL) == 0);

	/* 5b: verify that creation directory is not in BACKUPD_DIR. */
	CuAssert(ct, "__os_exist", __os_exists(NULL, "BACKUP/DATA", 0) != 0);

	/* 5c: verify log files are in BACKUP_DIR. */
	CuAssert(ct, "verify_db_log",
	    verify_db_log(dtype, 0, 1, NULL, NULL) == 0);

	/* 5d: verify that DB_CONFIG is not in BACKUP_DIR. */
	CuAssert(ct, "verify_dbconfig", verify_dbconfig(0) == 0);

	return (0);
}