-Merge branch 'master' of ssh://gnunet.org/gnunet into gsoc2018/rest_api
[oweals/gnunet.git] / src / util / bio.c
index 060b6f94b8f13f44f01affd454d0d1c988f16b93..a31e5448f383cbd6cdd6b83769752c48a92a1fe2 100644 (file)
@@ -1,31 +1,48 @@
 /*
      This file is part of GNUnet.
-     (C) 2006, 2009 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2006, 2009, 2013 GNUnet e.V.
 
-     GNUnet is free software; you can redistribute it and/or modify
-     it under the terms of the GNU General Public License as published
-     by the Free Software Foundation; either version 2, or (at your
-     option) any later version.
+     GNUnet is free software: you can redistribute it and/or modify it
+     under the terms of the GNU Affero General Public License as published
+     by the Free Software Foundation, either version 3 of the License,
+     or (at your option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
      WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-     General Public License for more details.
-
-     You should have received a copy of the GNU General Public License
-     along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-     Boston, MA 02111-1307, USA.
+     Affero General Public License for more details.
+    
+     You should have received a copy of the GNU Affero General Public License
+     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
-
-
 /**
  * @file util/bio.c
  * @brief functions for buffering IO
  * @author Christian Grothoff
  */
 #include "platform.h"
-#include "gnunet_bio_lib.h"
+#include "gnunet_util_lib.h"
+
+#define LOG(kind,...) GNUNET_log_from (kind, "util-bio",__VA_ARGS__)
+
+#ifndef PATH_MAX
+/**
+ * Assumed maximum path length (for source file names).
+ */
+#define PATH_MAX 4096
+#endif
+
+
+/**
+ * Size for I/O buffers.
+ */
+#define BIO_BUFFER_SIZE 65536
+
+/**
+ * Maximum size allowed for meta data written/read from disk.
+ * File-sharing limits to 64k, so this should be rather generous.
+ */
+#define MAX_META_DATA (1024 * 1024)
 
 
 /**
  */
 struct GNUNET_BIO_ReadHandle
 {
+  /**
+   * Underlying file abstraction.
+   */
+  struct GNUNET_DISK_FileHandle *fd;
+
+  /**
+   * Error message, NULL if there were no errors.
+   */
+  char *emsg;
+
+  /**
+   * I/O buffer.  Allocated at the end of the struct, do not free!
+   */
+  char *buffer;
+
+  /**
+   * Number of bytes available in read @e buffer.
+   */
+  size_t have;
+
+  /**
+   * Total size of @e buffer.
+   */
+  size_t size;
+
+  /**
+   * Current read offset in @e buffer.
+   */
+  off_t pos;
 };
 
 
@@ -42,9 +88,20 @@ struct GNUNET_BIO_ReadHandle
  * @param fn file name to be opened
  * @return IO handle on success, NULL on error
  */
-struct GNUNET_BIO_ReadHandle *GNUNET_BIO_read_open (const char *fn)
+struct GNUNET_BIO_ReadHandle *
+GNUNET_BIO_read_open (const char *fn)
 {
-  return NULL;
+  struct GNUNET_DISK_FileHandle *fd;
+  struct GNUNET_BIO_ReadHandle *h;
+
+  fd = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE);
+  if (NULL == fd)
+    return NULL;
+  h = GNUNET_malloc (sizeof (struct GNUNET_BIO_ReadHandle) + BIO_BUFFER_SIZE);
+  h->buffer = (char *) &h[1];
+  h->size = BIO_BUFFER_SIZE;
+  h->fd = fd;
+  return h;
 }
 
 
@@ -54,12 +111,22 @@ struct GNUNET_BIO_ReadHandle *GNUNET_BIO_read_open (const char *fn)
  *
  * @param h file handle
  * @param emsg set to the error message
- * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
  */
-int GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h,
-                          char **emsg)
+int
+GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h,
+                       char **emsg)
 {
-  return GNUNET_SYSERR;
+  int err;
+
+  err = (NULL == h->emsg) ? GNUNET_OK : GNUNET_SYSERR;
+  if (emsg != NULL)
+    *emsg = h->emsg;
+  else
+    GNUNET_free_non_null (h->emsg);
+  GNUNET_DISK_file_close (h->fd);
+  GNUNET_free (h);
+  return err;
 }
 
 
