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;
43 CMD_TRANSACTION_START,
44 CMD_TRANSACTION_COMMIT,
45 CMD_TRANSACTION_CANCEL,
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},
81 {"insert", CMD_INSERT},
86 {"hexkeys", CMD_HEXKEYS},
87 {"delete", CMD_DELETE},
88 {"list", CMD_LIST_HASH_FREE},
89 {"free", CMD_LIST_FREE},
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);
121 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...)
125 va_start(ap, format);
126 vfprintf(stderr, format, 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)
142 /* We're probably printing ASCII strings so don't try to display
143 the trailing NULL character. */
145 if (buf[len - 1] == 0)
149 printf("%c",isprint(buf[i])?buf[i]:'.');
152 static void print_data(const char *buf,int len)
158 printf("%02X ",(int)((unsigned char)buf[i]));
160 if (i%8 == 0) printf(" ");
162 print_asc(&buf[i-16],8); printf(" ");
163 print_asc(&buf[i-8],8); printf("\n");
164 if (i<len) printf("[%03X] ",i);
172 if (n>8) printf(" ");
173 while (n--) printf(" ");
177 print_asc(&buf[i-(i%16)],n); printf(" ");
179 if (n>0) print_asc(&buf[i-n],n);
184 static void help(void)
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"
215 static void terror(const char *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);
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,
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)
250 if ((keyname == NULL) || (keylen == 0)) {
255 key.dptr = (unsigned char *)keyname;
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)
269 if ((keyname == NULL) || (keylen == 0)) {
274 if ((data == NULL) || (datalen == 0)) {
279 key.dptr = (unsigned char *)keyname;
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)
296 if ((keyname == NULL) || (keylen == 0)) {
301 key.dptr = (unsigned char *)keyname;
304 dbuf = tdb_fetch(tdb, key);
306 terror("fetch failed");
310 print_rec(tdb, key, dbuf, NULL);
317 static void delete_tdb(char *keyname, size_t keylen)
321 if ((keyname == NULL) || (keylen == 0)) {
326 key.dptr = (unsigned char *)keyname;
329 if (tdb_delete(tdb, key) != 0) {
330 terror("delete failed");
334 static void move_rec(char *keyname, size_t keylen, char* tdbname)
337 TDB_CONTEXT *dst_tdb;
339 if ((keyname == NULL) || (keylen == 0)) {
345 terror("need destination tdb name");
349 key.dptr = (unsigned char *)keyname;
352 dbuf = tdb_fetch(tdb, key);
354 terror("fetch failed");
358 print_rec(tdb, key, dbuf, NULL);
360 dst_tdb = tdb_open(tdbname, 0, 0, O_RDWR, 0600);
362 terror("unable to open destination tdb");
366 if (tdb_store( dst_tdb, key, dbuf, TDB_REPLACE ) != 0) {
367 terror("failed to move record");
370 printf("record moved\n");
372 tdb_close( dst_tdb );
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);
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);
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);
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;
410 static void info_tdb(void)
412 char *summary = tdb_summary(tdb);
415 printf("Error = %s\n", tdb_errorstr(tdb));
417 printf("%s", 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;
428 if (timelimit == 0) timelimit = 5;
431 printf("Testing store speed for %u seconds\n", timelimit);
434 long int r = random();
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);
443 } while (t < timelimit);
444 printf("%10.3f ops/sec\n", ops/t);
447 printf("Testing fetch speed for %u seconds\n", timelimit);
450 long int r = random();
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);
459 } while (t < timelimit);
460 printf("%10.3f ops/sec\n", ops/t);
463 printf("Testing transaction speed for %u seconds\n", timelimit);
466 long int r = random();
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);
477 } while (t < timelimit);
478 printf("%10.3f ops/sec\n", ops/t);
481 printf("Testing traverse speed for %u seconds\n", timelimit);
484 tdb_traverse(tdb, traverse_fn, NULL);
487 } while (t < timelimit);
488 printf("%10.3f ops/sec\n", ops/t);
491 static void toggle_mmap(void)
493 disable_mmap = !disable_mmap;
495 printf("mmap is disabled\n");
497 printf("mmap is enabled\n");
501 static char *tdb_getline(const char *prompt)
503 static char thisline[1024];
505 fputs(prompt, stdout);
507 p = fgets(thisline, sizeof(thisline)-1, stdin);
508 if (p) p = strchr(p, '\n');
510 return p?thisline:NULL;
513 static int do_delete_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf,
516 return tdb_delete(the_tdb, key);
519 static void first_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
522 *pkey = tdb_firstkey(the_tdb);
524 dbuf = tdb_fetch(the_tdb, *pkey);
525 if (!dbuf.dptr) terror("fetch failed");
527 print_rec(the_tdb, *pkey, dbuf, NULL);
531 static void next_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
534 *pkey = tdb_nextkey(the_tdb, *pkey);
536 dbuf = tdb_fetch(the_tdb, *pkey);
538 terror("fetch failed");
540 print_rec(the_tdb, *pkey, dbuf, NULL);
543 static int count(TDB_DATA key, TDB_DATA data, void *private_data)
545 (*(unsigned int *)private_data)++;
549 static void check_db(TDB_CONTEXT *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");
557 printf("Database integrity is OK and has %d records.\n",
561 static int do_command(void)
563 COMMAND_TABLE *ctp = cmd_table;
564 enum commands mycmd = CMD_HELP;
567 if (cmdname && strlen(cmdname) == 0) {
571 cmd_len = strlen(ctp->name);
572 if (strncmp(ctp->name,cmdname,cmd_len) == 0) {
591 if (system(arg1) == -1) {
592 terror("system() call failed\n");
598 /* all the rest require a open database */
601 terror("database not open");
606 case CMD_TRANSACTION_START:
608 tdb_transaction_start(tdb);
610 case CMD_TRANSACTION_COMMIT:
612 tdb_transaction_commit(tdb);
614 case CMD_TRANSACTION_CANCEL:
616 tdb_transaction_cancel(tdb);
620 tdb_traverse(tdb, do_delete_fn, NULL);
624 tdb_traverse(tdb, print_rec, NULL);
628 insert_tdb(arg1, arg1len,arg2,arg2len);
632 move_rec(arg1,arg1len,arg2);
636 store_tdb(arg1,arg1len,arg2,arg2len);
640 show_tdb(arg1, arg1len);
643 tdb_traverse(tdb, print_key, NULL);
646 tdb_traverse(tdb, print_hexkey, NULL);
650 delete_tdb(arg1,arg1len);
652 case CMD_LIST_HASH_FREE:
656 tdb_printfreelist(tdb);
669 first_record(tdb, &iterate_kbuf);
673 next_record(tdb, &iterate_kbuf);
686 * unhandled commands. cases included here to avoid compiler
696 static char *tdb_convert_string(char *instring, size_t *sizep)
702 outp = inp = instring;
707 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
710 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
714 *outp++ = (char)strtol((const char *)temp,NULL,16);
727 int main(int argc, char *argv[])
746 /* Interactive mode */
747 while ((cmdname = tdb_getline("tdb> "))) {
749 if ((arg1 = strchr((const char *)cmdname,' ')) != NULL) {
757 if ((*arg2++ == '\\') && (*arg2 == ' ')) {
762 if (arg1) arg1 = tdb_convert_string(arg1,&arg1len);
763 if (arg2) arg2 = tdb_convert_string(arg2,&arg2len);
764 if (do_command()) break;
768 arg2 = tdb_convert_string(argv[4],&arg2len);
770 arg1 = tdb_convert_string(argv[3],&arg1len);
778 if (tdb) tdb_close(tdb);