1 | /*
|
---|
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
|
---|
8 |
|
---|
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.
|
---|
13 |
|
---|
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.
|
---|
18 |
|
---|
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/>.
|
---|
21 | */
|
---|
22 |
|
---|
23 | #include "replace.h"
|
---|
24 | #include "system/locale.h"
|
---|
25 | #include "system/time.h"
|
---|
26 | #include "system/filesys.h"
|
---|
27 | #include "system/wait.h"
|
---|
28 | #include "tdb.h"
|
---|
29 |
|
---|
30 | static int do_command(void);
|
---|
31 | const char *cmdname;
|
---|
32 | char *arg1, *arg2;
|
---|
33 | size_t arg1len, arg2len;
|
---|
34 | int bIterate = 0;
|
---|
35 | char *line;
|
---|
36 | TDB_DATA iterate_kbuf;
|
---|
37 | char cmdline[1024];
|
---|
38 | static int disable_mmap;
|
---|
39 | #ifdef __OS2__
|
---|
40 | static bool createDbCmd;
|
---|
41 | #endif
|
---|
42 |
|
---|
43 | enum commands {
|
---|
44 | CMD_CREATE_TDB,
|
---|
45 | CMD_OPEN_TDB,
|
---|
46 | CMD_ERASE,
|
---|
47 | CMD_DUMP,
|
---|
48 | CMD_INSERT,
|
---|
49 | CMD_MOVE,
|
---|
50 | CMD_STORE,
|
---|
51 | CMD_SHOW,
|
---|
52 | CMD_KEYS,
|
---|
53 | CMD_HEXKEYS,
|
---|
54 | CMD_DELETE,
|
---|
55 | CMD_LIST_HASH_FREE,
|
---|
56 | CMD_LIST_FREE,
|
---|
57 | CMD_INFO,
|
---|
58 | CMD_MMAP,
|
---|
59 | CMD_SPEED,
|
---|
60 | CMD_FIRST,
|
---|
61 | CMD_NEXT,
|
---|
62 | CMD_SYSTEM,
|
---|
63 | CMD_CHECK,
|
---|
64 | CMD_QUIT,
|
---|
65 | CMD_HELP
|
---|
66 | };
|
---|
67 |
|
---|
68 | typedef struct {
|
---|
69 | const char *name;
|
---|
70 | enum commands cmd;
|
---|
71 | } COMMAND_TABLE;
|
---|
72 |
|
---|
73 | COMMAND_TABLE cmd_table[] = {
|
---|
74 | {"create", CMD_CREATE_TDB},
|
---|
75 | {"open", CMD_OPEN_TDB},
|
---|
76 | {"erase", CMD_ERASE},
|
---|
77 | {"dump", CMD_DUMP},
|
---|
78 | {"insert", CMD_INSERT},
|
---|
79 | {"move", CMD_MOVE},
|
---|
80 | {"store", CMD_STORE},
|
---|
81 | {"show", CMD_SHOW},
|
---|
82 | {"keys", CMD_KEYS},
|
---|
83 | {"hexkeys", CMD_HEXKEYS},
|
---|
84 | {"delete", CMD_DELETE},
|
---|
85 | {"list", CMD_LIST_HASH_FREE},
|
---|
86 | {"free", CMD_LIST_FREE},
|
---|
87 | {"info", CMD_INFO},
|
---|
88 | {"speed", CMD_SPEED},
|
---|
89 | {"mmap", CMD_MMAP},
|
---|
90 | {"first", CMD_FIRST},
|
---|
91 | {"1", CMD_FIRST},
|
---|
92 | {"next", CMD_NEXT},
|
---|
93 | {"n", CMD_NEXT},
|
---|
94 | {"check", CMD_CHECK},
|
---|
95 | {"quit", CMD_QUIT},
|
---|
96 | {"q", CMD_QUIT},
|
---|
97 | {"!", CMD_SYSTEM},
|
---|
98 | {NULL, CMD_HELP}
|
---|
99 | };
|
---|
100 |
|
---|
101 | struct timeval tp1,tp2;
|
---|
102 |
|
---|
103 | static void _start_timer(void)
|
---|
104 | {
|
---|
105 | gettimeofday(&tp1,NULL);
|
---|
106 | }
|
---|
107 |
|
---|
108 | static double _end_timer(void)
|
---|
109 | {
|
---|
110 | gettimeofday(&tp2,NULL);
|
---|
111 | return((tp2.tv_sec - tp1.tv_sec) +
|
---|
112 | (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
|
---|
113 | }
|
---|
114 |
|
---|
115 | /* a tdb tool for manipulating a tdb database */
|
---|
116 |
|
---|
117 | static TDB_CONTEXT *tdb;
|
---|
118 |
|
---|
119 | static int print_rec(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
|
---|
120 | static int print_key(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
|
---|
121 | static int print_hexkey(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
|
---|
122 |
|
---|
123 | static void print_asc(const char *buf,int len)
|
---|
124 | {
|
---|
125 | int i;
|
---|
126 |
|
---|
127 | /* We're probably printing ASCII strings so don't try to display
|
---|
128 | the trailing NULL character. */
|
---|
129 |
|
---|
130 | if (buf[len - 1] == 0)
|
---|
131 | len--;
|
---|
132 |
|
---|
133 | for (i=0;i<len;i++)
|
---|
134 | printf("%c",isprint(buf[i])?buf[i]:'.');
|
---|
135 | }
|
---|
136 |
|
---|
137 | static void print_data(const char *buf,int len)
|
---|
138 | {
|
---|
139 | int i=0;
|
---|
140 | if (len<=0) return;
|
---|
141 | printf("[%03X] ",i);
|
---|
142 | for (i=0;i<len;) {
|
---|
143 | printf("%02X ",(int)((unsigned char)buf[i]));
|
---|
144 | i++;
|
---|
145 | if (i%8 == 0) printf(" ");
|
---|
146 | if (i%16 == 0) {
|
---|
147 | print_asc(&buf[i-16],8); printf(" ");
|
---|
148 | print_asc(&buf[i-8],8); printf("\n");
|
---|
149 | if (i<len) printf("[%03X] ",i);
|
---|
150 | }
|
---|
151 | }
|
---|
152 | if (i%16) {
|
---|
153 | int n;
|
---|
154 |
|
---|
155 | n = 16 - (i%16);
|
---|
156 | printf(" ");
|
---|
157 | if (n>8) printf(" ");
|
---|
158 | while (n--) printf(" ");
|
---|
159 |
|
---|
160 | n = i%16;
|
---|
161 | if (n > 8) n = 8;
|
---|
162 | print_asc(&buf[i-(i%16)],n); printf(" ");
|
---|
163 | n = (i%16) - n;
|
---|
164 | if (n>0) print_asc(&buf[i-n],n);
|
---|
165 | printf("\n");
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 | static void help(void)
|
---|
170 | {
|
---|
171 | printf("\n"
|
---|
172 | "tdbtool: \n"
|
---|
173 | " create dbname : create a database\n"
|
---|
174 | " open dbname : open an existing database\n"
|
---|
175 | " erase : erase the database\n"
|
---|
176 | " dump : dump the database as strings\n"
|
---|
177 | " keys : dump the database keys as strings\n"
|
---|
178 | " hexkeys : dump the database keys as hex values\n"
|
---|
179 | " info : print summary info about the database\n"
|
---|
180 | " insert key data : insert a record\n"
|
---|
181 | " move key file : move a record to a destination tdb\n"
|
---|
182 | " store key data : store a record (replace)\n"
|
---|
183 | " show key : show a record by key\n"
|
---|
184 | " delete key : delete a record by key\n"
|
---|
185 | " list : print the database hash table and freelist\n"
|
---|
186 | " free : print the database freelist\n"
|
---|
187 | " check : check the integrity of an opened database\n"
|
---|
188 | " ! command : execute system command\n"
|
---|
189 | " 1 | first : print the first record\n"
|
---|
190 | " n | next : print the next record\n"
|
---|
191 | " q | quit : terminate\n"
|
---|
192 | " \\n : repeat 'next' command\n"
|
---|
193 | "\n");
|
---|
194 | }
|
---|
195 |
|
---|
196 | static void terror(const char *why)
|
---|
197 | {
|
---|
198 | printf("%s\n", why);
|
---|
199 | }
|
---|
200 |
|
---|
201 | static void create_tdb(const char *tdbname)
|
---|
202 | {
|
---|
203 | if (tdb) tdb_close(tdb);
|
---|
204 | tdb = tdb_open(tdbname, 0, TDB_CLEAR_IF_FIRST | (disable_mmap?TDB_NOMMAP:0),
|
---|
205 | O_RDWR | O_CREAT | O_TRUNC, 0600);
|
---|
206 | if (!tdb) {
|
---|
207 | printf("Could not create %s: %s\n", tdbname, strerror(errno));
|
---|
208 | }
|
---|
209 | }
|
---|
210 |
|
---|
211 | static void open_tdb(const char *tdbname)
|
---|
212 | {
|
---|
213 | if (tdb) tdb_close(tdb);
|
---|
214 | tdb = tdb_open(tdbname, 0, disable_mmap?TDB_NOMMAP:0, O_RDWR, 0600);
|
---|
215 | #ifdef __OS2__
|
---|
216 | if (!tdb && !createDbCmd) {
|
---|
217 | #else
|
---|
218 | if (!tdb) {
|
---|
219 | #endif
|
---|
220 | printf("Could not open %s: %s\n", tdbname, strerror(errno));
|
---|
221 | }
|
---|
222 | }
|
---|
223 |
|
---|
224 | static void insert_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
|
---|
225 | {
|
---|
226 | TDB_DATA key, dbuf;
|
---|
227 |
|
---|
228 | if ((keyname == NULL) || (keylen == 0)) {
|
---|
229 | terror("need key");
|
---|
230 | return;
|
---|
231 | }
|
---|
232 |
|
---|
233 | key.dptr = (unsigned char *)keyname;
|
---|
234 | key.dsize = keylen;
|
---|
235 | dbuf.dptr = (unsigned char *)data;
|
---|
236 | dbuf.dsize = datalen;
|
---|
237 |
|
---|
238 | if (tdb_store(tdb, key, dbuf, TDB_INSERT) == -1) {
|
---|
239 | terror("insert failed");
|
---|
240 | }
|
---|
241 | }
|
---|
242 |
|
---|
243 | static void store_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
|
---|
244 | {
|
---|
245 | TDB_DATA key, dbuf;
|
---|
246 |
|
---|
247 | if ((keyname == NULL) || (keylen == 0)) {
|
---|
248 | terror("need key");
|
---|
249 | return;
|
---|
250 | }
|
---|
251 |
|
---|
252 | if ((data == NULL) || (datalen == 0)) {
|
---|
253 | terror("need data");
|
---|
254 | return;
|
---|
255 | }
|
---|
256 |
|
---|
257 | key.dptr = (unsigned char *)keyname;
|
---|
258 | key.dsize = keylen;
|
---|
259 | dbuf.dptr = (unsigned char *)data;
|
---|
260 | dbuf.dsize = datalen;
|
---|
261 |
|
---|
262 | printf("Storing key:\n");
|
---|
263 | print_rec(tdb, key, dbuf, NULL);
|
---|
264 |
|
---|
265 | if (tdb_store(tdb, key, dbuf, TDB_REPLACE) == -1) {
|
---|
266 | terror("store failed");
|
---|
267 | }
|
---|
268 | }
|
---|
269 |
|
---|
270 | static void show_tdb(char *keyname, size_t keylen)
|
---|
271 | {
|
---|
272 | TDB_DATA key, dbuf;
|
---|
273 |
|
---|
274 | if ((keyname == NULL) || (keylen == 0)) {
|
---|
275 | terror("need key");
|
---|
276 | return;
|
---|
277 | }
|
---|
278 |
|
---|
279 | key.dptr = (unsigned char *)keyname;
|
---|
280 | key.dsize = keylen;
|
---|
281 |
|
---|
282 | dbuf = tdb_fetch(tdb, key);
|
---|
283 | if (!dbuf.dptr) {
|
---|
284 | terror("fetch failed");
|
---|
285 | return;
|
---|
286 | }
|
---|
287 |
|
---|
288 | print_rec(tdb, key, dbuf, NULL);
|
---|
289 |
|
---|
290 | free( dbuf.dptr );
|
---|
291 |
|
---|
292 | return;
|
---|
293 | }
|
---|
294 |
|
---|
295 | static void delete_tdb(char *keyname, size_t keylen)
|
---|
296 | {
|
---|
297 | TDB_DATA key;
|
---|
298 |
|
---|
299 | if ((keyname == NULL) || (keylen == 0)) {
|
---|
300 | terror("need key");
|
---|
301 | return;
|
---|
302 | }
|
---|
303 |
|
---|
304 | key.dptr = (unsigned char *)keyname;
|
---|
305 | key.dsize = keylen;
|
---|
306 |
|
---|
307 | if (tdb_delete(tdb, key) != 0) {
|
---|
308 | terror("delete failed");
|
---|
309 | }
|
---|
310 | }
|
---|
311 |
|
---|
312 | static void move_rec(char *keyname, size_t keylen, char* tdbname)
|
---|
313 | {
|
---|
314 | TDB_DATA key, dbuf;
|
---|
315 | TDB_CONTEXT *dst_tdb;
|
---|
316 |
|
---|
317 | if ((keyname == NULL) || (keylen == 0)) {
|
---|
318 | terror("need key");
|
---|
319 | return;
|
---|
320 | }
|
---|
321 |
|
---|
322 | if ( !tdbname ) {
|
---|
323 | terror("need destination tdb name");
|
---|
324 | return;
|
---|
325 | }
|
---|
326 |
|
---|
327 | key.dptr = (unsigned char *)keyname;
|
---|
328 | key.dsize = keylen;
|
---|
329 |
|
---|
330 | dbuf = tdb_fetch(tdb, key);
|
---|
331 | if (!dbuf.dptr) {
|
---|
332 | terror("fetch failed");
|
---|
333 | return;
|
---|
334 | }
|
---|
335 |
|
---|
336 | print_rec(tdb, key, dbuf, NULL);
|
---|
337 |
|
---|
338 | dst_tdb = tdb_open(tdbname, 0, 0, O_RDWR, 0600);
|
---|
339 | if ( !dst_tdb ) {
|
---|
340 | terror("unable to open destination tdb");
|
---|
341 | return;
|
---|
342 | }
|
---|
343 |
|
---|
344 | if ( tdb_store( dst_tdb, key, dbuf, TDB_REPLACE ) == -1 ) {
|
---|
345 | terror("failed to move record");
|
---|
346 | }
|
---|
347 | else
|
---|
348 | printf("record moved\n");
|
---|
349 |
|
---|
350 | tdb_close( dst_tdb );
|
---|
351 |
|
---|
352 | return;
|
---|
353 | }
|
---|
354 |
|
---|
355 | static int print_rec(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
|
---|
356 | {
|
---|
357 | printf("\nkey %d bytes\n", (int)key.dsize);
|
---|
358 | print_asc((const char *)key.dptr, key.dsize);
|
---|
359 | printf("\ndata %d bytes\n", (int)dbuf.dsize);
|
---|
360 | print_data((const char *)dbuf.dptr, dbuf.dsize);
|
---|
361 | return 0;
|
---|
362 | }
|
---|
363 |
|
---|
364 | static int print_key(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
|
---|
365 | {
|
---|
366 | printf("key %d bytes: ", (int)key.dsize);
|
---|
367 | print_asc((const char *)key.dptr, key.dsize);
|
---|
368 | printf("\n");
|
---|
369 | return 0;
|
---|
370 | }
|
---|
371 |
|
---|
372 | static int print_hexkey(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
|
---|
373 | {
|
---|
374 | printf("key %d bytes\n", (int)key.dsize);
|
---|
375 | print_data((const char *)key.dptr, key.dsize);
|
---|
376 | printf("\n");
|
---|
377 | return 0;
|
---|
378 | }
|
---|
379 |
|
---|
380 | static int total_bytes;
|
---|
381 |
|
---|
382 | static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
|
---|
383 | {
|
---|
384 | total_bytes += dbuf.dsize;
|
---|
385 | return 0;
|
---|
386 | }
|
---|
387 |
|
---|
388 | static void info_tdb(void)
|
---|
389 | {
|
---|
390 | int count;
|
---|
391 | total_bytes = 0;
|
---|
392 | if ((count = tdb_traverse(tdb, traverse_fn, NULL)) == -1)
|
---|
393 | printf("Error = %s\n", tdb_errorstr(tdb));
|
---|
394 | else
|
---|
395 | printf("%d records totalling %d bytes\n", count, total_bytes);
|
---|
396 | }
|
---|
397 |
|
---|
398 | static void speed_tdb(const char *tlimit)
|
---|
399 | {
|
---|
400 | unsigned timelimit = tlimit?atoi(tlimit):0;
|
---|
401 | double t;
|
---|
402 | int ops=0;
|
---|
403 | if (timelimit == 0) timelimit = 10;
|
---|
404 | printf("Testing traverse speed for %u seconds\n", timelimit);
|
---|
405 | _start_timer();
|
---|
406 | while ((t=_end_timer()) < timelimit) {
|
---|
407 | tdb_traverse(tdb, traverse_fn, NULL);
|
---|
408 | printf("%10.3f ops/sec\r", (++ops)/t);
|
---|
409 | }
|
---|
410 | printf("\n");
|
---|
411 | }
|
---|
412 |
|
---|
413 | static void toggle_mmap(void)
|
---|
414 | {
|
---|
415 | disable_mmap = !disable_mmap;
|
---|
416 | if (disable_mmap) {
|
---|
417 | printf("mmap is disabled\n");
|
---|
418 | } else {
|
---|
419 | printf("mmap is enabled\n");
|
---|
420 | }
|
---|
421 | }
|
---|
422 |
|
---|
423 | static char *tdb_getline(const char *prompt)
|
---|
424 | {
|
---|
425 | static char thisline[1024];
|
---|
426 | char *p;
|
---|
427 | fputs(prompt, stdout);
|
---|
428 | thisline[0] = 0;
|
---|
429 | p = fgets(thisline, sizeof(thisline)-1, stdin);
|
---|
430 | if (p) p = strchr(p, '\n');
|
---|
431 | if (p) *p = 0;
|
---|
432 | return p?thisline:NULL;
|
---|
433 | }
|
---|
434 |
|
---|
435 | static int do_delete_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf,
|
---|
436 | void *state)
|
---|
437 | {
|
---|
438 | return tdb_delete(the_tdb, key);
|
---|
439 | }
|
---|
440 |
|
---|
441 | static void first_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
|
---|
442 | {
|
---|
443 | TDB_DATA dbuf;
|
---|
444 | *pkey = tdb_firstkey(the_tdb);
|
---|
445 |
|
---|
446 | dbuf = tdb_fetch(the_tdb, *pkey);
|
---|
447 | if (!dbuf.dptr) terror("fetch failed");
|
---|
448 | else {
|
---|
449 | print_rec(the_tdb, *pkey, dbuf, NULL);
|
---|
450 | }
|
---|
451 | }
|
---|
452 |
|
---|
453 | static void next_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
|
---|
454 | {
|
---|
455 | TDB_DATA dbuf;
|
---|
456 | *pkey = tdb_nextkey(the_tdb, *pkey);
|
---|
457 |
|
---|
458 | dbuf = tdb_fetch(the_tdb, *pkey);
|
---|
459 | if (!dbuf.dptr)
|
---|
460 | terror("fetch failed");
|
---|
461 | else
|
---|
462 | print_rec(the_tdb, *pkey, dbuf, NULL);
|
---|
463 | }
|
---|
464 |
|
---|
465 | static int test_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
|
---|
466 | {
|
---|
467 | return 0;
|
---|
468 | }
|
---|
469 |
|
---|
470 | static void check_db(TDB_CONTEXT *the_tdb)
|
---|
471 | {
|
---|
472 | int tdbcount=-1;
|
---|
473 | if (the_tdb) {
|
---|
474 | tdbcount = tdb_traverse(the_tdb, test_fn, NULL);
|
---|
475 | } else {
|
---|
476 | printf("Error: No database opened!\n");
|
---|
477 | }
|
---|
478 |
|
---|
479 | if (tdbcount<0) {
|
---|
480 | printf("Integrity check for the opened database failed.\n");
|
---|
481 | } else {
|
---|
482 | printf("Database integrity is OK and has %d records.\n", tdbcount);
|
---|
483 | }
|
---|
484 | }
|
---|
485 |
|
---|
486 | static int do_command(void)
|
---|
487 | {
|
---|
488 | COMMAND_TABLE *ctp = cmd_table;
|
---|
489 | enum commands mycmd = CMD_HELP;
|
---|
490 | int cmd_len;
|
---|
491 |
|
---|
492 | #ifdef __OS2__
|
---|
493 | if (cmdname) {
|
---|
494 | #endif
|
---|
495 |
|
---|
496 | if (cmdname && strlen(cmdname) == 0) {
|
---|
497 | mycmd = CMD_NEXT;
|
---|
498 | } else {
|
---|
499 | while (ctp->name) {
|
---|
500 | cmd_len = strlen(ctp->name);
|
---|
501 | if (strncmp(ctp->name,cmdname,cmd_len) == 0) {
|
---|
502 | mycmd = ctp->cmd;
|
---|
503 | break;
|
---|
504 | }
|
---|
505 | ctp++;
|
---|
506 | }
|
---|
507 | }
|
---|
508 |
|
---|
509 | #ifdef __OS2__
|
---|
510 | }
|
---|
511 | #endif
|
---|
512 |
|
---|
513 | switch (mycmd) {
|
---|
514 | case CMD_CREATE_TDB:
|
---|
515 | bIterate = 0;
|
---|
516 | create_tdb(arg1);
|
---|
517 | return 0;
|
---|
518 | case CMD_OPEN_TDB:
|
---|
519 | bIterate = 0;
|
---|
520 | open_tdb(arg1);
|
---|
521 | return 0;
|
---|
522 | case CMD_SYSTEM:
|
---|
523 | /* Shell command */
|
---|
524 | if (system(arg1) == -1) {
|
---|
525 | terror("system failed");
|
---|
526 | }
|
---|
527 | return 0;
|
---|
528 | case CMD_QUIT:
|
---|
529 | return 1;
|
---|
530 | default:
|
---|
531 | /* all the rest require a open database */
|
---|
532 | if (!tdb) {
|
---|
533 | bIterate = 0;
|
---|
534 | terror("database not open");
|
---|
535 | help();
|
---|
536 | return 0;
|
---|
537 | }
|
---|
538 | switch (mycmd) {
|
---|
539 | case CMD_ERASE:
|
---|
540 | bIterate = 0;
|
---|
541 | tdb_traverse(tdb, do_delete_fn, NULL);
|
---|
542 | return 0;
|
---|
543 | case CMD_DUMP:
|
---|
544 | bIterate = 0;
|
---|
545 | tdb_traverse(tdb, print_rec, NULL);
|
---|
546 | return 0;
|
---|
547 | case CMD_INSERT:
|
---|
548 | bIterate = 0;
|
---|
549 | insert_tdb(arg1, arg1len,arg2,arg2len);
|
---|
550 | return 0;
|
---|
551 | case CMD_MOVE:
|
---|
552 | bIterate = 0;
|
---|
553 | move_rec(arg1,arg1len,arg2);
|
---|
554 | return 0;
|
---|
555 | case CMD_STORE:
|
---|
556 | bIterate = 0;
|
---|
557 | store_tdb(arg1,arg1len,arg2,arg2len);
|
---|
558 | return 0;
|
---|
559 | case CMD_SHOW:
|
---|
560 | bIterate = 0;
|
---|
561 | show_tdb(arg1, arg1len);
|
---|
562 | return 0;
|
---|
563 | case CMD_KEYS:
|
---|
564 | tdb_traverse(tdb, print_key, NULL);
|
---|
565 | return 0;
|
---|
566 | case CMD_HEXKEYS:
|
---|
567 | tdb_traverse(tdb, print_hexkey, NULL);
|
---|
568 | return 0;
|
---|
569 | case CMD_DELETE:
|
---|
570 | bIterate = 0;
|
---|
571 | delete_tdb(arg1,arg1len);
|
---|
572 | return 0;
|
---|
573 | case CMD_LIST_HASH_FREE:
|
---|
574 | tdb_dump_all(tdb);
|
---|
575 | return 0;
|
---|
576 | case CMD_LIST_FREE:
|
---|
577 | tdb_printfreelist(tdb);
|
---|
578 | return 0;
|
---|
579 | case CMD_INFO:
|
---|
580 | info_tdb();
|
---|
581 | return 0;
|
---|
582 | case CMD_SPEED:
|
---|
583 | speed_tdb(arg1);
|
---|
584 | return 0;
|
---|
585 | case CMD_MMAP:
|
---|
586 | toggle_mmap();
|
---|
587 | return 0;
|
---|
588 | case CMD_FIRST:
|
---|
589 | bIterate = 1;
|
---|
590 | first_record(tdb, &iterate_kbuf);
|
---|
591 | return 0;
|
---|
592 | case CMD_NEXT:
|
---|
593 | if (bIterate)
|
---|
594 | next_record(tdb, &iterate_kbuf);
|
---|
595 | return 0;
|
---|
596 | case CMD_CHECK:
|
---|
597 | check_db(tdb);
|
---|
598 | return 0;
|
---|
599 | case CMD_HELP:
|
---|
600 | help();
|
---|
601 | return 0;
|
---|
602 | case CMD_CREATE_TDB:
|
---|
603 | case CMD_OPEN_TDB:
|
---|
604 | case CMD_SYSTEM:
|
---|
605 | case CMD_QUIT:
|
---|
606 | /*
|
---|
607 | * unhandled commands. cases included here to avoid compiler
|
---|
608 | * warnings.
|
---|
609 | */
|
---|
610 | return 0;
|
---|
611 | }
|
---|
612 | }
|
---|
613 |
|
---|
614 | return 0;
|
---|
615 | }
|
---|
616 |
|
---|
617 | static char *convert_string(char *instring, size_t *sizep)
|
---|
618 | {
|
---|
619 | size_t length = 0;
|
---|
620 | char *outp, *inp;
|
---|
621 | char temp[3];
|
---|
622 |
|
---|
623 |
|
---|
624 | outp = inp = instring;
|
---|
625 |
|
---|
626 | while (*inp) {
|
---|
627 | if (*inp == '\\') {
|
---|
628 | inp++;
|
---|
629 | if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
|
---|
630 | temp[0] = *inp++;
|
---|
631 | temp[1] = '\0';
|
---|
632 | if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
|
---|
633 | temp[1] = *inp++;
|
---|
634 | temp[2] = '\0';
|
---|
635 | }
|
---|
636 | *outp++ = (char)strtol((const char *)temp,NULL,16);
|
---|
637 | } else {
|
---|
638 | *outp++ = *inp++;
|
---|
639 | }
|
---|
640 | } else {
|
---|
641 | *outp++ = *inp++;
|
---|
642 | }
|
---|
643 | length++;
|
---|
644 | }
|
---|
645 | *sizep = length;
|
---|
646 | return instring;
|
---|
647 | }
|
---|
648 |
|
---|
649 | int main(int argc, char *argv[])
|
---|
650 | {
|
---|
651 | cmdname = "";
|
---|
652 | arg1 = NULL;
|
---|
653 | arg1len = 0;
|
---|
654 | arg2 = NULL;
|
---|
655 | arg2len = 0;
|
---|
656 |
|
---|
657 | #ifdef __OS2__
|
---|
658 | int cnt = 1;
|
---|
659 | createDbCmd = false;
|
---|
660 | while (argv[cnt]) {
|
---|
661 | if (strncmp("create",argv[cnt],6) == 0) {
|
---|
662 | createDbCmd = true;
|
---|
663 | break;
|
---|
664 | }
|
---|
665 | cnt++;
|
---|
666 | }
|
---|
667 | #endif
|
---|
668 |
|
---|
669 | if (argv[1]) {
|
---|
670 | cmdname = "open";
|
---|
671 | arg1 = argv[1];
|
---|
672 | do_command();
|
---|
673 | cmdname = "";
|
---|
674 | arg1 = NULL;
|
---|
675 | }
|
---|
676 |
|
---|
677 | switch (argc) {
|
---|
678 | case 1:
|
---|
679 | case 2:
|
---|
680 | /* Interactive mode */
|
---|
681 | while ((cmdname = tdb_getline("tdb> "))) {
|
---|
682 | arg2 = arg1 = NULL;
|
---|
683 | if ((arg1 = strchr((const char *)cmdname,' ')) != NULL) {
|
---|
684 | arg1++;
|
---|
685 | arg2 = arg1;
|
---|
686 | while (*arg2) {
|
---|
687 | if (*arg2 == ' ') {
|
---|
688 | *arg2++ = '\0';
|
---|
689 | break;
|
---|
690 | }
|
---|
691 | if ((*arg2++ == '\\') && (*arg2 == ' ')) {
|
---|
692 | arg2++;
|
---|
693 | }
|
---|
694 | }
|
---|
695 | }
|
---|
696 | if (arg1) arg1 = convert_string(arg1,&arg1len);
|
---|
697 | if (arg2) arg2 = convert_string(arg2,&arg2len);
|
---|
698 | if (do_command()) break;
|
---|
699 | }
|
---|
700 | break;
|
---|
701 | case 5:
|
---|
702 | arg2 = convert_string(argv[4],&arg2len);
|
---|
703 | case 4:
|
---|
704 | arg1 = convert_string(argv[3],&arg1len);
|
---|
705 | case 3:
|
---|
706 | cmdname = argv[2];
|
---|
707 | default:
|
---|
708 | #ifdef __OS2__
|
---|
709 | if (argc == 3) {
|
---|
710 | arg1 = convert_string(argv[1], &arg1len);
|
---|
711 | }
|
---|
712 | #endif
|
---|
713 | do_command();
|
---|
714 | break;
|
---|
715 | }
|
---|
716 |
|
---|
717 | if (tdb) tdb_close(tdb);
|
---|
718 |
|
---|
719 | return 0;
|
---|
720 | }
|
---|