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>
34 static int do_command(void);
37 size_t arg1len, arg2len;
40 TDB_DATA iterate_kbuf;
42 static int disable_mmap;
47 CMD_TRANSACTION_START,
48 CMD_TRANSACTION_COMMIT,
49 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},
90 {"hexkeys", CMD_HEXKEYS},
91 {"delete", CMD_DELETE},
92 {"list", CMD_LIST_HASH_FREE},
93 {"free", CMD_LIST_FREE},
101 {"check", CMD_CHECK},
108 struct timeval tp1,tp2;
110 static void _start_timer(void)
112 gettimeofday(&tp1,NULL);
115 static double _end_timer(void)
117 gettimeofday(&tp2,NULL);
118 return((tp2.tv_sec - tp1.tv_sec) +
119 (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
122 /* a tdb tool for manipulating a tdb database */
124 static TDB_CONTEXT *tdb;
126 static int print_rec(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
127 static int print_key(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
128 static int print_hexkey(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
130 static void print_asc(const char *buf,int len)
134 /* We're probably printing ASCII strings so don't try to display
135 the trailing NULL character. */
137 if (buf[len - 1] == 0)
141 printf("%c",isprint(buf[i])?buf[i]:'.');
144 static void print_data(const char *buf,int len)
150 printf("%02X ",(int)((unsigned char)buf[i]));
152 if (i%8 == 0) printf(" ");
154 print_asc(&buf[i-16],8); printf(" ");
155 print_asc(&buf[i-8],8); printf("\n");
156 if (i<len) printf("[%03X] ",i);
164 if (n>8) printf(" ");
165 while (n--) printf(" ");
169 print_asc(&buf[i-(i%16)],n); printf(" ");
171 if (n>0) print_asc(&buf[i-n],n);
176 static void help(void)
180 " create dbname : create a database\n"
181 " open dbname : open an existing database\n"
182 " transaction_start : start a transaction\n"
183 " transaction_commit : commit a transaction\n"
184 " transaction_cancel : cancel a transaction\n"
185 " erase : erase the database\n"
186 " dump : dump the database as strings\n"
187 " keys : dump the database keys as strings\n"
188 " hexkeys : dump the database keys as hex values\n"
189 " info : print summary info about the database\n"
190 " insert key data : insert a record\n"
191 " move key file : move a record to a destination tdb\n"
192 " store key data : store a record (replace)\n"
193 " show key : show a record by key\n"
194 " delete key : delete a record by key\n"
195 " list : print the database hash table and freelist\n"
196 " free : print the database freelist\n"
197 " check : check the integrity of an opened database\n"
198 " speed : perform speed tests on the database\n"
199 " ! command : execute system command\n"
200 " 1 | first : print the first record\n"
201 " n | next : print the next record\n"
202 " q | quit : terminate\n"
203 " \\n : repeat 'next' command\n"
207 static void terror(const char *why)
212 static void create_tdb(const char *tdbname)
214 if (tdb) tdb_close(tdb);
215 tdb = tdb_open(tdbname, 0, TDB_CLEAR_IF_FIRST | (disable_mmap?TDB_NOMMAP:0),
216 O_RDWR | O_CREAT | O_TRUNC, 0600);
218 printf("Could not create %s: %s\n", tdbname, strerror(errno));
222 static void open_tdb(const char *tdbname)
224 if (tdb) tdb_close(tdb);
225 tdb = tdb_open(tdbname, 0, disable_mmap?TDB_NOMMAP:0, O_RDWR, 0600);
227 printf("Could not open %s: %s\n", tdbname, strerror(errno));
231 static void insert_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
235 if ((keyname == NULL) || (keylen == 0)) {
240 key.dptr = (unsigned char *)keyname;
242 dbuf.dptr = (unsigned char *)data;
243 dbuf.dsize = datalen;
245 if (tdb_store(tdb, key, dbuf, TDB_INSERT) == -1) {
246 terror("insert failed");
250 static void store_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
254 if ((keyname == NULL) || (keylen == 0)) {
259 if ((data == NULL) || (datalen == 0)) {
264 key.dptr = (unsigned char *)keyname;
266 dbuf.dptr = (unsigned char *)data;
267 dbuf.dsize = datalen;
269 printf("Storing key:\n");
270 print_rec(tdb, key, dbuf, NULL);
272 if (tdb_store(tdb, key, dbuf, TDB_REPLACE) == -1) {
273 terror("store failed");
277 static void show_tdb(char *keyname, size_t keylen)
281 if ((keyname == NULL) || (keylen == 0)) {
286 key.dptr = (unsigned char *)keyname;
289 dbuf = tdb_fetch(tdb, key);
291 terror("fetch failed");
295 print_rec(tdb, key, dbuf, NULL);
302 static void delete_tdb(char *keyname, size_t keylen)
306 if ((keyname == NULL) || (keylen == 0)) {
311 key.dptr = (unsigned char *)keyname;
314 if (tdb_delete(tdb, key) != 0) {
315 terror("delete failed");
319 static void move_rec(char *keyname, size_t keylen, char* tdbname)
322 TDB_CONTEXT *dst_tdb;
324 if ((keyname == NULL) || (keylen == 0)) {
330 terror("need destination tdb name");
334 key.dptr = (unsigned char *)keyname;
337 dbuf = tdb_fetch(tdb, key);
339 terror("fetch failed");
343 print_rec(tdb, key, dbuf, NULL);
345 dst_tdb = tdb_open(tdbname, 0, 0, O_RDWR, 0600);
347 terror("unable to open destination tdb");
351 if ( tdb_store( dst_tdb, key, dbuf, TDB_REPLACE ) == -1 ) {
352 terror("failed to move record");
355 printf("record moved\n");
357 tdb_close( dst_tdb );
362 static int print_rec(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
364 printf("\nkey %d bytes\n", (int)key.dsize);
365 print_asc((const char *)key.dptr, key.dsize);
366 printf("\ndata %d bytes\n", (int)dbuf.dsize);
367 print_data((const char *)dbuf.dptr, dbuf.dsize);
371 static int print_key(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
373 printf("key %d bytes: ", (int)key.dsize);
374 print_asc((const char *)key.dptr, key.dsize);
379 static int print_hexkey(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
381 printf("key %d bytes\n", (int)key.dsize);
382 print_data((const char *)key.dptr, key.dsize);
387 static int total_bytes;
389 static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
391 total_bytes += dbuf.dsize;
395 static void info_tdb(void)
399 if ((count = tdb_traverse(tdb, traverse_fn, NULL)) == -1)
400 printf("Error = %s\n", tdb_errorstr(tdb));
402 printf("%d records totalling %d bytes\n", count, total_bytes);
405 static void speed_tdb(const char *tlimit)
407 unsigned timelimit = tlimit?atoi(tlimit):0;
410 if (timelimit == 0) timelimit = 5;
413 printf("Testing store speed for %u seconds\n", timelimit);
416 long int r = random();
418 key.dptr = (unsigned char *)"store test";
419 key.dsize = strlen((char *)key.dptr);
420 dbuf.dptr = (unsigned char *)&r;
421 dbuf.dsize = sizeof(r);
422 tdb_store(tdb, key, dbuf, TDB_REPLACE);
425 } while (t < timelimit);
426 printf("%10.3f ops/sec\n", ops/t);
429 printf("Testing fetch speed for %u seconds\n", timelimit);
432 long int r = random();
434 key.dptr = (unsigned char *)"store test";
435 key.dsize = strlen((char *)key.dptr);
436 dbuf.dptr = (unsigned char *)&r;
437 dbuf.dsize = sizeof(r);
441 } while (t < timelimit);
442 printf("%10.3f ops/sec\n", ops/t);
445 printf("Testing transaction speed for %u seconds\n", timelimit);
448 long int r = random();
450 key.dptr = (unsigned char *)"transaction test";
451 key.dsize = strlen((char *)key.dptr);
452 dbuf.dptr = (unsigned char *)&r;
453 dbuf.dsize = sizeof(r);
454 tdb_transaction_start(tdb);
455 tdb_store(tdb, key, dbuf, TDB_REPLACE);
456 tdb_transaction_commit(tdb);
459 } while (t < timelimit);
460 printf("%10.3f ops/sec\n", ops/t);
463 printf("Testing traverse speed for %u seconds\n", timelimit);
466 tdb_traverse(tdb, traverse_fn, NULL);
469 } while (t < timelimit);
470 printf("%10.3f ops/sec\n", ops/t);
473 static void toggle_mmap(void)
475 disable_mmap = !disable_mmap;
477 printf("mmap is disabled\n");
479 printf("mmap is enabled\n");
483 static char *tdb_getline(const char *prompt)
485 static char thisline[1024];
487 fputs(prompt, stdout);
489 p = fgets(thisline, sizeof(thisline)-1, stdin);
490 if (p) p = strchr(p, '\n');
492 return p?thisline:NULL;
495 static int do_delete_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf,
498 return tdb_delete(the_tdb, key);
501 static void first_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
504 *pkey = tdb_firstkey(the_tdb);
506 dbuf = tdb_fetch(the_tdb, *pkey);
507 if (!dbuf.dptr) terror("fetch failed");
509 print_rec(the_tdb, *pkey, dbuf, NULL);
513 static void next_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
516 *pkey = tdb_nextkey(the_tdb, *pkey);
518 dbuf = tdb_fetch(the_tdb, *pkey);
520 terror("fetch failed");
522 print_rec(the_tdb, *pkey, dbuf, NULL);
525 static int test_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
530 static void check_db(TDB_CONTEXT *the_tdb)
534 tdbcount = tdb_traverse(the_tdb, test_fn, NULL);
536 printf("Error: No database opened!\n");
540 printf("Integrity check for the opened database failed.\n");
542 printf("Database integrity is OK and has %d records.\n", tdbcount);
546 static int do_command(void)
548 COMMAND_TABLE *ctp = cmd_table;
549 enum commands mycmd = CMD_HELP;
552 if (cmdname && strlen(cmdname) == 0) {
556 cmd_len = strlen(ctp->name);
557 if (strncmp(ctp->name,cmdname,cmd_len) == 0) {
576 if (system(arg1) == -1) {
577 terror("system() call failed\n");
583 /* all the rest require a open database */
586 terror("database not open");
591 case CMD_TRANSACTION_START:
593 tdb_transaction_start(tdb);
595 case CMD_TRANSACTION_COMMIT:
597 tdb_transaction_commit(tdb);
599 case CMD_TRANSACTION_CANCEL:
601 tdb_transaction_cancel(tdb);
605 tdb_traverse(tdb, do_delete_fn, NULL);
609 tdb_traverse(tdb, print_rec, NULL);
613 insert_tdb(arg1, arg1len,arg2,arg2len);
617 move_rec(arg1,arg1len,arg2);
621 store_tdb(arg1,arg1len,arg2,arg2len);
625 show_tdb(arg1, arg1len);
628 tdb_traverse(tdb, print_key, NULL);
631 tdb_traverse(tdb, print_hexkey, NULL);
635 delete_tdb(arg1,arg1len);
637 case CMD_LIST_HASH_FREE:
641 tdb_printfreelist(tdb);
654 first_record(tdb, &iterate_kbuf);
658 next_record(tdb, &iterate_kbuf);
671 * unhandled commands. cases included here to avoid compiler
681 static char *convert_string(char *instring, size_t *sizep)
687 outp = inp = instring;
692 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
695 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
699 *outp++ = (char)strtol((const char *)temp,NULL,16);
712 int main(int argc, char *argv[])
731 /* Interactive mode */
732 while ((cmdname = tdb_getline("tdb> "))) {
734 if ((arg1 = strchr((const char *)cmdname,' ')) != NULL) {
742 if ((*arg2++ == '\\') && (*arg2 == ' ')) {
747 if (arg1) arg1 = convert_string(arg1,&arg1len);
748 if (arg2) arg2 = convert_string(arg2,&arg2len);
749 if (do_command()) break;
753 arg2 = convert_string(argv[4],&arg2len);
755 arg1 = convert_string(argv[3],&arg1len);
763 if (tdb) tdb_close(tdb);