4 * Copyright (C) Gregor Beck 2011
5 * Copyright (C) Michael Adam 2011
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "system/filesys.h"
23 #include "system/network.h"
24 #include "system/locale.h"
28 #include "protocol/protocol.h"
32 OUT_MODE = S_IRUSR | S_IWUSR,
33 OUT_FLAGS = O_EXCL|O_CREAT|O_RDWR,
37 struct ctdb_ltdb_header hdr;
38 uint32_t uints[MAX_HEADER_SIZE/4];
41 static const union ltdb_header DEFAULT_HDR = {
45 static int help(const char* cmd)
48 "Usage: %s [options] <command>\n"
51 " -s {0|32|64} specify how to determine the ctdb record header size\n"
52 " for the input database:\n"
53 " 0: no ctdb header\n"
54 " 32: ctdb header size of a 32 bit system (20 bytes)\n"
55 " 64: ctdb header size of a 64 bit system (24 bytes)\n"
56 " default: 32 or 64 depending on the system architecture\n"
58 " -S <num> the number of bytes to interpret as ctdb record header\n"
59 " for the input database (beware!)\n"
61 " -o {0|32|64} specify how to determine the ctdb record header size\n"
62 " for the output database\n"
63 " 0: no ctdb header\n"
64 " 32: ctdb header size of a 32 bit system (20 bytes)\n"
65 " 64: ctdb header size of a 64 bit system (24 bytes)\n"
66 " default: 32 or 64 depending on the system architecture\n"
68 " -O <num> the number of bytes to interpret as ctdb record header\n"
69 " for the output database (beware!)\n"
71 " -e Include empty records, defaults to off\n"
73 " -p print header (for the dump command), defaults to off\n"
75 " -h print this help\n"
78 " help print this help\n"
79 " dump <db> dump the db to stdout\n"
80 " convert <in_db> <out_db> convert the db\n\n", cmd);
84 static int usage(const char* cmd)
87 "Usage: %s dump [-e] [-p] [-s{0|32|64}] <idb>\n"
88 " %s convert [-e] [-s{0|32|64}] [-o{0|32|64}] <idb> <odb>\n"
95 ltdb_traverse(TDB_CONTEXT *tdb, int (*fn)(TDB_CONTEXT*, TDB_DATA, TDB_DATA,
96 struct ctdb_ltdb_header*, void *),
97 void *state, int hsize, bool skip_empty);
99 struct write_record_ctx {
106 write_record(TDB_CONTEXT* tdb, TDB_DATA key, TDB_DATA val,
107 struct ctdb_ltdb_header* hdr,
108 void* write_record_ctx);
111 struct dump_record_ctx {
113 void (*print_data)(FILE*, TDB_DATA);
114 void (*dump_header)(struct dump_record_ctx*, struct ctdb_ltdb_header*);
117 static int dump_record(TDB_CONTEXT* tdb, TDB_DATA key, TDB_DATA val,
118 struct ctdb_ltdb_header* hdr,
119 void* dump_record_ctx);
120 static void print_data_tdbdump(FILE* file, TDB_DATA data);
121 static void dump_header_full(struct dump_record_ctx*, struct ctdb_ltdb_header*);
122 static void dump_header_nop(struct dump_record_ctx* c,
123 struct ctdb_ltdb_header* h)
126 static int dump_db(const char* iname, FILE* ofile, int hsize, bool dump_header,
130 TDB_CONTEXT* idb = tdb_open(iname, 0, TDB_DEFAULT, O_RDONLY, 0);
132 perror("tdbopen in");
134 struct dump_record_ctx dump_ctx = {
136 .print_data = &print_data_tdbdump,
137 .dump_header = dump_header ? &dump_header_full
140 ret = ltdb_traverse(idb, &dump_record, &dump_ctx, hsize, !empty);
146 static int conv_db(const char* iname, const char* oname, size_t isize,
147 size_t osize, bool keep_empty)
150 TDB_CONTEXT* idb = tdb_open(iname, 0, TDB_DEFAULT, O_RDONLY, 0);
152 perror("tdbopen in");
154 TDB_CONTEXT* odb = tdb_open(oname, 0, TDB_DEFAULT, OUT_FLAGS, OUT_MODE);
156 perror("tdbopen out");
158 struct write_record_ctx ctx = {
161 .tdb_store_flags = TDB_REPLACE,
163 ret = ltdb_traverse(idb, &write_record, &ctx, isize, !keep_empty);
171 static bool parse_size(size_t* size, const char* arg, bool raw) {
174 val = strtol(arg, (char **) NULL, 10);
192 *size = MIN(val, MAX_HEADER_SIZE);
197 int main(int argc, char* argv[])
199 size_t isize = sizeof(struct ctdb_ltdb_header);
200 size_t osize = sizeof(struct ctdb_ltdb_header);
201 bool print_header = false;
202 bool keep_empty = false;
204 const char *cmd, *idb, *odb;
206 while ((opt = getopt(argc, argv, "s:o:S:O:phe")) != -1) {
210 if (!parse_size(&isize, optarg, isupper(opt))) {
211 return usage(argv[0]);
216 if (!parse_size(&osize, optarg, isupper(opt))) {
217 return usage(argv[0]);
227 return help(argv[0]);
229 return usage(argv[0]);
233 if (argc - optind < 1) {
234 return usage(argv[0]);
239 if (strcmp(cmd, "help") == 0) {
240 return help(argv[0]);
242 else if (strcmp(cmd, "dump") == 0) {
244 if (argc - optind != 2) {
245 return usage(argv[0]);
247 idb = argv[optind+1];
248 ret = dump_db(idb, stdout, isize, print_header, keep_empty);
249 return (ret >= 0) ? 0 : ret;
251 else if (strcmp(cmd, "convert") == 0) {
253 if (argc - optind != 3) {
254 return usage(argv[0]);
256 idb = argv[optind+1];
257 odb = argv[optind+2];
258 ret = conv_db(idb, odb, isize, osize, keep_empty);
259 return (ret >= 0) ? 0 : ret;
262 return usage(argv[0]);
265 struct ltdb_traverse_ctx {
266 int (*fn)(TDB_CONTEXT*,TDB_DATA,TDB_DATA,struct ctdb_ltdb_header*,void *);
274 ltdb_traverse_fn(TDB_CONTEXT* tdb, TDB_DATA key, TDB_DATA val,
275 void* ltdb_traverse_ctx)
277 struct ltdb_traverse_ctx* ctx =
278 (struct ltdb_traverse_ctx*)ltdb_traverse_ctx;
279 union ltdb_header hdr = DEFAULT_HDR;
281 const size_t hsize = MIN(sizeof(hdr), ctx->hsize);
282 if (val.dsize < hsize) {
283 fprintf(stderr, "Value too short to contain a ctdb header: ");
284 print_data_tdbdump(stderr, key);
285 fprintf(stderr, " = ");
286 print_data_tdbdump(stderr, val);
290 if (val.dsize == hsize && ctx->skip_empty) {
295 memcpy(&hdr, val.dptr, hsize);
297 if (hdr.uints[5] != 0) {
298 fprintf(stderr, "Warning: header padding isn't zero! Wrong header size?\n");
300 val.dptr += ctx->hsize;
301 val.dsize -= ctx->hsize;
302 return ctx->fn(tdb, key, val, &hdr.hdr, ctx->state);
305 int ltdb_traverse(TDB_CONTEXT *tdb,
306 int (*fn)(TDB_CONTEXT *,TDB_DATA,TDB_DATA,struct ctdb_ltdb_header*,void *),
307 void *state, int hsize, bool skip_empty)
309 struct ltdb_traverse_ctx ctx = {
312 .hsize = hsize < 0 ? sizeof(struct ctdb_ltdb_header) : hsize,
313 .skip_empty = skip_empty,
316 int ret = tdb_traverse(tdb, <db_traverse_fn, &ctx);
318 return (ret < 0) ? ret : (ret - ctx.nempty);
321 int write_record(TDB_CONTEXT* tdb, TDB_DATA key, TDB_DATA val,
322 struct ctdb_ltdb_header* hdr,
323 void* write_record_ctx)
325 struct write_record_ctx* ctx
326 = (struct write_record_ctx*)write_record_ctx;
328 if (ctx->hsize == 0) {
329 if (tdb_store(ctx->tdb, key, val, ctx->tdb_store_flags) == -1) {
330 fprintf(stderr, "tdb_store: %s\n", tdb_errorstr(ctx->tdb));
338 if(tdb_store(ctx->tdb, key, h, ctx->tdb_store_flags) == -1) {
339 fprintf(stderr, "tdb_store: %s\n", tdb_errorstr(ctx->tdb));
342 if(tdb_append(ctx->tdb, key, val) == -1) {
343 fprintf(stderr, "tdb_append: %s\n", tdb_errorstr(ctx->tdb));
350 int dump_record(TDB_CONTEXT* tdb, TDB_DATA key, TDB_DATA val,
351 struct ctdb_ltdb_header* hdr,
352 void* dump_record_ctx)
354 struct dump_record_ctx* ctx = (struct dump_record_ctx*)dump_record_ctx;
356 fprintf(ctx->file, "{\nkey(%d) = ", (int)key.dsize);
357 ctx->print_data(ctx->file, key);
358 fputc('\n', ctx->file);
359 ctx->dump_header(ctx, hdr);
360 fprintf(ctx->file, "data(%d) = ", (int)val.dsize);
361 ctx->print_data(ctx->file, val);
362 fprintf(ctx->file, "\n}\n");
366 void dump_header_full(struct dump_record_ctx* c, struct ctdb_ltdb_header* h)
368 fprintf(c->file, "dmaster: %d\nrsn: %llu\nflags: 0x%X\n",
370 (unsigned long long)h->rsn, h->flags);
373 void print_data_tdbdump(FILE* file, TDB_DATA data) {
374 unsigned char *ptr = data.dptr;
376 while (data.dsize--) {
377 if (isprint(*ptr) && !strchr("\"\\", *ptr)) {
380 fprintf(file, "\\%02X", *ptr);