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>
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, enum tdb_log_level level,
130 void *priv, const char *message)
132 fputs(message, stderr);
135 /* a tdb tool for manipulating a tdb database */
137 static struct tdb_context *tdb;
139 static int print_rec(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
140 static int print_key(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
141 static int print_hexkey(struct 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"
210 " list : print the database hash table and freelist\n"
211 " free : print the database freelist\n"
213 " check : check the integrity of an opened database\n"
214 " speed : perform speed tests on the database\n"
215 " ! command : execute system command\n"
216 " 1 | first : print the first record\n"
217 " n | next : print the next record\n"
218 " q | quit : terminate\n"
219 " \\n : repeat 'next' command\n"
223 static void terror(enum TDB_ERROR err, const char *why)
225 if (err != TDB_SUCCESS)
226 printf("%s:%s\n", tdb_errorstr(err), why);
231 static void create_tdb(const char *tdbname)
233 union tdb_attribute log_attr;
234 log_attr.base.attr = TDB_ATTRIBUTE_LOG;
235 log_attr.base.next = NULL;
236 log_attr.log.log_fn = tdb_log;
238 if (tdb) tdb_close(tdb);
239 tdb = tdb_open(tdbname, (disable_mmap?TDB_NOMMAP:0),
240 O_RDWR | O_CREAT | O_TRUNC, 0600, &log_attr);
242 printf("Could not create %s: %s\n", tdbname, strerror(errno));
246 static void open_tdb(const char *tdbname)
248 union tdb_attribute log_attr;
249 log_attr.base.attr = TDB_ATTRIBUTE_LOG;
250 log_attr.base.next = NULL;
251 log_attr.log.log_fn = tdb_log;
253 if (tdb) tdb_close(tdb);
254 tdb = tdb_open(tdbname, disable_mmap?TDB_NOMMAP:0, O_RDWR, 0600,
257 printf("Could not open %s: %s\n", tdbname, strerror(errno));
261 static void insert_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
264 enum TDB_ERROR ecode;
266 if ((keyname == NULL) || (keylen == 0)) {
267 terror(TDB_SUCCESS, "need key");
271 key.dptr = (unsigned char *)keyname;
273 dbuf.dptr = (unsigned char *)data;
274 dbuf.dsize = datalen;
276 ecode = tdb_store(tdb, key, dbuf, TDB_INSERT);
278 terror(ecode, "insert failed");
282 static void store_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
285 enum TDB_ERROR ecode;
287 if ((keyname == NULL) || (keylen == 0)) {
288 terror(TDB_SUCCESS, "need key");
292 if ((data == NULL) || (datalen == 0)) {
293 terror(TDB_SUCCESS, "need data");
297 key.dptr = (unsigned char *)keyname;
299 dbuf.dptr = (unsigned char *)data;
300 dbuf.dsize = datalen;
302 printf("Storing key:\n");
303 print_rec(tdb, key, dbuf, NULL);
305 ecode = tdb_store(tdb, key, dbuf, TDB_REPLACE);
307 terror(ecode, "store failed");
311 static void show_tdb(char *keyname, size_t keylen)
314 enum TDB_ERROR ecode;
316 if ((keyname == NULL) || (keylen == 0)) {
317 terror(TDB_SUCCESS, "need key");
321 key.dptr = (unsigned char *)keyname;
324 ecode = tdb_fetch(tdb, key, &dbuf);
326 terror(ecode, "fetch failed");
330 print_rec(tdb, key, dbuf, NULL);
335 static void delete_tdb(char *keyname, size_t keylen)
338 enum TDB_ERROR ecode;
340 if ((keyname == NULL) || (keylen == 0)) {
341 terror(TDB_SUCCESS, "need key");
345 key.dptr = (unsigned char *)keyname;
348 ecode = tdb_delete(tdb, key);
350 terror(ecode, "delete failed");
354 static void move_rec(char *keyname, size_t keylen, char* tdbname)
357 struct tdb_context *dst_tdb;
358 enum TDB_ERROR ecode;
360 if ((keyname == NULL) || (keylen == 0)) {
361 terror(TDB_SUCCESS, "need key");
366 terror(TDB_SUCCESS, "need destination tdb name");
370 key.dptr = (unsigned char *)keyname;
373 ecode = tdb_fetch(tdb, key, &dbuf);
375 terror(ecode, "fetch failed");
379 print_rec(tdb, key, dbuf, NULL);
381 dst_tdb = tdb_open(tdbname, 0, O_RDWR, 0600, NULL);
383 terror(TDB_SUCCESS, "unable to open destination tdb");
387 ecode = tdb_store( dst_tdb, key, dbuf, TDB_REPLACE);
389 terror(ecode, "failed to move record");
391 printf("record moved\n");
393 tdb_close( dst_tdb );
396 static int print_rec(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
398 printf("\nkey %d bytes\n", (int)key.dsize);
399 print_asc((const char *)key.dptr, key.dsize);
400 printf("\ndata %d bytes\n", (int)dbuf.dsize);
401 print_data((const char *)dbuf.dptr, dbuf.dsize);
405 static int print_key(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
407 printf("key %d bytes: ", (int)key.dsize);
408 print_asc((const char *)key.dptr, key.dsize);
413 static int print_hexkey(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
415 printf("key %d bytes\n", (int)key.dsize);
416 print_data((const char *)key.dptr, key.dsize);
421 static int total_bytes;
423 static int traverse_fn(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
425 total_bytes += dbuf.dsize;
429 static void info_tdb(void)
431 enum TDB_ERROR ecode;
434 ecode = tdb_summary(tdb, TDB_SUMMARY_HISTOGRAMS, &summary);
437 terror(ecode, "Getting summary");
439 printf("%s", summary);
444 static void speed_tdb(const char *tlimit)
446 unsigned timelimit = tlimit?atoi(tlimit):0;
449 if (timelimit == 0) timelimit = 5;
452 printf("Testing store speed for %u seconds\n", timelimit);
455 long int r = random();
457 key = tdb_mkdata("store test", strlen("store test"));
458 dbuf.dptr = (unsigned char *)&r;
459 dbuf.dsize = sizeof(r);
460 tdb_store(tdb, key, dbuf, TDB_REPLACE);
463 } while (t < timelimit);
464 printf("%10.3f ops/sec\n", ops/t);
467 printf("Testing fetch speed for %u seconds\n", timelimit);
470 long int r = random();
472 key = tdb_mkdata("store test", strlen("store test"));
473 dbuf.dptr = (unsigned char *)&r;
474 dbuf.dsize = sizeof(r);
475 tdb_fetch(tdb, key, &dbuf);
478 } while (t < timelimit);
479 printf("%10.3f ops/sec\n", ops/t);
482 printf("Testing transaction speed for %u seconds\n", timelimit);
485 long int r = random();
487 key = tdb_mkdata("transaction test", strlen("transaction test"));
488 dbuf.dptr = (unsigned char *)&r;
489 dbuf.dsize = sizeof(r);
490 tdb_transaction_start(tdb);
491 tdb_store(tdb, key, dbuf, TDB_REPLACE);
492 tdb_transaction_commit(tdb);
495 } while (t < timelimit);
496 printf("%10.3f ops/sec\n", ops/t);
499 printf("Testing traverse speed for %u seconds\n", timelimit);
502 tdb_traverse(tdb, traverse_fn, NULL);
505 } while (t < timelimit);
506 printf("%10.3f ops/sec\n", ops/t);
509 static void toggle_mmap(void)
511 disable_mmap = !disable_mmap;
513 printf("mmap is disabled\n");
515 printf("mmap is enabled\n");
519 static char *tdb_getline(const char *prompt)
521 static char thisline[1024];
523 fputs(prompt, stdout);
525 p = fgets(thisline, sizeof(thisline)-1, stdin);
526 if (p) p = strchr(p, '\n');
528 return p?thisline:NULL;
531 static int do_delete_fn(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf,
534 return tdb_delete(the_tdb, key);
537 static void first_record(struct tdb_context *the_tdb, TDB_DATA *pkey)
540 enum TDB_ERROR ecode;
541 ecode = tdb_firstkey(the_tdb, pkey);
543 ecode = tdb_fetch(the_tdb, *pkey, &dbuf);
544 if (ecode) terror(ecode, "fetch failed");
546 print_rec(the_tdb, *pkey, dbuf, NULL);
550 static void next_record(struct tdb_context *the_tdb, TDB_DATA *pkey)
553 enum TDB_ERROR ecode;
554 ecode = tdb_nextkey(the_tdb, pkey);
557 ecode = tdb_fetch(the_tdb, *pkey, &dbuf);
559 terror(ecode, "fetch failed");
561 print_rec(the_tdb, *pkey, dbuf, NULL);
564 static void check_db(struct tdb_context *the_tdb)
567 printf("Error: No database opened!\n");
569 if (tdb_check(the_tdb, NULL, NULL) != 0)
570 printf("Integrity check for the opened database failed.\n");
572 printf("Database integrity is OK.\n");
576 static int do_command(void)
578 COMMAND_TABLE *ctp = cmd_table;
579 enum commands mycmd = CMD_HELP;
582 if (cmdname && strlen(cmdname) == 0) {
586 cmd_len = strlen(ctp->name);
587 if (strncmp(ctp->name,cmdname,cmd_len) == 0) {
606 if (system(arg1) == -1) {
607 terror(TDB_SUCCESS, "system() call failed\n");
613 /* all the rest require a open database */
616 terror(TDB_SUCCESS, "database not open");
621 case CMD_TRANSACTION_START:
623 tdb_transaction_start(tdb);
625 case CMD_TRANSACTION_COMMIT:
627 tdb_transaction_commit(tdb);
629 case CMD_TRANSACTION_CANCEL:
631 tdb_transaction_cancel(tdb);
635 tdb_traverse(tdb, do_delete_fn, NULL);
639 tdb_traverse(tdb, print_rec, NULL);
643 insert_tdb(arg1, arg1len,arg2,arg2len);
647 move_rec(arg1,arg1len,arg2);
651 store_tdb(arg1,arg1len,arg2,arg2len);
655 show_tdb(arg1, arg1len);
658 tdb_traverse(tdb, print_key, NULL);
661 tdb_traverse(tdb, print_hexkey, NULL);
665 delete_tdb(arg1,arg1len);
668 case CMD_LIST_HASH_FREE:
672 tdb_printfreelist(tdb);
686 first_record(tdb, &iterate_kbuf);
690 next_record(tdb, &iterate_kbuf);
703 * unhandled commands. cases included here to avoid compiler
713 static char *convert_string(char *instring, size_t *sizep)
719 outp = inp = instring;
724 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
727 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
731 *outp++ = (char)strtol((const char *)temp,NULL,16);
744 int main(int argc, char *argv[])
763 /* Interactive mode */
764 while ((cmdname = tdb_getline("tdb> "))) {
766 if ((arg1 = strchr((const char *)cmdname,' ')) != NULL) {
774 if ((*arg2++ == '\\') && (*arg2 == ' ')) {
779 if (arg1) arg1 = convert_string(arg1,&arg1len);
780 if (arg2) arg2 = convert_string(arg2,&arg2len);
781 if (do_command()) break;
785 arg2 = convert_string(argv[4],&arg2len);
787 arg1 = convert_string(argv[3],&arg1len);
795 if (tdb) tdb_close(tdb);