test_db_env_open_nocreate.c 2.35 KB
Newer Older
1 2
/* -*- mode: C; c-basic-offset: 4 -*- */
#ident "Copyright (c) 2007 Tokutek Inc.  All rights reserved."
Yoni Fogel's avatar
Yoni Fogel committed
3 4
#include "test.h"

Yoni Fogel's avatar
Yoni Fogel committed
5

Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
6 7
// Try to open an environment where the directory does not exist 
// Try when the dir exists but is not an initialized env
8 9 10 11
// Try when the dir exists and we do DB_CREATE: it should work.
// And after that the open should work without a DB_CREATE
//   However, in BDB, after doing an DB_ENV->open and then a close, no state has changed
//   One must actually create a DB I think...
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
12 13 14 15 16 17 18 19 20

#include <assert.h>
#include <db.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>

21 22
#include <unistd.h>

23
// ENVDIR is defined in the Makefile
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
24

Yoni Fogel's avatar
Yoni Fogel committed
25 26 27
int
test_main(int argc, const char *argv[]) {
    parse_args(argc, argv);
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
28 29
    DB_ENV *dbenv;
    int r;
30
    int do_private;
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
31

32
    for (do_private=0; do_private<2; do_private++) {
33 34
#ifdef USE_TDB
	if (do_private==0) continue; // See #208.
35 36
#else
	if (do_private==1) continue; // See #530.  BDB 4.6.21 segfaults if DB_PRIVATE is passed when no environment previously exists.
37
#endif
38 39
	int private_flags = do_private ? DB_PRIVATE : 0;
	
40
	system("rm -rf " ENVDIR);
41 42
	r = db_env_create(&dbenv, 0);
	CKERR(r);
43
	r = dbenv->open(dbenv, ENVDIR, private_flags|DB_INIT_MPOOL, 0);
44 45 46
	assert(r==ENOENT);
	dbenv->close(dbenv,0); // free memory
	
47
	system("rm -rf " ENVDIR);
48
	toku_os_mkdir(ENVDIR, S_IRWXU+S_IRWXG+S_IRWXO);
49 50
	r = db_env_create(&dbenv, 0);
	CKERR(r);
51
	r = dbenv->open(dbenv, ENVDIR, private_flags|DB_INIT_MPOOL, 0);
52 53 54 55 56
#ifdef USE_TDB
	// TokuDB has no trouble opening an environment if the directory exists.
	CKERR(r);
	assert(r==0);
#else
57
	if (r!=ENOENT) printf("%s:%d %d: %s\n", __FILE__, __LINE__, r,db_strerror(r));
58
	assert(r==ENOENT);
59
#endif
60 61
	dbenv->close(dbenv,0); // free memory
    }
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
62

63
#ifndef USE_TDB
64
    // Now make sure that if we have a non-private DB that we can tell if it opened or not.
65
    DB *db;
66
    system("rm -rf " ENVDIR);
67
    toku_os_mkdir(ENVDIR, S_IRWXU+S_IRWXG+S_IRWXO);
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
68
    r = db_env_create(&dbenv, 0);
69
    CKERR(r);
70
    r = dbenv->open(dbenv, ENVDIR, DB_CREATE|DB_INIT_MPOOL, 0);
71 72 73
    CKERR(r);
    r=db_create(&db, dbenv, 0);
    CKERR(r);
74
    db->close(db, 0);
75 76 77
    dbenv->close(dbenv,0); // free memory
    r = db_env_create(&dbenv, 0);
    CKERR(r);
78
    r = dbenv->open(dbenv, ENVDIR, DB_INIT_MPOOL, 0);
79
    CKERR(r);
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
80
    dbenv->close(dbenv,0); // free memory
81 82
#endif

Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
83 84
    return 0;
}
85