1 /* mdb_dump.c - memory-mapped database dump tool */
3 * Copyright 2011-2014 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>.
32 typedef struct flagbit {
38 { MDB_REVERSEKEY, "reversekey" },
39 { MDB_DUPSORT, "dupsort" },
40 { MDB_INTEGERKEY, "integerkey" },
41 { MDB_DUPFIXED, "dupfixed" },
42 { MDB_INTEGERDUP, "integerdup" },
43 { MDB_REVERSEDUP, "reversedup" },
47 static volatile sig_atomic_t gotsig;
49 static void dumpsig( int sig )
54 static const char hexc[] = "0123456789abcdef";
56 static void hex(unsigned char c)
58 putchar(hexc[c >> 4]);
59 putchar(hexc[c & 0xf]);
62 static void text(MDB_val *v)
64 unsigned char *c, *end;
81 static void byte(MDB_val *v)
83 unsigned char *c, *end;
94 /* Dump in BDB-compatible format */
95 static int dumpit(MDB_txn *txn, MDB_dbi dbi, char *name)
104 rc = mdb_dbi_flags(txn, dbi, &flags);
107 rc = mdb_stat(txn, dbi, &ms);
110 rc = mdb_env_info(mdb_txn_env(txn), &info);
113 printf("VERSION=3\n");
114 printf("format=%s\n", mode & PRINT ? "print" : "bytevalue");
116 printf("database=%s\n", name);
117 printf("type=btree\n");
118 printf("mapsize=%" Z "u\n", info.me_mapsize);
120 printf("mapaddr=%p\n", info.me_mapaddr);
121 printf("maxreaders=%u\n", info.me_maxreaders);
123 if (flags & MDB_DUPSORT)
124 printf("duplicates=1\n");
126 for (i=0; dbflags[i].bit; i++)
127 if (flags & dbflags[i].bit)
128 printf("%s=1\n", dbflags[i].name);
130 printf("db_pagesize=%d\n", ms.ms_psize);
131 printf("HEADER=END\n");
133 rc = mdb_cursor_open(txn, dbi, &mc);
136 while ((rc = mdb_cursor_get(mc, &key, &data, MDB_NEXT) == MDB_SUCCESS)) {
149 printf("DATA=END\n");
150 if (rc == MDB_NOTFOUND)
156 static void usage(char *prog)
158 fprintf(stderr, "usage: %s dbpath [-V] [-f output] [-l] [-n] [-p] [-a|-s subdb]\n", prog);
162 int main(int argc, char *argv[])
168 char *prog = argv[0];
170 char *subname = NULL;
171 int alldbs = 0, envflags = 0, list = 0;
177 /* -a: dump main DB and all subDBs
178 * -s: dump only the named subDB
179 * -n: use NOSUBDIR flag on env_open
180 * -p: use printable characters
181 * -f: write to file instead of stdout
182 * -V: print version and exit
183 * (default) dump only the main DB
185 while ((i = getopt(argc, argv, "af:lnps:V")) != EOF) {
188 printf("%s\n", MDB_VERSION_STRING);
200 if (freopen(optarg, "w", stdout) == NULL) {
201 fprintf(stderr, "%s: %s: reopen: %s\n",
202 prog, optarg, strerror(errno));
207 envflags |= MDB_NOSUBDIR;
222 if (optind != argc - 1)
226 signal(SIGPIPE, dumpsig);
229 signal(SIGHUP, dumpsig);
231 signal(SIGINT, dumpsig);
232 signal(SIGTERM, dumpsig);
234 envname = argv[optind];
235 rc = mdb_env_create(&env);
237 fprintf(stderr, "mdb_env_create failed, error %d %s\n", rc, mdb_strerror(rc));
241 if (alldbs || subname) {
242 mdb_env_set_maxdbs(env, 2);
245 rc = mdb_env_open(env, envname, envflags | MDB_RDONLY, 0664);
247 fprintf(stderr, "mdb_env_open failed, error %d %s\n", rc, mdb_strerror(rc));
251 rc = mdb_txn_begin(env, NULL, MDB_RDONLY, &txn);
253 fprintf(stderr, "mdb_txn_begin failed, error %d %s\n", rc, mdb_strerror(rc));
257 rc = mdb_open(txn, subname, 0, &dbi);
259 fprintf(stderr, "mdb_open failed, error %d %s\n", rc, mdb_strerror(rc));
268 rc = mdb_cursor_open(txn, dbi, &cursor);
270 fprintf(stderr, "mdb_cursor_open failed, error %d %s\n", rc, mdb_strerror(rc));
273 while ((rc = mdb_cursor_get(cursor, &key, NULL, MDB_NEXT_NODUP)) == 0) {
276 if (memchr(key.mv_data, '\0', key.mv_size))
279 str = malloc(key.mv_size+1);
280 memcpy(str, key.mv_data, key.mv_size);
281 str[key.mv_size] = '\0';
282 rc = mdb_open(txn, str, 0, &db2);
283 if (rc == MDB_SUCCESS) {
288 rc = dumpit(txn, db2, str);
297 mdb_cursor_close(cursor);
299 fprintf(stderr, "%s: %s does not contain multiple databases\n", prog, envname);
301 } else if (rc == MDB_NOTFOUND) {
305 rc = dumpit(txn, dbi, subname);
307 if (rc && rc != MDB_NOTFOUND)
308 fprintf(stderr, "%s: %s: %s\n", prog, envname, mdb_strerror(rc));
316 return rc ? EXIT_FAILURE : EXIT_SUCCESS;