2 This file is part of GNUnet.
3 (C) 2006, 2009, 2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @brief functions for buffering IO
23 * @author Christian Grothoff
26 #include "gnunet_bio_lib.h"
27 #include "gnunet_disk_lib.h"
29 #define LOG(kind,...) GNUNET_log_from (kind, "util",__VA_ARGS__)
32 * Size for I/O buffers.
34 #define BIO_BUFFER_SIZE 65536
37 * Maximum size allowed for meta data written/read from disk.
38 * File-sharing limits to 64k, so this should be rather generous.
40 #define MAX_META_DATA (1024 * 1024)
44 * Handle for buffered reading.
46 struct GNUNET_BIO_ReadHandle
49 * Underlying file abstraction.
51 struct GNUNET_DISK_FileHandle *fd;
54 * Error message, NULL if there were no errors.
59 * I/O buffer. Allocated at the end of the struct, do not free!
64 * Number of bytes available in read @e buffer.
69 * Total size of @e buffer.
74 * Current read offset in @e buffer.
81 * Open a file for reading.
83 * @param fn file name to be opened
84 * @return IO handle on success, NULL on error
86 struct GNUNET_BIO_ReadHandle *
87 GNUNET_BIO_read_open (const char *fn)
89 struct GNUNET_DISK_FileHandle *fd;
90 struct GNUNET_BIO_ReadHandle *h;
92 fd = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE);
95 h = GNUNET_malloc (sizeof (struct GNUNET_BIO_ReadHandle) + BIO_BUFFER_SIZE);
96 h->buffer = (char *) &h[1];
97 h->size = BIO_BUFFER_SIZE;
104 * Close an open file. Reports if any errors reading
105 * from the file were encountered.
107 * @param h file handle
108 * @param emsg set to the error message
109 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
112 GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h,
117 err = (NULL == h->emsg) ? GNUNET_OK : GNUNET_SYSERR;
121 GNUNET_free_non_null (h->emsg);
122 GNUNET_DISK_file_close (h->fd);
129 * Read the contents of a binary file into a buffer.
131 * @param h handle to an open file
132 * @param what describes what is being read (for error message creation)
133 * @param result the buffer to write the result to
134 * @param len the number of bytes to read
135 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
138 GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h,
140 void *result, size_t len)
148 return GNUNET_SYSERR;
152 /* first, use buffer */
153 min = h->have - h->pos;
158 memcpy (&dst[pos], &h->buffer[h->pos], min);
163 return GNUNET_OK; /* done! */
164 GNUNET_assert (h->have == h->pos);
166 ret = GNUNET_DISK_file_read (h->fd, h->buffer, h->size);
169 GNUNET_asprintf (&h->emsg,
170 _("Error reading `%s': %s"),
173 return GNUNET_SYSERR;
177 GNUNET_asprintf (&h->emsg,
178 _("Error reading `%s': %s"),
181 return GNUNET_SYSERR;
186 while (pos < len); /* should always be true */
192 * Read the contents of a binary file into a buffer.
194 * @param h handle to an open file
195 * @param file name of the source file
196 * @param line line number in the source file
197 * @param result the buffer to write the result to
198 * @param len the number of bytes to read
199 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
202 GNUNET_BIO_read_fn (struct GNUNET_BIO_ReadHandle *h,
210 GNUNET_snprintf (what, sizeof (what), "%s:%d", file, line);
211 return GNUNET_BIO_read (h, what, result, len);
216 * Read 0-terminated string from a file.
218 * @param h handle to an open file
219 * @param what describes what is being read (for error message creation)
220 * @param result the buffer to store a pointer to the (allocated) string to
221 * (note that *result could be set to NULL as well)
222 * @param maxLen maximum allowed length for the string
223 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
226 GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h, const char *what,
227 char **result, size_t maxLen)
232 if (GNUNET_OK != GNUNET_BIO_read_int32 (h, &big))
234 GNUNET_free_non_null (h->emsg);
235 GNUNET_asprintf (&h->emsg, _("Error reading length of string `%s'"), what);
236 return GNUNET_SYSERR;
245 GNUNET_asprintf (&h->emsg, _("String `%s' longer than allowed (%u > %u)"),
247 return GNUNET_SYSERR;
249 buf = GNUNET_malloc (big);
254 if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, big))
258 return GNUNET_SYSERR;
265 * Read metadata container from a file.
267 * @param h handle to an open file
268 * @param what describes what is being read (for error message creation)
269 * @param result the buffer to store a pointer to the (allocated) metadata
270 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
273 GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h, const char *what,
274 struct GNUNET_CONTAINER_MetaData **result)
278 struct GNUNET_CONTAINER_MetaData *meta;
280 if (GNUNET_BIO_read_int32 (h, (int32_t *) & size) != GNUNET_OK)
281 return GNUNET_SYSERR;
287 if (size > MAX_META_DATA)
289 GNUNET_asprintf (&h->emsg,
290 _("Serialized metadata `%s' larger than allowed (%u>%u)"),
291 what, size, MAX_META_DATA);
292 return GNUNET_SYSERR;
294 buf = GNUNET_malloc (size);
295 if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, size))
298 return GNUNET_SYSERR;
300 meta = GNUNET_CONTAINER_meta_data_deserialize (buf, size);
304 GNUNET_asprintf (&h->emsg, _("Metadata `%s' failed to deserialize"), what);
305 return GNUNET_SYSERR;
314 * Read an (u)int32_t.
316 * @param h hande to open file
317 * @param file name of the source file
318 * @param line line number in the source file
319 * @param i address of 32-bit integer to read
320 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
323 GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h, const char *file,
324 int line, int32_t * i)
328 if (GNUNET_OK != GNUNET_BIO_read_fn (h, file, line, &big, sizeof (int32_t)))
329 return GNUNET_SYSERR;
336 * Read an (u)int64_t.
338 * @param h hande to open file
339 * @param file name of the source file
340 * @param line line number in the source file
341 * @param i address of 64-bit integer to read
342 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
345 GNUNET_BIO_read_int64__ (struct GNUNET_BIO_ReadHandle *h,
347 int line, int64_t * i)
351 if (GNUNET_OK != GNUNET_BIO_read_fn (h, file, line, &big, sizeof (int64_t)))
352 return GNUNET_SYSERR;
353 *i = GNUNET_ntohll (big);
359 * Handle for buffered writing.
361 struct GNUNET_BIO_WriteHandle
364 * Underlying file handle.
366 struct GNUNET_DISK_FileHandle *fd;
369 * I/O buffer. Do not free, allocated at the end of the struct.
374 * Number of bytes already in @e buffer.
379 * Total size of @e buffer.
386 * Open a file for writing.
388 * @param fn file name to be opened
389 * @return IO handle on success, NULL on error
391 struct GNUNET_BIO_WriteHandle *
392 GNUNET_BIO_write_open (const char *fn)
394 struct GNUNET_DISK_FileHandle *fd;
395 struct GNUNET_BIO_WriteHandle *h;
397 fd = GNUNET_DISK_file_open (fn,
398 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE
399 | GNUNET_DISK_OPEN_CREATE,
400 GNUNET_DISK_PERM_USER_READ |
401 GNUNET_DISK_PERM_USER_WRITE);
404 h = GNUNET_malloc (sizeof (struct GNUNET_BIO_WriteHandle) + BIO_BUFFER_SIZE);
405 h->buffer = (char *) &h[1];
406 h->size = BIO_BUFFER_SIZE;
413 * Close an open file for writing.
415 * @param h file handle
416 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
419 GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h)
424 if ( (NULL != h->fd) && (GNUNET_OK == (ret = GNUNET_BIO_flush (h))) )
425 GNUNET_DISK_file_close (h->fd);
432 * Force a buffered writer to flush its buffer
434 * @param h the writer handle
435 * @return #GNUNET_OK upon success. Upon failure #GNUNET_SYSERR is returned and
439 GNUNET_BIO_flush (struct GNUNET_BIO_WriteHandle *h)
443 ret = GNUNET_DISK_file_write (h->fd, h->buffer, h->have);
446 GNUNET_DISK_file_close (h->fd);
448 return GNUNET_SYSERR; /* error */
456 * Write a buffer to a file.
458 * @param h handle to open file
459 * @param buffer the data to write
460 * @param n number of bytes to write
461 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
464 GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h, const void *buffer,
467 const char *src = buffer;
472 return GNUNET_SYSERR;
476 /* first, just use buffer */
477 min = h->size - h->have;
480 memcpy (&h->buffer[h->have], &src[pos], min);
484 return GNUNET_OK; /* done */
485 GNUNET_assert (h->have == h->size);
486 if (GNUNET_OK != GNUNET_BIO_flush (h))
487 return GNUNET_SYSERR; /* error */
489 while (pos < n); /* should always be true */
496 * Write a string to a file.
498 * @param h handle to open file
499 * @param s string to write (can be NULL)
500 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
503 GNUNET_BIO_write_string (struct GNUNET_BIO_WriteHandle *h,
508 slen = (uint32_t) ((s == NULL) ? 0 : strlen (s) + 1);
509 if (GNUNET_OK != GNUNET_BIO_write_int32 (h, slen))
510 return GNUNET_SYSERR;
512 return GNUNET_BIO_write (h, s, slen - 1);
518 * Write metadata container to a file.
520 * @param h handle to open file
521 * @param m metadata to write
522 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
525 GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h,
526 const struct GNUNET_CONTAINER_MetaData *m)
532 return GNUNET_BIO_write_int32 (h, 0);
535 GNUNET_CONTAINER_meta_data_serialize (m, &buf, MAX_META_DATA,
536 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
540 return GNUNET_SYSERR;
542 if ((GNUNET_OK != GNUNET_BIO_write_int32 (h, (uint32_t) size)) ||
543 (GNUNET_OK != GNUNET_BIO_write (h, buf, size)))
546 return GNUNET_SYSERR;
554 * Write an (u)int32_t.
556 * @param h hande to open file
557 * @param i 32-bit integer to write
558 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
561 GNUNET_BIO_write_int32 (struct GNUNET_BIO_WriteHandle *h,
567 return GNUNET_BIO_write (h, &big, sizeof (int32_t));
572 * Write an (u)int64_t.
574 * @param h hande to open file
575 * @param i 64-bit integer to write
576 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
579 GNUNET_BIO_write_int64 (struct GNUNET_BIO_WriteHandle *h,
584 big = GNUNET_htonll (i);
585 return GNUNET_BIO_write (h, &big, sizeof (int64_t));