]> git.ozlabs.org Git - ccan/blobdiff - ccan/tdb/io.c
Import 03b3682e3fa53c9f5fdf2c4beac8b5d030fd2630 from ctdb:
[ccan] / ccan / tdb / io.c
index 172ab69d8c9e69ae7f160071567c4d885ac7848c..929290c1407b092ab2b4eb38ca704efbf450cd75 100644 (file)
    License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */
 
-
 #include "tdb_private.h"
+#ifndef MAX
+#define MAX(a,b) ((a) > (b) ? (a) : (b))
+#endif
 
 /* check for an out of bounds access - if it is out of bounds then
    see if the database has been expanded by someone else and expand
@@ -45,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) {
@@ -59,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;
@@ -92,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;
 }
@@ -146,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) {
@@ -187,7 +192,7 @@ int tdb_munmap(struct tdb_context *tdb)
        if (tdb->flags & TDB_INTERNAL)
                return 0;
 
-#ifdef HAVE_MMAP
+#if HAVE_MMAP
        if (tdb->map_ptr) {
                int ret = munmap(tdb->map_ptr, tdb->map_size);
                if (ret != 0)
@@ -203,11 +208,11 @@ void tdb_mmap(struct tdb_context *tdb)
        if (tdb->flags & TDB_INTERNAL)
                return;
 
-#ifdef HAVE_MMAP
+#if HAVE_MMAP
        if (!(tdb->flags & TDB_NOMMAP)) {
                tdb->map_ptr = mmap(NULL, tdb->map_size, 
                                    PROT_READ|(tdb->read_only? 0:PROT_WRITE), 
-                                   MAP_SHARED|MAP_FILE, tdb->fd, 0);
+                                   MAP_SHARED, tdb->fd, 0);
 
                /*
                 * NB. When mmap fails it returns MAP_FAILED *NOT* NULL !!!!
@@ -293,7 +298,7 @@ static int tdb_expand_file(struct tdb_context *tdb, tdb_off_t size, tdb_off_t ad
    file and doing the mmap again if necessary */
 int tdb_expand(struct tdb_context *tdb, tdb_off_t size)
 {
-       struct list_struct rec;
+       struct tdb_record rec;
        tdb_off_t offset, new_size;     
 
        if (tdb_lock(tdb, -1, F_WRLCK) == -1) {
@@ -381,16 +386,12 @@ unsigned char *tdb_alloc_read(struct tdb_context *tdb, tdb_off_t offset, tdb_len
        unsigned char *buf;
 
        /* some systems don't like zero length malloc */
-       if (len == 0) {
-               len = 1;
-       }
-
-       if (!(buf = (unsigned char *)malloc(len))) {
+       if (!(buf = (unsigned char *)malloc(len ? len : 1))) {
                /* Ensure ecode is set for log fn. */
                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);
@@ -434,7 +435,7 @@ int tdb_parse_data(struct tdb_context *tdb, TDB_DATA key,
 }
 
 /* read/write a record */
-int tdb_rec_read(struct tdb_context *tdb, tdb_off_t offset, struct list_struct *rec)
+int tdb_rec_read(struct tdb_context *tdb, tdb_off_t offset, struct tdb_record *rec)
 {
        if (tdb->methods->tdb_read(tdb, offset, rec, sizeof(*rec),DOCONV()) == -1)
                return -1;
@@ -442,14 +443,14 @@ 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);
 }
 
-int tdb_rec_write(struct tdb_context *tdb, tdb_off_t offset, struct list_struct *rec)
+int tdb_rec_write(struct tdb_context *tdb, tdb_off_t offset, struct tdb_record *rec)
 {
-       struct list_struct r = *rec;
+       struct tdb_record r = *rec;
        return tdb->methods->tdb_write(tdb, offset, CONVERT(r), sizeof(r));
 }
 
@@ -459,7 +460,8 @@ static const struct tdb_methods io_methods = {
        tdb_next_hash_chain,
        tdb_oob,
        tdb_expand_file,
-       tdb_brlock
+       tdb_brlock,
+       tdb_brunlock
 };
 
 /*