mdbx-tools: more for quiet mode inside mdbx_load.

Change-Id: I31839d530b94516c9819e6600fd808a3c97c609a
This commit is contained in:
Leonid Yuriev 2021-03-11 17:02:43 +03:00
parent f01e0efc2d
commit 0603dd8305

View File

@ -46,14 +46,17 @@ static void signal_handler(int sig) {
#endif /* !WINDOWS */ #endif /* !WINDOWS */
static char *prog; static char *prog;
static bool quiet = false;
static size_t lineno; static size_t lineno;
static void error(const char *func, int rc) { static void error(const char *func, int rc) {
if (lineno) if (!quiet) {
fprintf(stderr, "%s: at input line %" PRIiSIZE ": %s() error %d, %s\n", if (lineno)
prog, lineno, func, rc, mdbx_strerror(rc)); fprintf(stderr, "%s: at input line %" PRIiSIZE ": %s() error %d, %s\n",
else prog, lineno, func, rc, mdbx_strerror(rc));
fprintf(stderr, "%s: %s() error %d %s\n", prog, func, rc, else
mdbx_strerror(rc)); fprintf(stderr, "%s: %s() error %d %s\n", prog, func, rc,
mdbx_strerror(rc));
}
} }
static char *valstr(char *line, const char *item) { static char *valstr(char *line, const char *item) {
@ -63,8 +66,10 @@ static char *valstr(char *line, const char *item) {
if (line[len] != '=') { if (line[len] != '=') {
if (line[len] > ' ') if (line[len] > ' ')
return nullptr; return nullptr;
fprintf(stderr, "%s: line %" PRIiSIZE ": unexpected line format for '%s'\n", if (!quiet)
prog, lineno, item); fprintf(stderr,
"%s: line %" PRIiSIZE ": unexpected line format for '%s'\n", prog,
lineno, item);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
char *ptr = strchr(line, '\n'); char *ptr = strchr(line, '\n');
@ -81,9 +86,10 @@ static bool valnum(char *line, const char *item, uint64_t *value) {
char *end = nullptr; char *end = nullptr;
*value = strtoull(str, &end, 0); *value = strtoull(str, &end, 0);
if (end && *end) { if (end && *end) {
fprintf(stderr, if (!quiet)
"%s: line %" PRIiSIZE ": unexpected number format for '%s'\n", prog, fprintf(stderr,
lineno, item); "%s: line %" PRIiSIZE ": unexpected number format for '%s'\n",
prog, lineno, item);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
return true; return true;
@ -95,8 +101,9 @@ static bool valbool(char *line, const char *item, bool *value) {
return false; return false;
if (u64 > 1) { if (u64 > 1) {
fprintf(stderr, "%s: line %" PRIiSIZE ": unexpected value for '%s'\n", prog, if (!quiet)
lineno, item); fprintf(stderr, "%s: line %" PRIiSIZE ": unexpected value for '%s'\n",
prog, lineno, item);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
*value = u64 != 0; *value = u64 != 0;
@ -157,27 +164,30 @@ static int readhdr(void) {
if (valnum(dbuf.iov_base, "VERSION", &u64)) { if (valnum(dbuf.iov_base, "VERSION", &u64)) {
if (u64 != 3) { if (u64 != 3) {
fprintf(stderr, if (!quiet)
"%s: line %" PRIiSIZE ": unsupported value %" PRIu64 fprintf(stderr,
" for %s\n", "%s: line %" PRIiSIZE ": unsupported value %" PRIu64
prog, lineno, u64, "VERSION"); " for %s\n",
prog, lineno, u64, "VERSION");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
continue; continue;
} }
if (valnum(dbuf.iov_base, "db_pagesize", &u64)) { if (valnum(dbuf.iov_base, "db_pagesize", &u64)) {
if (!(mode & GLOBAL) && envinfo.mi_dxb_pagesize != u64) if (!(mode & GLOBAL) && envinfo.mi_dxb_pagesize != u64) {
fprintf(stderr, if (!quiet)
"%s: line %" PRIiSIZE ": ignore value %" PRIu64 fprintf(stderr,
" for '%s' in non-global context\n", "%s: line %" PRIiSIZE ": ignore value %" PRIu64
prog, lineno, u64, "db_pagesize"); " for '%s' in non-global context\n",
else if (u64 < MDBX_MIN_PAGESIZE || u64 > MDBX_MAX_PAGESIZE) prog, lineno, u64, "db_pagesize");
fprintf(stderr, } else if (u64 < MDBX_MIN_PAGESIZE || u64 > MDBX_MAX_PAGESIZE) {
"%s: line %" PRIiSIZE ": ignore unsupported value %" PRIu64 if (!quiet)
" for %s\n", fprintf(stderr,
prog, lineno, u64, "db_pagesize"); "%s: line %" PRIiSIZE ": ignore unsupported value %" PRIu64
else " for %s\n",
prog, lineno, u64, "db_pagesize");
} else
envinfo.mi_dxb_pagesize = (uint32_t)u64; envinfo.mi_dxb_pagesize = (uint32_t)u64;
continue; continue;
} }
@ -192,8 +202,10 @@ static int readhdr(void) {
mode &= ~PRINT; mode &= ~PRINT;
continue; continue;
} }
fprintf(stderr, "%s: line %" PRIiSIZE ": unsupported value '%s' for %s\n", if (!quiet)
prog, lineno, str, "format"); fprintf(stderr,
"%s: line %" PRIiSIZE ": unsupported value '%s' for %s\n", prog,
lineno, str, "format");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -203,7 +215,8 @@ static int readhdr(void) {
free(subname); free(subname);
subname = mdbx_strdup(str); subname = mdbx_strdup(str);
if (!subname) { if (!subname) {
perror("strdup()"); if (!quiet)
perror("strdup()");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
@ -213,9 +226,10 @@ static int readhdr(void) {
str = valstr(dbuf.iov_base, "type"); str = valstr(dbuf.iov_base, "type");
if (str) { if (str) {
if (strcmp(str, "btree") != 0) { if (strcmp(str, "btree") != 0) {
fprintf(stderr, if (!quiet)
"%s: line %" PRIiSIZE ": unsupported value '%s' for %s\n", prog, fprintf(stderr,
lineno, str, "type"); "%s: line %" PRIiSIZE ": unsupported value '%s' for %s\n",
prog, lineno, str, "type");
free(subname); free(subname);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -223,53 +237,61 @@ static int readhdr(void) {
} }
if (valnum(dbuf.iov_base, "mapaddr", &u64)) { if (valnum(dbuf.iov_base, "mapaddr", &u64)) {
if (u64) if (u64) {
fprintf(stderr, if (!quiet)
"%s: line %" PRIiSIZE ": ignore unsupported value 0x%" PRIx64 fprintf(stderr,
" for %s\n", "%s: line %" PRIiSIZE ": ignore unsupported value 0x%" PRIx64
prog, lineno, u64, "mapaddr"); " for %s\n",
prog, lineno, u64, "mapaddr");
}
continue; continue;
} }
if (valnum(dbuf.iov_base, "mapsize", &u64)) { if (valnum(dbuf.iov_base, "mapsize", &u64)) {
if (!(mode & GLOBAL)) if (!(mode & GLOBAL)) {
fprintf(stderr, if (!quiet)
"%s: line %" PRIiSIZE ": ignore value %" PRIu64 fprintf(stderr,
" for '%s' in non-global context\n", "%s: line %" PRIiSIZE ": ignore value %" PRIu64
prog, lineno, u64, "mapsize"); " for '%s' in non-global context\n",
else if (u64 < MIN_MAPSIZE || u64 > MAX_MAPSIZE64) prog, lineno, u64, "mapsize");
fprintf(stderr, } else if (u64 < MIN_MAPSIZE || u64 > MAX_MAPSIZE64) {
"%s: line %" PRIiSIZE ": ignore unsupported value 0x%" PRIx64 if (!quiet)
" for %s\n", fprintf(stderr,
prog, lineno, u64, "mapsize"); "%s: line %" PRIiSIZE ": ignore unsupported value 0x%" PRIx64
else " for %s\n",
prog, lineno, u64, "mapsize");
} else
envinfo.mi_mapsize = (size_t)u64; envinfo.mi_mapsize = (size_t)u64;
continue; continue;
} }
if (valnum(dbuf.iov_base, "maxreaders", &u64)) { if (valnum(dbuf.iov_base, "maxreaders", &u64)) {
if (!(mode & GLOBAL)) if (!(mode & GLOBAL)) {
fprintf(stderr, if (!quiet)
"%s: line %" PRIiSIZE ": ignore value %" PRIu64 fprintf(stderr,
" for '%s' in non-global context\n", "%s: line %" PRIiSIZE ": ignore value %" PRIu64
prog, lineno, u64, "maxreaders"); " for '%s' in non-global context\n",
else if (u64 < 1 || u64 > MDBX_READERS_LIMIT) prog, lineno, u64, "maxreaders");
fprintf(stderr, } else if (u64 < 1 || u64 > MDBX_READERS_LIMIT) {
"%s: line %" PRIiSIZE ": ignore unsupported value 0x%" PRIx64 if (!quiet)
" for %s\n", fprintf(stderr,
prog, lineno, u64, "maxreaders"); "%s: line %" PRIiSIZE ": ignore unsupported value 0x%" PRIx64
else " for %s\n",
prog, lineno, u64, "maxreaders");
} else
envinfo.mi_maxreaders = (int)u64; envinfo.mi_maxreaders = (int)u64;
continue; continue;
} }
if (valnum(dbuf.iov_base, "txnid", &u64)) { if (valnum(dbuf.iov_base, "txnid", &u64)) {
if (u64 < MIN_TXNID || u64 > MAX_TXNID) if (u64 < MIN_TXNID || u64 > MAX_TXNID) {
fprintf(stderr, if (!quiet)
"%s: line %" PRIiSIZE ": ignore unsupported value 0x%" PRIx64 fprintf(stderr,
" for %s\n", "%s: line %" PRIiSIZE ": ignore unsupported value 0x%" PRIx64
prog, lineno, u64, "txnid"); " for %s\n",
txnid = u64; prog, lineno, u64, "txnid");
} else
txnid = u64;
continue; continue;
} }
@ -280,20 +302,22 @@ static int readhdr(void) {
str = valstr(dbuf.iov_base, "geometry"); str = valstr(dbuf.iov_base, "geometry");
if (str) { if (str) {
if (!(mode & GLOBAL)) if (!(mode & GLOBAL)) {
fprintf(stderr, if (!quiet)
"%s: line %" PRIiSIZE ": ignore values %s" fprintf(stderr,
" for '%s' in non-global context\n", "%s: line %" PRIiSIZE ": ignore values %s"
prog, lineno, str, "geometry"); " for '%s' in non-global context\n",
else if (sscanf(str, prog, lineno, str, "geometry");
"l%" PRIu64 ",c%" PRIu64 ",u%" PRIu64 ",s%" PRIu64 } else if (sscanf(str,
",g%" PRIu64, "l%" PRIu64 ",c%" PRIu64 ",u%" PRIu64 ",s%" PRIu64
&envinfo.mi_geo.lower, &envinfo.mi_geo.current, ",g%" PRIu64,
&envinfo.mi_geo.upper, &envinfo.mi_geo.shrink, &envinfo.mi_geo.lower, &envinfo.mi_geo.current,
&envinfo.mi_geo.grow) != 5) { &envinfo.mi_geo.upper, &envinfo.mi_geo.shrink,
fprintf(stderr, &envinfo.mi_geo.grow) != 5) {
"%s: line %" PRIiSIZE ": unexpected line format for '%s'\n", if (!quiet)
prog, lineno, "geometry"); fprintf(stderr,
"%s: line %" PRIiSIZE ": unexpected line format for '%s'\n",
prog, lineno, "geometry");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
continue; continue;
@ -301,16 +325,18 @@ static int readhdr(void) {
str = valstr(dbuf.iov_base, "canary"); str = valstr(dbuf.iov_base, "canary");
if (str) { if (str) {
if (!(mode & GLOBAL)) if (!(mode & GLOBAL)) {
fprintf(stderr, if (!quiet)
"%s: line %" PRIiSIZE ": ignore values %s" fprintf(stderr,
" for '%s' in non-global context\n", "%s: line %" PRIiSIZE ": ignore values %s"
prog, lineno, str, "canary"); " for '%s' in non-global context\n",
else if (sscanf(str, "v%" PRIu64 ",x%" PRIu64 ",y%" PRIu64 ",z%" PRIu64, prog, lineno, str, "canary");
&canary.v, &canary.x, &canary.y, &canary.z) != 4) { } else if (sscanf(str, "v%" PRIu64 ",x%" PRIu64 ",y%" PRIu64 ",z%" PRIu64,
fprintf(stderr, &canary.v, &canary.x, &canary.y, &canary.z) != 4) {
"%s: line %" PRIiSIZE ": unexpected line format for '%s'\n", if (!quiet)
prog, lineno, "canary"); fprintf(stderr,
"%s: line %" PRIiSIZE ": unexpected line format for '%s'\n",
prog, lineno, "canary");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
continue; continue;
@ -333,17 +359,19 @@ static int readhdr(void) {
return MDBX_SUCCESS; return MDBX_SUCCESS;
} }
fprintf(stderr, if (!quiet)
"%s: line %" PRIiSIZE ": unrecognized keyword ignored: %s\n", prog, fprintf(stderr,
lineno, (char *)dbuf.iov_base); "%s: line %" PRIiSIZE ": unrecognized keyword ignored: %s\n",
prog, lineno, (char *)dbuf.iov_base);
next:; next:;
} }
return EOF; return EOF;
} }
static int badend(void) { static int badend(void) {
fprintf(stderr, "%s: line %" PRIiSIZE ": unexpected end of input\n", prog, if (!quiet)
lineno); fprintf(stderr, "%s: line %" PRIiSIZE ": unexpected end of input\n", prog,
lineno);
return errno ? errno : MDBX_ENODATA; return errno ? errno : MDBX_ENODATA;
} }
@ -395,8 +423,10 @@ static int readline(MDBX_val *out, MDBX_val *buf) {
while (c1[len - 1] != '\n') { while (c1[len - 1] != '\n') {
buf->iov_base = mdbx_realloc(buf->iov_base, buf->iov_len * 2); buf->iov_base = mdbx_realloc(buf->iov_base, buf->iov_len * 2);
if (!buf->iov_base) { if (!buf->iov_base) {
fprintf(stderr, "%s: line %" PRIiSIZE ": out of memory, line too long\n", if (!quiet)
prog, lineno); fprintf(stderr,
"%s: line %" PRIiSIZE ": out of memory, line too long\n", prog,
lineno);
return MDBX_ENOMEM; return MDBX_ENOMEM;
} }
c1 = buf->iov_base; c1 = buf->iov_base;
@ -482,7 +512,6 @@ int main(int argc, char *argv[]) {
MDBX_dbi dbi; MDBX_dbi dbi;
char *envname = nullptr; char *envname = nullptr;
int envflags = MDBX_SAFE_NOSYNC | MDBX_ACCEDE, putflags = MDBX_UPSERT; int envflags = MDBX_SAFE_NOSYNC | MDBX_ACCEDE, putflags = MDBX_UPSERT;
bool quiet = false;
bool rescue = false; bool rescue = false;
bool purge = false; bool purge = false;
@ -521,8 +550,9 @@ int main(int argc, char *argv[]) {
break; break;
case 'f': case 'f':
if (freopen(optarg, "r", stdin) == nullptr) { if (freopen(optarg, "r", stdin) == nullptr) {
fprintf(stderr, "%s: %s: open: %s\n", prog, optarg, if (!quiet)
mdbx_strerror(errno)); fprintf(stderr, "%s: %s: open: %s\n", prog, optarg,
mdbx_strerror(errno));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
break; break;
@ -618,11 +648,12 @@ int main(int argc, char *argv[]) {
envinfo.mi_dxb_pagesize ? (intptr_t)envinfo.mi_dxb_pagesize : -1); envinfo.mi_dxb_pagesize ? (intptr_t)envinfo.mi_dxb_pagesize : -1);
} else { } else {
if (envinfo.mi_mapsize > MAX_MAPSIZE) { if (envinfo.mi_mapsize > MAX_MAPSIZE) {
fprintf( if (!quiet)
stderr, fprintf(
"Database size is too large for current system (mapsize=%" PRIu64 stderr,
" is great than system-limit %zu)\n", "Database size is too large for current system (mapsize=%" PRIu64
envinfo.mi_mapsize, (size_t)MAX_MAPSIZE); " is great than system-limit %zu)\n",
envinfo.mi_mapsize, (size_t)MAX_MAPSIZE);
goto env_close; goto env_close;
} }
rc = mdbx_env_set_geometry( rc = mdbx_env_set_geometry(
@ -644,8 +675,9 @@ int main(int argc, char *argv[]) {
kbuf.iov_len = mdbx_env_get_maxvalsize_ex(env, 0) + 1; kbuf.iov_len = mdbx_env_get_maxvalsize_ex(env, 0) + 1;
if (kbuf.iov_len >= INTPTR_MAX / 2) { if (kbuf.iov_len >= INTPTR_MAX / 2) {
fprintf(stderr, "mdbx_env_get_maxkeysize() failed, returns %zu\n", if (!quiet)
kbuf.iov_len); fprintf(stderr, "mdbx_env_get_maxkeysize() failed, returns %zu\n",
kbuf.iov_len);
goto env_close; goto env_close;
} }
@ -696,10 +728,11 @@ int main(int argc, char *argv[]) {
goto txn_abort; goto txn_abort;
} }
if (present_sequence > sequence) { if (present_sequence > sequence) {
fprintf(stderr, if (!quiet)
"present sequence for '%s' value (%" PRIu64 fprintf(stderr,
") is greater than loaded (%" PRIu64 ")\n", "present sequence for '%s' value (%" PRIu64
dbi_name, present_sequence, sequence); ") is greater than loaded (%" PRIu64 ")\n",
dbi_name, present_sequence, sequence);
rc = MDBX_RESULT_TRUE; rc = MDBX_RESULT_TRUE;
goto txn_abort; goto txn_abort;
} }
@ -739,8 +772,9 @@ int main(int argc, char *argv[]) {
if (rc == MDBX_SUCCESS) if (rc == MDBX_SUCCESS)
rc = readline(&data, &dbuf); rc = readline(&data, &dbuf);
if (rc) { if (rc) {
fprintf(stderr, "%s: line %" PRIiSIZE ": failed to read key value\n", if (!quiet)
prog, lineno); fprintf(stderr, "%s: line %" PRIiSIZE ": failed to read key value\n",
prog, lineno);
goto txn_abort; goto txn_abort;
} }
@ -748,8 +782,9 @@ int main(int argc, char *argv[]) {
if (rc == MDBX_KEYEXIST && putflags) if (rc == MDBX_KEYEXIST && putflags)
continue; continue;
if (rc == MDBX_BAD_VALSIZE && rescue) { if (rc == MDBX_BAD_VALSIZE && rescue) {
fprintf(stderr, "%s: skip line %" PRIiSIZE ": due %s\n", prog, lineno, if (!quiet)
mdbx_strerror(rc)); fprintf(stderr, "%s: skip line %" PRIiSIZE ": due %s\n", prog, lineno,
mdbx_strerror(rc));
continue; continue;
} }
if (unlikely(rc != MDBX_SUCCESS)) { if (unlikely(rc != MDBX_SUCCESS)) {
@ -818,7 +853,8 @@ int main(int argc, char *argv[]) {
case MDBX_SUCCESS: case MDBX_SUCCESS:
break; break;
case MDBX_EINTR: case MDBX_EINTR:
fprintf(stderr, "Interrupted by signal/user\n"); if (!quiet)
fprintf(stderr, "Interrupted by signal/user\n");
break; break;
default: default:
if (unlikely(rc != MDBX_SUCCESS)) if (unlikely(rc != MDBX_SUCCESS))