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/tdb2/tdb2.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;
51 CMD_TRANSACTION_START,
52 CMD_TRANSACTION_COMMIT,
53 CMD_TRANSACTION_CANCEL,
84 COMMAND_TABLE cmd_table[] = {
85 {"create", CMD_CREATE_TDB},
86 {"open", CMD_OPEN_TDB},
87 {"openjh", CMD_OPENJH_TDB},
89 {"transaction_start", CMD_TRANSACTION_START},
90 {"transaction_commit", CMD_TRANSACTION_COMMIT},
91 {"transaction_cancel", CMD_TRANSACTION_CANCEL},
95 {"insert", CMD_INSERT},
100 {"hexkeys", CMD_HEXKEYS},
101 {"delete", CMD_DELETE},
103 {"list", CMD_LIST_HASH_FREE},
104 {"free", CMD_LIST_FREE},
107 {"speed", CMD_SPEED},
109 {"first", CMD_FIRST},
113 {"check", CMD_CHECK},
120 struct timeval tp1,tp2;
122 static void _start_timer(void)
124 gettimeofday(&tp1,NULL);
127 static double _end_timer(void)
129 gettimeofday(&tp2,NULL);
130 return((tp2.tv_sec - tp1.tv_sec) +
131 (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
134 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, void *priv, const char *format, ...)
138 va_start(ap, format);
139 vfprintf(stderr, format, ap);
143 /* a tdb tool for manipulating a tdb database */
145 static struct tdb_context *tdb;
147 static int print_rec(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
148 static int print_key(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
149 static int print_hexkey(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
151 static void print_asc(const char *buf,int len)
155 /* We're probably printing ASCII strings so don't try to display
156 the trailing NULL character. */
158 if (buf[len - 1] == 0)
162 printf("%c",isprint(buf[i])?buf[i]:'.');
165 static void print_data(const char *buf,int len)
171 printf("%02X ",(int)((unsigned char)buf[i]));
173 if (i%8 == 0) printf(" ");
175 print_asc(&buf[i-16],8); printf(" ");
176 print_asc(&buf[i-8],8); printf("\n");
177 if (i<len) printf("[%03X] ",i);
185 if (n>8) printf(" ");
186 while (n--) printf(" ");
190 print_asc(&buf[i-(i%16)],n); printf(" ");
192 if (n>0) print_asc(&buf[i-n],n);
197 static void help(void)
201 " create dbname : create a database\n"
202 " open dbname : open an existing database\n"
203 " openjh dbname : open an existing database (jenkins hash)\n"
204 " transaction_start : start a transaction\n"
205 " transaction_commit : commit a transaction\n"
206 " transaction_cancel : cancel a transaction\n"
207 " erase : erase the database\n"
208 " dump : dump the database as strings\n"
209 " keys : dump the database keys as strings\n"
210 " hexkeys : dump the database keys as hex values\n"
211 " info : print summary info about the database\n"
212 " insert key data : insert a record\n"
213 " move key file : move a record to a destination tdb\n"
214 " store key data : store a record (replace)\n"
215 " show key : show a record by key\n"
216 " delete key : delete a record by key\n"
217 " list : print the database hash table and freelist\n"
218 " free : print the database freelist\n"
219 " check : check the integrity of an opened database\n"
220 " speed : perform speed tests on the database\n"
221 " ! command : execute system command\n"
222 " 1 | first : print the first record\n"
223 " n | next : print the next record\n"
224 " q | quit : terminate\n"
225 " \\n : repeat 'next' command\n"
229 static void terror(const char *why)
234 static void create_tdb(const char *tdbname)
236 union tdb_attribute log_attr;
237 log_attr.base.attr = TDB_ATTRIBUTE_LOG;
238 log_attr.base.next = NULL;
239 log_attr.log.log_fn = tdb_log;
241 if (tdb) tdb_close(tdb);
242 tdb = tdb_open(tdbname, (disable_mmap?TDB_NOMMAP:0),
243 O_RDWR | O_CREAT | O_TRUNC, 0600, &log_attr);
245 printf("Could not create %s: %s\n", tdbname, strerror(errno));
249 static uint64_t jenkins_hash(const void *key, size_t len, uint64_t seed,
252 return hash_any(key, len, seed);
255 static void open_tdb(const char *tdbname, tdb_hashfn_t hashfn)
257 union tdb_attribute log_attr, hash_attr;
258 log_attr.base.attr = TDB_ATTRIBUTE_LOG;
259 log_attr.base.next = NULL;
260 log_attr.log.log_fn = tdb_log;
262 hash_attr.base.attr = TDB_ATTRIBUTE_HASH;
263 hash_attr.base.next = &log_attr;
264 hash_attr.hash.hash_fn = hashfn;
266 if (tdb) tdb_close(tdb);
267 tdb = tdb_open(tdbname, disable_mmap?TDB_NOMMAP:0, O_RDWR, 0600,
268 hashfn ? &hash_attr : &log_attr);
270 printf("Could not open %s: %s\n", tdbname, strerror(errno));
274 static void insert_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
278 if ((keyname == NULL) || (keylen == 0)) {
283 key.dptr = (unsigned char *)keyname;
285 dbuf.dptr = (unsigned char *)data;
286 dbuf.dsize = datalen;
288 if (tdb_store(tdb, key, dbuf, TDB_INSERT) == -1) {
289 terror("insert failed");
293 static void store_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
297 if ((keyname == NULL) || (keylen == 0)) {
302 if ((data == NULL) || (datalen == 0)) {
307 key.dptr = (unsigned char *)keyname;
309 dbuf.dptr = (unsigned char *)data;
310 dbuf.dsize = datalen;
312 printf("Storing key:\n");
313 print_rec(tdb, key, dbuf, NULL);
315 if (tdb_store(tdb, key, dbuf, TDB_REPLACE) == -1) {
316 terror("store failed");
320 static void show_tdb(char *keyname, size_t keylen)
324 if ((keyname == NULL) || (keylen == 0)) {
329 key.dptr = (unsigned char *)keyname;
332 dbuf = tdb_fetch(tdb, key);
334 terror("fetch failed");
338 print_rec(tdb, key, dbuf, NULL);
345 static void delete_tdb(char *keyname, size_t keylen)
349 if ((keyname == NULL) || (keylen == 0)) {
354 key.dptr = (unsigned char *)keyname;
357 if (tdb_delete(tdb, key) != 0) {
358 terror("delete failed");
362 static void move_rec(char *keyname, size_t keylen, char* tdbname)
365 struct tdb_context *dst_tdb;
367 if ((keyname == NULL) || (keylen == 0)) {
373 terror("need destination tdb name");
377 key.dptr = (unsigned char *)keyname;
380 dbuf = tdb_fetch(tdb, key);
382 terror("fetch failed");
386 print_rec(tdb, key, dbuf, NULL);
388 dst_tdb = tdb_open(tdbname, 0, O_RDWR, 0600, NULL);
390 terror("unable to open destination tdb");
394 if ( tdb_store( dst_tdb, key, dbuf, TDB_REPLACE ) == -1 ) {
395 terror("failed to move record");
398 printf("record moved\n");
400 tdb_close( dst_tdb );
405 static int print_rec(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
407 printf("\nkey %d bytes\n", (int)key.dsize);
408 print_asc((const char *)key.dptr, key.dsize);
409 printf("\ndata %d bytes\n", (int)dbuf.dsize);
410 print_data((const char *)dbuf.dptr, dbuf.dsize);
414 static int print_key(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
416 printf("key %d bytes: ", (int)key.dsize);
417 print_asc((const char *)key.dptr, key.dsize);
422 static int print_hexkey(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
424 printf("key %d bytes\n", (int)key.dsize);
425 print_data((const char *)key.dptr, key.dsize);
430 static int total_bytes;
432 static int traverse_fn(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
434 total_bytes += dbuf.dsize;
438 static void info_tdb(void)
440 char *summary = tdb_summary(tdb, TDB_SUMMARY_HISTOGRAMS);
443 printf("Error = %s\n", tdb_errorstr(tdb));
445 printf("%s", summary);
450 static void speed_tdb(const char *tlimit)
452 unsigned timelimit = tlimit?atoi(tlimit):0;
455 if (timelimit == 0) timelimit = 5;
458 printf("Testing store speed for %u seconds\n", timelimit);
461 long int r = random();
463 key.dptr = (unsigned char *)"store test";
464 key.dsize = strlen((char *)key.dptr);
465 dbuf.dptr = (unsigned char *)&r;
466 dbuf.dsize = sizeof(r);
467 tdb_store(tdb, key, dbuf, TDB_REPLACE);
470 } while (t < timelimit);
471 printf("%10.3f ops/sec\n", ops/t);
474 printf("Testing fetch speed for %u seconds\n", timelimit);
477 long int r = random();
479 key.dptr = (unsigned char *)"store test";
480 key.dsize = strlen((char *)key.dptr);
481 dbuf.dptr = (unsigned char *)&r;
482 dbuf.dsize = sizeof(r);
486 } while (t < timelimit);
487 printf("%10.3f ops/sec\n", ops/t);
491 printf("Testing transaction speed for %u seconds\n", timelimit);
494 long int r = random();
496 key.dptr = (unsigned char *)"transaction test";
497 key.dsize = strlen((char *)key.dptr);
498 dbuf.dptr = (unsigned char *)&r;
499 dbuf.dsize = sizeof(r);
500 tdb_transaction_start(tdb);
501 tdb_store(tdb, key, dbuf, TDB_REPLACE);
502 tdb_transaction_commit(tdb);
505 } while (t < timelimit);
506 printf("%10.3f ops/sec\n", ops/t);
510 printf("Testing traverse speed for %u seconds\n", timelimit);
513 tdb_traverse(tdb, traverse_fn, NULL);
516 } while (t < timelimit);
517 printf("%10.3f ops/sec\n", ops/t);
520 static void toggle_mmap(void)
522 disable_mmap = !disable_mmap;
524 printf("mmap is disabled\n");
526 printf("mmap is enabled\n");
530 static char *tdb_getline(const char *prompt)
532 static char thisline[1024];
534 fputs(prompt, stdout);
536 p = fgets(thisline, sizeof(thisline)-1, stdin);
537 if (p) p = strchr(p, '\n');
539 return p?thisline:NULL;
542 static int do_delete_fn(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf,
545 return tdb_delete(the_tdb, key);
548 static void first_record(struct tdb_context *the_tdb, TDB_DATA *pkey)
551 *pkey = tdb_firstkey(the_tdb);
553 dbuf = tdb_fetch(the_tdb, *pkey);
554 if (!dbuf.dptr) terror("fetch failed");
556 print_rec(the_tdb, *pkey, dbuf, NULL);
560 static void next_record(struct tdb_context *the_tdb, TDB_DATA *pkey)
563 *pkey = tdb_nextkey(the_tdb, *pkey);
565 dbuf = tdb_fetch(the_tdb, *pkey);
567 terror("fetch failed");
569 print_rec(the_tdb, *pkey, dbuf, NULL);
572 static void check_db(struct tdb_context *the_tdb)
575 printf("Error: No database opened!\n");
577 if (tdb_check(the_tdb, NULL, NULL) == -1)
578 printf("Integrity check for the opened database failed.\n");
580 printf("Database integrity is OK.\n");
584 static int do_command(void)
586 COMMAND_TABLE *ctp = cmd_table;
587 enum commands mycmd = CMD_HELP;
590 if (cmdname && strlen(cmdname) == 0) {
594 cmd_len = strlen(ctp->name);
595 if (strncmp(ctp->name,cmdname,cmd_len) == 0) {
610 open_tdb(arg1, NULL);
614 open_tdb(arg1, jenkins_hash);
618 if (system(arg1) == -1) {
619 terror("system() call failed\n");
625 /* all the rest require a open database */
628 terror("database not open");
634 case CMD_TRANSACTION_START:
636 tdb_transaction_start(tdb);
638 case CMD_TRANSACTION_COMMIT:
640 tdb_transaction_commit(tdb);
642 case CMD_TRANSACTION_CANCEL:
644 tdb_transaction_cancel(tdb);
649 tdb_traverse(tdb, do_delete_fn, NULL);
653 tdb_traverse(tdb, print_rec, NULL);
657 insert_tdb(arg1, arg1len,arg2,arg2len);
661 move_rec(arg1,arg1len,arg2);
665 store_tdb(arg1,arg1len,arg2,arg2len);
669 show_tdb(arg1, arg1len);
672 tdb_traverse(tdb, print_key, NULL);
675 tdb_traverse(tdb, print_hexkey, NULL);
679 delete_tdb(arg1,arg1len);
682 case CMD_LIST_HASH_FREE:
686 tdb_printfreelist(tdb);
700 first_record(tdb, &iterate_kbuf);
704 next_record(tdb, &iterate_kbuf);
718 * unhandled commands. cases included here to avoid compiler
728 static char *convert_string(char *instring, size_t *sizep)
734 outp = inp = instring;
739 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
742 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
746 *outp++ = (char)strtol((const char *)temp,NULL,16);
759 int main(int argc, char *argv[])
778 /* Interactive mode */
779 while ((cmdname = tdb_getline("tdb> "))) {
781 if ((arg1 = strchr((const char *)cmdname,' ')) != NULL) {
789 if ((*arg2++ == '\\') && (*arg2 == ' ')) {
794 if (arg1) arg1 = convert_string(arg1,&arg1len);
795 if (arg2) arg2 = convert_string(arg2,&arg2len);
796 if (do_command()) break;
800 arg2 = convert_string(argv[4],&arg2len);
802 arg1 = convert_string(argv[3],&arg1len);
810 if (tdb) tdb_close(tdb);