2 This file is part of GNUnet.
3 Copyright (C) 2006, 2009, 2013 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
17 * @brief functions for buffering IO
18 * @author Christian Grothoff
21 #include "gnunet_util_lib.h"
23 #define LOG(kind,...) GNUNET_log_from (kind, "util-bio",__VA_ARGS__)
27 * Assumed maximum path length (for source file names).
34 * Size for I/O buffers.
36 #define BIO_BUFFER_SIZE 65536
39 * Maximum size allowed for meta data written/read from disk.
40 * File-sharing limits to 64k, so this should be rather generous.
42 #define MAX_META_DATA (1024 * 1024)
46 * Handle for buffered reading.
48 struct GNUNET_BIO_ReadHandle
51 * Underlying file abstraction.
53 struct GNUNET_DISK_FileHandle *fd;
56 * Error message, NULL if there were no errors.
61 * I/O buffer. Allocated at the end of the struct, do not free!
66 * Number of bytes available in read @e buffer.
71 * Total size of @e buffer.
76 * Current read offset in @e buffer.
83 * Open a file for reading.
85 * @param fn file name to be opened
86 * @return IO handle on success, NULL on error
88 struct GNUNET_BIO_ReadHandle *
89 GNUNET_BIO_read_open (const char *fn)
91 struct GNUNET_DISK_FileHandle *fd;
92 struct GNUNET_BIO_ReadHandle *h;
94 fd = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE);
97 h = GNUNET_malloc (sizeof (struct GNUNET_BIO_ReadHandle) + BIO_BUFFER_SIZE);
98 h->buffer = (char *) &h[1];
99 h->size = BIO_BUFFER_SIZE;
106 * Close an open file. Reports if any errors reading
107 * from the file were encountered.
109 * @param h file handle
110 * @param emsg set to the error message
111 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
114 GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h,
119 err = (NULL == h->emsg) ? GNUNET_OK : GNUNET_SYSERR;
123 GNUNET_free_non_null (h->emsg);
124 GNUNET_DISK_file_close (h->fd);
131 * Read the contents of a binary file into a buffer.
133 * @param h handle to an open file
134 * @param what describes what is being read (for error message creation)
135 * @param result the buffer to write the result to
136 * @param len the number of bytes to read
137 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
140 GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h,
142 void *result, size_t len)
150 return GNUNET_SYSERR;
154 /* first, use buffer */
155 min = h->have - h->pos;
160 GNUNET_memcpy (&dst[pos],
167 return GNUNET_OK; /* done! */
168 GNUNET_assert (((off_t) h->have) == h->pos);
170 ret = GNUNET_DISK_file_read (h->fd,
175 GNUNET_asprintf (&h->emsg,
176 _("Error reading `%s': %s"),
179 return GNUNET_SYSERR;
183 GNUNET_asprintf (&h->emsg,
184 _("Error reading `%s': %s"),
187 return GNUNET_SYSERR;
192 while (pos < len); /* should always be true */
198 * Read the contents of a binary file into a buffer.
200 * @param h handle to an open file
201 * @param file name of the source file
202 * @param line line number in the source file
203 * @param result the buffer to write the result to
204 * @param len the number of bytes to read
205 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
208 GNUNET_BIO_read_fn (struct GNUNET_BIO_ReadHandle *h,
214 char what[PATH_MAX + 1024];
216 GNUNET_snprintf (what, sizeof (what), "%s:%d", file, line);
217 return GNUNET_BIO_read (h, what, result, len);
222 * Read 0-terminated string from a file.
224 * @param h handle to an open file
225 * @param what describes what is being read (for error message creation)
226 * @param result the buffer to store a pointer to the (allocated) string to
227 * (note that *result could be set to NULL as well)
228 * @param max_length maximum allowed length for the string
229 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
232 GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h,
240 if (GNUNET_OK != GNUNET_BIO_read_int32 (h, &big))
242 GNUNET_free_non_null (h->emsg);
243 GNUNET_asprintf (&h->emsg, _("Error reading length of string `%s'"), what);
244 return GNUNET_SYSERR;
251 if (big > max_length)
253 GNUNET_asprintf (&h->emsg, _("String `%s' longer than allowed (%u > %u)"),
254 what, big, max_length);
255 return GNUNET_SYSERR;
257 buf = GNUNET_malloc (big);
262 if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, big))
266 return GNUNET_SYSERR;
273 * Read metadata container from a file.
275 * @param h handle to an open file
276 * @param what describes what is being read (for error message creation)
277 * @param result the buffer to store a pointer to the (allocated) metadata
278 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
281 GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h,
283 struct GNUNET_CONTAINER_MetaData **result)
287 struct GNUNET_CONTAINER_MetaData *meta;
290 GNUNET_BIO_read_int32 (h,
292 return GNUNET_SYSERR;
298 if (size > MAX_META_DATA)
300 GNUNET_asprintf (&h->emsg,
301 _("Serialized metadata `%s' larger than allowed (%u>%u)"),
305 return GNUNET_SYSERR;
307 buf = GNUNET_malloc (size);
315 return GNUNET_SYSERR;
317 meta = GNUNET_CONTAINER_meta_data_deserialize (buf,
322 GNUNET_asprintf (&h->emsg,
323 _("Metadata `%s' failed to deserialize"),
325 return GNUNET_SYSERR;
334 * Read an (u)int32_t.
336 * @param h hande to open file
337 * @param file name of the source file
338 * @param line line number in the source file
339 * @param i address of 32-bit integer to read
340 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
343 GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h,
351 GNUNET_BIO_read_fn (h,
356 return GNUNET_SYSERR;
363 * Read an (u)int64_t.
365 * @param h hande to open file
366 * @param file name of the source file
367 * @param line line number in the source file
368 * @param i address of 64-bit integer to read
369 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
372 GNUNET_BIO_read_int64__ (struct GNUNET_BIO_ReadHandle *h,
380 GNUNET_BIO_read_fn (h,
385 return GNUNET_SYSERR;
386 *i = GNUNET_ntohll (big);
392 * Handle for buffered writing.
394 struct GNUNET_BIO_WriteHandle
397 * Underlying file handle.
399 struct GNUNET_DISK_FileHandle *fd;
402 * I/O buffer. Do not free, allocated at the end of the struct.
407 * Number of bytes already in @e buffer.
412 * Total size of @e buffer.
419 * Open a file for writing.
421 * @param fn file name to be opened
422 * @return IO handle on success, NULL on error
424 struct GNUNET_BIO_WriteHandle *
425 GNUNET_BIO_write_open (const char *fn)
427 struct GNUNET_DISK_FileHandle *fd;
428 struct GNUNET_BIO_WriteHandle *h;
430 fd = GNUNET_DISK_file_open (fn,
431 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE
432 | GNUNET_DISK_OPEN_CREATE,
433 GNUNET_DISK_PERM_USER_READ |
434 GNUNET_DISK_PERM_USER_WRITE);
437 h = GNUNET_malloc (sizeof (struct GNUNET_BIO_WriteHandle) + BIO_BUFFER_SIZE);
438 h->buffer = (char *) &h[1];
439 h->size = BIO_BUFFER_SIZE;
446 * Close an open file for writing.
448 * @param h file handle
449 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
452 GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h)
457 if ( (NULL != h->fd) &&
458 (GNUNET_OK == (ret = GNUNET_BIO_flush (h))) )
459 GNUNET_DISK_file_close (h->fd);
466 * Force a buffered writer to flush its buffer
468 * @param h the writer handle
469 * @return #GNUNET_OK upon success. Upon failure #GNUNET_SYSERR is returned and
473 GNUNET_BIO_flush (struct GNUNET_BIO_WriteHandle *h)
477 ret = GNUNET_DISK_file_write (h->fd,
480 if (ret != (ssize_t) h->have)
482 GNUNET_DISK_file_close (h->fd);
484 return GNUNET_SYSERR; /* error */
492 * Write a buffer to a file.
494 * @param h handle to open file
495 * @param buffer the data to write
496 * @param n number of bytes to write
497 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
500 GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h,
504 const char *src = buffer;
509 return GNUNET_SYSERR;
513 /* first, just use buffer */
514 min = h->size - h->have;
517 GNUNET_memcpy (&h->buffer[h->have],
523 return GNUNET_OK; /* done */
524 GNUNET_assert (h->have == h->size);
525 if (GNUNET_OK != GNUNET_BIO_flush (h))
526 return GNUNET_SYSERR; /* error */
528 while (pos < n); /* should always be true */
535 * Write a string to a file.
537 * @param h handle to open file
538 * @param s string to write (can be NULL)
539 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
542 GNUNET_BIO_write_string (struct GNUNET_BIO_WriteHandle *h,
547 slen = (uint32_t) ((s == NULL) ? 0 : strlen (s) + 1);
548 if (GNUNET_OK != GNUNET_BIO_write_int32 (h, slen))
549 return GNUNET_SYSERR;
551 return GNUNET_BIO_write (h, s, slen - 1);
557 * Write metadata container to a file.
559 * @param h handle to open file
560 * @param m metadata to write
561 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
564 GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h,
565 const struct GNUNET_CONTAINER_MetaData *m)
571 return GNUNET_BIO_write_int32 (h, 0);
574 GNUNET_CONTAINER_meta_data_serialize (m, &buf, MAX_META_DATA,
575 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
579 return GNUNET_SYSERR;
581 if ((GNUNET_OK != GNUNET_BIO_write_int32 (h, (uint32_t) size)) ||
582 (GNUNET_OK != GNUNET_BIO_write (h, buf, size)))
585 return GNUNET_SYSERR;
593 * Write an (u)int32_t.
595 * @param h hande to open file
596 * @param i 32-bit integer to write
597 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
600 GNUNET_BIO_write_int32 (struct GNUNET_BIO_WriteHandle *h,
606 return GNUNET_BIO_write (h, &big, sizeof (int32_t));
611 * Write an (u)int64_t.
613 * @param h hande to open file
614 * @param i 64-bit integer to write
615 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
618 GNUNET_BIO_write_int64 (struct GNUNET_BIO_WriteHandle *h,
623 big = GNUNET_htonll (i);
624 return GNUNET_BIO_write (h, &big, sizeof (int64_t));