From b77f41d58b05101e02d8ac0e54cb0e30807d89c2 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 22 Oct 2009 00:09:43 +1030 Subject: 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 --- lib/tdb/common/io.c | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) (limited to 'lib/tdb/common/io.c') diff --git a/lib/tdb/common/io.c b/lib/tdb/common/io.c index a0b3a3f24a..cc52bcfd69 100644 --- a/lib/tdb/common/io.c +++ b/lib/tdb/common/io.c @@ -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); } -- cgit