From e1266bf33174383cec0a1a8a25133cf86bbd51ff Mon Sep 17 00:00:00 2001 From: Hallvard Furuseth Date: Wed, 11 Dec 2013 11:57:12 +0100 Subject: [PATCH] Clean up mdb_copy. Support MDB_NOSUBDIR. Catch more errors. Write messages to stderr, not stdout which the data too may be piped to. --- libraries/liblmdb/mdb_copy.1 | 10 +++++++++- libraries/liblmdb/mdb_copy.c | 28 +++++++++++++++++++--------- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/libraries/liblmdb/mdb_copy.1 b/libraries/liblmdb/mdb_copy.1 index 9b053f97d5..b4ab7531a8 100644 --- a/libraries/liblmdb/mdb_copy.1 +++ b/libraries/liblmdb/mdb_copy.1 @@ -5,7 +5,11 @@ mdb_copy \- LMDB environment copy tool .SH SYNOPSIS .B mdb_copy -.I srcpath\ [dstpath] +[\c +.BR \-n ] +.B srcpath +[\c +.BR dstpath ] .SH DESCRIPTION The .B mdb_copy @@ -19,6 +23,10 @@ is specified it must be the path of an empty directory for storing the backup. Otherwise, the backup will be written to stdout. +.SH OPTIONS +.BR \-n +Open LDMB environment(s) which do not use subdirectories. + .SH DIAGNOSTICS Exit status is zero if no errors occur. Errors result in a non-zero exit status and diff --git a/libraries/liblmdb/mdb_copy.c b/libraries/liblmdb/mdb_copy.c index ca92009cff..bbf0dc902b 100644 --- a/libraries/liblmdb/mdb_copy.c +++ b/libraries/liblmdb/mdb_copy.c @@ -31,10 +31,18 @@ int main(int argc,char * argv[]) { int rc; MDB_env *env; - char *envname = argv[1]; + const char *progname = argv[0], *act; + unsigned flags = MDB_RDONLY; + + for (; argc > 1 && argv[1][0] == '-'; argc--, argv++) { + if (argv[1][1] == 'n' && argv[1][2] == '\0') + flags |= MDB_NOSUBDIR; + else + argc = 0; + } if (argc<2 || argc>3) { - fprintf(stderr, "usage: %s srcpath [dstpath]\n", argv[0]); + fprintf(stderr, "usage: %s [-n] srcpath [dstpath]\n", progname); exit(EXIT_FAILURE); } @@ -47,19 +55,21 @@ int main(int argc,char * argv[]) signal(SIGINT, sighandle); signal(SIGTERM, sighandle); + act = "opening environment"; rc = mdb_env_create(&env); - - rc = mdb_env_open(env, envname, MDB_RDONLY, 0); - if (rc) { - printf("mdb_env_open failed, error %d %s\n", rc, mdb_strerror(rc)); - } else { + if (rc == MDB_SUCCESS) { + rc = mdb_env_open(env, argv[1], flags, 0); + } + if (rc == MDB_SUCCESS) { + act = "copying"; if (argc == 2) rc = mdb_env_copyfd(env, MDB_STDOUT); else rc = mdb_env_copy(env, argv[2]); - if (rc) - printf("mdb_env_copy failed, error %d %s\n", rc, mdb_strerror(rc)); } + if (rc) + fprintf(stderr, "%s: %s failed, error %d (%s)\n", + progname, act, rc, mdb_strerror(rc)); mdb_env_close(env); return rc ? EXIT_FAILURE : EXIT_SUCCESS; -- 2.39.2