tdb:tdbtool: add transaction_start/_commit/_cancel commands.
[Samba/gbeck.git] / lib / tdb / tools / tdbtool.c
blob193140b801dae1e8973955b32090f28419198be9
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 /* a tdb tool for manipulating a tdb database */
120 static TDB_CONTEXT *tdb;
122 static int print_rec(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
123 static int print_key(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
124 static int print_hexkey(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
126 static void print_asc(const char *buf,int len)
128 int i;
130 /* We're probably printing ASCII strings so don't try to display
131 the trailing NULL character. */
133 if (buf[len - 1] == 0)
134 len--;
136 for (i=0;i<len;i++)
137 printf("%c",isprint(buf[i])?buf[i]:'.');
140 static void print_data(const char *buf,int len)
142 int i=0;
143 if (len<=0) return;
144 printf("[%03X] ",i);
145 for (i=0;i<len;) {
146 printf("%02X ",(int)((unsigned char)buf[i]));
147 i++;
148 if (i%8 == 0) printf(" ");
149 if (i%16 == 0) {
150 print_asc(&buf[i-16],8); printf(" ");
151 print_asc(&buf[i-8],8); printf("\n");
152 if (i<len) printf("[%03X] ",i);
155 if (i%16) {
156 int n;
158 n = 16 - (i%16);
159 printf(" ");
160 if (n>8) printf(" ");
161 while (n--) printf(" ");
163 n = i%16;
164 if (n > 8) n = 8;
165 print_asc(&buf[i-(i%16)],n); printf(" ");
166 n = (i%16) - n;
167 if (n>0) print_asc(&buf[i-n],n);
168 printf("\n");
172 static void help(void)
174 printf("\n"
175 "tdbtool: \n"
176 " create dbname : create a database\n"
177 " open dbname : open an existing database\n"
178 " transaction_start : start a transaction\n"
179 " transaction_commit : commit a transaction\n"
180 " transaction_cancel : cancel a transaction\n"
181 " erase : erase the database\n"
182 " dump : dump the database as strings\n"
183 " keys : dump the database keys as strings\n"
184 " hexkeys : dump the database keys as hex values\n"
185 " info : print summary info about the database\n"
186 " insert key data : insert a record\n"
187 " move key file : move a record to a destination tdb\n"
188 " store key data : store a record (replace)\n"
189 " show key : show a record by key\n"
190 " delete key : delete a record by key\n"
191 " list : print the database hash table and freelist\n"
192 " free : print the database freelist\n"
193 " check : check the integrity of an opened database\n"
194 " speed : perform speed tests on the database\n"
195 " ! command : execute system command\n"
196 " 1 | first : print the first record\n"
197 " n | next : print the next record\n"
198 " q | quit : terminate\n"
199 " \\n : repeat 'next' command\n"
200 "\n");
203 static void terror(const char *why)
205 printf("%s\n", why);
208 static void create_tdb(const char *tdbname)
210 if (tdb) tdb_close(tdb);
211 tdb = tdb_open(tdbname, 0, TDB_CLEAR_IF_FIRST | (disable_mmap?TDB_NOMMAP:0),
212 O_RDWR | O_CREAT | O_TRUNC, 0600);
213 if (!tdb) {
214 printf("Could not create %s: %s\n", tdbname, strerror(errno));
218 static void open_tdb(const char *tdbname)
220 if (tdb) tdb_close(tdb);
221 tdb = tdb_open(tdbname, 0, disable_mmap?TDB_NOMMAP:0, O_RDWR, 0600);
222 if (!tdb) {
223 printf("Could not open %s: %s\n", tdbname, strerror(errno));
227 static void insert_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
229 TDB_DATA key, dbuf;
231 if ((keyname == NULL) || (keylen == 0)) {
232 terror("need key");
233 return;
236 key.dptr = (unsigned char *)keyname;
237 key.dsize = keylen;
238 dbuf.dptr = (unsigned char *)data;
239 dbuf.dsize = datalen;
241 if (tdb_store(tdb, key, dbuf, TDB_INSERT) == -1) {
242 terror("insert failed");
246 static void store_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 if ((data == NULL) || (datalen == 0)) {
256 terror("need data");
257 return;
260 key.dptr = (unsigned char *)keyname;
261 key.dsize = keylen;
262 dbuf.dptr = (unsigned char *)data;
263 dbuf.dsize = datalen;
265 printf("Storing key:\n");
266 print_rec(tdb, key, dbuf, NULL);
268 if (tdb_store(tdb, key, dbuf, TDB_REPLACE) == -1) {
269 terror("store failed");
273 static void show_tdb(char *keyname, size_t keylen)
275 TDB_DATA key, dbuf;
277 if ((keyname == NULL) || (keylen == 0)) {
278 terror("need key");
279 return;
282 key.dptr = (unsigned char *)keyname;
283 key.dsize = keylen;
285 dbuf = tdb_fetch(tdb, key);
286 if (!dbuf.dptr) {
287 terror("fetch failed");
288 return;
291 print_rec(tdb, key, dbuf, NULL);
293 free( dbuf.dptr );
295 return;
298 static void delete_tdb(char *keyname, size_t keylen)
300 TDB_DATA key;
302 if ((keyname == NULL) || (keylen == 0)) {
303 terror("need key");
304 return;
307 key.dptr = (unsigned char *)keyname;
308 key.dsize = keylen;
310 if (tdb_delete(tdb, key) != 0) {
311 terror("delete failed");
315 static void move_rec(char *keyname, size_t keylen, char* tdbname)
317 TDB_DATA key, dbuf;
318 TDB_CONTEXT *dst_tdb;
320 if ((keyname == NULL) || (keylen == 0)) {
321 terror("need key");
322 return;
325 if ( !tdbname ) {
326 terror("need destination tdb name");
327 return;
330 key.dptr = (unsigned char *)keyname;
331 key.dsize = keylen;
333 dbuf = tdb_fetch(tdb, key);
334 if (!dbuf.dptr) {
335 terror("fetch failed");
336 return;
339 print_rec(tdb, key, dbuf, NULL);
341 dst_tdb = tdb_open(tdbname, 0, 0, O_RDWR, 0600);
342 if ( !dst_tdb ) {
343 terror("unable to open destination tdb");
344 return;
347 if ( tdb_store( dst_tdb, key, dbuf, TDB_REPLACE ) == -1 ) {
348 terror("failed to move record");
350 else
351 printf("record moved\n");
353 tdb_close( dst_tdb );
355 return;
358 static int print_rec(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
360 printf("\nkey %d bytes\n", (int)key.dsize);
361 print_asc((const char *)key.dptr, key.dsize);
362 printf("\ndata %d bytes\n", (int)dbuf.dsize);
363 print_data((const char *)dbuf.dptr, dbuf.dsize);
364 return 0;
367 static int print_key(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
369 printf("key %d bytes: ", (int)key.dsize);
370 print_asc((const char *)key.dptr, key.dsize);
371 printf("\n");
372 return 0;
375 static int print_hexkey(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
377 printf("key %d bytes\n", (int)key.dsize);
378 print_data((const char *)key.dptr, key.dsize);
379 printf("\n");
380 return 0;
383 static int total_bytes;
385 static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
387 total_bytes += dbuf.dsize;
388 return 0;
391 static void info_tdb(void)
393 int count;
394 total_bytes = 0;
395 if ((count = tdb_traverse(tdb, traverse_fn, NULL)) == -1)
396 printf("Error = %s\n", tdb_errorstr(tdb));
397 else
398 printf("%d records totalling %d bytes\n", count, total_bytes);
401 static void speed_tdb(const char *tlimit)
403 unsigned timelimit = tlimit?atoi(tlimit):0;
404 double t;
405 int ops;
406 if (timelimit == 0) timelimit = 5;
408 ops = 0;
409 printf("Testing store speed for %u seconds\n", timelimit);
410 _start_timer();
411 do {
412 long int r = random();
413 TDB_DATA key, dbuf;
414 key.dptr = (unsigned char *)"store test";
415 key.dsize = strlen((char *)key.dptr);
416 dbuf.dptr = (unsigned char *)&r;
417 dbuf.dsize = sizeof(r);
418 tdb_store(tdb, key, dbuf, TDB_REPLACE);
419 t = _end_timer();
420 ops++;
421 } while (t < timelimit);
422 printf("%10.3f ops/sec\n", ops/t);
424 ops = 0;
425 printf("Testing fetch speed for %u seconds\n", timelimit);
426 _start_timer();
427 do {
428 long int r = random();
429 TDB_DATA key, dbuf;
430 key.dptr = (unsigned char *)"store test";
431 key.dsize = strlen((char *)key.dptr);
432 dbuf.dptr = (unsigned char *)&r;
433 dbuf.dsize = sizeof(r);
434 tdb_fetch(tdb, key);
435 t = _end_timer();
436 ops++;
437 } while (t < timelimit);
438 printf("%10.3f ops/sec\n", ops/t);
440 ops = 0;
441 printf("Testing transaction speed for %u seconds\n", timelimit);
442 _start_timer();
443 do {
444 long int r = random();
445 TDB_DATA key, dbuf;
446 key.dptr = (unsigned char *)"transaction test";
447 key.dsize = strlen((char *)key.dptr);
448 dbuf.dptr = (unsigned char *)&r;
449 dbuf.dsize = sizeof(r);
450 tdb_transaction_start(tdb);
451 tdb_store(tdb, key, dbuf, TDB_REPLACE);
452 tdb_transaction_commit(tdb);
453 t = _end_timer();
454 ops++;
455 } while (t < timelimit);
456 printf("%10.3f ops/sec\n", ops/t);
458 ops = 0;
459 printf("Testing traverse speed for %u seconds\n", timelimit);
460 _start_timer();
461 do {
462 tdb_traverse(tdb, traverse_fn, NULL);
463 t = _end_timer();
464 ops++;
465 } while (t < timelimit);
466 printf("%10.3f ops/sec\n", ops/t);
469 static void toggle_mmap(void)
471 disable_mmap = !disable_mmap;
472 if (disable_mmap) {
473 printf("mmap is disabled\n");
474 } else {
475 printf("mmap is enabled\n");
479 static char *tdb_getline(const char *prompt)
481 static char thisline[1024];
482 char *p;
483 fputs(prompt, stdout);
484 thisline[0] = 0;
485 p = fgets(thisline, sizeof(thisline)-1, stdin);
486 if (p) p = strchr(p, '\n');
487 if (p) *p = 0;
488 return p?thisline:NULL;
491 static int do_delete_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf,
492 void *state)
494 return tdb_delete(the_tdb, key);
497 static void first_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
499 TDB_DATA dbuf;
500 *pkey = tdb_firstkey(the_tdb);
502 dbuf = tdb_fetch(the_tdb, *pkey);
503 if (!dbuf.dptr) terror("fetch failed");
504 else {
505 print_rec(the_tdb, *pkey, dbuf, NULL);
509 static void next_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
511 TDB_DATA dbuf;
512 *pkey = tdb_nextkey(the_tdb, *pkey);
514 dbuf = tdb_fetch(the_tdb, *pkey);
515 if (!dbuf.dptr)
516 terror("fetch failed");
517 else
518 print_rec(the_tdb, *pkey, dbuf, NULL);
521 static int test_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
523 return 0;
526 static void check_db(TDB_CONTEXT *the_tdb)
528 int tdbcount=-1;
529 if (the_tdb) {
530 tdbcount = tdb_traverse(the_tdb, test_fn, NULL);
531 } else {
532 printf("Error: No database opened!\n");
535 if (tdbcount<0) {
536 printf("Integrity check for the opened database failed.\n");
537 } else {
538 printf("Database integrity is OK and has %d records.\n", tdbcount);
542 static int do_command(void)
544 COMMAND_TABLE *ctp = cmd_table;
545 enum commands mycmd = CMD_HELP;
546 int cmd_len;
548 if (cmdname && strlen(cmdname) == 0) {
549 mycmd = CMD_NEXT;
550 } else {
551 while (ctp->name) {
552 cmd_len = strlen(ctp->name);
553 if (strncmp(ctp->name,cmdname,cmd_len) == 0) {
554 mycmd = ctp->cmd;
555 break;
557 ctp++;
561 switch (mycmd) {
562 case CMD_CREATE_TDB:
563 bIterate = 0;
564 create_tdb(arg1);
565 return 0;
566 case CMD_OPEN_TDB:
567 bIterate = 0;
568 open_tdb(arg1);
569 return 0;
570 case CMD_SYSTEM:
571 /* Shell command */
572 if (system(arg1) == -1) {
573 terror("system() call failed\n");
575 return 0;
576 case CMD_QUIT:
577 return 1;
578 default:
579 /* all the rest require a open database */
580 if (!tdb) {
581 bIterate = 0;
582 terror("database not open");
583 help();
584 return 0;
586 switch (mycmd) {
587 case CMD_TRANSACTION_START:
588 bIterate = 0;
589 tdb_transaction_start(tdb);
590 return 0;
591 case CMD_TRANSACTION_COMMIT:
592 bIterate = 0;
593 tdb_transaction_commit(tdb);
594 return 0;
595 case CMD_TRANSACTION_CANCEL:
596 bIterate = 0;
597 tdb_transaction_cancel(tdb);
598 return 0;
599 case CMD_ERASE:
600 bIterate = 0;
601 tdb_traverse(tdb, do_delete_fn, NULL);
602 return 0;
603 case CMD_DUMP:
604 bIterate = 0;
605 tdb_traverse(tdb, print_rec, NULL);
606 return 0;
607 case CMD_INSERT:
608 bIterate = 0;
609 insert_tdb(arg1, arg1len,arg2,arg2len);
610 return 0;
611 case CMD_MOVE:
612 bIterate = 0;
613 move_rec(arg1,arg1len,arg2);
614 return 0;
615 case CMD_STORE:
616 bIterate = 0;
617 store_tdb(arg1,arg1len,arg2,arg2len);
618 return 0;
619 case CMD_SHOW:
620 bIterate = 0;
621 show_tdb(arg1, arg1len);
622 return 0;
623 case CMD_KEYS:
624 tdb_traverse(tdb, print_key, NULL);
625 return 0;
626 case CMD_HEXKEYS:
627 tdb_traverse(tdb, print_hexkey, NULL);
628 return 0;
629 case CMD_DELETE:
630 bIterate = 0;
631 delete_tdb(arg1,arg1len);
632 return 0;
633 case CMD_LIST_HASH_FREE:
634 tdb_dump_all(tdb);
635 return 0;
636 case CMD_LIST_FREE:
637 tdb_printfreelist(tdb);
638 return 0;
639 case CMD_INFO:
640 info_tdb();
641 return 0;
642 case CMD_SPEED:
643 speed_tdb(arg1);
644 return 0;
645 case CMD_MMAP:
646 toggle_mmap();
647 return 0;
648 case CMD_FIRST:
649 bIterate = 1;
650 first_record(tdb, &iterate_kbuf);
651 return 0;
652 case CMD_NEXT:
653 if (bIterate)
654 next_record(tdb, &iterate_kbuf);
655 return 0;
656 case CMD_CHECK:
657 check_db(tdb);
658 return 0;
659 case CMD_HELP:
660 help();
661 return 0;
662 case CMD_CREATE_TDB:
663 case CMD_OPEN_TDB:
664 case CMD_SYSTEM:
665 case CMD_QUIT:
667 * unhandled commands. cases included here to avoid compiler
668 * warnings.
670 return 0;
674 return 0;
677 static char *convert_string(char *instring, size_t *sizep)
679 size_t length = 0;
680 char *outp, *inp;
681 char temp[3];
684 outp = inp = instring;
686 while (*inp) {
687 if (*inp == '\\') {
688 inp++;
689 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
690 temp[0] = *inp++;
691 temp[1] = '\0';
692 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
693 temp[1] = *inp++;
694 temp[2] = '\0';
696 *outp++ = (char)strtol((const char *)temp,NULL,16);
697 } else {
698 *outp++ = *inp++;
700 } else {
701 *outp++ = *inp++;
703 length++;
705 *sizep = length;
706 return instring;
709 int main(int argc, char *argv[])
711 cmdname = "";
712 arg1 = NULL;
713 arg1len = 0;
714 arg2 = NULL;
715 arg2len = 0;
717 if (argv[1]) {
718 cmdname = "open";
719 arg1 = argv[1];
720 do_command();
721 cmdname = "";
722 arg1 = NULL;
725 switch (argc) {
726 case 1:
727 case 2:
728 /* Interactive mode */
729 while ((cmdname = tdb_getline("tdb> "))) {
730 arg2 = arg1 = NULL;
731 if ((arg1 = strchr((const char *)cmdname,' ')) != NULL) {
732 arg1++;
733 arg2 = arg1;
734 while (*arg2) {
735 if (*arg2 == ' ') {
736 *arg2++ = '\0';
737 break;
739 if ((*arg2++ == '\\') && (*arg2 == ' ')) {
740 arg2++;
744 if (arg1) arg1 = convert_string(arg1,&arg1len);
745 if (arg2) arg2 = convert_string(arg2,&arg2len);
746 if (do_command()) break;
748 break;
749 case 5:
750 arg2 = convert_string(argv[4],&arg2len);
751 case 4:
752 arg1 = convert_string(argv[3],&arg1len);
753 case 3:
754 cmdname = argv[2];
755 default:
756 do_command();
757 break;
760 if (tdb) tdb_close(tdb);
762 return 0;