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