diff options
author | Christian Grothoff <christian@grothoff.org> | 2011-12-16 21:19:27 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2011-12-16 21:19:27 +0000 |
commit | 7eb418d4f9351657c3722374bd6660f077926eae (patch) | |
tree | a96e832e17379d84a9237094a07432648cd5ae42 /src/util/container_bloomfilter.c | |
parent | e0b0882dd6b10d362f2aba6f37946f13319e376d (diff) | |
download | gnunet-7eb418d4f9351657c3722374bd6660f077926eae.tar.gz gnunet-7eb418d4f9351657c3722374bd6660f077926eae.zip |
cleaning up bloomfilter code and disk file size tests
Diffstat (limited to 'src/util/container_bloomfilter.c')
-rw-r--r-- | src/util/container_bloomfilter.c | 47 |
1 files changed, 29 insertions, 18 deletions
diff --git a/src/util/container_bloomfilter.c b/src/util/container_bloomfilter.c index 788f148fa..40cdfd06e 100644 --- a/src/util/container_bloomfilter.c +++ b/src/util/container_bloomfilter.c | |||
@@ -282,33 +282,33 @@ decrementBit (char *bitArray, unsigned int bitIdx, | |||
282 | * @return GNUNET_OK if created ok, GNUNET_SYSERR otherwise | 282 | * @return GNUNET_OK if created ok, GNUNET_SYSERR otherwise |
283 | */ | 283 | */ |
284 | static int | 284 | static int |
285 | makeEmptyFile (const struct GNUNET_DISK_FileHandle *fh, size_t size) | 285 | make_empty_file (const struct GNUNET_DISK_FileHandle *fh, size_t size) |
286 | { | 286 | { |
287 | char *buffer; | 287 | char buffer[BUFFSIZE]; |
288 | size_t bytesleft = size; | 288 | size_t bytesleft = size; |
289 | int res = 0; | 289 | int res = 0; |
290 | 290 | ||
291 | if (GNUNET_DISK_handle_invalid (fh)) | 291 | if (GNUNET_DISK_handle_invalid (fh)) |
292 | return GNUNET_SYSERR; | 292 | return GNUNET_SYSERR; |
293 | buffer = GNUNET_malloc (BUFFSIZE); | 293 | memset (buffer, 0, sizeof (buffer)); |
294 | memset (buffer, 0, BUFFSIZE); | ||
295 | GNUNET_DISK_file_seek (fh, 0, GNUNET_DISK_SEEK_SET); | 294 | GNUNET_DISK_file_seek (fh, 0, GNUNET_DISK_SEEK_SET); |
296 | |||
297 | while (bytesleft > 0) | 295 | while (bytesleft > 0) |
298 | { | 296 | { |
299 | if (bytesleft > BUFFSIZE) | 297 | if (bytesleft > sizeof (buffer)) |
300 | { | 298 | { |
301 | res = GNUNET_DISK_file_write (fh, buffer, BUFFSIZE); | 299 | res = GNUNET_DISK_file_write (fh, buffer, sizeof (buffer)); |
302 | bytesleft -= BUFFSIZE; | 300 | if (res >= 0) |
301 | bytesleft -= res; | ||
303 | } | 302 | } |
304 | else | 303 | else |
305 | { | 304 | { |
306 | res = GNUNET_DISK_file_write (fh, buffer, bytesleft); | 305 | res = GNUNET_DISK_file_write (fh, buffer, bytesleft); |
307 | bytesleft = 0; | 306 | if (res >= 0) |
307 | bytesleft -= res; | ||
308 | } | 308 | } |
309 | GNUNET_assert (res != GNUNET_SYSERR); | 309 | if (GNUNET_SYSERR == res) |
310 | return GNUNET_SYSERR; | ||
310 | } | 311 | } |
311 | GNUNET_free (buffer); | ||
312 | return GNUNET_OK; | 312 | return GNUNET_OK; |
313 | } | 313 | } |
314 | 314 | ||
@@ -472,7 +472,8 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size, | |||
472 | 472 | ||
473 | bf = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_BloomFilter)); | 473 | bf = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_BloomFilter)); |
474 | /* Try to open a bloomfilter file */ | 474 | /* Try to open a bloomfilter file */ |
475 | bf->fh = | 475 | if (GNUNET_YES == GNUNET_DISK_file_test (filename)) |
476 | bf->fh = | ||
476 | GNUNET_DISK_file_open (filename, | 477 | GNUNET_DISK_file_open (filename, |
477 | GNUNET_DISK_OPEN_READWRITE, | 478 | GNUNET_DISK_OPEN_READWRITE, |
478 | GNUNET_DISK_PERM_USER_READ | | 479 | GNUNET_DISK_PERM_USER_READ | |
@@ -492,6 +493,14 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size, | |||
492 | GNUNET_free (bf); | 493 | GNUNET_free (bf); |
493 | return NULL; | 494 | return NULL; |
494 | } | 495 | } |
496 | if (GNUNET_OK != make_empty_file (bf->fh, size * 4LL)) | ||
497 | { | ||
498 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, | ||
499 | "write"); | ||
500 | GNUNET_DISK_file_close (bf->fh); | ||
501 | GNUNET_free (bf); | ||
502 | return NULL; | ||
503 | } | ||
495 | } | 504 | } |
496 | else | 505 | else |
497 | { | 506 | { |
@@ -504,10 +513,12 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size, | |||
504 | GNUNET_free (bf); | 513 | GNUNET_free (bf); |
505 | return NULL; | 514 | return NULL; |
506 | } | 515 | } |
507 | if (fsize != size * 8LL) | 516 | if (fsize != size * 4LL) |
508 | { | 517 | { |
509 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 518 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
510 | _("Size of file on disk is incorrect for this Bloom filter\n")); | 519 | _("Size of file on disk is incorrect for this Bloom filter (want %llu, have %llu)\n"), |
520 | (unsigned long long) fsize, | ||
521 | (unsigned long long) (size * 4LL)); | ||
511 | GNUNET_DISK_file_close (bf->fh); | 522 | GNUNET_DISK_file_close (bf->fh); |
512 | GNUNET_free (bf); | 523 | GNUNET_free (bf); |
513 | return NULL; | 524 | return NULL; |
@@ -528,8 +539,8 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size, | |||
528 | bf->addressesPerElement = k; | 539 | bf->addressesPerElement = k; |
529 | memset (bf->bitArray, 0, bf->bitArraySize); | 540 | memset (bf->bitArray, 0, bf->bitArraySize); |
530 | 541 | ||
531 | if (GNUNET_YES != must_read) | 542 | if (GNUNET_YES != must_read) |
532 | return bf; /* already done! */ | 543 | return bf; /* already done! */ |
533 | /* Read from the file what bits we can */ | 544 | /* Read from the file what bits we can */ |
534 | rbuff = GNUNET_malloc (BUFFSIZE); | 545 | rbuff = GNUNET_malloc (BUFFSIZE); |
535 | pos = 0; | 546 | pos = 0; |
@@ -670,7 +681,7 @@ GNUNET_CONTAINER_bloomfilter_clear (struct GNUNET_CONTAINER_BloomFilter *bf) | |||
670 | 681 | ||
671 | memset (bf->bitArray, 0, bf->bitArraySize); | 682 | memset (bf->bitArray, 0, bf->bitArraySize); |
672 | if (bf->filename != NULL) | 683 | if (bf->filename != NULL) |
673 | makeEmptyFile (bf->fh, bf->bitArraySize * 4); | 684 | make_empty_file (bf->fh, bf->bitArraySize * 4LL); |
674 | } | 685 | } |
675 | 686 | ||
676 | 687 | ||
@@ -827,7 +838,7 @@ GNUNET_CONTAINER_bloomfilter_resize (struct GNUNET_CONTAINER_BloomFilter *bf, | |||
827 | bf->bitArray = GNUNET_malloc (size); | 838 | bf->bitArray = GNUNET_malloc (size); |
828 | memset (bf->bitArray, 0, bf->bitArraySize); | 839 | memset (bf->bitArray, 0, bf->bitArraySize); |
829 | if (bf->filename != NULL) | 840 | if (bf->filename != NULL) |
830 | makeEmptyFile (bf->fh, bf->bitArraySize * 4); | 841 | make_empty_file (bf->fh, bf->bitArraySize * 4LL); |
831 | while (GNUNET_YES == iterator (iterator_cls, &hc)) | 842 | while (GNUNET_YES == iterator (iterator_cls, &hc)) |
832 | GNUNET_CONTAINER_bloomfilter_add (bf, &hc); | 843 | GNUNET_CONTAINER_bloomfilter_add (bf, &hc); |
833 | } | 844 | } |