1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3 #ident "$Id$"
4 /*======
5 This file is part of PerconaFT.
6 
7 
8 Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
9 
10     PerconaFT is free software: you can redistribute it and/or modify
11     it under the terms of the GNU General Public License, version 2,
12     as published by the Free Software Foundation.
13 
14     PerconaFT is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18 
19     You should have received a copy of the GNU General Public License
20     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
21 
22 ----------------------------------------
23 
24     PerconaFT is free software: you can redistribute it and/or modify
25     it under the terms of the GNU Affero General Public License, version 3,
26     as published by the Free Software Foundation.
27 
28     PerconaFT is distributed in the hope that it will be useful,
29     but WITHOUT ANY WARRANTY; without even the implied warranty of
30     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31     GNU Affero General Public License for more details.
32 
33     You should have received a copy of the GNU Affero General Public License
34     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
35 ======= */
36 
37 #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
38 
39 // test fopen, checkpoint fclose
40 
41 #include <sys/stat.h>
42 #include "test.h"
43 
44 
45 const int envflags = DB_INIT_MPOOL|DB_CREATE|DB_THREAD |DB_INIT_LOCK|DB_INIT_LOG|DB_INIT_TXN|DB_PRIVATE;
46 
47 const char *namea="a.db";
48 
run_test(void)49 static void run_test (void) {
50     int r;
51     toku_os_recursive_delete(TOKU_TEST_FILENAME);
52     toku_os_mkdir(TOKU_TEST_FILENAME, S_IRWXU+S_IRWXG+S_IRWXO);
53 
54     DB_ENV *env;
55     DB *db;
56     DB_TXN *txn;
57 
58     r = db_env_create(&env, 0);                                                         CKERR(r);
59     r = env->open(env, TOKU_TEST_FILENAME, envflags, S_IRWXU+S_IRWXG+S_IRWXO);                      CKERR(r);
60 
61     // fcreate
62     r = db_create(&db, env, 0);                                                         CKERR(r);
63     r = db->open(db, NULL, namea, NULL, DB_BTREE, DB_AUTO_COMMIT|DB_CREATE, 0666);      CKERR(r);
64     r = db->close(db, 0);                                                               CKERR(r);
65 
66     // dummy transaction
67     r = env->txn_begin(env, NULL, &txn, 0);                                             CKERR(r);
68 
69     // fopen
70     r = db_create(&db, env, 0);                                                         CKERR(r);
71     r = db->open(db, NULL, namea, NULL, DB_UNKNOWN, DB_AUTO_COMMIT, 0666);              CKERR(r);
72 
73     // fclose
74     r = db->close(db, 0);                                                               CKERR(r);
75 
76     // checkpoint
77     r = env->txn_checkpoint(env, 0, 0, 0);                                              CKERR(r);
78 
79     r = txn->commit(txn, DB_TXN_SYNC);                                                  CKERR(r);
80 
81     toku_hard_crash_on_purpose();
82 }
83 
run_recover(void)84 static void run_recover (void) {
85     DB_ENV *env;
86     int r;
87 
88     r = db_env_create(&env, 0);                                                             CKERR(r);
89     r = env->open(env, TOKU_TEST_FILENAME, envflags + DB_RECOVER, S_IRWXU+S_IRWXG+S_IRWXO);             CKERR(r);
90     r = env->close(env, 0);                                                                 CKERR(r);
91     exit(0);
92 }
93 
run_no_recover(void)94 static void run_no_recover (void) {
95     DB_ENV *env;
96     int r;
97 
98     r = db_env_create(&env, 0);                                                             CKERR(r);
99     r = env->open(env, TOKU_TEST_FILENAME, envflags & ~DB_RECOVER, S_IRWXU+S_IRWXG+S_IRWXO);            CKERR(r);
100     r = env->close(env, 0);                                                                 CKERR(r);
101     exit(0);
102 }
103 
104 const char *cmd;
105 
106 bool do_test=false, do_recover=false, do_recover_only=false, do_no_recover = false;
107 
test_parse_args(int argc,char * const argv[])108 static void test_parse_args (int argc, char * const argv[]) {
109     int resultcode;
110     cmd = argv[0];
111     argc--; argv++;
112     while (argc>0) {
113 	if (strcmp(argv[0], "-v") == 0) {
114 	    verbose++;
115 	} else if (strcmp(argv[0],"-q")==0) {
116 	    verbose--;
117 	    if (verbose<0) verbose=0;
118 	} else if (strcmp(argv[0], "--test")==0) {
119 	    do_test=true;
120         } else if (strcmp(argv[0], "--recover") == 0) {
121             do_recover=true;
122         } else if (strcmp(argv[0], "--recover-only") == 0) {
123             do_recover_only=true;
124         } else if (strcmp(argv[0], "--no-recover") == 0) {
125             do_no_recover=true;
126 	} else if (strcmp(argv[0], "-h")==0) {
127 	    resultcode=0;
128 	do_usage:
129 	    fprintf(stderr, "Usage:\n%s [-v|-q]* [-h] {--test | --recover } \n", cmd);
130 	    exit(resultcode);
131 	} else {
132 	    fprintf(stderr, "Unknown arg: %s\n", argv[0]);
133 	    resultcode=1;
134 	    goto do_usage;
135 	}
136 	argc--;
137 	argv++;
138     }
139 }
140 
test_main(int argc,char * const argv[])141 int test_main (int argc, char * const argv[]) {
142     test_parse_args(argc, argv);
143     if (do_test) {
144 	run_test();
145     } else if (do_recover) {
146         run_recover();
147     } else if (do_recover_only) {
148         run_recover();
149     } else if (do_no_recover) {
150         run_no_recover();
151     }
152     return 0;
153 }
154