change default configurations on systems with UNIX domain sockets to NOT specify...
[oweals/gnunet.git] / src / util / container_bloomfilter.c
index ebd05612836d954b65e6718d97bbfa7601919efe..84aab6b17865dd9484911136bec62ec725738c8a 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2001, 2002, 2003, 2004, 2006, 2008 Christian Grothoff (and other contributing authors)
+     (C) 2001, 2002, 2003, 2004, 2006, 2008, 2011 Christian Grothoff (and other contributing authors)
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
 #include "gnunet_container_lib.h"
 #include "gnunet_disk_lib.h"
 
+#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
+
+#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
+
+#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
+
 struct GNUNET_CONTAINER_BloomFilter
 {
 
@@ -177,7 +183,7 @@ static void
 incrementBit (char *bitArray, unsigned int bitIdx,
               const struct GNUNET_DISK_FileHandle *fh)
 {
-  off_t fileSlot;
+  OFF_T fileSlot;
   unsigned char value;
   unsigned int high;
   unsigned int low;
@@ -225,7 +231,7 @@ static void
 decrementBit (char *bitArray, unsigned int bitIdx,
               const struct GNUNET_DISK_FileHandle *fh)
 {
-  off_t fileSlot;
+  OFF_T fileSlot;
   unsigned char value;
   unsigned int high;
   unsigned int low;
@@ -276,33 +282,33 @@ decrementBit (char *bitArray, unsigned int bitIdx,
  * @return GNUNET_OK if created ok, GNUNET_SYSERR otherwise
  */
 static int
-makeEmptyFile (const struct GNUNET_DISK_FileHandle *fh, size_t size)
+make_empty_file (const struct GNUNET_DISK_FileHandle *fh, size_t size)
 {
-  char *buffer;
+  char buffer[BUFFSIZE];
   size_t bytesleft = size;
   int res = 0;
 
   if (GNUNET_DISK_handle_invalid (fh))
     return GNUNET_SYSERR;
-  buffer = GNUNET_malloc (BUFFSIZE);
-  memset (buffer, 0, BUFFSIZE);
+  memset (buffer, 0, sizeof (buffer));
   GNUNET_DISK_file_seek (fh, 0, GNUNET_DISK_SEEK_SET);
-
   while (bytesleft > 0)
   {
-    if (bytesleft > BUFFSIZE)
+    if (bytesleft > sizeof (buffer))
     {
-      res = GNUNET_DISK_file_write (fh, buffer, BUFFSIZE);
-      bytesleft -= BUFFSIZE;
+      res = GNUNET_DISK_file_write (fh, buffer, sizeof (buffer));
+      if (res >= 0)
+       bytesleft -= res;
     }
     else
     {
       res = GNUNET_DISK_file_write (fh, buffer, bytesleft);
-      bytesleft = 0;
+      if (res >= 0)
+       bytesleft -= res;
     }
-    GNUNET_assert (res != GNUNET_SYSERR);
+    if (GNUNET_SYSERR == res)
+      return GNUNET_SYSERR;
   }
-  GNUNET_free (buffer);
   return GNUNET_OK;
 }
 
@@ -316,10 +322,12 @@ makeEmptyFile (const struct GNUNET_DISK_FileHandle *fh, size_t size)
  * @param cls closure
  * @param bf the filter to manipulate
  * @param bit the current bit
+ * @return GNUNET_YES to continue, GNUNET_NO to stop early
  */
-typedef void (*BitIterator) (void *cls,
-                             const struct GNUNET_CONTAINER_BloomFilter * bf,
-                             unsigned int bit);
+typedef int (*BitIterator) (void *cls,
+                            const struct GNUNET_CONTAINER_BloomFilter * bf,
+                            unsigned int bit);
+
 
 /**
  * Call an iterator for each bit that the bloomfilter
@@ -336,19 +344,21 @@ iterateBits (const struct GNUNET_CONTAINER_BloomFilter *bf,
 {
   GNUNET_HashCode tmp[2];
   int bitCount;
-  int round;
+  unsigned int round;
   unsigned int slot = 0;
 
   bitCount = bf->addressesPerElement;
-  memcpy (&tmp[0], key, sizeof (GNUNET_HashCode));
+  tmp[0] = *key;
   round = 0;
   while (bitCount > 0)
   {
     while (slot < (sizeof (GNUNET_HashCode) / sizeof (uint32_t)))
     {
-      callback (arg, bf,
-                (((uint32_t *) & tmp[round & 1])[slot]) &
-                ((bf->bitArraySize * 8) - 1));
+      if (GNUNET_YES !=
+          callback (arg, bf,
+                    (((uint32_t *) & tmp[round & 1])[slot]) &
+                    ((bf->bitArraySize * 8) - 1)))
+        return;
       slot++;
       bitCount--;
       if (bitCount == 0)
@@ -364,53 +374,65 @@ iterateBits (const struct GNUNET_CONTAINER_BloomFilter *bf,
   }
 }
 
+
 /**
  * Callback: increment bit
  *
  * @param cls pointer to writeable form of bf
  * @param bf the filter to manipulate
  * @param bit the bit to increment
+ * @return GNUNET_YES
  */
-static void
+static int
 incrementBitCallback (void *cls, const struct GNUNET_CONTAINER_BloomFilter *bf,
                       unsigned int bit)
 {
   struct GNUNET_CONTAINER_BloomFilter *b = cls;
 
   incrementBit (b->bitArray, bit, bf->fh);
+  return GNUNET_YES;
 }
 
+
 /**
  * Callback: decrement bit
  *
  * @param cls pointer to writeable form of bf
  * @param bf the filter to manipulate
  * @param bit the bit to decrement
+ * @return GNUNET_YES
  */
-static void
+static int
 decrementBitCallback (void *cls, const struct GNUNET_CONTAINER_BloomFilter *bf,
                       unsigned int bit)
 {
   struct GNUNET_CONTAINER_BloomFilter *b = cls;
 
   decrementBit (b->bitArray, bit, bf->fh);
+  return GNUNET_YES;
 }
 
+
 /**
  * Callback: test if all bits are set
  *
  * @param cls pointer set to GNUNET_NO if bit is not set
  * @param bf the filter
  * @param bit the bit to test
+ * @return YES if the bit is set, NO if not
  */
-static void
+static int
 testBitCallback (void *cls, const struct GNUNET_CONTAINER_BloomFilter *bf,
                  unsigned int bit)
 {
   int *arg = cls;
 
   if (GNUNET_NO == testBit (bf->bitArray, bit))
+  {
     *arg = GNUNET_NO;
+    return GNUNET_NO;
+  }
+  return GNUNET_YES;
 }
 
 /* *********************** INTERFACE **************** */
@@ -431,9 +453,11 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size,
 {
   struct GNUNET_CONTAINER_BloomFilter *bf;
   char *rbuff;
-  off_t pos;
+  OFF_T pos;
   int i;
   size_t ui;
+  OFF_T fsize;
+  int must_read;
 
   GNUNET_assert (NULL != filename);
   if ((k == 0) || (size == 0))
@@ -441,22 +465,76 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size,
   if (size < BUFFSIZE)
     size = BUFFSIZE;
   ui = 1;
-  while (ui < size)
+  while ( (ui < size) &&
+         (ui * 2 > ui) )
     ui *= 2;
   size = ui;                    /* make sure it's a power of 2 */
 
   bf = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_BloomFilter));
   /* Try to open a bloomfilter file */
-  bf->fh =
+  if (GNUNET_YES == GNUNET_DISK_file_test (filename))
+    bf->fh =
       GNUNET_DISK_file_open (filename,
-                             GNUNET_DISK_OPEN_READWRITE |
-                             GNUNET_DISK_OPEN_CREATE,
+                             GNUNET_DISK_OPEN_READWRITE,
                              GNUNET_DISK_PERM_USER_READ |
                              GNUNET_DISK_PERM_USER_WRITE);
-  if (NULL == bf->fh)
+  if (NULL != bf->fh)
   {
-    GNUNET_free (bf);
-    return NULL;
+    /* file existed, try to read it! */
+    must_read = GNUNET_YES;
+    if (GNUNET_OK !=
+       GNUNET_DISK_file_handle_size (bf->fh, &fsize))
+    {
+      GNUNET_DISK_file_close (bf->fh);
+      GNUNET_free (bf);
+      return NULL;
+    }
+    if (fsize == 0)
+    {
+      /* found existing empty file, just overwrite */
+      if (GNUNET_OK != make_empty_file (bf->fh, size * 4LL))
+      {
+       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
+                            "write");
+       GNUNET_DISK_file_close (bf->fh);
+       GNUNET_free (bf);
+       return NULL;
+      }
+    }
+    else if (fsize != size * 4LL)
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                 _("Size of file on disk is incorrect for this Bloom filter (want %llu, have %llu)\n"),
+                 (unsigned long long) (size * 4LL),
+                 (unsigned long long) fsize);
+      GNUNET_DISK_file_close (bf->fh);
+      GNUNET_free (bf);
+      return NULL;
+    }
+  }
+  else
+  {
+    /* file did not exist, don't read, just create */
+    must_read = GNUNET_NO;
+    bf->fh =
+      GNUNET_DISK_file_open (filename,
+                             GNUNET_DISK_OPEN_CREATE |
+                             GNUNET_DISK_OPEN_READWRITE,
+                             GNUNET_DISK_PERM_USER_READ |
+                             GNUNET_DISK_PERM_USER_WRITE);
+    if (NULL == bf->fh)
+      {
+       GNUNET_free (bf);
+       return NULL;
+      }
+    if (GNUNET_OK != make_empty_file (bf->fh, size * 4LL))
+    {
+      GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
+                          "write");
+      GNUNET_DISK_file_close (bf->fh);
+      GNUNET_free (bf);
+      return NULL;
+    }
   }
   bf->filename = GNUNET_strdup (filename);
   /* Alloc block */
