tdb2: unify tdb1_check and tdb1_summary into tdb_check and tdb_summary.
authorRusty Russell <rusty@rustcorp.com.au>
Wed, 31 Aug 2011 06:01:07 +0000 (15:31 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Wed, 31 Aug 2011 06:01:07 +0000 (15:31 +0930)
Switch on the TDB_VERSION1 flag.  Also, change tdb1_check's checkfn argument
to return an error code (and set tdb->last_error accordingly).

12 files changed:
ccan/tdb2/check.c
ccan/tdb2/private.h
ccan/tdb2/summary.c
ccan/tdb2/tdb1.h
ccan/tdb2/tdb1_check.c
ccan/tdb2/test/run-tdb1-check.c
ccan/tdb2/test/run-tdb1-corrupt.c
ccan/tdb2/test/run-tdb1-incompatible.c
ccan/tdb2/test/run-tdb1-oldhash.c
ccan/tdb2/test/run-tdb1-readonly-check.c
ccan/tdb2/test/run-tdb1-wronghash-fail.c
ccan/tdb2/test/tdb1-external-agent.c

index 6d846f414264404255eac5f67d0c93ea824b219b..b3874c6d27b1b48256b748608a29d0e0b184a6e1 100644 (file)
@@ -782,6 +782,12 @@ enum TDB_ERROR tdb_check_(struct tdb_context *tdb,
        uint64_t features;
        enum TDB_ERROR ecode;
 
+       if (tdb->flags & TDB_VERSION1) {
+               if (tdb1_check(tdb, check, data) == -1)
+                       return tdb->last_error;
+               return TDB_SUCCESS;
+       }
+
        ecode = tdb_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_WAIT, false);
        if (ecode != TDB_SUCCESS) {
                return tdb->last_error = ecode;
index 96f23e92c364bb2cb91a699290d48fcf406ffe2e..f92d91fdce4b7e24563d7eba64cc6210e60578b9 100644 (file)
@@ -631,6 +631,12 @@ struct tdb_context {
 
 #define TDB1_BYTEREV(x) (((((x)&0xff)<<24)|((x)&0xFF00)<<8)|(((x)>>8)&0xFF00)|((x)>>24))
 
+/* tdb1_check.c: */
+int tdb1_check(struct tdb_context *tdb,
+              enum TDB_ERROR (*check)(TDB_DATA key, TDB_DATA data, void *),
+              void *private_data);
+
+
 /* tdb1_open.c: */
 int tdb1_new_database(struct tdb_context *tdb,
                      struct tdb_attribute_tdb1_hashsize *hashsize);
@@ -658,6 +664,9 @@ int tdb1_traverse(struct tdb_context *tdb,
                  int (*)(struct tdb_context *, TDB_DATA, TDB_DATA, void *),
                  void *private_data);
 
+/* tdb1_summary.c: */
+char *tdb1_summary(struct tdb_context *tdb);
+
 /* tdb1_tdb.c: */
 int tdb1_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, int flag);
 enum TDB_ERROR tdb1_fetch(struct tdb_context *tdb, TDB_DATA key,
index 26cdd3e4fe2cbe7d171b0a190e11dc0148f0991a..3960a20dba6521119ad3214fbd840ad6fa9b32f7 100644 (file)
@@ -161,6 +161,14 @@ enum TDB_ERROR tdb_summary(struct tdb_context *tdb,
        char *hashesg, *freeg, *keysg, *datag, *extrag, *uncoalg;
        enum TDB_ERROR ecode;
 
+       if (tdb->flags & TDB_VERSION1) {
+               /* tdb1 doesn't do graphs. */
+               *summary = tdb1_summary(tdb);
+               if (!*summary)
+                       return tdb->last_error;
+               return TDB_SUCCESS;
+       }
+
        hashesg = freeg = keysg = datag = extrag = uncoalg = NULL;
 
        ecode = tdb_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_WAIT, false);
index b565b1140889231fa5619f1547e4fc6afe1e3f02..2855d0c1b033828acdb50b546f04ebf3f66ff898 100644 (file)
@@ -48,19 +48,12 @@ void tdb1_increment_seqnum_nonblock(struct tdb_context *tdb);
 
 uint64_t tdb1_incompatible_hash(const void *key, size_t len, uint64_t seed, void *);
 
-int tdb1_check(struct tdb_context *tdb,
-             int (*check) (TDB_DATA key, TDB_DATA data, void *private_data),
-             void *private_data);
-
 /* @} ******************************************************************/
 
 /* wipe and repack */
 int tdb1_wipe_all(struct tdb_context *tdb);
 int tdb1_repack(struct tdb_context *tdb);
 
-/* Debug functions. Not used in production. */
-char *tdb1_summary(struct tdb_context *tdb);
-
 extern TDB_DATA tdb1_null;
 
 #endif /* tdb1.h */
index c3c3c60f431cf494d78fcd5c28174b10345e9dc2..4d71712c473fb465021ca0cd778dc3b9bbdad418 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;
index d2360f02fdc276c61531f9dae2aa264ce6b2bb0e..e939d04036fefeb1461adb3faf73024ac9347565 100644 (file)
@@ -19,7 +19,7 @@ int main(int argc, char *argv[])
                       O_CREAT|O_TRUNC|O_RDWR, 0600, &hsize);
 
        ok1(tdb);
-       ok1(tdb1_check(tdb, NULL, NULL) == 0);
+       ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
 
        key.dsize = strlen("hi");
        key.dptr = (void *)"hi";
@@ -27,18 +27,18 @@ int main(int argc, char *argv[])
        data.dptr = (void *)"world";
 
        ok1(tdb_store(tdb, key, data, TDB_INSERT) == TDB_SUCCESS);
-       ok1(tdb1_check(tdb, NULL, NULL) == 0);
+       ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
        tdb_close(tdb);
 
        tdb = tdb_open("run-check.tdb1", TDB_VERSION1, O_RDWR, 0, &tap_log_attr);
        ok1(tdb);
-       ok1(tdb1_check(tdb, NULL, NULL) == 0);
+       ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
        tdb_close(tdb);
 
        tdb = tdb_open("test/tdb1.corrupt", TDB_VERSION1, O_RDWR, 0,
                        &tap_log_attr);
        ok1(tdb);
-       ok1(tdb1_check(tdb, NULL, NULL) == -1);
+       ok1(tdb_check(tdb, NULL, NULL) == TDB_ERR_CORRUPT);
        ok1(tdb_error(tdb) == TDB_ERR_CORRUPT);
        tdb_close(tdb);
 
@@ -46,13 +46,13 @@ int main(int argc, char *argv[])
        tdb = tdb_open("test/old-nohash-le.tdb1", TDB_VERSION1, O_RDWR, 0,
                       &tap_log_attr);
        ok1(tdb);
-       ok1(tdb1_check(tdb, NULL, NULL) == 0);
+       ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
        tdb_close(tdb);
 
        tdb = tdb_open("test/old-nohash-be.tdb1", TDB_VERSION1, O_RDWR, 0,
                        &tap_log_attr);
        ok1(tdb);
-       ok1(tdb1_check(tdb, NULL, NULL) == 0);
+       ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
        tdb_close(tdb);
 
        return exit_status();
index 373f079512f5689a9b7c420d079b83587ce6268f..35bc4c3f229554938790ea8bb5a14b1d5e160e11 100644 (file)
@@ -45,7 +45,7 @@ static void check_test(struct tdb_context *tdb)
        TDB_DATA key, data;
        unsigned int i, verifiable, corrupt, sizes[2], dsize, ksize;
 
-       ok1(tdb1_check(tdb, NULL, NULL) == 0);
+       ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
 
        key.dptr = (void *)"hello";
        data.dsize = strlen("world");
@@ -81,7 +81,7 @@ static void check_test(struct tdb_context *tdb)
        for (i = 0, corrupt = 0; i < tdb->file->map_size * CHAR_BIT; i++) {
                tdb1_flip_bit(tdb, i);
                memset(sizes, 0, sizeof(sizes));
-               if (tdb1_check(tdb, check, sizes) != 0)
+               if (tdb_check(tdb, check, sizes) == TDB_ERR_CORRUPT)
                        corrupt++;
                else if (sizes[0] != ksize || sizes[1] != dsize)
                        corrupt++;
index e6b2994e203ed57e0f45d3e95d32a8a9cc97ddf0..46ab5669207b4e02c75fd88fd4afce25e11f04b7 100644 (file)
@@ -105,7 +105,7 @@ int main(int argc, char *argv[])
                ok1(tdb_fetch(tdb, d, &d) == TDB_SUCCESS);
                ok1(d.dsize == 5);
                free(d.dptr);
-               ok1(tdb1_check(tdb, NULL, NULL) == 0);
+               ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
                tdb_close(tdb);
 
                log_count = 0;
@@ -113,7 +113,7 @@ int main(int argc, char *argv[])
                               TDB_VERSION1, O_RDONLY, 0, &jhash_attr);
                ok1(tdb);
                ok1(log_count == 0);
-               ok1(tdb1_check(tdb, NULL, NULL) == 0);
+               ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
                tdb_close(tdb);
 
                log_count = 0;
@@ -121,7 +121,7 @@ int main(int argc, char *argv[])
                               TDB_VERSION1, O_RDONLY, 0, &jhash_attr);
                ok1(tdb);
                ok1(log_count == 0);
-               ok1(tdb1_check(tdb, NULL, NULL) == 0);
+               ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
                tdb_close(tdb);
 
                /* OK, now create with incompatible hash. */
@@ -156,7 +156,7 @@ int main(int argc, char *argv[])
                ok1(tdb_fetch(tdb, d, &d) == TDB_SUCCESS);
                ok1(d.dsize == 5);
                free(d.dptr);
-               ok1(tdb1_check(tdb, NULL, NULL) == 0);
+               ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
                tdb_close(tdb);
 
                /* Can open by letting it figure it out itself. */
@@ -170,7 +170,7 @@ int main(int argc, char *argv[])
                ok1(tdb_fetch(tdb, d, &d) == TDB_SUCCESS);
                ok1(d.dsize == 5);
                free(d.dptr);
-               ok1(tdb1_check(tdb, NULL, NULL) == 0);
+               ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
                tdb_close(tdb);
 
                /* FIXME: Not possible with TDB2 :( */
@@ -205,7 +205,7 @@ int main(int argc, char *argv[])
                ok1(tdb_fetch(tdb, d, &d) == TDB_SUCCESS);
                ok1(d.dsize == 5);
                free(d.dptr);
-               ok1(tdb1_check(tdb, NULL, NULL) == 0);
+               ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
                tdb_close(tdb);
        }
 
