GNUnet  0.11.x
bio.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2006, 2009, 2013 GNUnet e.V.
4 
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.
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  Affero General Public License for more details.
14 
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/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 
28 #define LOG(kind, ...) GNUNET_log_from (kind, "util-bio", __VA_ARGS__)
29 
30 #ifndef PATH_MAX
31 
34 #define PATH_MAX 4096
35 #endif
36 
37 
41 #define BIO_BUFFER_SIZE 65536
42 
47 #define MAX_META_DATA (1024 * 1024)
48 
49 
56 enum IOType
57 {
61  IO_FILE = 0,
62 
67 };
68 
69 
74 {
78  enum IOType type;
79 
84 
88  char *emsg;
89 
93  char *buffer;
94 
98  size_t have;
99 
103  size_t size;
104 
108  off_t pos;
109 };
110 
111 
118 struct GNUNET_BIO_ReadHandle *
120 {
121  struct GNUNET_DISK_FileHandle *fd;
122  struct GNUNET_BIO_ReadHandle *h;
123 
125  if (NULL == fd)
126  return NULL;
127  h = GNUNET_malloc (sizeof(struct GNUNET_BIO_ReadHandle) + BIO_BUFFER_SIZE);
128  h->type = IO_FILE;
129  h->buffer = (char *) &h[1];
130  h->size = BIO_BUFFER_SIZE;
131  h->fd = fd;
132  return h;
133 }
134 
135 
143 struct GNUNET_BIO_ReadHandle *
145 {
146  struct GNUNET_BIO_ReadHandle *h;
147 
148  h = GNUNET_new (struct GNUNET_BIO_ReadHandle);
149  h->type = IO_BUFFER;
150  h->buffer = buffer;
151  h->size = size;
152  return h;
153 }
154 
155 
165 int
167 {
168  int err;
169 
170  err = (NULL == h->emsg) ? GNUNET_OK : GNUNET_SYSERR;
171  if (NULL != emsg)
172  *emsg = h->emsg;
173  else
174  GNUNET_free (h->emsg);
175  switch (h->type)
176  {
177  case IO_FILE:
179  break;
180  case IO_BUFFER:
181  break;
182  default:
183  break;
184  }
185  GNUNET_free (h);
186  return err;
187 }
188 
189 
199 static int
201  const char *what,
202  char *result,
203  size_t len)
204 {
205  size_t pos = 0;
206  size_t min;
207  ssize_t ret;
208 
209  do
210  {
211  min = h->have - h->pos;
212  if (0 < min)
213  {
214  if (len - pos < min)
215  min = len - pos;
216  GNUNET_memcpy (&result[pos], &h->buffer[h->pos], min);
217  h->pos += min;
218  pos += min;
219  }
220  if (len == pos)
221  return GNUNET_OK;
222  GNUNET_assert (((off_t) h->have) == h->pos);
223  ret = GNUNET_DISK_file_read (h->fd, h->buffer, h->size);
224  if (-1 == ret)
225  {
226  GNUNET_asprintf (&h->emsg,
227  _ ("Error reading `%s' from file: %s"),
228  what,
229  strerror (errno));
230  return GNUNET_SYSERR;
231  }
232  if (0 == ret)
233  {
234  GNUNET_asprintf (&h->emsg,
235  _ ("Error reading `%s' from file: %s"),
236  what,
237  _ ("End of file"));
238  return GNUNET_SYSERR;
239  }
240  h->pos = 0;
241  h->have = ret;
242  }
243  while (pos < len);
244  return GNUNET_OK;
245 }
246 
247 
257 static int
259  const char *what,
260  char *result,
261  size_t len)
262 {
263  if (h->size < len || h->size - h->pos < len)
264  {
265  GNUNET_asprintf (&h->emsg,
266  _ ("Error while reading `%s' from buffer: %s"),
267  what,
268  _ ("Not enough data left"));
269  return GNUNET_SYSERR;
270  }
271  GNUNET_memcpy (result, h->buffer + h->pos, len);
272  h->pos += len;
273  return GNUNET_OK;
274 }
275 
276 
286 int
288  const char *what,
289  void *result,
290  size_t len)
291 {
292  char *dst = result;
293 
294  if (NULL != h->emsg)
295  return GNUNET_SYSERR;
296 
297  if (0 == len)
298  return GNUNET_OK;
299 
300  switch (h->type)
301  {
302  case IO_FILE:
303  return read_from_file (h, what, dst, len);
304  case IO_BUFFER:
305  return read_from_buffer (h, what, dst, len);
306  default:
307  GNUNET_asprintf (&h->emsg,
308  _ ("Invalid handle type while reading `%s'"),
309  what);
310  return GNUNET_SYSERR;
311  }
312 }
313 
314 
325 int
327  const char *what,
328  char **result,
329  size_t max_length)
330 {
331  char *buf;
332  uint32_t big;
333 
335  _ ("string length"),
336  (int32_t *) &big))
337  {
338  char *tmp = h->emsg;
339  if (NULL != tmp)
340  GNUNET_asprintf (&h->emsg,
341  _ ("%s (while reading `%s')"),
342  tmp,
343  what);
344  else
345  GNUNET_asprintf (&h->emsg,
346  _ ("Error reading length of string `%s'"),
347  what);
348  GNUNET_free (tmp);
349  return GNUNET_SYSERR;
350  }
351  if (0 == big)
352  {
353  *result = NULL;
354  return GNUNET_OK;
355  }
356  if (big > max_length)
357  {
358  GNUNET_asprintf (&h->emsg,
359  _ ("String `%s' longer than allowed (%u > %u)"),
360  what,
361  big,
362  max_length);
363  return GNUNET_SYSERR;
364  }
365  buf = GNUNET_malloc (big);
366  *result = buf;
367  buf[--big] = '\0';
368  if (0 == big)
369  return GNUNET_OK;
370  if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, big))
371  {
372  GNUNET_free (buf);
373  *result = NULL;
374  return GNUNET_SYSERR;
375  }
376  return GNUNET_OK;
377 }
378 
379 
388 int
390  const char *what,
392 {
393  uint32_t size;
394  char *buf;
396 
398  _ ("metadata length"),
399  (int32_t *) &size))
400  return GNUNET_SYSERR;
401  if (0 == size)
402  {
403  *result = NULL;
404  return GNUNET_OK;
405  }
406  if (MAX_META_DATA < size)
407  {
409  &h->emsg,
410  _ ("Serialized metadata `%s' larger than allowed (%u > %u)"),
411  what,
412  size,
413  MAX_META_DATA);
414  return GNUNET_SYSERR;
415  }
416  buf = GNUNET_malloc (size);
417  if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, size))
418  {
419  GNUNET_free (buf);
420  return GNUNET_SYSERR;
421  }
422  meta = GNUNET_CONTAINER_meta_data_deserialize (buf, size);
423  if (NULL == meta)
424  {
425  GNUNET_free (buf);
426  GNUNET_asprintf (&h->emsg, _ ("Failed to deserialize metadata `%s'"), what);
427  return GNUNET_SYSERR;
428  }
429  GNUNET_free (buf);
430  *result = meta;
431  return GNUNET_OK;
432 }
433 
441 int
443  const char *what,
444  float *f)
445 {
446  int32_t *i = (int32_t *) f;
447  return GNUNET_BIO_read_int32 (h, what, i);
448 }
449 
450 
458 int
460  const char *what,
461  double *f)
462 {
463  int64_t *i = (int64_t *) f;
464  return GNUNET_BIO_read_int64 (h, what, i);
465 }
466 
467 
476 int
478  const char *what,
479  int32_t *i)
480 {
481  int32_t big;
482 
483  if (GNUNET_OK != GNUNET_BIO_read (h, what, &big, sizeof(int32_t)))
484  return GNUNET_SYSERR;
485  *i = ntohl (big);
486  return GNUNET_OK;
487 }
488 
489 
498 int
500  const char *what,
501  int64_t *i)
502 {
503  int64_t big;
504 
505  if (GNUNET_OK != GNUNET_BIO_read (h, what, &big, sizeof(int64_t)))
506  return GNUNET_SYSERR;
507  *i = GNUNET_ntohll (big);
508  return GNUNET_OK;
509 }
510 
511 
516 {
520  enum IOType type;
521 
526 
530  char *emsg;
531 
537  void *buffer;
538 
542  size_t have;
543 
547  size_t size;
548 };
549 
550 
557 struct GNUNET_BIO_WriteHandle *
559 {
560  struct GNUNET_DISK_FileHandle *fd;
561  struct GNUNET_BIO_WriteHandle *h;
562 
563  fd =
570  if (NULL == fd)
571  return NULL;
573  h->buffer = &h[1];
574  h->size = BIO_BUFFER_SIZE;
575  h->fd = fd;
576  return h;
577 }
578 
579 
585 struct GNUNET_BIO_WriteHandle *
587 {
588  struct GNUNET_BIO_WriteHandle *h;
589 
590  h = GNUNET_new (struct GNUNET_BIO_WriteHandle);
591  h->type = IO_BUFFER;
592  h->buffer = (void *) GNUNET_malloc (sizeof (struct GNUNET_Buffer));
593  return h;
594 }
595 
596 
606 int
608 {
609  int err;
610 
611  err = (NULL == h->emsg) ? GNUNET_OK : GNUNET_SYSERR;
612  if (NULL != emsg)
613  *emsg = h->emsg;
614  else
615  GNUNET_free (h->emsg);
616  switch (h->type)
617  {
618  case IO_FILE:
619  if (NULL == h->fd)
620  return GNUNET_SYSERR;
621  if (GNUNET_OK != GNUNET_BIO_flush (h))
622  {
623  if (NULL != emsg)
624  *emsg = h->emsg;
625  else
626  GNUNET_free (h->emsg);
627  err = GNUNET_SYSERR;
628  }
629  else
630  {
632  }
633  break;
634  case IO_BUFFER:
635  GNUNET_buffer_clear ((struct GNUNET_Buffer *) h->buffer);
636  GNUNET_free (h->buffer);
637  break;
638  }
639  GNUNET_free (h);
640  return err;
641 }
642 
643 
653 int
655 {
656  ssize_t ret;
657 
658  if (IO_FILE != h->type)
659  return GNUNET_OK;
660 
661  ret = GNUNET_DISK_file_write (h->fd, h->buffer, h->have);
662  if (ret != (ssize_t) h->have)
663  {
665  h->fd = NULL;
666  GNUNET_free (h->emsg);
667  GNUNET_asprintf (&h->emsg, _ ("Unable to flush buffer to file"));
668  return GNUNET_SYSERR;
669  }
670  h->have = 0;
671  return GNUNET_OK;
672 }
673 
674 
687 int
689  char **emsg,
690  void **contents,
691  size_t *size)
692 {
693  if (IO_BUFFER != h->type)
694  return GNUNET_SYSERR;
695  if (NULL == contents || NULL == size)
696  return GNUNET_SYSERR;
697  int ret = (NULL != h->emsg) ? GNUNET_SYSERR : GNUNET_OK;
698  if (NULL != emsg)
699  *emsg = h->emsg;
700  else
701  GNUNET_free (h->emsg);
702  *contents = GNUNET_buffer_reap ((struct GNUNET_Buffer *) h->buffer, size);
703  return ret;
704 }
705 
706 
716 static int
718  const char *what,
719  const char *source,
720  size_t len)
721 {
722  size_t min;
723  size_t pos = 0;
724  char *buffer = (char *) h->buffer;
725 
726  if (NULL == h->fd)
727  {
728  GNUNET_asprintf (&h->emsg,
729  _ ("Error while writing `%s' to file: %s"),
730  what,
731  _ ("No associated file"));
732  return GNUNET_SYSERR;
733  }
734 
735  do
736  {
737  min = h->size - h->have;
738  if (len - pos < min)
739  min = len - pos;
740  GNUNET_memcpy (&buffer[h->have], &source[pos], min);
741  pos += min;
742  h->have += min;
743  if (len == pos)
744  return GNUNET_OK;
745  GNUNET_assert (h->have == h->size);
746  if (GNUNET_OK != GNUNET_BIO_flush (h))
747  {
748  char *tmp = h->emsg;
749  GNUNET_asprintf (&h->emsg,
750  _ ("Error while writing `%s' to file: %s"),
751  what,
752  tmp);
753  GNUNET_free (tmp);
754  return GNUNET_SYSERR;
755  }
756  }
757  while (pos < len);
758  GNUNET_break (0);
759  return GNUNET_OK;
760 }
761 
762 
772 static int
774  const char *what,
775  const char *source,
776  size_t len)
777 {
778  GNUNET_buffer_write ((struct GNUNET_Buffer *) h->buffer, source, len);
779  h->have += len;
780  return GNUNET_OK;
781 }
782 
783 
793 int
795  const char *what,
796  const void *buffer,
797  size_t n)
798 {
799  const char *src = buffer;
800 
801  if (NULL != h->emsg)
802  return GNUNET_SYSERR;
803 
804  if (0 == n)
805  return GNUNET_OK;
806 
807  switch (h->type)
808  {
809  case IO_FILE:
810  return write_to_file (h, what, src, n);
811  case IO_BUFFER:
812  return write_to_buffer (h, what, src, n);
813  default:
814  GNUNET_asprintf (&h->emsg,
815  _ ("Invalid handle type while writing `%s'"),
816  what);
817  return GNUNET_SYSERR;
818  }
819 }
820 
821 
830 int
832  const char *what,
833  const char *s)
834 {
835  uint32_t slen;
836 
837  slen = (uint32_t) ((s == NULL) ? 0 : strlen (s) + 1);
838  if (GNUNET_OK != GNUNET_BIO_write_int32 (h, _ ("string length"), slen))
839  return GNUNET_SYSERR;
840  if (0 != slen)
841  return GNUNET_BIO_write (h, what, s, slen - 1);
842  return GNUNET_OK;
843 }
844 
845 
854 int
856  const char *what,
857  const struct GNUNET_CONTAINER_MetaData *m)
858 {
859  ssize_t size;
860  char *buf;
861 
862  if (m == NULL)
863  return GNUNET_BIO_write_int32 (h, _ ("metadata length"), 0);
864  buf = NULL;
866  m,
867  &buf,
870  if (-1 == size)
871  {
872  GNUNET_free (buf);
873  GNUNET_free (h->emsg);
874  GNUNET_asprintf (&h->emsg,
875  _ ("Failed to serialize metadata `%s'"),
876  what);
877  return GNUNET_SYSERR;
878  }
880  _ ("metadata length"),
881  (uint32_t) size))
882  || (GNUNET_OK != GNUNET_BIO_write (h, what, buf, size)))
883  {
884  GNUNET_free (buf);
885  return GNUNET_SYSERR;
886  }
887  GNUNET_free (buf);
888  return GNUNET_OK;
889 }
890 
891 
899 int
901  const char *what,
902  float f)
903 {
904  int32_t i = f;
905  return GNUNET_BIO_write_int32 (h, what, i);
906 }
907 
908 
916 int
918  const char *what,
919  double f)
920 {
921  int64_t i = f;
922  return GNUNET_BIO_write_int64 (h, what, i);
923 }
924 
925 
934 int
936  const char *what,
937  int32_t i)
938 {
939  int32_t big;
940 
941  big = htonl (i);
942  return GNUNET_BIO_write (h, what, &big, sizeof(int32_t));
943 }
944 
945 
954 int
956  const char *what,
957  int64_t i)
958 {
959  int64_t big;
960 
961  big = GNUNET_htonll (i);
962  return GNUNET_BIO_write (h, what, &big, sizeof(int64_t));
963 }
964 
965 
976 static int
978  struct GNUNET_BIO_ReadHandle *h,
979  const char *what,
980  void *target,
981  size_t target_size)
982 {
983  return GNUNET_BIO_read (h, what, target, target_size);
984 }
985 
986 
995 struct GNUNET_BIO_ReadSpec
997  void *result,
998  size_t len)
999 {
1000  struct GNUNET_BIO_ReadSpec rs = {
1002  .cls = NULL,
1003  .what = what,
1004  .target = result,
1005  .size = len,
1006  };
1007 
1008  return rs;
1009 }
1010 
1011 
1022 static int
1024  struct GNUNET_BIO_ReadHandle *h,
1025  const char *what,
1026  void *target,
1027  size_t target_size)
1028 {
1029  char **result = target;
1030  return GNUNET_BIO_read_string (h, what, result, target_size);
1031 }
1032 
1033 
1043 struct GNUNET_BIO_ReadSpec
1045  char **result,
1046  size_t max_length)
1047 {
1048  struct GNUNET_BIO_ReadSpec rs = {
1050  .cls = NULL,
1051  .target = result,
1052  .size = max_length,
1053  };
1054 
1055  return rs;
1056 }
1057 
1058 
1070 static int
1072  struct GNUNET_BIO_ReadHandle *h,
1073  const char *what,
1074  void *target,
1075  size_t target_size)
1076 {
1077  struct GNUNET_CONTAINER_MetaData **result = target;
1078  return GNUNET_BIO_read_meta_data (h, what, result);
1079 }
1080 
1081 
1089 struct GNUNET_BIO_ReadSpec
1092 {
1093  struct GNUNET_BIO_ReadSpec rs = {
1095  .cls = NULL,
1096  .target = result,
1097  .size = 0,
1098  };
1099 
1100  return rs;
1101 }
1102 
1103 
1114 static int
1116  struct GNUNET_BIO_ReadHandle *h,
1117  const char *what,
1118  void *target,
1119  size_t target_size)
1120 {
1121  int32_t *result = target;
1122  return GNUNET_BIO_read_int32 (h, what, result);
1123 }
1124 
1125 
1133 struct GNUNET_BIO_ReadSpec
1135  int32_t *i)
1136 {
1137  struct GNUNET_BIO_ReadSpec rs = {
1139  .cls = NULL,
1140  .target = i,
1141  .size = 0,
1142  };
1143 
1144  return rs;
1145 }
1146 
1147 
1158 static int
1160  struct GNUNET_BIO_ReadHandle *h,
1161  const char *what,
1162  void *target,
1163  size_t target_size)
1164 {
1165  int64_t *result = target;
1166  return GNUNET_BIO_read_int64 (h, what, result);
1167 }
1168 
1169 
1177 struct GNUNET_BIO_ReadSpec
1179  int64_t *i)
1180 {
1181  struct GNUNET_BIO_ReadSpec rs = {
1183  .cls = NULL,
1184  .target = i,
1185  .size = 0,
1186  };
1187 
1188  return rs;
1189 }
1190 
1191 
1198 struct GNUNET_BIO_ReadSpec
1199 GNUNET_BIO_read_spec_float(const char *what, float *f)
1200 {
1201  struct GNUNET_BIO_ReadSpec rs = {
1203  .cls = NULL,
1204  .target = (int32_t *) f,
1205  .size = 0,
1206  };
1207 
1208  return rs;
1209 }
1210 
1211 
1218 struct GNUNET_BIO_ReadSpec
1219 GNUNET_BIO_read_spec_double(const char *what, double *f)
1220 {
1221  struct GNUNET_BIO_ReadSpec rs = {
1223  .cls = NULL,
1224  .target = (int64_t *) f,
1225  .size = 0,
1226  };
1227 
1228  return rs;
1229 }
1230 
1231 
1240 int
1242  struct GNUNET_BIO_ReadSpec *rs)
1243 {
1244  int ret = GNUNET_OK;
1245 
1246  for (size_t i=0; NULL!=rs[i].rh; ++i)
1247  {
1248  ret = rs[i].rh (rs[i].cls, h, rs[i].what, rs[i].target, rs[i].size);
1249  if (GNUNET_OK != ret)
1250  return ret;
1251  }
1252 
1253  return ret;
1254 }
1255 
1256 
1267 static int
1269  struct GNUNET_BIO_WriteHandle *h,
1270  const char *what,
1271  void *source,
1272  size_t source_size)
1273 {
1274  return GNUNET_BIO_write (h, what, source, source_size);
1275 }
1276 
1277 
1286 struct GNUNET_BIO_WriteSpec
1288  void *source,
1289  size_t size)
1290 {
1291  struct GNUNET_BIO_WriteSpec ws = {
1293  .cls = NULL,
1294  .what = what,
1295  .source = source,
1296  .source_size = size,
1297  };
1298 
1299  return ws;
1300 }
1301 
1302 
1314 static int
1316  struct GNUNET_BIO_WriteHandle *h,
1317  const char *what,
1318  void *source,
1319  size_t source_size)
1320 {
1321  const char *s = source;
1322  return GNUNET_BIO_write_string (h, what, s);
1323 }
1324 
1325 
1333 struct GNUNET_BIO_WriteSpec
1335  const char *s)
1336 {
1337  struct GNUNET_BIO_WriteSpec ws = {
1339  .cls = NULL,
1340  .what = what,
1341  .source = (void *) s,
1342  .source_size = 0,
1343  };
1344 
1345  return ws;
1346 }
1347 
1348 
1360 static int
1362  struct GNUNET_BIO_WriteHandle *h,
1363  const char *what,
1364  void *source,
1365  size_t source_size)
1366 {
1367  const struct GNUNET_CONTAINER_MetaData *m = source;
1368  return GNUNET_BIO_write_meta_data (h, what, m);
1369 }
1370 
1371 
1379 struct GNUNET_BIO_WriteSpec
1381  const struct GNUNET_CONTAINER_MetaData *m)
1382 {
1383  struct GNUNET_BIO_WriteSpec ws = {
1385  .cls = NULL,
1386  .what = what,
1387  .source = (void *) m,
1388  .source_size = 0,
1389  };
1390 
1391  return ws;
1392 }
1393 
1394 
1405 static int
1407  struct GNUNET_BIO_WriteHandle *h,
1408  const char *what,
1409  void *source,
1410  size_t source_size)
1411 {
1412  int32_t i = *(int32_t *) source;
1413  return GNUNET_BIO_write_int32 (h, what, i);
1414 }
1415 
1416 
1424 struct GNUNET_BIO_WriteSpec
1426  int32_t *i)
1427 {
1428  struct GNUNET_BIO_WriteSpec ws = {
1430  .cls = NULL,
1431  .what = what,
1432  .source = i,
1433  .source_size = 0,
1434  };
1435 
1436  return ws;
1437 }
1438 
1439 
1450 static int
1452  struct GNUNET_BIO_WriteHandle *h,
1453  const char *what,
1454  void *source,
1455  size_t source_size)
1456 {
1457  int64_t i = *(int64_t *) source;
1458  return GNUNET_BIO_write_int64 (h, what, i);
1459 }
1460 
1461 
1469 struct GNUNET_BIO_WriteSpec
1471  int64_t *i)
1472 {
1473  struct GNUNET_BIO_WriteSpec ws = {
1475  .cls = NULL,
1476  .what = what,
1477  .source = i,
1478  .source_size = 0,
1479  };
1480 
1481  return ws;
1482 }
1483 
1484 
1492 struct GNUNET_BIO_WriteSpec
1493 GNUNET_BIO_write_spec_float(const char *what, float *f)
1494 {
1495  struct GNUNET_BIO_WriteSpec ws = {
1497  .cls = NULL,
1498  .what = what,
1499  .source = (int32_t *) f,
1500  .source_size = 0,
1501  };
1502 
1503  return ws;
1504 }
1505 
1506 
1514 struct GNUNET_BIO_WriteSpec
1515 GNUNET_BIO_write_spec_double(const char *what, double *f)
1516 {
1517  struct GNUNET_BIO_WriteSpec ws = {
1519  .cls = NULL,
1520  .what = what,
1521  .source = (int64_t *) f,
1522  .source_size = 0,
1523  };
1524 
1525  return ws;
1526 }
1527 
1528 
1537 int
1539  struct GNUNET_BIO_WriteSpec *ws)
1540 {
1541  int ret = GNUNET_OK;
1542 
1543  for (size_t i=0; NULL!=ws[i].wh; ++i)
1544  {
1545  ret = ws[i].wh (ws[i].cls, h, ws[i].what, ws[i].source, ws[i].source_size);
1546  if (GNUNET_OK != ret)
1547  return ret;
1548  }
1549 
1550  /* If it's a file-based handle, the flush makes sure that the data in the
1551  buffer is actualy written to the disk. */
1552  if (IO_FILE == h->type)
1553  ret = GNUNET_BIO_flush (h);
1554 
1555  return ret;
1556 }
1557 
1558 
1559 /* end of bio.c */
void * cls
Closure for rh.
int GNUNET_BIO_read_float(struct GNUNET_BIO_ReadHandle *h, const char *what, float *f)
Read a float.
Definition: bio.c:442
Structure specifying a writing operation on an IO handle.
Open the file for reading.
struct GNUNET_BIO_WriteHandle * GNUNET_BIO_write_open_buffer(void)
Create a handle backed by an in-memory buffer.
Definition: bio.c:586
Create file if it doesn&#39;t exist.
int GNUNET_BIO_read_int32(struct GNUNET_BIO_ReadHandle *h, const char *what, int32_t *i)
Read an (u)int32_t.
Definition: bio.c:477
ssize_t GNUNET_CONTAINER_meta_data_serialize(const struct GNUNET_CONTAINER_MetaData *md, char **target, size_t max, enum GNUNET_CONTAINER_MetaDataSerializationOptions opt)
Serialize meta-data to target.
int GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1280
struct GNUNET_BIO_ReadSpec GNUNET_BIO_read_spec_object(const char *what, void *result, size_t len)
Create the specification to read a certain amount of bytes.
Definition: bio.c:996
IOType
Enum used internally to know how buffering is handled.
Definition: bio.c:56
If not enough space is available, it is acceptable to only serialize some of the metadata.
enum IOType type
The "backend" type.
Definition: bio.c:520
int GNUNET_BIO_write_double(struct GNUNET_BIO_WriteHandle *h, const char *what, double f)
Write a double.
Definition: bio.c:917
size_t source_size
Size of source.
ssize_t GNUNET_DISK_file_read(const struct GNUNET_DISK_FileHandle *h, void *result, size_t len)
Read the contents of a binary file into a buffer.
Definition: disk.c:732
struct GNUNET_BIO_ReadHandle * GNUNET_BIO_read_open_file(const char *fn)
Open a file for reading.
Definition: bio.c:119
int GNUNET_BIO_write_int64(struct GNUNET_BIO_WriteHandle *h, const char *what, int64_t i)
Write an (u)int64_t.
Definition: bio.c:955
int GNUNET_BIO_flush(struct GNUNET_BIO_WriteHandle *h)
Force a file-based buffered writer to flush its buffer.
Definition: bio.c:654
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static int write_to_file(struct GNUNET_BIO_WriteHandle *h, const char *what, const char *source, size_t len)
Function used internally to write the contents of a buffer into a file.
Definition: bio.c:717
struct GNUNET_DISK_FileHandle * fd
Handle to a file on disk, if type is IO_FILE.
Definition: bio.c:83
static int write_to_buffer(struct GNUNET_BIO_WriteHandle *h, const char *what, const char *source, size_t len)
Function used internally to write the contents of a buffer to another buffer.
Definition: bio.c:773
Nobody is allowed to do anything to the file.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
void GNUNET_buffer_write(struct GNUNET_Buffer *buf, const char *data, size_t len)
Write bytes to the buffer.
Definition: buffer.c:85
struct GNUNET_CONTAINER_MetaData * GNUNET_CONTAINER_meta_data_deserialize(const char *input, size_t size)
Deserialize meta-data.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
void * cls
Closure for rh.
#define MAX_META_DATA
Maximum size allowed for meta data written/read from disk.
Definition: bio.c:47
int GNUNET_BIO_read_spec_commit(struct GNUNET_BIO_ReadHandle *h, struct GNUNET_BIO_ReadSpec *rs)
Execute the read specifications in order.
Definition: bio.c:1241
static int read_from_file(struct GNUNET_BIO_ReadHandle *h, const char *what, char *result, size_t len)
Function used internally to read the contents of a file into a buffer.
Definition: bio.c:200
static int read_spec_handler_meta_data(void *cls, struct GNUNET_BIO_ReadHandle *h, const char *what, void *target, size_t target_size)
Function used internally to read a metadata container from within a read spec.
Definition: bio.c:1071
#define GNUNET_new(type)
Allocate a struct or union of the given type.
struct GNUNET_BIO_ReadHandle * GNUNET_BIO_read_open_buffer(void *buffer, size_t size)
Create a handle from an existing allocated buffer.
Definition: bio.c:144
#define BIO_BUFFER_SIZE
Size for I/O buffers.
Definition: bio.c:41
const char * what
What is being read (for error message creation)
struct GNUNET_BIO_ReadSpec GNUNET_BIO_read_spec_int64(const char *what, int64_t *i)
Create the specification to read an (u)int64_t.
Definition: bio.c:1178
struct GNUNET_BIO_WriteSpec GNUNET_BIO_write_spec_object(const char *what, void *source, size_t size)
Create the specification to read some bytes.
Definition: bio.c:1287
struct GNUNET_DISK_FileHandle * fd
Handle to a file on disk, if type is IO_FILE.
Definition: bio.c:525
static int read_spec_handler_object(void *cls, struct GNUNET_BIO_ReadHandle *h, const char *what, void *target, size_t target_size)
Function used internally to read some bytes from within a read spec.
Definition: bio.c:977
void * buffer
I/O buffer.
Definition: bio.c:537
GNUNET_BIO_ReadHandler rh
Function performing data deserialization.
int GNUNET_BIO_read_close(struct GNUNET_BIO_ReadHandle *h, char **emsg)
Close an open handle.
Definition: bio.c:166
size_t size
Total size of buffer.
Definition: bio.c:103
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:99
static int read_spec_handler_int64(void *cls, struct GNUNET_BIO_ReadHandle *h, const char *what, void *target, size_t target_size)
Function used internally to read an (u)int64_t from within a read spec.
Definition: bio.c:1159
#define _(String)
GNU gettext support macro.
Definition: platform.h:184
static struct GNUNET_ARM_MonitorHandle * m
Monitor connection with ARM.
Definition: gnunet-arm.c:104
int GNUNET_BIO_read(struct GNUNET_BIO_ReadHandle *h, const char *what, void *result, size_t len)
Read some contents into a buffer.
Definition: bio.c:287
struct GNUNET_BIO_WriteSpec GNUNET_BIO_write_spec_string(const char *what, const char *s)
Create the specification to write a 0-terminated string.
Definition: bio.c:1334
void * target
Destination buffer.
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
Meta data to associate with a file, directory or namespace.
static int read_from_buffer(struct GNUNET_BIO_ReadHandle *h, const char *what, char *result, size_t len)
Function used internally to read the content of a buffer into a buffer.
Definition: bio.c:258
struct GNUNET_BIO_ReadSpec GNUNET_BIO_read_spec_meta_data(const char *what, struct GNUNET_CONTAINER_MetaData **result)
Create the specification to read a metadata container.
Definition: bio.c:1090
void * GNUNET_buffer_reap(struct GNUNET_Buffer *buf, size_t *size)
Clear the buffer and return its contents.
Definition: buffer.c:148
static char * fn
Filename of the unique file.
static int write_spec_handler_int32(void *cls, struct GNUNET_BIO_WriteHandle *h, const char *what, void *source, size_t source_size)
Function used internally to write an (u)int32_t from within a write spec.
Definition: bio.c:1406
Truncate file if it exists.
ssize_t GNUNET_DISK_file_write(const struct GNUNET_DISK_FileHandle *h, const void *buffer, size_t n)
Write a buffer to a file.
Definition: disk.c:820
static int write_spec_handler_meta_data(void *cls, struct GNUNET_BIO_WriteHandle *h, const char *what, void *source, size_t source_size)
Function used internally to write a metadata container from within a write spec.
Definition: bio.c:1361
static int read_spec_handler_string(void *cls, struct GNUNET_BIO_ReadHandle *h, const char *what, void *target, size_t target_size)
Function used interally to read a string from within a read spec.
Definition: bio.c:1023
The handle uses a file to read/write data.
Definition: bio.c:61
static char buf[2048]
size_t have
Number of bytes available in buffer.
Definition: bio.c:98
size_t have
Number of bytes available in buffer.
Definition: bio.c:542
static int result
Global testing status.
Structure specifying a reading operation on an IO handle.
int GNUNET_BIO_get_buffer_contents(struct GNUNET_BIO_WriteHandle *h, char **emsg, void **contents, size_t *size)
Get the IO handle&#39;s contents.
Definition: bio.c:688
Handle for buffered writing.
Definition: bio.c:515
struct GNUNET_BIO_WriteSpec GNUNET_BIO_write_spec_double(const char *what, double *f)
Create the specification to write an double.
Definition: bio.c:1515
char * buffer
I/O buffer.
Definition: bio.c:93
off_t pos
Current read offset in buffer.
Definition: bio.c:108
static int write_spec_handler_string(void *cls, struct GNUNET_BIO_WriteHandle *h, const char *what, void *source, size_t source_size)
Function used internally to write a 0-terminated string from within a write spec. ...
Definition: bio.c:1315
int GNUNET_BIO_read_string(struct GNUNET_BIO_ReadHandle *h, const char *what, char **result, size_t max_length)
Read 0-terminated string.
Definition: bio.c:326
struct GNUNET_BIO_ReadSpec GNUNET_BIO_read_spec_string(const char *what, char **result, size_t max_length)
Create the specification to read a 0-terminated string.
Definition: bio.c:1044
static GstElement * source
Appsrc instance into which we write data for the pipeline.
struct GNUNET_BIO_WriteSpec GNUNET_BIO_write_spec_meta_data(const char *what, const struct GNUNET_CONTAINER_MetaData *m)
Create the specification to write a metadata container.
Definition: bio.c:1380
uint64_t GNUNET_htonll(uint64_t n)
Convert unsigned 64-bit integer to network byte order.
Definition: common_endian.c:36
struct GNUNET_BIO_WriteHandle * GNUNET_BIO_write_open_file(const char *fn)
Open a file for writing.
Definition: bio.c:558
static int write_spec_handler_int64(void *cls, struct GNUNET_BIO_WriteHandle *h, const char *what, void *source, size_t source_size)
Function used internally to write an (u)int64_t from within a write spec.
Definition: bio.c:1451
Open the file for writing.
int GNUNET_BIO_write_close(struct GNUNET_BIO_WriteHandle *h, char **emsg)
Close an IO handle.
Definition: bio.c:607
struct GNUNET_BIO_ReadSpec GNUNET_BIO_read_spec_float(const char *what, float *f)
Create the specification to read a float.
Definition: bio.c:1199
enum IOType type
The "backend" type.
Definition: bio.c:78
int GNUNET_BIO_write_int32(struct GNUNET_BIO_WriteHandle *h, const char *what, int32_t i)
Write an (u)int32_t.
Definition: bio.c:935
struct GNUNET_BIO_ReadSpec GNUNET_BIO_read_spec_int32(const char *what, int32_t *i)
Create the specification to read an (u)int32_t.
Definition: bio.c:1134
GNUNET_BIO_WriteHandler wh
Function performing data serialization.
The data is stored entirely in memory.
Definition: bio.c:66
char * emsg
Error message, NULL if there were no errors.
Definition: bio.c:530
int GNUNET_BIO_write_spec_commit(struct GNUNET_BIO_WriteHandle *h, struct GNUNET_BIO_WriteSpec *ws)
Execute the write specifications in order.
Definition: bio.c:1538
struct GNUNET_BIO_WriteSpec GNUNET_BIO_write_spec_int32(const char *what, int32_t *i)
Create the specification to write an (u)int32_t.
Definition: bio.c:1425
int GNUNET_BIO_write_string(struct GNUNET_BIO_WriteHandle *h, const char *what, const char *s)
Write a 0-terminated string.
Definition: bio.c:831
int GNUNET_BIO_write_meta_data(struct GNUNET_BIO_WriteHandle *h, const char *what, const struct GNUNET_CONTAINER_MetaData *m)
Write a metadata container.
Definition: bio.c:855
static struct GNUNET_CONTAINER_MetaData * meta
Meta-data provided via command-line option.
struct GNUNET_BIO_WriteSpec GNUNET_BIO_write_spec_float(const char *what, float *f)
Create the specification to write a float.
Definition: bio.c:1493
int GNUNET_BIO_read_double(struct GNUNET_BIO_ReadHandle *h, const char *what, double *f)
Read a double.
Definition: bio.c:459
void GNUNET_buffer_clear(struct GNUNET_Buffer *buf)
Free the backing memory of the given buffer.
Definition: buffer.c:163
size_t size
Total size of buffer.
Definition: bio.c:547
static int read_spec_handler_int32(void *cls, struct GNUNET_BIO_ReadHandle *h, const char *what, void *target, size_t target_size)
Function used internally to read an (u)int32_t from within a read spec.
Definition: bio.c:1115
int GNUNET_BIO_read_meta_data(struct GNUNET_BIO_ReadHandle *h, const char *what, struct GNUNET_CONTAINER_MetaData **result)
Read a metadata container.
Definition: bio.c:389
struct GNUNET_BIO_WriteSpec GNUNET_BIO_write_spec_int64(const char *what, int64_t *i)
Create the specification to write an (u)int64_t.
Definition: bio.c:1470
Common buffer management functions.
struct GNUNET_DISK_FileHandle * GNUNET_DISK_file_open(const char *fn, enum GNUNET_DISK_OpenFlags flags, enum GNUNET_DISK_AccessPermissions perm)
Open a file.
Definition: disk.c:1203
const char * what
What is being read (for error message creation)
int GNUNET_BIO_write(struct GNUNET_BIO_WriteHandle *h, const char *what, const void *buffer, size_t n)
Write a buffer to a handle.
Definition: bio.c:794
char * emsg
Error message, NULL if there were no errors.
Definition: bio.c:88
Handle used to access files (and pipes).
int GNUNET_BIO_read_int64(struct GNUNET_BIO_ReadHandle *h, const char *what, int64_t *i)
Read an (u)int64_t.
Definition: bio.c:499
#define GNUNET_malloc(size)
Wrapper around malloc.
Handle for buffered reading.
Definition: bio.c:73
uint64_t GNUNET_ntohll(uint64_t n)
Convert unsigned 64-bit integer to host byte order.
Definition: common_endian.c:53
#define GNUNET_free(ptr)
Wrapper around free.
static int write_spec_handler_object(void *cls, struct GNUNET_BIO_WriteHandle *h, const char *what, void *source, size_t source_size)
Function used internally to write some bytes from within a write spec.
Definition: bio.c:1268
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
int GNUNET_BIO_write_float(struct GNUNET_BIO_WriteHandle *h, const char *what, float f)
Write a float.
Definition: bio.c:900
struct GNUNET_BIO_ReadSpec GNUNET_BIO_read_spec_double(const char *what, double *f)
Create the specification to read a double.
Definition: bio.c:1219