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
79 int GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h,
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
98 int GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h,
109 return GNUNET_SYSERR;
113 /* first, use buffer */
114 min = h->have - h->pos;
119 memcpy (&dst[pos], &h->buffer[h->pos], min);
124 return GNUNET_OK; /* done! */
125 GNUNET_assert (h->have == h->pos);
127 ret = GNUNET_DISK_file_read (h->fd,
132 GNUNET_asprintf (&h->emsg,
133 _("Error reading `%s': %s"),
136 return GNUNET_SYSERR;
140 GNUNET_asprintf (&h->emsg,
141 _("Error reading `%s': %s"),
144 return GNUNET_SYSERR;
149 while (pos < len); /* should always be true */
155 * Read 0-terminated string from a file.
157 * @param h handle to an open file
158 * @param what describes what is being read (for error message creation)
159 * @param result the buffer to store a pointer to the (allocated) string to
160 * (note that *result could be set to NULL as well)
161 * @param maxLen maximum allowed length for the string
162 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
164 int GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h,
172 if (GNUNET_OK != GNUNET_BIO_read_int32 (h, &big))
173 return GNUNET_SYSERR;
181 GNUNET_asprintf (&h->emsg,
182 _("String `%s' longer than allowed (%u > %u)"),
186 return GNUNET_SYSERR;
188 buf = GNUNET_malloc (big);
190 if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, big))
193 return GNUNET_SYSERR;
201 * Read metadata container from a file.
203 * @param h handle to an open file
204 * @param what describes what is being read (for error message creation)
205 * @param result the buffer to store a pointer to the (allocated) metadata
206 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
208 int GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h,
210 struct GNUNET_CONTAINER_MetaData **result)
214 struct GNUNET_CONTAINER_MetaData *meta;
216 if (GNUNET_BIO_read_int32__ (h,
218 (int32_t*) &size) != GNUNET_OK)
219 return GNUNET_SYSERR;
220 if (size > MAX_META_DATA)
222 GNUNET_asprintf (&h->emsg,
223 _("Serialized metadata `%s' larger than allowed (%u > %u)"),
227 return GNUNET_SYSERR;
229 buf = GNUNET_malloc (size);
231 GNUNET_BIO_read (h, what, buf, size))
234 return GNUNET_SYSERR;
236 meta = GNUNET_CONTAINER_meta_data_deserialize (buf, size);
240 GNUNET_asprintf (&h->emsg,
241 _("Metadata `%s' failed to deserialize"),
243 return GNUNET_SYSERR;
252 * Read an (u)int32_t.
254 * @param h hande to open file
255 * @param what describes what is being read (for error message creation)
256 * @param i address of 32-bit integer to read
257 * @return GNUNET_OK on success, GNUNET_SYSERR on error
259 int GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h,
270 return GNUNET_SYSERR;
277 * Read an (u)int64_t.
279 * @param h hande to open file
280 * @param what describes what is being read (for error message creation)
281 * @param i address of 64-bit integer to read
282 * @return GNUNET_OK on success, GNUNET_SYSERR on error
284 int GNUNET_BIO_read_int64__ (struct GNUNET_BIO_ReadHandle *h,
295 return GNUNET_SYSERR;
296 *i = GNUNET_ntohll (big);
302 * Handle for buffered writing.
304 struct GNUNET_BIO_WriteHandle
306 struct GNUNET_DISK_FileHandle *fd;
314 * Open a file for writing.
316 * @param fn file name to be opened
317 * @return IO handle on success, NULL on error
319 struct GNUNET_BIO_WriteHandle *GNUNET_BIO_write_open (const char *fn)
321 struct GNUNET_DISK_FileHandle *fd;
322 struct GNUNET_BIO_WriteHandle *h;
324 fd = GNUNET_DISK_file_open (fn,
325 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE | GNUNET_DISK_OPEN_CREATE,
326 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
329 h = GNUNET_malloc (sizeof(struct GNUNET_BIO_WriteHandle) + BIO_BUFFER_SIZE);
330 h->buffer = (char*) &h[1];
331 h->size = BIO_BUFFER_SIZE;
338 * Close an open file for writing.
340 * @param h file handle
341 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
343 int GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h)
354 wrt = GNUNET_DISK_file_write (h->fd, h->buffer, h->have);
359 GNUNET_DISK_file_close (h->fd);
367 * Write a buffer to a file.
369 * @param h handle to open file
370 * @param buffer the data to write
371 * @param n number of bytes to write
372 * @return GNUNET_OK on success, GNUNET_SYSERR on error
374 int GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h,
378 const char *src = buffer;
384 return GNUNET_SYSERR;
388 /* first, just use buffer */
389 min = h->size - h->have;
392 memcpy (&h->buffer[h->have], &src[pos], min);
396 return GNUNET_OK; /* done */
397 GNUNET_assert (h->have == h->size);
398 ret = GNUNET_DISK_file_write (h->fd, h->buffer, h->size);
401 GNUNET_DISK_file_close (h->fd);
403 return GNUNET_SYSERR; /* error */
407 while (pos < n); /* should always be true */
413 * Write a string to a file.
415 * @param h handle to open file
416 * @param s string to write (can be NULL)
417 * @return GNUNET_OK on success, GNUNET_SYSERR on error
419 int GNUNET_BIO_write_string (struct GNUNET_BIO_WriteHandle *h,
424 slen = (uint32_t) ((s == NULL) ? 0 : strlen(s) + 1);
426 GNUNET_BIO_write_int32 (h, slen))
427 return GNUNET_SYSERR;
429 return GNUNET_BIO_write (h, s, slen - 1);
435 * Write metadata container to a file.
437 * @param h handle to open file
438 * @param m metadata to write
439 * @return GNUNET_OK on success, GNUNET_SYSERR on error
441 int GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h,
442 const struct GNUNET_CONTAINER_MetaData *m)
447 size = GNUNET_CONTAINER_meta_data_get_serialized_size (m,
448 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL
450 GNUNET_CONTAINER_META_DATA_SERIALIZE_NO_COMPRESS);
451 if (size > MAX_META_DATA)
452 size = MAX_META_DATA;
453 buf = GNUNET_malloc (size);
454 GNUNET_CONTAINER_meta_data_serialize (m,
457 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART |
458 GNUNET_CONTAINER_META_DATA_SERIALIZE_NO_COMPRESS);
460 GNUNET_BIO_write_int32 (h, size)) ||
462 GNUNET_BIO_write (h, buf, size)) )
465 return GNUNET_SYSERR;
473 * Write an (u)int32_t.
475 * @param h hande to open file
476 * @param i address of 32-bit integer to write
477 * @return GNUNET_OK on success, GNUNET_SYSERR on error
479 int GNUNET_BIO_write_int32 (struct GNUNET_BIO_WriteHandle *h,
484 return GNUNET_BIO_write (h, &big, sizeof (int32_t));
489 * Write an (u)int64_t.
491 * @param h hande to open file
492 * @param i address of 64-bit integer to write
493 * @return GNUNET_OK on success, GNUNET_SYSERR on error
495 int GNUNET_BIO_write_int64 (struct GNUNET_BIO_WriteHandle *h,
499 big = GNUNET_htonll (i);
500 return GNUNET_BIO_write (h, &big, sizeof (int64_t));