]> git.ozlabs.org Git - ccan/blobdiff - ccan/tdb2/summary.c
tdb2: rework some io.c functions to encode errors in their pointer returns.
[ccan] / ccan / tdb2 / summary.c
index 56d365883030dce6e85961022c7ac3d7f7e694aa..61d7689749caa6a37b8883439d4ba7c4bcbc524a 100644 (file)
@@ -1,7 +1,7 @@
- /* 
+ /*
    Trivial Database 2: human-readable summary code
    Copyright (C) Rusty Russell 2010
-   
+
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
@@ -26,8 +26,10 @@ static int count_hash(struct tdb_context *tdb,
        unsigned int i, count = 0;
 
        h = tdb_access_read(tdb, hash_off, sizeof(*h) << bits, true);
-       if (!h)
+       if (TDB_PTR_IS_ERR(h)) {
+               tdb->ecode = TDB_PTR_ERR(h);
                return -1;
+       }
        for (i = 0; i < (1 << bits); i++)
                count += (h[i] != 0);
 
@@ -38,7 +40,7 @@ static int count_hash(struct tdb_context *tdb,
 static bool summarize(struct tdb_context *tdb,
                      struct tally *hashes,
                      struct tally *ftables,
-                     struct tally *free,
+                     struct tally *fr,
                      struct tally *keys,
                      struct tally *data,
                      struct tally *extra,
@@ -58,8 +60,10 @@ static bool summarize(struct tdb_context *tdb,
                } *p;
                /* We might not be able to get the whole thing. */
                p = tdb_access_read(tdb, off, sizeof(p->f), true);
-               if (!p)
+               if (TDB_PTR_IS_ERR(p)) {
+                       tdb->ecode = TDB_PTR_ERR(p);
                        return false;
+               }
                if (p->r.magic == TDB_RECOVERY_INVALID_MAGIC
                    || p->r.magic == TDB_RECOVERY_MAGIC) {
                        if (unc) {
@@ -69,7 +73,7 @@ static bool summarize(struct tdb_context *tdb,
                        len = sizeof(p->r) + p->r.max_len;
                } else if (frec_magic(&p->f) == TDB_FREE_MAGIC) {
                        len = frec_len(&p->f);
-                       tally_add(free, len);
+                       tally_add(fr, len);
                        tally_add(buckets, size_to_bucket(len));
                        len += sizeof(p->u);
                        unc++;
@@ -152,13 +156,19 @@ char *tdb_summary(struct tdb_context *tdb, enum tdb_summary_flags flags)
                *buckets, *chains;
        char *hashesg, *freeg, *keysg, *datag, *extrag, *uncoalg, *bucketsg;
        char *ret = NULL;
+       enum TDB_ERROR ecode;
 
        hashesg = freeg = keysg = datag = extrag = uncoalg = bucketsg = NULL;
 
-       if (tdb_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_WAIT, false) != 0)
+       ecode = tdb_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_WAIT, false);
+       if (ecode != TDB_SUCCESS) {
+               tdb->ecode = ecode;
                return NULL;
+       }
 
-       if (tdb_lock_expand(tdb, F_RDLCK) != 0) {
+       ecode = tdb_lock_expand(tdb, F_RDLCK);
+       if (ecode != TDB_SUCCESS) {
+               tdb->ecode = ecode;
                tdb_allrecord_unlock(tdb, F_RDLCK);
                return NULL;
        }
@@ -175,7 +185,7 @@ char *tdb_summary(struct tdb_context *tdb, enum tdb_summary_flags flags)
        chains = tally_new(HISTO_HEIGHT);
        if (!ftables || !hashes || !freet || !keys || !data || !extra
            || !uncoal || !buckets || !chains) {
-               tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_ERROR,
+               tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
                           "tdb_summary: failed to allocate tally structures");
                goto unlock;
        }