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
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @brief functions for buffering IO
23 * @author Christian Grothoff
26 #include "gnunet_util_lib.h"
28 #define LOG(kind,...) GNUNET_log_from (kind, "util-bio",__VA_ARGS__)
32 * Assumed maximum path length (for source file names).
39 * Size for I/O buffers.
41 #define BIO_BUFFER_SIZE 65536
44 * Maximum size allowed for meta data written/read from disk.
45 * File-sharing limits to 64k, so this should be rather generous.
47 #define MAX_META_DATA (1024 * 1024)
51 * Handle for buffered reading.
53 struct GNUNET_BIO_ReadHandle
56 * Underlying file abstraction.
58 struct GNUNET_DISK_FileHandle *fd;
61 * Error message, NULL if there were no errors.
66 * I/O buffer. Allocated at the end of the struct, do not free!
71 * Number of bytes available in read @e buffer.
76 * Total size of @e buffer.
81 * Current read offset in @e buffer.
88 * Open a file for reading.
90 * @param fn file name to be opened
91 * @return IO handle on success, NULL on error
93 struct GNUNET_BIO_ReadHandle *
94 GNUNET_BIO_read_open (const char *fn)
96 struct GNUNET_DISK_FileHandle *fd;
97 struct GNUNET_BIO_ReadHandle *h;
99 fd = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE);
102 h = GNUNET_malloc (sizeof (struct GNUNET_BIO_ReadHandle) + BIO_BUFFER_SIZE);
103 h->buffer = (char *) &h[1];
104 h->size = BIO_BUFFER_SIZE;
111 * Close an open file. Reports if any errors reading
112 * from the file were encountered.
114 * @param h file handle
115 * @param emsg set to the error message
116 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
119 GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h,
124 err = (NULL == h->emsg) ? GNUNET_OK : GNUNET_SYSERR;
128 GNUNET_free_non_null (h->emsg);
129 GNUNET_DISK_file_close (h->fd);
136 * Read the contents of a binary file into a buffer.
138 * @param h handle to an open file
139 * @param what describes what is being read (for error message creation)
140 * @param result the buffer to write the result to
141 * @param len the number of bytes to read
142 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
145 GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h,
147 void *result, size_t len)
155 return GNUNET_SYSERR;
159 /* first, use buffer */
160 min = h->have - h->pos;
165 GNUNET_memcpy (&dst[pos], &h->buffer[h->pos], min);
170 return GNUNET_OK; /* done! */
171 GNUNET_assert (h->have == h->pos);
173 ret = GNUNET_DISK_file_read (h->fd, h->buffer, h->size);
176 GNUNET_asprintf (&h->emsg,
177 _("Error reading `%s': %s"),
180 return GNUNET_SYSERR;
184 GNUNET_asprintf (&h->emsg,
185 _("Error reading `%s': %s"),
188 return GNUNET_SYSERR;
193 while (pos < len); /* should always be true */
199 * Read the contents of a binary file into a buffer.
201 * @param h handle to an open file
202 * @param file name of the source file
203 * @param line line number in the source file
204 * @param result the buffer to write the result to
205 * @param len the number of bytes to read
206 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
209 GNUNET_BIO_read_fn (struct GNUNET_BIO_ReadHandle *h,
215 char what[PATH_MAX + 1024];
217 GNUNET_snprintf (what, sizeof (what), "%s:%d", file, line);
218 return GNUNET_BIO_read (h, what, result, len);
223 * Read 0-terminated string from a file.
225 * @param h handle to an open file
226 * @param what describes what is being read (for error message creation)
227 * @param result the buffer to store a pointer to the (allocated) string to
228 * (note that *result could be set to NULL as well)
229 * @param max_length maximum allowed length for the string
230 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
233 GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h,
241 if (GNUNET_OK != GNUNET_BIO_read_int32 (h, &big))
243 GNUNET_free_non_null (h->emsg);
244 GNUNET_asprintf (&h->emsg, _("Error reading length of string `%s'"), what);
245 return GNUNET_SYSERR;
252 if (big > max_length)
254 GNUNET_asprintf (&h->emsg, _("String `%s' longer than allowed (%u > %u)"),
255 what, big, max_length);
256 return GNUNET_SYSERR;
258 buf = GNUNET_malloc (big);
263 if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, big))
267 return GNUNET_SYSERR;
274 * Read metadata container from a file.
276 * @param h handle to an open file
277 * @param what describes what is being read (for error message creation)
278 * @param result the buffer to store a pointer to the (allocated) metadata
279 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
282 GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h,
284 struct GNUNET_CONTAINER_MetaData **result)
288 struct GNUNET_CONTAINER_MetaData *meta;
290 if (GNUNET_BIO_read_int32 (h, (int32_t *) & size) != GNUNET_OK)
291 return GNUNET_SYSERR;
297 if (size > MAX_META_DATA)
299 GNUNET_asprintf (&h->emsg,
300 _("Serialized metadata `%s' larger than allowed (%u>%u)"),
301 what, size, MAX_META_DATA);
302 return GNUNET_SYSERR;
304 buf = GNUNET_malloc (size);
305 if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, size))
308 return GNUNET_SYSERR;
310 meta = GNUNET_CONTAINER_meta_data_deserialize (buf, size);
314 GNUNET_asprintf (&h->emsg, _("Metadata `%s' failed to deserialize"), what);
315 return GNUNET_SYSERR;
324 * Read an (u)int32_t.
326 * @param h hande to open file
327 * @param file name of the source file
328 * @param line line number in the source file
329 * @param i address of 32-bit integer to read
330 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
333 GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h, const char *file,
334 int line, int32_t * i)
338 if (GNUNET_OK != GNUNET_BIO_read_fn (h, file, line, &big, sizeof (int32_t)))
339 return GNUNET_SYSERR;
346 * Read an (u)int64_t.
348 * @param h hande to open file
349 * @param file name of the source file
350 * @param line line number in the source file
351 * @param i address of 64-bit integer to read
352 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
355 GNUNET_BIO_read_int64__ (struct GNUNET_BIO_ReadHandle *h,
362 if (GNUNET_OK != GNUNET_BIO_read_fn (h, file, line, &big, sizeof (int64_t)))
363 return GNUNET_SYSERR;
364 *i = GNUNET_ntohll (big);
370 * Handle for buffered writing.
372 struct GNUNET_BIO_WriteHandle
375 * Underlying file handle.
377 struct GNUNET_DISK_FileHandle *fd;
380 * I/O buffer. Do not free, allocated at the end of the struct.
385 * Number of bytes already in @e buffer.
390 * Total size of @e buffer.
397 * Open a file for writing.
399 * @param fn file name to be opened
400 * @return IO handle on success, NULL on error
402 struct GNUNET_BIO_WriteHandle *
403 GNUNET_BIO_write_open (const char *fn)
405 struct GNUNET_DISK_FileHandle *fd;
406 struct GNUNET_BIO_WriteHandle *h;
408 fd = GNUNET_DISK_file_open (fn,
409 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE
410 | GNUNET_DISK_OPEN_CREATE,
411 GNUNET_DISK_PERM_USER_READ |
412 GNUNET_DISK_PERM_USER_WRITE);
415 h = GNUNET_malloc (sizeof (struct GNUNET_BIO_WriteHandle) + BIO_BUFFER_SIZE);
416 h->buffer = (char *) &h[1];
417 h->size = BIO_BUFFER_SIZE;
424 * Close an open file for writing.
426 * @param h file handle
427 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
430 GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h)
435 if ( (NULL != h->fd) && (GNUNET_OK == (ret = GNUNET_BIO_flush (h))) )
436 GNUNET_DISK_file_close (h->fd);
443 * Force a buffered writer to flush its buffer
445 * @param h the writer handle
446 * @return #GNUNET_OK upon success. Upon failure #GNUNET_SYSERR is returned and
450 GNUNET_BIO_flush (struct GNUNET_BIO_WriteHandle *h)
454 ret = GNUNET_DISK_file_write (h->fd, h->buffer, h->have);
457 GNUNET_DISK_file_close (h->fd);
459 return GNUNET_SYSERR; /* error */
467 * Write a buffer to a file.
469 * @param h handle to open file
470 * @param buffer the data to write
471 * @param n number of bytes to write
472 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
475 GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h, const void *buffer,
478 const char *src = buffer;
483 return GNUNET_SYSERR;
487 /* first, just use buffer */
488 min = h->size - h->have;
491 GNUNET_memcpy (&h->buffer[h->have], &src[pos], min);
495 return GNUNET_OK; /* done */
496 GNUNET_assert (h->have == h->size);
497 if (GNUNET_OK != GNUNET_BIO_flush (h))
498 return GNUNET_SYSERR; /* error */
500 while (pos < n); /* should always be true */
507 * Write a string to a file.
509 * @param h handle to open file
510 * @param s string to write (can be NULL)
511 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
514 GNUNET_BIO_write_string (struct GNUNET_BIO_WriteHandle *h,
519 slen = (uint32_t) ((s == NULL) ? 0 : strlen (s) + 1);
520 if (GNUNET_OK != GNUNET_BIO_write_int32 (h, slen))
521 return GNUNET_SYSERR;
523 return GNUNET_BIO_write (h, s, slen - 1);
529 * Write metadata container to a file.
531 * @param h handle to open file
532 * @param m metadata to write
533 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
536 GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h,
537 const struct GNUNET_CONTAINER_MetaData *m)
543 return GNUNET_BIO_write_int32 (h, 0);
546 GNUNET_CONTAINER_meta_data_serialize (m, &buf, MAX_META_DATA,
547 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
551 return GNUNET_SYSERR;
553 if ((GNUNET_OK != GNUNET_BIO_write_int32 (h, (uint32_t) size)) ||
554 (GNUNET_OK != GNUNET_BIO_write (h, buf, size)))
557 return GNUNET_SYSERR;
565 * Write an (u)int32_t.
567 * @param h hande to open file
568 * @param i 32-bit integer to write
569 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
572 GNUNET_BIO_write_int32 (struct GNUNET_BIO_WriteHandle *h,
578 return GNUNET_BIO_write (h, &big, sizeof (int32_t));
583 * Write an (u)int64_t.
585 * @param h hande to open file
586 * @param i 64-bit integer to write
587 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
590 GNUNET_BIO_write_int64 (struct GNUNET_BIO_WriteHandle *h,
595 big = GNUNET_htonll (i);
596 return GNUNET_BIO_write (h, &big, sizeof (int64_t));