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);
61 h = GNUNET_malloc (sizeof(struct GNUNET_BIO_ReadHandle) + BIO_BUFFER_SIZE);
62 h->buffer = (char*) &h[1];
63 h->size = BIO_BUFFER_SIZE;
70 * Close an open file. Reports if any errors reading
71 * from the file were encountered.
73 * @param h file handle
74 * @param emsg set to the error message
75 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
77 int GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h,
81 GNUNET_DISK_file_close (h->fd);
83 return (NULL == *emsg) ? GNUNET_OK : GNUNET_SYSERR;
88 * Read the contents of a binary file into a buffer.
90 * @param h handle to an open file
91 * @param what describes what is being read (for error message creation)
92 * @param result the buffer to write the result to
93 * @param len the number of bytes to read
94 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
96 int GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h,
107 return GNUNET_SYSERR;
111 /* first, use buffer */
112 min = h->have - h->pos;
117 memcpy (&dst[pos], &h->buffer[h->pos], min);
122 return GNUNET_OK; /* done! */
123 GNUNET_assert (h->have == h->pos);
125 ret = GNUNET_DISK_file_read (h->fd,
130 GNUNET_asprintf (&h->emsg,
131 _("Error reading `%s': %s"),
134 return GNUNET_SYSERR;
138 GNUNET_asprintf (&h->emsg,
139 _("Error reading `%s': %s"),
142 return GNUNET_SYSERR;
147 while (pos < len); /* should always be true */
153 * Read 0-terminated string from a file.
155 * @param h handle to an open file
156 * @param what describes what is being read (for error message creation)
157 * @param result the buffer to store a pointer to the (allocated) string to
158 * (note that *result could be set to NULL as well)
159 * @param maxLen maximum allowed length for the string
160 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
162 int GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h,
170 if (GNUNET_OK != GNUNET_BIO_read_int32 (h, &big))
171 return GNUNET_SYSERR;
179 GNUNET_asprintf (&h->emsg,
180 _("String `%s' longer than allowed (%u > %u)"),
184 return GNUNET_SYSERR;
186 buf = GNUNET_malloc (big);
188 if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, big))
191 return GNUNET_SYSERR;
199 * Read metadata container from a file.
201 * @param h handle to an open file
202 * @param what describes what is being read (for error message creation)
203 * @param result the buffer to store a pointer to the (allocated) metadata
204 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
206 int GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h,
208 struct GNUNET_CONTAINER_MetaData **result)
212 struct GNUNET_CONTAINER_MetaData *meta;
214 if (GNUNET_BIO_read_int32__ (h,
216 (int32_t*) &size) != GNUNET_OK)
217 return GNUNET_SYSERR;
218 if (size > MAX_META_DATA)
220 GNUNET_asprintf (&h->emsg,
221 _("Serialized metadata `%s' larger than allowed (%u > %u)"),
225 return GNUNET_SYSERR;
227 buf = GNUNET_malloc (size);
229 GNUNET_BIO_read (h, what, buf, size))
232 return GNUNET_SYSERR;
234 meta = GNUNET_CONTAINER_meta_data_deserialize (buf, size);
238 GNUNET_asprintf (&h->emsg,
239 _("Metadata `%s' failed to deserialize"),
241 return GNUNET_SYSERR;
250 * Read an (u)int32_t.
252 * @param h hande to open file
253 * @param what describes what is being read (for error message creation)
254 * @param i address of 32-bit integer to read
255 * @return GNUNET_OK on success, GNUNET_SYSERR on error
257 int GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h,
268 return GNUNET_SYSERR;
275 * Read an (u)int64_t.
277 * @param h hande to open file
278 * @param what describes what is being read (for error message creation)
279 * @param i address of 64-bit integer to read
280 * @return GNUNET_OK on success, GNUNET_SYSERR on error
282 int GNUNET_BIO_read_int64__ (struct GNUNET_BIO_ReadHandle *h,
293 return GNUNET_SYSERR;
294 *i = GNUNET_ntohll (big);
300 * Handle for buffered writing.
302 struct GNUNET_BIO_WriteHandle
304 struct GNUNET_DISK_FileHandle *fd;
312 * Open a file for writing.
314 * @param fn file name to be opened
315 * @return IO handle on success, NULL on error
317 struct GNUNET_BIO_WriteHandle *GNUNET_BIO_write_open (const char *fn)
319 struct GNUNET_DISK_FileHandle *fd;
320 struct GNUNET_BIO_WriteHandle *h;
322 fd = GNUNET_DISK_file_open (fn,
323 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE | GNUNET_DISK_OPEN_CREATE,
324 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
325 h = GNUNET_malloc (sizeof(struct GNUNET_BIO_WriteHandle) + BIO_BUFFER_SIZE);
326 h->buffer = (char*) &h[1];
327 h->size = BIO_BUFFER_SIZE;
335 * Close an open file for writing.
337 * @param h file handle
338 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
340 int GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h)
351 wrt = GNUNET_DISK_file_write (h->fd, h->buffer, h->have);
356 GNUNET_DISK_file_close (h->fd);
364 * Write a buffer to a file.
366 * @param h handle to open file
367 * @param buffer the data to write
368 * @param n number of bytes to write
369 * @return GNUNET_OK on success, GNUNET_SYSERR on error
371 int GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h,
375 const char *src = buffer;
381 return GNUNET_SYSERR;
385 /* first, just use buffer */
386 min = h->size - h->have;
389 memcpy (&h->buffer[h->have], &src[pos], min);
393 return GNUNET_OK; /* done */
394 GNUNET_assert (h->have == h->size);
395 ret = GNUNET_DISK_file_write (h->fd, h->buffer, h->size);
398 GNUNET_DISK_file_close (h->fd);
400 return GNUNET_SYSERR; /* error */
404 while (pos < n); /* should always be true */
410 * Write a string to a file.
412 * @param h handle to open file
413 * @param s string to write (can be NULL)
414 * @return GNUNET_OK on success, GNUNET_SYSERR on error
416 int GNUNET_BIO_write_string (struct GNUNET_BIO_WriteHandle *h,
421 slen = (uint32_t) ((s == NULL) ? 0 : strlen(s) + 1);
423 GNUNET_BIO_write_int32 (h, slen))
424 return GNUNET_SYSERR;
426 return GNUNET_BIO_write (h, s, slen - 1);
432 * Write metadata container to a file.
434 * @param h handle to open file
435 * @param m metadata to write
436 * @return GNUNET_OK on success, GNUNET_SYSERR on error
438 int GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h,
439 const struct GNUNET_CONTAINER_MetaData *m)
444 size = GNUNET_CONTAINER_meta_data_get_serialized_size (m,
445 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL
447 GNUNET_CONTAINER_META_DATA_SERIALIZE_NO_COMPRESS);
448 if (size > MAX_META_DATA)
449 size = MAX_META_DATA;
450 buf = GNUNET_malloc (size);
451 GNUNET_CONTAINER_meta_data_serialize (m,
454 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART |
455 GNUNET_CONTAINER_META_DATA_SERIALIZE_NO_COMPRESS);
457 GNUNET_BIO_write_int32 (h, size)) ||
459 GNUNET_BIO_write (h, buf, size)) )
462 return GNUNET_SYSERR;
470 * Write an (u)int32_t.
472 * @param h hande to open file
473 * @param i address of 32-bit integer to write
474 * @return GNUNET_OK on success, GNUNET_SYSERR on error
476 int GNUNET_BIO_write_int32 (struct GNUNET_BIO_WriteHandle *h,
481 return GNUNET_BIO_write (h, &big, sizeof (int32_t));
486 * Write an (u)int64_t.
488 * @param h hande to open file
489 * @param i address of 64-bit integer to write
490 * @return GNUNET_OK on success, GNUNET_SYSERR on error
492 int GNUNET_BIO_write_int64 (struct GNUNET_BIO_WriteHandle *h,
496 big = GNUNET_htonll (i);
497 return GNUNET_BIO_write (h, &big, sizeof (int64_t));