Remove unused variable.
[Samba/gebeck_regimport.git] / lib / tdb / tools / tdbtool.c
blobd93f742d1c2cfc14db886d6ff992e914d592e541
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
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.
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.
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/>.
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"
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;
40 enum commands {
41 CMD_CREATE_TDB,
42 CMD_OPEN_TDB,
43 CMD_TRANSACTION_START,
44 CMD_TRANSACTION_COMMIT,
45 CMD_TRANSACTION_CANCEL,
46 CMD_ERASE,
47 CMD_DUMP,
48 CMD_INSERT,
49 CMD_MOVE,
50 CMD_STORE,
51 CMD_SHOW,
52 CMD_KEYS,
53 CMD_HEXKEYS,
54 CMD_DELETE,
55 CMD_LIST_HASH_FREE,
56 CMD_LIST_FREE,
57 CMD_INFO,
58 CMD_MMAP,
59 CMD_SPEED,
60 CMD_FIRST,
61 CMD_NEXT,
62 CMD_SYSTEM,
63 CMD_CHECK,
64 CMD_QUIT,
65 CMD_HELP
68 typedef struct {
69 const char *name;
70 enum commands cmd;
71 } COMMAND_TABLE;
73 COMMAND_TABLE cmd_table[] = {
74 {"create", CMD_CREATE_TDB},
75 {"open", CMD_OPEN_TDB},
76 {"transaction_start", CMD_TRANSACTION_START},
77 {"transaction_commit", CMD_TRANSACTION_COMMIT},
78 {"transaction_cancel", CMD_TRANSACTION_CANCEL},
79 {"erase", CMD_ERASE},
80 {"dump", CMD_DUMP},
81 {"insert", CMD_INSERT},
82 {"move", CMD_MOVE},
83 {"store", CMD_STORE},
84 {"show", CMD_SHOW},
85 {"keys", CMD_KEYS},
86 {"hexkeys", CMD_HEXKEYS},
87 {"delete", CMD_DELETE},
88 {"list", CMD_LIST_HASH_FREE},
89 {"free", CMD_LIST_FREE},
90 {"info", CMD_INFO},
91 {"speed", CMD_SPEED},
92 {"mmap", CMD_MMAP},
93 {"first", CMD_FIRST},
94 {"1", CMD_FIRST},
95 {"next", CMD_NEXT},
96 {"n", CMD_NEXT},
97 {"check", CMD_CHECK},
98 {"quit", CMD_QUIT},
99 {"q", CMD_QUIT},
100 {"!", CMD_SYSTEM},
101 {NULL, CMD_HELP}
104 struct timeval tp1,tp2;
106 static void _start_timer(void)
108 gettimeofday(&tp1,NULL);
111 static double _end_timer(void)
113 gettimeofday(&tp2,NULL);
114 return((tp2.tv_sec - tp1.tv_sec) +
115 (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
118 #ifdef PRINTF_ATTRIBUTE
119 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...) PRINTF_ATTRIBUTE(3,4);
120 #endif
121 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...)
123 va_list ap;
125 va_start(ap, format);
126 vfprintf(stderr, format, ap);
127 va_end(ap);
130 /* a tdb tool for manipulating a tdb database */
132 static TDB_CONTEXT *tdb;
134 static int print_rec(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
135 static int print_key(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
136 static int print_hexkey(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
138 static void print_asc(const char *buf,int len)
140 int i;
142 /* We're probably printing ASCII strings so don't try to display
143 the trailing NULL character. */
145 if (buf[len - 1] == 0)
146 len--;
148 for (i=0;i<len;i++)
149 printf("%c",isprint(buf[i])?buf[i]:'.');
152 static void print_data(const char *buf,int len)
154 int i=0;
155 if (len<=0) return;
156 printf("[%03X] ",i);
157 for (i=0;i<len;) {
158 printf("%02X ",(int)((unsigned char)buf[i]));
159 i++;
160 if (i%8 == 0) printf(" ");
161 if (i%16 == 0) {
162 print_asc(&buf[i-16],8); printf(" ");
163 print_asc(&buf[i-8],8); printf("\n");
164 if (i<len) printf("[%03X] ",i);
167 if (i%16) {
168 int n;
170 n = 16 - (i%16);
171 printf(" ");
172 if (n>8) printf(" ");
173 while (n--) printf(" ");
175 n = i%16;
176 if (n > 8) n = 8;
177 print_asc(&buf[i-(i%16)],n); printf(" ");
178 n = (i%16) - n;
179 if (n>0) print_asc(&buf[i-n],n);
180 printf("\n");
184 static void help(void)
186 printf("\n"
187 "tdbtool: \n"
188 " create dbname : create a database\n"
189 " open dbname : open an existing database\n"
190 " transaction_start : start a transaction\n"
191 " transaction_commit : commit a transaction\n"
192 " transaction_cancel : cancel a transaction\n"
193 " erase : erase the database\n"
194 " dump : dump the database as strings\n"
195 " keys : dump the database keys as strings\n"
196 " hexkeys : dump the database keys as hex values\n"
197 " info : print summary info about the database\n"
198 " insert key data : insert a record\n"
199 " move key file : move a record to a destination tdb\n"
200 " store key data : store a record (replace)\n"
201 " show key : show a record by key\n"
202 " delete key : delete a record by key\n"
203 " list : print the database hash table and freelist\n"
204 " free : print the database freelist\n"
205 " check : check the integrity of an opened database\n"
206 " speed : perform speed tests on the database\n"
207 " ! command : execute system command\n"
208 " 1 | first : print the first record\n"
209 " n | next : print the next record\n"
210 " q | quit : terminate\n"
211 " \\n : repeat 'next' command\n"
212 "\n");
215 static void terror(const char *why)
217 printf("%s\n", why);
220 static void create_tdb(const char *tdbname)
222 struct tdb_logging_context log_ctx;
223 log_ctx.log_fn = tdb_log;
225 if (tdb) tdb_close(tdb);
226 tdb = tdb_open_ex(tdbname, 0, TDB_CLEAR_IF_FIRST | (disable_mmap?TDB_NOMMAP:0),
227 O_RDWR | O_CREAT | O_TRUNC, 0600, &log_ctx, NULL);
228 if (!tdb) {
229 printf("Could not create %s: %s\n", tdbname, strerror(errno));
233 static void open_tdb(const char *tdbname)
235 struct tdb_logging_context log_ctx;
236 log_ctx.log_fn = tdb_log;
238 if (tdb) tdb_close(tdb);
239 tdb = tdb_open_ex(tdbname, 0, disable_mmap?TDB_NOMMAP:0, O_RDWR, 0600,
240 &log_ctx, NULL);
241 if (!tdb) {
242 printf("Could not open %s: %s\n", tdbname, strerror(errno));
246 static void insert_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
248 TDB_DATA key, dbuf;
250 if ((keyname == NULL) || (keylen == 0)) {
251 terror("need key");
252 return;
255 key.dptr = (unsigned char *)keyname;
256 key.dsize = keylen;
257 dbuf.dptr = (unsigned char *)data;
258 dbuf.dsize = datalen;
260 if (tdb_store(tdb, key, dbuf, TDB_INSERT) != 0) {
261 terror("insert failed");
265 static void store_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
267 TDB_DATA key, dbuf;
269 if ((keyname == NULL) || (keylen == 0)) {
270 terror("need key");
271 return;
274 if ((data == NULL) || (datalen == 0)) {
275 terror("need data");
276 return;
279 key.dptr = (unsigned char *)keyname;
280 key.dsize = keylen;
281 dbuf.dptr = (unsigned char *)data;
282 dbuf.dsize = datalen;
284 printf("Storing key:\n");
285 print_rec(tdb, key, dbuf, NULL);
287 if (tdb_store(tdb, key, dbuf, TDB_REPLACE) != 0) {
288 terror("store failed");
292 static void show_tdb(char *keyname, size_t keylen)
294 TDB_DATA key, dbuf;
296 if ((keyname == NULL) || (keylen == 0)) {
297 terror("need key");
298 return;
301 key.dptr = (unsigned char *)keyname;
302 key.dsize = keylen;
304 dbuf = tdb_fetch(tdb, key);
305 if (!dbuf.dptr) {
306 terror("fetch failed");
307 return;
310 print_rec(tdb, key, dbuf, NULL);
312 free( dbuf.dptr );
314 return;
317 static void delete_tdb(char *keyname, size_t keylen)
319 TDB_DATA key;
321 if ((keyname == NULL) || (keylen == 0)) {
322 terror("need key");
323 return;
326 key.dptr = (unsigned char *)keyname;
327 key.dsize = keylen;
329 if (tdb_delete(tdb, key) != 0) {
330 terror("delete failed");
334 static void move_rec(char *keyname, size_t keylen, char* tdbname)
336 TDB_DATA key, dbuf;
337 TDB_CONTEXT *dst_tdb;
339 if ((keyname == NULL) || (keylen == 0)) {
340 terror("need key");
341 return;
344 if ( !tdbname ) {
345 terror("need destination tdb name");
346 return;
349 key.dptr = (unsigned char *)keyname;
350 key.dsize = keylen;
352 dbuf = tdb_fetch(tdb, key);
353 if (!dbuf.dptr) {
354 terror("fetch failed");
355 return;
358 print_rec(tdb, key, dbuf, NULL);
360 dst_tdb = tdb_open(tdbname, 0, 0, O_RDWR, 0600);
361 if ( !dst_tdb ) {
362 terror("unable to open destination tdb");
363 return;
366 if (tdb_store( dst_tdb, key, dbuf, TDB_REPLACE ) != 0) {
367 terror("failed to move record");
369 else
370 printf("record moved\n");
372 tdb_close( dst_tdb );
374 return;
377 static int print_rec(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
379 printf("\nkey %d bytes\n", (int)key.dsize);
380 print_asc((const char *)key.dptr, key.dsize);
381 printf("\ndata %d bytes\n", (int)dbuf.dsize);
382 print_data((const char *)dbuf.dptr, dbuf.dsize);
383 return 0;
386 static int print_key(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
388 printf("key %d bytes: ", (int)key.dsize);
389 print_asc((const char *)key.dptr, key.dsize);
390 printf("\n");
391 return 0;
394 static int print_hexkey(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
396 printf("key %d bytes\n", (int)key.dsize);
397 print_data((const char *)key.dptr, key.dsize);
398 printf("\n");
399 return 0;
402 static int total_bytes;
404 static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
406 total_bytes += dbuf.dsize;
407 return 0;
410 static void info_tdb(void)
412 char *summary = tdb_summary(tdb);
414 if (!summary) {
415 printf("Error = %s\n", tdb_errorstr(tdb));
416 } else {
417 printf("%s", summary);
418 free(summary);
422 static void speed_tdb(const char *tlimit)
424 const char *str = "store test", *str2 = "transaction test";
425 unsigned timelimit = tlimit?atoi(tlimit):0;
426 double t;
427 int ops;
428 if (timelimit == 0) timelimit = 5;
430 ops = 0;
431 printf("Testing store speed for %u seconds\n", timelimit);
432 _start_timer();
433 do {
434 long int r = random();
435 TDB_DATA key, dbuf;
436 key.dptr = discard_const_p(uint8_t, str);
437 key.dsize = strlen((char *)key.dptr);
438 dbuf.dptr = (uint8_t *) &r;
439 dbuf.dsize = sizeof(r);
440 tdb_store(tdb, key, dbuf, TDB_REPLACE);
441 t = _end_timer();
442 ops++;
443 } while (t < timelimit);
444 printf("%10.3f ops/sec\n", ops/t);
446 ops = 0;
447 printf("Testing fetch speed for %u seconds\n", timelimit);
448 _start_timer();
449 do {
450 TDB_DATA key;
451 key.dptr = discard_const_p(uint8_t, str);
452 key.dsize = strlen((char *)key.dptr);
453 tdb_fetch(tdb, key);
454 t = _end_timer();
455 ops++;
456 } while (t < timelimit);
457 printf("%10.3f ops/sec\n", ops/t);
459 ops = 0;
460 printf("Testing transaction speed for %u seconds\n", timelimit);
461 _start_timer();
462 do {
463 long int r = random();
464 TDB_DATA key, dbuf;
465 key.dptr = discard_const_p(uint8_t, str2);
466 key.dsize = strlen((char *)key.dptr);
467 dbuf.dptr = (uint8_t *) &r;
468 dbuf.dsize = sizeof(r);
469 tdb_transaction_start(tdb);
470 tdb_store(tdb, key, dbuf, TDB_REPLACE);
471 tdb_transaction_commit(tdb);
472 t = _end_timer();
473 ops++;
474 } while (t < timelimit);
475 printf("%10.3f ops/sec\n", ops/t);
477 ops = 0;
478 printf("Testing traverse speed for %u seconds\n", timelimit);
479 _start_timer();
480 do {
481 tdb_traverse(tdb, traverse_fn, NULL);
482 t = _end_timer();
483 ops++;
484 } while (t < timelimit);
485 printf("%10.3f ops/sec\n", ops/t);
488 static void toggle_mmap(void)
490 disable_mmap = !disable_mmap;
491 if (disable_mmap) {
492 printf("mmap is disabled\n");
493 } else {
494 printf("mmap is enabled\n");
498 static char *tdb_getline(const char *prompt)
500 static char thisline[1024];
501 char *p;
502 fputs(prompt, stdout);
503 thisline[0] = 0;
504 p = fgets(thisline, sizeof(thisline)-1, stdin);
505 if (p) p = strchr(p, '\n');
506 if (p) *p = 0;
507 return p?thisline:NULL;
510 static int do_delete_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf,
511 void *state)
513 return tdb_delete(the_tdb, key);
516 static void first_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
518 TDB_DATA dbuf;
519 *pkey = tdb_firstkey(the_tdb);
521 dbuf = tdb_fetch(the_tdb, *pkey);
522 if (!dbuf.dptr) terror("fetch failed");
523 else {
524 print_rec(the_tdb, *pkey, dbuf, NULL);
528 static void next_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
530 TDB_DATA dbuf;
531 *pkey = tdb_nextkey(the_tdb, *pkey);
533 dbuf = tdb_fetch(the_tdb, *pkey);
534 if (!dbuf.dptr)
535 terror("fetch failed");
536 else
537 print_rec(the_tdb, *pkey, dbuf, NULL);
540 static int count(TDB_DATA key, TDB_DATA data, void *private_data)
542 (*(unsigned int *)private_data)++;
543 return 0;
546 static void check_db(TDB_CONTEXT *the_tdb)
548 int tdbcount = 0;
549 if (!the_tdb)
550 printf("Error: No database opened!\n");
551 else if (tdb_check(the_tdb, count, &tdbcount) == -1)
552 printf("Integrity check for the opened database failed.\n");
553 else
554 printf("Database integrity is OK and has %d records.\n",
555 tdbcount);
558 static int do_command(void)
560 COMMAND_TABLE *ctp = cmd_table;
561 enum commands mycmd = CMD_HELP;
562 int cmd_len;
564 if (cmdname && strlen(cmdname) == 0) {
565 mycmd = CMD_NEXT;
566 } else {
567 while (ctp->name) {
568 cmd_len = strlen(ctp->name);
569 if (strncmp(ctp->name,cmdname,cmd_len) == 0) {
570 mycmd = ctp->cmd;
571 break;
573 ctp++;
577 switch (mycmd) {
578 case CMD_CREATE_TDB:
579 bIterate = 0;
580 create_tdb(arg1);
581 return 0;
582 case CMD_OPEN_TDB:
583 bIterate = 0;
584 open_tdb(arg1);
585 return 0;
586 case CMD_SYSTEM:
587 /* Shell command */
588 if (system(arg1) == -1) {
589 terror("system() call failed\n");
591 return 0;
592 case CMD_QUIT:
593 return 1;
594 default:
595 /* all the rest require a open database */
596 if (!tdb) {
597 bIterate = 0;
598 terror("database not open");
599 help();
600 return 0;
602 switch (mycmd) {
603 case CMD_TRANSACTION_START:
604 bIterate = 0;
605 tdb_transaction_start(tdb);
606 return 0;
607 case CMD_TRANSACTION_COMMIT:
608 bIterate = 0;
609 tdb_transaction_commit(tdb);
610 return 0;
611 case CMD_TRANSACTION_CANCEL:
612 bIterate = 0;
613 tdb_transaction_cancel(tdb);
614 return 0;
615 case CMD_ERASE:
616 bIterate = 0;
617 tdb_traverse(tdb, do_delete_fn, NULL);
618 return 0;
619 case CMD_DUMP:
620 bIterate = 0;
621 tdb_traverse(tdb, print_rec, NULL);
622 return 0;
623 case CMD_INSERT:
624 bIterate = 0;
625 insert_tdb(arg1, arg1len,arg2,arg2len);
626 return 0;
627 case CMD_MOVE:
628 bIterate = 0;
629 move_rec(arg1,arg1len,arg2);
630 return 0;
631 case CMD_STORE:
632 bIterate = 0;
633 store_tdb(arg1,arg1len,arg2,arg2len);
634 return 0;
635 case CMD_SHOW:
636 bIterate = 0;
637 show_tdb(arg1, arg1len);
638 return 0;
639 case CMD_KEYS:
640 tdb_traverse(tdb, print_key, NULL);
641 return 0;
642 case CMD_HEXKEYS:
643 tdb_traverse(tdb, print_hexkey, NULL);
644 return 0;
645 case CMD_DELETE:
646 bIterate = 0;
647 delete_tdb(arg1,arg1len);
648 return 0;
649 case CMD_LIST_HASH_FREE:
650 tdb_dump_all(tdb);
651 return 0;
652 case CMD_LIST_FREE:
653 tdb_printfreelist(tdb);
654 return 0;
655 case CMD_INFO:
656 info_tdb();
657 return 0;
658 case CMD_SPEED:
659 speed_tdb(arg1);
660 return 0;
661 case CMD_MMAP:
662 toggle_mmap();
663 return 0;
664 case CMD_FIRST:
665 bIterate = 1;
666 first_record(tdb, &iterate_kbuf);
667 return 0;
668 case CMD_NEXT:
669 if (bIterate)
670 next_record(tdb, &iterate_kbuf);
671 return 0;
672 case CMD_CHECK:
673 check_db(tdb);
674 return 0;
675 case CMD_HELP:
676 help();
677 return 0;
678 case CMD_CREATE_TDB:
679 case CMD_OPEN_TDB:
680 case CMD_SYSTEM:
681 case CMD_QUIT:
683 * unhandled commands. cases included here to avoid compiler
684 * warnings.
686 return 0;
690 return 0;
693 static char *tdb_convert_string(char *instring, size_t *sizep)
695 size_t length = 0;
696 char *outp, *inp;
697 char temp[3];
699 outp = inp = instring;
701 while (*inp) {
702 if (*inp == '\\') {
703 inp++;
704 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
705 temp[0] = *inp++;
706 temp[1] = '\0';
707 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
708 temp[1] = *inp++;
709 temp[2] = '\0';
711 *outp++ = (char)strtol((const char *)temp,NULL,16);
712 } else {
713 *outp++ = *inp++;
715 } else {
716 *outp++ = *inp++;
718 length++;
720 *sizep = length;
721 return instring;
724 int main(int argc, char *argv[])
726 cmdname = "";
727 arg1 = NULL;
728 arg1len = 0;
729 arg2 = NULL;
730 arg2len = 0;
732 if (argv[1]) {
733 cmdname = "open";
734 arg1 = argv[1];
735 do_command();
736 cmdname = "";
737 arg1 = NULL;
740 switch (argc) {
741 case 1:
742 case 2:
743 /* Interactive mode */
744 while ((cmdname = tdb_getline("tdb> "))) {
745 arg2 = arg1 = NULL;
746 if ((arg1 = strchr((const char *)cmdname,' ')) != NULL) {
747 arg1++;
748 arg2 = arg1;
749 while (*arg2) {
750 if (*arg2 == ' ') {
751 *arg2++ = '\0';
752 break;
754 if ((*arg2++ == '\\') && (*arg2 == ' ')) {
755 arg2++;
759 if (arg1) arg1 = tdb_convert_string(arg1,&arg1len);
760 if (arg2) arg2 = tdb_convert_string(arg2,&arg2len);
761 if (do_command()) break;
763 break;
764 case 5:
765 arg2 = tdb_convert_string(argv[4],&arg2len);
766 case 4:
767 arg1 = tdb_convert_string(argv[3],&arg1len);
768 case 3:
769 cmdname = argv[2];
770 default:
771 do_command();
772 break;
775 if (tdb) tdb_close(tdb);
777 return 0;