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>
24 #include <ccan/hash/hash.h>
29 #include <sys/types.h>
36 static int do_command(void);
39 size_t arg1len, arg2len;
42 TDB_DATA iterate_kbuf;
44 static int disable_mmap;
49 CMD_TRANSACTION_START,
50 CMD_TRANSACTION_COMMIT,
51 CMD_TRANSACTION_CANCEL,
81 COMMAND_TABLE cmd_table[] = {
82 {"create", CMD_CREATE_TDB},
83 {"open", CMD_OPEN_TDB},
85 {"transaction_start", CMD_TRANSACTION_START},
86 {"transaction_commit", CMD_TRANSACTION_COMMIT},
87 {"transaction_cancel", CMD_TRANSACTION_CANCEL},
91 {"insert", CMD_INSERT},
96 {"hexkeys", CMD_HEXKEYS},
97 {"delete", CMD_DELETE},
99 {"list", CMD_LIST_HASH_FREE},
100 {"free", CMD_LIST_FREE},
103 {"speed", CMD_SPEED},
105 {"first", CMD_FIRST},
109 {"check", CMD_CHECK},
116 struct timeval tp1,tp2;
118 static void _start_timer(void)
120 gettimeofday(&tp1,NULL);
123 static double _end_timer(void)
125 gettimeofday(&tp2,NULL);
126 return((tp2.tv_sec - tp1.tv_sec) +
127 (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
130 static void tdb_log(struct tdb_context *tdb, enum tdb_log_level level,
131 void *priv, const char *message)
133 fputs(message, stderr);
136 /* a tdb tool for manipulating a tdb database */
138 static struct tdb_context *tdb;
140 static int print_rec(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
141 static int print_key(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
142 static int print_hexkey(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
144 static void print_asc(const char *buf,int len)
148 /* We're probably printing ASCII strings so don't try to display
149 the trailing NULL character. */
151 if (buf[len - 1] == 0)
155 printf("%c",isprint(buf[i])?buf[i]:'.');
158 static void print_data(const char *buf,int len)
164 printf("%02X ",(int)((unsigned char)buf[i]));
166 if (i%8 == 0) printf(" ");
168 print_asc(&buf[i-16],8); printf(" ");
169 print_asc(&buf[i-8],8); printf("\n");
170 if (i<len) printf("[%03X] ",i);
178 if (n>8) printf(" ");
179 while (n--) printf(" ");
183 print_asc(&buf[i-(i%16)],n); printf(" ");
185 if (n>0) print_asc(&buf[i-n],n);
190 static void help(void)
194 " create dbname : create a database\n"
195 " open dbname : open an existing database\n"
196 " openjh dbname : open an existing database (jenkins hash)\n"
197 " transaction_start : start a transaction\n"
198 " transaction_commit : commit a transaction\n"
199 " transaction_cancel : cancel a transaction\n"
200 " erase : erase the database\n"
201 " dump : dump the database as strings\n"
202 " keys : dump the database keys as strings\n"
203 " hexkeys : dump the database keys as hex values\n"
204 " info : print summary info about the database\n"
205 " insert key data : insert a record\n"
206 " move key file : move a record to a destination tdb\n"
207 " store key data : store a record (replace)\n"
208 " show key : show a record by key\n"
209 " 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"
212 " check : check the integrity of an opened database\n"
213 " speed : perform speed tests on the database\n"
214 " ! command : execute system command\n"
215 " 1 | first : print the first record\n"
216 " n | next : print the next record\n"
217 " q | quit : terminate\n"
218 " \\n : repeat 'next' command\n"
222 static void terror(enum TDB_ERROR err, const char *why)
224 if (err != TDB_SUCCESS)
225 printf("%s:%s\n", tdb_errorstr(err), why);
230 static void create_tdb(const char *tdbname)
232 union tdb_attribute log_attr;
233 log_attr.base.attr = TDB_ATTRIBUTE_LOG;
234 log_attr.base.next = NULL;
235 log_attr.log.log_fn = tdb_log;
237 if (tdb) tdb_close(tdb);
238 tdb = tdb_open(tdbname, (disable_mmap?TDB_NOMMAP:0),
239 O_RDWR | O_CREAT | O_TRUNC, 0600, &log_attr);
241 printf("Could not create %s: %s\n", tdbname, strerror(errno));
245 static uint64_t jenkins_hash(const void *key, size_t len, uint64_t seed,
248 return hash_any(key, len, seed);
251 static void open_tdb(const char *tdbname)
253 union tdb_attribute log_attr;
254 log_attr.base.attr = TDB_ATTRIBUTE_LOG;
255 log_attr.base.next = NULL;
256 log_attr.log.log_fn = tdb_log;
258 if (tdb) tdb_close(tdb);
259 tdb = tdb_open(tdbname, disable_mmap?TDB_NOMMAP:0, O_RDWR, 0600,
262 printf("Could not open %s: %s\n", tdbname, strerror(errno));
266 static void insert_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
269 enum TDB_ERROR ecode;
271 if ((keyname == NULL) || (keylen == 0)) {
272 terror(TDB_SUCCESS, "need key");
276 key.dptr = (unsigned char *)keyname;
278 dbuf.dptr = (unsigned char *)data;
279 dbuf.dsize = datalen;
281 ecode = tdb_store(tdb, key, dbuf, TDB_INSERT);
283 terror(ecode, "insert failed");
287 static void store_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
290 enum TDB_ERROR ecode;
292 if ((keyname == NULL) || (keylen == 0)) {
293 terror(TDB_SUCCESS, "need key");
297 if ((data == NULL) || (datalen == 0)) {
298 terror(TDB_SUCCESS, "need data");
302 key.dptr = (unsigned char *)keyname;
304 dbuf.dptr = (unsigned char *)data;
305 dbuf.dsize = datalen;
307 printf("Storing key:\n");
308 print_rec(tdb, key, dbuf, NULL);
310 ecode = tdb_store(tdb, key, dbuf, TDB_REPLACE);
312 terror(ecode, "store failed");
316 static void show_tdb(char *keyname, size_t keylen)
319 enum TDB_ERROR ecode;
321 if ((keyname == NULL) || (keylen == 0)) {
322 terror(TDB_SUCCESS, "need key");
326 key.dptr = (unsigned char *)keyname;
329 ecode = tdb_fetch(tdb, key, &dbuf);
331 terror(ecode, "fetch failed");
335 print_rec(tdb, key, dbuf, NULL);
340 static void delete_tdb(char *keyname, size_t keylen)
343 enum TDB_ERROR ecode;
345 if ((keyname == NULL) || (keylen == 0)) {
346 terror(TDB_SUCCESS, "need key");
350 key.dptr = (unsigned char *)keyname;
353 ecode = tdb_delete(tdb, key);
355 terror(ecode, "delete failed");
359 static void move_rec(char *keyname, size_t keylen, char* tdbname)
362 struct tdb_context *dst_tdb;
363 enum TDB_ERROR ecode;
365 if ((keyname == NULL) || (keylen == 0)) {
366 terror(TDB_SUCCESS, "need key");
371 terror(TDB_SUCCESS, "need destination tdb name");
375 key.dptr = (unsigned char *)keyname;
378 ecode = tdb_fetch(tdb, key, &dbuf);
380 terror(ecode, "fetch failed");
384 print_rec(tdb, key, dbuf, NULL);
386 dst_tdb = tdb_open(tdbname, 0, O_RDWR, 0600, NULL);
388 terror(TDB_SUCCESS, "unable to open destination tdb");
392 ecode = tdb_store( dst_tdb, key, dbuf, TDB_REPLACE);
394 terror(ecode, "failed to move record");
396 printf("record moved\n");
398 tdb_close( dst_tdb );
401 static int print_rec(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
403 printf("\nkey %d bytes\n", (int)key.dsize);
404 print_asc((const char *)key.dptr, key.dsize);
405 printf("\ndata %d bytes\n", (int)dbuf.dsize);
406 print_data((const char *)dbuf.dptr, dbuf.dsize);
410 static int print_key(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
412 printf("key %d bytes: ", (int)key.dsize);
413 print_asc((const char *)key.dptr, key.dsize);
418 static int print_hexkey(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
420 printf("key %d bytes\n", (int)key.dsize);
421 print_data((const char *)key.dptr, key.dsize);
426 static int total_bytes;
428 static int traverse_fn(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
430 total_bytes += dbuf.dsize;
434 static void info_tdb(void)
436 enum TDB_ERROR ecode;
439 ecode = tdb_summary(tdb, TDB_SUMMARY_HISTOGRAMS, &summary);
442 terror(ecode, "Getting summary");
444 printf("%s", summary);
449 static void speed_tdb(const char *tlimit)
451 unsigned timelimit = tlimit?atoi(tlimit):0;
454 if (timelimit == 0) timelimit = 5;
457 printf("Testing store speed for %u seconds\n", timelimit);
460 long int r = random();
462 key.dptr = (unsigned char *)"store test";
463 key.dsize = strlen((char *)key.dptr);
464 dbuf.dptr = (unsigned char *)&r;
465 dbuf.dsize = sizeof(r);
466 tdb_store(tdb, key, dbuf, TDB_REPLACE);
469 } while (t < timelimit);
470 printf("%10.3f ops/sec\n", ops/t);
473 printf("Testing fetch speed for %u seconds\n", timelimit);
476 long int r = random();
478 key.dptr = (unsigned char *)"store test";
479 key.dsize = strlen((char *)key.dptr);
480 dbuf.dptr = (unsigned char *)&r;
481 dbuf.dsize = sizeof(r);
482 tdb_fetch(tdb, key, &dbuf);
485 } while (t < timelimit);
486 printf("%10.3f ops/sec\n", ops/t);
489 printf("Testing transaction speed for %u seconds\n", timelimit);
492 long int r = random();
494 key.dptr = (unsigned char *)"transaction test";
495 key.dsize = strlen((char *)key.dptr);
496 dbuf.dptr = (unsigned char *)&r;
497 dbuf.dsize = sizeof(r);
498 tdb_transaction_start(tdb);
499 tdb_store(tdb, key, dbuf, TDB_REPLACE);
500 tdb_transaction_commit(tdb);
503 } while (t < timelimit);
504 printf("%10.3f ops/sec\n", ops/t);
507 printf("Testing traverse speed for %u seconds\n", timelimit);
510 tdb_traverse(tdb, traverse_fn, NULL);
513 } while (t < timelimit);
514 printf("%10.3f ops/sec\n", ops/t);
517 static void toggle_mmap(void)
519 disable_mmap = !disable_mmap;
521 printf("mmap is disabled\n");
523 printf("mmap is enabled\n");
527 static char *tdb_getline(const char *prompt)
529 static char thisline[1024];
531 fputs(prompt, stdout);
533 p = fgets(thisline, sizeof(thisline)-1, stdin);
534 if (p) p = strchr(p, '\n');
536 return p?thisline:NULL;
539 static int do_delete_fn(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf,
542 return tdb_delete(the_tdb, key);
545 static void first_record(struct tdb_context *the_tdb, TDB_DATA *pkey)
548 enum TDB_ERROR ecode;
549 ecode = tdb_firstkey(the_tdb, pkey);
551 ecode = tdb_fetch(the_tdb, *pkey, &dbuf);
552 if (ecode) terror(ecode, "fetch failed");
554 print_rec(the_tdb, *pkey, dbuf, NULL);
558 static void next_record(struct tdb_context *the_tdb, TDB_DATA *pkey)
561 enum TDB_ERROR ecode;
562 ecode = tdb_nextkey(the_tdb, pkey);
565 ecode = tdb_fetch(the_tdb, *pkey, &dbuf);
567 terror(ecode, "fetch failed");
569 print_rec(the_tdb, *pkey, dbuf, NULL);
572 static void check_db(struct tdb_context *the_tdb)
575 printf("Error: No database opened!\n");
577 if (tdb_check(the_tdb, NULL, NULL) != 0)
578 printf("Integrity check for the opened database failed.\n");
580 printf("Database integrity is OK.\n");
584 static int do_command(void)
586 COMMAND_TABLE *ctp = cmd_table;
587 enum commands mycmd = CMD_HELP;
590 if (cmdname && strlen(cmdname) == 0) {
594 cmd_len = strlen(ctp->name);
595 if (strncmp(ctp->name,cmdname,cmd_len) == 0) {
614 if (system(arg1) == -1) {
615 terror(TDB_SUCCESS, "system() call failed\n");
621 /* all the rest require a open database */
624 terror(TDB_SUCCESS, "database not open");
629 case CMD_TRANSACTION_START:
631 tdb_transaction_start(tdb);
633 case CMD_TRANSACTION_COMMIT:
635 tdb_transaction_commit(tdb);
637 case CMD_TRANSACTION_CANCEL:
639 tdb_transaction_cancel(tdb);
643 tdb_traverse(tdb, do_delete_fn, NULL);
647 tdb_traverse(tdb, print_rec, NULL);
651 insert_tdb(arg1, arg1len,arg2,arg2len);
655 move_rec(arg1,arg1len,arg2);
659 store_tdb(arg1,arg1len,arg2,arg2len);
663 show_tdb(arg1, arg1len);
666 tdb_traverse(tdb, print_key, NULL);
669 tdb_traverse(tdb, print_hexkey, NULL);
673 delete_tdb(arg1,arg1len);
676 case CMD_LIST_HASH_FREE:
680 tdb_printfreelist(tdb);
694 first_record(tdb, &iterate_kbuf);
698 next_record(tdb, &iterate_kbuf);
711 * unhandled commands. cases included here to avoid compiler
721 static char *convert_string(char *instring, size_t *sizep)
727 outp = inp = instring;
732 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
735 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
739 *outp++ = (char)strtol((const char *)temp,NULL,16);
752 int main(int argc, char *argv[])
771 /* Interactive mode */
772 while ((cmdname = tdb_getline("tdb> "))) {
774 if ((arg1 = strchr((const char *)cmdname,' ')) != NULL) {
782 if ((*arg2++ == '\\') && (*arg2 == ' ')) {
787 if (arg1) arg1 = convert_string(arg1,&arg1len);
788 if (arg2) arg2 = convert_string(arg2,&arg2len);
789 if (do_command()) break;
793 arg2 = convert_string(argv[4],&arg2len);
795 arg1 = convert_string(argv[3],&arg1len);
803 if (tdb) tdb_close(tdb);