@@ -70,13 +137,87 @@ int GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h,
  * @param what describes what is being read (for error message creation)
  * @param result the buffer to write the result to
  * @param len the number of bytes to read
- * @return len on success, GNUNET_SYSERR on failure
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
  */
-ssize_t GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h, 
-                        const char *what,
-                        void *result, 
-                        size_t len)
+int
+GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h,
+                 const char *what,
+                 void *result, size_t len)
 {
+  char *dst = result;
+  size_t min;
+  size_t pos;
+  ssize_t ret;
+
+  if (NULL != h->emsg)
+    return GNUNET_SYSERR;
+  pos = 0;
+  do
+  {
+    /* first, use buffer */
+    min = h->have - h->pos;
+    if (min > 0)
+    {
+      if (min > len - pos)
+        min = len - pos;
+      GNUNET_memcpy (&dst[pos],
+                    &h->buffer[h->pos],
+                    min);
+      h->pos += min;
+      pos += min;
+    }
+    if (pos == len)
+      return GNUNET_OK;         /* done! */
+    GNUNET_assert (((off_t) h->have) == h->pos);
+    /* fill buffer */
+    ret = GNUNET_DISK_file_read (h->fd,
+                                h->buffer,
+                                h->size);
+    if (-1 == ret)
+    {
+      GNUNET_asprintf (&h->emsg,
+                      _("Error reading `%s': %s"),
+                      what,
+                       STRERROR (errno));
+      return GNUNET_SYSERR;
+    }
+    if (0 == ret)
+    {
+      GNUNET_asprintf (&h->emsg,
+                      _("Error reading `%s': %s"),
+                      what,
+                       _("End of file"));
+      return GNUNET_SYSERR;
+    }
+    h->pos = 0;
+    h->have = ret;
+  }
+  while (pos < len);            /* should always be true */
+  return GNUNET_OK;
+}
+
+
+/**
+ * Read the contents of a binary file into a buffer.
+ *
+ * @param h handle to an open file
+ * @param file name of the source file
+ * @param line line number in the source file
+ * @param result the buffer to write the result to
+ * @param len the number of bytes to read
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
+ */
+int
+GNUNET_BIO_read_fn (struct GNUNET_BIO_ReadHandle *h,
+                    const char *file,
+                    int line,
+                    void *result,
+                    size_t len)
+{
+  char what[PATH_MAX + 1024];
+
+  GNUNET_snprintf (what, sizeof (what), "%s:%d", file, line);
+  return GNUNET_BIO_read (h, what, result, len);
 }
 
 
@@ -87,12 +228,47 @@ ssize_t GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h,
  * @param what describes what is being read (for error message creation)
  * @param result the buffer to store a pointer to the (allocated) string to
  *        (note that *result could be set to NULL as well)
- * @return GNUNET_OK on success, GNUNET_SYSERR on failure
+ * @param max_length maximum allowed length for the string
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
  */
-int GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h, 
-                           const char *what,
-                           char **result)
+int
+GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h,
+                        const char *what,
+                        char **result,
+                        size_t max_length)
 {
+  char *buf;
+  uint32_t big;
+
+  if (GNUNET_OK != GNUNET_BIO_read_int32 (h, &big))
+  {
+    GNUNET_free_non_null (h->emsg);
+    GNUNET_asprintf (&h->emsg, _("Error reading length of string `%s'"), what);
+    return GNUNET_SYSERR;
+  }
+  if (0 == big)
+  {
+    *result = NULL;
+    return GNUNET_OK;
+  }
+  if (big > max_length)
+  {
+    GNUNET_asprintf (&h->emsg, _("String `%s' longer than allowed (%u > %u)"),
+                     what, big, max_length);
+    return GNUNET_SYSERR;
+  }
+  buf = GNUNET_malloc (big);
+  *result = buf;
+  buf[--big] = '\0';
+  if (0 == big)
+    return GNUNET_OK;
+  if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, big))
+  {
+    GNUNET_free (buf);
+    *result = NULL;
+    return GNUNET_SYSERR;
+  }
+  return GNUNET_OK;
 }
 
 
@@ -102,12 +278,58 @@ int GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h,
  * @param h handle to an open file
  * @param what describes what is being read (for error message creation)
  * @param result the buffer to store a pointer to the (allocated) metadata
