]> git.ozlabs.org Git - ccan/blobdiff - ccan/tdb2/tdb.c
tdb2: rework some io.c functions to return enum TDB_ERROR.
[ccan] / ccan / tdb2 / tdb.c
index fa8e88769bc5505a6836e546b0143aa1292b4f82..8079dad7887d89a649d3ec8aa67daf215d0af041 100644 (file)
@@ -173,6 +173,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
        ssize_t rlen;
        struct tdb_header hdr;
        struct tdb_attribute_seed *seed = NULL;
+       enum TDB_ERROR ecode;
 
        tdb = malloc(sizeof(*tdb));
        if (!tdb) {
@@ -265,9 +266,8 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
         fcntl(tdb->fd, F_SETFD, v | FD_CLOEXEC);
 
        /* ensure there is only one process initialising at once */
-       if (tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK) == -1) {
-               /* errno set by tdb_brlock */
-               saved_errno = errno;
+       tdb->ecode = tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK);
+       if (tdb->ecode != TDB_SUCCESS) {
                goto fail;
        }
 
@@ -346,8 +346,12 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
        tdb->methods->oob(tdb, tdb->map_size + 1, true);
 
        /* Now it's fully formed, recover if necessary. */
-       if (tdb_needs_recovery(tdb) && tdb_lock_and_recover(tdb) == -1) {
-               goto fail;
+       if (tdb_needs_recovery(tdb)) {
+               ecode = tdb_lock_and_recover(tdb);
+               if (ecode != TDB_SUCCESS) {
+                       tdb->ecode = ecode;
+                       goto fail;
+               }
        }
 
        if (tdb_ftable_init(tdb) == -1)
@@ -409,12 +413,18 @@ static int update_rec_hdr(struct tdb_context *tdb,
                          uint64_t h)
 {
        uint64_t dataroom = rec_data_length(rec) + rec_extra_padding(rec);
+       enum TDB_ERROR ecode;
 
        if (set_header(tdb, rec, TDB_USED_MAGIC, keylen, datalen,
                       keylen + dataroom, h))
                return -1;
 
-       return tdb_write_convert(tdb, off, rec, sizeof(*rec));
+       ecode = tdb_write_convert(tdb, off, rec, sizeof(*rec));
+       if (ecode != TDB_SUCCESS) {
+               tdb->ecode = ecode;
+               return -1;
+       }
+       return 0;
 }
 
 /* Returns -1 on error, 0 on OK */
@@ -425,6 +435,7 @@ static int replace_data(struct tdb_context *tdb,
                        bool growing)
 {
        tdb_off_t new_off;
+       enum TDB_ERROR ecode;
 
        /* Allocate a new record. */
        new_off = alloc(tdb, key.dsize, dbuf.dsize, h->h, TDB_USED_MAGIC,
@@ -446,12 +457,18 @@ static int replace_data(struct tdb_context *tdb,
        }
 
        new_off += sizeof(struct tdb_used_record);
-       if (tdb->methods->twrite(tdb, new_off, key.dptr, key.dsize) == -1)
+       ecode = tdb->methods->twrite(tdb, new_off, key.dptr, key.dsize);
+       if (ecode != TDB_SUCCESS) {
+               tdb->ecode = ecode;
                return -1;
+       }
 
        new_off += key.dsize;
-       if (tdb->methods->twrite(tdb, new_off, dbuf.dptr, dbuf.dsize) == -1)
+       ecode = tdb->methods->twrite(tdb, new_off, dbuf.dptr, dbuf.dsize);
+       if (ecode != TDB_SUCCESS) {
+               tdb->ecode = ecode;
                return -1;
+       }
 
        /* FIXME: tdb_increment_seqnum(tdb); */
        return 0;
@@ -465,6 +482,7 @@ int tdb_store(struct tdb_context *tdb,
        tdb_len_t old_room = 0;
        struct tdb_used_record rec;
        int ret;
+       enum TDB_ERROR ecode;
 
        off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
        if (unlikely(off == TDB_OFF_ERR))
@@ -486,10 +504,15 @@ int tdb_store(struct tdb_context *tdb,
                                                   key.dsize, dbuf.dsize,
                                                   &rec, h.h))
                                        goto fail;
-                               if (tdb->methods->twrite(tdb, off + sizeof(rec)
-                                                        + key.dsize,
-                                                        dbuf.dptr, dbuf.dsize))
+                               ecode = tdb->methods->twrite(tdb,
+                                                            off + sizeof(rec)
+                                                            + key.dsize,
+                                                            dbuf.dptr,
+                                                            dbuf.dsize);
+                               if (ecode != TDB_SUCCESS) {
+                                       tdb->ecode = ecode;
                                        goto fail;
+                               }
                                tdb_unlock_hashes(tdb, h.hlock_start,
                                                  h.hlock_range, F_WRLCK);
                                return 0;
@@ -524,6 +547,7 @@ int tdb_append(struct tdb_context *tdb,
        tdb_len_t old_room = 0, old_dlen;
        unsigned char *newdata;
        struct tdb_data new_dbuf;
+       enum TDB_ERROR ecode;
        int ret;
 
        off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
@@ -541,9 +565,12 @@ int tdb_append(struct tdb_context *tdb,
                                goto fail;
 
                        off += sizeof(rec) + key.dsize + old_dlen;
-                       if (tdb->methods->twrite(tdb, off, dbuf.dptr,
-                                                dbuf.dsize) == -1)
+                       ecode = tdb->methods->twrite(tdb, off, dbuf.dptr,
+                                                    dbuf.dsize);
+                       if (ecode != TDB_SUCCESS) {
+                               tdb->ecode = ecode;
                                goto fail;
+                       }
 
                        /* FIXME: tdb_increment_seqnum(tdb); */
                        tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range,
@@ -559,8 +586,10 @@ int tdb_append(struct tdb_context *tdb,
                                   (size_t)(key.dsize+old_dlen+dbuf.dsize));
                        goto fail;
                }
-               if (tdb->methods->tread(tdb, off + sizeof(rec) + key.dsize,
-                                       newdata, old_dlen) != 0) {
+               ecode = tdb->methods->tread(tdb, off + sizeof(rec) + key.dsize,
+                                           newdata, old_dlen);
+               if (ecode != TDB_SUCCESS) {
+                       tdb->ecode = ecode;
                        free(newdata);
                        goto fail;
                }