]> git.ozlabs.org Git - ccan/blobdiff - ccan/tdb2/io.c
tdb2: restore file filling code.
[ccan] / ccan / tdb2 / io.c
index 13af1aecc14336043ff8cc85856429152195a301..f326d98ba6d03e4c49e37409ae61280ae8a894bb 100644 (file)
@@ -1,4 +1,4 @@
- /* 
+ /*
    Unix SMB/CIFS implementation.
 
    trivial database library
@@ -56,7 +56,7 @@ void tdb_mmap(struct tdb_context *tdb)
         */
        if (tdb->map_ptr == MAP_FAILED) {
                tdb->map_ptr = NULL;
-               tdb_logerr(tdb, TDB_SUCCESS, TDB_DEBUG_WARNING,
+               tdb_logerr(tdb, TDB_SUCCESS, TDB_LOG_WARNING,
                           "tdb_mmap failed for size %lld (%s)",
                           (long long)tdb->map_size, strerror(errno));
        }
@@ -64,12 +64,13 @@ void tdb_mmap(struct tdb_context *tdb)
 
 /* 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
-   if necessary 
+   if necessary
    note that "len" is the minimum length needed for the db
 */
 static int tdb_oob(struct tdb_context *tdb, tdb_off_t len, bool probe)
 {
        struct stat st;
+       enum TDB_ERROR ecode;
 
        /* We can't hold pointers during this: we could unmap! */
        assert(!tdb->direct_access
@@ -80,7 +81,7 @@ static int tdb_oob(struct tdb_context *tdb, tdb_off_t len, bool probe)
                return 0;
        if (tdb->flags & TDB_INTERNAL) {
                if (!probe) {
-                       tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_FATAL,
+                       tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
                                 "tdb_oob len %lld beyond internal"
                                 " malloc size %lld",
                                 (long long)len,
@@ -89,11 +90,14 @@ static int tdb_oob(struct tdb_context *tdb, tdb_off_t len, bool probe)
                return -1;
        }
 
-       if (tdb_lock_expand(tdb, F_RDLCK) != 0)
+       ecode = tdb_lock_expand(tdb, F_RDLCK);
+       if (ecode != TDB_SUCCESS) {
+               tdb->ecode = ecode;
                return -1;
+       }
 
        if (fstat(tdb->fd, &st) != 0) {
-               tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_FATAL,
+               tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
                           "Failed to fstat file: %s", strerror(errno));
                tdb_unlock_expand(tdb, F_RDLCK);
                return -1;
@@ -103,7 +107,7 @@ static int tdb_oob(struct tdb_context *tdb, tdb_off_t len, bool probe)
 
        if (st.st_size < (size_t)len) {
                if (!probe) {
-                       tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_FATAL,
+                       tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
                                   "tdb_oob len %zu beyond eof at %zu",
                                   (size_t)len, st.st_size);
                }
@@ -207,11 +211,11 @@ tdb_off_t tdb_read_off(struct tdb_context *tdb, tdb_off_t off)
 }
 
 /* write a lump of data at a specified offset */
-static int tdb_write(struct tdb_context *tdb, tdb_off_t off, 
+static int tdb_write(struct tdb_context *tdb, tdb_off_t off,
                     const void *buf, tdb_len_t len)
 {
        if (tdb->read_only) {
-               tdb_logerr(tdb, TDB_ERR_RDONLY, TDB_DEBUG_WARNING,
+               tdb_logerr(tdb, TDB_ERR_RDONLY, TDB_LOG_USE_ERROR,
                           "Write to read-only database");
                return -1;
        }
@@ -234,7 +238,7 @@ static int tdb_write(struct tdb_context *tdb, tdb_off_t off,
                        if (ret >= 0)
                                errno = ENOSPC;
 
-                       tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_FATAL,
+                       tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
                                   "tdb_write: %zi at %zu len=%zu (%s)",
                                   ret, (size_t)off, (size_t)len,
                                   strerror(errno));
@@ -257,7 +261,7 @@ static int tdb_read(struct tdb_context *tdb, tdb_off_t off, void *buf,
        } else {
                ssize_t r = pread(tdb->fd, buf, len, off);
                if (r != len) {
-                       tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_FATAL,
+                       tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
                                   "tdb_read failed with %zi at %zu "
                                   "len=%zu (%s) map_size=%zu",
                                   r, (size_t)off, (size_t)len,
@@ -276,7 +280,7 @@ int tdb_write_convert(struct tdb_context *tdb, tdb_off_t off,
        if (unlikely((tdb->flags & TDB_CONVERT))) {
                void *conv = malloc(len);
                if (!conv) {
-                       tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_FATAL,
+                       tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
                                   "tdb_write: no memory converting"
                                   " %zu bytes", len);
                        return -1;
@@ -302,7 +306,7 @@ int tdb_read_convert(struct tdb_context *tdb, tdb_off_t off,
 int tdb_write_off(struct tdb_context *tdb, tdb_off_t off, tdb_off_t val)
 {
        if (tdb->read_only) {
-               tdb_logerr(tdb, TDB_ERR_RDONLY, TDB_DEBUG_WARNING,
+               tdb_logerr(tdb, TDB_ERR_RDONLY, TDB_LOG_USE_ERROR,
                           "Write to read-only database");
                return -1;
        }
@@ -326,7 +330,7 @@ static void *_tdb_alloc_read(struct tdb_context *tdb, tdb_off_t offset,
        /* some systems don't like zero length malloc */
        buf = malloc(prefix + len ? prefix + len : 1);
        if (!buf) {
-               tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_ERROR,
+               tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_USE_ERROR,
                           "tdb_alloc_read malloc failed len=%zu",
                           (size_t)(prefix + len));
        } else if (unlikely(tdb->methods->tread(tdb, offset, buf+prefix, len)
@@ -354,7 +358,7 @@ static int fill(struct tdb_context *tdb,
                        if (ret >= 0)
                                errno = ENOSPC;
 
-                       tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_FATAL,
+                       tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
                                   "fill failed: %zi at %zu len=%zu (%s)",
                                   ret, (size_t)off, (size_t)len,
                                   strerror(errno));
@@ -373,7 +377,7 @@ static int tdb_expand_file(struct tdb_context *tdb, tdb_len_t addition)
        char buf[8192];
 
        if (tdb->read_only) {
-               tdb_logerr(tdb, TDB_ERR_RDONLY, TDB_DEBUG_WARNING,
+               tdb_logerr(tdb, TDB_ERR_RDONLY, TDB_LOG_USE_ERROR,
                           "Expand on read-only database");
                return -1;
        }
@@ -381,7 +385,7 @@ static int tdb_expand_file(struct tdb_context *tdb, tdb_len_t addition)
        if (tdb->flags & TDB_INTERNAL) {
                char *new = realloc(tdb->map_ptr, tdb->map_size + addition);
                if (!new) {
-                       tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_FATAL,
+                       tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
                                   "No memory to expand database");
                        return -1;
                }
@@ -400,7 +404,7 @@ static int tdb_expand_file(struct tdb_context *tdb, tdb_len_t addition)
                   file isn't sparse, which would be very bad if we ran out of
                   disk. This must be done with write, not via mmap */
                memset(buf, 0x43, sizeof(buf));
-               if (0 || fill(tdb, buf, sizeof(buf), tdb->map_size, addition) == -1)
+               if (fill(tdb, buf, sizeof(buf), tdb->map_size, addition) == -1)
                        return -1;
                tdb->map_size += addition;
                tdb_mmap(tdb);
@@ -411,7 +415,7 @@ static int tdb_expand_file(struct tdb_context *tdb, tdb_len_t addition)
 const void *tdb_access_read(struct tdb_context *tdb,
                            tdb_off_t off, tdb_len_t len, bool convert)
 {
-       const void *ret = NULL; 
+       const void *ret = NULL;
 
        if (likely(!(tdb->flags & TDB_CONVERT)))
                ret = tdb->methods->direct(tdb, off, len, false);
@@ -438,7 +442,7 @@ void *tdb_access_write(struct tdb_context *tdb,
        void *ret = NULL;
 
        if (tdb->read_only) {
-               tdb_logerr(tdb, TDB_ERR_RDONLY, TDB_DEBUG_WARNING,
+               tdb_logerr(tdb, TDB_ERR_RDONLY, TDB_LOG_USE_ERROR,
                           "Write to read-only database");
                return NULL;
        }