mdbx: add mdbx_strerror_r().

This commit is contained in:
Leo Yuriev 2017-02-28 16:54:10 +03:00
parent 8fb2523276
commit 57dc59ecfb
2 changed files with 54 additions and 38 deletions

87
mdbx.c
View File

@ -1328,7 +1328,7 @@ static pthread_mutex_t tsan_mutex = PTHREAD_MUTEX_INITIALIZER;
#endif #endif
/** Return the library version info. */ /** Return the library version info. */
char *__cold mdbx_version(int *major, int *minor, int *patch) { const char *mdbx_version(int *major, int *minor, int *patch) {
if (major) if (major)
*major = MDB_VERSION_MAJOR; *major = MDB_VERSION_MAJOR;
if (minor) if (minor)
@ -1338,45 +1338,60 @@ char *__cold mdbx_version(int *major, int *minor, int *patch) {
return MDB_VERSION_STRING; return MDB_VERSION_STRING;
} }
/** Table of descriptions for LMDB @ref errors */ static const char *__mdbx_strerr(int err) {
static char *const mdbx_errstr[] = { /* Table of descriptions for LMDB errors */
"MDB_KEYEXIST: Key/data pair already exists", static const char *const tbl[] = {
"MDB_NOTFOUND: No matching key/data pair found", "MDB_KEYEXIST: Key/data pair already exists",
"MDB_PAGE_NOTFOUND: Requested page not found", "MDB_NOTFOUND: No matching key/data pair found",
"MDB_CORRUPTED: Located page was wrong type", "MDB_PAGE_NOTFOUND: Requested page not found",
"MDB_PANIC: Update of meta page failed or environment had fatal error", "MDB_CORRUPTED: Located page was wrong type",
"MDB_VERSION_MISMATCH: Database environment version mismatch", "MDB_PANIC: Update of meta page failed or environment had fatal error",
"MDB_INVALID: File is not an LMDB file", "MDB_VERSION_MISMATCH: Database environment version mismatch",
"MDB_MAP_FULL: Environment mapsize limit reached", "MDB_INVALID: File is not an LMDB file",
"MDB_DBS_FULL: Environment maxdbs limit reached", "MDB_MAP_FULL: Environment mapsize limit reached",
"MDB_READERS_FULL: Environment maxreaders limit reached", "MDB_DBS_FULL: Environment maxdbs limit reached",
"MDB_TLS_FULL: Thread-local storage keys full - too many environments " "MDB_READERS_FULL: Environment maxreaders limit reached",
"open", "MDB_TLS_FULL: Thread-local storage keys full - too many environments "
"MDB_TXN_FULL: Transaction has too many dirty pages - transaction too " "open",
"big", "MDB_TXN_FULL: Transaction has too many dirty pages - transaction too "
"MDB_CURSOR_FULL: Internal error - cursor stack limit reached", "big",
"MDB_PAGE_FULL: Internal error - page has no more space", "MDB_CURSOR_FULL: Internal error - cursor stack limit reached",
"MDB_MAP_RESIZED: Database contents grew beyond environment mapsize", "MDB_PAGE_FULL: Internal error - page has no more space",
"MDB_INCOMPATIBLE: Operation and DB incompatible, or DB flags changed", "MDB_MAP_RESIZED: Database contents grew beyond environment mapsize",
"MDB_BAD_RSLOT: Invalid reuse of reader locktable slot", "MDB_INCOMPATIBLE: Operation and DB incompatible, or DB flags changed",
"MDB_BAD_TXN: Transaction must abort, has a child, or is invalid", "MDB_BAD_RSLOT: Invalid reuse of reader locktable slot",
"MDB_BAD_VALSIZE: Unsupported size of key/DB name/data, or wrong " "MDB_BAD_TXN: Transaction must abort, has a child, or is invalid",
"DUPFIXED size", "MDB_BAD_VALSIZE: Unsupported size of key/DB name/data, or wrong "
"MDB_BAD_DBI: The specified DBI handle was closed/changed unexpectedly", "DUPFIXED size",
"MDB_PROBLEM: Unexpected problem - txn should abort", "MDB_BAD_DBI: The specified DBI handle was closed/changed unexpectedly",
}; "MDB_PROBLEM: Unexpected problem - txn should abort",
};
char *__cold mdbx_strerror(int err) {
int i;
if (!err)
return ("Successful return: 0");
if (err >= MDB_KEYEXIST && err <= MDB_LAST_ERRCODE) { if (err >= MDB_KEYEXIST && err <= MDB_LAST_ERRCODE) {
i = err - MDB_KEYEXIST; int i = err - MDB_KEYEXIST;
return mdbx_errstr[i]; return tbl[i];
} }
return strerror(err); switch (err) {
case MDB_SUCCESS:
return "Successful return: 0";
case MDBX_EMULTIVAL:
return "";
case MDBX_EBADSIGN:
return "";
default:
return NULL;
}
}
const char *mdbx_strerror_r(int err, char *buf, size_t buflen) {
const char *msg = __mdbx_strerr(err);
return msg ? msg : strerror_r(err, buf, buflen);
}
const char *__cold mdbx_strerror(int err) {
const char *msg = __mdbx_strerr(err);
return msg ? msg : strerror(err);
} }
#if MDBX_MODE_ENABLED #if MDBX_MODE_ENABLED

5
mdbx.h
View File

@ -343,7 +343,7 @@ typedef struct MDBX_envinfo {
* here * here
* Returns "version string" The library version as a string * Returns "version string" The library version as a string
*/ */
char *mdbx_version(int *major, int *minor, int *patch); const char *mdbx_version(int *major, int *minor, int *patch);
/* Return a string describing a given error code. /* Return a string describing a given error code.
* *
@ -355,7 +355,8 @@ char *mdbx_version(int *major, int *minor, int *patch);
* [in] err The error code * [in] err The error code
* Returns "error message" The description of the error * Returns "error message" The description of the error
*/ */
char *mdbx_strerror(int err); const char *mdbx_strerror(int err);
const char *mdbx_strerror_r(int err, char *buf, size_t buflen);
/* Create an LMDB environment handle. /* Create an LMDB environment handle.
* *