]> git.ozlabs.org Git - ccan/blobdiff - ccan/tdb2/io.c
tdb2: simplify logging levels, rename TDB_DEBUG_* to TDB_LOG_*
[ccan] / ccan / tdb2 / io.c
index ae09597ec4eecacaf362f7c9af616103d9857f1f..3c24be6774a3f0a4b841809e5a20a9cfe7f417da 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,7 +64,7 @@ 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)
@@ -80,7 +80,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,
@@ -93,7 +93,7 @@ static int tdb_oob(struct tdb_context *tdb, tdb_off_t len, bool probe)
                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 +103,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);
                }
@@ -182,7 +182,7 @@ int zero_out(struct tdb_context *tdb, tdb_off_t off, tdb_len_t len)
        }
        while (len) {
                unsigned todo = len < sizeof(buf) ? len : sizeof(buf);
-               if (tdb->methods->write(tdb, off, buf, todo) == -1)
+               if (tdb->methods->twrite(tdb, off, buf, todo) == -1)
                        return -1;
                len -= todo;
                off += todo;
@@ -206,69 +206,12 @@ tdb_off_t tdb_read_off(struct tdb_context *tdb, tdb_off_t off)
        return ret;
 }
 
-/* Even on files, we can get partial writes due to signals. */
-bool tdb_pwrite_all(int fd, const void *buf, size_t len, tdb_off_t off)
-{
-       while (len) {
-               ssize_t ret;
-               ret = pwrite(fd, buf, len, off);
-               if (ret < 0)
-                       return false;
-               if (ret == 0) {
-                       errno = ENOSPC;
-                       return false;
-               }
-               buf = (char *)buf + ret;
-               off += ret;
-               len -= ret;
-       }
-       return true;
-}
-
-/* Even on files, we can get partial reads due to signals. */
-bool tdb_pread_all(int fd, void *buf, size_t len, tdb_off_t off)
-{
-       while (len) {
-               ssize_t ret;
-               ret = pread(fd, buf, len, off);
-               if (ret < 0)
-                       return false;
-               if (ret == 0) {
-                       /* ETOOSHORT? */
-                       errno = EWOULDBLOCK;
-                       return false;
-               }
-               buf = (char *)buf + ret;
-               off += ret;
-               len -= ret;
-       }
-       return true;
-}
-
-bool tdb_read_all(int fd, void *buf, size_t len)
-{
-       while (len) {
-               ssize_t ret;
-               ret = read(fd, buf, len);
-               if (ret < 0)
-                       return false;
-               if (ret == 0) {
-                       /* ETOOSHORT? */
-                       errno = EWOULDBLOCK;
-                       return false;
-               }
-               buf = (char *)buf + ret;
-               len -= ret;
-       }
-       return true;
-}
-
 /* 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;
        }
@@ -284,10 +227,17 @@ static int tdb_write(struct tdb_context *tdb, tdb_off_t off,
        if (tdb->map_ptr) {
                memcpy(off + (char *)tdb->map_ptr, buf, len);
        } else {
-               if (!tdb_pwrite_all(tdb->fd, buf, len, off)) {
-                       tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_FATAL,
-                                  "tdb_write failed at %zu len=%zu (%s)",
-                                  (size_t)off, (size_t)len, strerror(errno));
+               ssize_t ret;
+               ret = pwrite(tdb->fd, buf, len, off);
+               if (ret < len) {
+                       /* This shouldn't happen: we avoid sparse files. */
+                       if (ret >= 0)
+                               errno = ENOSPC;
+
+                       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));
                        return -1;
                }
        }
@@ -305,13 +255,14 @@ static int tdb_read(struct tdb_context *tdb, tdb_off_t off, void *buf,
        if (tdb->map_ptr) {
                memcpy(buf, off + (char *)tdb->map_ptr, len);
        } else {
-               if (!tdb_pread_all(tdb->fd, buf, len, off)) {
-                       tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_FATAL,
-                                  "tdb_read failed at %zu "
+               ssize_t r = pread(tdb->fd, buf, len, off);
+               if (r != len) {
+                       tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
+                                  "tdb_read failed with %zi at %zu "
                                   "len=%zu (%s) map_size=%zu",
-                                (size_t)off, (size_t)len,
-                                strerror(errno),
-                                (size_t)tdb->map_size);
+                                  r, (size_t)off, (size_t)len,
+                                  strerror(errno),
+                                  (size_t)tdb->map_size);
                        return -1;
                }
        }
@@ -325,17 +276,17 @@ 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;
                }
                memcpy(conv, rec, len);
-               ret = tdb->methods->write(tdb, off,
-                                         tdb_convert(tdb, conv, len), len);
+               ret = tdb->methods->twrite(tdb, off,
+                                          tdb_convert(tdb, conv, len), len);
                free(conv);
        } else
-               ret = tdb->methods->write(tdb, off, rec, len);
+               ret = tdb->methods->twrite(tdb, off, rec, len);
 
        return ret;
 }
@@ -343,7 +294,7 @@ int tdb_write_convert(struct tdb_context *tdb, tdb_off_t off,
 int tdb_read_convert(struct tdb_context *tdb, tdb_off_t off,
                      void *rec, size_t len)
 {
-       int ret = tdb->methods->read(tdb, off, rec, len);
+       int ret = tdb->methods->tread(tdb, off, rec, len);
        tdb_convert(tdb, rec, len);
        return ret;
 }
@@ -351,7 +302,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;
        }
@@ -375,11 +326,11 @@ 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->read(tdb, offset, buf+prefix,
-                                              len) == -1)) {
+       } else if (unlikely(tdb->methods->tread(tdb, offset, buf+prefix, len)
+                           == -1)) {
                free(buf);
                buf = NULL;
        }
@@ -398,10 +349,15 @@ static int fill(struct tdb_context *tdb,
 {
        while (len) {
                size_t n = len > size ? size : len;
-
-               if (!tdb_pwrite_all(tdb->fd, buf, n, off)) {
-                       tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_FATAL,
-                                "fill write failed: giving up!");
+               ssize_t ret = pwrite(tdb->fd, buf, n, off);
+               if (ret < n) {
+                       if (ret >= 0)
+                               errno = ENOSPC;
+
+                       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));
                        return -1;
                }
                len -= n;
@@ -417,7 +373,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;
        }
@@ -425,7 +381,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;
                }
@@ -455,7 +411,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);
@@ -482,7 +438,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;
        }
@@ -552,7 +508,7 @@ int tdb_access_commit(struct tdb_context *tdb, void *p)
 }
 
 static void *tdb_direct(struct tdb_context *tdb, tdb_off_t off, size_t len,
-                       bool write)
+                       bool write_mode)
 {
        if (unlikely(!tdb->map_ptr))
                return NULL;
@@ -562,10 +518,10 @@ static void *tdb_direct(struct tdb_context *tdb, tdb_off_t off, size_t len,
        return (char *)tdb->map_ptr + off;
 }
 
-void add_stat_(struct tdb_context *tdb, uint64_t *stat, size_t val)
+void add_stat_(struct tdb_context *tdb, uint64_t *s, size_t val)
 {
-       if ((uintptr_t)stat < (uintptr_t)tdb->stats + tdb->stats->size)
-               *stat += val;
+       if ((uintptr_t)s < (uintptr_t)tdb->stats + tdb->stats->size)
+               *s += val;
 }
 
 static const struct tdb_methods io_methods = {