-improving docs
[oweals/gnunet.git] / src / util / bio.c
1 /*
2      This file is part of GNUnet.
3      (C) 2006, 2009, 2013 Christian Grothoff (and other contributing authors)
4
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 3, or (at your
8      option) any later version.
9
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.
14
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.
19 */
20 /**
21  * @file util/bio.c
22  * @brief functions for buffering IO
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_bio_lib.h"
27 #include "gnunet_disk_lib.h"
28
29 #define LOG(kind,...) GNUNET_log_from (kind, "util",__VA_ARGS__)
30
31 /**
32  * Size for I/O buffers.
33  */ 
34 #define BIO_BUFFER_SIZE 65536
35
36 /**
37  * Maximum size allowed for meta data written/read from disk.
38  * File-sharing limits to 64k, so this should be rather generous.
39  */
40 #define MAX_META_DATA (1024 * 1024)
41
42
43 /**
44  * Handle for buffered reading.
45  */
46 struct GNUNET_BIO_ReadHandle
47 {
48   /**
49    * Underlying file abstraction.
50    */
51   struct GNUNET_DISK_FileHandle *fd;
52
53   /**
54    * Error message, NULL if there were no errors.
55    */
56   char *emsg;
57
58   /**
59    * I/O buffer.  Allocated at the end of the struct, do not free!
60    */
61   char *buffer;
62
63   /**
64    * Number of bytes available in read @e buffer.
65    */
66   size_t have;
67
68   /**
69    * Total size of @e buffer.
70    */
71   size_t size;
72
73   /**
74    * Current read offset in @e buffer.
75    */
76   off_t pos;
77 };
78
79
80 /**
81  * Open a file for reading.
82  *
83  * @param fn file name to be opened
84  * @return IO handle on success, NULL on error
85  */
86 struct GNUNET_BIO_ReadHandle *
87 GNUNET_BIO_read_open (const char *fn)
88 {
89   struct GNUNET_DISK_FileHandle *fd;
90   struct GNUNET_BIO_ReadHandle *h;
91
92   fd = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE);
93   if (NULL == fd)
94     return NULL;
95   h = GNUNET_malloc (sizeof (struct GNUNET_BIO_ReadHandle) + BIO_BUFFER_SIZE);
96   h->buffer = (char *) &h[1];
97   h->size = BIO_BUFFER_SIZE;
98   h->fd = fd;
99   return h;
100 }
101
102
103 /**
104  * Close an open file.  Reports if any errors reading
105  * from the file were encountered.
106  *
107  * @param h file handle
108  * @param emsg set to the error message
109  * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
110  */
111 int
112 GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h,
113                        char **emsg)
114 {
115   int err;
116
117   err = (NULL == h->emsg) ? GNUNET_OK : GNUNET_SYSERR;
118   if (emsg != NULL)
119     *emsg = h->emsg;
120   else
121     GNUNET_free_non_null (h->emsg);
122   GNUNET_DISK_file_close (h->fd);
123   GNUNET_free (h);
124   return err;
125 }
126
127
128 /**
129  * Read the contents of a binary file into a buffer.
130  *
131  * @param h handle to an open file
132  * @param what describes what is being read (for error message creation)
133  * @param result the buffer to write the result to
134  * @param len the number of bytes to read
135  * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
136  */
137 int
138 GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h, 
139                  const char *what,
140                  void *result, size_t len)
141 {
142   char *dst = result;
143   size_t min;
144   size_t pos;
145   ssize_t ret;
146
147   if (NULL != h->emsg)
148     return GNUNET_SYSERR;
149   pos = 0;
150   do
151   {
152     /* first, use buffer */
153     min = h->have - h->pos;
154     if (min > 0)
155     {
156       if (min > len - pos)
157         min = len - pos;
158       memcpy (&dst[pos], &h->buffer[h->pos], min);
159       h->pos += min;
160       pos += min;
161     }
162     if (pos == len)
163       return GNUNET_OK;         /* done! */
164     GNUNET_assert (h->have == h->pos);
165     /* fill buffer */
166     ret = GNUNET_DISK_file_read (h->fd, h->buffer, h->size);
167     if (-1 == ret)
168     {
169       GNUNET_asprintf (&h->emsg,
170                        _("Error reading `%s': %s"),
171                        what,
172                        STRERROR (errno));
173       return GNUNET_SYSERR;
174     }
175     if (0 == ret)
176     {
177       GNUNET_asprintf (&h->emsg,
178                        _("Error reading `%s': %s"), 
179                        what,
180                        _("End of file"));
181       return GNUNET_SYSERR;
182     }
183     h->pos = 0;
184     h->have = ret;
185   }
186   while (pos < len);            /* should always be true */
187   return GNUNET_OK;
188 }
189
190
191 /**
192  * Read the contents of a binary file into a buffer.
193  *
194  * @param h handle to an open file
195  * @param file name of the source file
196  * @param line line number in the source file
197  * @param result the buffer to write the result to
198  * @param len the number of bytes to read
199  * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
200  */
201 int
202 GNUNET_BIO_read_fn (struct GNUNET_BIO_ReadHandle *h, 
203                     const char *file, 
204                     int line,
205                     void *result, 
206                     size_t len)
207 {
208   char what[1024];
209
210   GNUNET_snprintf (what, sizeof (what), "%s:%d", file, line);
211   return GNUNET_BIO_read (h, what, result, len);
212 }
213
214
215 /**
216  * Read 0-terminated string from a file.
217  *
218  * @param h handle to an open file
219  * @param what describes what is being read (for error message creation)
220  * @param result the buffer to store a pointer to the (allocated) string to
221  *        (note that *result could be set to NULL as well)
222  * @param maxLen maximum allowed length for the string
223  * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
224  */
225 int
226 GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h, const char *what,
227                         char **result, size_t maxLen)
228 {
229   char *buf;
230   uint32_t big;
231
232   if (GNUNET_OK != GNUNET_BIO_read_int32 (h, &big))
233   {
234     GNUNET_free_non_null (h->emsg);
235     GNUNET_asprintf (&h->emsg, _("Error reading length of string `%s'"), what);
236     return GNUNET_SYSERR;
237   }
238   if (0 == big)
239   {
240     *result = NULL;
241     return GNUNET_OK;
242   }
243   if (big > maxLen)
244   {
245     GNUNET_asprintf (&h->emsg, _("String `%s' longer than allowed (%u > %u)"),
246                      what, big, maxLen);
247     return GNUNET_SYSERR;
248   }
249   buf = GNUNET_malloc (big);
250   *result = buf;
251   buf[--big] = '\0';
252   if (0 == big)
253     return GNUNET_OK;
254   if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, big))
255   {
256     GNUNET_free (buf);
257     *result = NULL;
258     return GNUNET_SYSERR;
259   }
260   return GNUNET_OK;
261 }
262
263
264 /**
265  * Read metadata container from a file.
266  *
267  * @param h handle to an open file
268  * @param what describes what is being read (for error message creation)
269  * @param result the buffer to store a pointer to the (allocated) metadata
270  * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
271  */
272 int
273 GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h, const char *what,
274                            struct GNUNET_CONTAINER_MetaData **result)
275 {
276   uint32_t size;
277   char *buf;
278   struct GNUNET_CONTAINER_MetaData *meta;
279
280   if (GNUNET_BIO_read_int32 (h, (int32_t *) & size) != GNUNET_OK)
281     return GNUNET_SYSERR;
282   if (size == 0)
283   {
284     *result = NULL;
285     return GNUNET_OK;
286   }
287   if (size > MAX_META_DATA)
288   {
289     GNUNET_asprintf (&h->emsg,
290                      _("Serialized metadata `%s' larger than allowed (%u>%u)"),
291                      what, size, MAX_META_DATA);
292     return GNUNET_SYSERR;
293   }
294   buf = GNUNET_malloc (size);
295   if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, size))
296   {
297     GNUNET_free (buf);
298     return GNUNET_SYSERR;
299   }
300   meta = GNUNET_CONTAINER_meta_data_deserialize (buf, size);
301   if (meta == NULL)
302   {
303     GNUNET_free (buf);
304     GNUNET_asprintf (&h->emsg, _("Metadata `%s' failed to deserialize"), what);
305     return GNUNET_SYSERR;
306   }
307   GNUNET_free (buf);
308   *result = meta;
309   return GNUNET_OK;
310 }
311
312
313 /**
314  * Read an (u)int32_t.
315  *
316  * @param h hande to open file
317  * @param file name of the source file
318  * @param line line number in the source file
319  * @param i address of 32-bit integer to read
320  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
321  */
322 int
323 GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h, const char *file,
324                          int line, int32_t * i)
325 {
326   int32_t big;
327
328   if (GNUNET_OK != GNUNET_BIO_read_fn (h, file, line, &big, sizeof (int32_t)))
329     return GNUNET_SYSERR;
330   *i = ntohl (big);
331   return GNUNET_OK;
332 }
333
334
335 /**
336  * Read an (u)int64_t.
337  *
338  * @param h hande to open file
339  * @param file name of the source file
340  * @param line line number in the source file
341  * @param i address of 64-bit integer to read
342  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
343  */
344 int
345 GNUNET_BIO_read_int64__ (struct GNUNET_BIO_ReadHandle *h, 
346                          const char *file,
347                          int line, int64_t * i)
348 {
349   int64_t big;
350
351   if (GNUNET_OK != GNUNET_BIO_read_fn (h, file, line, &big, sizeof (int64_t)))
352     return GNUNET_SYSERR;
353   *i = GNUNET_ntohll (big);
354   return GNUNET_OK;
355 }
356
357
358 /**
359  * Handle for buffered writing.
360  */
361 struct GNUNET_BIO_WriteHandle
362 {
363   /**
364    * Underlying file handle.
365    */
366   struct GNUNET_DISK_FileHandle *fd;
367   
368   /**
369    * I/O buffer.  Do not free, allocated at the end of the struct.
370    */
371   char *buffer;
372
373   /**
374    * Number of bytes already in @e buffer.
375    */
376   size_t have;
377
378   /**
379    * Total size of @e buffer.
380    */
381   size_t size;
382 };
383
384
385 /**
386  * Open a file for writing.
387  *
388  * @param fn file name to be opened
389  * @return IO handle on success, NULL on error
390  */
391 struct GNUNET_BIO_WriteHandle *
392 GNUNET_BIO_write_open (const char *fn)
393 {
394   struct GNUNET_DISK_FileHandle *fd;
395   struct GNUNET_BIO_WriteHandle *h;
396
397   fd = GNUNET_DISK_file_open (fn,
398                               GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE
399                               | GNUNET_DISK_OPEN_CREATE,
400                               GNUNET_DISK_PERM_USER_READ |
401                               GNUNET_DISK_PERM_USER_WRITE);
402   if (NULL == fd)
403     return NULL;
404   h = GNUNET_malloc (sizeof (struct GNUNET_BIO_WriteHandle) + BIO_BUFFER_SIZE);
405   h->buffer = (char *) &h[1];
406   h->size = BIO_BUFFER_SIZE;
407   h->fd = fd;
408   return h;
409 }
410
411
412 /**
413  * Close an open file for writing.
414  *
415  * @param h file handle
416  * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
417  */
418 int
419 GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h)
420 {
421   int ret;
422
423   ret = GNUNET_SYSERR;
424   if ( (NULL != h->fd) && (GNUNET_OK == (ret = GNUNET_BIO_flush (h))) )
425     GNUNET_DISK_file_close (h->fd);
426   GNUNET_free (h);
427   return ret;
428 }
429
430
431 /**
432  * Force a buffered writer to flush its buffer
433  *
434  * @param h the writer handle
435  * @return #GNUNET_OK upon success.  Upon failure #GNUNET_SYSERR is returned and
436  *           the file is closed
437  */
438 int
439 GNUNET_BIO_flush (struct GNUNET_BIO_WriteHandle *h)
440 {
441   ssize_t ret;
442
443   ret = GNUNET_DISK_file_write (h->fd, h->buffer, h->have);
444   if (ret != h->have)
445   {
446     GNUNET_DISK_file_close (h->fd);
447     h->fd = NULL;
448     return GNUNET_SYSERR;     /* error */
449   }
450   h->have = 0;
451   return GNUNET_OK;
452 }
453
454
455 /**
456  * Write a buffer to a file.
457  *
458  * @param h handle to open file
459  * @param buffer the data to write
460  * @param n number of bytes to write
461  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
462  */
463 int
464 GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h, const void *buffer,
465                   size_t n)
466 {
467   const char *src = buffer;
468   size_t min;
469   size_t pos;
470
471   if (NULL == h->fd)
472     return GNUNET_SYSERR;
473   pos = 0;
474   do
475   {
476     /* first, just use buffer */
477     min = h->size - h->have;
478     if (min > n - pos)
479       min = n - pos;
480     memcpy (&h->buffer[h->have], &src[pos], min);
481     pos += min;
482     h->have += min;
483     if (pos == n)
484       return GNUNET_OK;         /* done */
485     GNUNET_assert (h->have == h->size);
486     if (GNUNET_OK != GNUNET_BIO_flush (h))
487       return GNUNET_SYSERR;     /* error */
488   }
489   while (pos < n);              /* should always be true */
490   GNUNET_break (0);
491   return GNUNET_OK;
492 }
493
494
495 /**
496  * Write a string to a file.
497  *
498  * @param h handle to open file
499  * @param s string to write (can be NULL)
500  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
501  */
502 int
503 GNUNET_BIO_write_string (struct GNUNET_BIO_WriteHandle *h, 
504                          const char *s)
505 {
506   uint32_t slen;
507
508   slen = (uint32_t) ((s == NULL) ? 0 : strlen (s) + 1);
509   if (GNUNET_OK != GNUNET_BIO_write_int32 (h, slen))
510     return GNUNET_SYSERR;
511   if (0 != slen)
512     return GNUNET_BIO_write (h, s, slen - 1);
513   return GNUNET_OK;
514 }
515
516
517 /**
518  * Write metadata container to a file.
519  *
520  * @param h handle to open file
521  * @param m metadata to write
522  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
523  */
524 int
525 GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h,
526                             const struct GNUNET_CONTAINER_MetaData *m)
527 {
528   ssize_t size;
529   char *buf;
530
531   if (m == NULL)
532     return GNUNET_BIO_write_int32 (h, 0);
533   buf = NULL;
534   size =
535       GNUNET_CONTAINER_meta_data_serialize (m, &buf, MAX_META_DATA,
536                                             GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
537   if (size == -1)
538   {
539     GNUNET_free (buf);
540     return GNUNET_SYSERR;
541   }
542   if ((GNUNET_OK != GNUNET_BIO_write_int32 (h, (uint32_t) size)) ||
543       (GNUNET_OK != GNUNET_BIO_write (h, buf, size)))
544   {
545     GNUNET_free (buf);
546     return GNUNET_SYSERR;
547   }
548   GNUNET_free (buf);
549   return GNUNET_OK;
550 }
551
552
553 /**
554  * Write an (u)int32_t.
555  *
556  * @param h hande to open file
557  * @param i 32-bit integer to write
558  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
559  */
560 int
561 GNUNET_BIO_write_int32 (struct GNUNET_BIO_WriteHandle *h, 
562                         int32_t i)
563 {
564   int32_t big;
565
566   big = htonl (i);
567   return GNUNET_BIO_write (h, &big, sizeof (int32_t));
568 }
569
570
571 /**
572  * Write an (u)int64_t.
573  *
574  * @param h hande to open file
575  * @param i 64-bit integer to write
576  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
577  */
578 int
579 GNUNET_BIO_write_int64 (struct GNUNET_BIO_WriteHandle *h, 
580                         int64_t i)
581 {
582   int64_t big;
583
584   big = GNUNET_htonll (i);
585   return GNUNET_BIO_write (h, &big, sizeof (int64_t));
586 }
587
588
589 /* end of bio.c */