Debian Codegraph @

external/db_drivers/liblmdb/mdb_dump.c

raw symbols SCIP
1 /* mdb_dump.c - memory-mapped database dump tool */
2 /*
3 * Copyright 2011-2018 Howard Chu, Symas Corp.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted only as authorized by the OpenLDAP
8 * Public License.
9 *
10 * A copy of this license is available in the file LICENSE in the
11 * top-level directory of the distribution or, alternatively, at
12 * <http://www.OpenLDAP.org/license.html>.
13 */
14 #include <stdio.h>
15 #include <errno.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <ctype.h>
19 #include <unistd.h>
20 #include <signal.h>
21 #include "lmdb.h"
22
23 #define Yu MDB_PRIy(u)
24
25 #define PRINT 1
26 static int mode;
27
28 typedef struct flagbit {
29 int bit;
30 char *name;
31 } flagbit;
32
33 flagbit dbflags[] = {
34 { MDB_REVERSEKEY, "reversekey" },
35 { MDB_DUPSORT, "dupsort" },
36 { MDB_INTEGERKEY, "integerkey" },
37 { MDB_DUPFIXED, "dupfixed" },
38 { MDB_INTEGERDUP, "integerdup" },
39 { MDB_REVERSEDUP, "reversedup" },
40 { 0, NULL }
41 };
42
43 static volatile sig_atomic_t gotsig;
44
45 static void dumpsig( int sig )
46 {
47 gotsig=1;
48 }
49
50 static const char hexc[] = "0123456789abcdef";
51
52 static void hex(unsigned char c)
53 {
54 putchar(hexc[c >> 4]);
55 putchar(hexc[c & 0xf]);
56 }
57
58 static void text(MDB_val *v)
59 {
60 unsigned char *c, *end;
61
62 putchar(' ');
63 c = v->mv_data;
64 end = c + v->mv_size;
65 while (c < end) {
66 if (isprint(*c)) {
67 if (*c == '\\')
68 putchar('\\');
69 putchar(*c);
70 } else {
71 putchar('\\');
72 hex(*c);
73 }
74 c++;
75 }
76 putchar('\n');
77 }
78
79 static void byte(MDB_val *v)
80 {
81 unsigned char *c, *end;
82
83 putchar(' ');
84 c = v->mv_data;
85 end = c + v->mv_size;
86 while (c < end) {
87 hex(*c++);
88 }
89 putchar('\n');
90 }
91
92 /* Dump in BDB-compatible format */
93 static int dumpit(MDB_txn *txn, MDB_dbi dbi, char *name)
94 {
95 MDB_cursor *mc;
96 MDB_stat ms;
97 MDB_val key, data;
98 MDB_envinfo info;
99 unsigned int flags;
100 int rc, i;
101
102 rc = mdb_dbi_flags(txn, dbi, &flags);
103 if (rc) return rc;
104
105 rc = mdb_stat(txn, dbi, &ms);
106 if (rc) return rc;
107
108 rc = mdb_env_info(mdb_txn_env(txn), &info);
109 if (rc) return rc;
110
111 printf("VERSION=3\n");
112 printf("format=%s\n", mode & PRINT ? "print" : "bytevalue");
113 if (name)
114 printf("database=%s\n", name);
115 printf("type=btree\n");
116 printf("mapsize=%"Yu"\n", info.me_mapsize);
117 if (info.me_mapaddr)
118 printf("mapaddr=%p\n", info.me_mapaddr);
119 printf("maxreaders=%u\n", info.me_maxreaders);
120
121 if (flags & MDB_DUPSORT)
122 printf("duplicates=1\n");
123
124 for (i=0; dbflags[i].bit; i++)
125 if (flags & dbflags[i].bit)
126 printf("%s=1\n", dbflags[i].name);
127
128 printf("db_pagesize=%d\n", ms.ms_psize);
129 printf("HEADER=END\n");
130
131 rc = mdb_cursor_open(txn, dbi, &mc);
132 if (rc) return rc;
133
134 while ((rc = mdb_cursor_get(mc, &key, &data, MDB_NEXT) == MDB_SUCCESS)) {
135 if (gotsig) {
136 rc = EINTR;
137 break;
138 }
139 if (mode & PRINT) {
140 text(&key);
141 text(&data);
142 } else {
143 byte(&key);
144 byte(&data);
145 }
146 }
147 printf("DATA=END\n");
148 if (rc == MDB_NOTFOUND)
149 rc = MDB_SUCCESS;
150
151 return rc;
152 }
153
154 static void usage(char *prog)
155 {
156 fprintf(stderr, "usage: %s [-V] [-f output] [-l] [-n] [-p] [-v] [-a|-s subdb] dbpath\n", prog);
157 exit(EXIT_FAILURE);
158 }
159
160 int main(int argc, char *argv[])
161 {
162 int i, rc;
163 MDB_env *env;
164 MDB_txn *txn;
165 MDB_dbi dbi;
166 char *prog = argv[0];
167 char *envname;
168 char *subname = NULL;
169 int alldbs = 0, envflags = 0, list = 0;
170
171 if (argc < 2) {
172 usage(prog);
173 }
174
175 /* -a: dump main DB and all subDBs
176 * -s: dump only the named subDB
177 * -n: use NOSUBDIR flag on env_open
178 * -p: use printable characters
179 * -f: write to file instead of stdout
180 * -v: use previous snapshot
181 * -V: print version and exit
182 * (default) dump only the main DB
183 */
184 while ((i = getopt(argc, argv, "af:lnps:V")) != EOF) {
185 switch(i) {
186 case 'V':
187 printf("%s\n", MDB_VERSION_STRING);
188 exit(0);
189 break;
190 case 'l':
191 list = 1;
192 /*FALLTHROUGH*/;
193 case 'a':
194 if (subname)
195 usage(prog);
196 alldbs++;
197 break;
198 case 'f':
199 if (freopen(optarg, "w", stdout) == NULL) {
200 fprintf(stderr, "%s: %s: reopen: %s\n",
201 prog, optarg, strerror(errno));
202 exit(EXIT_FAILURE);
203 }
204 break;
205 case 'n':
206 envflags |= MDB_NOSUBDIR;
207 break;
208 case 'v':
209 envflags |= MDB_PREVSNAPSHOT;
210 break;
211 case 'p':
212 mode |= PRINT;
213 break;
214 case 's':
215 if (alldbs)
216 usage(prog);
217 subname = optarg;
218 break;
219 default:
220 usage(prog);
221 }
222 }
223
224 if (optind != argc - 1)
225 usage(prog);
226
227 #ifdef SIGPIPE
228 signal(SIGPIPE, dumpsig);
229 #endif
230 #ifdef SIGHUP
231 signal(SIGHUP, dumpsig);
232 #endif
233 signal(SIGINT, dumpsig);
234 signal(SIGTERM, dumpsig);
235
236 envname = argv[optind];
237 rc = mdb_env_create(&env);
238 if (rc) {
239 fprintf(stderr, "mdb_env_create failed, error %d %s\n", rc, mdb_strerror(rc));
240 return EXIT_FAILURE;
241 }
242
243 if (alldbs || subname) {
244 mdb_env_set_maxdbs(env, 2);
245 }
246
247 rc = mdb_env_open(env, envname, envflags | MDB_RDONLY, 0664);
248 if (rc) {
249 fprintf(stderr, "mdb_env_open failed, error %d %s\n", rc, mdb_strerror(rc));
250 goto env_close;
251 }
252
253 rc = mdb_txn_begin(env, NULL, MDB_RDONLY, &txn);
254 if (rc) {
255 fprintf(stderr, "mdb_txn_begin failed, error %d %s\n", rc, mdb_strerror(rc));
256 goto env_close;
257 }
258
259 rc = mdb_open(txn, subname, 0, &dbi);
260 if (rc) {
261 fprintf(stderr, "mdb_open failed, error %d %s\n", rc, mdb_strerror(rc));
262 goto txn_abort;
263 }
264
265 if (alldbs) {
266 MDB_cursor *cursor;
267 MDB_val key;
268 int count = 0;
269
270 rc = mdb_cursor_open(txn, dbi, &cursor);
271 if (rc) {
272 fprintf(stderr, "mdb_cursor_open failed, error %d %s\n", rc, mdb_strerror(rc));
273 goto txn_abort;
274 }
275 while ((rc = mdb_cursor_get(cursor, &key, NULL, MDB_NEXT_NODUP)) == 0) {
276 char *str;
277 MDB_dbi db2;
278 if (memchr(key.mv_data, '\0', key.mv_size))
279 continue;
280 count++;
281 str = malloc(key.mv_size+1);
282 memcpy(str, key.mv_data, key.mv_size);
283 str[key.mv_size] = '\0';
284 rc = mdb_open(txn, str, 0, &db2);
285 if (rc == MDB_SUCCESS) {
286 if (list) {
287 printf("%s\n", str);
288 list++;
289 } else {
290 rc = dumpit(txn, db2, str);
291 if (rc)
292 break;
293 }
294 mdb_close(env, db2);
295 }
296 free(str);
297 if (rc) continue;
298 }
299 mdb_cursor_close(cursor);
300 if (!count) {
301 fprintf(stderr, "%s: %s does not contain multiple databases\n", prog, envname);
302 rc = MDB_NOTFOUND;
303 } else if (rc == MDB_NOTFOUND) {
304 rc = MDB_SUCCESS;
305 }
306 } else {
307 rc = dumpit(txn, dbi, subname);
308 }
309 if (rc && rc != MDB_NOTFOUND)
310 fprintf(stderr, "%s: %s: %s\n", prog, envname, mdb_strerror(rc));
311
312 mdb_close(env, dbi);
313 txn_abort:
314 mdb_txn_abort(txn);
315 env_close:
316 mdb_env_close(env);
317
318 return rc ? EXIT_FAILURE : EXIT_SUCCESS;
319 }