1 /* mdb_load.c - memory-mapped database load tool */
3 * Copyright 2011-2015 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_envinfo info;
39 static MDB_val kbuf, dbuf;
47 #define STRLENOF(s) (sizeof(s)-1)
49 typedef struct flagbit {
55 #define S(s) s, STRLENOF(s)
58 { MDB_REVERSEKEY, S("reversekey") },
59 { MDB_DUPSORT, S("dupsort") },
60 { MDB_INTEGERKEY, S("integerkey") },
61 { MDB_DUPFIXED, S("dupfixed") },
62 { MDB_INTEGERDUP, S("integerdup") },
63 { MDB_REVERSEDUP, S("reversedup") },
67 static void readhdr(void)
71 while (fgets(dbuf.mv_data, dbuf.mv_size, stdin) != NULL) {
73 if (!strncmp(dbuf.mv_data, "VERSION=", STRLENOF("VERSION="))) {
74 version=atoi((char *)dbuf.mv_data+STRLENOF("VERSION="));
76 fprintf(stderr, "%s: line %" Z "d: unsupported VERSION %d\n",
77 prog, lineno, version);
80 } else if (!strncmp(dbuf.mv_data, "HEADER=END", STRLENOF("HEADER=END"))) {
82 } else if (!strncmp(dbuf.mv_data, "format=", STRLENOF("format="))) {
83 if (!strncmp((char *)dbuf.mv_data+STRLENOF("FORMAT="), "print", STRLENOF("print")))
85 else if (strncmp((char *)dbuf.mv_data+STRLENOF("FORMAT="), "bytevalue", STRLENOF("bytevalue"))) {
86 fprintf(stderr, "%s: line %" Z "d: unsupported FORMAT %s\n",
87 prog, lineno, (char *)dbuf.mv_data+STRLENOF("FORMAT="));
90 } else if (!strncmp(dbuf.mv_data, "database=", STRLENOF("database="))) {
91 ptr = memchr(dbuf.mv_data, '\n', dbuf.mv_size);
93 if (subname) free(subname);
94 subname = strdup((char *)dbuf.mv_data+STRLENOF("database="));
95 } else if (!strncmp(dbuf.mv_data, "type=", STRLENOF("type="))) {
96 if (strncmp((char *)dbuf.mv_data+STRLENOF("type="), "btree", STRLENOF("btree"))) {
97 fprintf(stderr, "%s: line %" Z "d: unsupported type %s\n",
98 prog, lineno, (char *)dbuf.mv_data+STRLENOF("type="));
101 } else if (!strncmp(dbuf.mv_data, "mapaddr=", STRLENOF("mapaddr="))) {
103 ptr = memchr(dbuf.mv_data, '\n', dbuf.mv_size);
104 if (ptr) *ptr = '\0';
105 i = sscanf((char *)dbuf.mv_data+STRLENOF("mapaddr="), "%p", &info.me_mapaddr);
107 fprintf(stderr, "%s: line %" Z "d: invalid mapaddr %s\n",
108 prog, lineno, (char *)dbuf.mv_data+STRLENOF("mapaddr="));
111 } else if (!strncmp(dbuf.mv_data, "mapsize=", STRLENOF("mapsize="))) {
113 ptr = memchr(dbuf.mv_data, '\n', dbuf.mv_size);
114 if (ptr) *ptr = '\0';
115 i = sscanf((char *)dbuf.mv_data+STRLENOF("mapsize="), "%" Z "u", &info.me_mapsize);
117 fprintf(stderr, "%s: line %" Z "d: invalid mapsize %s\n",
118 prog, lineno, (char *)dbuf.mv_data+STRLENOF("mapsize="));
121 } else if (!strncmp(dbuf.mv_data, "maxreaders=", STRLENOF("maxreaders="))) {
123 ptr = memchr(dbuf.mv_data, '\n', dbuf.mv_size);
124 if (ptr) *ptr = '\0';
125 i = sscanf((char *)dbuf.mv_data+STRLENOF("maxreaders="), "%u", &info.me_maxreaders);
127 fprintf(stderr, "%s: line %" Z "d: invalid maxreaders %s\n",
128 prog, lineno, (char *)dbuf.mv_data+STRLENOF("maxreaders="));
133 for (i=0; dbflags[i].bit; i++) {
134 if (!strncmp(dbuf.mv_data, dbflags[i].name, dbflags[i].len) &&
135 ((char *)dbuf.mv_data)[dbflags[i].len] == '=') {
136 flags |= dbflags[i].bit;
140 if (!dbflags[i].bit) {
141 ptr = memchr(dbuf.mv_data, '=', dbuf.mv_size);
143 fprintf(stderr, "%s: line %" Z "d: unexpected format\n",
148 fprintf(stderr, "%s: line %" Z "d: unrecognized keyword ignored: %s\n",
149 prog, lineno, (char *)dbuf.mv_data);
156 static void badend(void)
158 fprintf(stderr, "%s: line %" Z "d: unexpected end of input\n",
162 static int unhex(unsigned char *c2)
176 static int readline(MDB_val *out, MDB_val *buf)
178 unsigned char *c1, *c2, *end;
182 if (!(mode & NOHDR)) {
190 if (fgets(buf->mv_data, buf->mv_size, stdin) == NULL) {
196 if (c == 'D' && !strncmp(buf->mv_data, "ATA=END", STRLENOF("ATA=END")))
201 if (fgets(buf->mv_data, buf->mv_size, stdin) == NULL) {
208 len = strlen((char *)c1);
211 /* Is buffer too short? */
212 while (c1[len-1] != '\n') {
213 buf->mv_data = realloc(buf->mv_data, buf->mv_size*2);
216 fprintf(stderr, "%s: line %" Z "d: out of memory, line too long\n",
222 if (fgets((char *)c1, buf->mv_size+1, stdin) == NULL) {
228 len = strlen((char *)c1);
231 c1 = c2 = buf->mv_data;
242 if (c2+3 > end || !isxdigit(c2[1]) || !isxdigit(c2[2])) {
255 /* odd length not allowed */
262 if (!isxdigit(*c2) || !isxdigit(c2[1])) {
271 c2 = out->mv_data = buf->mv_data;
272 out->mv_size = c1 - c2;
277 static void usage(void)
279 fprintf(stderr, "usage: %s [-V] [-f input] [-n] [-s name] [-N] [-T] dbpath\n", prog);
283 int main(int argc, char *argv[])
291 int envflags = 0, putflags = 0;
300 /* -f: load file instead of stdin
301 * -n: use NOSUBDIR flag on env_open
302 * -s: load into named subDB
303 * -N: use NOOVERWRITE on puts
305 * -V: print version and exit
307 while ((i = getopt(argc, argv, "f:ns:NTV")) != EOF) {
310 printf("%s\n", MDB_VERSION_STRING);
314 if (freopen(optarg, "r", stdin) == NULL) {
315 fprintf(stderr, "%s: %s: reopen: %s\n",
316 prog, optarg, strerror(errno));
321 envflags |= MDB_NOSUBDIR;
324 subname = strdup(optarg);
327 putflags = MDB_NOOVERWRITE|MDB_NODUPDATA;
337 if (optind != argc - 1)
341 dbuf.mv_data = malloc(dbuf.mv_size);
346 envname = argv[optind];
347 rc = mdb_env_create(&env);
349 fprintf(stderr, "mdb_env_create failed, error %d %s\n", rc, mdb_strerror(rc));
353 mdb_env_set_maxdbs(env, 2);
355 if (info.me_maxreaders)
356 mdb_env_set_maxreaders(env, info.me_maxreaders);
359 mdb_env_set_mapsize(env, info.me_mapsize);
362 envflags |= MDB_FIXEDMAP;
364 rc = mdb_env_open(env, envname, envflags, 0664);
366 fprintf(stderr, "mdb_env_open failed, error %d %s\n", rc, mdb_strerror(rc));
370 kbuf.mv_size = mdb_env_get_maxkeysize(env) * 2 + 2;
371 kbuf.mv_data = malloc(kbuf.mv_size);
380 } else if (!(mode & NOHDR))
383 rc = mdb_txn_begin(env, NULL, 0, &txn);
385 fprintf(stderr, "mdb_txn_begin failed, error %d %s\n", rc, mdb_strerror(rc));
389 rc = mdb_open(txn, subname, flags|MDB_CREATE, &dbi);
391 fprintf(stderr, "mdb_open failed, error %d %s\n", rc, mdb_strerror(rc));
395 rc = mdb_cursor_open(txn, dbi, &mc);
397 fprintf(stderr, "mdb_cursor_open failed, error %d %s\n", rc, mdb_strerror(rc));
402 rc = readline(&key, &kbuf);
403 if (rc) /* rc == EOF */
406 rc = readline(&data, &dbuf);
408 fprintf(stderr, "%s: line %" Z "d: failed to read key value\n", prog, lineno);
412 rc = mdb_cursor_put(mc, &key, &data, putflags);
413 if (rc == MDB_KEYEXIST && putflags)
416 fprintf(stderr, "mdb_cursor_put failed, error %d %s\n", rc, mdb_strerror(rc));
421 rc = mdb_txn_commit(txn);
423 fprintf(stderr, "%s: line %" Z "d: txn_commit: %s\n",
424 prog, lineno, mdb_strerror(rc));
427 rc = mdb_txn_begin(env, NULL, 0, &txn);
429 fprintf(stderr, "mdb_txn_begin failed, error %d %s\n", rc, mdb_strerror(rc));
432 rc = mdb_cursor_open(txn, dbi, &mc);
434 fprintf(stderr, "mdb_cursor_open failed, error %d %s\n", rc, mdb_strerror(rc));
440 rc = mdb_txn_commit(txn);
443 fprintf(stderr, "%s: line %" Z "d: txn_commit: %s\n",
444 prog, lineno, mdb_strerror(rc));
447 mdb_dbi_close(env, dbi);
455 return rc ? EXIT_FAILURE : EXIT_SUCCESS;