index 185001db74b41876d1782a907aa04ca50c153450..f9cffa25a67e9aa41948d3e82789efa83bd5b00c 100644 (file)
@@ -20,25 +20,25 @@ int main(int argc, char *argv[])
        tdb = tdb_open("test/old-nohash-le.tdb1", TDB_VERSION1, O_RDWR, 0,
                       &tap_log_attr);
        ok1(tdb);
-       ok1(tdb1_check(tdb, NULL, NULL) == 0);
+       ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
        tdb_close(tdb);
 
        tdb = tdb_open("test/old-nohash-be.tdb1", TDB_VERSION1, O_RDWR, 0,
                       &tap_log_attr);
        ok1(tdb);
-       ok1(tdb1_check(tdb, NULL, NULL) == 0);
+       ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
        tdb_close(tdb);
 
        tdb = tdb_open("test/old-nohash-le.tdb1", TDB_VERSION1, O_RDWR, 0,
                       &incompat_hash_attr);
        ok1(tdb);
-       ok1(tdb1_check(tdb, NULL, NULL) == 0);
+       ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
        tdb_close(tdb);
 
        tdb = tdb_open("test/old-nohash-be.tdb1", TDB_VERSION1, O_RDWR, 0,
                       &incompat_hash_attr);
        ok1(tdb);
