]> git.ozlabs.org Git - ccan/blobdiff - ccan/tdb2/check.c
tdb2: feature support.
[ccan] / ccan / tdb2 / check.c
index 4f340a2e15e7faaa7112a90291fa2945faea510b..9bbd12645f199ba6a5a62708caccc79d08cf16d1 100644 (file)
@@ -80,7 +80,8 @@ static enum TDB_ERROR check_hash_tree(struct tdb_context *tdb,
                                      tdb_off_t used[],
                                      size_t num_used,
                                      size_t *num_found,
-                                     int (*check)(TDB_DATA, TDB_DATA, void *),
+                                     enum TDB_ERROR (*check)(TDB_DATA,
+                                                             TDB_DATA, void *),
                                      void *private_data);
 
 static enum TDB_ERROR check_hash_chain(struct tdb_context *tdb,
@@ -89,7 +90,9 @@ static enum TDB_ERROR check_hash_chain(struct tdb_context *tdb,
                                       tdb_off_t used[],
                                       size_t num_used,
                                       size_t *num_found,
-                                      int (*check)(TDB_DATA, TDB_DATA, void *),
+                                      enum TDB_ERROR (*check)(TDB_DATA,
+                                                              TDB_DATA,
+                                                              void *),
                                       void *private_data)
 {
        struct tdb_used_record rec;
@@ -128,7 +131,7 @@ static enum TDB_ERROR check_hash_chain(struct tdb_context *tdb,
        ecode = check_hash_tree(tdb, off, 0, hash, 64,
                                used, num_used, num_found, check, private_data);
        if (ecode != TDB_SUCCESS) {
-               return false;
+               return ecode;
        }
 
        off = tdb_read_off(tdb, off + offsetof(struct tdb_chain, next));
@@ -149,7 +152,9 @@ static enum TDB_ERROR check_hash_record(struct tdb_context *tdb,
                                        tdb_off_t used[],
                                        size_t num_used,
                                        size_t *num_found,
-                                       int (*check)(TDB_DATA, TDB_DATA, void*),
+                                       enum TDB_ERROR (*check)(TDB_DATA,
+                                                               TDB_DATA,
+                                                               void *),
                                        void *private_data)
 {
        struct tdb_used_record rec;
@@ -218,7 +223,8 @@ static enum TDB_ERROR check_hash_tree(struct tdb_context *tdb,
                                      tdb_off_t used[],
                                      size_t num_used,
                                      size_t *num_found,
-                                     int (*check)(TDB_DATA, TDB_DATA, void *),
+                                     enum TDB_ERROR (*check)(TDB_DATA,
+                                                             TDB_DATA, void *),
                                      void *private_data)
 {
        unsigned int g, b;
@@ -395,8 +401,8 @@ static enum TDB_ERROR check_hash_tree(struct tdb_context *tdb,
                                        goto fail;
                                }
                                data.dptr = key.dptr + key.dsize;
-                               if (check(key, data, private_data) != 0) {
-                                       ecode = TDB_ERR_CORRUPT;
+                               ecode = check(key, data, private_data);
+                               if (ecode != TDB_SUCCESS) {
                                        goto fail;
                                }
                                tdb_access_release(tdb, key.dptr);
@@ -520,7 +526,7 @@ static enum TDB_ERROR check_free_table(struct tdb_context *tdb,
                        }
                        ecode = check_free(tdb, off, &f, prev, ftable_num, i);
                        if (ecode != TDB_SUCCESS) {
-                               return false;
+                               return ecode;
                        }
 
                        /* FIXME: Check hash bits */
@@ -711,9 +717,10 @@ static enum TDB_ERROR check_linear(struct tdb_context *tdb,
        return TDB_SUCCESS;
 }
 
-int tdb_check(struct tdb_context *tdb,
-             int (*check)(TDB_DATA key, TDB_DATA data, void *private_data),
-             void *private_data)
+enum TDB_ERROR tdb_check_(struct tdb_context *tdb,
+                         enum TDB_ERROR (*check)(TDB_DATA key, TDB_DATA data,
+                                                 void *private),
+                         void *private)
 {
        tdb_off_t *fr = NULL, *used = NULL, ft, recovery;
        size_t num_free = 0, num_used = 0, num_found = 0, num_ftables = 0;
@@ -721,15 +728,13 @@ int tdb_check(struct tdb_context *tdb,
 
        ecode = tdb_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_WAIT, false);
        if (ecode != TDB_SUCCESS) {
-               tdb->ecode = ecode;
-               return -1;
+               return ecode;
        }
 
        ecode = tdb_lock_expand(tdb, F_RDLCK);
        if (ecode != TDB_SUCCESS) {
-               tdb->ecode = ecode;
                tdb_allrecord_unlock(tdb, F_RDLCK);
-               return -1;
+               return ecode;
        }
 
        ecode = check_header(tdb, &recovery);
@@ -743,7 +748,7 @@ int tdb_check(struct tdb_context *tdb,
 
        for (ft = first_ftable(tdb); ft; ft = next_ftable(tdb, ft)) {
                if (TDB_OFF_IS_ERR(ft)) {
-                       tdb->ecode = ft;
+                       ecode = ft;
                        goto out;
                }
                ecode = check_free_table(tdb, ft, num_ftables, fr, num_free,
@@ -754,8 +759,7 @@ int tdb_check(struct tdb_context *tdb,
        }
 
        /* FIXME: Check key uniqueness? */
-       ecode = check_hash(tdb, used, num_used, num_ftables, check,
-                          private_data);
+       ecode = check_hash(tdb, used, num_used, num_ftables, check, private);
        if (ecode != TDB_SUCCESS)
                goto out;
 
@@ -770,9 +774,5 @@ out:
        tdb_unlock_expand(tdb, F_RDLCK);
        free(fr);
        free(used);
-       if (ecode != TDB_SUCCESS) {
-               tdb->ecode = ecode;
-               return -1;
-       }
-       return 0;
+       return ecode;
 }