From 29a2a839116cbc042073225e0c0d985f923fcdae Mon Sep 17 00:00:00 2001
From: Fredrik Noring <noring@nocrew.org>
Date: Sat, 12 Dec 1998 02:06:50 +0100
Subject: [PATCH] Fixed consistency bug and enhanced error description.

Rev: lib/modules/Yabu.pmod/module.pmod:1.3
---
 lib/modules/Yabu.pmod/module.pmod | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)

diff --git a/lib/modules/Yabu.pmod/module.pmod b/lib/modules/Yabu.pmod/module.pmod
index b9506642c1..79dd845d02 100644
--- a/lib/modules/Yabu.pmod/module.pmod
+++ b/lib/modules/Yabu.pmod/module.pmod
@@ -1,5 +1,5 @@
 // Yabu by Fredrik Noring
-// $Id: module.pmod,v 1.2 1998/06/10 19:20:59 hubbe Exp $
+// $Id: module.pmod,v 1.3 1998/12/12 01:06:50 noring Exp $
 
 #if constant(thread_create)
 #define THREAD_SAFE
@@ -194,6 +194,17 @@ class Chunk {
     return x;
   }
 
+  /* Perform consistency check. Returns 0 for failure, otherwise success. */
+  int consistency()
+  {
+    multiset k = mkmultiset(indices(keys));
+    foreach(indices(frees), int type)
+      foreach(frees[type], int offset)
+        if(k[encode_key(offset, type)])
+          return 0;
+    return 1;
+  }
+  
   array(string) list_keys()
   {
     return indices(keys);
@@ -219,7 +230,8 @@ class Chunk {
     
     mapping m = decode_chunk(file::read_at(offset, type));
     if(m->size == 0)
-      ERR("Cannot decode free chunk!");
+      ERR(sprintf("Cannot decode free chunk! [consistency status: %s]",
+                  consistency()?"#OK":"#FAILURE"));
     if(attributes)
       return m;
     return m->entry;
@@ -240,13 +252,15 @@ class Chunk {
 
   mapping state(array|void almost_free)
   {
-    mapping m = copy_value(frees);
+    mapping m_frees = copy_value(frees);
+    mapping m_keys = copy_value(keys);
     foreach(almost_free||({}), string key) {
       int offset, type;
       DECODE_KEY(key, offset, type);
-      m[type] = (m[type]||({})) | ({ offset });
+      m_frees[type] = (m_frees[type]||({})) | ({ offset });
+      m_delete(m_keys, key);
     }
-    return copy_value(([ "eof":eof, "keys":keys, "frees":m ]));
+    return copy_value(([ "eof":eof, "keys":m_keys, "frees":m_frees ]));
   }
 
   void purge()
-- 
GitLab