mdb_dump.c 6.2 KB

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