Browse Source

Wean off TDB_ERRCODE.
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.

Rusty Russell 16 years ago
parent
commit
bcf7916c5d
9 changed files with 58 additions and 38 deletions
  1. 3 2
      ccan/tdb/freelist.c
  2. 4 2
      ccan/tdb/freelistcheck.c
  3. 19 16
      ccan/tdb/io.c
  4. 18 9
      ccan/tdb/lock.c
  5. 4 2
      ccan/tdb/open.c
  6. 6 3
      ccan/tdb/tdb.c
  7. 0 2
      ccan/tdb/tdb.h
  8. 2 1
      ccan/tdb/transaction.c
  9. 2 1
      ccan/tdb/traverse.c

+ 3 - 2
ccan/tdb/freelist.c

@@ -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
 

+ 4 - 2
ccan/tdb/freelistcheck.c

@@ -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;
 		}
 

+ 19 - 16
ccan/tdb/io.c

@@ -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);
 }

+ 18 - 9
ccan/tdb/lock.c

@@ -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) {

+ 4 - 2
ccan/tdb/open.c

@@ -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;

+ 6 - 3
ccan/tdb/tdb.c

@@ -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);
 

+ 0 - 2
ccan/tdb/tdb.h

@@ -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,

+ 2 - 1
ccan/tdb/transaction.c

@@ -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;
 }
 
 /*

+ 2 - 1
ccan/tdb/traverse.c

@@ -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;