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);
62 h = GNUNET_malloc (sizeof(struct GNUNET_BIO_ReadHandle) + BIO_BUFFER_SIZE);
63 h->buffer = (char*) &h[1];
64 h->size = BIO_BUFFER_SIZE;
71 * Close an open file. Reports if any errors reading
72 * from the file were encountered.
74 * @param h file handle
75 * @param emsg set to the error message
76 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
78 int GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h,
82 GNUNET_DISK_file_close (h->fd);
84 return (NULL == *emsg) ? GNUNET_OK : GNUNET_SYSERR;
89 * Read the contents of a binary file into a buffer.
91 * @param h handle to an open file
92 * @param what describes what is being read (for error message creation)
93 * @param result the buffer to write the result to
94 * @param len the number of bytes to read
95 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
97 int GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h,
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,
131 GNUNET_asprintf (&h->emsg,
132 _("Error reading `%s': %s"),
135 return GNUNET_SYSERR;
139 GNUNET_asprintf (&h->emsg,
140 _("Error reading `%s': %s"),
143 return GNUNET_SYSERR;
148 while (pos < len); /* should always be true */
154 * Read 0-terminated string from a file.
156 * @param h handle to an open file
157 * @param what describes what is being read (for error message creation)
158 * @param result the buffer to store a pointer to the (allocated) string to
159 * (note that *result could be set to NULL as well)
160 * @param maxLen maximum allowed length for the string
161 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
163 int GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h,
171 if (! GNUNET_BIO_read_int32 (h, &big))
172 return GNUNET_SYSERR;
180 GNUNET_asprintf (&h->emsg,
181 _("String `%s' longer than allowed (%u > %u)"),
185 return GNUNET_SYSERR;
187 buf = GNUNET_malloc (big);
189 if (big != GNUNET_BIO_read (h, what, buf, big))
192 return GNUNET_SYSERR;
200 * Read metadata container from a file.
202 * @param h handle to an open file
203 * @param what describes what is being read (for error message creation)
204 * @param result the buffer to store a pointer to the (allocated) metadata
205 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
207 int GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h,
209 struct GNUNET_CONTAINER_MetaData **result)
213 struct GNUNET_CONTAINER_MetaData *meta;
215 if (GNUNET_BIO_read_int32__ (h,
217 (int32_t*) &size) != GNUNET_OK)
218 return GNUNET_SYSERR;
219 if (size > MAX_META_DATA)
221 GNUNET_asprintf (&h->emsg,
222 _("Serialized metadata `%s' larger than allowed (%u > %u)"),
226 return GNUNET_SYSERR;
228 buf = GNUNET_malloc (size);
230 GNUNET_BIO_read (h, what, buf, size))
233 return GNUNET_SYSERR;
235 meta = GNUNET_CONTAINER_meta_data_deserialize (buf, size);
239 GNUNET_asprintf (&h->emsg,
240 _("Metadata `%s' failed to deserialize"),
242 return GNUNET_SYSERR;
251 * Read an (u)int32_t.
253 * @param h hande to open file
254 * @param what describes what is being read (for error message creation)
255 * @param i address of 32-bit integer to read
256 * @return GNUNET_OK on success, GNUNET_SYSERR on error
258 int GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h,
264 if (sizeof (int32_t) !=
269 return GNUNET_SYSERR;
276 * Read an (u)int64_t.
278 * @param h hande to open file
279 * @param what describes what is being read (for error message creation)
280 * @param i address of 64-bit integer to read
281 * @return GNUNET_OK on success, GNUNET_SYSERR on error
283 int GNUNET_BIO_read_int64__ (struct GNUNET_BIO_ReadHandle *h,
289 if (sizeof (int64_t) !=
294 return GNUNET_SYSERR;
295 *i = GNUNET_ntohll (big);
301 * Handle for buffered writing.
303 struct GNUNET_BIO_WriteHandle
305 struct GNUNET_DISK_FileHandle *fd;
313 * Open a file for writing.
315 * @param fn file name to be opened
316 * @return IO handle on success, NULL on error
318 struct GNUNET_BIO_WriteHandle *GNUNET_BIO_write_open (const char *fn)
320 struct GNUNET_DISK_FileHandle *fd;
321 struct GNUNET_BIO_WriteHandle *h;
323 fd = GNUNET_DISK_file_open (fn,
324 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE | GNUNET_DISK_OPEN_CREATE,
325 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
328 h = GNUNET_malloc (sizeof(struct GNUNET_BIO_WriteHandle) + BIO_BUFFER_SIZE);
329 h->buffer = (char*) &h[1];
330 h->size = BIO_BUFFER_SIZE;
337 * Close an open file for writing.
339 * @param h file handle
340 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
342 int GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h)
352 GNUNET_DISK_file_close (h->fd);
361 * Write a buffer to a file.
363 * @param h handle to open file
364 * @param buffer the data to write
365 * @param n number of bytes to write
366 * @return GNUNET_OK on success, GNUNET_SYSERR on error
368 int GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h,
372 const char *src = buffer;
378 return GNUNET_SYSERR;
382 /* first, just use buffer */
383 min = h->size - h->have;
386 memcpy (&h->buffer[h->have], &src[pos], min);
390 return GNUNET_OK; /* done */
391 GNUNET_assert (h->have == h->size);
392 ret = GNUNET_DISK_file_write (h->fd, h->buffer, h->size);
395 GNUNET_DISK_file_close (h->fd);
397 return GNUNET_SYSERR; /* error */
401 while (pos < n); /* should always be true */
407 * Write a string to a file.
409 * @param h handle to open file
410 * @param s string to write (can be NULL)
411 * @return GNUNET_OK on success, GNUNET_SYSERR on error
413 int GNUNET_BIO_write_string (struct GNUNET_BIO_WriteHandle *h,
418 slen = (uint32_t) ((s == NULL) ? 0 : strlen(s) + 1);
420 GNUNET_BIO_write_int32 (h, slen))
421 return GNUNET_SYSERR;
423 return GNUNET_BIO_write (h, s, slen - 1);
429 * Write metadata container to a file.
431 * @param h handle to open file
432 * @param m metadata to write
433 * @return GNUNET_OK on success, GNUNET_SYSERR on error
435 int GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h,
436 const struct GNUNET_CONTAINER_MetaData *m)
441 size = GNUNET_CONTAINER_meta_data_get_serialized_size (m,
442 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL
444 GNUNET_CONTAINER_META_DATA_SERIALIZE_NO_COMPRESS);
445 if (size > MAX_META_DATA)
446 size = MAX_META_DATA;
447 buf = GNUNET_malloc (size);
448 GNUNET_CONTAINER_meta_data_serialize (m,
451 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART |
452 GNUNET_CONTAINER_META_DATA_SERIALIZE_NO_COMPRESS);
454 GNUNET_BIO_write_int32 (h, size)) ||
456 GNUNET_BIO_write (h, buf, size)) )
459 return GNUNET_SYSERR;
467 * Write an (u)int32_t.
469 * @param h hande to open file
470 * @param i address of 32-bit integer to write
471 * @return GNUNET_OK on success, GNUNET_SYSERR on error
473 int GNUNET_BIO_write_int32 (struct GNUNET_BIO_WriteHandle *h,
478 return GNUNET_BIO_write (h, &big, sizeof (int32_t));
483 * Write an (u)int64_t.
485 * @param h hande to open file
486 * @param i address of 64-bit integer to write
487 * @return GNUNET_OK on success, GNUNET_SYSERR on error
489 int GNUNET_BIO_write_int64 (struct GNUNET_BIO_WriteHandle *h,
493 big = GNUNET_htonll (i);
494 return GNUNET_BIO_write (h, &big, sizeof (int64_t));