1 /* mdb_load.c - memory-mapped database load tool */
3 * Copyright 2011-2014 Howard Chu, Symas Corp.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted only as authorized by the OpenLDAP
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>.
26 static char *subname = NULL;
37 static MDB_val kbuf, dbuf;
39 #define STRLENOF(s) (sizeof(s)-1)
41 typedef struct flagbit {
47 #define S(s) s, STRLENOF(s)
50 { MDB_REVERSEKEY, S("reversekey") },
51 { MDB_DUPSORT, S("dupsort") },
52 { MDB_INTEGERKEY, S("integerkey") },
53 { MDB_DUPFIXED, S("dupfixed") },
54 { MDB_INTEGERDUP, S("integerdup") },
55 { MDB_REVERSEDUP, S("reversedup") },
59 static const char hexc[] = "0123456789abcdef";
65 while (fgets(dbuf.mv_data, dbuf.mv_size, stdin) != NULL) {
67 if (!strncmp(dbuf.mv_data, "VERSION=", STRLENOF("VERSION="))) {
68 version=atoi((char *)dbuf.mv_data+STRLENOF("VERSION="));
70 fprintf(stderr, "%s: line %zd: unsupported VERSION %d\n",
71 prog, lineno, version);
74 } else if (!strncmp(dbuf.mv_data, "HEADER=END", STRLENOF("HEADER=END"))) {
76 } else if (!strncmp(dbuf.mv_data, "format=", STRLENOF("format="))) {
77 if (!strncmp((char *)dbuf.mv_data+STRLENOF("FORMAT="), "print", STRLENOF("print")))
79 else if (strncmp((char *)dbuf.mv_data+STRLENOF("FORMAT="), "bytevalue", STRLENOF("bytevalue"))) {
80 fprintf(stderr, "%s: line %zd: unsupported FORMAT %s\n",
81 prog, lineno, (char *)dbuf.mv_data+STRLENOF("FORMAT="));
84 } else if (!strncmp(dbuf.mv_data, "database=", STRLENOF("database="))) {
85 ptr = memchr(dbuf.mv_data, '\n', dbuf.mv_size);
87 if (subname) free(subname);
88 subname = strdup((char *)dbuf.mv_data+STRLENOF("database="));
89 } else if (!strncmp(dbuf.mv_data, "type=", STRLENOF("type="))) {
90 if (strncmp((char *)dbuf.mv_data+STRLENOF("type="), "btree", STRLENOF("btree"))) {
91 fprintf(stderr, "%s: line %zd: unsupported type %s\n",
92 prog, lineno, (char *)dbuf.mv_data+STRLENOF("type="));
97 for (i=0; dbflags[i].bit; i++) {
98 if (!strncmp(dbuf.mv_data, dbflags[i].name, dbflags[i].len) &&
99 ((char *)dbuf.mv_data)[dbflags[i].len] == '=') {
100 flags |= dbflags[i].bit;
104 if (!dbflags[i].bit) {
105 ptr = memchr(dbuf.mv_data, '=', dbuf.mv_size);
107 fprintf(stderr, "%s: line %zd: unexpected format\n",
112 fprintf(stderr, "%s: line %zd: unrecognized keyword ignored: %s\n",
113 prog, lineno, (char *)dbuf.mv_data);
122 fprintf(stderr, "%s: line %zd: unexpected end of input\n",
126 static int unhex(unsigned char *c2)
140 static int readline(MDB_val *out, MDB_val *buf)
142 unsigned char *c1, *c2, *end;
146 if (!(mode & NOHDR)) {
154 if (fgets(buf->mv_data, buf->mv_size, stdin) == NULL) {
160 if (c == 'D' && !strncmp(buf->mv_data, "ATA=END", STRLENOF("ATA=END")))
165 if (fgets(buf->mv_data, buf->mv_size, stdin) == NULL) {
172 len = strlen((char *)c1);
174 /* Is buffer too short? */
175 while (c1[len-1] != '\n') {
176 buf->mv_data = realloc(buf->mv_data, buf->mv_size*2);
179 fprintf(stderr, "%s: line %zd: out of memory, line too long\n",
185 if (fgets((char *)c1, buf->mv_size, stdin) == NULL) {
191 len = strlen((char *)c1);
193 c1 = c2 = buf->mv_data;
194 len = strlen((char *)c1);
204 if (c2+3 >= end || !isxdigit(c2[1]) || !isxdigit(c2[2])) {
217 /* odd length not allowed */
224 if (!isxdigit(*c2) || !isxdigit(c2[1])) {
233 c2 = out->mv_data = buf->mv_data;
234 out->mv_size = c1 - c2;
241 fprintf(stderr, "usage: %s dbpath [-V] [-f input] [-n] [-s name] [-N] [-T]\n", prog);
245 int main(int argc, char *argv[])
253 int envflags = 0, putflags = 0;
261 /* -f: load file instead of stdin
262 * -n: use NOSUBDIR flag on env_open
263 * -s: load into named subDB
264 * -N: use NOOVERWRITE on puts
266 * -V: print version and exit
268 while ((i = getopt(argc, argv, "f:ns:NTV")) != EOF) {
271 printf("%s\n", MDB_VERSION_STRING);
275 if (freopen(optarg, "r", stdin) == NULL) {
276 fprintf(stderr, "%s: %s: reopen: %s\n",
277 prog, optarg, strerror(errno));
282 envflags |= MDB_NOSUBDIR;
285 subname = strdup(optarg);
288 putflags = MDB_NOOVERWRITE|MDB_NODUPDATA;
298 if (optind != argc - 1)
301 envname = argv[optind];
302 rc = mdb_env_create(&env);
304 mdb_env_set_maxdbs(env, 2);
306 rc = mdb_env_open(env, envname, envflags, 0664);
308 fprintf(stderr, "mdb_env_open failed, error %d %s\n", rc, mdb_strerror(rc));
312 kbuf.mv_size = mdb_env_get_maxkeysize(env) * 2 + 2;
313 kbuf.mv_data = malloc(kbuf.mv_size);
315 dbuf.mv_data = malloc(dbuf.mv_size);
325 rc = mdb_txn_begin(env, NULL, 0, &txn);
327 fprintf(stderr, "mdb_txn_begin failed, error %d %s\n", rc, mdb_strerror(rc));
331 rc = mdb_open(txn, subname, flags|MDB_CREATE, &dbi);
333 fprintf(stderr, "mdb_open failed, error %d %s\n", rc, mdb_strerror(rc));
337 rc = mdb_cursor_open(txn, dbi, &mc);
339 fprintf(stderr, "mdb_cursor_open failed, error %d %s\n", rc, mdb_strerror(rc));
344 rc = readline(&key, &kbuf);
350 rc = readline(&data, &dbuf);
354 rc = mdb_cursor_put(mc, &key, &data, putflags);
355 if (rc == MDB_KEYEXIST && putflags)
361 rc = mdb_txn_commit(txn);
363 fprintf(stderr, "%s: line %zd: txn_commit: %s\n",
364 prog, lineno, mdb_strerror(rc));
367 rc = mdb_txn_begin(env, NULL, 0, &txn);
369 fprintf(stderr, "mdb_txn_begin failed, error %d %s\n", rc, mdb_strerror(rc));
372 rc = mdb_cursor_open(txn, dbi, &mc);
374 fprintf(stderr, "mdb_cursor_open failed, error %d %s\n", rc, mdb_strerror(rc));
380 rc = mdb_txn_commit(txn);
383 fprintf(stderr, "%s: line %zd: txn_commit: %s\n",
384 prog, lineno, mdb_strerror(rc));
387 mdb_dbi_close(env, dbi);
395 return rc ? EXIT_FAILURE : EXIT_SUCCESS;