@@ -473,18 +551,24 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size,
   bf->addressesPerElement = k;
   memset (bf->bitArray, 0, bf->bitArraySize);
 
+  if (GNUNET_YES != must_read)      
+    return bf; /* already done! */  
   /* Read from the file what bits we can */
   rbuff = GNUNET_malloc (BUFFSIZE);
   pos = 0;
-  while (pos < size * 8)
+  while (pos < size * 8LL)
   {
     int res;
 
     res = GNUNET_DISK_file_read (bf->fh, rbuff, BUFFSIZE);
     if (res == -1)
     {
-      GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "read",
-                                bf->filename);
+      LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "read", bf->filename);
+      GNUNET_free (rbuff);
+      GNUNET_free (bf->filename);
+      GNUNET_DISK_file_close (bf->fh);
+      GNUNET_free (bf);
+      return NULL;
     }
     if (res == 0)
       break;                    /* is ok! we just did not use that many bits yet */
@@ -575,6 +659,7 @@ GNUNET_CONTAINER_bloomfilter_get_raw_data (const struct
   return GNUNET_OK;
 }
 
+
 /**
  * Free the space associated with a filter
  * in memory, flush to drive if needed (do not
@@ -594,6 +679,7 @@ GNUNET_CONTAINER_bloomfilter_free (struct GNUNET_CONTAINER_BloomFilter *bf)
   GNUNET_free (bf);
 }
 
+
 /**
  * Reset a bloom filter to empty. Clears the file on disk.
  *
@@ -607,7 +693,7 @@ GNUNET_CONTAINER_bloomfilter_clear (struct GNUNET_CONTAINER_BloomFilter *bf)
 
   memset (bf->bitArray, 0, bf->bitArraySize);
   if (bf->filename != NULL)
-    makeEmptyFile (bf->fh, bf->bitArraySize * 4);
+    make_empty_file (bf->fh, bf->bitArraySize * 4LL);
 }
 
 
@@ -631,6 +717,7 @@ GNUNET_CONTAINER_bloomfilter_test (const struct GNUNET_CONTAINER_BloomFilter
   return res;
 }
 
+
 /**
  * Add an element to the filter
  *
@@ -641,7 +728,6 @@ void
 GNUNET_CONTAINER_bloomfilter_add (struct GNUNET_CONTAINER_BloomFilter *bf,
                                   const GNUNET_HashCode * e)
 {
-
   if (NULL == bf)
     return;
   iterateBits (bf, &incrementBitCallback, bf, e);
@@ -764,7 +850,7 @@ GNUNET_CONTAINER_bloomfilter_resize (struct GNUNET_CONTAINER_BloomFilter *bf,
   bf->bitArray = GNUNET_malloc (size);
   memset (bf->bitArray, 0, bf->bitArraySize);
   if (bf->filename != NULL)
-    makeEmptyFile (bf->fh, bf->bitArraySize * 4);
+    make_empty_file (bf->fh, bf->bitArraySize * 4LL);
   while (GNUNET_YES == iterator (iterator_cls, &hc))
     GNUNET_CONTAINER_bloomfilter_add (bf, &hc);
 }