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 <ccan/tdb/tdb.h>
28 #include <sys/types.h>
35 static int do_command(void);
38 size_t arg1len, arg2len;
41 TDB_DATA iterate_kbuf;
43 static int disable_mmap;
48 CMD_TRANSACTION_START,
49 CMD_TRANSACTION_COMMIT,
50 CMD_TRANSACTION_CANCEL,
78 COMMAND_TABLE cmd_table[] = {
79 {"create", CMD_CREATE_TDB},
80 {"open", CMD_OPEN_TDB},
81 {"transaction_start", CMD_TRANSACTION_START},
82 {"transaction_commit", CMD_TRANSACTION_COMMIT},
83 {"transaction_cancel", CMD_TRANSACTION_CANCEL},
86 {"insert", CMD_INSERT},
91 {"hexkeys", CMD_HEXKEYS},
92 {"delete", CMD_DELETE},
93 {"list", CMD_LIST_HASH_FREE},
94 {"free", CMD_LIST_FREE},
102 {"check", CMD_CHECK},
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 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...)
127 va_start(ap, format);
128 vfprintf(stderr, format, ap);
132 /* a tdb tool for manipulating a tdb database */
134 static TDB_CONTEXT *tdb;
136 static int print_rec(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
137 static int print_key(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
138 static int print_hexkey(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
140 static void print_asc(const char *buf,int len)
144 /* We're probably printing ASCII strings so don't try to display
145 the trailing NULL character. */
147 if (buf[len - 1] == 0)
151 printf("%c",isprint(buf[i])?buf[i]:'.');
154 static void print_data(const char *buf,int len)
160 printf("%02X ",(int)((unsigned char)buf[i]));
162 if (i%8 == 0) printf(" ");
164 print_asc(&buf[i-16],8); printf(" ");
165 print_asc(&buf[i-8],8); printf("\n");
166 if (i<len) printf("[%03X] ",i);
174 if (n>8) printf(" ");
175 while (n--) printf(" ");
179 print_asc(&buf[i-(i%16)],n); printf(" ");
181 if (n>0) print_asc(&buf[i-n],n);
186 static void help(void)
190 " create dbname : create a database\n"
191 " open dbname : open an existing database\n"
192 " openjh dbname : open an existing database (jenkins hash)\n"
193 " transaction_start : start a transaction\n"
194 " transaction_commit : commit a transaction\n"
195 " transaction_cancel : cancel a transaction\n"
196 " erase : erase the database\n"
197 " dump : dump the database as strings\n"
198 " keys : dump the database keys as strings\n"
199 " hexkeys : dump the database keys as hex values\n"
200 " info : print summary info about the database\n"
201 " insert key data : insert a record\n"
202 " move key file : move a record to a destination tdb\n"
203 " store key data : store a record (replace)\n"
204 " show key : show a record by key\n"
205 " delete key : delete a record by key\n"
206 " list : print the database hash table and freelist\n"
207 " free : print the database freelist\n"
208 " check : check the integrity of an opened database\n"
209 " speed : perform speed tests on the database\n"
210 " ! command : execute system command\n"
211 " 1 | first : print the first record\n"
212 " n | next : print the next record\n"
213 " q | quit : terminate\n"
214 " \\n : repeat 'next' command\n"
218 static void terror(const char *why)
223 static void create_tdb(const char *tdbname)
225 struct tdb_logging_context log_ctx;
226 log_ctx.log_fn = tdb_log;
228 if (tdb) tdb_close(tdb);
229 tdb = tdb_open_ex(tdbname, 0, TDB_CLEAR_IF_FIRST | (disable_mmap?TDB_NOMMAP:0),
230 O_RDWR | O_CREAT | O_TRUNC, 0600, &log_ctx, NULL);
232 printf("Could not create %s: %s\n", tdbname, strerror(errno));
236 static void open_tdb(const char *tdbname)
238 struct tdb_logging_context log_ctx;
239 log_ctx.log_fn = tdb_log;
241 if (tdb) tdb_close(tdb);
242 tdb = tdb_open_ex(tdbname, 0, disable_mmap?TDB_NOMMAP:0, O_RDWR, 0600,
245 printf("Could not open %s: %s\n", tdbname, strerror(errno));
249 static void insert_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
253 if ((keyname == NULL) || (keylen == 0)) {
258 key.dptr = (unsigned char *)keyname;
260 dbuf.dptr = (unsigned char *)data;
261 dbuf.dsize = datalen;
263 if (tdb_store(tdb, key, dbuf, TDB_INSERT) == -1) {
264 terror("insert failed");
268 static void store_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
272 if ((keyname == NULL) || (keylen == 0)) {
277 if ((data == NULL) || (datalen == 0)) {
282 key.dptr = (unsigned char *)keyname;
284 dbuf.dptr = (unsigned char *)data;
285 dbuf.dsize = datalen;
287 printf("Storing key:\n");
288 print_rec(tdb, key, dbuf, NULL);
290 if (tdb_store(tdb, key, dbuf, TDB_REPLACE) == -1) {
291 terror("store failed");
295 static void show_tdb(char *keyname, size_t keylen)
299 if ((keyname == NULL) || (keylen == 0)) {
304 key.dptr = (unsigned char *)keyname;
307 dbuf = tdb_fetch(tdb, key);
309 terror("fetch failed");
313 print_rec(tdb, key, dbuf, NULL);
320 static void delete_tdb(char *keyname, size_t keylen)
324 if ((keyname == NULL) || (keylen == 0)) {
329 key.dptr = (unsigned char *)keyname;
332 if (tdb_delete(tdb, key) != 0) {
333 terror("delete failed");
337 static void move_rec(char *keyname, size_t keylen, char* tdbname)
340 TDB_CONTEXT *dst_tdb;
342 if ((keyname == NULL) || (keylen == 0)) {
348 terror("need destination tdb name");
352 key.dptr = (unsigned char *)keyname;
355 dbuf = tdb_fetch(tdb, key);
357 terror("fetch failed");
361 print_rec(tdb, key, dbuf, NULL);
363 dst_tdb = tdb_open(tdbname, 0, 0, O_RDWR, 0600);
365 terror("unable to open destination tdb");
369 if ( tdb_store( dst_tdb, key, dbuf, TDB_REPLACE ) == -1 ) {
370 terror("failed to move record");
373 printf("record moved\n");
375 tdb_close( dst_tdb );
380 static int print_rec(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
382 printf("\nkey %d bytes\n", (int)key.dsize);
383 print_asc((const char *)key.dptr, key.dsize);
384 printf("\ndata %d bytes\n", (int)dbuf.dsize);
385 print_data((const char *)dbuf.dptr, dbuf.dsize);
389 static int print_key(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
391 printf("key %d bytes: ", (int)key.dsize);
392 print_asc((const char *)key.dptr, key.dsize);
397 static int print_hexkey(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
399 printf("key %d bytes\n", (int)key.dsize);
400 print_data((const char *)key.dptr, key.dsize);
405 static int total_bytes;
407 static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
409 total_bytes += dbuf.dsize;
413 static void info_tdb(void)
415 char *summary = tdb_summary(tdb, TDB_SUMMARY_HISTOGRAMS);
418 printf("Error = %s\n", tdb_errorstr(tdb));
420 printf("%s", summary);
425 static void speed_tdb(const char *tlimit)
427 unsigned timelimit = tlimit?atoi(tlimit):0;
430 if (timelimit == 0) timelimit = 5;
433 printf("Testing store speed for %u seconds\n", timelimit);
436 long int r = random();
438 key.dptr = (unsigned char *)"store test";
439 key.dsize = strlen((char *)key.dptr);
440 dbuf.dptr = (unsigned char *)&r;
441 dbuf.dsize = sizeof(r);
442 tdb_store(tdb, key, dbuf, TDB_REPLACE);
445 } while (t < timelimit);
446 printf("%10.3f ops/sec\n", ops/t);
449 printf("Testing fetch speed for %u seconds\n", timelimit);
452 long int r = random();
454 key.dptr = (unsigned char *)"store test";
455 key.dsize = strlen((char *)key.dptr);
456 dbuf.dptr = (unsigned char *)&r;
457 dbuf.dsize = sizeof(r);
461 } while (t < timelimit);
462 printf("%10.3f ops/sec\n", ops/t);
465 printf("Testing transaction speed for %u seconds\n", timelimit);
468 long int r = random();
470 key.dptr = (unsigned char *)"transaction test";
471 key.dsize = strlen((char *)key.dptr);
472 dbuf.dptr = (unsigned char *)&r;
473 dbuf.dsize = sizeof(r);
474 tdb_transaction_start(tdb);
475 tdb_store(tdb, key, dbuf, TDB_REPLACE);
476 tdb_transaction_commit(tdb);
479 } while (t < timelimit);
480 printf("%10.3f ops/sec\n", ops/t);
483 printf("Testing traverse speed for %u seconds\n", timelimit);
486 tdb_traverse(tdb, traverse_fn, NULL);
489 } while (t < timelimit);
490 printf("%10.3f ops/sec\n", ops/t);
493 static void toggle_mmap(void)
495 disable_mmap = !disable_mmap;
497 printf("mmap is disabled\n");
499 printf("mmap is enabled\n");
503 static char *tdb_getline(const char *prompt)
505 static char thisline[1024];
507 fputs(prompt, stdout);
509 p = fgets(thisline, sizeof(thisline)-1, stdin);
510 if (p) p = strchr(p, '\n');
512 return p?thisline:NULL;
515 static int do_delete_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf,
518 return tdb_delete(the_tdb, key);
521 static void first_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
524 *pkey = tdb_firstkey(the_tdb);
526 dbuf = tdb_fetch(the_tdb, *pkey);
527 if (!dbuf.dptr) terror("fetch failed");
529 print_rec(the_tdb, *pkey, dbuf, NULL);
533 static void next_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
536 *pkey = tdb_nextkey(the_tdb, *pkey);
538 dbuf = tdb_fetch(the_tdb, *pkey);
540 terror("fetch failed");
542 print_rec(the_tdb, *pkey, dbuf, NULL);
545 static void check_db(TDB_CONTEXT *the_tdb)
548 printf("Error: No database opened!\n");
550 if (tdb_check(the_tdb, NULL, NULL) == -1)
551 printf("Integrity check for the opened database failed.\n");
553 printf("Database integrity is OK.\n");
557 static int do_command(void)
559 COMMAND_TABLE *ctp = cmd_table;
560 enum commands mycmd = CMD_HELP;
563 if (cmdname && strlen(cmdname) == 0) {
567 cmd_len = strlen(ctp->name);
568 if (strncmp(ctp->name,cmdname,cmd_len) == 0) {
587 if (system(arg1) == -1) {
588 terror("system() call failed\n");
594 /* all the rest require a open database */
597 terror("database not open");
602 case CMD_TRANSACTION_START:
604 tdb_transaction_start(tdb);
606 case CMD_TRANSACTION_COMMIT:
608 tdb_transaction_commit(tdb);
610 case CMD_TRANSACTION_CANCEL:
612 tdb_transaction_cancel(tdb);
616 tdb_traverse(tdb, do_delete_fn, NULL);
620 tdb_traverse(tdb, print_rec, NULL);
624 insert_tdb(arg1, arg1len,arg2,arg2len);
628 move_rec(arg1,arg1len,arg2);
632 store_tdb(arg1,arg1len,arg2,arg2len);
636 show_tdb(arg1, arg1len);
639 tdb_traverse(tdb, print_key, NULL);
642 tdb_traverse(tdb, print_hexkey, NULL);
646 delete_tdb(arg1,arg1len);
648 case CMD_LIST_HASH_FREE:
652 tdb_printfreelist(tdb);
665 first_record(tdb, &iterate_kbuf);
669 next_record(tdb, &iterate_kbuf);
682 * unhandled commands. cases included here to avoid compiler
692 static char *convert_string(char *instring, size_t *sizep)
698 outp = inp = instring;
703 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
706 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
710 *outp++ = (char)strtol((const char *)temp,NULL,16);
723 int main(int argc, char *argv[])
742 /* Interactive mode */
743 while ((cmdname = tdb_getline("tdb> "))) {
745 if ((arg1 = strchr((const char *)cmdname,' ')) != NULL) {
753 if ((*arg2++ == '\\') && (*arg2 == ' ')) {
758 if (arg1) arg1 = convert_string(arg1,&arg1len);
759 if (arg2) arg2 = convert_string(arg2,&arg2len);
760 if (do_command()) break;
764 arg2 = convert_string(argv[4],&arg2len);
766 arg1 = convert_string(argv[3],&arg1len);
774 if (tdb) tdb_close(tdb);