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/>.
27 static int do_command(void);
30 size_t arg1len, arg2len;
33 NTDB_DATA iterate_kbuf;
35 static int disable_mmap;
40 CMD_TRANSACTION_START,
41 CMD_TRANSACTION_COMMIT,
42 CMD_TRANSACTION_CANCEL,
72 COMMAND_TABLE cmd_table[] = {
73 {"create", CMD_CREATE_NTDB},
74 {"open", CMD_OPEN_NTDB},
76 {"transaction_start", CMD_TRANSACTION_START},
77 {"transaction_commit", CMD_TRANSACTION_COMMIT},
78 {"transaction_cancel", CMD_TRANSACTION_CANCEL},
82 {"insert", CMD_INSERT},
87 {"hexkeys", CMD_HEXKEYS},
88 {"delete", CMD_DELETE},
90 {"list", CMD_LIST_HASH_FREE},
91 {"free", CMD_LIST_FREE},
100 {"check", CMD_CHECK},
107 struct timeval tp1,tp2;
109 static void _start_timer(void)
111 gettimeofday(&tp1,NULL);
114 static double _end_timer(void)
116 gettimeofday(&tp2,NULL);
117 return((tp2.tv_sec - tp1.tv_sec) +
118 (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
121 static void ntdb_log(struct ntdb_context *ntdb,
122 enum ntdb_log_level level,
123 enum NTDB_ERROR ecode,
127 fprintf(stderr, "ntdb:%s:%s:%s\n",
128 ntdb_name(ntdb), ntdb_errorstr(ecode), message);
131 /* a ntdb tool for manipulating a ntdb database */
133 static struct ntdb_context *ntdb;
135 static int print_rec(struct ntdb_context *the_ntdb, NTDB_DATA key, NTDB_DATA dbuf, void *state);
136 static int print_key(struct ntdb_context *the_ntdb, NTDB_DATA key, NTDB_DATA dbuf, void *state);
137 static int print_hexkey(struct ntdb_context *the_ntdb, NTDB_DATA key, NTDB_DATA dbuf, void *state);
139 static void print_asc(const char *buf,int len)
143 /* We're probably printing ASCII strings so don't try to display
144 the trailing NULL character. */
146 if (buf[len - 1] == 0)
150 printf("%c",isprint(buf[i])?buf[i]:'.');
153 static void print_data(const char *buf,int len)
159 printf("%02X ",(int)((unsigned char)buf[i]));
161 if (i%8 == 0) printf(" ");
163 print_asc(&buf[i-16],8); printf(" ");
164 print_asc(&buf[i-8],8); printf("\n");
165 if (i<len) printf("[%03X] ",i);
173 if (n>8) printf(" ");
174 while (n--) printf(" ");
178 print_asc(&buf[i-(i%16)],n); printf(" ");
180 if (n>0) print_asc(&buf[i-n],n);
185 static void help(void)
189 " create dbname : create a database\n"
190 " open dbname : open an existing database\n"
191 " openjh dbname : open an existing database (jenkins hash)\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 ntdb\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"
206 " list : print the database hash table and freelist\n"
207 " free : print the database freelist\n"
209 " check : check the integrity of an opened database\n"
210 " speed : perform speed tests on the database\n"
211 " ! command : execute system command\n"
212 " 1 | first : print the first record\n"
213 " n | next : print the next record\n"
214 " q | quit : terminate\n"
215 " \\n : repeat 'next' command\n"
219 static void terror(enum NTDB_ERROR err, const char *why)
221 if (err != NTDB_SUCCESS)
222 printf("%s:%s\n", ntdb_errorstr(err), why);
227 static void create_ntdb(const char *tdbname)
229 union ntdb_attribute log_attr;
230 log_attr.base.attr = NTDB_ATTRIBUTE_LOG;
231 log_attr.base.next = NULL;
232 log_attr.log.fn = ntdb_log;
234 if (ntdb) ntdb_close(ntdb);
235 ntdb = ntdb_open(tdbname, (disable_mmap?NTDB_NOMMAP:0),
236 O_RDWR | O_CREAT | O_TRUNC, 0600, &log_attr);
238 printf("Could not create %s: %s\n", tdbname, strerror(errno));
242 static void open_ntdb(const char *tdbname)
244 union ntdb_attribute log_attr;
245 log_attr.base.attr = NTDB_ATTRIBUTE_LOG;
246 log_attr.base.next = NULL;
247 log_attr.log.fn = ntdb_log;
249 if (ntdb) ntdb_close(ntdb);
250 ntdb = ntdb_open(tdbname, disable_mmap?NTDB_NOMMAP:0, O_RDWR, 0600,
253 printf("Could not open %s: %s\n", tdbname, strerror(errno));
257 static void insert_ntdb(char *keyname, size_t keylen, char* data, size_t datalen)
260 enum NTDB_ERROR ecode;
262 if ((keyname == NULL) || (keylen == 0)) {
263 terror(NTDB_SUCCESS, "need key");
267 key.dptr = (unsigned char *)keyname;
269 dbuf.dptr = (unsigned char *)data;
270 dbuf.dsize = datalen;
272 ecode = ntdb_store(ntdb, key, dbuf, NTDB_INSERT);
274 terror(ecode, "insert failed");
278 static void store_ntdb(char *keyname, size_t keylen, char* data, size_t datalen)
281 enum NTDB_ERROR ecode;
283 if ((keyname == NULL) || (keylen == 0)) {
284 terror(NTDB_SUCCESS, "need key");
288 if ((data == NULL) || (datalen == 0)) {
289 terror(NTDB_SUCCESS, "need data");
293 key.dptr = (unsigned char *)keyname;
295 dbuf.dptr = (unsigned char *)data;
296 dbuf.dsize = datalen;
298 printf("Storing key:\n");
299 print_rec(ntdb, key, dbuf, NULL);
301 ecode = ntdb_store(ntdb, key, dbuf, NTDB_REPLACE);
303 terror(ecode, "store failed");
307 static void show_ntdb(char *keyname, size_t keylen)
310 enum NTDB_ERROR ecode;
312 if ((keyname == NULL) || (keylen == 0)) {
313 terror(NTDB_SUCCESS, "need key");
317 key.dptr = (unsigned char *)keyname;
320 ecode = ntdb_fetch(ntdb, key, &dbuf);
322 terror(ecode, "fetch failed");
326 print_rec(ntdb, key, dbuf, NULL);
331 static void delete_ntdb(char *keyname, size_t keylen)
334 enum NTDB_ERROR ecode;
336 if ((keyname == NULL) || (keylen == 0)) {
337 terror(NTDB_SUCCESS, "need key");
341 key.dptr = (unsigned char *)keyname;
344 ecode = ntdb_delete(ntdb, key);
346 terror(ecode, "delete failed");
350 static void move_rec(char *keyname, size_t keylen, char* tdbname)
353 struct ntdb_context *dst_ntdb;
354 enum NTDB_ERROR ecode;
356 if ((keyname == NULL) || (keylen == 0)) {
357 terror(NTDB_SUCCESS, "need key");
362 terror(NTDB_SUCCESS, "need destination ntdb name");
366 key.dptr = (unsigned char *)keyname;
369 ecode = ntdb_fetch(ntdb, key, &dbuf);
371 terror(ecode, "fetch failed");
375 print_rec(ntdb, key, dbuf, NULL);
377 dst_ntdb = ntdb_open(tdbname, 0, O_RDWR, 0600, NULL);
379 terror(NTDB_SUCCESS, "unable to open destination ntdb");
383 ecode = ntdb_store( dst_ntdb, key, dbuf, NTDB_REPLACE);
385 terror(ecode, "failed to move record");
387 printf("record moved\n");
389 ntdb_close( dst_ntdb );
392 static int print_rec(struct ntdb_context *the_ntdb, NTDB_DATA key, NTDB_DATA dbuf, void *state)
394 printf("\nkey %d bytes\n", (int)key.dsize);
395 print_asc((const char *)key.dptr, key.dsize);
396 printf("\ndata %d bytes\n", (int)dbuf.dsize);
397 print_data((const char *)dbuf.dptr, dbuf.dsize);
401 static int print_key(struct ntdb_context *the_ntdb, NTDB_DATA key, NTDB_DATA dbuf, void *state)
403 printf("key %d bytes: ", (int)key.dsize);
404 print_asc((const char *)key.dptr, key.dsize);
409 static int print_hexkey(struct ntdb_context *the_ntdb, NTDB_DATA key, NTDB_DATA dbuf, void *state)
411 printf("key %d bytes\n", (int)key.dsize);
412 print_data((const char *)key.dptr, key.dsize);
417 static int total_bytes;
419 static int traverse_fn(struct ntdb_context *the_ntdb, NTDB_DATA key, NTDB_DATA dbuf, void *state)
421 total_bytes += dbuf.dsize;
425 static void info_ntdb(void)
427 enum NTDB_ERROR ecode;
430 ecode = ntdb_summary(ntdb, NTDB_SUMMARY_HISTOGRAMS, &summary);
433 terror(ecode, "Getting summary");
435 printf("%s", summary);
440 static void speed_ntdb(const char *tlimit)
442 unsigned timelimit = tlimit?atoi(tlimit):0;
445 if (timelimit == 0) timelimit = 5;
448 printf("Testing store speed for %u seconds\n", timelimit);
451 long int r = random();
453 key = ntdb_mkdata("store test", strlen("store test"));
454 dbuf.dptr = (unsigned char *)&r;
455 dbuf.dsize = sizeof(r);
456 ntdb_store(ntdb, key, dbuf, NTDB_REPLACE);
459 } while (t < timelimit);
460 printf("%10.3f ops/sec\n", ops/t);
463 printf("Testing fetch speed for %u seconds\n", timelimit);
466 long int r = random();
468 key = ntdb_mkdata("store test", strlen("store test"));
469 dbuf.dptr = (unsigned char *)&r;
470 dbuf.dsize = sizeof(r);
471 ntdb_fetch(ntdb, key, &dbuf);
474 } while (t < timelimit);
475 printf("%10.3f ops/sec\n", ops/t);
478 printf("Testing transaction speed for %u seconds\n", timelimit);
481 long int r = random();
483 key = ntdb_mkdata("transaction test", strlen("transaction test"));
484 dbuf.dptr = (unsigned char *)&r;
485 dbuf.dsize = sizeof(r);
486 ntdb_transaction_start(ntdb);
487 ntdb_store(ntdb, key, dbuf, NTDB_REPLACE);
488 ntdb_transaction_commit(ntdb);
491 } while (t < timelimit);
492 printf("%10.3f ops/sec\n", ops/t);
495 printf("Testing traverse speed for %u seconds\n", timelimit);
498 ntdb_traverse(ntdb, traverse_fn, NULL);
501 } while (t < timelimit);
502 printf("%10.3f ops/sec\n", ops/t);
505 static void toggle_mmap(void)
507 disable_mmap = !disable_mmap;
509 printf("mmap is disabled\n");
511 printf("mmap is enabled\n");
515 static char *ntdb_getline(const char *prompt)
517 static char thisline[1024];
519 fputs(prompt, stdout);
521 p = fgets(thisline, sizeof(thisline)-1, stdin);
522 if (p) p = strchr(p, '\n');
524 return p?thisline:NULL;
527 static int do_delete_fn(struct ntdb_context *the_ntdb, NTDB_DATA key, NTDB_DATA dbuf,
530 return ntdb_delete(the_ntdb, key);
533 static void first_record(struct ntdb_context *the_ntdb, NTDB_DATA *pkey)
536 enum NTDB_ERROR ecode;
537 ecode = ntdb_firstkey(the_ntdb, pkey);
539 ecode = ntdb_fetch(the_ntdb, *pkey, &dbuf);
540 if (ecode) terror(ecode, "fetch failed");
542 print_rec(the_ntdb, *pkey, dbuf, NULL);
546 static void next_record(struct ntdb_context *the_ntdb, NTDB_DATA *pkey)
549 enum NTDB_ERROR ecode;
550 ecode = ntdb_nextkey(the_ntdb, pkey);
553 ecode = ntdb_fetch(the_ntdb, *pkey, &dbuf);
555 terror(ecode, "fetch failed");
557 print_rec(the_ntdb, *pkey, dbuf, NULL);
560 static void check_db(struct ntdb_context *the_ntdb)
563 printf("Error: No database opened!\n");
565 if (ntdb_check(the_ntdb, NULL, NULL) != 0)
566 printf("Integrity check for the opened database failed.\n");
568 printf("Database integrity is OK.\n");
572 static int do_command(void)
574 COMMAND_TABLE *ctp = cmd_table;
575 enum commands mycmd = CMD_HELP;
578 if (cmdname && strlen(cmdname) == 0) {
582 cmd_len = strlen(ctp->name);
583 if (strncmp(ctp->name,cmdname,cmd_len) == 0) {
592 case CMD_CREATE_NTDB:
602 if (system(arg1) == -1) {
603 terror(NTDB_SUCCESS, "system() call failed\n");
609 /* all the rest require a open database */
612 terror(NTDB_SUCCESS, "database not open");
617 case CMD_TRANSACTION_START:
619 ntdb_transaction_start(ntdb);
621 case CMD_TRANSACTION_COMMIT:
623 ntdb_transaction_commit(ntdb);
625 case CMD_TRANSACTION_CANCEL:
627 ntdb_transaction_cancel(ntdb);
631 ntdb_traverse(ntdb, do_delete_fn, NULL);
635 ntdb_traverse(ntdb, print_rec, NULL);
639 insert_ntdb(arg1, arg1len,arg2,arg2len);
643 move_rec(arg1,arg1len,arg2);
647 store_ntdb(arg1,arg1len,arg2,arg2len);
651 show_ntdb(arg1, arg1len);
654 ntdb_traverse(ntdb, print_key, NULL);
657 ntdb_traverse(ntdb, print_hexkey, NULL);
661 delete_ntdb(arg1,arg1len);
664 case CMD_LIST_HASH_FREE:
668 ntdb_printfreelist(ntdb);
682 first_record(ntdb, &iterate_kbuf);
686 next_record(ntdb, &iterate_kbuf);
694 case CMD_CREATE_NTDB:
699 * unhandled commands. cases included here to avoid compiler
709 static char *convert_string(char *instring, size_t *sizep)
715 outp = inp = instring;
720 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
723 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
727 *outp++ = (char)strtol((const char *)temp,NULL,16);
740 int main(int argc, char *argv[])
759 /* Interactive mode */
760 while ((cmdname = ntdb_getline("ntdb> "))) {
762 if ((arg1 = strchr((const char *)cmdname,' ')) != NULL) {
770 if ((*arg2++ == '\\') && (*arg2 == ' ')) {
775 if (arg1) arg1 = convert_string(arg1,&arg1len);
776 if (arg2) arg2 = convert_string(arg2,&arg2len);
777 if (do_command()) break;
781 arg2 = convert_string(argv[4],&arg2len);
783 arg1 = convert_string(argv[3],&arg1len);
791 if (ntdb) ntdb_close(ntdb);