From 9746dd20dfa994e0ffb9aec17d3c025ade17198d Mon Sep 17 00:00:00 2001 From: Leonid Yuriev Date: Wed, 12 May 2021 10:52:13 +0300 Subject: [PATCH] mdbx-tools: refine/fix `quiet` mode. --- src/mdbx_drop.c | 19 ++++++++++++------- src/mdbx_dump.c | 19 ++++++++++++------- src/mdbx_stat.c | 28 ++++++++++++++++++++-------- 3 files changed, 44 insertions(+), 22 deletions(-) diff --git a/src/mdbx_drop.c b/src/mdbx_drop.c index f066f20d..2cc18c21 100644 --- a/src/mdbx_drop.c +++ b/src/mdbx_drop.c @@ -79,11 +79,12 @@ int main(int argc, char *argv[]) { if (argc < 2) usage(); - /* -d: delete the db, don't just empty it - * -s: drop the named subDB - * -V: print version and exit - * (default) empty the main DB */ - while ((i = getopt(argc, argv, "ds:nV")) != EOF) { + while ((i = getopt(argc, argv, + "d" + "s:" + "n" + "q" + "V")) != EOF) { switch (i) { case 'V': printf("mdbx_drop version %d.%d.%d.%d\n" @@ -99,6 +100,9 @@ int main(int argc, char *argv[]) { mdbx_build.target, mdbx_build.compiler, mdbx_build.flags, mdbx_build.options); return EXIT_SUCCESS; + case 'q': + quiet = true; + break; case 'd': delete = true; break; @@ -130,11 +134,12 @@ int main(int argc, char *argv[]) { #endif /* !WINDOWS */ envname = argv[optind]; - if (!quiet) + if (!quiet) { printf("mdbx_drop %s (%s, T-%s)\nRunning for %s/%s...\n", mdbx_version.git.describe, mdbx_version.git.datetime, mdbx_version.git.tree, envname, subname ? subname : "@MAIN"); - fflush(nullptr); + fflush(nullptr); + } rc = mdbx_env_create(&env); if (unlikely(rc != MDBX_SUCCESS)) { diff --git a/src/mdbx_dump.c b/src/mdbx_dump.c index 3cf9b709..a51b519b 100644 --- a/src/mdbx_dump.c +++ b/src/mdbx_dump.c @@ -101,7 +101,9 @@ static void dumpval(MDBX_val *v) { bool quiet = false, rescue = false; const char *prog; static void error(const char *func, int rc) { - fprintf(stderr, "%s: %s() error %d %s\n", prog, func, rc, mdbx_strerror(rc)); + if (!quiet) + fprintf(stderr, "%s: %s() error %d %s\n", prog, func, rc, + mdbx_strerror(rc)); } /* Dump in BDB-compatible format */ @@ -216,7 +218,7 @@ static int dump_sdb(MDBX_txn *txn, MDBX_dbi dbi, char *name) { static void usage(void) { fprintf(stderr, - "usage: %s [-V] [-q] [-f file] [-l] [-p] [-a|-s subdb] [-r] " + "usage: %s [-V] [-q] [-f file] [-l] [-p] [-r] [-a|-s subdb] " "dbpath\n" " -V\t\tprint version and exit\n" " -q\t\tbe quiet\n" @@ -431,8 +433,9 @@ int main(int argc, char *argv[]) { if (unlikely(rc != MDBX_SUCCESS)) { if (!rescue) break; - fprintf(stderr, "%s: %s: ignore %s for `%s` and continue\n", prog, - envname, mdbx_strerror(rc), subname); + if (!quiet) + fprintf(stderr, "%s: %s: ignore %s for `%s` and continue\n", prog, + envname, mdbx_strerror(rc), subname); /* Here is a hack for rescue mode, don't do that: * - we should restart transaction in case error due * database corruption; @@ -466,8 +469,9 @@ int main(int argc, char *argv[]) { if (have_raw && (!count /* || rescue */)) rc = dump_sdb(txn, MAIN_DBI, nullptr); else if (!count) { - fprintf(stderr, "%s: %s does not contain multiple databases\n", prog, - envname); + if (!quiet) + fprintf(stderr, "%s: %s does not contain multiple databases\n", prog, + envname); rc = MDBX_NOTFOUND; } } else { @@ -480,7 +484,8 @@ int main(int argc, char *argv[]) { case MDBX_SUCCESS: break; case MDBX_EINTR: - fprintf(stderr, "Interrupted by signal/user\n"); + if (!quiet) + fprintf(stderr, "Interrupted by signal/user\n"); break; default: if (unlikely(rc != MDBX_SUCCESS)) diff --git a/src/mdbx_stat.c b/src/mdbx_stat.c index 0d7de306..1085f41b 100644 --- a/src/mdbx_stat.c +++ b/src/mdbx_stat.c @@ -54,8 +54,9 @@ static void print_stat(MDBX_stat *ms) { static void usage(const char *prog) { fprintf(stderr, - "usage: %s [-V] [-e] [-f[f[f]]] [-r[r]] [-a|-s name] dbpath\n" + "usage: %s [-V] [-q] [-e] [-f[f[f]]] [-r[r]] [-a|-s name] dbpath\n" " -V\t\tprint version and exit\n" + " -q\t\tbe quiet\n" " -e\t\tshow whole DB info\n" " -f\t\tshow GC info\n" " -r\t\tshow readers\n" @@ -88,8 +89,11 @@ static int reader_list_func(void *ctx, int num, int slot, mdbx_pid_t pid, } const char *prog; +bool quiet = false; static void error(const char *func, int rc) { - fprintf(stderr, "%s: %s() error %d %s\n", prog, func, rc, mdbx_strerror(rc)); + if (!quiet) + fprintf(stderr, "%s: %s() error %d %s\n", prog, func, rc, + mdbx_strerror(rc)); } int main(int argc, char *argv[]) { @@ -108,6 +112,7 @@ int main(int argc, char *argv[]) { while ((o = getopt(argc, argv, "V" + "q" "a" "e" "f" @@ -129,6 +134,9 @@ int main(int argc, char *argv[]) { mdbx_build.target, mdbx_build.compiler, mdbx_build.flags, mdbx_build.options); return EXIT_SUCCESS; + case 'q': + quiet = true; + break; case 'a': if (subname) usage(prog); @@ -174,10 +182,12 @@ int main(int argc, char *argv[]) { envname = argv[optind]; envname = argv[optind]; - printf("mdbx_stat %s (%s, T-%s)\nRunning for %s...\n", - mdbx_version.git.describe, mdbx_version.git.datetime, - mdbx_version.git.tree, envname); - fflush(nullptr); + if (!quiet) { + printf("mdbx_stat %s (%s, T-%s)\nRunning for %s...\n", + mdbx_version.git.describe, mdbx_version.git.datetime, + mdbx_version.git.tree, envname); + fflush(nullptr); + } rc = mdbx_env_create(&env); if (unlikely(rc != MDBX_SUCCESS)) { @@ -356,7 +366,8 @@ int main(int argc, char *argv[]) { case MDBX_NOTFOUND: break; case MDBX_EINTR: - fprintf(stderr, "Interrupted by signal/user\n"); + if (!quiet) + fprintf(stderr, "Interrupted by signal/user\n"); goto txn_abort; default: error("mdbx_cursor_get", rc); @@ -462,7 +473,8 @@ int main(int argc, char *argv[]) { case MDBX_NOTFOUND: break; case MDBX_EINTR: - fprintf(stderr, "Interrupted by signal/user\n"); + if (!quiet) + fprintf(stderr, "Interrupted by signal/user\n"); break; default: if (unlikely(rc != MDBX_SUCCESS))