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