source: trunk/server/lib/tdb/tools/tdbtool.c

Last change on this file was 987, checked in by Silvan Scherrer, 9 years ago

samba server: fix crlf in tdb trunk code

File size: 16.7 KB
Line 
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
30static int do_command(void);
31const char *cmdname;
32char *arg1, *arg2;
33size_t arg1len, arg2len;
34int bIterate = 0;
35char *line;
36TDB_DATA iterate_kbuf;
37char cmdline[1024];
38static int disable_mmap;
39
40enum 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
66};
67
68typedef struct {
69 const char *name;
70 enum commands cmd;
71} COMMAND_TABLE;
72
73COMMAND_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}
102};
103
104struct timeval tp1,tp2;
105
106static void _start_timer(void)
107{
108 gettimeofday(&tp1,NULL);
109}
110
111static double _end_timer(void)
112{
113 gettimeofday(&tp2,NULL);
114 return((tp2.tv_sec - tp1.tv_sec) +
115 (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
116}
117
118#ifdef PRINTF_ATTRIBUTE
119static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...) PRINTF_ATTRIBUTE(3,4);
120#endif
121static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...)
122{
123 va_list ap;
124
125 va_start(ap, format);
126 vfprintf(stderr, format, ap);
127 va_end(ap);
128}
129
130/* a tdb tool for manipulating a tdb database */
131
132static TDB_CONTEXT *tdb;
133
134static int print_rec(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
135static int print_key(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
136static int print_hexkey(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
137
138static void print_asc(const char *buf,int len)
139{
140 int i;
141
142 /* We're probably printing ASCII strings so don't try to display
143 the trailing NULL character. */
144
145 if (buf[len - 1] == 0)
146 len--;
147
148 for (i=0;i<len;i++)
149 printf("%c",isprint(buf[i])?buf[i]:'.');
150}
151
152static void print_data(const char *buf,int len)
153{
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);
165 }
166 }
167 if (i%16) {
168 int n;
169
170 n = 16 - (i%16);
171 printf(" ");
172 if (n>8) printf(" ");
173 while (n--) printf(" ");
174
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");
181 }
182}
183
184static void help(void)
185{
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");
213}
214
215static void terror(const char *why)
216{
217 printf("%s\n", why);
218}
219
220static void create_tdb(const char *tdbname)
221{
222 struct tdb_logging_context log_ctx;
223 log_ctx.log_fn = tdb_log;
224
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));
230 }
231}
232
233static void open_tdb(const char *tdbname)
234{
235 struct tdb_logging_context log_ctx;
236 log_ctx.log_fn = tdb_log;
237
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));
243 }
244}
245
246static void insert_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
247{
248 TDB_DATA key, dbuf;
249
250 if ((keyname == NULL) || (keylen == 0)) {
251 terror("need key");
252 return;
253 }
254
255 key.dptr = (unsigned char *)keyname;
256 key.dsize = keylen;
257 dbuf.dptr = (unsigned char *)data;
258 dbuf.dsize = datalen;
259
260 if (tdb_store(tdb, key, dbuf, TDB_INSERT) == -1) {
261 terror("insert failed");
262 }
263}
264
265static void store_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
266{
267 TDB_DATA key, dbuf;
268
269 if ((keyname == NULL) || (keylen == 0)) {
270 terror("need key");
271 return;
272 }
273
274 if ((data == NULL) || (datalen == 0)) {
275 terror("need data");
276 return;
277 }
278
279 key.dptr = (unsigned char *)keyname;
280 key.dsize = keylen;
281 dbuf.dptr = (unsigned char *)data;
282 dbuf.dsize = datalen;
283
284 printf("Storing key:\n");
285 print_rec(tdb, key, dbuf, NULL);
286
287 if (tdb_store(tdb, key, dbuf, TDB_REPLACE) == -1) {
288 terror("store failed");
289 }
290}
291
292static void show_tdb(char *keyname, size_t keylen)
293{
294 TDB_DATA key, dbuf;
295
296 if ((keyname == NULL) || (keylen == 0)) {
297 terror("need key");
298 return;
299 }
300
301 key.dptr = (unsigned char *)keyname;
302 key.dsize = keylen;
303
304 dbuf = tdb_fetch(tdb, key);
305 if (!dbuf.dptr) {
306 terror("fetch failed");
307 return;
308 }
309
310 print_rec(tdb, key, dbuf, NULL);
311
312 free( dbuf.dptr );
313
314 return;
315}
316
317static void delete_tdb(char *keyname, size_t keylen)
318{
319 TDB_DATA key;
320
321 if ((keyname == NULL) || (keylen == 0)) {
322 terror("need key");
323 return;
324 }
325
326 key.dptr = (unsigned char *)keyname;
327 key.dsize = keylen;
328
329 if (tdb_delete(tdb, key) != 0) {
330 terror("delete failed");
331 }
332}
333
334static void move_rec(char *keyname, size_t keylen, char* tdbname)
335{
336 TDB_DATA key, dbuf;
337 TDB_CONTEXT *dst_tdb;
338
339 if ((keyname == NULL) || (keylen == 0)) {
340 terror("need key");
341 return;
342 }
343
344 if ( !tdbname ) {
345 terror("need destination tdb name");
346 return;
347 }
348
349 key.dptr = (unsigned char *)keyname;
350 key.dsize = keylen;
351
352 dbuf = tdb_fetch(tdb, key);
353 if (!dbuf.dptr) {
354 terror("fetch failed");
355 return;
356 }
357
358 print_rec(tdb, key, dbuf, NULL);
359
360 dst_tdb = tdb_open(tdbname, 0, 0, O_RDWR, 0600);
361 if ( !dst_tdb ) {
362 terror("unable to open destination tdb");
363 return;
364 }
365
366 if ( tdb_store( dst_tdb, key, dbuf, TDB_REPLACE ) == -1 ) {
367 terror("failed to move record");
368 }
369 else
370 printf("record moved\n");
371
372 tdb_close( dst_tdb );
373
374 return;
375}
376
377static int print_rec(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
378{
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;
384}
385
386static int print_key(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
387{
388 printf("key %d bytes: ", (int)key.dsize);
389 print_asc((const char *)key.dptr, key.dsize);
390 printf("\n");
391 return 0;
392}
393
394static int print_hexkey(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
395{
396 printf("key %d bytes\n", (int)key.dsize);
397 print_data((const char *)key.dptr, key.dsize);
398 printf("\n");
399 return 0;
400}
401
402static int total_bytes;
403
404static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
405{
406 total_bytes += dbuf.dsize;
407 return 0;
408}
409
410static void info_tdb(void)
411{
412 char *summary = tdb_summary(tdb);
413
414 if (!summary) {
415 printf("Error = %s\n", tdb_errorstr(tdb));
416 } else {
417 printf("%s", summary);
418 free(summary);
419 }
420}
421
422static void speed_tdb(const char *tlimit)
423{
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;
429
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);
445
446 ops = 0;
447 printf("Testing fetch speed for %u seconds\n", timelimit);
448 _start_timer();
449 do {
450 long int r = random();
451 TDB_DATA key, dbuf;
452 key.dptr = discard_const_p(uint8_t, str);
453 key.dsize = strlen((char *)key.dptr);
454 dbuf.dptr = (uint8_t *) &r;
455 dbuf.dsize = sizeof(r);
456 tdb_fetch(tdb, key);
457 t = _end_timer();
458 ops++;
459 } while (t < timelimit);
460 printf("%10.3f ops/sec\n", ops/t);
461
462 ops = 0;
463 printf("Testing transaction speed for %u seconds\n", timelimit);
464 _start_timer();
465 do {
466 long int r = random();
467 TDB_DATA key, dbuf;
468 key.dptr = discard_const_p(uint8_t, str2);
469 key.dsize = strlen((char *)key.dptr);
470 dbuf.dptr = (uint8_t *) &r;
471 dbuf.dsize = sizeof(r);
472 tdb_transaction_start(tdb);
473 tdb_store(tdb, key, dbuf, TDB_REPLACE);
474 tdb_transaction_commit(tdb);
475 t = _end_timer();
476 ops++;
477 } while (t < timelimit);
478 printf("%10.3f ops/sec\n", ops/t);
479
480 ops = 0;
481 printf("Testing traverse speed for %u seconds\n", timelimit);
482 _start_timer();
483 do {
484 tdb_traverse(tdb, traverse_fn, NULL);
485 t = _end_timer();
486 ops++;
487 } while (t < timelimit);
488 printf("%10.3f ops/sec\n", ops/t);
489}
490
491static void toggle_mmap(void)
492{
493 disable_mmap = !disable_mmap;
494 if (disable_mmap) {
495 printf("mmap is disabled\n");
496 } else {
497 printf("mmap is enabled\n");
498 }
499}
500
501static char *tdb_getline(const char *prompt)
502{
503 static char thisline[1024];
504 char *p;
505 fputs(prompt, stdout);
506 thisline[0] = 0;
507 p = fgets(thisline, sizeof(thisline)-1, stdin);
508 if (p) p = strchr(p, '\n');
509 if (p) *p = 0;
510 return p?thisline:NULL;
511}
512
513static int do_delete_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf,
514 void *state)
515{
516 return tdb_delete(the_tdb, key);
517}
518
519static void first_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
520{
521 TDB_DATA dbuf;
522 *pkey = tdb_firstkey(the_tdb);
523
524 dbuf = tdb_fetch(the_tdb, *pkey);
525 if (!dbuf.dptr) terror("fetch failed");
526 else {
527 print_rec(the_tdb, *pkey, dbuf, NULL);
528 }
529}
530
531static void next_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
532{
533 TDB_DATA dbuf;
534 *pkey = tdb_nextkey(the_tdb, *pkey);
535
536 dbuf = tdb_fetch(the_tdb, *pkey);
537 if (!dbuf.dptr)
538 terror("fetch failed");
539 else
540 print_rec(the_tdb, *pkey, dbuf, NULL);
541}
542
543static int count(TDB_DATA key, TDB_DATA data, void *private_data)
544{
545 (*(unsigned int *)private_data)++;
546 return 0;
547}
548
549static void check_db(TDB_CONTEXT *the_tdb)
550{
551 int tdbcount = 0;
552 if (!the_tdb)
553 printf("Error: No database opened!\n");
554 else if (tdb_check(the_tdb, count, &tdbcount) == -1)
555 printf("Integrity check for the opened database failed.\n");
556 else
557 printf("Database integrity is OK and has %d records.\n",
558 tdbcount);
559}
560
561static int do_command(void)
562{
563 COMMAND_TABLE *ctp = cmd_table;
564 enum commands mycmd = CMD_HELP;
565 int cmd_len;
566
567 if (cmdname && strlen(cmdname) == 0) {
568 mycmd = CMD_NEXT;
569 } else {
570 while (ctp->name) {
571 cmd_len = strlen(ctp->name);
572 if (strncmp(ctp->name,cmdname,cmd_len) == 0) {
573 mycmd = ctp->cmd;
574 break;
575 }
576 ctp++;
577 }
578 }
579
580 switch (mycmd) {
581 case CMD_CREATE_TDB:
582 bIterate = 0;
583 create_tdb(arg1);
584 return 0;
585 case CMD_OPEN_TDB:
586 bIterate = 0;
587 open_tdb(arg1);
588 return 0;
589 case CMD_SYSTEM:
590 /* Shell command */
591 if (system(arg1) == -1) {
592 terror("system() call failed\n");
593 }
594 return 0;
595 case CMD_QUIT:
596 return 1;
597 default:
598 /* all the rest require a open database */
599 if (!tdb) {
600 bIterate = 0;
601 terror("database not open");
602 help();
603 return 0;
604 }
605 switch (mycmd) {
606 case CMD_TRANSACTION_START:
607 bIterate = 0;
608 tdb_transaction_start(tdb);
609 return 0;
610 case CMD_TRANSACTION_COMMIT:
611 bIterate = 0;
612 tdb_transaction_commit(tdb);
613 return 0;
614 case CMD_TRANSACTION_CANCEL:
615 bIterate = 0;
616 tdb_transaction_cancel(tdb);
617 return 0;
618 case CMD_ERASE:
619 bIterate = 0;
620 tdb_traverse(tdb, do_delete_fn, NULL);
621 return 0;
622 case CMD_DUMP:
623 bIterate = 0;
624 tdb_traverse(tdb, print_rec, NULL);
625 return 0;
626 case CMD_INSERT:
627 bIterate = 0;
628 insert_tdb(arg1, arg1len,arg2,arg2len);
629 return 0;
630 case CMD_MOVE:
631 bIterate = 0;
632 move_rec(arg1,arg1len,arg2);
633 return 0;
634 case CMD_STORE:
635 bIterate = 0;
636 store_tdb(arg1,arg1len,arg2,arg2len);
637 return 0;
638 case CMD_SHOW:
639 bIterate = 0;
640 show_tdb(arg1, arg1len);
641 return 0;
642 case CMD_KEYS:
643 tdb_traverse(tdb, print_key, NULL);
644 return 0;
645 case CMD_HEXKEYS:
646 tdb_traverse(tdb, print_hexkey, NULL);
647 return 0;
648 case CMD_DELETE:
649 bIterate = 0;
650 delete_tdb(arg1,arg1len);
651 return 0;
652 case CMD_LIST_HASH_FREE:
653 tdb_dump_all(tdb);
654 return 0;
655 case CMD_LIST_FREE:
656 tdb_printfreelist(tdb);
657 return 0;
658 case CMD_INFO:
659 info_tdb();
660 return 0;
661 case CMD_SPEED:
662 speed_tdb(arg1);
663 return 0;
664 case CMD_MMAP:
665 toggle_mmap();
666 return 0;
667 case CMD_FIRST:
668 bIterate = 1;
669 first_record(tdb, &iterate_kbuf);
670 return 0;
671 case CMD_NEXT:
672 if (bIterate)
673 next_record(tdb, &iterate_kbuf);
674 return 0;
675 case CMD_CHECK:
676 check_db(tdb);
677 return 0;
678 case CMD_HELP:
679 help();
680 return 0;
681 case CMD_CREATE_TDB:
682 case CMD_OPEN_TDB:
683 case CMD_SYSTEM:
684 case CMD_QUIT:
685 /*
686 * unhandled commands. cases included here to avoid compiler
687 * warnings.
688 */
689 return 0;
690 }
691 }
692
693 return 0;
694}
695
696static char *convert_string(char *instring, size_t *sizep)
697{
698 size_t length = 0;
699 char *outp, *inp;
700 char temp[3];
701
702 outp = inp = instring;
703
704 while (*inp) {
705 if (*inp == '\\') {
706 inp++;
707 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
708 temp[0] = *inp++;
709 temp[1] = '\0';
710 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
711 temp[1] = *inp++;
712 temp[2] = '\0';
713 }
714 *outp++ = (char)strtol((const char *)temp,NULL,16);
715 } else {
716 *outp++ = *inp++;
717 }
718 } else {
719 *outp++ = *inp++;
720 }
721 length++;
722 }
723 *sizep = length;
724 return instring;
725}
726
727int main(int argc, char *argv[])
728{
729 cmdname = "";
730 arg1 = NULL;
731 arg1len = 0;
732 arg2 = NULL;
733 arg2len = 0;
734
735 if (argv[1]) {
736 cmdname = "open";
737 arg1 = argv[1];
738 do_command();
739 cmdname = "";
740 arg1 = NULL;
741 }
742
743 switch (argc) {
744 case 1:
745 case 2:
746 /* Interactive mode */
747 while ((cmdname = tdb_getline("tdb> "))) {
748 arg2 = arg1 = NULL;
749 if ((arg1 = strchr((const char *)cmdname,' ')) != NULL) {
750 arg1++;
751 arg2 = arg1;
752 while (*arg2) {
753 if (*arg2 == ' ') {
754 *arg2++ = '\0';
755 break;
756 }
757 if ((*arg2++ == '\\') && (*arg2 == ' ')) {
758 arg2++;
759 }
760 }
761 }
762 if (arg1) arg1 = convert_string(arg1,&arg1len);
763 if (arg2) arg2 = convert_string(arg2,&arg2len);
764 if (do_command()) break;
765 }
766 break;
767 case 5:
768 arg2 = convert_string(argv[4],&arg2len);
769 case 4:
770 arg1 = convert_string(argv[3],&arg1len);
771 case 3:
772 cmdname = argv[2];
773 default:
774 do_command();
775 break;
776 }
777
778 if (tdb) tdb_close(tdb);
779
780 return 0;
781}
Note: See TracBrowser for help on using the repository browser.