s3:registry: do not use regdb functions during db upgrade
[Samba/gebeck_regimport.git] / lib / tdb2 / tools / tdb2dump.c
blobabe1d9b87100a362d74a72400b5360d0eaef1fe5
1 /*
2 simple tdb2 dump util
3 Copyright (C) Andrew Tridgell 2001
4 Copyright (C) Rusty Russell 2011
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "tdb2.h"
20 #include <ctype.h>
21 #include <stdio.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <stdlib.h>
26 #include <unistd.h>
28 static void print_data(TDB_DATA d)
30 unsigned char *p = (unsigned char *)d.dptr;
31 int len = d.dsize;
32 while (len--) {
33 if (isprint(*p) && !strchr("\"\\", *p)) {
34 fputc(*p, stdout);
35 } else {
36 printf("\\%02X", *p);
38 p++;
42 static int traverse_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
44 printf("{\n");
45 printf("key(%d) = \"", (int)key.dsize);
46 print_data(key);
47 printf("\"\n");
48 printf("data(%d) = \"", (int)dbuf.dsize);
49 print_data(dbuf);
50 printf("\"\n");
51 printf("}\n");
52 return 0;
55 static int dump_tdb(const char *fname, const char *keyname)
57 struct tdb_context *tdb;
58 TDB_DATA key, value;
60 tdb = tdb_open(fname, 0, O_RDONLY, 0, NULL);
61 if (!tdb) {
62 printf("Failed to open %s\n", fname);
63 return 1;
66 if (!keyname) {
67 tdb_traverse(tdb, traverse_fn, NULL);
68 } else {
69 key = tdb_mkdata(keyname, strlen(keyname));
70 if (tdb_fetch(tdb, key, &value) != 0) {
71 return 1;
72 } else {
73 print_data(value);
74 free(value.dptr);
78 return 0;
81 static void usage( void)
83 printf( "Usage: tdb2dump [options] <filename>\n\n");
84 printf( " -h this help message\n");
85 printf( " -k keyname dumps value of keyname\n");
88 int main(int argc, char *argv[])
90 char *fname, *keyname=NULL;
91 int c;
93 if (argc < 2) {
94 printf("Usage: tdb2dump <fname>\n");
95 exit(1);
98 while ((c = getopt( argc, argv, "hk:")) != -1) {
99 switch (c) {
100 case 'h':
101 usage();
102 exit( 0);
103 case 'k':
104 keyname = optarg;
105 break;
106 default:
107 usage();
108 exit( 1);
112 fname = argv[optind];
114 return dump_tdb(fname, keyname);