]> git.ozlabs.org Git - ccan/blob - ccan/ntdb/tools/ntdbdump.c
ntdb: next-generation trivial key-value database
[ccan] / ccan / ntdb / tools / ntdbdump.c
1 /*
2    simple ntdb dump util
3    Copyright (C) Andrew Tridgell              2001
4    Copyright (C) Rusty Russell                2011
5
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.
10
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.
15
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/>.
18 */
19 #include "config.h"
20 #include "ntdb.h"
21 #include "private.h"
22
23 static void print_data(NTDB_DATA d)
24 {
25         unsigned char *p = (unsigned char *)d.dptr;
26         int len = d.dsize;
27         while (len--) {
28                 if (isprint(*p) && !strchr("\"\\", *p)) {
29                         fputc(*p, stdout);
30                 } else {
31                         printf("\\%02X", *p);
32                 }
33                 p++;
34         }
35 }
36
37 static int traverse_fn(struct ntdb_context *ntdb, NTDB_DATA key, NTDB_DATA dbuf, void *state)
38 {
39         printf("{\n");
40         printf("key(%d) = \"", (int)key.dsize);
41         print_data(key);
42         printf("\"\n");
43         printf("data(%d) = \"", (int)dbuf.dsize);
44         print_data(dbuf);
45         printf("\"\n");
46         printf("}\n");
47         return 0;
48 }
49
50 static int dump_ntdb(const char *fname, const char *keyname)
51 {
52         struct ntdb_context *ntdb;
53         NTDB_DATA key, value;
54
55         ntdb = ntdb_open(fname, 0, O_RDONLY, 0, NULL);
56         if (!ntdb) {
57                 printf("Failed to open %s\n", fname);
58                 return 1;
59         }
60
61         if (!keyname) {
62                 ntdb_traverse(ntdb, traverse_fn, NULL);
63         } else {
64                 key = ntdb_mkdata(keyname, strlen(keyname));
65                 if (ntdb_fetch(ntdb, key, &value) != 0) {
66                         return 1;
67                 } else {
68                         print_data(value);
69                         free(value.dptr);
70                 }
71         }
72
73         return 0;
74 }
75
76 static void usage( void)
77 {
78         printf( "Usage: ntdbdump [options] <filename>\n\n");
79         printf( "   -h          this help message\n");
80         printf( "   -k keyname  dumps value of keyname\n");
81 }
82
83  int main(int argc, char *argv[])
84 {
85         char *fname, *keyname=NULL;
86         int c;
87
88         if (argc < 2) {
89                 printf("Usage: ntdbdump <fname>\n");
90                 exit(1);
91         }
92
93         while ((c = getopt( argc, argv, "hk:")) != -1) {
94                 switch (c) {
95                 case 'h':
96                         usage();
97                         exit( 0);
98                 case 'k':
99                         keyname = optarg;
100                         break;
101                 default:
102                         usage();
103                         exit( 1);
104                 }
105         }
106
107         fname = argv[optind];
108
109         return dump_ntdb(fname, keyname);
110 }