Merge branch 'singlecompression' into single
[kai/samba-autobuild/.git] / tdb / tools / tdbtool.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba database functions
4    Copyright (C) Andrew Tridgell              1999-2000
5    Copyright (C) Paul `Rusty' Russell              2000
6    Copyright (C) Jeremy Allison                    2000
7    Copyright (C) Andrew Esh                        2001
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "replace.h"
24 #include "system/locale.h"
25 #include "system/time.h"
26 #include "system/filesys.h"
27 #include "system/wait.h"
28 #include "tdb.h"
29
30 static int do_command(void);
31 const char *cmdname;
32 char *arg1, *arg2;
33 size_t arg1len, arg2len;
34 int bIterate = 0;
35 char *line;
36 TDB_DATA iterate_kbuf;
37 char cmdline[1024];
38 static int disable_mmap;
39
40 enum commands {
41         CMD_CREATE_TDB,
42         CMD_OPEN_TDB,
43         CMD_ERASE,
44         CMD_DUMP,
45         CMD_INSERT,
46         CMD_MOVE,
47         CMD_STORE,
48         CMD_SHOW,
49         CMD_KEYS,
50         CMD_HEXKEYS,
51         CMD_DELETE,
52         CMD_LIST_HASH_FREE,
53         CMD_LIST_FREE,
54         CMD_INFO,
55         CMD_MMAP,
56         CMD_SPEED,
57         CMD_FIRST,
58         CMD_NEXT,
59         CMD_SYSTEM,
60         CMD_QUIT,
61         CMD_HELP
62 };
63
64 typedef struct {
65         const char *name;
66         enum commands cmd;
67 } COMMAND_TABLE;
68
69 COMMAND_TABLE cmd_table[] = {
70         {"create",      CMD_CREATE_TDB},
71         {"open",        CMD_OPEN_TDB},
72         {"erase",       CMD_ERASE},
73         {"dump",        CMD_DUMP},
74         {"insert",      CMD_INSERT},
75         {"move",        CMD_MOVE},
76         {"store",       CMD_STORE},
77         {"show",        CMD_SHOW},
78         {"keys",        CMD_KEYS},
79         {"hexkeys",     CMD_HEXKEYS},
80         {"delete",      CMD_DELETE},
81         {"list",        CMD_LIST_HASH_FREE},
82         {"free",        CMD_LIST_FREE},
83         {"info",        CMD_INFO},
84         {"speed",       CMD_SPEED},
85         {"mmap",        CMD_MMAP},
86         {"first",       CMD_FIRST},
87         {"1",           CMD_FIRST},
88         {"next",        CMD_NEXT},
89         {"n",           CMD_NEXT},
90         {"quit",        CMD_QUIT},
91         {"q",           CMD_QUIT},
92         {"!",           CMD_SYSTEM},
93         {NULL,          CMD_HELP}
94 };
95
96 struct timeval tp1,tp2;
97
98 static void _start_timer(void)
99 {
100         gettimeofday(&tp1,NULL);
101 }
102
103 static double _end_timer(void)
104 {
105         gettimeofday(&tp2,NULL);
106         return((tp2.tv_sec - tp1.tv_sec) + 
107                (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
108 }
109
110 /* a tdb tool for manipulating a tdb database */
111
112 static TDB_CONTEXT *tdb;
113
114 static int print_rec(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
115 static int print_key(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
116 static int print_hexkey(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
117
118 static void print_asc(const char *buf,int len)
119 {
120         int i;
121
122         /* We're probably printing ASCII strings so don't try to display
123            the trailing NULL character. */
124
125         if (buf[len - 1] == 0)
126                 len--;
127
128         for (i=0;i<len;i++)
129                 printf("%c",isprint(buf[i])?buf[i]:'.');
130 }
131
132 static void print_data(const char *buf,int len)
133 {
134         int i=0;
135         if (len<=0) return;
136         printf("[%03X] ",i);
137         for (i=0;i<len;) {
138                 printf("%02X ",(int)((unsigned char)buf[i]));
139                 i++;
140                 if (i%8 == 0) printf(" ");
141                 if (i%16 == 0) {      
142                         print_asc(&buf[i-16],8); printf(" ");
143                         print_asc(&buf[i-8],8); printf("\n");
144                         if (i<len) printf("[%03X] ",i);
145                 }
146         }
147         if (i%16) {
148                 int n;
149                 
150                 n = 16 - (i%16);
151                 printf(" ");
152                 if (n>8) printf(" ");
153                 while (n--) printf("   ");
154                 
155                 n = i%16;
156                 if (n > 8) n = 8;
157                 print_asc(&buf[i-(i%16)],n); printf(" ");
158                 n = (i%16) - n;
159                 if (n>0) print_asc(&buf[i-n],n); 
160                 printf("\n");    
161         }
162 }
163
164 static void help(void)
165 {
166         printf("\n"
167 "tdbtool: \n"
168 "  create    dbname     : create a database\n"
169 "  open      dbname     : open an existing database\n"
170 "  erase                : erase the database\n"
171 "  dump                 : dump the database as strings\n"
172 "  keys                 : dump the database keys as strings\n"
173 "  hexkeys              : dump the database keys as hex values\n"
174 "  info                 : print summary info about the database\n"
175 "  insert    key  data  : insert a record\n"
176 "  move      key  file  : move a record to a destination tdb\n"
177 "  store     key  data  : store a record (replace)\n"
178 "  show      key        : show a record by key\n"
179 "  delete    key        : delete a record by key\n"
180 "  list                 : print the database hash table and freelist\n"
181 "  free                 : print the database freelist\n"
182 "  ! command            : execute system command\n"             
183 "  1 | first            : print the first record\n"
184 "  n | next             : print the next record\n"
185 "  q | quit             : terminate\n"
186 "  \\n                   : repeat 'next' command\n"
187 "\n");
188 }
189
190 static void terror(const char *why)
191 {
192         printf("%s\n", why);
193 }
194
195 static void create_tdb(const char *tdbname)
196 {
197         if (tdb) tdb_close(tdb);
198         tdb = tdb_open(tdbname, 0, TDB_CLEAR_IF_FIRST | (disable_mmap?TDB_NOMMAP:0),
199                        O_RDWR | O_CREAT | O_TRUNC, 0600);
200         if (!tdb) {
201                 printf("Could not create %s: %s\n", tdbname, strerror(errno));
202         }
203 }
204
205 static void open_tdb(const char *tdbname)
206 {
207         if (tdb) tdb_close(tdb);
208         tdb = tdb_open(tdbname, 0, disable_mmap?TDB_NOMMAP:0, O_RDWR, 0600);
209         if (!tdb) {
210                 printf("Could not open %s: %s\n", tdbname, strerror(errno));
211         }
212 }
213
214 static void insert_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
215 {
216         TDB_DATA key, dbuf;
217
218         if ((keyname == NULL) || (keylen == 0)) {
219                 terror("need key");
220                 return;
221         }
222
223         key.dptr = (unsigned char *)keyname;
224         key.dsize = keylen;
225         dbuf.dptr = (unsigned char *)data;
226         dbuf.dsize = datalen;
227
228         if (tdb_store(tdb, key, dbuf, TDB_INSERT) == -1) {
229                 terror("insert failed");
230         }
231 }
232
233 static void store_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
234 {
235         TDB_DATA key, dbuf;
236
237         if ((keyname == NULL) || (keylen == 0)) {
238                 terror("need key");
239                 return;
240         }
241
242         if ((data == NULL) || (datalen == 0)) {
243                 terror("need data");
244                 return;
245         }
246
247         key.dptr = (unsigned char *)keyname;
248         key.dsize = keylen;
249         dbuf.dptr = (unsigned char *)data;
250         dbuf.dsize = datalen;
251
252         printf("Storing key:\n");
253         print_rec(tdb, key, dbuf, NULL);
254
255         if (tdb_store(tdb, key, dbuf, TDB_REPLACE) == -1) {
256                 terror("store failed");
257         }
258 }
259
260 static void show_tdb(char *keyname, size_t keylen)
261 {
262         TDB_DATA key, dbuf;
263
264         if ((keyname == NULL) || (keylen == 0)) {
265                 terror("need key");
266                 return;
267         }
268
269         key.dptr = (unsigned char *)keyname;
270         key.dsize = keylen;
271
272         dbuf = tdb_fetch(tdb, key);
273         if (!dbuf.dptr) {
274             terror("fetch failed");
275             return;
276         }
277         
278         print_rec(tdb, key, dbuf, NULL);
279         
280         free( dbuf.dptr );
281         
282         return;
283 }
284
285 static void delete_tdb(char *keyname, size_t keylen)
286 {
287         TDB_DATA key;
288
289         if ((keyname == NULL) || (keylen == 0)) {
290                 terror("need key");
291                 return;
292         }
293
294         key.dptr = (unsigned char *)keyname;
295         key.dsize = keylen;
296
297         if (tdb_delete(tdb, key) != 0) {
298                 terror("delete failed");
299         }
300 }
301
302 static void move_rec(char *keyname, size_t keylen, char* tdbname)
303 {
304         TDB_DATA key, dbuf;
305         TDB_CONTEXT *dst_tdb;
306
307         if ((keyname == NULL) || (keylen == 0)) {
308                 terror("need key");
309                 return;
310         }
311
312         if ( !tdbname ) {
313                 terror("need destination tdb name");
314                 return;
315         }
316
317         key.dptr = (unsigned char *)keyname;
318         key.dsize = keylen;
319
320         dbuf = tdb_fetch(tdb, key);
321         if (!dbuf.dptr) {
322                 terror("fetch failed");
323                 return;
324         }
325         
326         print_rec(tdb, key, dbuf, NULL);
327         
328         dst_tdb = tdb_open(tdbname, 0, 0, O_RDWR, 0600);
329         if ( !dst_tdb ) {
330                 terror("unable to open destination tdb");
331                 return;
332         }
333         
334         if ( tdb_store( dst_tdb, key, dbuf, TDB_REPLACE ) == -1 ) {
335                 terror("failed to move record");
336         }
337         else
338                 printf("record moved\n");
339         
340         tdb_close( dst_tdb );
341         
342         return;
343 }
344
345 static int print_rec(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
346 {
347         printf("\nkey %d bytes\n", (int)key.dsize);
348         print_asc((const char *)key.dptr, key.dsize);
349         printf("\ndata %d bytes\n", (int)dbuf.dsize);
350         print_data((const char *)dbuf.dptr, dbuf.dsize);
351         return 0;
352 }
353
354 static int print_key(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
355 {
356         printf("key %d bytes: ", (int)key.dsize);
357         print_asc((const char *)key.dptr, key.dsize);
358         printf("\n");
359         return 0;
360 }
361
362 static int print_hexkey(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
363 {
364         printf("key %d bytes\n", (int)key.dsize);
365         print_data((const char *)key.dptr, key.dsize);
366         printf("\n");
367         return 0;
368 }
369
370 static int total_bytes;
371
372 static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
373 {
374         total_bytes += dbuf.dsize;
375         return 0;
376 }
377
378 static void info_tdb(void)
379 {
380         int count;
381         total_bytes = 0;
382         if ((count = tdb_traverse(tdb, traverse_fn, NULL)) == -1)
383                 printf("Error = %s\n", tdb_errorstr(tdb));
384         else
385                 printf("%d records totalling %d bytes\n", count, total_bytes);
386 }
387
388 static void speed_tdb(const char *tlimit)
389 {
390         unsigned timelimit = tlimit?atoi(tlimit):0;
391         double t;
392         int ops=0;
393         if (timelimit == 0) timelimit = 10;
394         printf("Testing traverse speed for %u seconds\n", timelimit);
395         _start_timer();
396         while ((t=_end_timer()) < timelimit) {
397                 tdb_traverse(tdb, traverse_fn, NULL);
398                 printf("%10.3f ops/sec\r", (++ops)/t);
399         }
400         printf("\n");
401 }
402
403 static void toggle_mmap(void)
404 {
405         disable_mmap = !disable_mmap;
406         if (disable_mmap) {
407                 printf("mmap is disabled\n");
408         } else {
409                 printf("mmap is enabled\n");
410         }
411 }
412
413 static char *tdb_getline(const char *prompt)
414 {
415         static char thisline[1024];
416         char *p;
417         fputs(prompt, stdout);
418         thisline[0] = 0;
419         p = fgets(thisline, sizeof(thisline)-1, stdin);
420         if (p) p = strchr(p, '\n');
421         if (p) *p = 0;
422         return p?thisline:NULL;
423 }
424
425 static int do_delete_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf,
426                      void *state)
427 {
428     return tdb_delete(the_tdb, key);
429 }
430
431 static void first_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
432 {
433         TDB_DATA dbuf;
434         *pkey = tdb_firstkey(the_tdb);
435         
436         dbuf = tdb_fetch(the_tdb, *pkey);
437         if (!dbuf.dptr) terror("fetch failed");
438         else {
439                 print_rec(the_tdb, *pkey, dbuf, NULL);
440         }
441 }
442
443 static void next_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
444 {
445         TDB_DATA dbuf;
446         *pkey = tdb_nextkey(the_tdb, *pkey);
447         
448         dbuf = tdb_fetch(the_tdb, *pkey);
449         if (!dbuf.dptr) 
450                 terror("fetch failed");
451         else
452                 print_rec(the_tdb, *pkey, dbuf, NULL);
453 }
454
455 static int do_command(void)
456 {
457         COMMAND_TABLE *ctp = cmd_table;
458         enum commands mycmd = CMD_HELP;
459         int cmd_len;
460
461         if (cmdname && strlen(cmdname) == 0) {
462             mycmd = CMD_NEXT;
463         } else {
464             while (ctp->name) {
465                 cmd_len = strlen(ctp->name);
466                 if (strncmp(ctp->name,cmdname,cmd_len) == 0) {
467                         mycmd = ctp->cmd;
468                         break;
469                 }
470                 ctp++;
471             }
472         }
473
474         switch (mycmd) {
475         case CMD_CREATE_TDB:
476             bIterate = 0;
477             create_tdb(arg1);
478             return 0;
479         case CMD_OPEN_TDB:
480             bIterate = 0;
481             open_tdb(arg1);
482             return 0;
483         case CMD_SYSTEM:
484             /* Shell command */
485             system(arg1);
486             return 0;
487         case CMD_QUIT:
488             return 1;
489         default:
490             /* all the rest require a open database */
491             if (!tdb) {
492                 bIterate = 0;
493                 terror("database not open");
494                 help();
495                 return 0;
496             }
497             switch (mycmd) {
498             case CMD_ERASE:
499                 bIterate = 0;
500                 tdb_traverse(tdb, do_delete_fn, NULL);
501                 return 0;
502             case CMD_DUMP:
503                 bIterate = 0;
504                 tdb_traverse(tdb, print_rec, NULL);
505                 return 0;
506             case CMD_INSERT:
507                 bIterate = 0;
508                 insert_tdb(arg1, arg1len,arg2,arg2len);
509                 return 0;
510             case CMD_MOVE:
511                 bIterate = 0;
512                 move_rec(arg1,arg1len,arg2);
513                 return 0;
514             case CMD_STORE:
515                 bIterate = 0;
516                 store_tdb(arg1,arg1len,arg2,arg2len);
517                 return 0;
518             case CMD_SHOW:
519                 bIterate = 0;
520                 show_tdb(arg1, arg1len);
521                 return 0;
522             case CMD_KEYS:
523                 tdb_traverse(tdb, print_key, NULL);
524                 return 0;
525             case CMD_HEXKEYS:
526                 tdb_traverse(tdb, print_hexkey, NULL);
527                 return 0;
528             case CMD_DELETE:
529                 bIterate = 0;
530                 delete_tdb(arg1,arg1len);
531                 return 0;
532             case CMD_LIST_HASH_FREE:
533                 tdb_dump_all(tdb);
534                 return 0;
535             case CMD_LIST_FREE:
536                 tdb_printfreelist(tdb);
537                 return 0;
538             case CMD_INFO:
539                 info_tdb();
540                 return 0;
541             case CMD_SPEED:
542                 speed_tdb(arg1);
543                 return 0;
544             case CMD_MMAP:
545                 toggle_mmap();
546                 return 0;
547             case CMD_FIRST:
548                 bIterate = 1;
549                 first_record(tdb, &iterate_kbuf);
550                 return 0;
551             case CMD_NEXT:
552                if (bIterate)
553                   next_record(tdb, &iterate_kbuf);
554                 return 0;
555             case CMD_HELP:
556                 help();
557                 return 0;
558             case CMD_CREATE_TDB:
559             case CMD_OPEN_TDB:
560             case CMD_SYSTEM:
561             case CMD_QUIT:
562                 /*
563                  * unhandled commands.  cases included here to avoid compiler
564                  * warnings.
565                  */
566                 return 0;
567             }
568         }
569
570         return 0;
571 }
572
573 static char *convert_string(char *instring, size_t *sizep)
574 {
575     size_t length = 0;
576     char *outp, *inp;
577     char temp[3];
578     
579
580     outp = inp = instring;
581
582     while (*inp) {
583         if (*inp == '\\') {
584             inp++;
585             if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
586                 temp[0] = *inp++;
587                 temp[1] = '\0';
588                 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
589                     temp[1] = *inp++;
590                     temp[2] = '\0';
591                 }
592                 *outp++ = (char)strtol((const char *)temp,NULL,16);
593             } else {
594                 *outp++ = *inp++;
595             }
596         } else {
597             *outp++ = *inp++;
598         }
599         length++;
600     }
601     *sizep = length;
602     return instring;
603 }
604
605 int main(int argc, char *argv[])
606 {
607     cmdname = "";
608     arg1 = NULL;
609     arg1len = 0;
610     arg2 = NULL;
611     arg2len = 0;
612
613     if (argv[1]) {
614         cmdname = "open";
615         arg1 = argv[1];
616         do_command();
617         cmdname =  "";
618         arg1 = NULL;
619     }
620
621     switch (argc) {
622         case 1:
623         case 2:
624             /* Interactive mode */
625             while ((cmdname = tdb_getline("tdb> "))) {
626                 arg2 = arg1 = NULL;
627                 if ((arg1 = strchr((const char *)cmdname,' ')) != NULL) {
628                     arg1++;
629                     arg2 = arg1;
630                     while (*arg2) {
631                         if (*arg2 == ' ') {
632                             *arg2++ = '\0';
633                             break;
634                         }
635                         if ((*arg2++ == '\\') && (*arg2 == ' ')) {
636                             arg2++;
637                         }
638                     }
639                 }
640                 if (arg1) arg1 = convert_string(arg1,&arg1len);
641                 if (arg2) arg2 = convert_string(arg2,&arg2len);
642                 if (do_command()) break;
643             }
644             break;
645         case 5:
646             arg2 = convert_string(argv[4],&arg2len);
647         case 4:
648             arg1 = convert_string(argv[3],&arg1len);
649         case 3:
650             cmdname = argv[2];
651         default:
652             do_command();
653             break;
654     }
655
656     if (tdb) tdb_close(tdb);
657
658     return 0;
659 }