New version 1.0.114.8
[obnox/ctdb.git] / tools / ltdbtool.c
1 /*
2  * ctdb local tdb tool
3  *
4  * Copyright (C) Gregor Beck 2011
5  * Copyright (C) Michael Adam 2011
6  *
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.
11  *
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.
16  *
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/>.
19  */
20
21 #include <stdio.h>
22 #include <stdbool.h>
23 #include <stddef.h>
24 #include <stdlib.h>
25 #include <ctype.h> /* isprint */
26 #include <string.h> /* strstr */
27 #include <fcntl.h> /* mode_t */
28 #include <sys/stat.h> /* S_IRUSR */
29 #include <stdint.h> /* uint32_t */
30 #include <netinet/in.h> /* struct sockaddr_in */
31 #include <sys/socket.h> /* struct sockaddr */
32 #include <sys/param.h>  /* MIN */
33 #include <tdb.h>
34 #include <unistd.h> /* getopt */
35 #include <errno.h>
36
37 #include "includes.h"
38 #include "ctdb_private.h"
39
40 enum {
41         MAX_HEADER_SIZE=24,
42         OUT_MODE = S_IRUSR | S_IWUSR,
43         OUT_FLAGS = O_EXCL|O_CREAT|O_RDWR,
44 };
45
46 union  ltdb_header {
47         struct ctdb_ltdb_header hdr;
48         uint32_t uints[MAX_HEADER_SIZE/4];
49 };
50
51 static const union ltdb_header DEFAULT_HDR = {
52         .hdr.dmaster = -1,
53 };
54
55 static int help(const char* cmd)
56 {
57         fprintf(stdout, ""
58 "Usage: %s [options] <command>\n"
59 "\n"
60 "Options:\n"
61 "   -s {0|32|64}    specify how to determine the ctdb record header size\n"
62 "                   for the input 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"
67 "\n"
68 "   -S <num>        the number of bytes to interpret as ctdb record header\n"
69 "                   for the input database (beware!)\n"
70 "\n"
71 "   -o {0|32|64}    specify how to determine the ctdb record header size\n"
72 "                   for the output database\n"
73 "                   0: no ctdb header\n"
74 "                   32: ctdb header size of a 32 bit system (20 bytes)\n"
75 "                   64: ctdb header size of a 64 bit system (24 bytes)\n"
76 "                   default: 32 or 64 depending on the system architecture\n"
77 "\n"
78 "   -O <num>        the number of bytes to interpret as ctdb record header\n"
79 "                   for the output database (beware!)\n"
80 "\n"
81 "   -p              print header (for the dump command), defaults ot off\n"
82 "\n"
83 "   -h              print this help\n"
84 "\n"
85 "Commands:\n"
86 "  help                         print this help\n"
87 "  dump <db>                    dump the db to stdout\n"
88 "  convert <in_db> <out_db>     convert the db\n\n", cmd);
89         return 0;
90 }
91
92 static int usage(const char* cmd)
93 {
94         fprintf(stderr,
95                 "Usage: %s dump [-p] [-s{0|32|64}] <idb>\n"
96                 "       %s convert [-s{0|32|64}] [-o{0|32|64}] <idb> <odb>\n"
97                 "       %s {help|-h}\n"
98                 , cmd, cmd, cmd);
99         return -1;
100 }
101
102 static int
103 ltdb_traverse(TDB_CONTEXT *tdb, int (*fn)(TDB_CONTEXT*, TDB_DATA, TDB_DATA,
104                                           struct ctdb_ltdb_header*, void *),
105               void *state, int hsize, bool skip_empty);
106
107 struct write_record_ctx {
108         TDB_CONTEXT* tdb;
109         size_t hsize;
110         int tdb_store_flags;
111 };
112
113 static int
114 write_record(TDB_CONTEXT* tdb, TDB_DATA key, TDB_DATA val,
115              struct ctdb_ltdb_header* hdr,
116              void* write_record_ctx);
117
118
119 struct dump_record_ctx {
120         FILE* file;
121         void (*print_data)(FILE*, TDB_DATA);
122         void (*dump_header)(struct dump_record_ctx*, struct ctdb_ltdb_header*);
123 };
124
125 static int dump_record(TDB_CONTEXT* tdb, TDB_DATA key, TDB_DATA val,
126                        struct ctdb_ltdb_header* hdr,
127                        void* dump_record_ctx);
128 static void print_data_tdbdump(FILE* file, TDB_DATA data);
129 static void dump_header_full(struct dump_record_ctx*, struct ctdb_ltdb_header*);
130 static void dump_header_nop(struct dump_record_ctx* c,
131                             struct ctdb_ltdb_header* h)
132 {}
133
134 static int dump_db(const char* iname, FILE* ofile, int hsize, bool dump_header,
135                    bool empty)
136 {
137         int ret = -1;
138         TDB_CONTEXT* idb = tdb_open(iname, 0, TDB_DEFAULT, O_RDONLY, 0);
139         if (!idb) {
140                 perror("tdbopen in");
141         } else {
142                 struct dump_record_ctx dump_ctx = {
143                         .file = ofile,
144                         .print_data =  &print_data_tdbdump,
145                         .dump_header = dump_header ? &dump_header_full
146                                                    : &dump_header_nop,
147                 };
148                 ret = ltdb_traverse(idb, &dump_record, &dump_ctx, hsize, !empty);
149                 tdb_close(idb);
150         }
151         return ret;
152 }
153
154 static int conv_db(const char* iname, const char* oname, size_t isize,
155                    size_t osize, bool keep_empty)
156 {
157         int ret = -1;
158         TDB_CONTEXT* idb = tdb_open(iname, 0, TDB_DEFAULT, O_RDONLY, 0);
159         if (!idb) {
160                 perror("tdbopen in");
161         } else {
162                 TDB_CONTEXT* odb = tdb_open(oname, 0, TDB_DEFAULT, OUT_FLAGS, OUT_MODE);
163                 if (!odb) {
164                         perror("tdbopen out");
165                 } else {
166                         struct write_record_ctx ctx = {
167                                 .tdb = odb,
168                                 .hsize = osize,
169                                 .tdb_store_flags = TDB_REPLACE,
170                         };
171                         ret = ltdb_traverse(idb, &write_record, &ctx, isize, !keep_empty);
172                         tdb_close(odb);
173                 }
174                 tdb_close(idb);
175         }
176         return ret;
177 }
178
179 static bool parse_size(size_t* size, const char* arg, bool raw) {
180         long val;
181         errno = 0;
182         val = strtol(arg, (char **) NULL, 10);
183         if (errno != 0) {
184                 return false;
185         }
186         if (!raw) {
187                 switch(val) {
188                 case 0:
189                         break;
190                 case 32:
191                         val = 20;
192                         break;
193                 case 64:
194                         val = 24;
195                         break;
196                 default:
197                         return false;
198                 }
199         }
200         *size = MIN(val, MAX_HEADER_SIZE);
201         return true;
202 }
203
204
205 int main(int argc, char* argv[])
206 {
207         size_t isize = sizeof(struct ctdb_ltdb_header);
208         size_t osize = sizeof(struct ctdb_ltdb_header);
209         bool print_header = false;
210         bool keep_empty = false;
211         int opt;
212         const char *cmd, *idb, *odb;
213
214         while ((opt = getopt(argc, argv, "s:o:S:O:ph:e")) != -1) {
215                 switch (opt) {
216                 case 's':
217                 case 'S':
218                         if (!parse_size(&isize, optarg, isupper(opt))) {
219                                 return usage(argv[0]);
220                         }
221                         break;
222                 case 'o':
223                 case 'O':
224                         if (!parse_size(&osize, optarg, isupper(opt))) {
225                                 return usage(argv[0]);
226                         }
227                         break;
228                 case 'p':
229                         print_header = true;
230                         break;
231                 case 'e':
232                         keep_empty = true;
233                 case 'h':
234                         return help(argv[0]);
235                 default:
236                         return usage(argv[0]);
237                 }
238         }
239
240         if (argc - optind < 1) {
241                 return usage(argv[0]);
242         }
243
244         cmd = argv[optind];
245
246         if (strcmp(cmd, "help") == 0) {
247                 return help(argv[0]);
248         }
249         else if (strcmp(cmd, "dump") == 0) {
250                 int ret;
251                 if (argc - optind != 2) {
252                         return usage(argv[0]);
253                 }
254                 idb = argv[optind+1];
255                 ret = dump_db(idb, stdout, isize, print_header, keep_empty);
256                 return (ret >= 0) ? 0 : ret;
257         }
258         else if (strcmp(cmd, "convert") == 0) {
259                 int ret;
260                 if (argc - optind != 3) {
261                         return usage(argv[0]);
262                 }
263                 idb = argv[optind+1];
264                 odb = argv[optind+2];
265                 ret = conv_db(idb, odb, isize, osize, keep_empty);
266                 return (ret >= 0) ? 0 : ret;
267         }
268
269         return usage(argv[0]);
270 }
271
272 struct ltdb_traverse_ctx {
273         int (*fn)(TDB_CONTEXT*,TDB_DATA,TDB_DATA,struct ctdb_ltdb_header*,void *);
274         void* state;
275         size_t hsize;
276         bool skip_empty;
277         unsigned nempty;
278 };
279
280 static int
281 ltdb_traverse_fn(TDB_CONTEXT* tdb, TDB_DATA key, TDB_DATA val,
282                  void* ltdb_traverse_ctx)
283 {
284         struct ltdb_traverse_ctx* ctx =
285                 (struct ltdb_traverse_ctx*)ltdb_traverse_ctx;
286         union ltdb_header hdr = DEFAULT_HDR;
287
288         const size_t hsize = MIN(sizeof(hdr), ctx->hsize);
289         if (val.dsize < hsize) {
290                 fprintf(stderr, "Value too short to contain a ctdb header: ");
291                 print_data_tdbdump(stderr, key);
292                 fprintf(stderr, " = ");
293                 print_data_tdbdump(stderr, val);
294                 fputc('\n', stderr);
295                 return -1;
296         }
297         if (val.dsize == hsize && ctx->skip_empty) {
298                 ctx->nempty++;
299                 return 0;
300         }
301
302         memcpy(&hdr, val.dptr, hsize);
303
304         if (hdr.uints[5] != 0) {
305                 fprintf(stderr, "Warning: header padding isn't zero! Wrong header size?\n");
306         }
307         val.dptr += ctx->hsize;
308         val.dsize -= ctx->hsize;
309         return ctx->fn(tdb, key, val, &hdr.hdr, ctx->state);
310 }
311
312 int ltdb_traverse(TDB_CONTEXT *tdb,
313                   int (*fn)(TDB_CONTEXT *,TDB_DATA,TDB_DATA,struct ctdb_ltdb_header*,void *),
314                   void *state, int hsize, bool skip_empty)
315 {
316         struct ltdb_traverse_ctx ctx = {
317                 .fn = fn,
318                 .state = state,
319                 .hsize = hsize < 0 ? sizeof(struct ctdb_ltdb_header) : hsize,
320                 .skip_empty = skip_empty,
321                 .nempty = 0,
322         };
323         int ret = tdb_traverse(tdb, &ltdb_traverse_fn, &ctx);
324
325         return (ret < 0) ? ret : (ret - ctx.nempty);
326 }
327
328 int write_record(TDB_CONTEXT* tdb, TDB_DATA key, TDB_DATA val,
329                  struct ctdb_ltdb_header* hdr,
330                  void* write_record_ctx)
331 {
332         struct write_record_ctx* ctx
333                 = (struct write_record_ctx*)write_record_ctx;
334
335         if (ctx->hsize == 0) {
336                 if (tdb_store(ctx->tdb, key, val, ctx->tdb_store_flags) == -1) {
337                         fprintf(stderr, "tdb_store: %s\n", tdb_errorstr(ctx->tdb));
338                         return -1;
339                 }
340         } else {
341                 TDB_DATA h = {
342                         .dptr = (void*)hdr,
343                         .dsize = ctx->hsize,
344                 };
345                 if(tdb_store(ctx->tdb, key, h, ctx->tdb_store_flags) == -1) {
346                         fprintf(stderr, "tdb_store: %s\n", tdb_errorstr(ctx->tdb));
347                         return -1;
348                 }
349                 if(tdb_append(ctx->tdb, key, val) == -1) {
350                         fprintf(stderr, "tdb_append: %s\n", tdb_errorstr(ctx->tdb));
351                         return -1;
352                 }
353         }
354         return 0;
355 }
356
357 int dump_record(TDB_CONTEXT* tdb, TDB_DATA key, TDB_DATA val,
358                 struct ctdb_ltdb_header* hdr,
359                 void* dump_record_ctx)
360 {
361         struct dump_record_ctx* ctx = (struct dump_record_ctx*)dump_record_ctx;
362
363         fprintf(ctx->file, "{\nkey(%d) = ", (int)key.dsize);
364         ctx->print_data(ctx->file, key);
365         fputc('\n', ctx->file);
366         ctx->dump_header(ctx, hdr);
367         fprintf(ctx->file, "data(%d) = ", (int)val.dsize);
368         ctx->print_data(ctx->file, val);
369         fprintf(ctx->file, "\n}\n");
370         return 0;
371 }
372
373 void dump_header_full(struct dump_record_ctx* c, struct ctdb_ltdb_header* h)
374 {
375         fprintf(c->file, "dmaster: %d\nrsn: %llu\nflags: 0x%X\n",
376                 (int)h->dmaster,
377                 (unsigned long long)h->rsn, h->flags);
378 }
379
380 void print_data_tdbdump(FILE* file, TDB_DATA data) {
381         unsigned char *ptr = data.dptr;
382         fputc('"', file);
383         while (data.dsize--) {
384                 if (isprint(*ptr) && !strchr("\"\\", *ptr)) {
385                         fputc(*ptr, file);
386                 } else {
387                         fprintf(file, "\\%02X", *ptr);
388                 }
389                 ptr++;
390         }
391         fputc('"',file);
392 }
393