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