Ejemplo n.º 1
0
static int btreeCleanupEnv(const char *home)
{
	DB_ENV *tmp_env;
	int count, i, ret;
	char **names, buf[512];

	log_msg(LOG_DEBUG, "btreeCleanupEnv removing existing env.");
	/*
	 * If there is a directory (environment), but no
	 * database file. Clear the environment to avoid
	 * carrying over information from earlier sessions.
	 */
	if ((ret = db_env_create(&tmp_env, 0)) != 0)
		return ret;

	/* Remove log files */
	if ((ret = __os_dirlist(tmp_env->env, home, 0, &names, &count)) != 0) {
		(void)tmp_env->close(tmp_env, 0);
		return ret;
	}

	for (i = 0; i < count; i++) {
		if (strncmp(names[i], "log.", 4) != 0)
			continue;
		sqlite3_snprintf(sizeof(buf), buf, "%s%s%s",
		    home, "/", names[i]);
		/*
		 * Use Berkeley DB __os_unlink (not sqlite3OsDelete) since
		 * this file has always been managed by Berkeley DB.
		 */
		(void)__os_unlink(NULL, buf, 0);
	}

	__os_dirfree(tmp_env->env, names, count);

	/*
	 * TODO: Do we want force here? Ideally all handles
	 * would always be closed on exit, so DB_FORCE would
	 * not be necessary.  The world is not currently ideal.
	 */
	return tmp_env->remove(tmp_env, home, DB_FORCE);
}
Ejemplo n.º 2
0
// Create test directory.
inline int dir_setup(const char *testdir)
{
	int ret;

#if DB_VERSION_MAJOR > 4 || DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 3
	if ((ret = __os_mkdir(NULL, testdir, 0755)) != 0) {
#else
	if ((ret = mkdir(testdir, 0755)) != 0) {
#endif
		fprintf(stderr,
		    "dir_setup: Creating directory %s: %s\n", 
		    testdir, db_strerror(ret));
		return (1);
	}
	return (0);
}

inline int os_unlink(const char *path)
{
#if DB_VERSION_MAJOR < 4 || DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR < 7
	return (__os_unlink(NULL, path));
#else
	return (__os_unlink(NULL, path, 0));
#endif
}

#if DB_VERSION_MAJOR > 4 || DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 4
#define	OS_EXISTS(a, b, c)	__os_exists(a, b, c)
#else
#define	OS_EXISTS(a, b, c)	__os_exists(b, c)
#endif

// Remove contents in specified directory recursively(in DB versions earlier 
// than 4.6, don't recursively), and optionally remove the directory itself.
int rmdir_rcsv(const char *dir, bool keep_this_dir)
{
	int cnt, i, isdir, ret;
	char buf[1024], **names;

	ret = 0;

	/* If the directory doesn't exist, we're done. */
	if (OS_EXISTS(NULL, dir, &isdir) != 0)
		return (0);

	/* Get a list of the directory contents. */
#if DB_VERSION_MAJOR > 4 || DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 6
	if ((ret = __os_dirlist(NULL, dir, 1, &names, &cnt)) != 0)
		return (ret);
#else
	if ((ret = __os_dirlist(NULL, dir, &names, &cnt)) != 0)
		return (ret);
#endif
	/* Go through the file name list, remove each file in the list */
	for (i = 0; i < cnt; ++i) {
		(void)snprintf(buf, sizeof(buf),
		    "%s%c%s", dir, PATH_SEPARATOR[0], names[i]);
		if ((ret = OS_EXISTS(NULL, buf, &isdir)) != 0)
			goto file_err;
		if (!isdir && (ret = os_unlink(buf)) != 0) {
file_err:		fprintf(stderr, 
			    "os_unlink: Error unlinking file: %s: %s\n",
			    buf, db_strerror(ret));
			break;
		}
		if (isdir && rmdir_rcsv(buf, false) != 0)
			goto file_err;
	}

	__os_dirfree(NULL, names, cnt);

	/*
	 * If we removed the contents of the directory and we don't want to
	 * keep this directory, remove the directory itself.
	 */
	if (i == cnt && !keep_this_dir && (ret = rmdir(dir)) != 0)
		fprintf(stderr,
		    "rmdir_rcsv(%s): %s\n", dir, db_strerror(errno));
	return (ret);
} // rmdir_rcsv
Ejemplo n.º 3
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);
}
Ejemplo n.º 4
0
int TestBackupSimpleEnvWithConfig(CuTest *ct) {
	DB_ENV *dbenv;
	DB *dbp;
	DBTYPE dtype;
	struct handlers *info;
	char **names;
	int cnt, has_callback;
	time_t end_time, secs1, secs2, start_time;
	u_int32_t flag, value;

	dtype = DB_BTREE;
	info = ct->context;
	has_callback = 0;
	flag = DB_EXCL;
	end_time = secs1 = secs2 = start_time = 0;

	/* Step 1: set up directories. */
	CuAssert(ct, "setup_dir", setup_dir(0, NULL) == 0);

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

	/*
	 * Step 3: store records into db so that there is more than
	 * 1 data page in the db.
	 */
	CuAssert(ct, "store_records", store_records(dbp, 10) == 0);
	CuAssert(ct, "DB->sync", dbp->sync(dbp, 0) == 0);

	/*
	 * Step 4: verify the backup handle is NULL,
	 * since we never configure the backup.
	 */
	CuAssert(ct, "DB_ENV->get_backup_config",
	    dbenv->get_backup_config(dbenv,
	    DB_BACKUP_WRITE_DIRECT, &value) == EINVAL);

	/*
	 * Step 5: backup without any backup configs.
	 * 5a: backup only the db file without callbacks and record the time.
	 */
	start_time = time(NULL);
	CuAssert(ct, "backup_db",
	    backup_db(ct, dbenv, BACKUP_DB, flag, has_callback) == 0);
	end_time = time(NULL);
	secs1 = end_time - start_time;

	/* 5b: verify db file is in BACKUP_DIR. */
	CuAssert(ct, "verify_db_log",
	    verify_db_log(dtype, 0, 0, NULL, NULL) == 0);

	/* 5c: verify that no other files are in BACKUP_DIR. */
	CuAssert(ct, "__os_dirlist",
	    __os_dirlist(NULL, BACKUP_DIR, 0, &names, &cnt) == 0);
	CuAssert(ct, "too many files in backupdir", cnt == 1);

	/* Clean up the backup directory. */
	setup_envdir(BACKUP_DIR, 1);

	/*
	 * Step 6: backup with backup configs.
	 * 6a: configure the backup handle: use direct I/O to write pages to
	 * the disk, the backup buffer size is 256 bytes (which is smaller
	 * than the db page size), the number of pages
	 * to read before pausing is 1, and the number of seconds to sleep
	 * between batches of reads is 1.
	 */
	CuAssert(ct, "DB_ENV->set_backup_config",
	    dbenv->set_backup_config(dbenv, DB_BACKUP_WRITE_DIRECT, 1) == 0);
	CuAssert(ct, "DB_ENV->set_backup_config",
	    dbenv->set_backup_config(dbenv, DB_BACKUP_SIZE, 256) == 0);
	CuAssert(ct, "DB_ENV->set_backup_config",
	    dbenv->set_backup_config(dbenv, DB_BACKUP_READ_COUNT, 1) == 0);
	CuAssert(ct, "DB_ENV->set_backup_config",
	    dbenv->set_backup_config(dbenv,
	    DB_BACKUP_READ_SLEEP, US_PER_SEC / 2) == 0);

	/*
	 * 6b: backup only the db file without callbacks and
	 * record the time.
	 */
	start_time = time(NULL);
	CuAssert(ct, "backup_db",
	    backup_db(ct, dbenv, BACKUP_DB, flag, has_callback) == 0);
	end_time = time(NULL);
	secs2 = end_time - start_time;

	/* 6c: verify db file is in BACKUP_DIR. */
	CuAssert(ct, "verify_db_log",
	    verify_db_log(dtype, 0, 0, NULL, NULL) == 0);

	/* 6d: no other files are in BACKUP_DIR. */
	CuAssert(ct, "__os_dirlist",
	    __os_dirlist(NULL, BACKUP_DIR, 0, &names, &cnt) == 0);
	CuAssert(ct, "too many files in backupdir", cnt == 1);

	/* 6e: verify the backup config. */
	CuAssert(ct, "DB_ENV->get_backup_config",
	    dbenv->get_backup_config(dbenv,
	    DB_BACKUP_READ_SLEEP, &value) == 0);
	CuAssertTrue(ct, value == US_PER_SEC / 2);
	/*
	 * Verify the backup config DB_BACKUP_READ_SLEEP works. That is with
	 * the configuration, backup pauses for a number of microseconds
	 * between batches of reads. So for the same backup content, the backup
	 * time with the configuration should be longer than that without it.
	 */
	CuAssertTrue(ct, secs2 > secs1);

	CuAssert(ct, "DB_ENV->get_backup_config",
	    dbenv->get_backup_config(dbenv,
	    DB_BACKUP_READ_COUNT, &value) == 0);
	CuAssertTrue(ct, value == 1);
	CuAssert(ct, "DB_ENV->get_backup_config",
	    dbenv->get_backup_config(dbenv, DB_BACKUP_SIZE, &value) == 0);
	CuAssertTrue(ct, value == 256);
	CuAssert(ct, "DB_ENV->get_backup_config",
	    dbenv->get_backup_config(dbenv,
	    DB_BACKUP_WRITE_DIRECT, &value) == 0);
	CuAssertTrue(ct, value == 1);

	/*
	 * Step 7: re-configure the backup write direct config and
	 * verify the new config value.
	 */
	CuAssert(ct, "DB_ENV->set_backup_config",
	    dbenv->set_backup_config(dbenv, DB_BACKUP_WRITE_DIRECT, 0) == 0);
	CuAssert(ct, "DB_ENV->get_backup_config",
	    dbenv->get_backup_config(dbenv,
	    DB_BACKUP_WRITE_DIRECT, &value) == 0);
	CuAssertTrue(ct, value == 0);

	return (0);
}