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.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @brief functions for buffering IO
21 * @author Christian Grothoff
24 #include "gnunet_util_lib.h"
26 #define LOG(kind,...) GNUNET_log_from (kind, "util-bio",__VA_ARGS__)
30 * Assumed maximum path length (for source file names).
37 * Size for I/O buffers.
39 #define BIO_BUFFER_SIZE 65536
42 * Maximum size allowed for meta data written/read from disk.
43 * File-sharing limits to 64k, so this should be rather generous.
45 #define MAX_META_DATA (1024 * 1024)
49 * Handle for buffered reading.
51 struct GNUNET_BIO_ReadHandle
54 * Underlying file abstraction.
56 struct GNUNET_DISK_FileHandle *fd;
59 * Error message, NULL if there were no errors.
64 * I/O buffer. Allocated at the end of the struct, do not free!
69 * Number of bytes available in read @e buffer.
74 * Total size of @e buffer.
79 * Current read offset in @e buffer.
86 * Open a file for reading.
88 * @param fn file name to be opened
89 * @return IO handle on success, NULL on error
91 struct GNUNET_BIO_ReadHandle *
92 GNUNET_BIO_read_open (const char *fn)
94 struct GNUNET_DISK_FileHandle *fd;
95 struct GNUNET_BIO_ReadHandle *h;
97 fd = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE);
100 h = GNUNET_malloc (sizeof (struct GNUNET_BIO_ReadHandle) + BIO_BUFFER_SIZE);
101 h->buffer = (char *) &h[1];
102 h->size = BIO_BUFFER_SIZE;
109 * Close an open file. Reports if any errors reading
110 * from the file were encountered.
112 * @param h file handle
113 * @param emsg set to the error message
114 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
117 GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h,
122 err = (NULL == h->emsg) ? GNUNET_OK : GNUNET_SYSERR;
126 GNUNET_free_non_null (h->emsg);
127 GNUNET_DISK_file_close (h->fd);
134 * Read the contents of a binary file into a buffer.
136 * @param h handle to an open file
137 * @param what describes what is being read (for error message creation)
138 * @param result the buffer to write the result to
139 * @param len the number of bytes to read
140 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
143 GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h,
145 void *result, size_t len)
153 return GNUNET_SYSERR;
157 /* first, use buffer */
158 min = h->have - h->pos;
163 GNUNET_memcpy (&dst[pos],
170 return GNUNET_OK; /* done! */
171 GNUNET_assert (((off_t) h->have) == h->pos);
173 ret = GNUNET_DISK_file_read (h->fd,
178 GNUNET_asprintf (&h->emsg,
179 _("Error reading `%s': %s"),
182 return GNUNET_SYSERR;
186 GNUNET_asprintf (&h->emsg,
187 _("Error reading `%s': %s"),
190 return GNUNET_SYSERR;
195 while (pos < len); /* should always be true */
201 * Read the contents of a binary file into a buffer.
203 * @param h handle to an open file
204 * @param file name of the source file
205 * @param line line number in the source file
206 * @param result the buffer to write the result to
207 * @param len the number of bytes to read
208 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
211 GNUNET_BIO_read_fn (struct GNUNET_BIO_ReadHandle *h,
217 char what[PATH_MAX + 1024];
219 GNUNET_snprintf (what, sizeof (what), "%s:%d", file, line);
220 return GNUNET_BIO_read (h, what, result, len);
225 * Read 0-terminated string from a file.
227 * @param h handle to an open file
228 * @param what describes what is being read (for error message creation)
229 * @param result the buffer to store a pointer to the (allocated) string to
230 * (note that *result could be set to NULL as well)
231 * @param max_length maximum allowed length for the string
232 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
235 GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h,
243 if (GNUNET_OK != GNUNET_BIO_read_int32 (h, &big))
245 GNUNET_free_non_null (h->emsg);
246 GNUNET_asprintf (&h->emsg, _("Error reading length of string `%s'"), what);
247 return GNUNET_SYSERR;
254 if (big > max_length)
256 GNUNET_asprintf (&h->emsg, _("String `%s' longer than allowed (%u > %u)"),
257 what, big, max_length);
258 return GNUNET_SYSERR;
260 buf = GNUNET_malloc (big);
265 if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, big))
269 return GNUNET_SYSERR;
276 * Read metadata container from a file.
278 * @param h handle to an open file
279 * @param what describes what is being read (for error message creation)
280 * @param result the buffer to store a pointer to the (allocated) metadata
281 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
284 GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h,
286 struct GNUNET_CONTAINER_MetaData **result)
290 struct GNUNET_CONTAINER_MetaData *meta;
293 GNUNET_BIO_read_int32 (h,
295 return GNUNET_SYSERR;
301 if (size > MAX_META_DATA)
303 GNUNET_asprintf (&h->emsg,
304 _("Serialized metadata `%s' larger than allowed (%u>%u)"),
308 return GNUNET_SYSERR;
310 buf = GNUNET_malloc (size);
318 return GNUNET_SYSERR;
320 meta = GNUNET_CONTAINER_meta_data_deserialize (buf,
325 GNUNET_asprintf (&h->emsg,
326 _("Metadata `%s' failed to deserialize"),
328 return GNUNET_SYSERR;
337 * Read an (u)int32_t.
339 * @param h hande to open file
340 * @param file name of the source file
341 * @param line line number in the source file
342 * @param i address of 32-bit integer to read
343 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
346 GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h,
354 GNUNET_BIO_read_fn (h,
359 return GNUNET_SYSERR;
366 * Read an (u)int64_t.
368 * @param h hande to open file
369 * @param file name of the source file
370 * @param line line number in the source file
371 * @param i address of 64-bit integer to read
372 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
375 GNUNET_BIO_read_int64__ (struct GNUNET_BIO_ReadHandle *h,
383 GNUNET_BIO_read_fn (h,
388 return GNUNET_SYSERR;
389 *i = GNUNET_ntohll (big);
395 * Handle for buffered writing.
397 struct GNUNET_BIO_WriteHandle
400 * Underlying file handle.
402 struct GNUNET_DISK_FileHandle *fd;
405 * I/O buffer. Do not free, allocated at the end of the struct.
410 * Number of bytes already in @e buffer.
415 * Total size of @e buffer.
422 * Open a file for writing.
424 * @param fn file name to be opened
425 * @return IO handle on success, NULL on error
427 struct GNUNET_BIO_WriteHandle *
428 GNUNET_BIO_write_open (const char *fn)
430 struct GNUNET_DISK_FileHandle *fd;
431 struct GNUNET_BIO_WriteHandle *h;
433 fd = GNUNET_DISK_file_open (fn,
434 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE
435 | GNUNET_DISK_OPEN_CREATE,
436 GNUNET_DISK_PERM_USER_READ |
437 GNUNET_DISK_PERM_USER_WRITE);
440 h = GNUNET_malloc (sizeof (struct GNUNET_BIO_WriteHandle) + BIO_BUFFER_SIZE);
441 h->buffer = (char *) &h[1];
442 h->size = BIO_BUFFER_SIZE;
449 * Close an open file for writing.
451 * @param h file handle
452 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
455 GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h)
460 if ( (NULL != h->fd) &&
461 (GNUNET_OK == (ret = GNUNET_BIO_flush (h))) )
462 GNUNET_DISK_file_close (h->fd);
469 * Force a buffered writer to flush its buffer
471 * @param h the writer handle
472 * @return #GNUNET_OK upon success. Upon failure #GNUNET_SYSERR is returned and
476 GNUNET_BIO_flush (struct GNUNET_BIO_WriteHandle *h)
480 ret = GNUNET_DISK_file_write (h->fd,
483 if (ret != (ssize_t) h->have)
485 GNUNET_DISK_file_close (h->fd);
487 return GNUNET_SYSERR; /* error */
495 * Write a buffer to a file.
497 * @param h handle to open file
498 * @param buffer the data to write
499 * @param n number of bytes to write
500 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
503 GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h,
507 const char *src = buffer;
512 return GNUNET_SYSERR;
516 /* first, just use buffer */
517 min = h->size - h->have;
520 GNUNET_memcpy (&h->buffer[h->have],
526 return GNUNET_OK; /* done */
527 GNUNET_assert (h->have == h->size);
528 if (GNUNET_OK != GNUNET_BIO_flush (h))
529 return GNUNET_SYSERR; /* error */
531 while (pos < n); /* should always be true */
538 * Write a string to a file.
540 * @param h handle to open file
541 * @param s string to write (can be NULL)
542 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
545 GNUNET_BIO_write_string (struct GNUNET_BIO_WriteHandle *h,
550 slen = (uint32_t) ((s == NULL) ? 0 : strlen (s) + 1);
551 if (GNUNET_OK != GNUNET_BIO_write_int32 (h, slen))
552 return GNUNET_SYSERR;
554 return GNUNET_BIO_write (h, s, slen - 1);
560 * Write metadata container to a file.
562 * @param h handle to open file
563 * @param m metadata to write
564 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
567 GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h,
568 const struct GNUNET_CONTAINER_MetaData *m)
574 return GNUNET_BIO_write_int32 (h, 0);
577 GNUNET_CONTAINER_meta_data_serialize (m, &buf, MAX_META_DATA,
578 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
582 return GNUNET_SYSERR;
584 if ((GNUNET_OK != GNUNET_BIO_write_int32 (h, (uint32_t) size)) ||
585 (GNUNET_OK != GNUNET_BIO_write (h, buf, size)))
588 return GNUNET_SYSERR;
596 * Write an (u)int32_t.
598 * @param h hande to open file
599 * @param i 32-bit integer to write
600 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
603 GNUNET_BIO_write_int32 (struct GNUNET_BIO_WriteHandle *h,
609 return GNUNET_BIO_write (h, &big, sizeof (int32_t));
614 * Write an (u)int64_t.
616 * @param h hande to open file
617 * @param i 64-bit integer to write
618 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
621 GNUNET_BIO_write_int64 (struct GNUNET_BIO_WriteHandle *h,
626 big = GNUNET_htonll (i);
627 return GNUNET_BIO_write (h, &big, sizeof (int64_t));