- wb->have += min;
- if (pos == size)
- return; /* done */
- GNUNET_GE_ASSERT (NULL, wb->have == wb->size);
- ret = WRITE (wb->fd, wb->buffer, wb->size);
- if (ret != wb->size)
- {
- CLOSE (wb->fd);
- wb->fd = -1;
- return; /* error */
- }
- wb->have = 0;
- }
- while (pos < size); /* should always be true */
-}
-
-
-static void
-WRITEINT (WriteBuffer * wb, int val)
-{
- int big;
- big = htonl (val);
- write_buffered (wb, &big, sizeof (int));
-}
-
-static void
-WRITELONG (WriteBuffer * wb, long long val)
-{
- long long big;
- big = GNUNET_htonll (val);
- write_buffered (wb, &big, sizeof (long long));
-}
-
-static void
-writeURI (WriteBuffer * wb, const struct GNUNET_ECRS_URI *uri)
-{
- char *buf;
- unsigned int size;
-
- buf = GNUNET_ECRS_uri_to_string (uri);
- size = strlen (buf);
- WRITEINT (wb, size);
- write_buffered (wb, buf, size);
- GNUNET_free (buf);
-}
-
-static void
-WRITESTRING (WriteBuffer * wb, const char *name)
-{
- GNUNET_GE_BREAK (NULL, name != NULL);
- WRITEINT (wb, strlen (name));
- write_buffered (wb, name, strlen (name));
-}
-
-static void
-writeMetaData (struct GNUNET_GE_Context *ectx,
- WriteBuffer * wb, const struct GNUNET_MetaData *meta)
-{
- unsigned int size;
- char *buf;
-
- size = GNUNET_meta_data_get_serialized_size (meta,
- GNUNET_SERIALIZE_FULL
- |
- GNUNET_SERIALIZE_NO_COMPRESS);
- if (size > 1024 * 1024)
- size = 1024 * 1024;
- buf = GNUNET_malloc (size);
- GNUNET_meta_data_serialize (ectx,
- meta,
- buf,
- size,
- GNUNET_SERIALIZE_PART |
- GNUNET_SERIALIZE_NO_COMPRESS);
- WRITEINT (wb, size);
- write_buffered (wb, buf, size);
- GNUNET_free (buf);
-}
-
-
-static void
-writeFileInfo (struct GNUNET_GE_Context *ectx, WriteBuffer * wb,
- const GNUNET_ECRS_FileInfo * fi)
-{
- writeMetaData (ectx, wb, fi->meta);
- writeURI (wb, fi->uri);
-}
-
-
-
-
-typedef struct
-{
- int fd;
- unsigned int have;
- unsigned int size;
- unsigned int pos;
- char *buffer;
-} ReadBuffer;
-
-static int
-read_buffered (ReadBuffer * rb, void *d, unsigned int size)
-{
- char *dst = d;
- unsigned int min;
- unsigned int pos;
- int ret;
-
- if (rb->fd == -1)
- return -1;
- pos = 0;
- do
- {
- /* first, use buffer */
- min = rb->have - rb->pos;
- if (min > 0)
- {
- if (min > size - pos)
- min = size - pos;
- memcpy (&dst[pos], &rb->buffer[rb->pos], min);
- rb->pos += min;
- pos += min;
- }
- if (pos == size)
- return pos; /* done! */
- GNUNET_GE_ASSERT (NULL, rb->have == rb->pos);
- /* fill buffer */
- ret = READ (rb->fd, rb->buffer, rb->size);
- if (ret == -1)