diff options
author | Sree Harsha Totakura <totakura@in.tum.de> | 2013-04-23 15:45:18 +0000 |
---|---|---|
committer | Sree Harsha Totakura <totakura@in.tum.de> | 2013-04-23 15:45:18 +0000 |
commit | cd74baf3b5f44e84783a0020e6edd8454bfe0cbf (patch) | |
tree | 39581ca9bba59171e7cc234b958e34cf74bcc2ad /src/util/bio.c | |
parent | 0793e4b7af79a49bf1bc275be95f306136ac8cbb (diff) | |
download | gnunet-cd74baf3b5f44e84783a0020e6edd8454bfe0cbf.tar.gz gnunet-cd74baf3b5f44e84783a0020e6edd8454bfe0cbf.zip |
- Support flushing of buffered data
Diffstat (limited to 'src/util/bio.c')
-rw-r--r-- | src/util/bio.c | 49 |
1 files changed, 27 insertions, 22 deletions
diff --git a/src/util/bio.c b/src/util/bio.c index 053b8e1ca..34210535a 100644 --- a/src/util/bio.c +++ b/src/util/bio.c | |||
@@ -349,7 +349,6 @@ GNUNET_BIO_write_open (const char *fn) | |||
349 | h->buffer = (char *) &h[1]; | 349 | h->buffer = (char *) &h[1]; |
350 | h->size = BIO_BUFFER_SIZE; | 350 | h->size = BIO_BUFFER_SIZE; |
351 | h->fd = fd; | 351 | h->fd = fd; |
352 | |||
353 | return h; | 352 | return h; |
354 | } | 353 | } |
355 | 354 | ||
@@ -363,28 +362,41 @@ GNUNET_BIO_write_open (const char *fn) | |||
363 | int | 362 | int |
364 | GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h) | 363 | GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h) |
365 | { | 364 | { |
366 | ssize_t wrt; | ||
367 | int ret; | 365 | int ret; |
368 | 366 | ||
369 | if (NULL == h->fd) | 367 | ret = GNUNET_SYSERR; |
370 | { | 368 | if ( (NULL != h->fd) && (GNUNET_OK == (ret = GNUNET_BIO_flush (h))) ) |
371 | ret = GNUNET_SYSERR; | ||
372 | } | ||
373 | else | ||
374 | { | ||
375 | wrt = GNUNET_DISK_file_write (h->fd, h->buffer, h->have); | ||
376 | if (wrt == h->have) | ||
377 | ret = GNUNET_OK; | ||
378 | else | ||
379 | ret = GNUNET_SYSERR; | ||
380 | GNUNET_DISK_file_close (h->fd); | 369 | GNUNET_DISK_file_close (h->fd); |
381 | } | ||
382 | GNUNET_free (h); | 370 | GNUNET_free (h); |
383 | return ret; | 371 | return ret; |
384 | } | 372 | } |
385 | 373 | ||
386 | 374 | ||
387 | /** | 375 | /** |
376 | * Force a buffered writer to flush its buffer | ||
377 | * | ||
378 | * @param h the writer handle | ||
379 | * @return GNUNET_OK upon success. Upon failure GNUNET_SYSERR is returned and | ||
380 | * the file is closed | ||
381 | */ | ||
382 | int | ||
383 | GNUNET_BIO_flush (struct GNUNET_BIO_WriteHandle *h) | ||
384 | { | ||
385 | ssize_t ret; | ||
386 | |||
387 | ret = GNUNET_DISK_file_write (h->fd, h->buffer, h->have); | ||
388 | if (ret != h->have) | ||
389 | { | ||
390 | GNUNET_DISK_file_close (h->fd); | ||
391 | h->fd = NULL; | ||
392 | return GNUNET_SYSERR; /* error */ | ||
393 | } | ||
394 | h->have = 0; | ||
395 | return GNUNET_OK; | ||
396 | } | ||
397 | |||
398 | |||
399 | /** | ||
388 | * Write a buffer to a file. | 400 | * Write a buffer to a file. |
389 | * | 401 | * |
390 | * @param h handle to open file | 402 | * @param h handle to open file |
@@ -399,7 +411,6 @@ GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h, const void *buffer, | |||
399 | const char *src = buffer; | 411 | const char *src = buffer; |
400 | size_t min; | 412 | size_t min; |
401 | size_t pos; | 413 | size_t pos; |
402 | ssize_t ret; | ||
403 | 414 | ||
404 | if (NULL == h->fd) | 415 | if (NULL == h->fd) |
405 | return GNUNET_SYSERR; | 416 | return GNUNET_SYSERR; |
@@ -416,14 +427,8 @@ GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h, const void *buffer, | |||
416 | if (pos == n) | 427 | if (pos == n) |
417 | return GNUNET_OK; /* done */ | 428 | return GNUNET_OK; /* done */ |
418 | GNUNET_assert (h->have == h->size); | 429 | GNUNET_assert (h->have == h->size); |
419 | ret = GNUNET_DISK_file_write (h->fd, h->buffer, h->size); | 430 | if (GNUNET_OK != GNUNET_BIO_flush (h)) |
420 | if (ret != h->size) | ||
421 | { | ||
422 | GNUNET_DISK_file_close (h->fd); | ||
423 | h->fd = NULL; | ||
424 | return GNUNET_SYSERR; /* error */ | 431 | return GNUNET_SYSERR; /* error */ |
425 | } | ||
426 | h->have = 0; | ||
427 | } | 432 | } |
428 | while (pos < n); /* should always be true */ | 433 | while (pos < n); /* should always be true */ |
429 | GNUNET_break (0); | 434 | GNUNET_break (0); |