-       ok1(tdb1_check(tdb, NULL, NULL) == 0);
+       ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
        tdb_close(tdb);
 
        return exit_status();
index 471f813da24159052969a64cd88aa68e3606b6c2..f42a8f5e27464d2870fc0d385e38c66e9399d470 100644 (file)
@@ -1,5 +1,5 @@
-/* We should be able to tdb1_check a O_RDONLY tdb, and we were previously allowed
- * to tdb1_check() inside a transaction (though that's paranoia!). */
+/* We should be able to tdb_check a O_RDONLY tdb, and we were previously allowed
+ * to tdb_check() inside a transaction (though that's paranoia!). */
 #include "tdb2-source.h"
 #include <ccan/tap/tap.h>
 #include <stdlib.h>
@@ -28,11 +28,11 @@ int main(int argc, char *argv[])
        data.dptr = (void *)"world";
 
        ok1(tdb_store(tdb, key, data, TDB_INSERT) == TDB_SUCCESS);
-       ok1(tdb1_check(tdb, NULL, NULL) == 0);
+       ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
 
        /* We are also allowed to do a check inside a transaction. */
        ok1(tdb_transaction_start(tdb) == TDB_SUCCESS);
-       ok1(tdb1_check(tdb, NULL, NULL) == 0);
+       ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
        ok1(tdb_close(tdb) == 0);
 
        tdb = tdb_open("run-readonly-check.tdb1",
@@ -40,7 +40,7 @@ int main(int argc, char *argv[])
 
        ok1(tdb);
        ok1(tdb_store(tdb, key, data, TDB_MODIFY) == TDB_ERR_RDONLY);
-       ok1(tdb1_check(tdb, NULL, NULL) == 0);
+       ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
        ok1(tdb_close(tdb) == 0);
 
        return exit_status();
index c7e789f2ca7a02f6dec0a87fdc3632fc315ba84d..63c1bdf1e46b637ded5ee05bb2fc5742d01ea79f 100644 (file)
@@ -102,7 +102,7 @@ int main(int argc, char *argv[])
                       0, &incompat_hash_attr);
        ok1(tdb);
        ok1(log_count == 0);
