2 This file is part of GNUnet.
3 Copyright (C) 2006, 2009, 2013 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @brief functions for buffering IO
23 * @author Christian Grothoff
26 #include "gnunet_util_lib.h"
28 #define LOG(kind, ...) GNUNET_log_from(kind, "util-bio", __VA_ARGS__)
32 * Assumed maximum path length (for source file names).
39 * Size for I/O buffers.
41 #define BIO_BUFFER_SIZE 65536
44 * Maximum size allowed for meta data written/read from disk.
45 * File-sharing limits to 64k, so this should be rather generous.
47 #define MAX_META_DATA (1024 * 1024)
51 * Handle for buffered reading.
53 struct GNUNET_BIO_ReadHandle {
55 * Underlying file abstraction.
57 struct GNUNET_DISK_FileHandle *fd;
60 * Error message, NULL if there were no errors.
65 * I/O buffer. Allocated at the end of the struct, do not free!
70 * Number of bytes available in read @e buffer.
75 * Total size of @e buffer.
80 * Current read offset in @e buffer.
87 * Open a file for reading.
89 * @param fn file name to be opened
90 * @return IO handle on success, NULL on error
92 struct GNUNET_BIO_ReadHandle *
93 GNUNET_BIO_read_open(const char *fn)
95 struct GNUNET_DISK_FileHandle *fd;
96 struct GNUNET_BIO_ReadHandle *h;
98 fd = GNUNET_DISK_file_open(fn, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE);
101 h = GNUNET_malloc(sizeof(struct GNUNET_BIO_ReadHandle) + BIO_BUFFER_SIZE);
102 h->buffer = (char *)&h[1];
103 h->size = BIO_BUFFER_SIZE;
110 * Close an open file. Reports if any errors reading
111 * from the file were encountered.
113 * @param h file handle
114 * @param emsg set to the error message
115 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
118 GNUNET_BIO_read_close(struct GNUNET_BIO_ReadHandle *h, char **emsg)
122 err = (NULL == h->emsg) ? GNUNET_OK : GNUNET_SYSERR;
126 GNUNET_free_non_null(h->emsg);
127 GNUNET_DISK_file_close(h->fd);
134 * Read the contents of a binary file into a buffer.
136 * @param h handle to an open file
137 * @param what describes what is being read (for error message creation)
138 * @param result the buffer to write the result to
139 * @param len the number of bytes to read
140 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
143 GNUNET_BIO_read(struct GNUNET_BIO_ReadHandle *h,
154 return GNUNET_SYSERR;
158 /* first, use buffer */
159 min = h->have - h->pos;
164 GNUNET_memcpy(&dst[pos], &h->buffer[h->pos], min);
169 return GNUNET_OK; /* done! */
170 GNUNET_assert(((off_t)h->have) == h->pos);
172 ret = GNUNET_DISK_file_read(h->fd, h->buffer, h->size);
175 GNUNET_asprintf(&h->emsg,
176 _("Error reading `%s': %s"),
179 return GNUNET_SYSERR;
183 GNUNET_asprintf(&h->emsg,
184 _("Error reading `%s': %s"),
187 return GNUNET_SYSERR;
192 while (pos < len); /* should always be true */
198 * Read the contents of a binary file into a buffer.
200 * @param h handle to an open file
201 * @param file name of the source file
202 * @param line line number in the source file
203 * @param result the buffer to write the result to
204 * @param len the number of bytes to read
205 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
208 GNUNET_BIO_read_fn(struct GNUNET_BIO_ReadHandle *h,
214 char what[PATH_MAX + 1024];
216 GNUNET_snprintf(what, sizeof(what), "%s:%d", file, line);
217 return GNUNET_BIO_read(h, what, result, len);
222 * Read 0-terminated string from a file.
224 * @param h handle to an open file
225 * @param what describes what is being read (for error message creation)
226 * @param result the buffer to store a pointer to the (allocated) string to
227 * (note that *result could be set to NULL as well)
228 * @param max_length maximum allowed length for the string
229 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
232 GNUNET_BIO_read_string(struct GNUNET_BIO_ReadHandle *h,
240 if (GNUNET_OK != GNUNET_BIO_read_int32(h, &big))
242 GNUNET_free_non_null(h->emsg);
243 GNUNET_asprintf(&h->emsg, _("Error reading length of string `%s'"), what);
244 return GNUNET_SYSERR;
251 if (big > max_length)
253 GNUNET_asprintf(&h->emsg,
254 _("String `%s' longer than allowed (%u > %u)"),
258 return GNUNET_SYSERR;
260 buf = GNUNET_malloc(big);
265 if (GNUNET_OK != GNUNET_BIO_read(h, what, buf, big))
269 return GNUNET_SYSERR;
276 * Read metadata container from a file.
278 * @param h handle to an open file
279 * @param what describes what is being read (for error message creation)
280 * @param result the buffer to store a pointer to the (allocated) metadata
281 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
284 GNUNET_BIO_read_meta_data(struct GNUNET_BIO_ReadHandle *h,
286 struct GNUNET_CONTAINER_MetaData **result)
290 struct GNUNET_CONTAINER_MetaData *meta;
292 if (GNUNET_OK != GNUNET_BIO_read_int32(h, (int32_t *)&size))
293 return GNUNET_SYSERR;
299 if (size > MAX_META_DATA)
301 GNUNET_asprintf(&h->emsg,
302 _("Serialized metadata `%s' larger than allowed (%u>%u)"),
306 return GNUNET_SYSERR;
308 buf = GNUNET_malloc(size);
309 if (GNUNET_OK != GNUNET_BIO_read(h, what, buf, size))
312 return GNUNET_SYSERR;
314 meta = GNUNET_CONTAINER_meta_data_deserialize(buf, size);
318 GNUNET_asprintf(&h->emsg, _("Metadata `%s' failed to deserialize"), what);
319 return GNUNET_SYSERR;
328 * Read an (u)int32_t.
330 * @param h hande to open file
331 * @param file name of the source file
332 * @param line line number in the source file
333 * @param i address of 32-bit integer to read
334 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
337 GNUNET_BIO_read_int32__(struct GNUNET_BIO_ReadHandle *h,
344 if (GNUNET_OK != GNUNET_BIO_read_fn(h, file, line, &big, sizeof(int32_t)))
345 return GNUNET_SYSERR;
352 * Read an (u)int64_t.
354 * @param h hande to open file
355 * @param file name of the source file
356 * @param line line number in the source file
357 * @param i address of 64-bit integer to read
358 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
361 GNUNET_BIO_read_int64__(struct GNUNET_BIO_ReadHandle *h,
368 if (GNUNET_OK != GNUNET_BIO_read_fn(h, file, line, &big, sizeof(int64_t)))
369 return GNUNET_SYSERR;
370 *i = GNUNET_ntohll(big);
376 * Handle for buffered writing.
378 struct GNUNET_BIO_WriteHandle {
380 * Underlying file handle.
382 struct GNUNET_DISK_FileHandle *fd;
385 * I/O buffer. Do not free, allocated at the end of the struct.
390 * Number of bytes already in @e buffer.
395 * Total size of @e buffer.
402 * Open a file for writing.
404 * @param fn file name to be opened
405 * @return IO handle on success, NULL on error
407 struct GNUNET_BIO_WriteHandle *
408 GNUNET_BIO_write_open(const char *fn)
410 struct GNUNET_DISK_FileHandle *fd;
411 struct GNUNET_BIO_WriteHandle *h;
414 GNUNET_DISK_file_open(fn,
415 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE |
416 GNUNET_DISK_OPEN_CREATE,
417 GNUNET_DISK_PERM_USER_READ |
418 GNUNET_DISK_PERM_USER_WRITE);
421 h = GNUNET_malloc(sizeof(struct GNUNET_BIO_WriteHandle) + BIO_BUFFER_SIZE);
422 h->buffer = (char *)&h[1];
423 h->size = BIO_BUFFER_SIZE;
430 * Close an open file for writing.
432 * @param h file handle
433 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
436 GNUNET_BIO_write_close(struct GNUNET_BIO_WriteHandle *h)
441 if ((NULL != h->fd) && (GNUNET_OK == (ret = GNUNET_BIO_flush(h))))
442 GNUNET_DISK_file_close(h->fd);
449 * Force a buffered writer to flush its buffer
451 * @param h the writer handle
452 * @return #GNUNET_OK upon success. Upon failure #GNUNET_SYSERR is returned and
456 GNUNET_BIO_flush(struct GNUNET_BIO_WriteHandle *h)
460 ret = GNUNET_DISK_file_write(h->fd, h->buffer, h->have);
461 if (ret != (ssize_t)h->have)
463 GNUNET_DISK_file_close(h->fd);
465 return GNUNET_SYSERR; /* error */
473 * Write a buffer to a file.
475 * @param h handle to open file
476 * @param buffer the data to write
477 * @param n number of bytes to write
478 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
481 GNUNET_BIO_write(struct GNUNET_BIO_WriteHandle *h,
485 const char *src = buffer;
490 return GNUNET_SYSERR;
494 /* first, just use buffer */
495 min = h->size - h->have;
498 GNUNET_memcpy(&h->buffer[h->have], &src[pos], min);
502 return GNUNET_OK; /* done */
503 GNUNET_assert(h->have == h->size);
504 if (GNUNET_OK != GNUNET_BIO_flush(h))
505 return GNUNET_SYSERR; /* error */
507 while (pos < n); /* should always be true */
514 * Write a string to a file.
516 * @param h handle to open file
517 * @param s string to write (can be NULL)
518 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
521 GNUNET_BIO_write_string(struct GNUNET_BIO_WriteHandle *h, const char *s)
525 slen = (uint32_t)((s == NULL) ? 0 : strlen(s) + 1);
526 if (GNUNET_OK != GNUNET_BIO_write_int32(h, slen))
527 return GNUNET_SYSERR;
529 return GNUNET_BIO_write(h, s, slen - 1);
535 * Write metadata container to a file.
537 * @param h handle to open file
538 * @param m metadata to write
539 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
542 GNUNET_BIO_write_meta_data(struct GNUNET_BIO_WriteHandle *h,
543 const struct GNUNET_CONTAINER_MetaData *m)
549 return GNUNET_BIO_write_int32(h, 0);
551 size = GNUNET_CONTAINER_meta_data_serialize(
555 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
559 return GNUNET_SYSERR;
561 if ((GNUNET_OK != GNUNET_BIO_write_int32(h, (uint32_t)size)) ||
562 (GNUNET_OK != GNUNET_BIO_write(h, buf, size)))
565 return GNUNET_SYSERR;
573 * Write an (u)int32_t.
575 * @param h hande to open file
576 * @param i 32-bit integer to write
577 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
580 GNUNET_BIO_write_int32(struct GNUNET_BIO_WriteHandle *h, int32_t i)
585 return GNUNET_BIO_write(h, &big, sizeof(int32_t));
590 * Write an (u)int64_t.
592 * @param h hande to open file
593 * @param i 64-bit integer to write
594 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
597 GNUNET_BIO_write_int64(struct GNUNET_BIO_WriteHandle *h, int64_t i)
601 big = GNUNET_htonll(i);
602 return GNUNET_BIO_write(h, &big, sizeof(int64_t));