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