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>
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,
78 COMMAND_TABLE cmd_table[] = {
79 {"create", CMD_CREATE_TDB},
80 {"open", CMD_OPEN_TDB},
81 {"transaction_start", CMD_TRANSACTION_START},
82 {"transaction_commit", CMD_TRANSACTION_COMMIT},
83 {"transaction_cancel", CMD_TRANSACTION_CANCEL},
86 {"insert", CMD_INSERT},
91 {"hexkeys", CMD_HEXKEYS},
92 {"delete", CMD_DELETE},
93 {"list", CMD_LIST_HASH_FREE},
94 {"free", CMD_LIST_FREE},
102 {"check", CMD_CHECK},
109 struct timeval tp1,tp2;
111 static void _start_timer(void)
113 gettimeofday(&tp1,NULL);
116 static double _end_timer(void)
118 gettimeofday(&tp2,NULL);
119 return((tp2.tv_sec - tp1.tv_sec) +
120 (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
123 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...)
127 va_start(ap, format);
128 vfprintf(stderr, format, ap);
132 /* a tdb tool for manipulating a tdb database */
134 static TDB_CONTEXT *tdb;
136 static int print_rec(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
137 static int print_key(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
138 static int print_hexkey(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
140 static void print_asc(const char *buf,int len)
144 /* We're probably printing ASCII strings so don't try to display
145 the trailing NULL character. */
147 if (buf[len - 1] == 0)
151 printf("%c",isprint(buf[i])?buf[i]:'.');
154 static void print_data(const char *buf,int len)
160 printf("%02X ",(int)((unsigned char)buf[i]));
162 if (i%8 == 0) printf(" ");
164 print_asc(&buf[i-16],8); printf(" ");
165 print_asc(&buf[i-8],8); printf("\n");
166 if (i<len) printf("[%03X] ",i);
174 if (n>8) printf(" ");
175 while (n--) printf(" ");
179 print_asc(&buf[i-(i%16)],n); printf(" ");
181 if (n>0) print_asc(&buf[i-n],n);
186 static void help(void)
190 " create dbname : create a database\n"
191 " open dbname : open an existing database\n"
192 " transaction_start : start a transaction\n"
193 " transaction_commit : commit a transaction\n"
194 " transaction_cancel : cancel a transaction\n"
195 " erase : erase the database\n"
196 " dump : dump the database as strings\n"
197 " keys : dump the database keys as strings\n"
198 " hexkeys : dump the database keys as hex values\n"
199 " info : print summary info about the database\n"
200 " insert key data : insert a record\n"
201 " move key file : move a record to a destination tdb\n"
202 " store key data : store a record (replace)\n"
203 " show key : show a record by key\n"
204 " delete key : delete a record by key\n"
205 " list : print the database hash table and freelist\n"
206 " free : print the database freelist\n"
207 " check : check the integrity of an opened database\n"
208 " speed : perform speed tests on the database\n"
209 " ! command : execute system command\n"
210 " 1 | first : print the first record\n"
211 " n | next : print the next record\n"
212 " q | quit : terminate\n"
213 " \\n : repeat 'next' command\n"
217 static void terror(const char *why)
222 static void create_tdb(const char *tdbname)
224 struct tdb_logging_context log_ctx;
225 log_ctx.log_fn = tdb_log;
227 if (tdb) tdb_close(tdb);
228 tdb = tdb_open_ex(tdbname, 0, TDB_CLEAR_IF_FIRST | (disable_mmap?TDB_NOMMAP:0),
229 O_RDWR | O_CREAT | O_TRUNC, 0600, &log_ctx, NULL);
231 printf("Could not create %s: %s\n", tdbname, strerror(errno));
235 static void open_tdb(const char *tdbname)
237 struct tdb_logging_context log_ctx;
238 log_ctx.log_fn = tdb_log;
240 if (tdb) tdb_close(tdb);
241 tdb = tdb_open_ex(tdbname, 0, disable_mmap?TDB_NOMMAP:0, O_RDWR, 0600,
244 printf("Could not open %s: %s\n", tdbname, strerror(errno));
248 static void insert_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
252 if ((keyname == NULL) || (keylen == 0)) {
257 key.dptr = (unsigned char *)keyname;
259 dbuf.dptr = (unsigned char *)data;
260 dbuf.dsize = datalen;
262 if (tdb_store(tdb, key, dbuf, TDB_INSERT) == -1) {
263 terror("insert failed");
267 static void store_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
271 if ((keyname == NULL) || (keylen == 0)) {
276 if ((data == NULL) || (datalen == 0)) {
281 key.dptr = (unsigned char *)keyname;
283 dbuf.dptr = (unsigned char *)data;
284 dbuf.dsize = datalen;
286 printf("Storing key:\n");
287 print_rec(tdb, key, dbuf, NULL);
289 if (tdb_store(tdb, key, dbuf, TDB_REPLACE) == -1) {
290 terror("store failed");
294 static void show_tdb(char *keyname, size_t keylen)
298 if ((keyname == NULL) || (keylen == 0)) {
303 key.dptr = (unsigned char *)keyname;
306 dbuf = tdb_fetch(tdb, key);
308 terror("fetch failed");
312 print_rec(tdb, key, dbuf, NULL);
319 static void delete_tdb(char *keyname, size_t keylen)
323 if ((keyname == NULL) || (keylen == 0)) {
328 key.dptr = (unsigned char *)keyname;
331 if (tdb_delete(tdb, key) != 0) {
332 terror("delete failed");
336 static void move_rec(char *keyname, size_t keylen, char* tdbname)
339 TDB_CONTEXT *dst_tdb;
341 if ((keyname == NULL) || (keylen == 0)) {
347 terror("need destination tdb name");
351 key.dptr = (unsigned char *)keyname;
354 dbuf = tdb_fetch(tdb, key);
356 terror("fetch failed");
360 print_rec(tdb, key, dbuf, NULL);
362 dst_tdb = tdb_open(tdbname, 0, 0, O_RDWR, 0600);
364 terror("unable to open destination tdb");
368 if ( tdb_store( dst_tdb, key, dbuf, TDB_REPLACE ) == -1 ) {
369 terror("failed to move record");
372 printf("record moved\n");
374 tdb_close( dst_tdb );
379 static int print_rec(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
381 printf("\nkey %d bytes\n", (int)key.dsize);
382 print_asc((const char *)key.dptr, key.dsize);
383 printf("\ndata %d bytes\n", (int)dbuf.dsize);
384 print_data((const char *)dbuf.dptr, dbuf.dsize);
388 static int print_key(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
390 printf("key %d bytes: ", (int)key.dsize);
391 print_asc((const char *)key.dptr, key.dsize);
396 static int print_hexkey(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
398 printf("key %d bytes\n", (int)key.dsize);
399 print_data((const char *)key.dptr, key.dsize);
404 static int total_bytes;
406 static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
408 total_bytes += dbuf.dsize;
412 static void info_tdb(void)
416 if ((count = tdb_traverse(tdb, traverse_fn, NULL)) == -1)
417 printf("Error = %s\n", tdb_errorstr(tdb));
419 printf("%d records totalling %d bytes\n", count, total_bytes);
422 static void speed_tdb(const char *tlimit)
424 unsigned timelimit = tlimit?atoi(tlimit):0;
427 if (timelimit == 0) timelimit = 5;
430 printf("Testing store speed for %u seconds\n", timelimit);
433 long int r = random();
435 key.dptr = (unsigned char *)"store test";
436 key.dsize = strlen((char *)key.dptr);
437 dbuf.dptr = (unsigned char *)&r;
438 dbuf.dsize = sizeof(r);
439 tdb_store(tdb, key, dbuf, TDB_REPLACE);
442 } while (t < timelimit);
443 printf("%10.3f ops/sec\n", ops/t);
446 printf("Testing fetch speed for %u seconds\n", timelimit);
449 long int r = random();
451 key.dptr = (unsigned char *)"store test";
452 key.dsize = strlen((char *)key.dptr);
453 dbuf.dptr = (unsigned char *)&r;
454 dbuf.dsize = sizeof(r);
458 } while (t < timelimit);
459 printf("%10.3f ops/sec\n", ops/t);
462 printf("Testing transaction speed for %u seconds\n", timelimit);
465 long int r = random();
467 key.dptr = (unsigned char *)"transaction test";
468 key.dsize = strlen((char *)key.dptr);
469 dbuf.dptr = (unsigned char *)&r;
470 dbuf.dsize = sizeof(r);
471 tdb_transaction_start(tdb);
472 tdb_store(tdb, key, dbuf, TDB_REPLACE);
473 tdb_transaction_commit(tdb);
476 } while (t < timelimit);
477 printf("%10.3f ops/sec\n", ops/t);
480 printf("Testing traverse speed for %u seconds\n", timelimit);
483 tdb_traverse(tdb, traverse_fn, NULL);
486 } while (t < timelimit);
487 printf("%10.3f ops/sec\n", ops/t);
490 static void toggle_mmap(void)
492 disable_mmap = !disable_mmap;
494 printf("mmap is disabled\n");
496 printf("mmap is enabled\n");
500 static char *tdb_getline(const char *prompt)
502 static char thisline[1024];
504 fputs(prompt, stdout);
506 p = fgets(thisline, sizeof(thisline)-1, stdin);
507 if (p) p = strchr(p, '\n');
509 return p?thisline:NULL;
512 static int do_delete_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf,
515 return tdb_delete(the_tdb, key);
518 static void first_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
521 *pkey = tdb_firstkey(the_tdb);
523 dbuf = tdb_fetch(the_tdb, *pkey);
524 if (!dbuf.dptr) terror("fetch failed");
526 print_rec(the_tdb, *pkey, dbuf, NULL);
530 static void next_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
533 *pkey = tdb_nextkey(the_tdb, *pkey);
535 dbuf = tdb_fetch(the_tdb, *pkey);
537 terror("fetch failed");
539 print_rec(the_tdb, *pkey, dbuf, NULL);
542 static void check_db(TDB_CONTEXT *the_tdb)
545 printf("Error: No database opened!\n");
547 if (tdb_check(the_tdb, NULL, NULL) == -1)
548 printf("Integrity check for the opened database failed.\n");
550 printf("Database integrity is OK.\n");
554 static int do_command(void)
556 COMMAND_TABLE *ctp = cmd_table;
557 enum commands mycmd = CMD_HELP;
560 if (cmdname && strlen(cmdname) == 0) {
564 cmd_len = strlen(ctp->name);
565 if (strncmp(ctp->name,cmdname,cmd_len) == 0) {
584 if (system(arg1) == -1) {
585 terror("system() call failed\n");
591 /* all the rest require a open database */
594 terror("database not open");
599 case CMD_TRANSACTION_START:
601 tdb_transaction_start(tdb);
603 case CMD_TRANSACTION_COMMIT:
605 tdb_transaction_commit(tdb);
607 case CMD_TRANSACTION_CANCEL:
609 tdb_transaction_cancel(tdb);
613 tdb_traverse(tdb, do_delete_fn, NULL);
617 tdb_traverse(tdb, print_rec, NULL);
621 insert_tdb(arg1, arg1len,arg2,arg2len);
625 move_rec(arg1,arg1len,arg2);
629 store_tdb(arg1,arg1len,arg2,arg2len);
633 show_tdb(arg1, arg1len);
636 tdb_traverse(tdb, print_key, NULL);
639 tdb_traverse(tdb, print_hexkey, NULL);
643 delete_tdb(arg1,arg1len);
645 case CMD_LIST_HASH_FREE:
649 tdb_printfreelist(tdb);
662 first_record(tdb, &iterate_kbuf);
666 next_record(tdb, &iterate_kbuf);
679 * unhandled commands. cases included here to avoid compiler
689 static char *convert_string(char *instring, size_t *sizep)
695 outp = inp = instring;
700 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
703 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
707 *outp++ = (char)strtol((const char *)temp,NULL,16);
720 int main(int argc, char *argv[])
739 /* Interactive mode */
740 while ((cmdname = tdb_getline("tdb> "))) {
742 if ((arg1 = strchr((const char *)cmdname,' ')) != NULL) {
750 if ((*arg2++ == '\\') && (*arg2 == ' ')) {
755 if (arg1) arg1 = convert_string(arg1,&arg1len);
756 if (arg2) arg2 = convert_string(arg2,&arg2len);
757 if (do_command()) break;
761 arg2 = convert_string(argv[4],&arg2len);
763 arg1 = convert_string(argv[3],&arg1len);
771 if (tdb) tdb_close(tdb);