aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/include/gnunet_disk_lib.h13
-rw-r--r--src/util/container_bloomfilter.c23
-rw-r--r--src/util/disk.c20
3 files changed, 55 insertions, 1 deletions
diff --git a/src/include/gnunet_disk_lib.h b/src/include/gnunet_disk_lib.h
index 5f6a37840..d0a9dfdb7 100644
--- a/src/include/gnunet_disk_lib.h
+++ b/src/include/gnunet_disk_lib.h
@@ -368,6 +368,19 @@ struct GNUNET_DISK_FileHandle *
368GNUNET_DISK_file_open (const char *fn, enum GNUNET_DISK_OpenFlags flags, 368GNUNET_DISK_file_open (const char *fn, enum GNUNET_DISK_OpenFlags flags,
369 enum GNUNET_DISK_AccessPermissions perm); 369 enum GNUNET_DISK_AccessPermissions perm);
370 370
371
372/**
373 * Get the size of an open file.
374 *
375 * @param fh open file handle
376 * @param size where to write size of the file
377 * @return GNUNET_OK on success, GNUNET_SYSERR on error
378 */
379int
380GNUNET_DISK_file_handle_size (struct GNUNET_DISK_FileHandle *fh,
381 off_t *size);
382
383
371/** 384/**
372 * Creates an interprocess channel 385 * Creates an interprocess channel
373 * @param blocking creates an asynchronous pipe if set to GNUNET_NO 386 * @param blocking creates an asynchronous pipe if set to GNUNET_NO
diff --git a/src/util/container_bloomfilter.c b/src/util/container_bloomfilter.c
index 31e777dc3..0349c32ac 100644
--- a/src/util/container_bloomfilter.c
+++ b/src/util/container_bloomfilter.c
@@ -456,6 +456,7 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size,
456 off_t pos; 456 off_t pos;
457 int i; 457 int i;
458 size_t ui; 458 size_t ui;
459 off_t fsize;
459 460
460 GNUNET_assert (NULL != filename); 461 GNUNET_assert (NULL != filename);
461 if ((k == 0) || (size == 0)) 462 if ((k == 0) || (size == 0))
@@ -481,6 +482,21 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size,
481 GNUNET_free (bf); 482 GNUNET_free (bf);
482 return NULL; 483 return NULL;
483 } 484 }
485 if (GNUNET_OK !=
486 GNUNET_DISK_file_handle_size (bf->fh, &fsize))
487 {
488 GNUNET_DISK_file_close (bf->fh);
489 GNUNET_free (bf);
490 return NULL;
491 }
492 if (fsize != size * 8LL)
493 {
494 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
495 _("Size of file on disk is incorrect for this Bloom filter\n"));
496 GNUNET_DISK_file_close (bf->fh);
497 GNUNET_free (bf);
498 return NULL;
499 }
484 bf->filename = GNUNET_strdup (filename); 500 bf->filename = GNUNET_strdup (filename);
485 /* Alloc block */ 501 /* Alloc block */
486 bf->bitArray = GNUNET_malloc_large (size); 502 bf->bitArray = GNUNET_malloc_large (size);
@@ -499,7 +515,7 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size,
499 /* Read from the file what bits we can */ 515 /* Read from the file what bits we can */
500 rbuff = GNUNET_malloc (BUFFSIZE); 516 rbuff = GNUNET_malloc (BUFFSIZE);
501 pos = 0; 517 pos = 0;
502 while (pos < size * 8) 518 while (pos < size * 8LL)
503 { 519 {
504 int res; 520 int res;
505 521
@@ -507,6 +523,11 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size,
507 if (res == -1) 523 if (res == -1)
508 { 524 {
509 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "read", bf->filename); 525 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "read", bf->filename);
526 GNUNET_free (rbuff);
527 GNUNET_free (bf->filename);
528 GNUNET_DISK_file_close (bf->fh);
529 GNUNET_free (bf);
530 return NULL;
510 } 531 }
511 if (res == 0) 532 if (res == 0)
512 break; /* is ok! we just did not use that many bits yet */ 533 break; /* is ok! we just did not use that many bits yet */
diff --git a/src/util/disk.c b/src/util/disk.c
index 2bdcf3262..ba5d159e4 100644
--- a/src/util/disk.c
+++ b/src/util/disk.c
@@ -206,6 +206,26 @@ GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h)
206 206
207 207
208/** 208/**
209 * Get the size of an open file.
210 *
211 * @param fh open file handle
212 * @param size where to write size of the file
213 * @return GNUNET_OK on success, GNUNET_SYSERR on error
214 */
215int
216GNUNET_DISK_file_handle_size (struct GNUNET_DISK_FileHandle *fh,
217 off_t *size)
218{
219 struct stat sbuf;
220
221 if (0 != FSTAT (fh->fd, &sbuf))
222 return GNUNET_SYSERR;
223 *size = sbuf.st_size;
224 return GNUNET_OK;
225}
226
227
228/**
209 * Move the read/write pointer in a file 229 * Move the read/write pointer in a file
210 * 230 *
211 * @param h handle of an open file 231 * @param h handle of an open file