aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2011-12-15 12:44:23 +0000
committerChristian Grothoff <christian@grothoff.org>2011-12-15 12:44:23 +0000
commit04ae9253f7dff8d442e36edec1465f54d3f436a5 (patch)
tree8b80816a56cbf2c6dfe1f9aa5d5b1e0b6fd5bc47 /src
parentaf3bc9fff3dadfbcaa96f73dd8c751bbde8d3165 (diff)
downloadgnunet-04ae9253f7dff8d442e36edec1465f54d3f436a5.tar.gz
gnunet-04ae9253f7dff8d442e36edec1465f54d3f436a5.zip
only try to read bf from disk if we didn't just create the file
Diffstat (limited to 'src')
-rw-r--r--src/util/container_bloomfilter.c52
1 files changed, 35 insertions, 17 deletions
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,
457 int i; 457 int i;
458 size_t ui; 458 size_t ui;
459 off_t fsize; 459 off_t fsize;
460 int must_read;
460 461
461 GNUNET_assert (NULL != filename); 462 GNUNET_assert (NULL != filename);
462 if ((k == 0) || (size == 0)) 463 if ((k == 0) || (size == 0))
@@ -473,29 +474,44 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size,
473 /* Try to open a bloomfilter file */ 474 /* Try to open a bloomfilter file */
474 bf->fh = 475 bf->fh =
475 GNUNET_DISK_file_open (filename, 476 GNUNET_DISK_file_open (filename,
476 GNUNET_DISK_OPEN_READWRITE | 477 GNUNET_DISK_OPEN_READWRITE,
477 GNUNET_DISK_OPEN_CREATE,
478 GNUNET_DISK_PERM_USER_READ | 478 GNUNET_DISK_PERM_USER_READ |
479 GNUNET_DISK_PERM_USER_WRITE); 479 GNUNET_DISK_PERM_USER_WRITE);
480 if (NULL == bf->fh) 480 if (NULL == bf->fh)
481 { 481 {
482 GNUNET_free (bf); 482 /* file did not exist, don't read */
483 return NULL; 483 must_read = GNUNET_NO;
484 } 484 bf->fh =
485 if (GNUNET_OK != 485 GNUNET_DISK_file_open (filename,
486 GNUNET_DISK_file_handle_size (bf->fh, &fsize)) 486 GNUNET_DISK_OPEN_CREATE |
487 { 487 GNUNET_DISK_OPEN_READWRITE,
488 GNUNET_DISK_file_close (bf->fh); 488 GNUNET_DISK_PERM_USER_READ |
489 GNUNET_free (bf); 489 GNUNET_DISK_PERM_USER_WRITE);
490 return NULL; 490 if (NULL == bf->fh)
491 {
492 GNUNET_free (bf);
493 return NULL;
494 }
491 } 495 }
492 if (fsize != size * 8LL) 496 else
493 { 497 {
494 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 498 /* file existed, try to read it! */
495 _("Size of file on disk is incorrect for this Bloom filter\n")); 499 must_read = GNUNET_YES;
496 GNUNET_DISK_file_close (bf->fh); 500 if (GNUNET_OK !=
497 GNUNET_free (bf); 501 GNUNET_DISK_file_handle_size (bf->fh, &fsize))
498 return NULL; 502 {
503 GNUNET_DISK_file_close (bf->fh);
504 GNUNET_free (bf);
505 return NULL;
506 }
507 if (fsize != size * 8LL)
508 {
509 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
510 _("Size of file on disk is incorrect for this Bloom filter\n"));
511 GNUNET_DISK_file_close (bf->fh);
512 GNUNET_free (bf);
513 return NULL;
514 }
499 } 515 }
500 bf->filename = GNUNET_strdup (filename); 516 bf->filename = GNUNET_strdup (filename);
501 /* Alloc block */ 517 /* Alloc block */
@@ -512,6 +528,8 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size,
512 bf->addressesPerElement = k; 528 bf->addressesPerElement = k;
513 memset (bf->bitArray, 0, bf->bitArraySize); 529 memset (bf->bitArray, 0, bf->bitArraySize);
514 530
531 if (GNUNET_YES != must_read)
532 return bf; /* already done! */
515 /* Read from the file what bits we can */ 533 /* Read from the file what bits we can */
516 rbuff = GNUNET_malloc (BUFFSIZE); 534 rbuff = GNUNET_malloc (BUFFSIZE);
517 pos = 0; 535 pos = 0;