1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-22 13:34:15 +03:00

lib/tdb: wean off TDB_ERRCODE.

It was a regrettable hack which I used to reduce line count in tdb; in fact it caused confusion as can be seen in this patch.
In particular, ecode now needs to be set before TDB_LOG anyway, and having it exposed in
the header is useless (the struct tdb_context isn't defined, so it's doubly useless).
Also, we should never set errno, as io.c was doing.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2009-10-22 00:09:43 +10:30
parent 703004340c
commit b77f41d58b
9 changed files with 59 additions and 40 deletions

View File

@ -54,7 +54,7 @@ int tdb_rec_free_read(struct tdb_context *tdb, tdb_off_t off, struct list_struct
tdb->ecode = TDB_ERR_CORRUPT;
TDB_LOG((tdb, TDB_DEBUG_WARNING, "tdb_rec_free_read bad magic 0x%x at offset=%d\n",
rec->magic, off));
return TDB_ERRCODE(TDB_ERR_CORRUPT, -1);
return -1;
}
if (tdb->methods->tdb_oob(tdb, rec->next+sizeof(*rec), 0) != 0)
return -1;
@ -78,8 +78,9 @@ static int remove_from_freelist(struct tdb_context *tdb, tdb_off_t off, tdb_off_
/* Follow chain (next offset is at start of record) */
last_ptr = i;
}
tdb->ecode = TDB_ERR_CORRUPT;
TDB_LOG((tdb, TDB_DEBUG_FATAL,"remove_from_freelist: not on list at off=%d\n", off));
return TDB_ERRCODE(TDB_ERR_CORRUPT, -1);
return -1;
}
#endif

View File

@ -67,7 +67,8 @@ int tdb_validate_freelist(struct tdb_context *tdb, int *pnum_entries)
/* Store the FREELIST_TOP record. */
if (seen_insert(mem_tdb, last_ptr) == -1) {
ret = TDB_ERRCODE(TDB_ERR_CORRUPT, -1);
tdb->ecode = TDB_ERR_CORRUPT;
ret = -1;
goto fail;
}
@ -83,7 +84,8 @@ int tdb_validate_freelist(struct tdb_context *tdb, int *pnum_entries)
be corrupt. */
if (seen_insert(mem_tdb, rec_ptr)) {
ret = TDB_ERRCODE(TDB_ERR_CORRUPT, -1);
tdb->ecode = TDB_ERR_CORRUPT;
ret = -1;
goto fail;
}

View File

