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/>.
24 #include "system/locale.h"
25 #include "system/time.h"
26 #include "system/filesys.h"
27 #include "system/wait.h"
30 static int do_command(void);
33 size_t arg1len, arg2len;
36 TDB_DATA iterate_kbuf;
38 static int disable_mmap;
39 static int disable_lock;
44 CMD_TRANSACTION_START,
45 CMD_TRANSACTION_COMMIT,
46 CMD_TRANSACTION_CANCEL,
77 COMMAND_TABLE cmd_table[] = {
78 {"create", CMD_CREATE_TDB},
79 {"open", CMD_OPEN_TDB},
80 {"transaction_start", CMD_TRANSACTION_START},
81 {"transaction_commit", CMD_TRANSACTION_COMMIT},
82 {"transaction_cancel", CMD_TRANSACTION_CANCEL},
85 {"insert", CMD_INSERT},
87 {"storehex", CMD_STOREHEX},
91 {"hexkeys", CMD_HEXKEYS},
92 {"delete", CMD_DELETE},
93 {"list", CMD_LIST_HASH_FREE},
94 {"free", CMD_LIST_FREE},
95 {"freelist_size", CMD_FREELIST_SIZE},
103 {"check", CMD_CHECK},
107 {"repack", CMD_REPACK},
111 struct timeval tp1,tp2;
113 static void _start_timer(void)
115 gettimeofday(&tp1,NULL);
118 static double _end_timer(void)
120 gettimeofday(&tp2,NULL);
121 return((tp2.tv_sec - tp1.tv_sec) +
122 (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
125 #ifdef PRINTF_ATTRIBUTE
126 static void tdb_log_open(struct tdb_context *tdb, enum tdb_debug_level level,
127 const char *format, ...) PRINTF_ATTRIBUTE(3,4);
129 static void tdb_log_open(struct tdb_context *tdb, enum tdb_debug_level level,
130 const char *format, ...)
132 const char *mutex_msg =
133 "Can use mutexes only with MUTEX_LOCKING or NOLOCK\n";
137 p = strstr(format, mutex_msg);
140 * Yes, this is a hack, but we don't want to see this
141 * message on first open, but we want to see
147 va_start(ap, format);
148 vfprintf(stderr, format, ap);
152 #ifdef PRINTF_ATTRIBUTE
153 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...) PRINTF_ATTRIBUTE(3,4);
155 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...)
159 va_start(ap, format);
160 vfprintf(stderr, format, ap);
164 /* a tdb tool for manipulating a tdb database */
166 static TDB_CONTEXT *tdb;
168 static int print_rec(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
169 static int print_key(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
170 static int print_hexkey(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
172 static void print_asc(const char *buf,int len)
176 /* We're probably printing ASCII strings so don't try to display
177 the trailing NULL character. */
179 if (buf[len - 1] == 0)
183 printf("%c",isprint(buf[i])?buf[i]:'.');
186 static void print_data(const char *buf,int len)
192 printf("%02X ",(int)((unsigned char)buf[i]));
194 if (i%8 == 0) printf(" ");
196 print_asc(&buf[i-16],8); printf(" ");
197 print_asc(&buf[i-8],8); printf("\n");
198 if (i<len) printf("[%03X] ",i);
206 if (n>8) printf(" ");
207 while (n--) printf(" ");
211 print_asc(&buf[i-(i%16)],n); printf(" ");
213 if (n>0) print_asc(&buf[i-n],n);
218 static void help(void)
222 " create dbname : create a database\n"
223 " open dbname : open an existing database\n"
224 " transaction_start : start a transaction\n"
225 " transaction_commit : commit a transaction\n"
226 " transaction_cancel : cancel a transaction\n"
227 " erase : erase the database\n"
228 " dump : dump the database as strings\n"
229 " keys : dump the database keys as strings\n"
230 " hexkeys : dump the database keys as hex values\n"
231 " info : print summary info about the database\n"
232 " insert key data : insert a record\n"
233 " move key file : move a record to a destination tdb\n"
234 " storehex key data : store a record (replace), key/value in hex format\n"
235 " store key data : store a record (replace)\n"
236 " show key : show a record by key\n"
237 " delete key : delete a record by key\n"
238 " list : print the database hash table and freelist\n"
239 " free : print the database freelist\n"
240 " freelist_size : print the number of records in the freelist\n"
241 " check : check the integrity of an opened database\n"
242 " repack : repack the database\n"
243 " speed : perform speed tests on the database\n"
244 " ! command : execute system command\n"
245 " 1 | first : print the first record\n"
246 " n | next : print the next record\n"
247 " q | quit : terminate\n"
248 " \\n : repeat 'next' command\n"
252 static void terror(const char *why)
257 static void create_tdb(const char *tdbname)
259 struct tdb_logging_context log_ctx = { NULL, NULL};
260 log_ctx.log_fn = tdb_log;
262 if (tdb) tdb_close(tdb);
263 tdb = tdb_open_ex(tdbname, 0,
265 (disable_mmap?TDB_NOMMAP:0) |
266 (disable_lock?TDB_NOLOCK:0),
267 O_RDWR | O_CREAT | O_TRUNC, 0600, &log_ctx, NULL);
269 printf("Could not create %s: %s\n", tdbname, strerror(errno));
273 static void open_tdb(const char *tdbname)
275 struct tdb_logging_context log_ctx = { NULL, NULL };
276 log_ctx.log_fn = tdb_log_open;
278 if (tdb) tdb_close(tdb);
279 tdb = tdb_open_ex(tdbname, 0,
280 (disable_mmap?TDB_NOMMAP:0) |
281 (disable_lock?TDB_NOLOCK:0),
285 log_ctx.log_fn = tdb_log;
287 tdb_set_logging_function(tdb, &log_ctx);
290 if ((tdb == NULL) && (errno == EINVAL)) {
292 * Retry NOLOCK and readonly. There we want to see all
295 tdb = tdb_open_ex(tdbname, 0,
296 (disable_mmap?TDB_NOMMAP:0) |TDB_NOLOCK,
302 printf("Could not open %s: %s\n", tdbname, strerror(errno));
306 static void insert_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
310 if ((keyname == NULL) || (keylen == 0)) {
315 key.dptr = (unsigned char *)keyname;
317 dbuf.dptr = (unsigned char *)data;
318 dbuf.dsize = datalen;
320 if (tdb_store(tdb, key, dbuf, TDB_INSERT) != 0) {
321 terror("insert failed");
325 static void store_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
329 if ((keyname == NULL) || (keylen == 0)) {
334 if ((data == NULL) || (datalen == 0)) {
339 key.dptr = (unsigned char *)keyname;
341 dbuf.dptr = (unsigned char *)data;
342 dbuf.dsize = datalen;
344 printf("Storing key:\n");
345 print_rec(tdb, key, dbuf, NULL);
347 if (tdb_store(tdb, key, dbuf, TDB_REPLACE) != 0) {
348 terror("store failed");
352 static bool hexchar(char c, uint8_t *v)
354 if ((c >= '0') && (c <= '9')) {
358 if ((c >= 'A') && (c <= 'F')) {
362 if ((c >= 'a') && (c <= 'f')) {
369 static bool parse_hex(const char *src, size_t srclen, uint8_t *dst)
373 if ((srclen % 2) != 0) {
381 ok = (hexchar(src[i++], &hi) && hexchar(src[i++], &lo));
392 static void store_hex_tdb(char *keystr, size_t keylen,
393 char *datastr, size_t datalen)
395 if ((keystr == NULL) || (keylen == 0)) {
399 if ((datastr == NULL) || (datalen == 0)) {
405 uint8_t keybuf[keylen/2];
406 TDB_DATA key = { .dptr = keybuf, .dsize = sizeof(keybuf) };
407 uint8_t databuf[datalen/2];
408 TDB_DATA data = { .dptr = databuf, .dsize = sizeof(databuf) };
411 ok = parse_hex(keystr, keylen, keybuf);
413 terror("need hex key");
416 ok = parse_hex(datastr, datalen, databuf);
418 terror("need hex data");
422 printf("storing key/data:\n");
423 print_data((char *)key.dptr, key.dsize);
424 print_data((char *)data.dptr, data.dsize);
426 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0) {
427 terror("store failed");
432 static void show_tdb(char *keyname, size_t keylen)
436 if ((keyname == NULL) || (keylen == 0)) {
441 key.dptr = (unsigned char *)keyname;
444 dbuf = tdb_fetch(tdb, key);
446 terror("fetch failed");
450 print_rec(tdb, key, dbuf, NULL);
457 static void delete_tdb(char *keyname, size_t keylen)
461 if ((keyname == NULL) || (keylen == 0)) {
466 key.dptr = (unsigned char *)keyname;
469 if (tdb_delete(tdb, key) != 0) {
470 terror("delete failed");
474 static void move_rec(char *keyname, size_t keylen, char* tdbname)
477 TDB_CONTEXT *dst_tdb;
479 if ((keyname == NULL) || (keylen == 0)) {
485 terror("need destination tdb name");
489 key.dptr = (unsigned char *)keyname;
492 dbuf = tdb_fetch(tdb, key);
494 terror("fetch failed");
498 print_rec(tdb, key, dbuf, NULL);
500 dst_tdb = tdb_open(tdbname, 0, 0, O_RDWR, 0600);
502 terror("unable to open destination tdb");
506 if (tdb_store( dst_tdb, key, dbuf, TDB_REPLACE ) != 0) {
507 terror("failed to move record");
510 printf("record moved\n");
512 tdb_close( dst_tdb );
517 static int print_rec(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
519 printf("\nkey %d bytes\n", (int)key.dsize);
520 print_asc((const char *)key.dptr, key.dsize);
521 printf("\ndata %d bytes\n", (int)dbuf.dsize);
522 print_data((const char *)dbuf.dptr, dbuf.dsize);
526 static int print_key(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
528 printf("key %d bytes: ", (int)key.dsize);
529 print_asc((const char *)key.dptr, key.dsize);
534 static int print_hexkey(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
536 printf("key %d bytes\n", (int)key.dsize);
537 print_data((const char *)key.dptr, key.dsize);
542 static int total_bytes;
544 static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
546 total_bytes += dbuf.dsize;
550 static void info_tdb(void)
552 char *summary = tdb_summary(tdb);
555 printf("Error = %s\n", tdb_errorstr(tdb));
557 printf("%s", summary);
562 static void speed_tdb(const char *tlimit)
564 const char *str = "store test", *str2 = "transaction test";
565 unsigned timelimit = tlimit?atoi(tlimit):0;
568 if (timelimit == 0) timelimit = 5;
571 printf("Testing store speed for %u seconds\n", timelimit);
574 long int r = random();
576 key.dptr = discard_const_p(uint8_t, str);
577 key.dsize = strlen((char *)key.dptr);
578 dbuf.dptr = (uint8_t *) &r;
579 dbuf.dsize = sizeof(r);
580 tdb_store(tdb, key, dbuf, TDB_REPLACE);
583 } while (t < timelimit);
584 printf("%10.3f ops/sec\n", ops/t);
587 printf("Testing fetch speed for %u seconds\n", timelimit);
591 key.dptr = discard_const_p(uint8_t, str);
592 key.dsize = strlen((char *)key.dptr);
596 } while (t < timelimit);
597 printf("%10.3f ops/sec\n", ops/t);
600 printf("Testing transaction speed for %u seconds\n", timelimit);
603 long int r = random();
605 key.dptr = discard_const_p(uint8_t, str2);
606 key.dsize = strlen((char *)key.dptr);
607 dbuf.dptr = (uint8_t *) &r;
608 dbuf.dsize = sizeof(r);
609 tdb_transaction_start(tdb);
610 tdb_store(tdb, key, dbuf, TDB_REPLACE);
611 tdb_transaction_commit(tdb);
614 } while (t < timelimit);
615 printf("%10.3f ops/sec\n", ops/t);
618 printf("Testing traverse speed for %u seconds\n", timelimit);
621 tdb_traverse(tdb, traverse_fn, NULL);
624 } while (t < timelimit);
625 printf("%10.3f ops/sec\n", ops/t);
628 static void toggle_mmap(void)
630 disable_mmap = !disable_mmap;
632 printf("mmap is disabled\n");
634 printf("mmap is enabled\n");
638 static char *tdb_getline(const char *prompt)
640 static char thisline[1024];
642 fputs(prompt, stdout);
644 p = fgets(thisline, sizeof(thisline)-1, stdin);
645 if (p) p = strchr(p, '\n');
647 return p?thisline:NULL;
650 static int do_delete_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf,
653 return tdb_delete(the_tdb, key);
656 static void first_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
659 *pkey = tdb_firstkey(the_tdb);
661 dbuf = tdb_fetch(the_tdb, *pkey);
662 if (!dbuf.dptr) terror("fetch failed");
664 print_rec(the_tdb, *pkey, dbuf, NULL);
668 static void next_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
671 *pkey = tdb_nextkey(the_tdb, *pkey);
673 dbuf = tdb_fetch(the_tdb, *pkey);
675 terror("fetch failed");
677 print_rec(the_tdb, *pkey, dbuf, NULL);
680 static int count(TDB_DATA key, TDB_DATA data, void *private_data)
682 (*(unsigned int *)private_data)++;
686 static void check_db(TDB_CONTEXT *the_tdb)
690 printf("Error: No database opened!\n");
691 else if (tdb_check(the_tdb, count, &tdbcount) == -1)
692 printf("Integrity check for the opened database failed.\n");
694 printf("Database integrity is OK and has %d records.\n",
698 static int do_command(void)
700 COMMAND_TABLE *ctp = cmd_table;
701 enum commands mycmd = CMD_HELP;
704 if (cmdname && strlen(cmdname) == 0) {
708 cmd_len = strlen(ctp->name);
709 if (strncmp(ctp->name,cmdname,cmd_len) == 0) {
728 if (system(arg1) == -1) {
729 terror("system() call failed\n");
735 /* all the rest require a open database */
738 terror("database not open");
743 case CMD_TRANSACTION_START:
745 tdb_transaction_start(tdb);
747 case CMD_TRANSACTION_COMMIT:
749 tdb_transaction_commit(tdb);
755 case CMD_TRANSACTION_CANCEL:
757 tdb_transaction_cancel(tdb);
761 tdb_traverse(tdb, do_delete_fn, NULL);
765 tdb_traverse(tdb, print_rec, NULL);
769 insert_tdb(arg1, arg1len,arg2,arg2len);
773 move_rec(arg1,arg1len,arg2);
777 store_tdb(arg1,arg1len,arg2,arg2len);
781 store_hex_tdb(arg1,arg1len,arg2,arg2len);
785 show_tdb(arg1, arg1len);
788 tdb_traverse(tdb, print_key, NULL);
791 tdb_traverse(tdb, print_hexkey, NULL);
795 delete_tdb(arg1,arg1len);
797 case CMD_LIST_HASH_FREE:
801 tdb_printfreelist(tdb);
803 case CMD_FREELIST_SIZE: {
806 size = tdb_freelist_size(tdb);
808 printf("Error getting freelist size.\n");
810 printf("freelist size: %d\n", size);
826 first_record(tdb, &iterate_kbuf);
830 next_record(tdb, &iterate_kbuf);
843 * unhandled commands. cases included here to avoid compiler
853 static char *tdb_convert_string(char *instring, size_t *sizep)
859 outp = inp = instring;
864 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
867 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
871 *outp++ = (char)strtol((const char *)temp,NULL,16);
884 int main(int argc, char *argv[])
892 if (argv[1] && (strcmp(argv[1], "-l") == 0)) {
910 /* Interactive mode */
911 while ((cmdname = tdb_getline("tdb> "))) {
913 if ((arg1 = strchr((const char *)cmdname,' ')) != NULL) {
921 if ((*arg2++ == '\\') && (*arg2 == ' ')) {
926 if (arg1) arg1 = tdb_convert_string(arg1,&arg1len);
927 if (arg2) arg2 = tdb_convert_string(arg2,&arg2len);
928 if (do_command()) break;
932 arg2 = tdb_convert_string(argv[4],&arg2len);
935 arg1 = tdb_convert_string(argv[3],&arg1len);
945 if (tdb) tdb_close(tdb);