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;
70 COMMAND_TABLE cmd_table[] = {
71 {"create", CMD_CREATE_TDB},
72 {"open", CMD_OPEN_TDB},
75 {"insert", CMD_INSERT},
80 {"hexkeys", CMD_HEXKEYS},
81 {"delete", CMD_DELETE},
82 {"list", CMD_LIST_HASH_FREE},
83 {"free", CMD_LIST_FREE},
98 struct timeval tp1,tp2;
100 static void _start_timer(void)
102 gettimeofday(&tp1,NULL);
105 static double _end_timer(void)
107 gettimeofday(&tp2,NULL);
108 return((tp2.tv_sec - tp1.tv_sec) +
109 (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
112 /* a tdb tool for manipulating a tdb database */
114 static TDB_CONTEXT *tdb;
116 static int print_rec(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
117 static int print_key(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
118 static int print_hexkey(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
120 static void print_asc(const char *buf,int len)
124 /* We're probably printing ASCII strings so don't try to display
125 the trailing NULL character. */
127 if (buf[len - 1] == 0)
131 printf("%c",isprint(buf[i])?buf[i]:'.');
134 static void print_data(const char *buf,int len)
140 printf("%02X ",(int)((unsigned char)buf[i]));
142 if (i%8 == 0) printf(" ");
144 print_asc(&buf[i-16],8); printf(" ");
145 print_asc(&buf[i-8],8); printf("\n");
146 if (i<len) printf("[%03X] ",i);
154 if (n>8) printf(" ");
155 while (n--) printf(" ");
159 print_asc(&buf[i-(i%16)],n); printf(" ");
161 if (n>0) print_asc(&buf[i-n],n);
166 static void help(void)
170 " create dbname : create a database\n"
171 " open dbname : open an existing database\n"
172 " erase : erase the database\n"
173 " dump : dump the database as strings\n"
174 " keys : dump the database keys as strings\n"
175 " hexkeys : dump the database keys as hex values\n"
176 " info : print summary info about the database\n"
177 " insert key data : insert a record\n"
178 " move key file : move a record to a destination tdb\n"
179 " store key data : store a record (replace)\n"
180 " show key : show a record by key\n"
181 " delete key : delete a record by key\n"
182 " list : print the database hash table and freelist\n"
183 " free : print the database freelist\n"
184 " check : check the integrity of an opened database\n"
185 " ! command : execute system command\n"
186 " 1 | first : print the first record\n"
187 " n | next : print the next record\n"
188 " q | quit : terminate\n"
189 " \\n : repeat 'next' command\n"
193 static void terror(const char *why)
198 static void create_tdb(const char *tdbname)
200 if (tdb) tdb_close(tdb);
201 tdb = tdb_open(tdbname, 0, TDB_CLEAR_IF_FIRST | (disable_mmap?TDB_NOMMAP:0),
202 O_RDWR | O_CREAT | O_TRUNC, 0600);
204 printf("Could not create %s: %s\n", tdbname, strerror(errno));
208 static void open_tdb(const char *tdbname)
210 if (tdb) tdb_close(tdb);
211 tdb = tdb_open(tdbname, 0, disable_mmap?TDB_NOMMAP:0, O_RDWR, 0600);
213 printf("Could not open %s: %s\n", tdbname, strerror(errno));
217 static void insert_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
221 if ((keyname == NULL) || (keylen == 0)) {
226 key.dptr = (unsigned char *)keyname;
228 dbuf.dptr = (unsigned char *)data;
229 dbuf.dsize = datalen;
231 if (tdb_store(tdb, key, dbuf, TDB_INSERT) == -1) {
232 terror("insert failed");
236 static void store_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
240 if ((keyname == NULL) || (keylen == 0)) {
245 if ((data == NULL) || (datalen == 0)) {
250 key.dptr = (unsigned char *)keyname;
252 dbuf.dptr = (unsigned char *)data;
253 dbuf.dsize = datalen;
255 printf("Storing key:\n");
256 print_rec(tdb, key, dbuf, NULL);
258 if (tdb_store(tdb, key, dbuf, TDB_REPLACE) == -1) {
259 terror("store failed");
263 static void show_tdb(char *keyname, size_t keylen)
267 if ((keyname == NULL) || (keylen == 0)) {
272 key.dptr = (unsigned char *)keyname;
275 dbuf = tdb_fetch(tdb, key);
277 terror("fetch failed");
281 print_rec(tdb, key, dbuf, NULL);
288 static void delete_tdb(char *keyname, size_t keylen)
292 if ((keyname == NULL) || (keylen == 0)) {
297 key.dptr = (unsigned char *)keyname;
300 if (tdb_delete(tdb, key) != 0) {
301 terror("delete failed");
305 static void move_rec(char *keyname, size_t keylen, char* tdbname)
308 TDB_CONTEXT *dst_tdb;
310 if ((keyname == NULL) || (keylen == 0)) {
316 terror("need destination tdb name");
320 key.dptr = (unsigned char *)keyname;
323 dbuf = tdb_fetch(tdb, key);
325 terror("fetch failed");
329 print_rec(tdb, key, dbuf, NULL);
331 dst_tdb = tdb_open(tdbname, 0, 0, O_RDWR, 0600);
333 terror("unable to open destination tdb");
337 if ( tdb_store( dst_tdb, key, dbuf, TDB_REPLACE ) == -1 ) {
338 terror("failed to move record");
341 printf("record moved\n");
343 tdb_close( dst_tdb );
348 static int print_rec(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
350 printf("\nkey %d bytes\n", (int)key.dsize);
351 print_asc((const char *)key.dptr, key.dsize);
352 printf("\ndata %d bytes\n", (int)dbuf.dsize);
353 print_data((const char *)dbuf.dptr, dbuf.dsize);
357 static int print_key(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
359 printf("key %d bytes: ", (int)key.dsize);
360 print_asc((const char *)key.dptr, key.dsize);
365 static int print_hexkey(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
367 printf("key %d bytes\n", (int)key.dsize);
368 print_data((const char *)key.dptr, key.dsize);
373 static int total_bytes;
375 static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
377 total_bytes += dbuf.dsize;
381 static void info_tdb(void)
385 if ((count = tdb_traverse(tdb, traverse_fn, NULL)) == -1)
386 printf("Error = %s\n", tdb_errorstr(tdb));
388 printf("%d records totalling %d bytes\n", count, total_bytes);
391 static void speed_tdb(const char *tlimit)
393 unsigned timelimit = tlimit?atoi(tlimit):0;
396 if (timelimit == 0) timelimit = 10;
397 printf("Testing traverse speed for %u seconds\n", timelimit);
399 while ((t=_end_timer()) < timelimit) {
400 tdb_traverse(tdb, traverse_fn, NULL);
401 printf("%10.3f ops/sec\r", (++ops)/t);
406 static void toggle_mmap(void)
408 disable_mmap = !disable_mmap;
410 printf("mmap is disabled\n");
412 printf("mmap is enabled\n");
416 static char *tdb_getline(const char *prompt)
418 static char thisline[1024];
420 fputs(prompt, stdout);
422 p = fgets(thisline, sizeof(thisline)-1, stdin);
423 if (p) p = strchr(p, '\n');
425 return p?thisline:NULL;
428 static int do_delete_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf,
431 return tdb_delete(the_tdb, key);
434 static void first_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
437 *pkey = tdb_firstkey(the_tdb);
439 dbuf = tdb_fetch(the_tdb, *pkey);
440 if (!dbuf.dptr) terror("fetch failed");
442 print_rec(the_tdb, *pkey, dbuf, NULL);
446 static void next_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
449 *pkey = tdb_nextkey(the_tdb, *pkey);
451 dbuf = tdb_fetch(the_tdb, *pkey);
453 terror("fetch failed");
455 print_rec(the_tdb, *pkey, dbuf, NULL);
458 static int test_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
463 static void check_db(TDB_CONTEXT *the_tdb)
467 tdbcount = tdb_traverse(the_tdb, test_fn, NULL);
469 printf("Error: No database opened!\n");
473 printf("Integrity check for the opened database failed.\n");
475 printf("Database integrity is OK and has %d records.\n", tdbcount);
479 static int do_command(void)
481 COMMAND_TABLE *ctp = cmd_table;
482 enum commands mycmd = CMD_HELP;
485 if (cmdname && strlen(cmdname) == 0) {
489 cmd_len = strlen(ctp->name);
490 if (strncmp(ctp->name,cmdname,cmd_len) == 0) {
514 /* all the rest require a open database */
517 terror("database not open");
524 tdb_traverse(tdb, do_delete_fn, NULL);
528 tdb_traverse(tdb, print_rec, NULL);
532 insert_tdb(arg1, arg1len,arg2,arg2len);
536 move_rec(arg1,arg1len,arg2);
540 store_tdb(arg1,arg1len,arg2,arg2len);
544 show_tdb(arg1, arg1len);
547 tdb_traverse(tdb, print_key, NULL);
550 tdb_traverse(tdb, print_hexkey, NULL);
554 delete_tdb(arg1,arg1len);
556 case CMD_LIST_HASH_FREE:
560 tdb_printfreelist(tdb);
573 first_record(tdb, &iterate_kbuf);
577 next_record(tdb, &iterate_kbuf);
590 * unhandled commands. cases included here to avoid compiler
600 static char *convert_string(char *instring, size_t *sizep)
607 outp = inp = instring;
612 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
615 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
619 *outp++ = (char)strtol((const char *)temp,NULL,16);
632 int main(int argc, char *argv[])
651 /* Interactive mode */
652 while ((cmdname = tdb_getline("tdb> "))) {
654 if ((arg1 = strchr((const char *)cmdname,' ')) != NULL) {
662 if ((*arg2++ == '\\') && (*arg2 == ' ')) {
667 if (arg1) arg1 = convert_string(arg1,&arg1len);
668 if (arg2) arg2 = convert_string(arg2,&arg2len);
669 if (do_command()) break;
673 arg2 = convert_string(argv[4],&arg2len);
675 arg1 = convert_string(argv[3],&arg1len);
683 if (tdb) tdb_close(tdb);