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