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>
24 #include <ccan/hash/hash.h>
29 #include <sys/types.h>
36 static int do_command(void);
39 size_t arg1len, arg2len;
42 TDB_DATA iterate_kbuf;
44 static int disable_mmap;
50 CMD_TRANSACTION_START,
51 CMD_TRANSACTION_COMMIT,
52 CMD_TRANSACTION_CANCEL,
80 COMMAND_TABLE cmd_table[] = {
81 {"create", CMD_CREATE_TDB},
82 {"open", CMD_OPEN_TDB},
83 {"openjh", CMD_OPENJH_TDB},
84 {"transaction_start", CMD_TRANSACTION_START},
85 {"transaction_commit", CMD_TRANSACTION_COMMIT},
86 {"transaction_cancel", CMD_TRANSACTION_CANCEL},
89 {"insert", CMD_INSERT},
94 {"hexkeys", CMD_HEXKEYS},
95 {"delete", CMD_DELETE},
96 {"list", CMD_LIST_HASH_FREE},
97 {"free", CMD_LIST_FREE},
101 {"first", CMD_FIRST},
105 {"check", CMD_CHECK},
112 struct timeval tp1,tp2;
114 static void _start_timer(void)
116 gettimeofday(&tp1,NULL);
119 static double _end_timer(void)
121 gettimeofday(&tp2,NULL);
122 return((tp2.tv_sec - tp1.tv_sec) +
123 (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
126 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...)
130 va_start(ap, format);
131 vfprintf(stderr, format, ap);
135 /* a tdb tool for manipulating a tdb database */
137 static TDB_CONTEXT *tdb;
139 static int print_rec(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
140 static int print_key(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
141 static int print_hexkey(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
143 static void print_asc(const char *buf,int len)
147 /* We're probably printing ASCII strings so don't try to display
148 the trailing NULL character. */
150 if (buf[len - 1] == 0)
154 printf("%c",isprint(buf[i])?buf[i]:'.');
157 static void print_data(const char *buf,int len)
163 printf("%02X ",(int)((unsigned char)buf[i]));
165 if (i%8 == 0) printf(" ");
167 print_asc(&buf[i-16],8); printf(" ");
168 print_asc(&buf[i-8],8); printf("\n");
169 if (i<len) printf("[%03X] ",i);
177 if (n>8) printf(" ");
178 while (n--) printf(" ");
182 print_asc(&buf[i-(i%16)],n); printf(" ");
184 if (n>0) print_asc(&buf[i-n],n);
189 static void help(void)
193 " create dbname : create a database\n"
194 " open dbname : open an existing database\n"
195 " openjh dbname : open an existing database (jenkins hash)\n"
196 " transaction_start : start a transaction\n"
197 " transaction_commit : commit a transaction\n"
198 " transaction_cancel : cancel a transaction\n"
199 " erase : erase the database\n"
200 " dump : dump the database as strings\n"
201 " keys : dump the database keys as strings\n"
202 " hexkeys : dump the database keys as hex values\n"
203 " info : print summary info about the database\n"
204 " insert key data : insert a record\n"
205 " move key file : move a record to a destination tdb\n"
206 " store key data : store a record (replace)\n"
207 " show key : show a record by key\n"
208 " delete key : delete a record by key\n"
209 " list : print the database hash table and freelist\n"
210 " free : print the database freelist\n"
211 " check : check the integrity of an opened database\n"
212 " speed : perform speed tests on the database\n"
213 " ! command : execute system command\n"
214 " 1 | first : print the first record\n"
215 " n | next : print the next record\n"
216 " q | quit : terminate\n"
217 " \\n : repeat 'next' command\n"
221 static void terror(const char *why)
226 static void create_tdb(const char *tdbname)
228 struct tdb_logging_context log_ctx;
229 log_ctx.log_fn = tdb_log;
231 if (tdb) tdb_close(tdb);
232 tdb = tdb_open_ex(tdbname, 0, TDB_CLEAR_IF_FIRST | (disable_mmap?TDB_NOMMAP:0),
233 O_RDWR | O_CREAT | O_TRUNC, 0600, &log_ctx, NULL);
235 printf("Could not create %s: %s\n", tdbname, strerror(errno));
239 static unsigned int jenkins_hash(TDB_DATA *key)
241 return hash_any(key->dptr, key->dsize, 0);
244 static void open_tdb(const char *tdbname, tdb_hash_func hashfn)
246 struct tdb_logging_context log_ctx;
247 log_ctx.log_fn = tdb_log;
249 if (tdb) tdb_close(tdb);
250 tdb = tdb_open_ex(tdbname, 0, disable_mmap?TDB_NOMMAP:0, O_RDWR, 0600,
253 printf("Could not open %s: %s\n", tdbname, strerror(errno));
257 static void insert_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
261 if ((keyname == NULL) || (keylen == 0)) {
266 key.dptr = (unsigned char *)keyname;
268 dbuf.dptr = (unsigned char *)data;
269 dbuf.dsize = datalen;
271 if (tdb_store(tdb, key, dbuf, TDB_INSERT) == -1) {
272 terror("insert failed");
276 static void store_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
280 if ((keyname == NULL) || (keylen == 0)) {
285 if ((data == NULL) || (datalen == 0)) {
290 key.dptr = (unsigned char *)keyname;
292 dbuf.dptr = (unsigned char *)data;
293 dbuf.dsize = datalen;
295 printf("Storing key:\n");
296 print_rec(tdb, key, dbuf, NULL);
298 if (tdb_store(tdb, key, dbuf, TDB_REPLACE) == -1) {
299 terror("store failed");
303 static void show_tdb(char *keyname, size_t keylen)
307 if ((keyname == NULL) || (keylen == 0)) {
312 key.dptr = (unsigned char *)keyname;
315 dbuf = tdb_fetch(tdb, key);
317 terror("fetch failed");
321 print_rec(tdb, key, dbuf, NULL);
328 static void delete_tdb(char *keyname, size_t keylen)
332 if ((keyname == NULL) || (keylen == 0)) {
337 key.dptr = (unsigned char *)keyname;
340 if (tdb_delete(tdb, key) != 0) {
341 terror("delete failed");
345 static void move_rec(char *keyname, size_t keylen, char* tdbname)
348 TDB_CONTEXT *dst_tdb;
350 if ((keyname == NULL) || (keylen == 0)) {
356 terror("need destination tdb name");
360 key.dptr = (unsigned char *)keyname;
363 dbuf = tdb_fetch(tdb, key);
365 terror("fetch failed");
369 print_rec(tdb, key, dbuf, NULL);
371 dst_tdb = tdb_open(tdbname, 0, 0, O_RDWR, 0600);
373 terror("unable to open destination tdb");
377 if ( tdb_store( dst_tdb, key, dbuf, TDB_REPLACE ) == -1 ) {
378 terror("failed to move record");
381 printf("record moved\n");
383 tdb_close( dst_tdb );
388 static int print_rec(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
390 printf("\nkey %d bytes\n", (int)key.dsize);
391 print_asc((const char *)key.dptr, key.dsize);
392 printf("\ndata %d bytes\n", (int)dbuf.dsize);
393 print_data((const char *)dbuf.dptr, dbuf.dsize);
397 static int print_key(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
399 printf("key %d bytes: ", (int)key.dsize);
400 print_asc((const char *)key.dptr, key.dsize);
405 static int print_hexkey(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
407 printf("key %d bytes\n", (int)key.dsize);
408 print_data((const char *)key.dptr, key.dsize);
413 static int total_bytes;
415 static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
417 total_bytes += dbuf.dsize;
421 static void info_tdb(void)
425 if ((count = tdb_traverse(tdb, traverse_fn, NULL)) == -1)
426 printf("Error = %s\n", tdb_errorstr(tdb));
428 printf("%d records totalling %d bytes\n", count, total_bytes);
431 static void speed_tdb(const char *tlimit)
433 unsigned timelimit = tlimit?atoi(tlimit):0;
436 if (timelimit == 0) timelimit = 5;
439 printf("Testing store speed for %u seconds\n", timelimit);
442 long int r = random();
444 key.dptr = (unsigned char *)"store test";
445 key.dsize = strlen((char *)key.dptr);
446 dbuf.dptr = (unsigned char *)&r;
447 dbuf.dsize = sizeof(r);
448 tdb_store(tdb, key, dbuf, TDB_REPLACE);
451 } while (t < timelimit);
452 printf("%10.3f ops/sec\n", ops/t);
455 printf("Testing fetch speed for %u seconds\n", timelimit);
458 long int r = random();
460 key.dptr = (unsigned char *)"store test";
461 key.dsize = strlen((char *)key.dptr);
462 dbuf.dptr = (unsigned char *)&r;
463 dbuf.dsize = sizeof(r);
467 } while (t < timelimit);
468 printf("%10.3f ops/sec\n", ops/t);
471 printf("Testing transaction speed for %u seconds\n", timelimit);
474 long int r = random();
476 key.dptr = (unsigned char *)"transaction test";
477 key.dsize = strlen((char *)key.dptr);
478 dbuf.dptr = (unsigned char *)&r;
479 dbuf.dsize = sizeof(r);
480 tdb_transaction_start(tdb);
481 tdb_store(tdb, key, dbuf, TDB_REPLACE);
482 tdb_transaction_commit(tdb);
485 } while (t < timelimit);
486 printf("%10.3f ops/sec\n", ops/t);
489 printf("Testing traverse speed for %u seconds\n", timelimit);
492 tdb_traverse(tdb, traverse_fn, NULL);
495 } while (t < timelimit);
496 printf("%10.3f ops/sec\n", ops/t);
499 static void toggle_mmap(void)
501 disable_mmap = !disable_mmap;
503 printf("mmap is disabled\n");
505 printf("mmap is enabled\n");
509 static char *tdb_getline(const char *prompt)
511 static char thisline[1024];
513 fputs(prompt, stdout);
515 p = fgets(thisline, sizeof(thisline)-1, stdin);
516 if (p) p = strchr(p, '\n');
518 return p?thisline:NULL;
521 static int do_delete_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf,
524 return tdb_delete(the_tdb, key);
527 static void first_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
530 *pkey = tdb_firstkey(the_tdb);
532 dbuf = tdb_fetch(the_tdb, *pkey);
533 if (!dbuf.dptr) terror("fetch failed");
535 print_rec(the_tdb, *pkey, dbuf, NULL);
539 static void next_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
542 *pkey = tdb_nextkey(the_tdb, *pkey);
544 dbuf = tdb_fetch(the_tdb, *pkey);
546 terror("fetch failed");
548 print_rec(the_tdb, *pkey, dbuf, NULL);
551 static void check_db(TDB_CONTEXT *the_tdb)
554 printf("Error: No database opened!\n");
556 if (tdb_check(the_tdb, NULL, NULL) == -1)
557 printf("Integrity check for the opened database failed.\n");
559 printf("Database integrity is OK.\n");
563 static int do_command(void)
565 COMMAND_TABLE *ctp = cmd_table;
566 enum commands mycmd = CMD_HELP;
569 if (cmdname && strlen(cmdname) == 0) {
573 cmd_len = strlen(ctp->name);
574 if (strncmp(ctp->name,cmdname,cmd_len) == 0) {
589 open_tdb(arg1, NULL);
593 open_tdb(arg1, jenkins_hash);
597 if (system(arg1) == -1) {
598 terror("system() call failed\n");
604 /* all the rest require a open database */
607 terror("database not open");
612 case CMD_TRANSACTION_START:
614 tdb_transaction_start(tdb);
616 case CMD_TRANSACTION_COMMIT:
618 tdb_transaction_commit(tdb);
620 case CMD_TRANSACTION_CANCEL:
622 tdb_transaction_cancel(tdb);
626 tdb_traverse(tdb, do_delete_fn, NULL);
630 tdb_traverse(tdb, print_rec, NULL);
634 insert_tdb(arg1, arg1len,arg2,arg2len);
638 move_rec(arg1,arg1len,arg2);
642 store_tdb(arg1,arg1len,arg2,arg2len);
646 show_tdb(arg1, arg1len);
649 tdb_traverse(tdb, print_key, NULL);
652 tdb_traverse(tdb, print_hexkey, NULL);
656 delete_tdb(arg1,arg1len);
658 case CMD_LIST_HASH_FREE:
662 tdb_printfreelist(tdb);
675 first_record(tdb, &iterate_kbuf);
679 next_record(tdb, &iterate_kbuf);
693 * unhandled commands. cases included here to avoid compiler
703 static char *convert_string(char *instring, size_t *sizep)
709 outp = inp = instring;
714 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
717 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
721 *outp++ = (char)strtol((const char *)temp,NULL,16);
734 int main(int argc, char *argv[])
753 /* Interactive mode */
754 while ((cmdname = tdb_getline("tdb> "))) {
756 if ((arg1 = strchr((const char *)cmdname,' ')) != NULL) {
764 if ((*arg2++ == '\\') && (*arg2 == ' ')) {
769 if (arg1) arg1 = convert_string(arg1,&arg1len);
770 if (arg2) arg2 = convert_string(arg2,&arg2len);
771 if (do_command()) break;
775 arg2 = convert_string(argv[4],&arg2len);
777 arg1 = convert_string(argv[3],&arg1len);
785 if (tdb) tdb_close(tdb);