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/>.
24 #include "system/locale.h"
25 #include "system/time.h"
26 #include "system/filesys.h"
27 #include "system/wait.h"
30 static int do_command(void);
33 size_t arg1len, arg2len;
36 TDB_DATA iterate_kbuf;
38 static int disable_mmap;
43 CMD_TRANSACTION_START,
44 CMD_TRANSACTION_COMMIT,
45 CMD_TRANSACTION_CANCEL,
73 COMMAND_TABLE cmd_table[] = {
74 {"create", CMD_CREATE_TDB},
75 {"open", CMD_OPEN_TDB},
76 {"transaction_start", CMD_TRANSACTION_START},
77 {"transaction_commit", CMD_TRANSACTION_COMMIT},
78 {"transaction_cancel", CMD_TRANSACTION_CANCEL},
81 {"insert", CMD_INSERT},
86 {"hexkeys", CMD_HEXKEYS},
87 {"delete", CMD_DELETE},
88 {"list", CMD_LIST_HASH_FREE},
89 {"free", CMD_LIST_FREE},
104 struct timeval tp1,tp2;
106 static void _start_timer(void)
108 gettimeofday(&tp1,NULL);
111 static double _end_timer(void)
113 gettimeofday(&tp2,NULL);
114 return((tp2.tv_sec - tp1.tv_sec) +
115 (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
118 /* a tdb tool for manipulating a tdb database */
120 static TDB_CONTEXT *tdb;
122 static int print_rec(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
123 static int print_key(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
124 static int print_hexkey(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
126 static void print_asc(const char *buf,int len)
130 /* We're probably printing ASCII strings so don't try to display
131 the trailing NULL character. */
133 if (buf[len - 1] == 0)
137 printf("%c",isprint(buf[i])?buf[i]:'.');
140 static void print_data(const char *buf,int len)
146 printf("%02X ",(int)((unsigned char)buf[i]));
148 if (i%8 == 0) printf(" ");
150 print_asc(&buf[i-16],8); printf(" ");
151 print_asc(&buf[i-8],8); printf("\n");
152 if (i<len) printf("[%03X] ",i);
160 if (n>8) printf(" ");
161 while (n--) printf(" ");
165 print_asc(&buf[i-(i%16)],n); printf(" ");
167 if (n>0) print_asc(&buf[i-n],n);
172 static void help(void)
176 " create dbname : create a database\n"
177 " open dbname : open an existing database\n"
178 " transaction_start : start a transaction\n"
179 " transaction_commit : commit a transaction\n"
180 " transaction_cancel : cancel a transaction\n"
181 " erase : erase the database\n"
182 " dump : dump the database as strings\n"
183 " keys : dump the database keys as strings\n"
184 " hexkeys : dump the database keys as hex values\n"
185 " info : print summary info about the database\n"
186 " insert key data : insert a record\n"
187 " move key file : move a record to a destination tdb\n"
188 " store key data : store a record (replace)\n"
189 " show key : show a record by key\n"
190 " delete key : delete a record by key\n"
191 " list : print the database hash table and freelist\n"
192 " free : print the database freelist\n"
193 " check : check the integrity of an opened database\n"
194 " speed : perform speed tests on the database\n"
195 " ! command : execute system command\n"
196 " 1 | first : print the first record\n"
197 " n | next : print the next record\n"
198 " q | quit : terminate\n"
199 " \\n : repeat 'next' command\n"
203 static void terror(const char *why)
208 static void create_tdb(const char *tdbname)
210 if (tdb) tdb_close(tdb);
211 tdb = tdb_open(tdbname, 0, TDB_CLEAR_IF_FIRST | (disable_mmap?TDB_NOMMAP:0),
212 O_RDWR | O_CREAT | O_TRUNC, 0600);
214 printf("Could not create %s: %s\n", tdbname, strerror(errno));
218 static void open_tdb(const char *tdbname)
220 if (tdb) tdb_close(tdb);
221 tdb = tdb_open(tdbname, 0, disable_mmap?TDB_NOMMAP:0, O_RDWR, 0600);
223 printf("Could not open %s: %s\n", tdbname, strerror(errno));
227 static void insert_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
231 if ((keyname == NULL) || (keylen == 0)) {
236 key.dptr = (unsigned char *)keyname;
238 dbuf.dptr = (unsigned char *)data;
239 dbuf.dsize = datalen;
241 if (tdb_store(tdb, key, dbuf, TDB_INSERT) == -1) {
242 terror("insert failed");
246 static void store_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
250 if ((keyname == NULL) || (keylen == 0)) {
255 if ((data == NULL) || (datalen == 0)) {
260 key.dptr = (unsigned char *)keyname;
262 dbuf.dptr = (unsigned char *)data;
263 dbuf.dsize = datalen;
265 printf("Storing key:\n");
266 print_rec(tdb, key, dbuf, NULL);
268 if (tdb_store(tdb, key, dbuf, TDB_REPLACE) == -1) {
269 terror("store failed");
273 static void show_tdb(char *keyname, size_t keylen)
277 if ((keyname == NULL) || (keylen == 0)) {
282 key.dptr = (unsigned char *)keyname;
285 dbuf = tdb_fetch(tdb, key);
287 terror("fetch failed");
291 print_rec(tdb, key, dbuf, NULL);
298 static void delete_tdb(char *keyname, size_t keylen)
302 if ((keyname == NULL) || (keylen == 0)) {
307 key.dptr = (unsigned char *)keyname;
310 if (tdb_delete(tdb, key) != 0) {
311 terror("delete failed");
315 static void move_rec(char *keyname, size_t keylen, char* tdbname)
318 TDB_CONTEXT *dst_tdb;
320 if ((keyname == NULL) || (keylen == 0)) {
326 terror("need destination tdb name");
330 key.dptr = (unsigned char *)keyname;
333 dbuf = tdb_fetch(tdb, key);
335 terror("fetch failed");
339 print_rec(tdb, key, dbuf, NULL);
341 dst_tdb = tdb_open(tdbname, 0, 0, O_RDWR, 0600);
343 terror("unable to open destination tdb");
347 if ( tdb_store( dst_tdb, key, dbuf, TDB_REPLACE ) == -1 ) {
348 terror("failed to move record");
351 printf("record moved\n");
353 tdb_close( dst_tdb );
358 static int print_rec(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
360 printf("\nkey %d bytes\n", (int)key.dsize);
361 print_asc((const char *)key.dptr, key.dsize);
362 printf("\ndata %d bytes\n", (int)dbuf.dsize);
363 print_data((const char *)dbuf.dptr, dbuf.dsize);
367 static int print_key(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
369 printf("key %d bytes: ", (int)key.dsize);
370 print_asc((const char *)key.dptr, key.dsize);
375 static int print_hexkey(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
377 printf("key %d bytes\n", (int)key.dsize);
378 print_data((const char *)key.dptr, key.dsize);
383 static int total_bytes;
385 static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
387 total_bytes += dbuf.dsize;
391 static void info_tdb(void)
395 if ((count = tdb_traverse(tdb, traverse_fn, NULL)) == -1)
396 printf("Error = %s\n", tdb_errorstr(tdb));
398 printf("%d records totalling %d bytes\n", count, total_bytes);
401 static void speed_tdb(const char *tlimit)
403 unsigned timelimit = tlimit?atoi(tlimit):0;
406 if (timelimit == 0) timelimit = 5;
409 printf("Testing store speed for %u seconds\n", timelimit);
412 long int r = random();
414 key.dptr = (unsigned char *)"store test";
415 key.dsize = strlen((char *)key.dptr);
416 dbuf.dptr = (unsigned char *)&r;
417 dbuf.dsize = sizeof(r);
418 tdb_store(tdb, key, dbuf, TDB_REPLACE);
421 } while (t < timelimit);
422 printf("%10.3f ops/sec\n", ops/t);
425 printf("Testing fetch speed for %u seconds\n", timelimit);
428 long int r = random();
430 key.dptr = (unsigned char *)"store test";
431 key.dsize = strlen((char *)key.dptr);
432 dbuf.dptr = (unsigned char *)&r;
433 dbuf.dsize = sizeof(r);
437 } while (t < timelimit);
438 printf("%10.3f ops/sec\n", ops/t);
441 printf("Testing transaction speed for %u seconds\n", timelimit);
444 long int r = random();
446 key.dptr = (unsigned char *)"transaction test";
447 key.dsize = strlen((char *)key.dptr);
448 dbuf.dptr = (unsigned char *)&r;
449 dbuf.dsize = sizeof(r);
450 tdb_transaction_start(tdb);
451 tdb_store(tdb, key, dbuf, TDB_REPLACE);
452 tdb_transaction_commit(tdb);
455 } while (t < timelimit);
456 printf("%10.3f ops/sec\n", ops/t);
459 printf("Testing traverse speed for %u seconds\n", timelimit);
462 tdb_traverse(tdb, traverse_fn, NULL);
465 } while (t < timelimit);
466 printf("%10.3f ops/sec\n", ops/t);
469 static void toggle_mmap(void)
471 disable_mmap = !disable_mmap;
473 printf("mmap is disabled\n");
475 printf("mmap is enabled\n");
479 static char *tdb_getline(const char *prompt)
481 static char thisline[1024];
483 fputs(prompt, stdout);
485 p = fgets(thisline, sizeof(thisline)-1, stdin);
486 if (p) p = strchr(p, '\n');
488 return p?thisline:NULL;
491 static int do_delete_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf,
494 return tdb_delete(the_tdb, key);
497 static void first_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
500 *pkey = tdb_firstkey(the_tdb);
502 dbuf = tdb_fetch(the_tdb, *pkey);
503 if (!dbuf.dptr) terror("fetch failed");
505 print_rec(the_tdb, *pkey, dbuf, NULL);
509 static void next_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
512 *pkey = tdb_nextkey(the_tdb, *pkey);
514 dbuf = tdb_fetch(the_tdb, *pkey);
516 terror("fetch failed");
518 print_rec(the_tdb, *pkey, dbuf, NULL);
521 static int test_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
526 static void check_db(TDB_CONTEXT *the_tdb)
530 tdbcount = tdb_traverse(the_tdb, test_fn, NULL);
532 printf("Error: No database opened!\n");
536 printf("Integrity check for the opened database failed.\n");
538 printf("Database integrity is OK and has %d records.\n", tdbcount);
542 static int do_command(void)
544 COMMAND_TABLE *ctp = cmd_table;
545 enum commands mycmd = CMD_HELP;
548 if (cmdname && strlen(cmdname) == 0) {
552 cmd_len = strlen(ctp->name);
553 if (strncmp(ctp->name,cmdname,cmd_len) == 0) {
572 if (system(arg1) == -1) {
573 terror("system() call failed\n");
579 /* all the rest require a open database */
582 terror("database not open");
587 case CMD_TRANSACTION_START:
589 tdb_transaction_start(tdb);
591 case CMD_TRANSACTION_COMMIT:
593 tdb_transaction_commit(tdb);
595 case CMD_TRANSACTION_CANCEL:
597 tdb_transaction_cancel(tdb);
601 tdb_traverse(tdb, do_delete_fn, NULL);
605 tdb_traverse(tdb, print_rec, NULL);
609 insert_tdb(arg1, arg1len,arg2,arg2len);
613 move_rec(arg1,arg1len,arg2);
617 store_tdb(arg1,arg1len,arg2,arg2len);
621 show_tdb(arg1, arg1len);
624 tdb_traverse(tdb, print_key, NULL);
627 tdb_traverse(tdb, print_hexkey, NULL);
631 delete_tdb(arg1,arg1len);
633 case CMD_LIST_HASH_FREE:
637 tdb_printfreelist(tdb);
650 first_record(tdb, &iterate_kbuf);
654 next_record(tdb, &iterate_kbuf);
667 * unhandled commands. cases included here to avoid compiler
677 static char *convert_string(char *instring, size_t *sizep)
683 outp = inp = instring;
688 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
691 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
695 *outp++ = (char)strtol((const char *)temp,NULL,16);
708 int main(int argc, char *argv[])
727 /* Interactive mode */
728 while ((cmdname = tdb_getline("tdb> "))) {
730 if ((arg1 = strchr((const char *)cmdname,' ')) != NULL) {
738 if ((*arg2++ == '\\') && (*arg2 == ' ')) {
743 if (arg1) arg1 = convert_string(arg1,&arg1len);
744 if (arg2) arg2 = convert_string(arg2,&arg2len);
745 if (do_command()) break;
749 arg2 = convert_string(argv[4],&arg2len);
751 arg1 = convert_string(argv[3],&arg1len);
759 if (tdb) tdb_close(tdb);