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/>.
25 #ifdef HAVE_LIBREPLACE
27 #include <system/filesys.h>
28 #include <system/time.h>
29 #include <system/locale.h>
35 #include <sys/types.h>
43 static int do_command(void);
46 size_t arg1len
, arg2len
;
49 NTDB_DATA iterate_kbuf
;
51 static int disable_mmap
;
56 CMD_TRANSACTION_START
,
57 CMD_TRANSACTION_COMMIT
,
58 CMD_TRANSACTION_CANCEL
,
88 COMMAND_TABLE cmd_table
[] = {
89 {"create", CMD_CREATE_NTDB
},
90 {"open", CMD_OPEN_NTDB
},
92 {"transaction_start", CMD_TRANSACTION_START
},
93 {"transaction_commit", CMD_TRANSACTION_COMMIT
},
94 {"transaction_cancel", CMD_TRANSACTION_CANCEL
},
98 {"insert", CMD_INSERT
},
100 {"store", CMD_STORE
},
103 {"hexkeys", CMD_HEXKEYS
},
104 {"delete", CMD_DELETE
},
106 {"list", CMD_LIST_HASH_FREE
},
107 {"free", CMD_LIST_FREE
},
110 {"speed", CMD_SPEED
},
112 {"first", CMD_FIRST
},
116 {"check", CMD_CHECK
},
123 struct timeval tp1
,tp2
;
125 static void _start_timer(void)
127 gettimeofday(&tp1
,NULL
);
130 static double _end_timer(void)
132 gettimeofday(&tp2
,NULL
);
133 return((tp2
.tv_sec
- tp1
.tv_sec
) +
134 (tp2
.tv_usec
- tp1
.tv_usec
)*1.0e-6);
137 static void ntdb_log(struct ntdb_context
*ntdb
,
138 enum ntdb_log_level level
,
139 enum NTDB_ERROR ecode
,
143 fprintf(stderr
, "ntdb:%s:%s:%s\n",
144 ntdb_name(ntdb
), ntdb_errorstr(ecode
), message
);
147 /* a ntdb tool for manipulating a ntdb database */
149 static struct ntdb_context
*ntdb
;
151 static int print_rec(struct ntdb_context
*the_ntdb
, NTDB_DATA key
, NTDB_DATA dbuf
, void *state
);
152 static int print_key(struct ntdb_context
*the_ntdb
, NTDB_DATA key
, NTDB_DATA dbuf
, void *state
);
153 static int print_hexkey(struct ntdb_context
*the_ntdb
, NTDB_DATA key
, NTDB_DATA dbuf
, void *state
);
155 static void print_asc(const char *buf
,int len
)
159 /* We're probably printing ASCII strings so don't try to display
160 the trailing NULL character. */
162 if (buf
[len
- 1] == 0)
166 printf("%c",isprint(buf
[i
])?buf
[i
]:'.');
169 static void print_data(const char *buf
,int len
)
175 printf("%02X ",(int)((unsigned char)buf
[i
]));
177 if (i
%8 == 0) printf(" ");
179 print_asc(&buf
[i
-16],8); printf(" ");
180 print_asc(&buf
[i
-8],8); printf("\n");
181 if (i
<len
) printf("[%03X] ",i
);
189 if (n
>8) printf(" ");
190 while (n
--) printf(" ");
194 print_asc(&buf
[i
-(i
%16)],n
); printf(" ");
196 if (n
>0) print_asc(&buf
[i
-n
],n
);
201 static void help(void)
205 " create dbname : create a database\n"
206 " open dbname : open an existing database\n"
207 " openjh dbname : open an existing database (jenkins hash)\n"
208 " transaction_start : start a transaction\n"
209 " transaction_commit : commit a transaction\n"
210 " transaction_cancel : cancel a transaction\n"
211 " erase : erase the database\n"
212 " dump : dump the database as strings\n"
213 " keys : dump the database keys as strings\n"
214 " hexkeys : dump the database keys as hex values\n"
215 " info : print summary info about the database\n"
216 " insert key data : insert a record\n"
217 " move key file : move a record to a destination ntdb\n"
218 " store key data : store a record (replace)\n"
219 " show key : show a record by key\n"
220 " delete key : delete a record by key\n"
222 " list : print the database hash table and freelist\n"
223 " free : print the database freelist\n"
225 " check : check the integrity of an opened database\n"
226 " speed : perform speed tests on the database\n"
227 " ! command : execute system command\n"
228 " 1 | first : print the first record\n"
229 " n | next : print the next record\n"
230 " q | quit : terminate\n"
231 " \\n : repeat 'next' command\n"
235 static void terror(enum NTDB_ERROR err
, const char *why
)
237 if (err
!= NTDB_SUCCESS
)
238 printf("%s:%s\n", ntdb_errorstr(err
), why
);
243 static void create_ntdb(const char *tdbname
)
245 union ntdb_attribute log_attr
;
246 log_attr
.base
.attr
= NTDB_ATTRIBUTE_LOG
;
247 log_attr
.base
.next
= NULL
;
248 log_attr
.log
.fn
= ntdb_log
;
250 if (ntdb
) ntdb_close(ntdb
);
251 ntdb
= ntdb_open(tdbname
, (disable_mmap
?NTDB_NOMMAP
:0),
252 O_RDWR
| O_CREAT
| O_TRUNC
, 0600, &log_attr
);
254 printf("Could not create %s: %s\n", tdbname
, strerror(errno
));
258 static void open_ntdb(const char *tdbname
)
260 union ntdb_attribute log_attr
;
261 log_attr
.base
.attr
= NTDB_ATTRIBUTE_LOG
;
262 log_attr
.base
.next
= NULL
;
263 log_attr
.log
.fn
= ntdb_log
;
265 if (ntdb
) ntdb_close(ntdb
);
266 ntdb
= ntdb_open(tdbname
, disable_mmap
?NTDB_NOMMAP
:0, O_RDWR
, 0600,
269 printf("Could not open %s: %s\n", tdbname
, strerror(errno
));
273 static void insert_ntdb(char *keyname
, size_t keylen
, char* data
, size_t datalen
)
276 enum NTDB_ERROR ecode
;
278 if ((keyname
== NULL
) || (keylen
== 0)) {
279 terror(NTDB_SUCCESS
, "need key");
283 key
.dptr
= (unsigned char *)keyname
;
285 dbuf
.dptr
= (unsigned char *)data
;
286 dbuf
.dsize
= datalen
;
288 ecode
= ntdb_store(ntdb
, key
, dbuf
, NTDB_INSERT
);
290 terror(ecode
, "insert failed");
294 static void store_ntdb(char *keyname
, size_t keylen
, char* data
, size_t datalen
)
297 enum NTDB_ERROR ecode
;
299 if ((keyname
== NULL
) || (keylen
== 0)) {
300 terror(NTDB_SUCCESS
, "need key");
304 if ((data
== NULL
) || (datalen
== 0)) {
305 terror(NTDB_SUCCESS
, "need data");
309 key
.dptr
= (unsigned char *)keyname
;
311 dbuf
.dptr
= (unsigned char *)data
;
312 dbuf
.dsize
= datalen
;
314 printf("Storing key:\n");
315 print_rec(ntdb
, key
, dbuf
, NULL
);
317 ecode
= ntdb_store(ntdb
, key
, dbuf
, NTDB_REPLACE
);
319 terror(ecode
, "store failed");
323 static void show_ntdb(char *keyname
, size_t keylen
)
326 enum NTDB_ERROR ecode
;
328 if ((keyname
== NULL
) || (keylen
== 0)) {
329 terror(NTDB_SUCCESS
, "need key");
333 key
.dptr
= (unsigned char *)keyname
;
336 ecode
= ntdb_fetch(ntdb
, key
, &dbuf
);
338 terror(ecode
, "fetch failed");
342 print_rec(ntdb
, key
, dbuf
, NULL
);
347 static void delete_ntdb(char *keyname
, size_t keylen
)
350 enum NTDB_ERROR ecode
;
352 if ((keyname
== NULL
) || (keylen
== 0)) {
353 terror(NTDB_SUCCESS
, "need key");
357 key
.dptr
= (unsigned char *)keyname
;
360 ecode
= ntdb_delete(ntdb
, key
);
362 terror(ecode
, "delete failed");
366 static void move_rec(char *keyname
, size_t keylen
, char* tdbname
)
369 struct ntdb_context
*dst_ntdb
;
370 enum NTDB_ERROR ecode
;
372 if ((keyname
== NULL
) || (keylen
== 0)) {
373 terror(NTDB_SUCCESS
, "need key");
378 terror(NTDB_SUCCESS
, "need destination ntdb name");
382 key
.dptr
= (unsigned char *)keyname
;
385 ecode
= ntdb_fetch(ntdb
, key
, &dbuf
);
387 terror(ecode
, "fetch failed");
391 print_rec(ntdb
, key
, dbuf
, NULL
);
393 dst_ntdb
= ntdb_open(tdbname
, 0, O_RDWR
, 0600, NULL
);
395 terror(NTDB_SUCCESS
, "unable to open destination ntdb");
399 ecode
= ntdb_store( dst_ntdb
, key
, dbuf
, NTDB_REPLACE
);
401 terror(ecode
, "failed to move record");
403 printf("record moved\n");
405 ntdb_close( dst_ntdb
);
408 static int print_rec(struct ntdb_context
*the_ntdb
, NTDB_DATA key
, NTDB_DATA dbuf
, void *state
)
410 printf("\nkey %d bytes\n", (int)key
.dsize
);
411 print_asc((const char *)key
.dptr
, key
.dsize
);
412 printf("\ndata %d bytes\n", (int)dbuf
.dsize
);
413 print_data((const char *)dbuf
.dptr
, dbuf
.dsize
);
417 static int print_key(struct ntdb_context
*the_ntdb
, NTDB_DATA key
, NTDB_DATA dbuf
, void *state
)
419 printf("key %d bytes: ", (int)key
.dsize
);
420 print_asc((const char *)key
.dptr
, key
.dsize
);
425 static int print_hexkey(struct ntdb_context
*the_ntdb
, NTDB_DATA key
, NTDB_DATA dbuf
, void *state
)
427 printf("key %d bytes\n", (int)key
.dsize
);
428 print_data((const char *)key
.dptr
, key
.dsize
);
433 static int total_bytes
;
435 static int traverse_fn(struct ntdb_context
*the_ntdb
, NTDB_DATA key
, NTDB_DATA dbuf
, void *state
)
437 total_bytes
+= dbuf
.dsize
;
441 static void info_ntdb(void)
443 enum NTDB_ERROR ecode
;
446 ecode
= ntdb_summary(ntdb
, NTDB_SUMMARY_HISTOGRAMS
, &summary
);
449 terror(ecode
, "Getting summary");
451 printf("%s", summary
);
456 static void speed_ntdb(const char *tlimit
)
458 unsigned timelimit
= tlimit
?atoi(tlimit
):0;
461 if (timelimit
== 0) timelimit
= 5;
464 printf("Testing store speed for %u seconds\n", timelimit
);
467 long int r
= random();
469 key
= ntdb_mkdata("store test", strlen("store test"));
470 dbuf
.dptr
= (unsigned char *)&r
;
471 dbuf
.dsize
= sizeof(r
);
472 ntdb_store(ntdb
, key
, dbuf
, NTDB_REPLACE
);
475 } while (t
< timelimit
);
476 printf("%10.3f ops/sec\n", ops
/t
);
479 printf("Testing fetch speed for %u seconds\n", timelimit
);
482 long int r
= random();
484 key
= ntdb_mkdata("store test", strlen("store test"));
485 dbuf
.dptr
= (unsigned char *)&r
;
486 dbuf
.dsize
= sizeof(r
);
487 ntdb_fetch(ntdb
, key
, &dbuf
);
490 } while (t
< timelimit
);
491 printf("%10.3f ops/sec\n", ops
/t
);
494 printf("Testing transaction speed for %u seconds\n", timelimit
);
497 long int r
= random();
499 key
= ntdb_mkdata("transaction test", strlen("transaction test"));
500 dbuf
.dptr
= (unsigned char *)&r
;
501 dbuf
.dsize
= sizeof(r
);
502 ntdb_transaction_start(ntdb
);
503 ntdb_store(ntdb
, key
, dbuf
, NTDB_REPLACE
);
504 ntdb_transaction_commit(ntdb
);
507 } while (t
< timelimit
);
508 printf("%10.3f ops/sec\n", ops
/t
);
511 printf("Testing traverse speed for %u seconds\n", timelimit
);
514 ntdb_traverse(ntdb
, traverse_fn
, NULL
);
517 } while (t
< timelimit
);
518 printf("%10.3f ops/sec\n", ops
/t
);
521 static void toggle_mmap(void)
523 disable_mmap
= !disable_mmap
;
525 printf("mmap is disabled\n");
527 printf("mmap is enabled\n");
531 static char *ntdb_getline(const char *prompt
)
533 static char thisline
[1024];
535 fputs(prompt
, stdout
);
537 p
= fgets(thisline
, sizeof(thisline
)-1, stdin
);
538 if (p
) p
= strchr(p
, '\n');
540 return p
?thisline
:NULL
;
543 static int do_delete_fn(struct ntdb_context
*the_ntdb
, NTDB_DATA key
, NTDB_DATA dbuf
,
546 return ntdb_delete(the_ntdb
, key
);
549 static void first_record(struct ntdb_context
*the_ntdb
, NTDB_DATA
*pkey
)
552 enum NTDB_ERROR ecode
;
553 ecode
= ntdb_firstkey(the_ntdb
, pkey
);
555 ecode
= ntdb_fetch(the_ntdb
, *pkey
, &dbuf
);
556 if (ecode
) terror(ecode
, "fetch failed");
558 print_rec(the_ntdb
, *pkey
, dbuf
, NULL
);
562 static void next_record(struct ntdb_context
*the_ntdb
, NTDB_DATA
*pkey
)
565 enum NTDB_ERROR ecode
;
566 ecode
= ntdb_nextkey(the_ntdb
, pkey
);
569 ecode
= ntdb_fetch(the_ntdb
, *pkey
, &dbuf
);
571 terror(ecode
, "fetch failed");
573 print_rec(the_ntdb
, *pkey
, dbuf
, NULL
);
576 static void check_db(struct ntdb_context
*the_ntdb
)
579 printf("Error: No database opened!\n");
581 if (ntdb_check(the_ntdb
, NULL
, NULL
) != 0)
582 printf("Integrity check for the opened database failed.\n");
584 printf("Database integrity is OK.\n");
588 static int do_command(void)
590 COMMAND_TABLE
*ctp
= cmd_table
;
591 enum commands mycmd
= CMD_HELP
;
594 if (cmdname
&& strlen(cmdname
) == 0) {
598 cmd_len
= strlen(ctp
->name
);
599 if (strncmp(ctp
->name
,cmdname
,cmd_len
) == 0) {
608 case CMD_CREATE_NTDB
:
618 if (system(arg1
) == -1) {
619 terror(NTDB_SUCCESS
, "system() call failed\n");
625 /* all the rest require a open database */
628 terror(NTDB_SUCCESS
, "database not open");
633 case CMD_TRANSACTION_START
:
635 ntdb_transaction_start(ntdb
);
637 case CMD_TRANSACTION_COMMIT
:
639 ntdb_transaction_commit(ntdb
);
641 case CMD_TRANSACTION_CANCEL
:
643 ntdb_transaction_cancel(ntdb
);
647 ntdb_traverse(ntdb
, do_delete_fn
, NULL
);
651 ntdb_traverse(ntdb
, print_rec
, NULL
);
655 insert_ntdb(arg1
, arg1len
,arg2
,arg2len
);
659 move_rec(arg1
,arg1len
,arg2
);
663 store_ntdb(arg1
,arg1len
,arg2
,arg2len
);
667 show_ntdb(arg1
, arg1len
);
670 ntdb_traverse(ntdb
, print_key
, NULL
);
673 ntdb_traverse(ntdb
, print_hexkey
, NULL
);
677 delete_ntdb(arg1
,arg1len
);
680 case CMD_LIST_HASH_FREE
:
684 ntdb_printfreelist(ntdb
);
698 first_record(ntdb
, &iterate_kbuf
);
702 next_record(ntdb
, &iterate_kbuf
);
710 case CMD_CREATE_NTDB
:
715 * unhandled commands. cases included here to avoid compiler
725 static char *convert_string(char *instring
, size_t *sizep
)
731 outp
= inp
= instring
;
736 if (*inp
&& strchr("0123456789abcdefABCDEF",(int)*inp
)) {
739 if (*inp
&& strchr("0123456789abcdefABCDEF",(int)*inp
)) {
743 *outp
++ = (char)strtol((const char *)temp
,NULL
,16);
756 int main(int argc
, char *argv
[])
775 /* Interactive mode */
776 while ((cmdname
= ntdb_getline("ntdb> "))) {
778 if ((arg1
= strchr((const char *)cmdname
,' ')) != NULL
) {
786 if ((*arg2
++ == '\\') && (*arg2
== ' ')) {
791 if (arg1
) arg1
= convert_string(arg1
,&arg1len
);
792 if (arg2
) arg2
= convert_string(arg2
,&arg2len
);
793 if (do_command()) break;
797 arg2
= convert_string(argv
[4],&arg2len
);
799 arg1
= convert_string(argv
[3],&arg1len
);
807 if (ntdb
) ntdb_close(ntdb
);