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 2 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, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "system/locale.h"
26 #include "system/time.h"
27 #include "system/filesys.h"
30 static int do_command(void);
33 size_t arg1len
, arg2len
;
36 TDB_DATA iterate_kbuf
;
38 static int disable_mmap
;
69 COMMAND_TABLE cmd_table
[] = {
70 {"create", CMD_CREATE_TDB
},
71 {"open", CMD_OPEN_TDB
},
74 {"insert", CMD_INSERT
},
79 {"hexkeys", CMD_HEXKEYS
},
80 {"delete", CMD_DELETE
},
81 {"list", CMD_LIST_HASH_FREE
},
82 {"free", CMD_LIST_FREE
},
96 struct timeval tp1
,tp2
;
98 static void _start_timer(void)
100 gettimeofday(&tp1
,NULL
);
103 static double _end_timer(void)
105 gettimeofday(&tp2
,NULL
);
106 return((tp2
.tv_sec
- tp1
.tv_sec
) +
107 (tp2
.tv_usec
- tp1
.tv_usec
)*1.0e-6);
110 /* a tdb tool for manipulating a tdb database */
112 static TDB_CONTEXT
*tdb
;
114 static int print_rec(TDB_CONTEXT
*the_tdb
, TDB_DATA key
, TDB_DATA dbuf
, void *state
);
115 static int print_key(TDB_CONTEXT
*the_tdb
, TDB_DATA key
, TDB_DATA dbuf
, void *state
);
116 static int print_hexkey(TDB_CONTEXT
*the_tdb
, TDB_DATA key
, TDB_DATA dbuf
, void *state
);
118 static void print_asc(const char *buf
,int len
)
122 /* We're probably printing ASCII strings so don't try to display
123 the trailing NULL character. */
125 if (buf
[len
- 1] == 0)
129 printf("%c",isprint(buf
[i
])?buf
[i
]:'.');
132 static void print_data(const char *buf
,int len
)
138 printf("%02X ",(int)buf
[i
]);
140 if (i
%8 == 0) printf(" ");
142 print_asc(&buf
[i
-16],8); printf(" ");
143 print_asc(&buf
[i
-8],8); printf("\n");
144 if (i
<len
) printf("[%03X] ",i
);
152 if (n
>8) printf(" ");
153 while (n
--) printf(" ");
157 print_asc(&buf
[i
-(i
%16)],n
); printf(" ");
159 if (n
>0) print_asc(&buf
[i
-n
],n
);
164 static void help(void)
168 " create dbname : create a database\n"
169 " open dbname : open an existing database\n"
170 " erase : erase the database\n"
171 " dump : dump the database as strings\n"
172 " keys : dump the database keys as strings\n"
173 " hexkeys : dump the database keys as hex values\n"
174 " info : print summary info about the database\n"
175 " insert key data : insert a record\n"
176 " move key file : move a record to a destination tdb\n"
177 " store key data : store a record (replace)\n"
178 " show key : show a record by key\n"
179 " delete key : delete a record by key\n"
180 " list : print the database hash table and freelist\n"
181 " free : print the database freelist\n"
182 " ! command : execute system command\n"
183 " 1 | first : print the first record\n"
184 " n | next : print the next record\n"
185 " q | quit : terminate\n"
186 " \\n : repeat 'next' command\n"
190 static void terror(const char *why
)
195 static void create_tdb(const char *tdbname
)
197 if (tdb
) tdb_close(tdb
);
198 tdb
= tdb_open(tdbname
, 0, TDB_CLEAR_IF_FIRST
| (disable_mmap
?TDB_NOMMAP
:0),
199 O_RDWR
| O_CREAT
| O_TRUNC
, 0600);
201 printf("Could not create %s: %s\n", tdbname
, strerror(errno
));
205 static void open_tdb(const char *tdbname
)
207 if (tdb
) tdb_close(tdb
);
208 tdb
= tdb_open(tdbname
, 0, disable_mmap
?TDB_NOMMAP
:0, O_RDWR
, 0600);
210 printf("Could not open %s: %s\n", tdbname
, strerror(errno
));
214 static void insert_tdb(char *keyname
, size_t keylen
, char* data
, size_t datalen
)
218 if ((keyname
== NULL
) || (keylen
== 0)) {
223 key
.dptr
= (unsigned char *)keyname
;
225 dbuf
.dptr
= (unsigned char *)data
;
226 dbuf
.dsize
= datalen
;
228 if (tdb_store(tdb
, key
, dbuf
, TDB_INSERT
) == -1) {
229 terror("insert failed");
233 static void store_tdb(char *keyname
, size_t keylen
, char* data
, size_t datalen
)
237 if ((keyname
== NULL
) || (keylen
== 0)) {
242 if ((data
== NULL
) || (datalen
== 0)) {
247 key
.dptr
= (unsigned char *)keyname
;
249 dbuf
.dptr
= (unsigned char *)data
;
250 dbuf
.dsize
= datalen
;
252 printf("Storing key:\n");
253 print_rec(tdb
, key
, dbuf
, NULL
);
255 if (tdb_store(tdb
, key
, dbuf
, TDB_REPLACE
) == -1) {
256 terror("store failed");
260 static void show_tdb(char *keyname
, size_t keylen
)
264 if ((keyname
== NULL
) || (keylen
== 0)) {
269 key
.dptr
= (unsigned char *)keyname
;
272 dbuf
= tdb_fetch(tdb
, key
);
274 terror("fetch failed");
278 print_rec(tdb
, key
, dbuf
, NULL
);
285 static void delete_tdb(char *keyname
, size_t keylen
)
289 if ((keyname
== NULL
) || (keylen
== 0)) {
294 key
.dptr
= (unsigned char *)keyname
;
297 if (tdb_delete(tdb
, key
) != 0) {
298 terror("delete failed");
302 static void move_rec(char *keyname
, size_t keylen
, char* tdbname
)
305 TDB_CONTEXT
*dst_tdb
;
307 if ((keyname
== NULL
) || (keylen
== 0)) {
313 terror("need destination tdb name");
317 key
.dptr
= (unsigned char *)keyname
;
320 dbuf
= tdb_fetch(tdb
, key
);
322 terror("fetch failed");
326 print_rec(tdb
, key
, dbuf
, NULL
);
328 dst_tdb
= tdb_open(tdbname
, 0, 0, O_RDWR
, 0600);
330 terror("unable to open destination tdb");
334 if ( tdb_store( dst_tdb
, key
, dbuf
, TDB_REPLACE
) == -1 ) {
335 terror("failed to move record");
338 printf("record moved\n");
340 tdb_close( dst_tdb
);
345 static int print_rec(TDB_CONTEXT
*the_tdb
, TDB_DATA key
, TDB_DATA dbuf
, void *state
)
347 printf("\nkey %d bytes\n", (int)key
.dsize
);
348 print_asc((const char *)key
.dptr
, key
.dsize
);
349 printf("\ndata %d bytes\n", (int)dbuf
.dsize
);
350 print_data((const char *)dbuf
.dptr
, dbuf
.dsize
);
354 static int print_key(TDB_CONTEXT
*the_tdb
, TDB_DATA key
, TDB_DATA dbuf
, void *state
)
356 printf("key %d bytes: ", (int)key
.dsize
);
357 print_asc((const char *)key
.dptr
, key
.dsize
);
362 static int print_hexkey(TDB_CONTEXT
*the_tdb
, TDB_DATA key
, TDB_DATA dbuf
, void *state
)
364 printf("key %d bytes\n", (int)key
.dsize
);
365 print_data((const char *)key
.dptr
, key
.dsize
);
370 static int total_bytes
;
372 static int traverse_fn(TDB_CONTEXT
*the_tdb
, TDB_DATA key
, TDB_DATA dbuf
, void *state
)
374 total_bytes
+= dbuf
.dsize
;
378 static void info_tdb(void)
382 if ((count
= tdb_traverse(tdb
, traverse_fn
, NULL
)) == -1)
383 printf("Error = %s\n", tdb_errorstr(tdb
));
385 printf("%d records totalling %d bytes\n", count
, total_bytes
);
388 static void speed_tdb(const char *tlimit
)
390 unsigned timelimit
= tlimit
?atoi(tlimit
):0;
393 if (timelimit
== 0) timelimit
= 10;
394 printf("Testing traverse speed for %u seconds\n", timelimit
);
396 while ((t
=_end_timer()) < timelimit
) {
397 tdb_traverse(tdb
, traverse_fn
, NULL
);
398 printf("%10.3f ops/sec\r", (++ops
)/t
);
403 static void toggle_mmap(void)
405 disable_mmap
= !disable_mmap
;
407 printf("mmap is disabled\n");
409 printf("mmap is enabled\n");
413 static char *tdb_getline(const char *prompt
)
415 static char thisline
[1024];
417 fputs(prompt
, stdout
);
419 p
= fgets(thisline
, sizeof(thisline
)-1, stdin
);
420 if (p
) p
= strchr(p
, '\n');
422 return p
?thisline
:NULL
;
425 static int do_delete_fn(TDB_CONTEXT
*the_tdb
, TDB_DATA key
, TDB_DATA dbuf
,
428 return tdb_delete(the_tdb
, key
);
431 static void first_record(TDB_CONTEXT
*the_tdb
, TDB_DATA
*pkey
)
434 *pkey
= tdb_firstkey(the_tdb
);
436 dbuf
= tdb_fetch(the_tdb
, *pkey
);
437 if (!dbuf
.dptr
) terror("fetch failed");
439 print_rec(the_tdb
, *pkey
, dbuf
, NULL
);
443 static void next_record(TDB_CONTEXT
*the_tdb
, TDB_DATA
*pkey
)
446 *pkey
= tdb_nextkey(the_tdb
, *pkey
);
448 dbuf
= tdb_fetch(the_tdb
, *pkey
);
450 terror("fetch failed");
452 print_rec(the_tdb
, *pkey
, dbuf
, NULL
);
455 static int do_command(void)
457 COMMAND_TABLE
*ctp
= cmd_table
;
458 enum commands mycmd
= CMD_HELP
;
461 if (cmdname
&& strlen(cmdname
) == 0) {
465 cmd_len
= strlen(ctp
->name
);
466 if (strncmp(ctp
->name
,cmdname
,cmd_len
) == 0) {
490 /* all the rest require a open database */
493 terror("database not open");
500 tdb_traverse(tdb
, do_delete_fn
, NULL
);
504 tdb_traverse(tdb
, print_rec
, NULL
);
508 insert_tdb(arg1
, arg1len
,arg2
,arg2len
);
512 move_rec(arg1
,arg1len
,arg2
);
516 store_tdb(arg1
,arg1len
,arg2
,arg2len
);
520 show_tdb(arg1
, arg1len
);
523 tdb_traverse(tdb
, print_key
, NULL
);
526 tdb_traverse(tdb
, print_hexkey
, NULL
);
530 delete_tdb(arg1
,arg1len
);
532 case CMD_LIST_HASH_FREE
:
536 tdb_printfreelist(tdb
);
549 first_record(tdb
, &iterate_kbuf
);
553 next_record(tdb
, &iterate_kbuf
);
563 * unhandled commands. cases included here to avoid compiler
573 static char *convert_string(char *instring
, size_t *sizep
)
580 outp
= inp
= instring
;
585 if (*inp
&& strchr("0123456789abcdefABCDEF",(int)*inp
)) {
588 if (*inp
&& strchr("0123456789abcdefABCDEF",(int)*inp
)) {
592 *outp
++ = (char)strtol((const char *)temp
,NULL
,16);
605 int main(int argc
, char *argv
[])
624 /* Interactive mode */
625 while ((cmdname
= tdb_getline("tdb> "))) {
627 if ((arg1
= strchr((const char *)cmdname
,' ')) != NULL
) {
635 if ((*arg2
++ == '\\') && (*arg2
== ' ')) {
640 if (arg1
) arg1
= convert_string(arg1
,&arg1len
);
641 if (arg2
) arg2
= convert_string(arg2
,&arg2len
);
642 if (do_command()) break;
646 arg2
= convert_string(argv
[4],&arg2len
);
648 arg1
= convert_string(argv
[3],&arg1len
);
656 if (tdb
) tdb_close(tdb
);