1 /* mdb_dump.c - memory-mapped database dump tool */
3 * Copyright 2011-2016 Howard Chu, Symas Corp.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted only as authorized by the OpenLDAP
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>.
23 #define Yu MDB_PRIy(u)
28 typedef struct flagbit {
34 { MDB_REVERSEKEY, "reversekey" },
35 { MDB_DUPSORT, "dupsort" },
36 { MDB_INTEGERKEY, "integerkey" },
37 { MDB_DUPFIXED, "dupfixed" },
38 { MDB_INTEGERDUP, "integerdup" },
39 { MDB_REVERSEDUP, "reversedup" },
43 static volatile sig_atomic_t gotsig;
45 static void dumpsig( int sig )
50 static const char hexc[] = "0123456789abcdef";
52 static void hex(unsigned char c)
54 putchar(hexc[c >> 4]);
55 putchar(hexc[c & 0xf]);
58 static void text(MDB_val *v)
60 unsigned char *c, *end;
77 static void byte(MDB_val *v)
79 unsigned char *c, *end;
90 /* Dump in BDB-compatible format */
91 static int dumpit(MDB_txn *txn, MDB_dbi dbi, char *name)
100 rc = mdb_dbi_flags(txn, dbi, &flags);
103 rc = mdb_stat(txn, dbi, &ms);
106 rc = mdb_env_info(mdb_txn_env(txn), &info);
109 printf("VERSION=3\n");
110 printf("format=%s\n", mode & PRINT ? "print" : "bytevalue");
112 printf("database=%s\n", name);
113 printf("type=btree\n");
114 printf("mapsize=%"Yu"\n", info.me_mapsize);
116 printf("mapaddr=%p\n", info.me_mapaddr);
117 printf("maxreaders=%u\n", info.me_maxreaders);
119 if (flags & MDB_DUPSORT)
120 printf("duplicates=1\n");
122 for (i=0; dbflags[i].bit; i++)
123 if (flags & dbflags[i].bit)
124 printf("%s=1\n", dbflags[i].name);
126 printf("db_pagesize=%d\n", ms.ms_psize);
127 printf("HEADER=END\n");
129 rc = mdb_cursor_open(txn, dbi, &mc);
132 while ((rc = mdb_cursor_get(mc, &key, &data, MDB_NEXT) == MDB_SUCCESS)) {
145 printf("DATA=END\n");
146 if (rc == MDB_NOTFOUND)
152 static void usage(char *prog)
154 fprintf(stderr, "usage: %s [-V] [-f output] [-l] [-n] [-p] [-a|-s subdb] dbpath\n", prog);
158 int main(int argc, char *argv[])
164 char *prog = argv[0];
166 char *subname = NULL;
167 int alldbs = 0, envflags = 0, list = 0;
173 /* -a: dump main DB and all subDBs
174 * -s: dump only the named subDB
175 * -n: use NOSUBDIR flag on env_open
176 * -p: use printable characters
177 * -f: write to file instead of stdout
178 * -V: print version and exit
179 * (default) dump only the main DB
181 while ((i = getopt(argc, argv, "af:lnps:V")) != EOF) {
184 printf("%s\n", MDB_VERSION_STRING);
196 if (freopen(optarg, "w", stdout) == NULL) {
197 fprintf(stderr, "%s: %s: reopen: %s\n",
198 prog, optarg, strerror(errno));
203 envflags |= MDB_NOSUBDIR;
218 if (optind != argc - 1)
222 signal(SIGPIPE, dumpsig);
225 signal(SIGHUP, dumpsig);
227 signal(SIGINT, dumpsig);
228 signal(SIGTERM, dumpsig);
230 envname = argv[optind];
231 rc = mdb_env_create(&env);
233 fprintf(stderr, "mdb_env_create failed, error %d %s\n", rc, mdb_strerror(rc));
237 if (alldbs || subname) {
238 mdb_env_set_maxdbs(env, 2);
241 rc = mdb_env_open(env, envname, envflags | MDB_RDONLY, 0664);
243 fprintf(stderr, "mdb_env_open failed, error %d %s\n", rc, mdb_strerror(rc));
247 rc = mdb_txn_begin(env, NULL, MDB_RDONLY, &txn);
249 fprintf(stderr, "mdb_txn_begin failed, error %d %s\n", rc, mdb_strerror(rc));
253 rc = mdb_open(txn, subname, 0, &dbi);
255 fprintf(stderr, "mdb_open failed, error %d %s\n", rc, mdb_strerror(rc));
264 rc = mdb_cursor_open(txn, dbi, &cursor);
266 fprintf(stderr, "mdb_cursor_open failed, error %d %s\n", rc, mdb_strerror(rc));
269 while ((rc = mdb_cursor_get(cursor, &key, NULL, MDB_NEXT_NODUP)) == 0) {
272 if (memchr(key.mv_data, '\0', key.mv_size))
275 str = malloc(key.mv_size+1);
276 memcpy(str, key.mv_data, key.mv_size);
277 str[key.mv_size] = '\0';
278 rc = mdb_open(txn, str, 0, &db2);
279 if (rc == MDB_SUCCESS) {
284 rc = dumpit(txn, db2, str);
293 mdb_cursor_close(cursor);
295 fprintf(stderr, "%s: %s does not contain multiple databases\n", prog, envname);
297 } else if (rc == MDB_NOTFOUND) {
301 rc = dumpit(txn, dbi, subname);
303 if (rc && rc != MDB_NOTFOUND)
304 fprintf(stderr, "%s: %s: %s\n", prog, envname, mdb_strerror(rc));
312 return rc ? EXIT_FAILURE : EXIT_SUCCESS;