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 max_length 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,
234 if (GNUNET_OK != GNUNET_BIO_read_int32 (h, &big))
236 GNUNET_free_non_null (h->emsg);
237 GNUNET_asprintf (&h->emsg, _("Error reading length of string `%s'"), what);
238 return GNUNET_SYSERR;
245 if (big > max_length)
247 GNUNET_asprintf (&h->emsg, _("String `%s' longer than allowed (%u > %u)"),
248 what, big, max_length);
249 return GNUNET_SYSERR;
251 buf = GNUNET_malloc (big);
256 if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, big))
260 return GNUNET_SYSERR;
267 * Read metadata container from a file.
269 * @param h handle to an open file
270 * @param what describes what is being read (for error message creation)
271 * @param result the buffer to store a pointer to the (allocated) metadata
272 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
275 GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h,
277 struct GNUNET_CONTAINER_MetaData **result)
281 struct GNUNET_CONTAINER_MetaData *meta;
283 if (GNUNET_BIO_read_int32 (h, (int32_t *) & size) != GNUNET_OK)
284 return GNUNET_SYSERR;
290 if (size > MAX_META_DATA)
292 GNUNET_asprintf (&h->emsg,
293 _("Serialized metadata `%s' larger than allowed (%u>%u)"),
294 what, size, MAX_META_DATA);
295 return GNUNET_SYSERR;
297 buf = GNUNET_malloc (size);
298 if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, size))
301 return GNUNET_SYSERR;
303 meta = GNUNET_CONTAINER_meta_data_deserialize (buf, size);
307 GNUNET_asprintf (&h->emsg, _("Metadata `%s' failed to deserialize"), what);
308 return GNUNET_SYSERR;
317 * Read an (u)int32_t.
319 * @param h hande to open file
320 * @param file name of the source file
321 * @param line line number in the source file
322 * @param i address of 32-bit integer to read
323 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
326 GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h, const char *file,
327 int line, int32_t * i)
331 if (GNUNET_OK != GNUNET_BIO_read_fn (h, file, line, &big, sizeof (int32_t)))
332 return GNUNET_SYSERR;
339 * Read an (u)int64_t.
341 * @param h hande to open file
342 * @param file name of the source file
343 * @param line line number in the source file
344 * @param i address of 64-bit integer to read
345 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
348 GNUNET_BIO_read_int64__ (struct GNUNET_BIO_ReadHandle *h,
355 if (GNUNET_OK != GNUNET_BIO_read_fn (h, file, line, &big, sizeof (int64_t)))
356 return GNUNET_SYSERR;
357 *i = GNUNET_ntohll (big);
363 * Handle for buffered writing.
365 struct GNUNET_BIO_WriteHandle
368 * Underlying file handle.
370 struct GNUNET_DISK_FileHandle *fd;
373 * I/O buffer. Do not free, allocated at the end of the struct.
378 * Number of bytes already in @e buffer.
383 * Total size of @e buffer.
390 * Open a file for writing.
392 * @param fn file name to be opened
393 * @return IO handle on success, NULL on error
395 struct GNUNET_BIO_WriteHandle *
396 GNUNET_BIO_write_open (const char *fn)
398 struct GNUNET_DISK_FileHandle *fd;
399 struct GNUNET_BIO_WriteHandle *h;
401 fd = GNUNET_DISK_file_open (fn,
402 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE
403 | GNUNET_DISK_OPEN_CREATE,
404 GNUNET_DISK_PERM_USER_READ |
405 GNUNET_DISK_PERM_USER_WRITE);
408 h = GNUNET_malloc (sizeof (struct GNUNET_BIO_WriteHandle) + BIO_BUFFER_SIZE);
409 h->buffer = (char *) &h[1];
410 h->size = BIO_BUFFER_SIZE;
417 * Close an open file for writing.
419 * @param h file handle
420 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
423 GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h)
428 if ( (NULL != h->fd) && (GNUNET_OK == (ret = GNUNET_BIO_flush (h))) )
429 GNUNET_DISK_file_close (h->fd);
436 * Force a buffered writer to flush its buffer
438 * @param h the writer handle
439 * @return #GNUNET_OK upon success. Upon failure #GNUNET_SYSERR is returned and
443 GNUNET_BIO_flush (struct GNUNET_BIO_WriteHandle *h)
447 ret = GNUNET_DISK_file_write (h->fd, h->buffer, h->have);
450 GNUNET_DISK_file_close (h->fd);
452 return GNUNET_SYSERR; /* error */
460 * Write a buffer to a file.
462 * @param h handle to open file
463 * @param buffer the data to write
464 * @param n number of bytes to write
465 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
468 GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h, const void *buffer,
471 const char *src = buffer;
476 return GNUNET_SYSERR;
480 /* first, just use buffer */
481 min = h->size - h->have;
484 memcpy (&h->buffer[h->have], &src[pos], min);
488 return GNUNET_OK; /* done */
489 GNUNET_assert (h->have == h->size);
490 if (GNUNET_OK != GNUNET_BIO_flush (h))
491 return GNUNET_SYSERR; /* error */
493 while (pos < n); /* should always be true */
500 * Write a string to a file.
502 * @param h handle to open file
503 * @param s string to write (can be NULL)
504 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
507 GNUNET_BIO_write_string (struct GNUNET_BIO_WriteHandle *h,
512 slen = (uint32_t) ((s == NULL) ? 0 : strlen (s) + 1);
513 if (GNUNET_OK != GNUNET_BIO_write_int32 (h, slen))
514 return GNUNET_SYSERR;
516 return GNUNET_BIO_write (h, s, slen - 1);
522 * Write metadata container to a file.
524 * @param h handle to open file
525 * @param m metadata to write
526 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
529 GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h,
530 const struct GNUNET_CONTAINER_MetaData *m)
536 return GNUNET_BIO_write_int32 (h, 0);
539 GNUNET_CONTAINER_meta_data_serialize (m, &buf, MAX_META_DATA,
540 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
544 return GNUNET_SYSERR;
546 if ((GNUNET_OK != GNUNET_BIO_write_int32 (h, (uint32_t) size)) ||
547 (GNUNET_OK != GNUNET_BIO_write (h, buf, size)))
550 return GNUNET_SYSERR;
558 * Write an (u)int32_t.
560 * @param h hande to open file
561 * @param i 32-bit integer to write
562 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
565 GNUNET_BIO_write_int32 (struct GNUNET_BIO_WriteHandle *h,
571 return GNUNET_BIO_write (h, &big, sizeof (int32_t));
576 * Write an (u)int64_t.
578 * @param h hande to open file
579 * @param i 64-bit integer to write
580 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
583 GNUNET_BIO_write_int64 (struct GNUNET_BIO_WriteHandle *h,
588 big = GNUNET_htonll (i);
589 return GNUNET_BIO_write (h, &big, sizeof (int64_t));