From 04ae9253f7dff8d442e36edec1465f54d3f436a5 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Thu, 15 Dec 2011 12:44:23 +0000 Subject: only try to read bf from disk if we didn't just create the file --- src/util/container_bloomfilter.c | 52 +++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/util/container_bloomfilter.c b/src/util/container_bloomfilter.c index 0349c32ac..788f148fa 100644 --- a/src/util/container_bloomfilter.c +++ b/src/util/container_bloomfilter.c @@ -457,6 +457,7 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size, int i; size_t ui; off_t fsize; + int must_read; GNUNET_assert (NULL != filename); if ((k == 0) || (size == 0)) @@ -473,29 +474,44 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size, /* Try to open a bloomfilter file */ 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) { - GNUNET_free (bf); - return NULL; - } - if (GNUNET_OK != - GNUNET_DISK_file_handle_size (bf->fh, &fsize)) - { - GNUNET_DISK_file_close (bf->fh); - GNUNET_free (bf); - return NULL; + /* file did not exist, don't read */ + 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 (fsize != size * 8LL) + else { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Size of file on disk is incorrect for this Bloom filter\n")); - GNUNET_DISK_file_close (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 != size * 8LL) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _("Size of file on disk is incorrect for this Bloom filter\n")); + GNUNET_DISK_file_close (bf->fh); + GNUNET_free (bf); + return NULL; + } } bf->filename = GNUNET_strdup (filename); /* Alloc block */ @@ -512,6 +528,8 @@ 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; -- cgit v1.2.3