- * @return GNUNET_OK on success, GNUNET_SYSERR on failure
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
  */
-int GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h, 
-                              const char *what,
-                              struct GNUNET_CONTAINER_MetaData **result)
+int
+GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h,
+                           const char *what,
+                           struct GNUNET_CONTAINER_MetaData **result)
 {
+  uint32_t size;
+  char *buf;
+  struct GNUNET_CONTAINER_MetaData *meta;
+
+  if (GNUNET_OK !=
+      GNUNET_BIO_read_int32 (h,
+                            (int32_t *) & size))
+    return GNUNET_SYSERR;
+  if (size == 0)
+  {
+    *result = NULL;
+    return GNUNET_OK;
+  }
+  if (size > MAX_META_DATA)
+  {
+    GNUNET_asprintf (&h->emsg,
+                     _("Serialized metadata `%s' larger than allowed (%u>%u)"),
+                     what,
+                    size,
+                    MAX_META_DATA);
+    return GNUNET_SYSERR;
+  }
+  buf = GNUNET_malloc (size);
+  if (GNUNET_OK !=
+      GNUNET_BIO_read (h,
+                      what,
+                      buf,
+                      size))
+  {
+    GNUNET_free (buf);
+    return GNUNET_SYSERR;
+  }
+  meta = GNUNET_CONTAINER_meta_data_deserialize (buf,
+                                                size);
+  if (NULL == meta)
+  {
+    GNUNET_free (buf);
+    GNUNET_asprintf (&h->emsg,
+                    _("Metadata `%s' failed to deserialize"),
+                    what);
+    return GNUNET_SYSERR;
+  }
+  GNUNET_free (buf);
+  *result = meta;
+  return GNUNET_OK;
 }
 
 
@@ -115,32 +337,84 @@ int GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h,
  * Read an (u)int32_t.
  *
  * @param h hande to open file
- * @param what describes what is being read (for error message creation)
+ * @param file name of the source file
+ * @param line line number in the source file
  * @param i address of 32-bit integer to read
- * @return GNUNET_OK on success, GNUNET_SYSERR on error
- */ 
-int GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h, 
-                            const char *what,
-                            int32_t *i);
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
+ */
+int
+GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h,
+                        const char *file,
+                         int line,
+                        int32_t * i)
+{
+  int32_t big;
+
+  if (GNUNET_OK !=
+      GNUNET_BIO_read_fn (h,
+                         file,
+                         line,
+                         &big,
+                         sizeof (int32_t)))
+    return GNUNET_SYSERR;
+  *i = ntohl (big);
+  return GNUNET_OK;
+}
 
 
 /**
  * Read an (u)int64_t.
  *
  * @param h hande to open file
- * @param what describes what is being read (for error message creation)
+ * @param file name of the source file
+ * @param line line number in the source file
  * @param i address of 64-bit integer to read
- * @return GNUNET_OK on success, GNUNET_SYSERR on error
- */ 
-int GNUNET_BIO_read_int64__ (struct GNUNET_BIO_ReadHandle *h, 
-                            const char *what,
-                            int64_t *i);
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
+ */
+int
+GNUNET_BIO_read_int64__ (struct GNUNET_BIO_ReadHandle *h,
+                         const char *file,
+                         int line,
+                         int64_t *i)
+{
+  int64_t big;
+
+  if (GNUNET_OK !=
+      GNUNET_BIO_read_fn (h,
+                         file,
+                         line,
+                         &big,
+                         sizeof (int64_t)))
+    return GNUNET_SYSERR;
+  *i = GNUNET_ntohll (big);
+  return GNUNET_OK;
+}
+
 
 /**
  * Handle for buffered writing.
  */
 struct GNUNET_BIO_WriteHandle
 {
+  /**
+   * Underlying file handle.
+   */
+  struct GNUNET_DISK_FileHandle *fd;
+
+  /**
+   * I/O buffer.  Do not free, allocated at the end of the struct.
+   */
+  char *buffer;
+
+  /**
+   * Number of bytes already in @e buffer.
+   */
+  size_t have;
+
+  /**
+   * Total size of @e buffer.
+   */
+  size_t size;
 };
 
 
