]> git.sur5r.net Git - openldap/blob - libraries/liblmdb/mdb_dump.c
Happy New Year
[openldap] / libraries / liblmdb / mdb_dump.c
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                         putchar(*c);
68                 } else {
69                         putchar('\\');
70                         hex(*c);
71                 }
72                 c++;
73         }
74         putchar('\n');
75 }
76
77 static void byte(MDB_val *v)
78 {
79         unsigned char *c, *end;
80
81         putchar(' ');
82         c = v->mv_data;
83         end = c + v->mv_size;
84         while (c < end) {
85                 hex(*c++);
86         }
87         putchar('\n');
88 }
89
90 /* Dump in BDB-compatible format */
91 static int dumpit(MDB_txn *txn, MDB_dbi dbi, char *name)
92 {
93         MDB_cursor *mc;
94         MDB_stat ms;
95         MDB_val key, data;
96         MDB_envinfo info;
97         unsigned int flags;
98         int rc, i;
99
100         rc = mdb_dbi_flags(txn, dbi, &flags);
101         if (rc) return rc;
102
103         rc = mdb_stat(txn, dbi, &ms);
104         if (rc) return rc;
105
106         rc = mdb_env_info(mdb_txn_env(txn), &info);
107         if (rc) return rc;
108
109         printf("VERSION=3\n");
110         printf("format=%s\n", mode & PRINT ? "print" : "bytevalue");
111         if (name)
112                 printf("database=%s\n", name);
113         printf("type=btree\n");
114         printf("mapsize=%"Yu"\n", info.me_mapsize);
115         if (info.me_mapaddr)
116                 printf("mapaddr=%p\n", info.me_mapaddr);
117         printf("maxreaders=%u\n", info.me_maxreaders);
118
119         if (flags & MDB_DUPSORT)
120                 printf("duplicates=1\n");
121
122         for (i=0; dbflags[i].bit; i++)
123                 if (flags & dbflags[i].bit)
124                         printf("%s=1\n", dbflags[i].name);
125
126         printf("db_pagesize=%d\n", ms.ms_psize);
127         printf("HEADER=END\n");
128
129         rc = mdb_cursor_open(txn, dbi, &mc);
130         if (rc) return rc;
131
132         while ((rc = mdb_cursor_get(mc, &key, &data, MDB_NEXT) == MDB_SUCCESS)) {
133                 if (gotsig) {
134                         rc = EINTR;
135                         break;
136                 }
137                 if (mode & PRINT) {
138                         text(&key);
139                         text(&data);
140                 } else {
141                         byte(&key);
142                         byte(&data);
143                 }
144         }
145         printf("DATA=END\n");
146         if (rc == MDB_NOTFOUND)
147                 rc = MDB_SUCCESS;
148
149         return rc;
150 }
151
152 static void usage(char *prog)
153 {
154         fprintf(stderr, "usage: %s [-V] [-f output] [-l] [-n] [-p] [-a|-s subdb] dbpath\n", prog);
155         exit(EXIT_FAILURE);
156 }
157
158 int main(int argc, char *argv[])
159 {
160         int i, rc;
161         MDB_env *env;
162         MDB_txn *txn;
163         MDB_dbi dbi;
164         char *prog = argv[0];
165         char *envname;
166         char *subname = NULL;
167         int alldbs = 0, envflags = 0, list = 0;
168
169         if (argc < 2) {
170                 usage(prog);
171         }
172
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
180          */
181         while ((i = getopt(argc, argv, "af:lnps:V")) != EOF) {
182                 switch(i) {
183                 case 'V':
184                         printf("%s\n", MDB_VERSION_STRING);
185                         exit(0);
186                         break;
187                 case 'l':
188                         list = 1;
189                         /*FALLTHROUGH*/;
190                 case 'a':
191                         if (subname)
192                                 usage(prog);
193                         alldbs++;
194                         break;
195                 case 'f':
196                         if (freopen(optarg, "w", stdout) == NULL) {
197                                 fprintf(stderr, "%s: %s: reopen: %s\n",
198                                         prog, optarg, strerror(errno));
199                                 exit(EXIT_FAILURE);
200                         }
201                         break;
202                 case 'n':
203                         envflags |= MDB_NOSUBDIR;
204                         break;
205                 case 'p':
206                         mode |= PRINT;
207                         break;
208                 case 's':
209                         if (alldbs)
210                                 usage(prog);
211                         subname = optarg;
212                         break;
213                 default:
214                         usage(prog);
215                 }
216         }
217
218         if (optind != argc - 1)
219                 usage(prog);
220
221 #ifdef SIGPIPE
222         signal(SIGPIPE, dumpsig);
223 #endif
224 #ifdef SIGHUP
225         signal(SIGHUP, dumpsig);
226 #endif
227         signal(SIGINT, dumpsig);
228         signal(SIGTERM, dumpsig);
229
230         envname = argv[optind];
231         rc = mdb_env_create(&env);
232         if (rc) {
233                 fprintf(stderr, "mdb_env_create failed, error %d %s\n", rc, mdb_strerror(rc));
234                 return EXIT_FAILURE;
235         }
236
237         if (alldbs || subname) {
238                 mdb_env_set_maxdbs(env, 2);
239         }
240
241         rc = mdb_env_open(env, envname, envflags | MDB_RDONLY, 0664);
242         if (rc) {
243                 fprintf(stderr, "mdb_env_open failed, error %d %s\n", rc, mdb_strerror(rc));
244                 goto env_close;
245         }
246
247         rc = mdb_txn_begin(env, NULL, MDB_RDONLY, &txn);
248         if (rc) {
249                 fprintf(stderr, "mdb_txn_begin failed, error %d %s\n", rc, mdb_strerror(rc));
250                 goto env_close;
251         }
252
253         rc = mdb_open(txn, subname, 0, &dbi);
254         if (rc) {
255                 fprintf(stderr, "mdb_open failed, error %d %s\n", rc, mdb_strerror(rc));
256                 goto txn_abort;
257         }
258
259         if (alldbs) {
260                 MDB_cursor *cursor;
261                 MDB_val key;
262                 int count = 0;
263
264                 rc = mdb_cursor_open(txn, dbi, &cursor);
265                 if (rc) {
266                         fprintf(stderr, "mdb_cursor_open failed, error %d %s\n", rc, mdb_strerror(rc));
267                         goto txn_abort;
268                 }
269                 while ((rc = mdb_cursor_get(cursor, &key, NULL, MDB_NEXT_NODUP)) == 0) {
270                         char *str;
271                         MDB_dbi db2;
272                         if (memchr(key.mv_data, '\0', key.mv_size))
273                                 continue;
274                         count++;
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) {
280                                 if (list) {
281                                         printf("%s\n", str);
282                                         list++;
283                                 } else {
284                                         rc = dumpit(txn, db2, str);
285                                         if (rc)
286                                                 break;
287                                 }
288                                 mdb_close(env, db2);
289                         }
290                         free(str);
291                         if (rc) continue;
292                 }
293                 mdb_cursor_close(cursor);
294                 if (!count) {
295                         fprintf(stderr, "%s: %s does not contain multiple databases\n", prog, envname);
296                         rc = MDB_NOTFOUND;
297                 } else if (rc == MDB_NOTFOUND) {
298                         rc = MDB_SUCCESS;
299                 }
300         } else {
301                 rc = dumpit(txn, dbi, subname);
302         }
303         if (rc && rc != MDB_NOTFOUND)
304                 fprintf(stderr, "%s: %s: %s\n", prog, envname, mdb_strerror(rc));
305
306         mdb_close(env, dbi);
307 txn_abort:
308         mdb_txn_abort(txn);
309 env_close:
310         mdb_env_close(env);
311
312         return rc ? EXIT_FAILURE : EXIT_SUCCESS;
313 }