]> git.ozlabs.org Git - ccan/commitdiff
tdb: combine dead_space helper from summary.c and check.c
authorRusty Russell <rusty@rustcorp.com.au>
Thu, 23 Dec 2010 02:13:54 +0000 (12:43 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Thu, 23 Dec 2010 02:13:54 +0000 (12:43 +1030)
Remove code duplication, and allow unit tests (which #include both).

ccan/tdb/check.c
ccan/tdb/summary.c
ccan/tdb/tdb_private.h

index fa003b79aebabd28aed807c4f9ca1d65d3014d09..4fbbb92c5a4ae62902d14b7ccffa697df0ccbde8 100644 (file)
@@ -309,7 +309,7 @@ static bool tdb_check_free_record(struct tdb_context *tdb,
 }
 
 /* Slow, but should be very rare. */
-static size_t dead_space(struct tdb_context *tdb, tdb_off_t off)
+size_t tdb_dead_space(struct tdb_context *tdb, tdb_off_t off)
 {
        size_t len;
 
@@ -407,7 +407,7 @@ int tdb_check(struct tdb_context *tdb,
                                found_recovery = true;
                                break;
                        }
-                       dead = dead_space(tdb, off);
+                       dead = tdb_dead_space(tdb, off);
                        if (dead < sizeof(rec))
                                goto corrupt;
 
index 3404a4144f8e78df40bb3e2c6e05c98463183f8b..cb6dd6ee127bf20e1af36361bb0670930007e91b 100644 (file)
 #define HISTO_WIDTH 70
 #define HISTO_HEIGHT 20
 
-/* Slow, but should be very rare. */
-static size_t dead_space(struct tdb_context *tdb, tdb_off_t off)
-{
-       size_t len;
-
-       for (len = 0; off + len < tdb->map_size; len++) {
-               char c;
-               if (tdb->methods->tdb_read(tdb, off, &c, 1, 0))
-                       return 0;
-               if (c != 0 && c != 0x42)
-                       break;
-       }
-       return len;
-}
-
 static size_t get_hash_length(struct tdb_context *tdb, unsigned int i)
 {
        tdb_off_t rec_ptr;
@@ -126,7 +111,7 @@ char *tdb_summary(struct tdb_context *tdb, enum tdb_summary_flags flags)
                case TDB_RECOVERY_INVALID_MAGIC:
                case 0x42424242:
                        unc++;
-                       rec.rec_len = dead_space(tdb, off) - sizeof(rec);
+                       rec.rec_len = tdb_dead_space(tdb, off) - sizeof(rec);
                        /* Fall through */
                case TDB_DEAD_MAGIC:
                        tally_add(dead, rec.rec_len);
index 7482c84f725bcc4ad79fc312dfee82dbb11ffd74..babf01bd836f20dfbe771b6926e911b52d118260 100644 (file)
@@ -304,4 +304,5 @@ int tdb_rec_free_read(struct tdb_context *tdb, tdb_off_t off,
 void tdb_header_hash(struct tdb_context *tdb,
                     uint32_t *magic1_hash, uint32_t *magic2_hash);
 unsigned int tdb_old_hash(TDB_DATA *key);
+size_t tdb_dead_space(struct tdb_context *tdb, tdb_off_t off);
 #endif