@@ -150,9 +424,24 @@ struct GNUNET_BIO_WriteHandle
  * @param fn file name to be opened
  * @return IO handle on success, NULL on error
  */
-struct GNUNET_BIO_WriteHandle *GNUNET_BIO_write_open (const char *fn)
+struct GNUNET_BIO_WriteHandle *
+GNUNET_BIO_write_open (const char *fn)
 {
-  return NULL;
+  struct GNUNET_DISK_FileHandle *fd;
+  struct GNUNET_BIO_WriteHandle *h;
+
+  fd = GNUNET_DISK_file_open (fn,
+                              GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE
+                              | GNUNET_DISK_OPEN_CREATE,
+                              GNUNET_DISK_PERM_USER_READ |
+                              GNUNET_DISK_PERM_USER_WRITE);
+  if (NULL == fd)
+    return NULL;
+  h = GNUNET_malloc (sizeof (struct GNUNET_BIO_WriteHandle) + BIO_BUFFER_SIZE);
+  h->buffer = (char *) &h[1];
+  h->size = BIO_BUFFER_SIZE;
+  h->fd = fd;
+  return h;
 }
 
 
@@ -160,9 +449,46 @@ struct GNUNET_BIO_WriteHandle *GNUNET_BIO_write_open (const char *fn)
  * Close an open file for writing.
  *
  * @param h file handle
- * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
+ */
+int
+GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h)
+{
+  int ret;
+
+  ret = GNUNET_SYSERR;
+  if ( (NULL != h->fd) &&
+       (GNUNET_OK == (ret = GNUNET_BIO_flush (h))) )
+    GNUNET_DISK_file_close (h->fd);
+  GNUNET_free (h);
+  return ret;
+}
+
+
+/**
+ * Force a buffered writer to flush its buffer
+ *
+ * @param h the writer handle
+ * @return #GNUNET_OK upon success.  Upon failure #GNUNET_SYSERR is returned and
+ *           the file is closed
  */
