]> git.ozlabs.org Git - ccan/blobdiff - ccan/tdb2/tdb1_check.c
failtest: add --trace to replace --tracepath
[ccan] / ccan / tdb2 / tdb1_check.c
index 50e71d9783461a67442e99323d111dd208920215..a8e54b2ee092f4cb7e1866818551e4e6177f1e5b 100644 (file)
@@ -236,7 +236,8 @@ static bool tdb1_check_used_record(struct tdb_context *tdb,
                                  tdb1_off_t off,
                                  const struct tdb1_record *rec,
                                  unsigned char **hashes,
-                                 int (*check)(TDB_DATA, TDB_DATA, void *),
+                                 enum TDB_ERROR (*check)(TDB_DATA, TDB_DATA,
+                                                         void *),
                                  void *private_data)
 {
        TDB_DATA key, data;
@@ -270,13 +271,18 @@ static bool tdb1_check_used_record(struct tdb_context *tdb,
        /* If they supply a check function and this record isn't dead,
           get data and feed it. */
        if (check && rec->magic != TDB1_DEAD_MAGIC) {
+               enum TDB_ERROR ecode;
+
                data = get_bytes(tdb, off + sizeof(*rec) + rec->key_len,
                                 rec->data_len);
                if (!data.dptr)
                        goto fail_put_key;
 
-               if (check(key, data, private_data) == -1)
+               ecode = check(key, data, private_data);
+               if (ecode != TDB_SUCCESS) {
+                       tdb->last_error = ecode;
                        goto fail_put_data;
+               }
                put_bytes(tdb, data);
        }
 
@@ -323,8 +329,8 @@ size_t tdb1_dead_space(struct tdb_context *tdb, tdb1_off_t off)
 }
 
 int tdb1_check(struct tdb_context *tdb,
-             int (*check)(TDB_DATA key, TDB_DATA data, void *private_data),
-             void *private_data)
+              enum TDB_ERROR (*check)(TDB_DATA key, TDB_DATA data, void *),
+              void *private_data)
 {
        unsigned int h;
        unsigned char **hashes;
@@ -333,12 +339,13 @@ int tdb1_check(struct tdb_context *tdb,
        bool found_recovery = false;
        tdb1_len_t dead;
        bool locked;
+       size_t alloc_len;
 
        /* We may have a write lock already, so don't re-lock. */
        if (tdb->file->allrecord_lock.count != 0) {
                locked = false;
        } else {
-               if (tdb1_lockall_read(tdb) == -1)
+               if (tdb_lockall_read(tdb) != TDB_SUCCESS)
                        return -1;
                locked = true;
        }
@@ -358,11 +365,13 @@ int tdb1_check(struct tdb_context *tdb,
        }
 
        /* One big malloc: pointers then bit arrays. */
-       hashes = (unsigned char **)calloc(
-                       1, sizeof(hashes[0]) * (1+tdb->tdb1.header.hash_size)
-                       + BITMAP_BITS / CHAR_BIT * (1+tdb->tdb1.header.hash_size));
+       alloc_len = sizeof(hashes[0]) * (1+tdb->tdb1.header.hash_size)
+               + BITMAP_BITS / CHAR_BIT * (1+tdb->tdb1.header.hash_size);
+       hashes = (unsigned char **)calloc(1, alloc_len);
        if (!hashes) {
-               tdb->last_error = TDB_ERR_OOM;
+               tdb->last_error = tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
+                                            "tdb_check: could not allocate %zu",
+                                            alloc_len);
                goto unlock;
        }
 
@@ -455,7 +464,7 @@ int tdb1_check(struct tdb_context *tdb,
 
        free(hashes);
        if (locked) {
-               tdb1_unlockall_read(tdb);
+               tdb_unlockall_read(tdb);
        }
        return 0;
 
@@ -463,7 +472,7 @@ free:
        free(hashes);
 unlock:
        if (locked) {
-               tdb1_unlockall_read(tdb);
+               tdb_unlockall_read(tdb);
        }
        return -1;
 }