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