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/>.
25 #ifdef HAVE_LIBREPLACE
27 #include <system/filesys.h>
28 #include <system/time.h>
29 #include <system/locale.h>
35 #include <sys/types.h>
43 static int do_command(void);
46 size_t arg1len, arg2len;
49 TDB_DATA iterate_kbuf;
51 static int disable_mmap;
56 CMD_TRANSACTION_START,
57 CMD_TRANSACTION_COMMIT,
58 CMD_TRANSACTION_CANCEL,
88 COMMAND_TABLE cmd_table[] = {
89 {"create", CMD_CREATE_TDB},
90 {"open", CMD_OPEN_TDB},
92 {"transaction_start", CMD_TRANSACTION_START},
93 {"transaction_commit", CMD_TRANSACTION_COMMIT},
94 {"transaction_cancel", CMD_TRANSACTION_CANCEL},
98 {"insert", CMD_INSERT},
100 {"store", CMD_STORE},
103 {"hexkeys", CMD_HEXKEYS},
104 {"delete", CMD_DELETE},
106 {"list", CMD_LIST_HASH_FREE},
107 {"free", CMD_LIST_FREE},
110 {"speed", CMD_SPEED},
112 {"first", CMD_FIRST},
116 {"check", CMD_CHECK},
123 struct timeval tp1,tp2;
125 static void _start_timer(void)
127 gettimeofday(&tp1,NULL);
130 static double _end_timer(void)
132 gettimeofday(&tp2,NULL);
133 return((tp2.tv_sec - tp1.tv_sec) +
134 (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
137 static void tdb_log(struct tdb_context *tdb,
138 enum tdb_log_level level,
139 enum TDB_ERROR ecode,
143 fprintf(stderr, "tdb:%s:%s:%s\n",
144 tdb_name(tdb), tdb_errorstr(ecode), message);
147 /* a tdb tool for manipulating a tdb database */
149 static struct tdb_context *tdb;
151 static int print_rec(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
152 static int print_key(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
153 static int print_hexkey(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
155 static void print_asc(const char *buf,int len)
159 /* We're probably printing ASCII strings so don't try to display
160 the trailing NULL character. */
162 if (buf[len - 1] == 0)
166 printf("%c",isprint(buf[i])?buf[i]:'.');
169 static void print_data(const char *buf,int len)
175 printf("%02X ",(int)((unsigned char)buf[i]));
177 if (i%8 == 0) printf(" ");
179 print_asc(&buf[i-16],8); printf(" ");
180 print_asc(&buf[i-8],8); printf("\n");
181 if (i<len) printf("[%03X] ",i);
189 if (n>8) printf(" ");
190 while (n--) printf(" ");
194 print_asc(&buf[i-(i%16)],n); printf(" ");
196 if (n>0) print_asc(&buf[i-n],n);
201 static void help(void)
205 " create dbname : create a database\n"
206 " open dbname : open an existing database\n"
207 " openjh dbname : open an existing database (jenkins hash)\n"
208 " transaction_start : start a transaction\n"
209 " transaction_commit : commit a transaction\n"
210 " transaction_cancel : cancel a transaction\n"
211 " erase : erase the database\n"
212 " dump : dump the database as strings\n"
213 " keys : dump the database keys as strings\n"
214 " hexkeys : dump the database keys as hex values\n"
215 " info : print summary info about the database\n"
216 " insert key data : insert a record\n"
217 " move key file : move a record to a destination tdb\n"
218 " store key data : store a record (replace)\n"
219 " show key : show a record by key\n"
220 " delete key : delete a record by key\n"
222 " list : print the database hash table and freelist\n"
223 " free : print the database freelist\n"
225 " check : check the integrity of an opened database\n"
226 " speed : perform speed tests on the database\n"
227 " ! command : execute system command\n"
228 " 1 | first : print the first record\n"
229 " n | next : print the next record\n"
230 " q | quit : terminate\n"
231 " \\n : repeat 'next' command\n"
235 static void terror(enum TDB_ERROR err, const char *why)
237 if (err != TDB_SUCCESS)
238 printf("%s:%s\n", tdb_errorstr(err), why);
243 static void create_tdb(const char *tdbname)
245 union tdb_attribute log_attr;
246 log_attr.base.attr = TDB_ATTRIBUTE_LOG;
247 log_attr.base.next = NULL;
248 log_attr.log.fn = tdb_log;
250 if (tdb) tdb_close(tdb);
251 tdb = tdb_open(tdbname, (disable_mmap?TDB_NOMMAP:0),
252 O_RDWR | O_CREAT | O_TRUNC, 0600, &log_attr);
254 printf("Could not create %s: %s\n", tdbname, strerror(errno));
258 static void open_tdb(const char *tdbname)
260 union tdb_attribute log_attr;
261 log_attr.base.attr = TDB_ATTRIBUTE_LOG;
262 log_attr.base.next = NULL;
263 log_attr.log.fn = tdb_log;
265 if (tdb) tdb_close(tdb);
266 tdb = tdb_open(tdbname, disable_mmap?TDB_NOMMAP:0, O_RDWR, 0600,
269 printf("Could not open %s: %s\n", tdbname, strerror(errno));
273 static void insert_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
276 enum TDB_ERROR ecode;
278 if ((keyname == NULL) || (keylen == 0)) {
279 terror(TDB_SUCCESS, "need key");
283 key.dptr = (unsigned char *)keyname;
285 dbuf.dptr = (unsigned char *)data;
286 dbuf.dsize = datalen;
288 ecode = tdb_store(tdb, key, dbuf, TDB_INSERT);
290 terror(ecode, "insert failed");
294 static void store_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
297 enum TDB_ERROR ecode;
299 if ((keyname == NULL) || (keylen == 0)) {
300 terror(TDB_SUCCESS, "need key");
304 if ((data == NULL) || (datalen == 0)) {
305 terror(TDB_SUCCESS, "need data");
309 key.dptr = (unsigned char *)keyname;
311 dbuf.dptr = (unsigned char *)data;
312 dbuf.dsize = datalen;
314 printf("Storing key:\n");
315 print_rec(tdb, key, dbuf, NULL);
317 ecode = tdb_store(tdb, key, dbuf, TDB_REPLACE);
319 terror(ecode, "store failed");
323 static void show_tdb(char *keyname, size_t keylen)
326 enum TDB_ERROR ecode;
328 if ((keyname == NULL) || (keylen == 0)) {
329 terror(TDB_SUCCESS, "need key");
333 key.dptr = (unsigned char *)keyname;
336 ecode = tdb_fetch(tdb, key, &dbuf);
338 terror(ecode, "fetch failed");
342 print_rec(tdb, key, dbuf, NULL);
347 static void delete_tdb(char *keyname, size_t keylen)
350 enum TDB_ERROR ecode;
352 if ((keyname == NULL) || (keylen == 0)) {
353 terror(TDB_SUCCESS, "need key");
357 key.dptr = (unsigned char *)keyname;
360 ecode = tdb_delete(tdb, key);
362 terror(ecode, "delete failed");
366 static void move_rec(char *keyname, size_t keylen, char* tdbname)
369 struct tdb_context *dst_tdb;
370 enum TDB_ERROR ecode;
372 if ((keyname == NULL) || (keylen == 0)) {
373 terror(TDB_SUCCESS, "need key");
378 terror(TDB_SUCCESS, "need destination tdb name");
382 key.dptr = (unsigned char *)keyname;
385 ecode = tdb_fetch(tdb, key, &dbuf);
387 terror(ecode, "fetch failed");
391 print_rec(tdb, key, dbuf, NULL);
393 dst_tdb = tdb_open(tdbname, 0, O_RDWR, 0600, NULL);
395 terror(TDB_SUCCESS, "unable to open destination tdb");
399 ecode = tdb_store( dst_tdb, key, dbuf, TDB_REPLACE);
401 terror(ecode, "failed to move record");
403 printf("record moved\n");
405 tdb_close( dst_tdb );
408 static int print_rec(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
410 printf("\nkey %d bytes\n", (int)key.dsize);
411 print_asc((const char *)key.dptr, key.dsize);
412 printf("\ndata %d bytes\n", (int)dbuf.dsize);
413 print_data((const char *)dbuf.dptr, dbuf.dsize);
417 static int print_key(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
419 printf("key %d bytes: ", (int)key.dsize);
420 print_asc((const char *)key.dptr, key.dsize);
425 static int print_hexkey(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
427 printf("key %d bytes\n", (int)key.dsize);
428 print_data((const char *)key.dptr, key.dsize);
433 static int total_bytes;
435 static int traverse_fn(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
437 total_bytes += dbuf.dsize;
441 static void info_tdb(void)
443 enum TDB_ERROR ecode;
446 ecode = tdb_summary(tdb, TDB_SUMMARY_HISTOGRAMS, &summary);
449 terror(ecode, "Getting summary");
451 printf("%s", summary);
456 static void speed_tdb(const char *tlimit)
458 unsigned timelimit = tlimit?atoi(tlimit):0;
461 if (timelimit == 0) timelimit = 5;
464 printf("Testing store speed for %u seconds\n", timelimit);
467 long int r = random();
469 key = tdb_mkdata("store test", strlen("store test"));
470 dbuf.dptr = (unsigned char *)&r;
471 dbuf.dsize = sizeof(r);
472 tdb_store(tdb, key, dbuf, TDB_REPLACE);
475 } while (t < timelimit);
476 printf("%10.3f ops/sec\n", ops/t);
479 printf("Testing fetch speed for %u seconds\n", timelimit);
482 long int r = random();
484 key = tdb_mkdata("store test", strlen("store test"));
485 dbuf.dptr = (unsigned char *)&r;
486 dbuf.dsize = sizeof(r);
487 tdb_fetch(tdb, key, &dbuf);
490 } while (t < timelimit);
491 printf("%10.3f ops/sec\n", ops/t);
494 printf("Testing transaction speed for %u seconds\n", timelimit);
497 long int r = random();
499 key = tdb_mkdata("transaction test", strlen("transaction test"));
500 dbuf.dptr = (unsigned char *)&r;
501 dbuf.dsize = sizeof(r);
502 tdb_transaction_start(tdb);
503 tdb_store(tdb, key, dbuf, TDB_REPLACE);
504 tdb_transaction_commit(tdb);
507 } while (t < timelimit);
508 printf("%10.3f ops/sec\n", ops/t);
511 printf("Testing traverse speed for %u seconds\n", timelimit);
514 tdb_traverse(tdb, traverse_fn, NULL);
517 } while (t < timelimit);
518 printf("%10.3f ops/sec\n", ops/t);
521 static void toggle_mmap(void)
523 disable_mmap = !disable_mmap;
525 printf("mmap is disabled\n");
527 printf("mmap is enabled\n");
531 static char *tdb_getline(const char *prompt)
533 static char thisline[1024];
535 fputs(prompt, stdout);
537 p = fgets(thisline, sizeof(thisline)-1, stdin);
538 if (p) p = strchr(p, '\n');
540 return p?thisline:NULL;
543 static int do_delete_fn(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf,
546 return tdb_delete(the_tdb, key);
549 static void first_record(struct tdb_context *the_tdb, TDB_DATA *pkey)
552 enum TDB_ERROR ecode;
553 ecode = tdb_firstkey(the_tdb, pkey);
555 ecode = tdb_fetch(the_tdb, *pkey, &dbuf);
556 if (ecode) terror(ecode, "fetch failed");
558 print_rec(the_tdb, *pkey, dbuf, NULL);
562 static void next_record(struct tdb_context *the_tdb, TDB_DATA *pkey)
565 enum TDB_ERROR ecode;
566 ecode = tdb_nextkey(the_tdb, pkey);
569 ecode = tdb_fetch(the_tdb, *pkey, &dbuf);
571 terror(ecode, "fetch failed");
573 print_rec(the_tdb, *pkey, dbuf, NULL);
576 static void check_db(struct tdb_context *the_tdb)
579 printf("Error: No database opened!\n");
581 if (tdb_check(the_tdb, NULL, NULL) != 0)
582 printf("Integrity check for the opened database failed.\n");
584 printf("Database integrity is OK.\n");
588 static int do_command(void)
590 COMMAND_TABLE *ctp = cmd_table;
591 enum commands mycmd = CMD_HELP;
594 if (cmdname && strlen(cmdname) == 0) {
598 cmd_len = strlen(ctp->name);
599 if (strncmp(ctp->name,cmdname,cmd_len) == 0) {
618 if (system(arg1) == -1) {
619 terror(TDB_SUCCESS, "system() call failed\n");
625 /* all the rest require a open database */
628 terror(TDB_SUCCESS, "database not open");
633 case CMD_TRANSACTION_START:
635 tdb_transaction_start(tdb);
637 case CMD_TRANSACTION_COMMIT:
639 tdb_transaction_commit(tdb);
641 case CMD_TRANSACTION_CANCEL:
643 tdb_transaction_cancel(tdb);
647 tdb_traverse(tdb, do_delete_fn, NULL);
651 tdb_traverse(tdb, print_rec, NULL);
655 insert_tdb(arg1, arg1len,arg2,arg2len);
659 move_rec(arg1,arg1len,arg2);
663 store_tdb(arg1,arg1len,arg2,arg2len);
667 show_tdb(arg1, arg1len);
670 tdb_traverse(tdb, print_key, NULL);
673 tdb_traverse(tdb, print_hexkey, NULL);
677 delete_tdb(arg1,arg1len);
680 case CMD_LIST_HASH_FREE:
684 tdb_printfreelist(tdb);
698 first_record(tdb, &iterate_kbuf);
702 next_record(tdb, &iterate_kbuf);
715 * unhandled commands. cases included here to avoid compiler
725 static char *convert_string(char *instring, size_t *sizep)
731 outp = inp = instring;
736 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
739 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
743 *outp++ = (char)strtol((const char *)temp,NULL,16);
756 int main(int argc, char *argv[])
775 /* Interactive mode */
776 while ((cmdname = tdb_getline("tdb> "))) {
778 if ((arg1 = strchr((const char *)cmdname,' ')) != NULL) {
786 if ((*arg2++ == '\\') && (*arg2 == ' ')) {
791 if (arg1) arg1 = convert_string(arg1,&arg1len);
792 if (arg2) arg2 = convert_string(arg2,&arg2len);
793 if (do_command()) break;
797 arg2 = convert_string(argv[4],&arg2len);
799 arg1 = convert_string(argv[3],&arg1len);
807 if (tdb) tdb_close(tdb);