change default configurations on systems with UNIX domain sockets to NOT specify...
[oweals/gnunet.git] / src / util / container_bloomfilter.c
index 40cdfd06ecff70a817b787c5d236fb103677dc3a..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
@@ -183,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;
@@ -231,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;
@@ -453,10 +453,10 @@ 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;
+  OFF_T fsize;
   int must_read;
 
   GNUNET_assert (NULL != filename);
@@ -478,9 +478,43 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size,
                              GNUNET_DISK_OPEN_READWRITE,
                              GNUNET_DISK_PERM_USER_READ |
                              GNUNET_DISK_PERM_USER_WRITE);
-  if (NULL == bf->fh)
+  if (NULL != bf->fh)
   {
-    /* file did not exist, don't read */
+    /* 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,
@@ -502,28 +536,6 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size,
       return NULL;
     }
   }
-  else
-  {
-    /* 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 != 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) fsize,
-                 (unsigned long long) (size * 4LL));
-      GNUNET_DISK_file_close (bf->fh);
-      GNUNET_free (bf);
-      return NULL;
-    }
-  }
   bf->filename = GNUNET_strdup (filename);
   /* Alloc block */
   bf->bitArray = GNUNET_malloc_large (size);