Commit f6d1f564 authored by Rusty Russell's avatar Rusty Russell

tdb2: Internal error helpers.

I use the "high pointers hold error numbers" trick, and also make
tdb_logerr return the error code, which enables the common case of
"return tdb_logerr(...)".
parent 74c15d72
...@@ -74,6 +74,16 @@ typedef uint64_t tdb_off_t; ...@@ -74,6 +74,16 @@ typedef uint64_t tdb_off_t;
#define TDB_RECOVERY_INVALID_MAGIC (0x0ULL) #define TDB_RECOVERY_INVALID_MAGIC (0x0ULL)
#define TDB_OFF_ERR ((tdb_off_t)-1) #define TDB_OFF_ERR ((tdb_off_t)-1)
#define TDB_OFF_IS_ERR(off) unlikely(off >= (tdb_off_t)TDB_ERR_LAST)
/* Packing errors into pointers and v.v. */
#define TDB_PTR_IS_ERR(ptr) \
unlikely((void *)(ptr) >= (void *)(long)TDB_ERR_LAST)
#define TDB_PTR_ERR(p) ((enum TDB_ERROR)(long)(p))
#define TDB_ERR_PTR(err) ((void *)(long)(err))
/* Common case of returning true, false or -ve error. */
typedef int tdb_bool_err;
/* Prevent others from opening the file. */ /* Prevent others from opening the file. */
#define TDB_OPEN_LOCK 0 #define TDB_OPEN_LOCK 0
...@@ -551,10 +561,10 @@ int tdb_transaction_recover(struct tdb_context *tdb); ...@@ -551,10 +561,10 @@ int tdb_transaction_recover(struct tdb_context *tdb);
bool tdb_needs_recovery(struct tdb_context *tdb); bool tdb_needs_recovery(struct tdb_context *tdb);
/* tdb.c: */ /* tdb.c: */
void COLD tdb_logerr(struct tdb_context *tdb, enum TDB_ERROR COLD tdb_logerr(struct tdb_context *tdb,
enum TDB_ERROR ecode, enum TDB_ERROR ecode,
enum tdb_log_level level, enum tdb_log_level level,
const char *fmt, ...); const char *fmt, ...);
#ifdef TDB_TRACE #ifdef TDB_TRACE
void tdb_trace(struct tdb_context *tdb, const char *op); void tdb_trace(struct tdb_context *tdb, const char *op);
......
...@@ -706,10 +706,10 @@ const char *tdb_errorstr(const struct tdb_context *tdb) ...@@ -706,10 +706,10 @@ const char *tdb_errorstr(const struct tdb_context *tdb)
return "Invalid error code"; return "Invalid error code";
} }
void COLD tdb_logerr(struct tdb_context *tdb, enum TDB_ERROR COLD tdb_logerr(struct tdb_context *tdb,
enum TDB_ERROR ecode, enum TDB_ERROR ecode,
enum tdb_log_level level, enum tdb_log_level level,
const char *fmt, ...) const char *fmt, ...)
{ {
char *message; char *message;
va_list ap; va_list ap;
...@@ -720,7 +720,7 @@ void COLD tdb_logerr(struct tdb_context *tdb, ...@@ -720,7 +720,7 @@ void COLD tdb_logerr(struct tdb_context *tdb,
tdb->ecode = ecode; tdb->ecode = ecode;
if (!tdb->logfn) if (!tdb->logfn)
return; return ecode;
/* FIXME: Doesn't assume asprintf. */ /* FIXME: Doesn't assume asprintf. */
va_start(ap, fmt); va_start(ap, fmt);
...@@ -732,7 +732,7 @@ void COLD tdb_logerr(struct tdb_context *tdb, ...@@ -732,7 +732,7 @@ void COLD tdb_logerr(struct tdb_context *tdb,
tdb->logfn(tdb, TDB_LOG_ERROR, tdb->log_private, tdb->logfn(tdb, TDB_LOG_ERROR, tdb->log_private,
"out of memory formatting message:"); "out of memory formatting message:");
tdb->logfn(tdb, level, tdb->log_private, fmt); tdb->logfn(tdb, level, tdb->log_private, fmt);
return; return ecode;
} }
va_start(ap, fmt); va_start(ap, fmt);
len = vsprintf(message, fmt, ap); len = vsprintf(message, fmt, ap);
...@@ -740,4 +740,5 @@ void COLD tdb_logerr(struct tdb_context *tdb, ...@@ -740,4 +740,5 @@ void COLD tdb_logerr(struct tdb_context *tdb,
tdb->logfn(tdb, level, tdb->log_private, message); tdb->logfn(tdb, level, tdb->log_private, message);
free(message); free(message);
errno = saved_errno; errno = saved_errno;
return ecode;
} }
...@@ -111,7 +111,8 @@ enum TDB_ERROR { ...@@ -111,7 +111,8 @@ enum TDB_ERROR {
TDB_ERR_EXISTS = -5, /* The key already exists. */ TDB_ERR_EXISTS = -5, /* The key already exists. */
TDB_ERR_NOEXIST = -6, /* The key does not exist. */ TDB_ERR_NOEXIST = -6, /* The key does not exist. */
TDB_ERR_EINVAL = -7, /* You're using it wrong. */ TDB_ERR_EINVAL = -7, /* You're using it wrong. */
TDB_ERR_RDONLY = -8 /* The database is read-only. */ TDB_ERR_RDONLY = -8, /* The database is read-only. */
TDB_ERR_LAST = TDB_ERR_RDONLY
}; };
/** /**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment