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,
76 COMMAND_TABLE cmd_table[] = {
77 {"create", CMD_CREATE_TDB},
78 {"open", CMD_OPEN_TDB},
79 {"transaction_start", CMD_TRANSACTION_START},
80 {"transaction_commit", CMD_TRANSACTION_COMMIT},
81 {"transaction_cancel", CMD_TRANSACTION_CANCEL},
84 {"insert", CMD_INSERT},
89 {"hexkeys", CMD_HEXKEYS},
90 {"delete", CMD_DELETE},
91 {"list", CMD_LIST_HASH_FREE},
92 {"free", CMD_LIST_FREE},
93 {"freelist_size", CMD_FREELIST_SIZE},
101 {"check", CMD_CHECK},
105 {"repack", CMD_REPACK},
109 struct timeval tp1,tp2;
111 static void _start_timer(void)
113 gettimeofday(&tp1,NULL);
116 static double _end_timer(void)
118 gettimeofday(&tp2,NULL);
119 return((tp2.tv_sec - tp1.tv_sec) +
120 (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
123 #ifdef PRINTF_ATTRIBUTE
124 static void tdb_log_open(struct tdb_context *tdb, enum tdb_debug_level level,
125 const char *format, ...) PRINTF_ATTRIBUTE(3,4);
127 static void tdb_log_open(struct tdb_context *tdb, enum tdb_debug_level level,
128 const char *format, ...)
130 const char *mutex_msg =
131 "Can use mutexes only with MUTEX_LOCKING or NOLOCK\n";
135 p = strstr(format, mutex_msg);
138 * Yes, this is a hack, but we don't want to see this
139 * message on first open, but we want to see
145 va_start(ap, format);
146 vfprintf(stderr, format, ap);
150 #ifdef PRINTF_ATTRIBUTE
151 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...) PRINTF_ATTRIBUTE(3,4);
153 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...)
157 va_start(ap, format);
158 vfprintf(stderr, format, ap);
162 /* a tdb tool for manipulating a tdb database */
164 static TDB_CONTEXT *tdb;
166 static int print_rec(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
167 static int print_key(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
168 static int print_hexkey(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
170 static void print_asc(const char *buf,int len)
174 /* We're probably printing ASCII strings so don't try to display
175 the trailing NULL character. */
177 if (buf[len - 1] == 0)
181 printf("%c",isprint(buf[i])?buf[i]:'.');
184 static void print_data(const char *buf,int len)
190 printf("%02X ",(int)((unsigned char)buf[i]));
192 if (i%8 == 0) printf(" ");
194 print_asc(&buf[i-16],8); printf(" ");
195 print_asc(&buf[i-8],8); printf("\n");
196 if (i<len) printf("[%03X] ",i);
204 if (n>8) printf(" ");
205 while (n--) printf(" ");
209 print_asc(&buf[i-(i%16)],n); printf(" ");
211 if (n>0) print_asc(&buf[i-n],n);
216 static void help(void)
220 " create dbname : create a database\n"
221 " open dbname : open an existing database\n"
222 " transaction_start : start a transaction\n"
223 " transaction_commit : commit a transaction\n"
224 " transaction_cancel : cancel a transaction\n"
225 " erase : erase the database\n"
226 " dump : dump the database as strings\n"
227 " keys : dump the database keys as strings\n"
228 " hexkeys : dump the database keys as hex values\n"
229 " info : print summary info about the database\n"
230 " insert key data : insert a record\n"
231 " move key file : move a record to a destination tdb\n"
232 " store key data : store a record (replace)\n"
233 " show key : show a record by key\n"
234 " delete key : delete a record by key\n"
235 " list : print the database hash table and freelist\n"
236 " free : print the database freelist\n"
237 " freelist_size : print the number of records in the freelist\n"
238 " check : check the integrity of an opened database\n"
239 " repack : repack the database\n"
240 " speed : perform speed tests on the database\n"
241 " ! command : execute system command\n"
242 " 1 | first : print the first record\n"
243 " n | next : print the next record\n"
244 " q | quit : terminate\n"
245 " \\n : repeat 'next' command\n"
249 static void terror(const char *why)
254 static void create_tdb(const char *tdbname)
256 struct tdb_logging_context log_ctx = { NULL, NULL};
257 log_ctx.log_fn = tdb_log;
259 if (tdb) tdb_close(tdb);
260 tdb = tdb_open_ex(tdbname, 0,
262 (disable_mmap?TDB_NOMMAP:0) |
263 (disable_lock?TDB_NOLOCK:0),
264 O_RDWR | O_CREAT | O_TRUNC, 0600, &log_ctx, NULL);
266 printf("Could not create %s: %s\n", tdbname, strerror(errno));
270 static void open_tdb(const char *tdbname)
272 struct tdb_logging_context log_ctx = { NULL, NULL };
273 log_ctx.log_fn = tdb_log_open;
275 if (tdb) tdb_close(tdb);
276 tdb = tdb_open_ex(tdbname, 0,
277 (disable_mmap?TDB_NOMMAP:0) |
278 (disable_lock?TDB_NOLOCK:0),
282 log_ctx.log_fn = tdb_log;
284 tdb_set_logging_function(tdb, &log_ctx);
287 if ((tdb == NULL) && (errno == EINVAL)) {
289 * Retry NOLOCK and readonly. There we want to see all
292 tdb = tdb_open_ex(tdbname, 0,
293 (disable_mmap?TDB_NOMMAP:0) |TDB_NOLOCK,
299 printf("Could not open %s: %s\n", tdbname, strerror(errno));
303 static void insert_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
307 if ((keyname == NULL) || (keylen == 0)) {
312 key.dptr = (unsigned char *)keyname;
314 dbuf.dptr = (unsigned char *)data;
315 dbuf.dsize = datalen;
317 if (tdb_store(tdb, key, dbuf, TDB_INSERT) != 0) {
318 terror("insert failed");
322 static void store_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
326 if ((keyname == NULL) || (keylen == 0)) {
331 if ((data == NULL) || (datalen == 0)) {
336 key.dptr = (unsigned char *)keyname;
338 dbuf.dptr = (unsigned char *)data;
339 dbuf.dsize = datalen;
341 printf("Storing key:\n");
342 print_rec(tdb, key, dbuf, NULL);
344 if (tdb_store(tdb, key, dbuf, TDB_REPLACE) != 0) {
345 terror("store failed");
349 static void show_tdb(char *keyname, size_t keylen)
353 if ((keyname == NULL) || (keylen == 0)) {
358 key.dptr = (unsigned char *)keyname;
361 dbuf = tdb_fetch(tdb, key);
363 terror("fetch failed");
367 print_rec(tdb, key, dbuf, NULL);
374 static void delete_tdb(char *keyname, size_t keylen)
378 if ((keyname == NULL) || (keylen == 0)) {
383 key.dptr = (unsigned char *)keyname;
386 if (tdb_delete(tdb, key) != 0) {
387 terror("delete failed");
391 static void move_rec(char *keyname, size_t keylen, char* tdbname)
394 TDB_CONTEXT *dst_tdb;
396 if ((keyname == NULL) || (keylen == 0)) {
402 terror("need destination tdb name");
406 key.dptr = (unsigned char *)keyname;
409 dbuf = tdb_fetch(tdb, key);
411 terror("fetch failed");
415 print_rec(tdb, key, dbuf, NULL);
417 dst_tdb = tdb_open(tdbname, 0, 0, O_RDWR, 0600);
419 terror("unable to open destination tdb");
423 if (tdb_store( dst_tdb, key, dbuf, TDB_REPLACE ) != 0) {
424 terror("failed to move record");
427 printf("record moved\n");
429 tdb_close( dst_tdb );
434 static int print_rec(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
436 printf("\nkey %d bytes\n", (int)key.dsize);
437 print_asc((const char *)key.dptr, key.dsize);
438 printf("\ndata %d bytes\n", (int)dbuf.dsize);
439 print_data((const char *)dbuf.dptr, dbuf.dsize);
443 static int print_key(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
445 printf("key %d bytes: ", (int)key.dsize);
446 print_asc((const char *)key.dptr, key.dsize);
451 static int print_hexkey(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
453 printf("key %d bytes\n", (int)key.dsize);
454 print_data((const char *)key.dptr, key.dsize);
459 static int total_bytes;
461 static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
463 total_bytes += dbuf.dsize;
467 static void info_tdb(void)
469 char *summary = tdb_summary(tdb);
472 printf("Error = %s\n", tdb_errorstr(tdb));
474 printf("%s", summary);
479 static void speed_tdb(const char *tlimit)
481 const char *str = "store test", *str2 = "transaction test";
482 unsigned timelimit = tlimit?atoi(tlimit):0;
485 if (timelimit == 0) timelimit = 5;
488 printf("Testing store speed for %u seconds\n", timelimit);
491 long int r = random();
493 key.dptr = discard_const_p(uint8_t, str);
494 key.dsize = strlen((char *)key.dptr);
495 dbuf.dptr = (uint8_t *) &r;
496 dbuf.dsize = sizeof(r);
497 tdb_store(tdb, key, dbuf, TDB_REPLACE);
500 } while (t < timelimit);
501 printf("%10.3f ops/sec\n", ops/t);
504 printf("Testing fetch speed for %u seconds\n", timelimit);
508 key.dptr = discard_const_p(uint8_t, str);
509 key.dsize = strlen((char *)key.dptr);
513 } while (t < timelimit);
514 printf("%10.3f ops/sec\n", ops/t);
517 printf("Testing transaction speed for %u seconds\n", timelimit);
520 long int r = random();
522 key.dptr = discard_const_p(uint8_t, str2);
523 key.dsize = strlen((char *)key.dptr);
524 dbuf.dptr = (uint8_t *) &r;
525 dbuf.dsize = sizeof(r);
526 tdb_transaction_start(tdb);
527 tdb_store(tdb, key, dbuf, TDB_REPLACE);
528 tdb_transaction_commit(tdb);
531 } while (t < timelimit);
532 printf("%10.3f ops/sec\n", ops/t);
535 printf("Testing traverse speed for %u seconds\n", timelimit);
538 tdb_traverse(tdb, traverse_fn, NULL);
541 } while (t < timelimit);
542 printf("%10.3f ops/sec\n", ops/t);
545 static void toggle_mmap(void)
547 disable_mmap = !disable_mmap;
549 printf("mmap is disabled\n");
551 printf("mmap is enabled\n");
555 static char *tdb_getline(const char *prompt)
557 static char thisline[1024];
559 fputs(prompt, stdout);
561 p = fgets(thisline, sizeof(thisline)-1, stdin);
562 if (p) p = strchr(p, '\n');
564 return p?thisline:NULL;
567 static int do_delete_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf,
570 return tdb_delete(the_tdb, key);
573 static void first_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
576 *pkey = tdb_firstkey(the_tdb);
578 dbuf = tdb_fetch(the_tdb, *pkey);
579 if (!dbuf.dptr) terror("fetch failed");
581 print_rec(the_tdb, *pkey, dbuf, NULL);
585 static void next_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
588 *pkey = tdb_nextkey(the_tdb, *pkey);
590 dbuf = tdb_fetch(the_tdb, *pkey);
592 terror("fetch failed");
594 print_rec(the_tdb, *pkey, dbuf, NULL);
597 static int count(TDB_DATA key, TDB_DATA data, void *private_data)
599 (*(unsigned int *)private_data)++;
603 static void check_db(TDB_CONTEXT *the_tdb)
607 printf("Error: No database opened!\n");
608 else if (tdb_check(the_tdb, count, &tdbcount) == -1)
609 printf("Integrity check for the opened database failed.\n");
611 printf("Database integrity is OK and has %d records.\n",
615 static int do_command(void)
617 COMMAND_TABLE *ctp = cmd_table;
618 enum commands mycmd = CMD_HELP;
621 if (cmdname && strlen(cmdname) == 0) {
625 cmd_len = strlen(ctp->name);
626 if (strncmp(ctp->name,cmdname,cmd_len) == 0) {
645 if (system(arg1) == -1) {
646 terror("system() call failed\n");
652 /* all the rest require a open database */
655 terror("database not open");
660 case CMD_TRANSACTION_START:
662 tdb_transaction_start(tdb);
664 case CMD_TRANSACTION_COMMIT:
666 tdb_transaction_commit(tdb);
672 case CMD_TRANSACTION_CANCEL:
674 tdb_transaction_cancel(tdb);
678 tdb_traverse(tdb, do_delete_fn, NULL);
682 tdb_traverse(tdb, print_rec, NULL);
686 insert_tdb(arg1, arg1len,arg2,arg2len);
690 move_rec(arg1,arg1len,arg2);
694 store_tdb(arg1,arg1len,arg2,arg2len);
698 show_tdb(arg1, arg1len);
701 tdb_traverse(tdb, print_key, NULL);
704 tdb_traverse(tdb, print_hexkey, NULL);
708 delete_tdb(arg1,arg1len);
710 case CMD_LIST_HASH_FREE:
714 tdb_printfreelist(tdb);
716 case CMD_FREELIST_SIZE: {
719 size = tdb_freelist_size(tdb);
721 printf("Error getting freelist size.\n");
723 printf("freelist size: %d\n", size);
739 first_record(tdb, &iterate_kbuf);
743 next_record(tdb, &iterate_kbuf);
756 * unhandled commands. cases included here to avoid compiler
766 static char *tdb_convert_string(char *instring, size_t *sizep)
772 outp = inp = instring;
777 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
780 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
784 *outp++ = (char)strtol((const char *)temp,NULL,16);
797 int main(int argc, char *argv[])
805 if (argv[1] && (strcmp(argv[1], "-l") == 0)) {
823 /* Interactive mode */
824 while ((cmdname = tdb_getline("tdb> "))) {
826 if ((arg1 = strchr((const char *)cmdname,' ')) != NULL) {
834 if ((*arg2++ == '\\') && (*arg2 == ' ')) {
839 if (arg1) arg1 = tdb_convert_string(arg1,&arg1len);
840 if (arg2) arg2 = tdb_convert_string(arg2,&arg2len);
841 if (do_command()) break;
845 arg2 = tdb_convert_string(argv[4],&arg2len);
847 arg1 = tdb_convert_string(argv[3],&arg1len);
855 if (tdb) tdb_close(tdb);