-int GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h);
+int
+GNUNET_BIO_flush (struct GNUNET_BIO_WriteHandle *h)
+{
+  ssize_t ret;
+
+  ret = GNUNET_DISK_file_write (h->fd,
+                               h->buffer,
+                               h->have);
+  if (ret != (ssize_t) h->have)
+  {
+    GNUNET_DISK_file_close (h->fd);
+    h->fd = NULL;
+    return GNUNET_SYSERR;     /* error */
+  }
+  h->have = 0;
+  return GNUNET_OK;
+}
 
 
 /**
@@ -171,11 +497,41 @@ int GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h);
  * @param h handle to open file
  * @param buffer the data to write
  * @param n number of bytes to write
- * @return GNUNET_OK on success, GNUNET_SYSERR on error
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  */
-ssize_t GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h, 
-                         const void *buffer,
-                         size_t n);
+int
+GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h,
+                 const void *buffer,
+                  size_t n)
+{
+  const char *src = buffer;
+  size_t min;
+  size_t pos;
+
+  if (NULL == h->fd)
+    return GNUNET_SYSERR;
+  pos = 0;
+  do
+  {
+    /* first, just use buffer */
+    min = h->size - h->have;
+    if (min > n - pos)
+      min = n - pos;
+    GNUNET_memcpy (&h->buffer[h->have],
+                  &src[pos],
+                  min);
+    pos += min;
+    h->have += min;
+    if (pos == n)
+      return GNUNET_OK;         /* done */
+    GNUNET_assert (h->have == h->size);
+    if (GNUNET_OK != GNUNET_BIO_flush (h))
+      return GNUNET_SYSERR;     /* error */
+  }
+  while (pos < n);              /* should always be true */
+  GNUNET_break (0);
+  return GNUNET_OK;
+}
 
 
 /**
@@ -183,12 +539,21 @@ ssize_t GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h,
  *
  * @param h handle to open file
  * @param s string to write (can be NULL)
- * @return GNUNET_OK on success, GNUNET_SYSERR on error
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  */
-int GNUNET_BIO_write_string (struct GNUNET_BIO_WriteHandle *h, 
-                            const char *s);
-
+int
+GNUNET_BIO_write_string (struct GNUNET_BIO_WriteHandle *h,
+                        const char *s)
+{
+  uint32_t slen;
 
+  slen = (uint32_t) ((s == NULL) ? 0 : strlen (s) + 1);
+  if (GNUNET_OK != GNUNET_BIO_write_int32 (h, slen))
+    return GNUNET_SYSERR;
+  if (0 != slen)
+    return GNUNET_BIO_write (h, s, slen - 1);
+  return GNUNET_OK;
+}
 
 
 /**
@@ -196,350 +561,71 @@ int GNUNET_BIO_write_string (struct GNUNET_BIO_WriteHandle *h,
  *
  * @param h handle to open file
  * @param m metadata to write
- * @return GNUNET_OK on success, GNUNET_SYSERR on error
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  */
-int GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h, 
-                               const struct GNUNET_CONTAINER_MetaData *m);
-
-
-
-/**
- * Write a float.
- *
- * @param h hande to open file
- * @param f float to write (must be a variable)
- */ 
-#define GNUNET_BIO_write_float(h, f) (sizeof(float) == GNUNET_BIO_write (h, &f, sizeof(float)))
-
-
-
-/**
- * Write a double.
- *
- * @param h hande to open file
- * @param f double to write (must be a variable)
- */ 
-#define GNUNET_BIO_write_float(h, f) (sizeof(double) == GNUNET_BIO_write (h, &f, sizeof(double)))
-
-
-/**
- * Write an (u)int32_t.
- *
- * @param h hande to open file
- * @param i address of 32-bit integer to write
- * @return GNUNET_OK on success, GNUNET_SYSERR on error
- */ 
-int GNUNET_BIO_write_int32 (struct GNUNET_BIO_ReadHandle *h, 
-                           int32_t i);
-
-
-/**
- * Write an (u)int64_t.
- *
- * @param h hande to open file
- * @param i address of 64-bit integer to write
- * @return GNUNET_OK on success, GNUNET_SYSERR on error
- */ 
-int GNUNET_BIO_write_int64 (struct GNUNET_BIO_ReadHandle *h, 
-                           int64_t i);
-
-
-
-
-
-typedef struct
-{
-  int fd;
-  unsigned int have;
-  unsigned int size;
-  char *buffer;
-} WriteBuffer;
-
-static void
-write_buffered (WriteBuffer * wb, const void *s, unsigned int size)
-{
-  const char *src = s;
-  unsigned int min;
-  unsigned int pos;
-  int ret;
-
-  if (wb->fd == -1)
-    return;
-  pos = 0;
-  do
-    {
-      /* first, just use buffer */
-      min = wb->size - wb->have;
-      if (min > size - pos)
-        min = size - pos;
-      memcpy (&wb->buffer[wb->have], &src[pos], min);
-      pos += min;
-      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)
+int
+GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h,
+                            const struct GNUNET_CONTAINER_MetaData *m)
 {
+  ssize_t size;
   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)
-        {
-          CLOSE (rb->fd);
-          rb->fd = -1;
-          return -1;
-        }
-      if (ret == 0)
-        return 0;
-      rb->pos = 0;
-      rb->have = ret;
-    }
-  while (pos < size);           /* should always be true */
-  return pos;
-}
-
 
