Wean off TDB_ERRCODE.
authorRusty Russell <rusty@rustcorp.com.au>
Tue, 4 Aug 2009 03:52:28 +0000 (13:22 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Tue, 4 Aug 2009 03:52:28 +0000 (13:22 +0930)
It was a regrettable hack 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.
Also, we should never set errno, as io.c was doing.

ccan/tdb/freelist.c
ccan/tdb/freelistcheck.c
ccan/tdb/io.c
ccan/tdb/lock.c
ccan/tdb/open.c
ccan/tdb/tdb.c
ccan/tdb/tdb.h
ccan/tdb/transaction.c
ccan/tdb/traverse.c

index 3bc3965141b605ba5ad62f918e156e9deece9aed..dedf78cf921e98d80eeeb3968b3484b47b273bf2 100644 (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
 
index efc050df9c65f8650bf0631fc394394c19501ac0..972b2a41c49e32f8d7ff479490702973f43ecc95 100644 (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;
                }
 
index d8140fea31d3655146b697a1dd0959502bafa33e..59caa5dd04c37e13a63132231c7dd6986982b011 100644 (file)
@@ -47,11 +47,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) {
@@ -61,12 +62,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;
@@ -94,27 +97,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;
 }
@@ -148,7 +151,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) {
@@ -388,7 +391,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);
@@ -440,7 +443,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);
 }
index 326d38e70713443f86aa45e821b20e883d3df44a..07545b345ba0c6d80af5ed541376eca195afb87b 100644 (file)
@@ -84,7 +84,7 @@ int tdb_brlock(struct tdb_context *tdb, tdb_off_t offset,
                        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 +133,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 +230,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 +353,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 +365,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 +401,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) {
index 0292920c11b4d565c1f4df9c1b7c032cea995d54..a5a8e875cbcd5ca4ad927ee93474a25b01ab6252 100644 (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;
index 3e663345e60c5f9ebc14e1d41813f39a7d3bc4bd..cb2af49f8f9f7b8bb29c4f86d0624712579543ea 100644 (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 */
@@ -217,7 +219,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,
                                   -TDB_ERR_NOEXIST);
-               return TDB_ERRCODE(TDB_ERR_NOEXIST, 0);
+               tdb->ecode = TDB_ERR_NOEXIST;
+               return 0;
        }
        tdb_trace_1rec_ret(tdb, "tdb_parse_record", key, 0);
 
index 80bf89617e7d2dcdf13f23a82d047c340ec800ba..5ab724031dc5bebeccc920bfc6142a542bb39bc0 100644 (file)
@@ -57,8 +57,6 @@ extern "C" {
 #define TDB_VOLATILE   256 /* Activate the per-hashchain freelist, default 5 */
 #define TDB_NO_NESTING 512 /* Dont allow transaction nesting */
 
-#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,
index 0944bb36e98a6a3af4e32ac2052e512483bc4311..6909c261a2808ca50c33cb413a5597cde8d4cbd5 100644 (file)
@@ -385,7 +385,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;
 }
 
 /*
index d8b15aff4aa39f22f76fd6080e8e251404be4d75..4b3a316229c2059ef1977b9466b2d5d5fe34473d 100644 (file)
@@ -121,7 +121,8 @@ static int tdb_next_lock(struct tdb_context *tdb, struct tdb_traverse_lock *tloc
                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;