2 This file is part of GNUnet.
3 (C) 2006, 2009 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 2, 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__)
31 #define BIO_BUFFER_SIZE 65536
33 #define MAX_META_DATA (1024 * 1024)
36 * Handle for buffered reading.
38 struct GNUNET_BIO_ReadHandle
40 struct GNUNET_DISK_FileHandle *fd;
50 * Open a file for reading.
52 * @param fn file name to be opened
53 * @return IO handle on success, NULL on error
55 struct GNUNET_BIO_ReadHandle *
56 GNUNET_BIO_read_open (const char *fn)
58 struct GNUNET_DISK_FileHandle *fd;
59 struct GNUNET_BIO_ReadHandle *h;
61 fd = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE);
64 h = GNUNET_malloc (sizeof (struct GNUNET_BIO_ReadHandle) + BIO_BUFFER_SIZE);
65 h->buffer = (char *) &h[1];
66 h->size = BIO_BUFFER_SIZE;
73 * Close an open file. Reports if any errors reading
74 * from the file were encountered.
76 * @param h file handle
77 * @param emsg set to the error message
78 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
81 GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h, char **emsg)
85 err = (NULL == h->emsg) ? GNUNET_OK : GNUNET_SYSERR;
89 GNUNET_free_non_null (h->emsg);
90 GNUNET_DISK_file_close (h->fd);
97 * Read the contents of a binary file into a buffer.
99 * @param h handle to an open file
100 * @param what describes what is being read (for error message creation)
101 * @param result the buffer to write the result to
102 * @param len the number of bytes to read
103 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
106 GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h, const char *what,
107 void *result, size_t len)
115 return GNUNET_SYSERR;
119 /* first, use buffer */
120 min = h->have - h->pos;
125 memcpy (&dst[pos], &h->buffer[h->pos], min);
130 return GNUNET_OK; /* done! */
131 GNUNET_assert (h->have == h->pos);
133 ret = GNUNET_DISK_file_read (h->fd, h->buffer, h->size);
136 GNUNET_asprintf (&h->emsg, _("Error reading `%s': %s"), what,
138 return GNUNET_SYSERR;
142 GNUNET_asprintf (&h->emsg, _("Error reading `%s': %s"), what,
144 return GNUNET_SYSERR;
149 while (pos < len); /* should always be true */
155 * Read the contents of a binary file into a buffer.
157 * @param h handle to an open file
158 * @param file name of the source file
159 * @param line line number in the source file
160 * @param result the buffer to write the result to
161 * @param len the number of bytes to read
162 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
165 GNUNET_BIO_read_fn (struct GNUNET_BIO_ReadHandle *h, const char *file, int line,
166 void *result, size_t len)
170 GNUNET_snprintf (what, sizeof (what), "%s:%d", file, line);
171 return GNUNET_BIO_read (h, what, result, len);
176 * Read 0-terminated string from a file.
178 * @param h handle to an open file
179 * @param what describes what is being read (for error message creation)
180 * @param result the buffer to store a pointer to the (allocated) string to
181 * (note that *result could be set to NULL as well)
182 * @param maxLen maximum allowed length for the string
183 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
186 GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h, const char *what,
187 char **result, size_t maxLen)
192 if (GNUNET_OK != GNUNET_BIO_read_int32 (h, &big))
194 GNUNET_free_non_null (h->emsg);
195 GNUNET_asprintf (&h->emsg, _("Error reading length of string `%s'"), what);
196 return GNUNET_SYSERR;
205 GNUNET_asprintf (&h->emsg, _("String `%s' longer than allowed (%u > %u)"),
207 return GNUNET_SYSERR;
209 buf = GNUNET_malloc (big);
214 if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, big))
218 return GNUNET_SYSERR;
225 * Read metadata container 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) metadata
230 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
233 GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h, const char *what,
234 struct GNUNET_CONTAINER_MetaData **result)
238 struct GNUNET_CONTAINER_MetaData *meta;
240 if (GNUNET_BIO_read_int32 (h, (int32_t *) & size) != GNUNET_OK)
241 return GNUNET_SYSERR;
247 if (size > MAX_META_DATA)
249 GNUNET_asprintf (&h->emsg,
250 _("Serialized metadata `%s' larger than allowed (%u>%u)"),
251 what, size, MAX_META_DATA);
252 return GNUNET_SYSERR;
254 buf = GNUNET_malloc (size);
255 if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, size))
258 return GNUNET_SYSERR;
260 meta = GNUNET_CONTAINER_meta_data_deserialize (buf, size);
264 GNUNET_asprintf (&h->emsg, _("Metadata `%s' failed to deserialize"), what);
265 return GNUNET_SYSERR;
274 * Read an (u)int32_t.
276 * @param h hande to open file
277 * @param file name of the source file
278 * @param line line number in the source file
279 * @param i address of 32-bit integer to read
280 * @return GNUNET_OK on success, GNUNET_SYSERR on error
283 GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h, const char *file,
284 int line, int32_t * i)
288 if (GNUNET_OK != GNUNET_BIO_read_fn (h, file, line, &big, sizeof (int32_t)))
289 return GNUNET_SYSERR;
296 * Read an (u)int64_t.
298 * @param h hande to open file
299 * @param file name of the source file
300 * @param line line number in the source file
301 * @param i address of 64-bit integer to read
302 * @return GNUNET_OK on success, GNUNET_SYSERR on error
305 GNUNET_BIO_read_int64__ (struct GNUNET_BIO_ReadHandle *h, const char *file,
306 int line, int64_t * i)
310 if (GNUNET_OK != GNUNET_BIO_read_fn (h, file, line, &big, sizeof (int64_t)))
311 return GNUNET_SYSERR;
312 *i = GNUNET_ntohll (big);
318 * Handle for buffered writing.
320 struct GNUNET_BIO_WriteHandle
322 struct GNUNET_DISK_FileHandle *fd;
330 * Open a file for writing.
332 * @param fn file name to be opened
333 * @return IO handle on success, NULL on error
335 struct GNUNET_BIO_WriteHandle *
336 GNUNET_BIO_write_open (const char *fn)
338 struct GNUNET_DISK_FileHandle *fd;
339 struct GNUNET_BIO_WriteHandle *h;
341 fd = GNUNET_DISK_file_open (fn,
342 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE
343 | GNUNET_DISK_OPEN_CREATE,
344 GNUNET_DISK_PERM_USER_READ |
345 GNUNET_DISK_PERM_USER_WRITE);
348 h = GNUNET_malloc (sizeof (struct GNUNET_BIO_WriteHandle) + BIO_BUFFER_SIZE);
349 h->buffer = (char *) &h[1];
350 h->size = BIO_BUFFER_SIZE;
357 * Close an open file for writing.
359 * @param h file handle
360 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
363 GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h)
368 if ( (NULL != h->fd) && (GNUNET_OK == (ret = GNUNET_BIO_flush (h))) )
369 GNUNET_DISK_file_close (h->fd);
376 * Force a buffered writer to flush its buffer
378 * @param h the writer handle
379 * @return GNUNET_OK upon success. Upon failure GNUNET_SYSERR is returned and
383 GNUNET_BIO_flush (struct GNUNET_BIO_WriteHandle *h)
387 ret = GNUNET_DISK_file_write (h->fd, h->buffer, h->have);
390 GNUNET_DISK_file_close (h->fd);
392 return GNUNET_SYSERR; /* error */
400 * Write a buffer to a file.
402 * @param h handle to open file
403 * @param buffer the data to write
404 * @param n number of bytes to write
405 * @return GNUNET_OK on success, GNUNET_SYSERR on error
408 GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h, const void *buffer,
411 const char *src = buffer;
416 return GNUNET_SYSERR;
420 /* first, just use buffer */
421 min = h->size - h->have;
424 memcpy (&h->buffer[h->have], &src[pos], min);
428 return GNUNET_OK; /* done */
429 GNUNET_assert (h->have == h->size);
430 if (GNUNET_OK != GNUNET_BIO_flush (h))
431 return GNUNET_SYSERR; /* error */
433 while (pos < n); /* should always be true */
440 * Write a string to a file.
442 * @param h handle to open file
443 * @param s string to write (can be NULL)
444 * @return GNUNET_OK on success, GNUNET_SYSERR on error
447 GNUNET_BIO_write_string (struct GNUNET_BIO_WriteHandle *h, const char *s)
451 slen = (uint32_t) ((s == NULL) ? 0 : strlen (s) + 1);
452 if (GNUNET_OK != GNUNET_BIO_write_int32 (h, slen))
453 return GNUNET_SYSERR;
455 return GNUNET_BIO_write (h, s, slen - 1);
461 * Write metadata container to a file.
463 * @param h handle to open file
464 * @param m metadata to write
465 * @return GNUNET_OK on success, GNUNET_SYSERR on error
468 GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h,
469 const struct GNUNET_CONTAINER_MetaData *m)
475 return GNUNET_BIO_write_int32 (h, 0);
478 GNUNET_CONTAINER_meta_data_serialize (m, &buf, MAX_META_DATA,
479 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
483 return GNUNET_SYSERR;
485 if ((GNUNET_OK != GNUNET_BIO_write_int32 (h, (uint32_t) size)) ||
486 (GNUNET_OK != GNUNET_BIO_write (h, buf, size)))
489 return GNUNET_SYSERR;
497 * Write an (u)int32_t.
499 * @param h hande to open file
500 * @param i 32-bit integer to write
501 * @return GNUNET_OK on success, GNUNET_SYSERR on error
504 GNUNET_BIO_write_int32 (struct GNUNET_BIO_WriteHandle *h, int32_t i)
509 return GNUNET_BIO_write (h, &big, sizeof (int32_t));
514 * Write an (u)int64_t.
516 * @param h hande to open file
517 * @param i 64-bit integer to write
518 * @return GNUNET_OK on success, GNUNET_SYSERR on error
521 GNUNET_BIO_write_int64 (struct GNUNET_BIO_WriteHandle *h, int64_t i)
525 big = GNUNET_htonll (i);
526 return GNUNET_BIO_write (h, &big, sizeof (int64_t));