3
* Copyright (c) 1999-2000 Andrew Tridgell
4
* Copyright (c) 2000 Paul `Rusty' Russell
5
* Copyright (c) 2000 Jeremy Allison
6
* Copyright (c) 2001 Andrew Esh
7
* Copyright (c) 2005 Benedikt Meurer <benny@xfce.org>
9
* This library is free software; you can redistribute it and/or
10
* modify it under the terms of the GNU Library General Public
11
* License as published by the Free Software Foundation; either
12
* version 2 of the License, or (at your option) any later version.
14
* This library 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 GNU
17
* Library General Public License for more details.
19
* You should have received a copy of the GNU Library General Public
20
* License along with this library; if not, write to the
21
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22
* Boston, MA 02111-1307, USA.
24
* This file was originally part of the tdb library, which in turn is
25
* part of the Samba suite, a Unix SMB/CIFS implementation.
32
#ifdef HAVE_SYS_TYPES_H
33
#include <sys/types.h>
35
#ifdef HAVE_SYS_MMAN_H
38
#ifdef HAVE_SYS_STAT_H
41
#ifdef HAVE_SYS_TIME_H
73
/* a tdb tool for manipulating a tdb database */
75
#define FSTRING_LEN 256
76
typedef char fstring[FSTRING_LEN];
78
typedef struct connections_key {
84
typedef struct connections_data {
96
static TDB_CONTEXT *tdb;
99
static int print_rec(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
101
static char *get_token(int startover)
103
static char tmp[1024];
104
static char *cont = NULL;
105
char *insert, *start;
106
char *k = strtok(NULL, " ");
117
insert = start + strlen(start) - 1;
118
while (*insert == '\\') {
120
k = strtok(NULL, " ");
124
insert = start + strlen(start) - 1;
127
/* Get ready for next call */
128
cont = start + strlen(start) + 1;
132
static int open_dump_file(void)
135
char *tok = get_token(0);
139
pDumpFile = fopen(tok, "w");
141
if (pDumpFile == NULL) {
142
printf("File Open Failed! -- %s", tok);
145
printf("Writing to file: %s\n", tok);
151
static void close_dump_file(void)
153
if(pDumpFile != NULL && pDumpFile != stdout) {
158
static void print_asc(unsigned char *buf,int len)
162
/* We're probably printing ASCII strings so don't try to display
163
the trailing NULL character. */
165
if (buf[len - 1] == 0)
169
fprintf(pDumpFile,"%c",isprint(buf[i])?buf[i]:'.');
172
static void print_data(unsigned char *buf,int len)
176
fprintf(pDumpFile,"[%03X] ",i);
178
fprintf(pDumpFile,"%02X ",(int)buf[i]);
180
if (i%8 == 0) fprintf(pDumpFile," ");
182
print_asc(&buf[i-16],8); fprintf(pDumpFile," ");
183
print_asc(&buf[i-8],8); fprintf(pDumpFile,"\n");
184
if (i<len) fprintf(pDumpFile,"[%03X] ",i);
191
fprintf(pDumpFile," ");
192
if (n>8) fprintf(pDumpFile," ");
193
while (n--) fprintf(pDumpFile," ");
197
print_asc(&buf[i-(i%16)],n); fprintf(pDumpFile," ");
199
if (n>0) print_asc(&buf[i-n],n);
200
fprintf(pDumpFile,"\n");
204
static void help(void)
208
" create dbname : create a database\n"
209
" open dbname : open an existing database\n"
210
" erase : erase the database\n"
211
" dump dumpname : dump the database as strings\n"
212
" insert key data : insert a record\n"
213
" store key data : store a record (replace)\n"
214
" show key : show a record by key\n"
215
" delete key : delete a record by key\n"
216
" list : print the database hash table and freelist\n"
217
" free : print the database freelist\n"
218
" 1 | first : print the first record\n"
219
" n | next : print the next record\n"
220
" q | quit : terminate\n"
221
" \\n : repeat 'next' command\n");
224
static void terror(char *why)
229
static void create_tdb(void)
231
char *tok = get_token(1);
236
if (tdb) tdb_close(tdb);
237
tdb = tdb_open(tok, 0, TDB_CLEAR_IF_FIRST,
238
O_RDWR | O_CREAT | O_TRUNC, 0600);
240
printf("Could not create %s: %s\n", tok, strerror(errno));
244
static void open_tdb(void)
246
char *tok = get_token(1);
251
if (tdb) tdb_close(tdb);
252
tdb = tdb_open(tok, 0, 0, O_RDWR, 0600);
254
printf("Could not open %s: %s\n", tok, strerror(errno));
258
static void insert_tdb(void)
260
char *k = get_token(1);
261
char *d = get_token(0);
270
key.dsize = strlen(k)+1;
272
dbuf.dsize = strlen(d)+1;
274
if (tdb_store(tdb, key, dbuf, TDB_INSERT) == -1) {
275
terror("insert failed");
279
static void store_tdb(void)
281
char *k = get_token(1);
282
char *d = get_token(0);
291
key.dsize = strlen(k)+1;
293
dbuf.dsize = strlen(d)+1;
295
printf("Storing key:\n");
296
print_rec(tdb, key, dbuf, NULL);
298
if (tdb_store(tdb, key, dbuf, TDB_REPLACE) == -1) {
299
terror("store failed");
303
static void show_tdb(void)
305
char *k = get_token(1);
314
key.dsize = strlen(k)+1;
316
dbuf = tdb_fetch(tdb, key);
318
terror("fetch failed");
321
/* printf("%s : %*.*s\n", k, (int)dbuf.dsize, (int)dbuf.dsize, dbuf.dptr); */
322
print_rec(tdb, key, dbuf, NULL);
325
static void delete_tdb(void)
327
char *k = get_token(1);
336
key.dsize = strlen(k)+1;
338
if (tdb_delete(tdb, key) != 0) {
339
terror("delete failed");
343
static int print_rec(TDB_CONTEXT *context, TDB_DATA key, TDB_DATA dbuf, void *state)
345
fprintf(pDumpFile,"\nkey %u bytes\n", (unsigned) key.dsize);
346
print_asc((unsigned char*)key.dptr, key.dsize);
347
fprintf(pDumpFile,"\ndata %u bytes\n", (unsigned) dbuf.dsize);
348
print_data((unsigned char*)dbuf.dptr, dbuf.dsize);
352
static int print_key(TDB_CONTEXT *context, TDB_DATA key, TDB_DATA dbuf, void *state)
354
print_asc((unsigned char*)key.dptr, key.dsize);
359
static int total_bytes;
361
static int traverse_fn(TDB_CONTEXT *context, TDB_DATA key, TDB_DATA dbuf, void *state)
363
total_bytes += dbuf.dsize;
367
static void info_tdb(void)
371
if ((count = tdb_traverse(tdb, traverse_fn, NULL) == -1))
372
printf("Error = %s\n", tdb_errorstr(tdb));
374
printf("%d records totalling %d bytes\n", count, total_bytes);
377
static char *tdb_getline(char *prompt)
379
static char line[1024];
381
fputs(prompt, stdout);
383
p = fgets(line, sizeof(line)-1, stdin);
384
if (p) p = strchr(p, '\n');
390
static int do_delete_fn(TDB_CONTEXT *context, TDB_DATA key, TDB_DATA dbuf,
393
return tdb_delete(context, key);
396
static void first_record(TDB_CONTEXT *context, TDB_DATA *pkey)
399
*pkey = tdb_firstkey(context);
401
dbuf = tdb_fetch(context, *pkey);
402
if (!dbuf.dptr) terror("fetch failed");
403
/* printf("%s : %*.*s\n", k, (int)dbuf.dsize, (int)dbuf.dsize, dbuf.dptr); */
404
print_rec(context, *pkey, dbuf, NULL);
407
static void next_record(TDB_CONTEXT *context, TDB_DATA *pkey)
410
*pkey = tdb_nextkey(context, *pkey);
412
dbuf = tdb_fetch(context, *pkey);
414
terror("fetch failed");
416
/* printf("%s : %*.*s\n", k, (int)dbuf.dsize, (int)dbuf.dsize, dbuf.dptr); */
417
print_rec(context, *pkey, dbuf, NULL);
420
int main(int argc, char *argv[])
422
int bIterate = 0, ignore;
425
TDB_DATA iterate_kbuf;
428
static char tmp[1024];
429
sprintf(tmp, "open %s", argv[1]);
434
while ((line = tdb_getline("tdb> "))) {
438
if (line[0] == '!') {
439
ignore = system(line + 1);
443
if ((tok = strtok(line," "))==NULL) {
445
next_record(tdb, &iterate_kbuf);
448
if (strcmp(tok,"create") == 0) {
452
} else if (strcmp(tok,"open") == 0) {
455
} else if ((strcmp(tok, "q") == 0) ||
456
(strcmp(tok, "quit") == 0)) {
460
/* all the rest require a open database */
463
terror("database not open");
468
if (strcmp(tok,"insert") == 0) {
471
} else if (strcmp(tok,"store") == 0) {
474
} else if (strcmp(tok,"show") == 0) {
477
} else if (strcmp(tok,"erase") == 0) {
479
tdb_traverse(tdb, do_delete_fn, NULL);
480
} else if (strcmp(tok,"delete") == 0) {
483
} else if (strcmp(tok,"dump") == 0) {
485
if(open_dump_file() == 0) { /* open file */
486
tdb_traverse(tdb, print_rec, NULL);
487
close_dump_file(); /* close file */
490
} else if (strcmp(tok,"list") == 0) {
492
} else if (strcmp(tok, "free") == 0) {
493
tdb_printfreelist(tdb);
494
} else if (strcmp(tok,"info") == 0) {
496
} else if ( (strcmp(tok, "1") == 0) ||
497
(strcmp(tok, "first") == 0)) {
499
first_record(tdb, &iterate_kbuf);
500
} else if ((strcmp(tok, "n") == 0) ||
501
(strcmp(tok, "next") == 0)) {
502
next_record(tdb, &iterate_kbuf);
503
} else if ((strcmp(tok, "keys") == 0)) {
505
tdb_traverse(tdb, print_key, NULL);
511
if (tdb) tdb_close(tdb);