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 
checkpoint_callback_closeit(void * extra)49 static void checkpoint_callback_closeit(void *extra) {
50     DB *db = (DB *) extra;
51     int r = db->close(db, 0); CKERR(r);
52 }
53 
run_test(void)54 static void run_test (void) {
55     int r;
56     toku_os_recursive_delete(TOKU_TEST_FILENAME);
57     toku_os_mkdir(TOKU_TEST_FILENAME, S_IRWXU+S_IRWXG+S_IRWXO);
58 
59     DB_ENV *env;
60     DB *db;
61     DB_TXN *txn;
62 
63     r = db_env_create(&env, 0);                                                         CKERR(r);
64     r = env->open(env, TOKU_TEST_FILENAME, envflags, S_IRWXU+S_IRWXG+S_IRWXO);                      CKERR(r);
65 
66     // fcreate
67     r = db_create(&db, env, 0);                                                         CKERR(r);
68     r = db->open(db, NULL, namea, NULL, DB_BTREE, DB_AUTO_COMMIT|DB_CREATE, 0666);      CKERR(r);
69     r = db->close(db, 0);                                                               CKERR(r);
70 
71     // dummy transaction
72     r = env->txn_begin(env, NULL, &txn, 0);                                             CKERR(r);
73 
74     // fopen
75     r = db_create(&db, env, 0);                                                         CKERR(r);
76     r = db->open(db, NULL, namea, NULL, DB_UNKNOWN, DB_AUTO_COMMIT, 0666);              CKERR(r);
77 
78     db_env_set_checkpoint_callback(checkpoint_callback_closeit, db);
79 
80     // checkpoint
81     r = env->txn_checkpoint(env, 0, 0, 0);                                              CKERR(r);
82 
83     r = txn->commit(txn, DB_TXN_SYNC);                                                  CKERR(r);
84 
85     toku_hard_crash_on_purpose();
86 }
87 
run_recover(void)88 static void run_recover (void) {
89     DB_ENV *env;
90     int r;
91 
92     r = db_env_create(&env, 0);                                                             CKERR(r);
93     r = env->open(env, TOKU_TEST_FILENAME, envflags + DB_RECOVER, S_IRWXU+S_IRWXG+S_IRWXO);             CKERR(r);
94     r = env->close(env, 0);                                                                 CKERR(r);
95     exit(0);
96 }
97 
run_no_recover(void)98 static void run_no_recover (void) {
99     DB_ENV *env;
100     int r;
101 
102     r = db_env_create(&env, 0);                                                             CKERR(r);
103     r = env->open(env, TOKU_TEST_FILENAME, envflags & ~DB_RECOVER, S_IRWXU+S_IRWXG+S_IRWXO);            CKERR(r);
104     r = env->close(env, 0);                                                                 CKERR(r);
105     exit(0);
106 }
107 
108 const char *cmd;
109 
110 bool do_test=false, do_recover=false, do_recover_only=false, do_no_recover = false;
111 
test_parse_args(int argc,char * const argv[])112 static void test_parse_args (int argc, char * const argv[]) {
113     int resultcode;
114     cmd = argv[0];
115     argc--; argv++;
116     while (argc>0) {
117 	if (strcmp(argv[0], "-v") == 0) {
118 	    verbose++;
119 	} else if (strcmp(argv[0],"-q")==0) {
120 	    verbose--;
121 	    if (verbose<0) verbose=0;
122 	} else if (strcmp(argv[0], "--test")==0) {
123 	    do_test=true;
124         } else if (strcmp(argv[0], "--recover") == 0) {
125             do_recover=true;
126         } else if (strcmp(argv[0], "--recover-only") == 0) {
127             do_recover_only=true;
128         } else if (strcmp(argv[0], "--no-recover") == 0) {
129             do_no_recover=true;
130 	} else if (strcmp(argv[0], "-h")==0) {
131 	    resultcode=0;
132 	do_usage:
133 	    fprintf(stderr, "Usage:\n%s [-v|-q]* [-h] {--test | --recover } \n", cmd);
134 	    exit(resultcode);
135 	} else {
136 	    fprintf(stderr, "Unknown arg: %s\n", argv[0]);
137 	    resultcode=1;
138 	    goto do_usage;
139 	}
140 	argc--;
141 	argv++;
142     }
143 }
144 
test_main(int argc,char * const argv[])145 int test_main (int argc, char * const argv[]) {
146     test_parse_args(argc, argv);
147     if (do_test) {
148 	run_test();
149     } else if (do_recover) {
150         run_recover();
151     } else if (do_recover_only) {
152         run_recover();
153     } else if (do_no_recover) {
154         run_no_recover();
155     }
156     return 0;
157 }
158