GNUnet  0.17.6
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
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;
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 
149  h->type = IO_BUFFER;
150  h->buffer = buffer;
151  h->size = size;
152  return h;
153 }
154 
155 
166 int
168 {
169  int err;
170 
171  err = (NULL == h->emsg) ? GNUNET_OK : GNUNET_SYSERR;
172  if (NULL != emsg)
173  *emsg = h->emsg;
174  else
175  GNUNET_free (h->emsg);
176  switch (h->type)
177  {
178  case IO_FILE:
180  break;
181  case IO_BUFFER:
182  break;
183  default:
184  break;
185  }
186  GNUNET_free (h);
187  return err;
188 }
189 
190 
200 static int
202  const char *what,
203  char *result,
204  size_t len)
205 {
206  size_t pos = 0;
207  size_t min;
208  ssize_t ret;
209 
210  do
211  {
212  min = h->have - h->pos;
213  if (0 < min)
214  {
215  if (len - pos < min)
216  min = len - pos;
217  GNUNET_memcpy (&result[pos], &h->buffer[h->pos], min);
218  h->pos += min;
219  pos += min;
220  }
221  if (len == pos)
222  return GNUNET_OK;
223  GNUNET_assert (((off_t) h->have) == h->pos);
224  ret = GNUNET_DISK_file_read (h->fd, h->buffer, h->size);
225  if (-1 == ret)
226  {
227  GNUNET_asprintf (&h->emsg,
228  _ ("Error reading `%s' from file: %s"),
229  what,
230  strerror (errno));
231  return GNUNET_SYSERR;
232  }
233  if (0 == ret)
234  {
235  GNUNET_asprintf (&h->emsg,
236  _ ("Error reading `%s' from file: %s"),
237  what,
238  _ ("End of file"));
239  return GNUNET_SYSERR;
240  }
241  h->pos = 0;
242  h->have = ret;
243  }
244  while (pos < len);
245  return GNUNET_OK;
246 }
247 
248 
258 static int
260  const char *what,
261  char *result,
262  size_t len)
263 {
264  if ((h->size < len) || (h->size - h->pos < len))
265  {
266  GNUNET_asprintf (&h->emsg,
267  _ ("Error while reading `%s' from buffer: %s"),
268  what,
269  _ ("Not enough data left"));
270  return GNUNET_SYSERR;
271  }
272  GNUNET_memcpy (result, h->buffer + h->pos, len);
273  h->pos += len;
274  return GNUNET_OK;
275 }
276 
277 
287 int
289  const char *what,
290  void *result,
291  size_t len)
292 {
293  char *dst = result;
294 
295  if (NULL != h->emsg)
296  return GNUNET_SYSERR;
297 
298  if (0 == len)
299  return GNUNET_OK;
300 
301  switch (h->type)
302  {
303  case IO_FILE:
304  return read_from_file (h, what, dst, len);
305  case IO_BUFFER:
306  return read_from_buffer (h, what, dst, len);
307  default:
308  GNUNET_asprintf (&h->emsg,
309  _ ("Invalid handle type while reading `%s'"),
310  what);
311  return GNUNET_SYSERR;
312  }
313 }
314 
315 
326 int
328  const char *what,
329  char **result,
330  size_t max_length)
331 {
332  char *buf;
333  uint32_t big;
334 
336  _ ("string length"),
337  (int32_t *) &big))
338  {
339  char *tmp = h->emsg;
340  if (NULL != tmp)
341  GNUNET_asprintf (&h->emsg,
342  _ ("%s (while reading `%s')"),
343  tmp,
344  what);
345  else
346  GNUNET_asprintf (&h->emsg,
347  _ ("Error reading length of string `%s'"),
348  what);
349  GNUNET_free (tmp);
350  return GNUNET_SYSERR;
351  }
352  if (0 == big)
353  {
354  *result = NULL;
355  return GNUNET_OK;
356  }
357  if (big > max_length)
358  {
359  GNUNET_asprintf (&h->emsg,
360  _ ("String `%s' longer than allowed (%u > %lu)"),
361  what,
362  big,
363  (unsigned long) max_length);
364  return GNUNET_SYSERR;
365  }
366  buf = GNUNET_malloc (big);
367  *result = buf;
368  buf[--big] = '\0';
369  if (0 == big)
370  return GNUNET_OK;
371  if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, big))
372  {
373  GNUNET_free (buf);
374  *result = NULL;
375  return GNUNET_SYSERR;
376  }
377  return GNUNET_OK;
378 }
379 
380 
389 int
391  const char *what,
393 {
394  uint32_t size;
395  char *buf;
397 
399  _ ("metadata length"),
400  (int32_t *) &size))
401  return GNUNET_SYSERR;
402  if (0 == size)
403  {
404  *result = NULL;
405  return GNUNET_OK;
406  }
407  if (MAX_META_DATA < size)
408  {
410  &h->emsg,
411  _ ("Serialized metadata `%s' larger than allowed (%u > %u)"),
412  what,
413  size,
414  MAX_META_DATA);
415  return GNUNET_SYSERR;
416  }
417  buf = GNUNET_malloc (size);
418  if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, size))
419  {
420  GNUNET_free (buf);
421  return GNUNET_SYSERR;
422  }
424  if (NULL == meta)
425  {
426  GNUNET_free (buf);
427  GNUNET_asprintf (&h->emsg, _ ("Failed to deserialize metadata `%s'"), what);
428  return GNUNET_SYSERR;
429  }
430  GNUNET_free (buf);
431  *result = meta;
432  return GNUNET_OK;
433 }
434 
435 
443 int
445  const char *what,
446  float *f)
447 {
448  int32_t *i = (int32_t *) f;
449  return GNUNET_BIO_read_int32 (h, what, i);
450 }
451 
452 
460 int
462  const char *what,
463  double *f)
464 {
465  int64_t *i = (int64_t *) f;
466  return GNUNET_BIO_read_int64 (h, what, i);
467 }
468 
469 
478 int
480  const char *what,
481  int32_t *i)
482 {
483  int32_t big;
484 
485  if (GNUNET_OK != GNUNET_BIO_read (h, what, &big, sizeof(int32_t)))
486  return GNUNET_SYSERR;
487  *i = ntohl (big);
488  return GNUNET_OK;
489 }
490 
491 
500 int
502  const char *what,
503  int64_t *i)
504 {
505  int64_t big;
506 
507  if (GNUNET_OK != GNUNET_BIO_read (h, what, &big, sizeof(int64_t)))
508  return GNUNET_SYSERR;
509  *i = GNUNET_ntohll (big);
510  return GNUNET_OK;
511 }
512 
513 
518 {
522  enum IOType type;
523 
528 
532  char *emsg;
533 
539  void *buffer;
540 
544  size_t have;
545 
549  size_t size;
550 };
551 
552 
559 struct GNUNET_BIO_WriteHandle *
561 {
562  struct GNUNET_DISK_FileHandle *fd;
563  struct GNUNET_BIO_WriteHandle *h;
564 
565  fd =
572  if (NULL == fd)
573  return NULL;
575  h->buffer = &h[1];
576  h->size = BIO_BUFFER_SIZE;
577  h->fd = fd;
578  return h;
579 }
580 
581 
587 struct GNUNET_BIO_WriteHandle *
589 {
590  struct GNUNET_BIO_WriteHandle *h;
591 
593  h->type = IO_BUFFER;
594  h->buffer = (void *) GNUNET_malloc (sizeof (struct GNUNET_Buffer));
595  return h;
596 }
597 
598 
608 int
610 {
611  int err;
612 
613  err = (NULL == h->emsg) ? GNUNET_OK : GNUNET_SYSERR;
614  if (NULL != emsg)
615  *emsg = h->emsg;
616  else
617  GNUNET_free (h->emsg);
618  switch (h->type)
619  {
620  case IO_FILE:
621  if (NULL == h->fd)
622  return GNUNET_SYSERR;
623  if (GNUNET_OK != GNUNET_BIO_flush (h))
624  {
625  if (NULL != emsg)
626  *emsg = h->emsg;
627  else
628  GNUNET_free (h->emsg);
629  err = GNUNET_SYSERR;
630  }
631  else
632  {
634  }
635  break;
636  case IO_BUFFER:
637  GNUNET_buffer_clear ((struct GNUNET_Buffer *) h->buffer);
638  GNUNET_free (h->buffer);
639  break;
640  }
641  GNUNET_free (h);
642  return err;
643 }
644 
645 
655 int
657 {
658  ssize_t ret;
659 
660  if (IO_FILE != h->type)
661  return GNUNET_OK;
662 
663  ret = GNUNET_DISK_file_write (h->fd, h->buffer, h->have);
664  if (ret != (ssize_t) h->have)
665  {
667  h->fd = NULL;
668  GNUNET_free (h->emsg);
669  GNUNET_asprintf (&h->emsg, _ ("Unable to flush buffer to file"));
670  return GNUNET_SYSERR;
671  }
672  h->have = 0;
673  return GNUNET_OK;
674 }
675 
676 
689 int
691  char **emsg,
692  void **contents,
693  size_t *size)
694 {
695  if (IO_BUFFER != h->type)
696  return GNUNET_SYSERR;
697  if ((NULL == contents) || (NULL == size))
698  return GNUNET_SYSERR;
699  int ret = (NULL != h->emsg) ? GNUNET_SYSERR : GNUNET_OK;
700  if (NULL != emsg)
701  *emsg = h->emsg;
702  else
703  GNUNET_free (h->emsg);
704  *contents = GNUNET_buffer_reap ((struct GNUNET_Buffer *) h->buffer, size);
705  return ret;
706 }
707 
708 
718 static int
720  const char *what,
721  const char *source,
722  size_t len)
723 {
724  size_t min;
725  size_t pos = 0;
726  char *buffer = (char *) h->buffer;
727 
728  if (NULL == h->fd)
729  {
730  GNUNET_asprintf (&h->emsg,
731  _ ("Error while writing `%s' to file: %s"),
732  what,
733  _ ("No associated file"));
734  return GNUNET_SYSERR;
735  }
736 
737  do
738  {
739  min = h->size - h->have;
740  if (len - pos < min)
741  min = len - pos;
742  GNUNET_memcpy (&buffer[h->have], &source[pos], min);
743  pos += min;
744  h->have += min;
745  if (len == pos)
746  return GNUNET_OK;
747  GNUNET_assert (h->have == h->size);
748  if (GNUNET_OK != GNUNET_BIO_flush (h))
749  {
750  char *tmp = h->emsg;
751  GNUNET_asprintf (&h->emsg,
752  _ ("Error while writing `%s' to file: %s"),
753  what,
754  tmp);
755  GNUNET_free (tmp);
756  return GNUNET_SYSERR;
757  }
758  }
759  while (pos < len);
760  GNUNET_break (0);
761  return GNUNET_OK;
762 }
763 
764 
774 static int
776  const char *what,
777  const char *source,
778  size_t len)
779 {
780  GNUNET_buffer_write ((struct GNUNET_Buffer *) h->buffer, source, len);
781  h->have += len;
782  return GNUNET_OK;
783 }
784 
785 
795 int
797  const char *what,
798  const void *buffer,
799  size_t n)
800 {
801  const char *src = buffer;
802 
803  if (NULL != h->emsg)
804  return GNUNET_SYSERR;
805 
806  if (0 == n)
807  return GNUNET_OK;
808 
809  switch (h->type)
810  {
811  case IO_FILE:
812  return write_to_file (h, what, src, n);
813  case IO_BUFFER:
814  return write_to_buffer (h, what, src, n);
815  default:
816  GNUNET_asprintf (&h->emsg,
817  _ ("Invalid handle type while writing `%s'"),
818  what);
819  return GNUNET_SYSERR;
820  }
821 }
822 
823 
832 int
834  const char *what,
835  const char *s)
836 {
837  uint32_t slen;
838 
839  slen = (uint32_t) ((s == NULL) ? 0 : strlen (s) + 1);
840  if (GNUNET_OK != GNUNET_BIO_write_int32 (h, _ ("string length"), slen))
841  return GNUNET_SYSERR;
842  if (0 != slen)
843  return GNUNET_BIO_write (h, what, s, slen - 1);
844  return GNUNET_OK;
845 }
846 
847 
856 int
858  const char *what,
859  const struct GNUNET_CONTAINER_MetaData *m)
860 {
861  ssize_t size;
862  char *buf;
863 
864  if (m == NULL)
865  return GNUNET_BIO_write_int32 (h, _ ("metadata length"), 0);
866  buf = NULL;
868  m,
869  &buf,
872  if (-1 == size)
873  {
874  GNUNET_free (buf);
875  GNUNET_free (h->emsg);
876  GNUNET_asprintf (&h->emsg,
877  _ ("Failed to serialize metadata `%s'"),
878  what);
879  return GNUNET_SYSERR;
880  }
882  _ ("metadata length"),
883  (uint32_t) size))
884  || (GNUNET_OK != GNUNET_BIO_write (h, what, buf, size)))
885  {
886  GNUNET_free (buf);
887  return GNUNET_SYSERR;
888  }
889  GNUNET_free (buf);
890  return GNUNET_OK;
891 }
892 
893 
901 int
903  const char *what,
904  float f)
905 {
906  int32_t i = f;
907  return GNUNET_BIO_write_int32 (h, what, i);
908 }
909 
910 
918 int
920  const char *what,
921  double f)
922 {
923  int64_t i = f;
924  return GNUNET_BIO_write_int64 (h, what, i);
925 }
926 
927 
936 int
938  const char *what,
939  int32_t i)
940 {
941  int32_t big;
942 
943  big = htonl (i);
944  return GNUNET_BIO_write (h, what, &big, sizeof(int32_t));
945 }
946 
947 
956 int
958  const char *what,
959  int64_t i)
960 {
961  int64_t big;
962 
963  big = GNUNET_htonll (i);
964  return GNUNET_BIO_write (h, what, &big, sizeof(int64_t));
965 }
966 
967 
978 static int
980  struct GNUNET_BIO_ReadHandle *h,
981  const char *what,
982  void *target,
983  size_t target_size)
984 {
985  return GNUNET_BIO_read (h, what, target, target_size);
986 }
987 
988 
997 struct GNUNET_BIO_ReadSpec
998 GNUNET_BIO_read_spec_object (const char *what,
999  void *result,
1000  size_t len)
1001 {
1002  struct GNUNET_BIO_ReadSpec rs = {
1004  .cls = NULL,
1005  .what = what,
1006  .target = result,
1007  .size = len,
1008  };
1009 
1010  return rs;
1011 }
1012 
1013 
1024 static int
1026  struct GNUNET_BIO_ReadHandle *h,
1027  const char *what,
1028  void *target,
1029  size_t target_size)
1030 {
1031  char **result = target;
1032  return GNUNET_BIO_read_string (h, what, result, target_size);
1033 }
1034 
1035 
1045 struct GNUNET_BIO_ReadSpec
1046 GNUNET_BIO_read_spec_string (const char *what,
1047  char **result,
1048  size_t max_length)
1049 {
1050  struct GNUNET_BIO_ReadSpec rs = {
1052  .cls = NULL,
1053  .target = result,
1054  .size = max_length,
1055  };
1056 
1057  return rs;
1058 }
1059 
1060 
1072 static int
1074  struct GNUNET_BIO_ReadHandle *h,
1075  const char *what,
1076  void *target,
1077  size_t target_size)
1078 {
1079  struct GNUNET_CONTAINER_MetaData **result = target;
1080  return GNUNET_BIO_read_meta_data (h, what, result);
1081 }
1082 
1083 
1091 struct GNUNET_BIO_ReadSpec
1094 {
1095  struct GNUNET_BIO_ReadSpec rs = {
1097  .cls = NULL,
1098  .target = result,
1099  .size = 0,
1100  };
1101 
1102  return rs;
1103 }
1104 
1105 
1116 static int
1118  struct GNUNET_BIO_ReadHandle *h,
1119  const char *what,
1120  void *target,
1121  size_t target_size)
1122 {
1123  int32_t *result = target;
1124  return GNUNET_BIO_read_int32 (h, what, result);
1125 }
1126 
1127 
1135 struct GNUNET_BIO_ReadSpec
1136 GNUNET_BIO_read_spec_int32 (const char *what,
1137  int32_t *i)
1138 {
1139  struct GNUNET_BIO_ReadSpec rs = {
1141  .cls = NULL,
1142  .target = i,
1143  .size = 0,
1144  };
1145 
1146  return rs;
1147 }
1148 
1149 
1160 static int
1162  struct GNUNET_BIO_ReadHandle *h,
1163  const char *what,
1164  void *target,
1165  size_t target_size)
1166 {
1167  int64_t *result = target;
1168  return GNUNET_BIO_read_int64 (h, what, result);
1169 }
1170 
1171 
1179 struct GNUNET_BIO_ReadSpec
1180 GNUNET_BIO_read_spec_int64 (const char *what,
1181  int64_t *i)
1182 {
1183  struct GNUNET_BIO_ReadSpec rs = {
1185  .cls = NULL,
1186  .target = i,
1187  .size = 0,
1188  };
1189 
1190  return rs;
1191 }
1192 
1193 
1200 struct GNUNET_BIO_ReadSpec
1201 GNUNET_BIO_read_spec_float (const char *what, float *f)
1202 {
1203  struct GNUNET_BIO_ReadSpec rs = {
1205  .cls = NULL,
1206  .target = (int32_t *) f,
1207  .size = 0,
1208  };
1209 
1210  return rs;
1211 }
1212 
1213 
1220 struct GNUNET_BIO_ReadSpec
1221 GNUNET_BIO_read_spec_double (const char *what, double *f)
1222 {
1223  struct GNUNET_BIO_ReadSpec rs = {
1225  .cls = NULL,
1226  .target = (int64_t *) f,
1227  .size = 0,
1228  };
1229 
1230  return rs;
1231 }
1232 
1233 
1242 int
1244  struct GNUNET_BIO_ReadSpec *rs)
1245 {
1246  int ret = GNUNET_OK;
1247 
1248  for (size_t i = 0; NULL!=rs[i].rh; ++i)
1249  {
1250  ret = rs[i].rh (rs[i].cls, h, rs[i].what, rs[i].target, rs[i].size);
1251  if (GNUNET_OK != ret)
1252  return ret;
1253  }
1254 
1255  return ret;
1256 }
1257 
1258 
1269 static int
1271  struct GNUNET_BIO_WriteHandle *h,
1272  const char *what,
1273  void *source,
1274  size_t source_size)
1275 {
1276  return GNUNET_BIO_write (h, what, source, source_size);
1277 }
1278 
1279 
1288 struct GNUNET_BIO_WriteSpec
1289 GNUNET_BIO_write_spec_object (const char *what,
1290  void *source,
1291  size_t size)
1292 {
1293  struct GNUNET_BIO_WriteSpec ws = {
1295  .cls = NULL,
1296  .what = what,
1297  .source = source,
1298  .source_size = size,
1299  };
1300 
1301  return ws;
1302 }
1303 
1304 
1316 static int
1318  struct GNUNET_BIO_WriteHandle *h,
1319  const char *what,
1320  void *source,
1321  size_t source_size)
1322 {
1323  const char *s = source;
1324  return GNUNET_BIO_write_string (h, what, s);
1325 }
1326 
1327 
1335 struct GNUNET_BIO_WriteSpec
1336 GNUNET_BIO_write_spec_string (const char *what,
1337  const char *s)
1338 {
1339  struct GNUNET_BIO_WriteSpec ws = {
1341  .cls = NULL,
1342  .what = what,
1343  .source = (void *) s,
1344  .source_size = 0,
1345  };
1346 
1347  return ws;
1348 }
1349 
1350 
1362 static int
1364  struct GNUNET_BIO_WriteHandle *h,
1365  const char *what,
1366  void *source,
1367  size_t source_size)
1368 {
1369  const struct GNUNET_CONTAINER_MetaData *m = source;
1370  return GNUNET_BIO_write_meta_data (h, what, m);
1371 }
1372 
1373 
1381 struct GNUNET_BIO_WriteSpec
1383  const struct GNUNET_CONTAINER_MetaData *m)
1384 {
1385  struct GNUNET_BIO_WriteSpec ws = {
1387  .cls = NULL,
1388  .what = what,
1389  .source = (void *) m,
1390  .source_size = 0,
1391  };
1392 
1393  return ws;
1394 }
1395 
1396 
1407 static int
1409  struct GNUNET_BIO_WriteHandle *h,
1410  const char *what,
1411  void *source,
1412  size_t source_size)
1413 {
1414  int32_t i = *(int32_t *) source;
1415  return GNUNET_BIO_write_int32 (h, what, i);
1416 }
1417 
1418 
1426 struct GNUNET_BIO_WriteSpec
1427 GNUNET_BIO_write_spec_int32 (const char *what,
1428  int32_t *i)
1429 {
1430  struct GNUNET_BIO_WriteSpec ws = {
1432  .cls = NULL,
1433  .what = what,
1434  .source = i,
1435  .source_size = 0,
1436  };
1437 
1438  return ws;
1439 }
1440 
1441 
1452 static int
1454  struct GNUNET_BIO_WriteHandle *h,
1455  const char *what,
1456  void *source,
1457  size_t source_size)
1458 {
1459  int64_t i = *(int64_t *) source;
1460  return GNUNET_BIO_write_int64 (h, what, i);
1461 }
1462 
1463 
1471 struct GNUNET_BIO_WriteSpec
1472 GNUNET_BIO_write_spec_int64 (const char *what,
1473  int64_t *i)
1474 {
1475  struct GNUNET_BIO_WriteSpec ws = {
1477  .cls = NULL,
1478  .what = what,
1479  .source = i,
1480  .source_size = 0,
1481  };
1482 
1483  return ws;
1484 }
1485 
1486 
1494 struct GNUNET_BIO_WriteSpec
1495 GNUNET_BIO_write_spec_float (const char *what, float *f)
1496 {
1497  struct GNUNET_BIO_WriteSpec ws = {
1499  .cls = NULL,
1500  .what = what,
1501  .source = (int32_t *) f,
1502  .source_size = 0,
1503  };
1504 
1505  return ws;
1506 }
1507 
1508 
1516 struct GNUNET_BIO_WriteSpec
1517 GNUNET_BIO_write_spec_double (const char *what, double *f)
1518 {
1519  struct GNUNET_BIO_WriteSpec ws = {
1521  .cls = NULL,
1522  .what = what,
1523  .source = (int64_t *) f,
1524  .source_size = 0,
1525  };
1526 
1527  return ws;
1528 }
1529 
1530 
1539 int
1541  struct GNUNET_BIO_WriteSpec *ws)
1542 {
1543  int ret = GNUNET_OK;
1544 
1545  for (size_t i = 0; NULL!=ws[i].wh; ++i)
1546  {
1547  ret = ws[i].wh (ws[i].cls, h, ws[i].what, ws[i].source, ws[i].source_size);
1548  if (GNUNET_OK != ret)
1549  return ret;
1550  }
1551 
1552  /* If it's a file-based handle, the flush makes sure that the data in the
1553  buffer is actually written to the disk. */
1554  if (IO_FILE == h->type)
1555  ret = GNUNET_BIO_flush (h);
1556 
1557  return ret;
1558 }
1559 
1560 
1561 /* end of bio.c */
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:1453
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:979
#define BIO_BUFFER_SIZE
Size for I/O buffers.
Definition: bio.c:41
IOType
Enum used internally to know how buffering is handled.
Definition: bio.c:57
@ IO_BUFFER
The data is stored entirely in memory.
Definition: bio.c:66
@ IO_FILE
The handle uses a file to read/write data.
Definition: bio.c:61
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:1161
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:1270
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:1317
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:259
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:1363
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:1073
#define MAX_META_DATA
Maximum size allowed for meta data written/read from disk.
Definition: bio.c:47
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:719
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:1117
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:775
static int read_spec_handler_string(void *cls, struct GNUNET_BIO_ReadHandle *h, const char *what, void *target, size_t target_size)
Function used internally to read a string from within a read spec.
Definition: bio.c:1025
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:1408
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:201
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static struct GNUNET_ARM_MonitorHandle * m
Monitor connection with ARM.
Definition: gnunet-arm.c:104
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:99
static GstElement * source
Appsrc instance into which we write data for the pipeline.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
static struct GNUNET_CONTAINER_MetaData * meta
Meta-data provided via command-line option.
static int result
Global testing status.
static char buf[2048]
int GNUNET_BIO_write_float(struct GNUNET_BIO_WriteHandle *h, const char *what, float f)
Write a float.
Definition: bio.c:902
int GNUNET_BIO_read_float(struct GNUNET_BIO_ReadHandle *h, const char *what, float *f)
Read a float.
Definition: bio.c:444
struct GNUNET_BIO_ReadSpec GNUNET_BIO_read_spec_float(const char *what, float *f)
Create the specification to read a float.
Definition: bio.c:1201
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:1046
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:327
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:390
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:1540
struct GNUNET_BIO_WriteHandle * GNUNET_BIO_write_open_buffer(void)
Create a handle backed by an in-memory buffer.
Definition: bio.c:588
int GNUNET_BIO_read_int32(struct GNUNET_BIO_ReadHandle *h, const char *what, int32_t *i)
Read an (u)int32_t.
Definition: bio.c:479
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(struct GNUNET_BIO_WriteHandle *h, const char *what, const void *buffer, size_t n)
Write a buffer to a handle.
Definition: bio.c:796
int GNUNET_BIO_read_int64(struct GNUNET_BIO_ReadHandle *h, const char *what, int64_t *i)
Read an (u)int64_t.
Definition: bio.c:501
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:998
int GNUNET_BIO_write_string(struct GNUNET_BIO_WriteHandle *h, const char *what, const char *s)
Write a 0-terminated string.
Definition: bio.c:833
int GNUNET_BIO_write_close(struct GNUNET_BIO_WriteHandle *h, char **emsg)
Close an IO handle.
Definition: bio.c:609
int GNUNET_BIO_write_int32(struct GNUNET_BIO_WriteHandle *h, const char *what, int32_t i)
Write an (u)int32_t.
Definition: bio.c:937
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:1336
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:1243
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:1136
int GNUNET_BIO_get_buffer_contents(struct GNUNET_BIO_WriteHandle *h, char **emsg, void **contents, size_t *size)
Get the IO handle's contents.
Definition: bio.c:690
int GNUNET_BIO_write_double(struct GNUNET_BIO_WriteHandle *h, const char *what, double f)
Write a double.
Definition: bio.c:919
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
int GNUNET_BIO_read_close(struct GNUNET_BIO_ReadHandle *h, char **emsg)
Close an open handle.
Definition: bio.c:167
struct GNUNET_BIO_WriteSpec GNUNET_BIO_write_spec_double(const char *what, double *f)
Create the specification to write an double.
Definition: bio.c:1517
struct GNUNET_BIO_WriteSpec GNUNET_BIO_write_spec_float(const char *what, float *f)
Create the specification to write a float.
Definition: bio.c:1495
int GNUNET_BIO_write_int64(struct GNUNET_BIO_WriteHandle *h, const char *what, int64_t i)
Write an (u)int64_t.
Definition: bio.c:957
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:288
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:1289
struct GNUNET_BIO_WriteHandle * GNUNET_BIO_write_open_file(const char *fn)
Open a file for writing.
Definition: bio.c:560
struct GNUNET_BIO_ReadSpec GNUNET_BIO_read_spec_double(const char *what, double *f)
Create the specification to read a double.
Definition: bio.c:1221
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:1382
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:1092
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:1427
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:857
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:1180
int GNUNET_BIO_flush(struct GNUNET_BIO_WriteHandle *h)
Force a file-based buffered writer to flush its buffer.
Definition: bio.c:656
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:1472
int GNUNET_BIO_read_double(struct GNUNET_BIO_ReadHandle *h, const char *what, double *f)
Read a double.
Definition: bio.c:461
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:1235
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:686
enum GNUNET_GenericReturnValue GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1306
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:622
@ GNUNET_DISK_OPEN_READ
Open the file for reading.
@ GNUNET_DISK_OPEN_WRITE
Open the file for writing.
@ GNUNET_DISK_OPEN_TRUNCATE
Truncate file if it exists.
@ GNUNET_DISK_OPEN_CREATE
Create file if it doesn't exist.
@ GNUNET_DISK_PERM_USER_READ
Owner can read.
@ GNUNET_DISK_PERM_NONE
Nobody is allowed to do anything to the file.
@ GNUNET_DISK_PERM_USER_WRITE
Owner can write.
void * GNUNET_buffer_reap(struct GNUNET_Buffer *buf, size_t *size)
Clear the buffer and return its contents.
Definition: buffer.c:149
uint64_t GNUNET_ntohll(uint64_t n)
Convert unsigned 64-bit integer to host byte order.
Definition: common_endian.c:53
uint64_t GNUNET_htonll(uint64_t n)
Convert unsigned 64-bit integer to network byte order.
Definition: common_endian.c:36
void GNUNET_buffer_write(struct GNUNET_Buffer *buf, const char *data, size_t len)
Write bytes to the buffer.
Definition: buffer.c:86
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
void GNUNET_buffer_clear(struct GNUNET_Buffer *buf)
Free the backing memory of the given buffer.
Definition: buffer.c:164
@ GNUNET_OK
Definition: gnunet_common.h:99
@ GNUNET_SYSERR
Definition: gnunet_common.h:97
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
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.
struct GNUNET_CONTAINER_MetaData * GNUNET_CONTAINER_meta_data_deserialize(const char *input, size_t size)
Deserialize meta-data.
@ GNUNET_CONTAINER_META_DATA_SERIALIZE_PART
If not enough space is available, it is acceptable to only serialize some of the metadata.
#define min(x, y)
static unsigned int size
Size of the "table".
Definition: peer.c:67
#define _(String)
GNU gettext support macro.
Definition: platform.h:177
Handle for buffered reading.
Definition: bio.c:74
off_t pos
Current read offset in buffer.
Definition: bio.c:108
size_t size
Total size of buffer.
Definition: bio.c:103
size_t have
Number of bytes available in buffer.
Definition: bio.c:98
char * emsg
Error message, NULL if there were no errors.
Definition: bio.c:88
enum IOType type
The "backend" type.
Definition: bio.c:78
struct GNUNET_DISK_FileHandle * fd
Handle to a file on disk, if type is IO_FILE.
Definition: bio.c:83
char * buffer
I/O buffer.
Definition: bio.c:93
Structure specifying a reading operation on an IO handle.
void * cls
Closure for rh.
const char * what
What is being read (for error message creation)
GNUNET_BIO_ReadHandler rh
Function performing data deserialization.
void * target
Destination buffer.
Handle for buffered writing.
Definition: bio.c:518
char * emsg
Error message, NULL if there were no errors.
Definition: bio.c:532
size_t have
Number of bytes available in buffer.
Definition: bio.c:544
struct GNUNET_DISK_FileHandle * fd
Handle to a file on disk, if type is IO_FILE.
Definition: bio.c:527
size_t size
Total size of buffer.
Definition: bio.c:549
enum IOType type
The "backend" type.
Definition: bio.c:522
void * buffer
I/O buffer.
Definition: bio.c:539
Structure specifying a writing operation on an IO handle.
GNUNET_BIO_WriteHandler wh
Function performing data serialization.
size_t source_size
Size of source.
void * cls
Closure for rh.
const char * what
What is being read (for error message creation)
Dynamically growing buffer.
Meta data to associate with a file, directory or namespace.
Handle used to access files (and pipes).
int fd
File handle on Unix-like systems.