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 BIO_BUFFER_SIZE 65536
31 #define MAX_META_DATA (1024 * 1024)
34 * Handle for buffered reading.
36 struct GNUNET_BIO_ReadHandle
38 struct GNUNET_DISK_FileHandle *fd;
48 * Open a file for reading.
50 * @param fn file name to be opened
51 * @return IO handle on success, NULL on error
53 struct GNUNET_BIO_ReadHandle *
54 GNUNET_BIO_read_open (const char *fn)
56 struct GNUNET_DISK_FileHandle *fd;
57 struct GNUNET_BIO_ReadHandle *h;
59 fd = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ,
60 GNUNET_DISK_PERM_NONE);
63 h = GNUNET_malloc (sizeof (struct GNUNET_BIO_ReadHandle) + BIO_BUFFER_SIZE);
64 h->buffer = (char *) &h[1];
65 h->size = BIO_BUFFER_SIZE;
72 * Close an open file. Reports if any errors reading
73 * from the file were encountered.
75 * @param h file handle
76 * @param emsg set to the error message
77 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
80 GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h, char **emsg)
83 GNUNET_DISK_file_close (h->fd);
85 return (NULL == *emsg) ? GNUNET_OK : GNUNET_SYSERR;
90 * Read the contents of a binary file into a buffer.
92 * @param h handle to an open file
93 * @param what describes what is being read (for error message creation)
94 * @param result the buffer to write the result to
95 * @param len the number of bytes to read
96 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
99 GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h,
100 const char *what, void *result, size_t len)
108 return GNUNET_SYSERR;
112 /* first, use buffer */
113 min = h->have - h->pos;
118 memcpy (&dst[pos], &h->buffer[h->pos], min);
123 return GNUNET_OK; /* done! */
124 GNUNET_assert (h->have == h->pos);
126 ret = GNUNET_DISK_file_read (h->fd, h->buffer, h->size);
129 GNUNET_asprintf (&h->emsg,
130 _("Error reading `%s': %s"),
131 what, STRERROR (errno));
132 return GNUNET_SYSERR;
136 GNUNET_asprintf (&h->emsg,
137 _("Error reading `%s': %s"),
138 what, _("End of file"));
139 return GNUNET_SYSERR;
144 while (pos < len); /* should always be true */
150 * Read the contents of a binary file into a buffer.
152 * @param h handle to an open file
153 * @param file name of the source file
154 * @param line line number in the source file
155 * @param result the buffer to write the result to
156 * @param len the number of bytes to read
157 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
159 int GNUNET_BIO_read_fn (struct GNUNET_BIO_ReadHandle *h,
160 const char *file, int line,
165 GNUNET_snprintf (what,
169 return GNUNET_BIO_read (h, what, result, len);
174 * Read 0-terminated string from a file.
176 * @param h handle to an open file
177 * @param what describes what is being read (for error message creation)
178 * @param result the buffer to store a pointer to the (allocated) string to
179 * (note that *result could be set to NULL as well)
180 * @param maxLen maximum allowed length for the string
181 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
184 GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h,
185 const char *what, char **result, size_t maxLen)
190 if (GNUNET_OK != GNUNET_BIO_read_int32 (h, &big))
191 return GNUNET_SYSERR;
199 GNUNET_asprintf (&h->emsg,
200 _("String `%s' longer than allowed (%u > %u)"),
202 return GNUNET_SYSERR;
204 buf = GNUNET_malloc (big);
209 if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, big))
213 return GNUNET_SYSERR;
220 * Read metadata container from a file.
222 * @param h handle to an open file
223 * @param what describes what is being read (for error message creation)
224 * @param result the buffer to store a pointer to the (allocated) metadata
225 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
228 GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h,
230 struct GNUNET_CONTAINER_MetaData **result)
234 struct GNUNET_CONTAINER_MetaData *meta;
236 if (GNUNET_BIO_read_int32 (h, (int32_t *) &size) != GNUNET_OK)
237 return GNUNET_SYSERR;
238 if (size > MAX_META_DATA)
240 GNUNET_asprintf (&h->emsg,
241 _("Serialized metadata `%s' larger than allowed (%u>%u)"),
242 what, size, MAX_META_DATA);
243 return GNUNET_SYSERR;
245 buf = GNUNET_malloc (size);
246 if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, size))
249 return GNUNET_SYSERR;
251 meta = GNUNET_CONTAINER_meta_data_deserialize (buf, size);
255 GNUNET_asprintf (&h->emsg,
256 _("Metadata `%s' failed to deserialize"), what);
257 return GNUNET_SYSERR;
266 * Read an (u)int32_t.
268 * @param h hande to open file
269 * @param file name of the source file
270 * @param line line number in the source file
271 * @param i address of 32-bit integer to read
272 * @return GNUNET_OK on success, GNUNET_SYSERR on error
275 GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h,
282 if (GNUNET_OK != GNUNET_BIO_read_fn (h, file, line, &big, sizeof (int32_t)))
283 return GNUNET_SYSERR;
290 * Read an (u)int64_t.
292 * @param h hande to open file
293 * @param file name of the source file
294 * @param line line number in the source file
295 * @param i address of 64-bit integer to read
296 * @return GNUNET_OK on success, GNUNET_SYSERR on error
299 GNUNET_BIO_read_int64__ (struct GNUNET_BIO_ReadHandle *h,
306 if (GNUNET_OK != GNUNET_BIO_read_fn (h, file, line, &big, sizeof (int64_t)))
307 return GNUNET_SYSERR;
308 *i = GNUNET_ntohll (big);
314 * Handle for buffered writing.
316 struct GNUNET_BIO_WriteHandle
318 struct GNUNET_DISK_FileHandle *fd;
326 * Open a file for writing.
328 * @param fn file name to be opened
329 * @return IO handle on success, NULL on error
331 struct GNUNET_BIO_WriteHandle *
332 GNUNET_BIO_write_open (const char *fn)
334 struct GNUNET_DISK_FileHandle *fd;
335 struct GNUNET_BIO_WriteHandle *h;
337 fd = GNUNET_DISK_file_open (fn,
338 GNUNET_DISK_OPEN_WRITE |
339 GNUNET_DISK_OPEN_TRUNCATE |
340 GNUNET_DISK_OPEN_CREATE,
341 GNUNET_DISK_PERM_USER_READ |
342 GNUNET_DISK_PERM_USER_WRITE);
346 GNUNET_malloc (sizeof (struct GNUNET_BIO_WriteHandle) + BIO_BUFFER_SIZE);
347 h->buffer = (char *) &h[1];
348 h->size = BIO_BUFFER_SIZE;
356 * Close an open file for writing.
358 * @param h file handle
359 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
362 GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h)
373 wrt = GNUNET_DISK_file_write (h->fd, h->buffer, h->have);
378 GNUNET_DISK_file_close (h->fd);
386 * Write a buffer to a file.
388 * @param h handle to open file
389 * @param buffer the data to write
390 * @param n number of bytes to write
391 * @return GNUNET_OK on success, GNUNET_SYSERR on error
394 GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h,
395 const void *buffer, size_t n)
397 const char *src = buffer;
403 return GNUNET_SYSERR;
407 /* first, just use buffer */
408 min = h->size - h->have;
411 memcpy (&h->buffer[h->have], &src[pos], min);
415 return GNUNET_OK; /* done */
416 GNUNET_assert (h->have == h->size);
417 ret = GNUNET_DISK_file_write (h->fd, h->buffer, h->size);
420 GNUNET_DISK_file_close (h->fd);
422 return GNUNET_SYSERR; /* error */
426 while (pos < n); /* should always be true */
433 * Write a string to a file.
435 * @param h handle to open file
436 * @param s string to write (can be NULL)
437 * @return GNUNET_OK on success, GNUNET_SYSERR on error
440 GNUNET_BIO_write_string (struct GNUNET_BIO_WriteHandle *h, const char *s)
444 slen = (uint32_t) ((s == NULL) ? 0 : strlen (s) + 1);
445 if (GNUNET_OK != GNUNET_BIO_write_int32 (h, slen))
446 return GNUNET_SYSERR;
448 return GNUNET_BIO_write (h, s, slen - 1);
454 * Write metadata container to a file.
456 * @param h handle to open file
457 * @param m metadata to write
458 * @return GNUNET_OK on success, GNUNET_SYSERR on error
461 GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h,
462 const struct GNUNET_CONTAINER_MetaData *m)
468 size = GNUNET_CONTAINER_meta_data_serialize (m,
471 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
475 return GNUNET_SYSERR;
477 if ( (GNUNET_OK != GNUNET_BIO_write_int32 (h, (uint32_t) size)) ||
478 (GNUNET_OK != GNUNET_BIO_write (h, buf, size)) )
481 return GNUNET_SYSERR;
489 * Write an (u)int32_t.
491 * @param h hande to open file
492 * @param i address of 32-bit integer to write
493 * @return GNUNET_OK on success, GNUNET_SYSERR on error
496 GNUNET_BIO_write_int32 (struct GNUNET_BIO_WriteHandle *h, int32_t i)
500 return GNUNET_BIO_write (h, &big, sizeof (int32_t));
505 * Write an (u)int64_t.
507 * @param h hande to open file
508 * @param i address of 64-bit integer to write
509 * @return GNUNET_OK on success, GNUNET_SYSERR on error
512 GNUNET_BIO_write_int64 (struct GNUNET_BIO_WriteHandle *h, int64_t i)
515 big = GNUNET_htonll (i);
516 return GNUNET_BIO_write (h, &big, sizeof (int64_t));