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 " speed : perform speed tests on the database\n"
186 " ! command : execute system command\n"
187 " 1 | first : print the first record\n"
188 " n | next : print the next record\n"
189 " q | quit : terminate\n"
190 " \\n : repeat 'next' command\n"
194 static void terror(const char *why
)
199 static void create_tdb(const char *tdbname
)
201 if (tdb
) tdb_close(tdb
);
202 tdb
= tdb_open(tdbname
, 0, TDB_CLEAR_IF_FIRST
| (disable_mmap
?TDB_NOMMAP
:0),
203 O_RDWR
| O_CREAT
| O_TRUNC
, 0600);
205 printf("Could not create %s: %s\n", tdbname
, strerror(errno
));
209 static void open_tdb(const char *tdbname
)
211 if (tdb
) tdb_close(tdb
);
212 tdb
= tdb_open(tdbname
, 0, disable_mmap
?TDB_NOMMAP
:0, O_RDWR
, 0600);
214 printf("Could not open %s: %s\n", tdbname
, strerror(errno
));
218 static void insert_tdb(char *keyname
, size_t keylen
, char* data
, size_t datalen
)
222 if ((keyname
== NULL
) || (keylen
== 0)) {
227 key
.dptr
= (unsigned char *)keyname
;
229 dbuf
.dptr
= (unsigned char *)data
;
230 dbuf
.dsize
= datalen
;
232 if (tdb_store(tdb
, key
, dbuf
, TDB_INSERT
) == -1) {
233 terror("insert failed");
237 static void store_tdb(char *keyname
, size_t keylen
, char* data
, size_t datalen
)
241 if ((keyname
== NULL
) || (keylen
== 0)) {
246 if ((data
== NULL
) || (datalen
== 0)) {
251 key
.dptr
= (unsigned char *)keyname
;
253 dbuf
.dptr
= (unsigned char *)data
;
254 dbuf
.dsize
= datalen
;
256 printf("Storing key:\n");
257 print_rec(tdb
, key
, dbuf
, NULL
);
259 if (tdb_store(tdb
, key
, dbuf
, TDB_REPLACE
) == -1) {
260 terror("store failed");
264 static void show_tdb(char *keyname
, size_t keylen
)
268 if ((keyname
== NULL
) || (keylen
== 0)) {
273 key
.dptr
= (unsigned char *)keyname
;
276 dbuf
= tdb_fetch(tdb
, key
);
278 terror("fetch failed");
282 print_rec(tdb
, key
, dbuf
, NULL
);
289 static void delete_tdb(char *keyname
, size_t keylen
)
293 if ((keyname
== NULL
) || (keylen
== 0)) {
298 key
.dptr
= (unsigned char *)keyname
;
301 if (tdb_delete(tdb
, key
) != 0) {
302 terror("delete failed");
306 static void move_rec(char *keyname
, size_t keylen
, char* tdbname
)
309 TDB_CONTEXT
*dst_tdb
;
311 if ((keyname
== NULL
) || (keylen
== 0)) {
317 terror("need destination tdb name");
321 key
.dptr
= (unsigned char *)keyname
;
324 dbuf
= tdb_fetch(tdb
, key
);
326 terror("fetch failed");
330 print_rec(tdb
, key
, dbuf
, NULL
);
332 dst_tdb
= tdb_open(tdbname
, 0, 0, O_RDWR
, 0600);
334 terror("unable to open destination tdb");
338 if ( tdb_store( dst_tdb
, key
, dbuf
, TDB_REPLACE
) == -1 ) {
339 terror("failed to move record");
342 printf("record moved\n");
344 tdb_close( dst_tdb
);
349 static int print_rec(TDB_CONTEXT
*the_tdb
, TDB_DATA key
, TDB_DATA dbuf
, void *state
)
351 printf("\nkey %d bytes\n", (int)key
.dsize
);
352 print_asc((const char *)key
.dptr
, key
.dsize
);
353 printf("\ndata %d bytes\n", (int)dbuf
.dsize
);
354 print_data((const char *)dbuf
.dptr
, dbuf
.dsize
);
358 static int print_key(TDB_CONTEXT
*the_tdb
, TDB_DATA key
, TDB_DATA dbuf
, void *state
)
360 printf("key %d bytes: ", (int)key
.dsize
);
361 print_asc((const char *)key
.dptr
, key
.dsize
);
366 static int print_hexkey(TDB_CONTEXT
*the_tdb
, TDB_DATA key
, TDB_DATA dbuf
, void *state
)
368 printf("key %d bytes\n", (int)key
.dsize
);
369 print_data((const char *)key
.dptr
, key
.dsize
);
374 static int total_bytes
;
376 static int traverse_fn(TDB_CONTEXT
*the_tdb
, TDB_DATA key
, TDB_DATA dbuf
, void *state
)
378 total_bytes
+= dbuf
.dsize
;
382 static void info_tdb(void)
386 if ((count
= tdb_traverse(tdb
, traverse_fn
, NULL
)) == -1)
387 printf("Error = %s\n", tdb_errorstr(tdb
));
389 printf("%d records totalling %d bytes\n", count
, total_bytes
);
392 static void speed_tdb(const char *tlimit
)
394 unsigned timelimit
= tlimit
?atoi(tlimit
):0;
397 if (timelimit
== 0) timelimit
= 5;
400 printf("Testing store speed for %u seconds\n", timelimit
);
403 long int r
= random();
405 key
.dptr
= (unsigned char *)"store test";
406 key
.dsize
= strlen((char *)key
.dptr
);
407 dbuf
.dptr
= (unsigned char *)&r
;
408 dbuf
.dsize
= sizeof(r
);
409 tdb_store(tdb
, key
, dbuf
, TDB_REPLACE
);
412 } while (t
< timelimit
);
413 printf("%10.3f ops/sec\n", ops
/t
);
416 printf("Testing fetch speed for %u seconds\n", timelimit
);
419 long int r
= random();
421 key
.dptr
= (unsigned char *)"store test";
422 key
.dsize
= strlen((char *)key
.dptr
);
423 dbuf
.dptr
= (unsigned char *)&r
;
424 dbuf
.dsize
= sizeof(r
);
428 } while (t
< timelimit
);
429 printf("%10.3f ops/sec\n", ops
/t
);
432 printf("Testing transaction speed for %u seconds\n", timelimit
);
435 long int r
= random();
437 key
.dptr
= (unsigned char *)"transaction test";
438 key
.dsize
= strlen((char *)key
.dptr
);
439 dbuf
.dptr
= (unsigned char *)&r
;
440 dbuf
.dsize
= sizeof(r
);
441 tdb_transaction_start(tdb
);
442 tdb_store(tdb
, key
, dbuf
, TDB_REPLACE
);
443 tdb_transaction_commit(tdb
);
446 } while (t
< timelimit
);
447 printf("%10.3f ops/sec\n", ops
/t
);
450 printf("Testing traverse speed for %u seconds\n", timelimit
);
453 tdb_traverse(tdb
, traverse_fn
, NULL
);
456 } while (t
< timelimit
);
457 printf("%10.3f ops/sec\n", ops
/t
);
460 static void toggle_mmap(void)
462 disable_mmap
= !disable_mmap
;
464 printf("mmap is disabled\n");
466 printf("mmap is enabled\n");
470 static char *tdb_getline(const char *prompt
)
472 static char thisline
[1024];
474 fputs(prompt
, stdout
);
476 p
= fgets(thisline
, sizeof(thisline
)-1, stdin
);
477 if (p
) p
= strchr(p
, '\n');
479 return p
?thisline
:NULL
;
482 static int do_delete_fn(TDB_CONTEXT
*the_tdb
, TDB_DATA key
, TDB_DATA dbuf
,
485 return tdb_delete(the_tdb
, key
);
488 static void first_record(TDB_CONTEXT
*the_tdb
, TDB_DATA
*pkey
)
491 *pkey
= tdb_firstkey(the_tdb
);
493 dbuf
= tdb_fetch(the_tdb
, *pkey
);
494 if (!dbuf
.dptr
) terror("fetch failed");
496 print_rec(the_tdb
, *pkey
, dbuf
, NULL
);
500 static void next_record(TDB_CONTEXT
*the_tdb
, TDB_DATA
*pkey
)
503 *pkey
= tdb_nextkey(the_tdb
, *pkey
);
505 dbuf
= tdb_fetch(the_tdb
, *pkey
);
507 terror("fetch failed");
509 print_rec(the_tdb
, *pkey
, dbuf
, NULL
);
512 static int test_fn(TDB_CONTEXT
*the_tdb
, TDB_DATA key
, TDB_DATA dbuf
, void *state
)
517 static void check_db(TDB_CONTEXT
*the_tdb
)
521 tdbcount
= tdb_traverse(the_tdb
, test_fn
, NULL
);
523 printf("Error: No database opened!\n");
527 printf("Integrity check for the opened database failed.\n");
529 printf("Database integrity is OK and has %d records.\n", tdbcount
);
533 static int do_command(void)
535 COMMAND_TABLE
*ctp
= cmd_table
;
536 enum commands mycmd
= CMD_HELP
;
539 if (cmdname
&& strlen(cmdname
) == 0) {
543 cmd_len
= strlen(ctp
->name
);
544 if (strncmp(ctp
->name
,cmdname
,cmd_len
) == 0) {
563 if (system(arg1
) == -1) {
564 terror("system() call failed\n");
570 /* all the rest require a open database */
573 terror("database not open");
580 tdb_traverse(tdb
, do_delete_fn
, NULL
);
584 tdb_traverse(tdb
, print_rec
, NULL
);
588 insert_tdb(arg1
, arg1len
,arg2
,arg2len
);
592 move_rec(arg1
,arg1len
,arg2
);
596 store_tdb(arg1
,arg1len
,arg2
,arg2len
);
600 show_tdb(arg1
, arg1len
);
603 tdb_traverse(tdb
, print_key
, NULL
);
606 tdb_traverse(tdb
, print_hexkey
, NULL
);
610 delete_tdb(arg1
,arg1len
);
612 case CMD_LIST_HASH_FREE
:
616 tdb_printfreelist(tdb
);
629 first_record(tdb
, &iterate_kbuf
);
633 next_record(tdb
, &iterate_kbuf
);
646 * unhandled commands. cases included here to avoid compiler
656 static char *convert_string(char *instring
, size_t *sizep
)
663 outp
= inp
= instring
;
668 if (*inp
&& strchr("0123456789abcdefABCDEF",(int)*inp
)) {
671 if (*inp
&& strchr("0123456789abcdefABCDEF",(int)*inp
)) {
675 *outp
++ = (char)strtol((const char *)temp
,NULL
,16);
688 int main(int argc
, char *argv
[])
707 /* Interactive mode */
708 while ((cmdname
= tdb_getline("tdb> "))) {
710 if ((arg1
= strchr((const char *)cmdname
,' ')) != NULL
) {
718 if ((*arg2
++ == '\\') && (*arg2
== ' ')) {
723 if (arg1
) arg1
= convert_string(arg1
,&arg1len
);
724 if (arg2
) arg2
= convert_string(arg2
,&arg2len
);
725 if (do_command()) break;
729 arg2
= convert_string(argv
[4],&arg2len
);
731 arg1
= convert_string(argv
[3],&arg1len
);
739 if (tdb
) tdb_close(tdb
);