2 database code for libctdb
4 Copyright (C) Rusty Russell 2010
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include <sys/types.h>
28 #include "ctdb-test.h"
30 #include <ctdb_protocol.h>
33 #define DB_PATH "/tmp/ctdbd-test/dbs/"
35 static int check_header(TDB_DATA key
, TDB_DATA data
, void *unused
)
37 struct ctdb_ltdb_header
*hdr
= (void *)data
.dptr
;
38 if (data
.dsize
< sizeof(*hdr
)) {
39 log_line(LOG_ALWAYS
, "tdb entry '%.*s' is truncated",
43 /* Currently a single-node cluster. */
44 if (hdr
->dmaster
!= 0) {
45 log_line(LOG_ALWAYS
, "tdb entry '%.*s' dmaster %u",
46 key
.dsize
, key
.dptr
, hdr
->dmaster
);
49 /* Currently a single-node cluster. */
50 if (hdr
->laccessor
!= 0) {
51 log_line(LOG_ALWAYS
, "tdb entry '%.*s' laccessor %u",
52 key
.dsize
, key
.dptr
, hdr
->laccessor
);
58 static void check_database(const char *name
)
60 struct tdb_context
*tdb
= tdb_open(name
, 0, TDB_DEFAULT
, O_RDWR
, 0);
62 err(1, "Opening tdb %s", name
);
64 if (tdb_check(tdb
, check_header
, NULL
) != 0) {
65 log_line(LOG_ALWAYS
, "tdb %s is corrupt", name
);
71 void check_databases(void)
74 DIR *d
= opendir(DB_PATH
);
76 err(1, "Reading directory %s", DB_PATH
);
78 while ((ent
= readdir(d
)) != NULL
) {
79 if (strends(ent
->d_name
, ".tdb.0")) {
80 char *fullpath
= talloc_asprintf(NULL
, "%s/%s",
81 DB_PATH
, ent
->d_name
);
82 check_database(fullpath
);
83 talloc_free(fullpath
);
89 /* FIXME: We assume we don't need locks here. Not a solid assumption! */
90 void *save_databases(void)
92 struct tdb_context
*tdb
= tdb_open(NULL
, 0, TDB_INTERNAL
, 0, 0);
94 DIR *d
= opendir(DB_PATH
);
96 err(1, "Reading directory %s", DB_PATH
);
98 while ((ent
= readdir(d
)) != NULL
) {
99 if (strends(ent
->d_name
, ".tdb.0")) {
102 char *fullpath
= talloc_asprintf(NULL
, "%s/%s",
103 DB_PATH
, ent
->d_name
);
104 fd
= open(fullpath
, O_RDONLY
);
106 err(1, "Saving tdb %s", fullpath
);
107 data
.dptr
= grab_fd(fd
, &data
.dsize
);
108 key
.dptr
= (void *)fullpath
;
109 key
.dsize
= strlen(fullpath
) + 1;
110 tdb_store(tdb
, key
, data
, TDB_INSERT
);
111 talloc_free(fullpath
);
119 void restore_databases(void *_tdb
)
121 struct tdb_context
*tdb
= _tdb
;
124 for (key
= tdb_firstkey(tdb
); key
.dptr
; key
= tdb_nextkey(tdb
, key
)) {
125 int fd
= open((char *)key
.dptr
, O_WRONLY
);
127 err(1, "Restoring tdb %s", (char *)key
.dptr
);
128 data
= tdb_fetch(tdb
, key
);
129 write(fd
, data
.dptr
, data
.dsize
);