@ -45,11 +45,12 @@ static int tdb_oob(struct tdb_context *tdb, tdb_off_t len, int probe)
TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_oob len %d beyond internal malloc size %d\n",
(int)len, (int)tdb->map_size));
}
return TDB_ERRCODE(TDB_ERR_IO, -1);
return -1;
}
if (fstat(tdb->fd, &st) == -1) {
return TDB_ERRCODE(TDB_ERR_IO, -1);
tdb->ecode = TDB_ERR_IO;
return -1;
}
if (st.st_size < (size_t)len) {
@ -59,12 +60,14 @@ static int tdb_oob(struct tdb_context *tdb, tdb_off_t len, int probe)
TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_oob len %d beyond eof at %d\n",
(int)len, (int)st.st_size));
}
return TDB_ERRCODE(TDB_ERR_IO, -1);
return -1;
}
/* Unmap, update size, remap */
if (tdb_munmap(tdb) == -1)
return TDB_ERRCODE(TDB_ERR_IO, -1);
if (tdb_munmap(tdb) == -1) {
tdb->ecode = TDB_ERR_IO;
return -1;
}
tdb->map_size = st.st_size;
tdb_mmap(tdb);
return 0;
@ -92,27 +95,27 @@ static int tdb_write(struct tdb_context *tdb, tdb_off_t off,
ssize_t written = pwrite(tdb->fd, buf, len, off);
if ((written != (ssize_t)len) && (written != -1)) {
/* try once more */
tdb->ecode = TDB_ERR_IO;
TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_write: wrote only "
"%d of %d bytes at %d, trying once more\n",
(int)written, len, off));
errno = ENOSPC;
written = pwrite(tdb->fd, (const void *)((const char *)buf+written),
written = pwrite(tdb->fd, (const char *)buf+written,
len-written,
off+written);
}
if (written == -1) {
/* Ensure ecode is set for log fn. */
tdb->ecode = TDB_ERR_IO;
/* Ensure ecode is set for log fn. */
tdb->ecode = TDB_ERR_IO;
TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_write failed at %d "
"len=%d (%s)\n", off, len, strerror(errno)));
return TDB_ERRCODE(TDB_ERR_IO, -1);
return -1;
} else if (written != (ssize_t)len) {
tdb->ecode = TDB_ERR_IO;
TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_write: failed to "
"write %d bytes at %d in two attempts\n",
len, off));
errno = ENOSPC;
return TDB_ERRCODE(TDB_ERR_IO, -1);
}
return -1;
}
}
return 0;
}
@ -146,7 +149,7 @@ static int tdb_read(struct tdb_context *tdb, tdb_off_t off, void *buf,
"len=%d ret=%d (%s) map_size=%d\n",
(int)off, (int)len, (int)ret, strerror(errno),
(int)tdb->map_size));
return TDB_ERRCODE(TDB_ERR_IO, -1);
return -1;
}
}
if (cv) {
@ -389,7 +392,7 @@ unsigned char *tdb_alloc_read(struct tdb_context *tdb, tdb_off_t offset, tdb_len
tdb->ecode = TDB_ERR_OOM;
TDB_LOG((tdb, TDB_DEBUG_ERROR,"tdb_alloc_read malloc failed len=%d (%s)\n",
len, strerror(errno)));
return TDB_ERRCODE(TDB_ERR_OOM, buf);
return NULL;
}
if (tdb->methods->tdb_read(tdb, offset, buf, len, 0) == -1) {
SAFE_FREE(buf);
@ -441,7 +444,7 @@ int tdb_rec_read(struct tdb_context *tdb, tdb_off_t offset, struct list_struct *
/* Ensure ecode is set for log fn. */
tdb->ecode = TDB_ERR_CORRUPT;
TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_rec_read bad magic 0x%x at offset=%d\n", rec->magic, offset));
return TDB_ERRCODE(TDB_ERR_CORRUPT, -1);
return -1;
}
return tdb->methods->tdb_oob(tdb, rec->next+sizeof(*rec), 0);
}

View File

@ -75,16 +75,15 @@ int tdb_brlock(struct tdb_context *tdb, tdb_off_t offset,
} while (ret == -1 && errno == EINTR);
if (ret == -1) {
tdb->ecode = TDB_ERR_LOCK;
/* Generic lock error. errno set by fcntl.
* EAGAIN is an expected return from non-blocking
* locks. */
if (!probe && lck_type != F_SETLK) {
/* Ensure error code is set for log fun to examine. */
tdb->ecode = TDB_ERR_LOCK;
TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_brlock failed (fd=%d) at offset %d rw_type=%d lck_type=%d len=%d\n",
tdb->fd, offset, rw_type, lck_type, (int)len));
}
return TDB_ERRCODE(TDB_ERR_LOCK, -1);
return -1;
}
return 0;
}
@ -133,10 +132,12 @@ static int _tdb_lock(struct tdb_context *tdb, int list, int ltype, int op)
}
if (tdb->global_lock.count) {
return TDB_ERRCODE(TDB_ERR_LOCK, -1);
tdb->ecode = TDB_ERR_LOCK;
return -1;
}
if (list < -1 || list >= (int)tdb->header.hash_size) {
tdb->ecode = TDB_ERR_LOCK;
TDB_LOG((tdb, TDB_DEBUG_ERROR,"tdb_lock: invalid list %d for ltype=%d\n",
list, ltype));
return -1;
@ -228,7 +229,8 @@ int tdb_unlock(struct tdb_context *tdb, int list, int ltype)
}
if (tdb->global_lock.count) {
return TDB_ERRCODE(TDB_ERR_LOCK, -1);
tdb->ecode = TDB_ERR_LOCK;
return -1;
}
if (tdb->flags & TDB_NOLOCK)
@ -350,8 +352,10 @@ static int _tdb_lockall(struct tdb_context *tdb, int ltype, int op)
ltype &= ~TDB_MARK_LOCK;
/* There are no locks on read-only dbs */
if (tdb->read_only || tdb->traverse_read)
return TDB_ERRCODE(TDB_ERR_LOCK, -1);
if (tdb->read_only || tdb->traverse_read) {
tdb->ecode = TDB_ERR_LOCK;
return -1;
}
if (tdb->global_lock.count && tdb->global_lock.ltype == ltype) {
tdb->global_lock.count++;
@ -360,12 +364,14 @@ static int _tdb_lockall(struct tdb_context *tdb, int ltype, int op)
if (tdb->global_lock.count) {
/* a global lock of a different type exists */
return TDB_ERRCODE(TDB_ERR_LOCK, -1);
tdb->ecode = TDB_ERR_LOCK;
return -1;
}
if (tdb->num_locks != 0) {
/* can't combine global and chain locks */
return TDB_ERRCODE(TDB_ERR_LOCK, -1);
tdb->ecode = TDB_ERR_LOCK;
return -1;
}
if (!mark_lock &&
@ -394,11 +400,13 @@ static int _tdb_unlockall(struct tdb_context *tdb, int ltype)
/* There are no locks on read-only dbs */
if (tdb->read_only || tdb->traverse_read) {
return TDB_ERRCODE(TDB_ERR_LOCK, -1);
tdb->ecode = TDB_ERR_LOCK;
return -1;
}
if (tdb->global_lock.ltype != ltype || tdb->global_lock.count == 0) {
return TDB_ERRCODE(TDB_ERR_LOCK, -1);
tdb->ecode = TDB_ERR_LOCK;
return -1;
}
if (tdb->global_lock.count > 1) {

View File

@ -55,8 +55,10 @@ static int tdb_new_database(struct tdb_context *tdb, int hash_size)
/* We make it up in memory, then write it out if not internal */
size = sizeof(struct tdb_header) + (hash_size+1)*sizeof(tdb_off_t);
if (!(newdb = (struct tdb_header *)calloc(size, 1)))
return TDB_ERRCODE(TDB_ERR_OOM, -1);
if (!(newdb = (struct tdb_header *)calloc(size, 1))) {
tdb->ecode = TDB_ERR_OOM;
return -1;
}
/* Fill in the header */
newdb->version = TDB_VERSION;

View File

@ -98,12 +98,14 @@ static tdb_off_t tdb_find(struct tdb_context *tdb, TDB_DATA key, uint32_t hash,
}
/* detect tight infinite loop */
if (rec_ptr == r->next) {
tdb->ecode = TDB_ERR_CORRUPT;
TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_find: loop detected.\n"));
return TDB_ERRCODE(TDB_ERR_CORRUPT, 0);
return 0;
}
rec_ptr = r->next;
}
return TDB_ERRCODE(TDB_ERR_NOEXIST, 0);
tdb->ecode = TDB_ERR_NOEXIST;
return 0;
}
/* As tdb_find, but if you succeed, keep the lock */
@ -216,7 +218,8 @@ int tdb_parse_record(struct tdb_context *tdb, TDB_DATA key,
if (!(rec_ptr = tdb_find_lock_hash(tdb,key,hash,F_RDLCK,&rec))) {
tdb_trace_1rec_ret(tdb, "tdb_parse_record", key, -1);
return TDB_ERRCODE(TDB_ERR_NOEXIST, 0);
tdb->ecode = TDB_ERR_NOEXIST;
return 0;
}
tdb_trace_1rec_ret(tdb, "tdb_parse_record", key, 0);

View File

@ -372,7 +372,8 @@ static int transaction_oob(struct tdb_context *tdb, tdb_off_t len, int probe)
if (len <= tdb->map_size) {
return 0;
}
return TDB_ERRCODE(TDB_ERR_IO, -1);
tdb->ecode = TDB_ERR_IO;
return -1;
}
/*

View File

@ -125,7 +125,8 @@ static tdb_off_t tdb_next_lock(struct tdb_context *tdb, struct tdb_traverse_lock
want_next = 0;
}
/* We finished iteration without finding anything */
return TDB_ERRCODE(TDB_SUCCESS, 0);
tdb->ecode = TDB_SUCCESS;
return 0;
fail:
tlock->off = 0;

View File

@ -49,8 +49,6 @@ extern "C" {
#define TDB_SEQNUM 128 /* maintain a sequence number */
#define TDB_VOLATILE 256 /* Activate the per-hashchain freelist, default 5 */
#define TDB_ERRCODE(code, ret) ((tdb->ecode = (code)), ret)
/* error codes */
enum TDB_ERROR {TDB_SUCCESS=0, TDB_ERR_CORRUPT, TDB_ERR_IO, TDB_ERR_LOCK,
TDB_ERR_OOM, TDB_ERR_EXISTS, TDB_ERR_NOLOCK, TDB_ERR_LOCK_TIMEOUT,