tools/ctdb: Pass memory context for returning nodes in parse_nodestring
[Samba/wip.git] / ctdb / libctdb / test / databases.c
blobb0b9bbc20bf924d88eaf71a2d738825c36a4701f
1 /*
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/>.
19 #include <unistd.h>
20 #include <err.h>
21 #include <talloc.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <dirent.h>
25 #include <fcntl.h>
26 #include "utils.h"
27 #include "log.h"
28 #include "ctdb-test.h"
29 #include <tdb.h>
30 #include <ctdb_protocol.h>
32 /* FIXME */
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",
40 key.dsize, key.dptr);
41 return -1;
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);
47 return -1;
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);
53 return -1;
55 return 0;
58 static void check_database(const char *name)
60 struct tdb_context *tdb = tdb_open(name, 0, TDB_DEFAULT, O_RDWR, 0);
61 if (!tdb)
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);
66 exit(EXIT_FAILURE);
68 tdb_close(tdb);
71 void check_databases(void)
73 struct dirent *ent;
74 DIR *d = opendir(DB_PATH);
75 if (!d)
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);
86 closedir(d);
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);
93 struct dirent *ent;
94 DIR *d = opendir(DB_PATH);
95 if (!d)
96 err(1, "Reading directory %s", DB_PATH);
98 while ((ent = readdir(d)) != NULL) {
99 if (strends(ent->d_name, ".tdb.0")) {
100 TDB_DATA data, key;
101 int fd;
102 char *fullpath = talloc_asprintf(NULL, "%s/%s",
103 DB_PATH, ent->d_name);
104 fd = open(fullpath, O_RDONLY);
105 if (fd < 0)
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);
112 close(fd);
115 closedir(d);
116 return tdb;
119 void restore_databases(void *_tdb)
121 struct tdb_context *tdb = _tdb;
122 TDB_DATA key, data;
124 for (key = tdb_firstkey(tdb); key.dptr; key = tdb_nextkey(tdb, key)) {
125 int fd = open((char *)key.dptr, O_WRONLY);
126 if (fd < 0)
127 err(1, "Restoring tdb %s", (char *)key.dptr);
128 data = tdb_fetch(tdb, key);
129 write(fd, data.dptr, data.dsize);
130 free(data.dptr);
131 close(fd);
133 tdb_close(tdb);