-static int
-read_int (ReadBuffer * rb, int *val)
-{
-  int big;
-
-  if (sizeof (int) != read_buffered (rb, &big, sizeof (int)))
+  if (m == NULL)
+    return GNUNET_BIO_write_int32 (h, 0);
+  buf = NULL;
+  size =
+      GNUNET_CONTAINER_meta_data_serialize (m, &buf, MAX_META_DATA,
+                                            GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
+  if (size == -1)
+  {
+    GNUNET_free (buf);
     return GNUNET_SYSERR;
-  *val = ntohl (big);
-  return GNUNET_OK;
-}
-
-static unsigned int
-read_uint (ReadBuffer * rb, unsigned int *val)
-{
-  unsigned int big;
-
-  if (sizeof (unsigned int) !=
-      read_buffered (rb, &big, sizeof (unsigned int)))
+  }
+  if ((GNUNET_OK != GNUNET_BIO_write_int32 (h, (uint32_t) size)) ||
+      (GNUNET_OK != GNUNET_BIO_write (h, buf, size)))
+  {
+    GNUNET_free (buf);
     return GNUNET_SYSERR;
-  *val = ntohl (big);
-  return GNUNET_OK;
-}
-
-#define READINT(a) if (GNUNET_OK != read_int(rb, (int*) &a)) return GNUNET_SYSERR;
-
-static int
-read_long (ReadBuffer * rb, long long *val)
-{
-  long long big;
-
-  if (sizeof (long long) != read_buffered (rb, &big, sizeof (long long)))
-    return GNUNET_SYSERR;
-  *val = GNUNET_ntohll (big);
-  return GNUNET_OK;
-}
-
-#define READLONG(a) if (GNUNET_OK != read_long(rb, (long long*) &a)) return GNUNET_SYSERR;
-
-static struct GNUNET_ECRS_URI *
-read_uri (struct GNUNET_GE_Context *ectx, ReadBuffer * rb)
-{
-  char *buf;
-  struct GNUNET_ECRS_URI *ret;
-  unsigned int size;
-
-  if (GNUNET_OK != read_uint (rb, &size))
-    return NULL;
-  buf = GNUNET_malloc (size + 1);
-  buf[size] = '\0';
-  if (size != read_buffered (rb, buf, size))
-    {
-      GNUNET_free (buf);
-      return NULL;
-    }
-  ret = GNUNET_ECRS_string_to_uri (ectx, buf);
-  GNUNET_GE_BREAK (ectx, ret != NULL);
+  }
   GNUNET_free (buf);
-  return ret;
+  return GNUNET_OK;
 }
 
-#define READURI(u) if (NULL == (u = read_uri(ectx, rb))) return GNUNET_SYSERR;
 
-static char *
-read_string (ReadBuffer * rb, unsigned int maxLen)
+/**
+ * Write an (u)int32_t.
+ *
+ * @param h hande to open file
+ * @param i 32-bit integer to write
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
+ */
+int
+GNUNET_BIO_write_int32 (struct GNUNET_BIO_WriteHandle *h,
+                        int32_t i)
 {
-  char *buf;
-  unsigned int big;
+  int32_t big;
 
-  if (GNUNET_OK != read_uint (rb, &big))
-    return NULL;
-  if (big > maxLen)
-    return NULL;
-  buf = GNUNET_malloc (big + 1);
-  buf[big] = '\0';
-  if (big != read_buffered (rb, buf, big))
-    {
-      GNUNET_free (buf);
-      return NULL;
-    }
-  return buf;
+  big = htonl (i);
+  return GNUNET_BIO_write (h, &big, sizeof (int32_t));
 }
 
-#define READSTRING(c, max) if (NULL == (c = read_string(rb, max))) return GNUNET_SYSERR;
 
 /**
- * Read file info from file.
+ * Write an (u)int64_t.
  *
- * @return GNUNET_OK on success, GNUNET_SYSERR on error
+ * @param h hande to open file
+ * @param i 64-bit integer to write
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  */
-static struct GNUNET_MetaData *
-read_meta (struct GNUNET_GE_Context *ectx, ReadBuffer * rb)
+int
+GNUNET_BIO_write_int64 (struct GNUNET_BIO_WriteHandle *h,
+                        int64_t i)
 {
-  unsigned int size;
-  char *buf;
-  struct GNUNET_MetaData *meta;
+  int64_t big;
 
-  if (read_uint (rb, &size) != GNUNET_OK)
-    {
-      GNUNET_GE_BREAK (ectx, 0);
-      return NULL;
-    }
-  if (size > 1024 * 1024)
-    {
-      GNUNET_GE_BREAK (ectx, 0);
-      return NULL;
-    }
-  buf = GNUNET_malloc (size);
-  if (size != read_buffered (rb, buf, size))
-    {
-      GNUNET_free (buf);
-      GNUNET_GE_BREAK (ectx, 0);
-      return NULL;
-    }
-  meta = GNUNET_meta_data_deserialize (ectx, buf, size);
-  if (meta == NULL)
-    {
-      GNUNET_free (buf);
-      GNUNET_GE_BREAK (ectx, 0);
-      return NULL;
-    }
-  GNUNET_free (buf);
-  return meta;
+  big = GNUNET_htonll (i);
+  return GNUNET_BIO_write (h, &big, sizeof (int64_t));
 }
 
+
 /* end of bio.c */