-       ok1(tdb1_check(tdb, NULL, NULL) == 0);
+       ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
        tdb_close(tdb);
 
        log_count = 0;
@@ -110,7 +110,7 @@ int main(int argc, char *argv[])
                       0, &incompat_hash_attr);
        ok1(tdb);
        ok1(log_count == 0);
-       ok1(tdb1_check(tdb, NULL, NULL) == 0);
+       ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
        tdb_close(tdb);
 
        /* It should open with jenkins hash if we don't specify. */
@@ -119,7 +119,7 @@ int main(int argc, char *argv[])
                       &log_attr);
        ok1(tdb);
        ok1(log_count == 0);
-       ok1(tdb1_check(tdb, NULL, NULL) == 0);
+       ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
        tdb_close(tdb);
 
        log_count = 0;
@@ -127,7 +127,7 @@ int main(int argc, char *argv[])
                       &log_attr);
        ok1(tdb);
        ok1(log_count == 0);
-       ok1(tdb1_check(tdb, NULL, NULL) == 0);
+       ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
        tdb_close(tdb);
 
        log_count = 0;
@@ -135,7 +135,7 @@ int main(int argc, char *argv[])
                       0, &log_attr);
        ok1(tdb);
        ok1(log_count == 0);
-       ok1(tdb1_check(tdb, NULL, NULL) == 0);
+       ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
        tdb_close(tdb);
 
 
index 6b6f87dea5ea64bce3f1e4d83b7dc75365d5a873..150150754a71dd9eb91c372be53926101af2fa8f 100644 (file)
@@ -74,7 +74,7 @@ static enum agent_return do_operation(enum operation op, const char *name)
                ret = tdb_transaction_commit(tdb) == TDB_SUCCESS ? SUCCESS : OTHER_FAILURE;
                break;
        case CHECK:
-               ret = tdb1_check(tdb, NULL, NULL) == 0 ? SUCCESS : OTHER_FAILURE;
+               ret = tdb_check(tdb, NULL, NULL) == TDB_SUCCESS ? SUCCESS : OTHER_FAILURE;
                break;
        case NEEDS_RECOVERY:
                ret = tdb1_needs_recovery(tdb) ? SUCCESS : FAILED;