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/>.
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,
80 COMMAND_TABLE cmd_table[] = {
81 {"create", CMD_CREATE_TDB},
82 {"open", CMD_OPEN_TDB},
84 {"transaction_start", CMD_TRANSACTION_START},
85 {"transaction_commit", CMD_TRANSACTION_COMMIT},
86 {"transaction_cancel", CMD_TRANSACTION_CANCEL},
90 {"insert", CMD_INSERT},
95 {"hexkeys", CMD_HEXKEYS},
96 {"delete", CMD_DELETE},
98 {"list", CMD_LIST_HASH_FREE},
99 {"free", CMD_LIST_FREE},
102 {"speed", CMD_SPEED},
104 {"first", CMD_FIRST},
108 {"check", CMD_CHECK},
115 struct timeval tp1,tp2;
117 static void _start_timer(void)
119 gettimeofday(&tp1,NULL);
122 static double _end_timer(void)
124 gettimeofday(&tp2,NULL);
125 return((tp2.tv_sec - tp1.tv_sec) +
126 (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
129 static void tdb_log(struct tdb_context *tdb,
130 enum tdb_log_level level,
131 enum TDB_ERROR ecode,
135 fprintf(stderr, "tdb:%s:%s:%s\n",
136 tdb_name(tdb), tdb_errorstr(ecode), message);
139 /* a tdb tool for manipulating a tdb database */
141 static struct tdb_context *tdb;
143 static int print_rec(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
144 static int print_key(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
145 static int print_hexkey(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
147 static void print_asc(const char *buf,int len)
151 /* We're probably printing ASCII strings so don't try to display
152 the trailing NULL character. */
154 if (buf[len - 1] == 0)
158 printf("%c",isprint(buf[i])?buf[i]:'.');
161 static void print_data(const char *buf,int len)
167 printf("%02X ",(int)((unsigned char)buf[i]));
169 if (i%8 == 0) printf(" ");
171 print_asc(&buf[i-16],8); printf(" ");
172 print_asc(&buf[i-8],8); printf("\n");
173 if (i<len) printf("[%03X] ",i);
181 if (n>8) printf(" ");
182 while (n--) printf(" ");
186 print_asc(&buf[i-(i%16)],n); printf(" ");
188 if (n>0) print_asc(&buf[i-n],n);
193 static void help(void)
197 " create dbname : create a database\n"
198 " open dbname : open an existing database\n"
199 " openjh dbname : open an existing database (jenkins hash)\n"
200 " transaction_start : start a transaction\n"
201 " transaction_commit : commit a transaction\n"
202 " transaction_cancel : cancel a transaction\n"
203 " erase : erase the database\n"
204 " dump : dump the database as strings\n"
205 " keys : dump the database keys as strings\n"
206 " hexkeys : dump the database keys as hex values\n"
207 " info : print summary info about the database\n"
208 " insert key data : insert a record\n"
209 " move key file : move a record to a destination tdb\n"
210 " store key data : store a record (replace)\n"
211 " show key : show a record by key\n"
212 " delete key : delete a record by key\n"
214 " list : print the database hash table and freelist\n"
215 " free : print the database freelist\n"
217 " check : check the integrity of an opened database\n"
218 " speed : perform speed tests on the database\n"
219 " ! command : execute system command\n"
220 " 1 | first : print the first record\n"
221 " n | next : print the next record\n"
222 " q | quit : terminate\n"
223 " \\n : repeat 'next' command\n"
227 static void terror(enum TDB_ERROR err, const char *why)
229 if (err != TDB_SUCCESS)
230 printf("%s:%s\n", tdb_errorstr(err), why);
235 static void create_tdb(const char *tdbname)
237 union tdb_attribute log_attr;
238 log_attr.base.attr = TDB_ATTRIBUTE_LOG;
239 log_attr.base.next = NULL;
240 log_attr.log.fn = tdb_log;
242 if (tdb) tdb_close(tdb);
243 tdb = tdb_open(tdbname, (disable_mmap?TDB_NOMMAP:0),
244 O_RDWR | O_CREAT | O_TRUNC, 0600, &log_attr);
246 printf("Could not create %s: %s\n", tdbname, strerror(errno));
250 static void open_tdb(const char *tdbname)
252 union tdb_attribute log_attr;
253 log_attr.base.attr = TDB_ATTRIBUTE_LOG;
254 log_attr.base.next = NULL;
255 log_attr.log.fn = tdb_log;
257 if (tdb) tdb_close(tdb);
258 tdb = tdb_open(tdbname, disable_mmap?TDB_NOMMAP:0, O_RDWR, 0600,
261 printf("Could not open %s: %s\n", tdbname, strerror(errno));
265 static void insert_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
268 enum TDB_ERROR ecode;
270 if ((keyname == NULL) || (keylen == 0)) {
271 terror(TDB_SUCCESS, "need key");
275 key.dptr = (unsigned char *)keyname;
277 dbuf.dptr = (unsigned char *)data;
278 dbuf.dsize = datalen;
280 ecode = tdb_store(tdb, key, dbuf, TDB_INSERT);
282 terror(ecode, "insert failed");
286 static void store_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
289 enum TDB_ERROR ecode;
291 if ((keyname == NULL) || (keylen == 0)) {
292 terror(TDB_SUCCESS, "need key");
296 if ((data == NULL) || (datalen == 0)) {
297 terror(TDB_SUCCESS, "need data");
301 key.dptr = (unsigned char *)keyname;
303 dbuf.dptr = (unsigned char *)data;
304 dbuf.dsize = datalen;
306 printf("Storing key:\n");
307 print_rec(tdb, key, dbuf, NULL);
309 ecode = tdb_store(tdb, key, dbuf, TDB_REPLACE);
311 terror(ecode, "store failed");
315 static void show_tdb(char *keyname, size_t keylen)
318 enum TDB_ERROR ecode;
320 if ((keyname == NULL) || (keylen == 0)) {
321 terror(TDB_SUCCESS, "need key");
325 key.dptr = (unsigned char *)keyname;
328 ecode = tdb_fetch(tdb, key, &dbuf);
330 terror(ecode, "fetch failed");
334 print_rec(tdb, key, dbuf, NULL);
339 static void delete_tdb(char *keyname, size_t keylen)
342 enum TDB_ERROR ecode;
344 if ((keyname == NULL) || (keylen == 0)) {
345 terror(TDB_SUCCESS, "need key");
349 key.dptr = (unsigned char *)keyname;
352 ecode = tdb_delete(tdb, key);
354 terror(ecode, "delete failed");
358 static void move_rec(char *keyname, size_t keylen, char* tdbname)
361 struct tdb_context *dst_tdb;
362 enum TDB_ERROR ecode;
364 if ((keyname == NULL) || (keylen == 0)) {
365 terror(TDB_SUCCESS, "need key");
370 terror(TDB_SUCCESS, "need destination tdb name");
374 key.dptr = (unsigned char *)keyname;
377 ecode = tdb_fetch(tdb, key, &dbuf);
379 terror(ecode, "fetch failed");
383 print_rec(tdb, key, dbuf, NULL);
385 dst_tdb = tdb_open(tdbname, 0, O_RDWR, 0600, NULL);
387 terror(TDB_SUCCESS, "unable to open destination tdb");
391 ecode = tdb_store( dst_tdb, key, dbuf, TDB_REPLACE);
393 terror(ecode, "failed to move record");
395 printf("record moved\n");
397 tdb_close( dst_tdb );
400 static int print_rec(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
402 printf("\nkey %d bytes\n", (int)key.dsize);
403 print_asc((const char *)key.dptr, key.dsize);
404 printf("\ndata %d bytes\n", (int)dbuf.dsize);
405 print_data((const char *)dbuf.dptr, dbuf.dsize);
409 static int print_key(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
411 printf("key %d bytes: ", (int)key.dsize);
412 print_asc((const char *)key.dptr, key.dsize);
417 static int print_hexkey(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
419 printf("key %d bytes\n", (int)key.dsize);
420 print_data((const char *)key.dptr, key.dsize);
425 static int total_bytes;
427 static int traverse_fn(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
429 total_bytes += dbuf.dsize;
433 static void info_tdb(void)
435 enum TDB_ERROR ecode;
438 ecode = tdb_summary(tdb, TDB_SUMMARY_HISTOGRAMS, &summary);
441 terror(ecode, "Getting summary");
443 printf("%s", summary);
448 static void speed_tdb(const char *tlimit)
450 unsigned timelimit = tlimit?atoi(tlimit):0;
453 if (timelimit == 0) timelimit = 5;
456 printf("Testing store speed for %u seconds\n", timelimit);
459 long int r = random();
461 key = tdb_mkdata("store test", strlen("store test"));
462 dbuf.dptr = (unsigned char *)&r;
463 dbuf.dsize = sizeof(r);
464 tdb_store(tdb, key, dbuf, TDB_REPLACE);
467 } while (t < timelimit);
468 printf("%10.3f ops/sec\n", ops/t);
471 printf("Testing fetch speed for %u seconds\n", timelimit);
474 long int r = random();
476 key = tdb_mkdata("store test", strlen("store test"));
477 dbuf.dptr = (unsigned char *)&r;
478 dbuf.dsize = sizeof(r);
479 tdb_fetch(tdb, key, &dbuf);
482 } while (t < timelimit);
483 printf("%10.3f ops/sec\n", ops/t);
486 printf("Testing transaction speed for %u seconds\n", timelimit);
489 long int r = random();
491 key = tdb_mkdata("transaction test", strlen("transaction test"));
492 dbuf.dptr = (unsigned char *)&r;
493 dbuf.dsize = sizeof(r);
494 tdb_transaction_start(tdb);
495 tdb_store(tdb, key, dbuf, TDB_REPLACE);
496 tdb_transaction_commit(tdb);
499 } while (t < timelimit);
500 printf("%10.3f ops/sec\n", ops/t);
503 printf("Testing traverse speed for %u seconds\n", timelimit);
506 tdb_traverse(tdb, traverse_fn, NULL);
509 } while (t < timelimit);
510 printf("%10.3f ops/sec\n", ops/t);
513 static void toggle_mmap(void)
515 disable_mmap = !disable_mmap;
517 printf("mmap is disabled\n");
519 printf("mmap is enabled\n");
523 static char *tdb_getline(const char *prompt)
525 static char thisline[1024];
527 fputs(prompt, stdout);
529 p = fgets(thisline, sizeof(thisline)-1, stdin);
530 if (p) p = strchr(p, '\n');
532 return p?thisline:NULL;
535 static int do_delete_fn(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf,
538 return tdb_delete(the_tdb, key);
541 static void first_record(struct tdb_context *the_tdb, TDB_DATA *pkey)
544 enum TDB_ERROR ecode;
545 ecode = tdb_firstkey(the_tdb, pkey);
547 ecode = tdb_fetch(the_tdb, *pkey, &dbuf);
548 if (ecode) terror(ecode, "fetch failed");
550 print_rec(the_tdb, *pkey, dbuf, NULL);
554 static void next_record(struct tdb_context *the_tdb, TDB_DATA *pkey)
557 enum TDB_ERROR ecode;
558 ecode = tdb_nextkey(the_tdb, pkey);
561 ecode = tdb_fetch(the_tdb, *pkey, &dbuf);
563 terror(ecode, "fetch failed");
565 print_rec(the_tdb, *pkey, dbuf, NULL);
568 static void check_db(struct tdb_context *the_tdb)
571 printf("Error: No database opened!\n");
573 if (tdb_check(the_tdb, NULL, NULL) != 0)
574 printf("Integrity check for the opened database failed.\n");
576 printf("Database integrity is OK.\n");
580 static int do_command(void)
582 COMMAND_TABLE *ctp = cmd_table;
583 enum commands mycmd = CMD_HELP;
586 if (cmdname && strlen(cmdname) == 0) {
590 cmd_len = strlen(ctp->name);
591 if (strncmp(ctp->name,cmdname,cmd_len) == 0) {
610 if (system(arg1) == -1) {
611 terror(TDB_SUCCESS, "system() call failed\n");
617 /* all the rest require a open database */
620 terror(TDB_SUCCESS, "database not open");
625 case CMD_TRANSACTION_START:
627 tdb_transaction_start(tdb);
629 case CMD_TRANSACTION_COMMIT:
631 tdb_transaction_commit(tdb);
633 case CMD_TRANSACTION_CANCEL:
635 tdb_transaction_cancel(tdb);
639 tdb_traverse(tdb, do_delete_fn, NULL);
643 tdb_traverse(tdb, print_rec, NULL);
647 insert_tdb(arg1, arg1len,arg2,arg2len);
651 move_rec(arg1,arg1len,arg2);
655 store_tdb(arg1,arg1len,arg2,arg2len);
659 show_tdb(arg1, arg1len);
662 tdb_traverse(tdb, print_key, NULL);
665 tdb_traverse(tdb, print_hexkey, NULL);
669 delete_tdb(arg1,arg1len);
672 case CMD_LIST_HASH_FREE:
676 tdb_printfreelist(tdb);
690 first_record(tdb, &iterate_kbuf);
694 next_record(tdb, &iterate_kbuf);
707 * unhandled commands. cases included here to avoid compiler
717 static char *convert_string(char *instring, size_t *sizep)
723 outp = inp = instring;
728 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
731 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
735 *outp++ = (char)strtol((const char *)temp,NULL,16);
748 int main(int argc, char *argv[])
767 /* Interactive mode */
768 while ((cmdname = tdb_getline("tdb> "))) {
770 if ((arg1 = strchr((const char *)cmdname,' ')) != NULL) {
778 if ((*arg2++ == '\\') && (*arg2 == ' ')) {
783 if (arg1) arg1 = convert_string(arg1,&arg1len);
784 if (arg2) arg2 = convert_string(arg2,&arg2len);
785 if (do_command()) break;
789 arg2 = convert_string(argv[4],&arg2len);
791 arg1 = convert_string(argv[3],&arg1len);
799 if (tdb) tdb_close(tdb);