- test for external iterator
[oweals/gnunet.git] / src / util / bio.c
index b29d75eb9e0988b5a51491a617c6d5e03bddf16a..34210535ac65f68bf74c4232cdbe1ea95436a77e 100644 (file)
@@ -26,6 +26,8 @@
 #include "gnunet_bio_lib.h"
 #include "gnunet_disk_lib.h"
 
+#define LOG(kind,...) GNUNET_log_from (kind, "util",__VA_ARGS__)
+
 #define BIO_BUFFER_SIZE 65536
 
 #define MAX_META_DATA (1024 * 1024)
@@ -101,8 +103,8 @@ GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h, char **emsg)
  * @return GNUNET_OK on success, GNUNET_SYSERR on failure
  */
 int
-GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h,
-                 const char *what, void *result, size_t len)
+GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h, const char *what,
+                 void *result, size_t len)
 {
   char *dst = result;
   size_t min;
@@ -131,14 +133,14 @@ GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h,
     ret = GNUNET_DISK_file_read (h->fd, h->buffer, h->size);
     if (ret == -1)
     {
-      GNUNET_asprintf (&h->emsg,
-                       _("Error reading `%s': %s"), what, STRERROR (errno));
+      GNUNET_asprintf (&h->emsg, _("Error reading `%s': %s"), what,
+                       STRERROR (errno));
       return GNUNET_SYSERR;
     }
     if (ret == 0)
     {
-      GNUNET_asprintf (&h->emsg,
-                       _("Error reading `%s': %s"), what, _("End of file"));
+      GNUNET_asprintf (&h->emsg, _("Error reading `%s': %s"), what,
+                       _("End of file"));
       return GNUNET_SYSERR;
     }
     h->pos = 0;
@@ -160,8 +162,8 @@ GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h,
  * @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)
+GNUNET_BIO_read_fn (struct GNUNET_BIO_ReadHandle *h, const char *file, int line,
+                    void *result, size_t len)
 {
   char what[1024];
 
@@ -181,8 +183,8 @@ GNUNET_BIO_read_fn (struct GNUNET_BIO_ReadHandle *h,
  * @return GNUNET_OK on success, GNUNET_SYSERR on failure
  */
 int
-GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h,
-                        const char *what, char **result, size_t maxLen)
+GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h, const char *what,
+                        char **result, size_t maxLen)
 {
   char *buf;
   uint32_t big;
@@ -200,8 +202,7 @@ GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h,
   }
   if (big > maxLen)
   {
-    GNUNET_asprintf (&h->emsg,
-                     _("String `%s' longer than allowed (%u > %u)"),
+    GNUNET_asprintf (&h->emsg, _("String `%s' longer than allowed (%u > %u)"),
                      what, big, maxLen);
     return GNUNET_SYSERR;
   }
@@ -229,8 +230,7 @@ GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h,
  * @return GNUNET_OK on success, GNUNET_SYSERR on failure
  */
 int
-GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h,
-                           const char *what,
+GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h, const char *what,
                            struct GNUNET_CONTAINER_MetaData **result)
 {
   uint32_t size;
@@ -280,8 +280,8 @@ GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h,
  * @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)
+GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h, const char *file,
+                         int line, int32_t * i)
 {
   int32_t big;
 
@@ -302,8 +302,8 @@ GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h,
  * @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)
+GNUNET_BIO_read_int64__ (struct GNUNET_BIO_ReadHandle *h, const char *file,
+                         int line, int64_t * i)
 {
   int64_t big;
 
@@ -339,9 +339,8 @@ GNUNET_BIO_write_open (const char *fn)
   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_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE
+                              | GNUNET_DISK_OPEN_CREATE,
                               GNUNET_DISK_PERM_USER_READ |
                               GNUNET_DISK_PERM_USER_WRITE);
   if (NULL == fd)
@@ -350,7 +349,6 @@ GNUNET_BIO_write_open (const char *fn)
   h->buffer = (char *) &h[1];
   h->size = BIO_BUFFER_SIZE;
   h->fd = fd;
-
   return h;
 }
 
@@ -364,27 +362,40 @@ GNUNET_BIO_write_open (const char *fn)
 int
 GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h)
 {
-  ssize_t wrt;
   int ret;
 
-  if (NULL == h->fd)
-  {
-    ret = GNUNET_SYSERR;
-  }
-  else
-  {
-    wrt = GNUNET_DISK_file_write (h->fd, h->buffer, h->have);
-    if (wrt == h->have)
-      ret = GNUNET_OK;
-    else
-      ret = GNUNET_SYSERR;
+  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_flush (struct GNUNET_BIO_WriteHandle *h)
+{
+  ssize_t ret;
+
+  ret = GNUNET_DISK_file_write (h->fd, h->buffer, h->have);
+  if (ret != h->have)
+  {
+    GNUNET_DISK_file_close (h->fd);
+    h->fd = NULL;
+    return GNUNET_SYSERR;     /* error */
+  }
+  h->have = 0;
+  return GNUNET_OK;
+}
+
+
 /**
  * Write a buffer to a file.
  *
@@ -394,13 +405,12 @@ GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h)
  * @return GNUNET_OK on success, GNUNET_SYSERR on error
  */
 int
-GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h,
-                  const void *buffer, size_t n)
+GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h, const void *buffer,
+                  size_t n)
 {
   const char *src = buffer;
   size_t min;
   size_t pos;
-  ssize_t ret;
 
   if (NULL == h->fd)
     return GNUNET_SYSERR;
@@ -417,14 +427,8 @@ GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h,
     if (pos == n)
       return GNUNET_OK;         /* done */
     GNUNET_assert (h->have == h->size);
-    ret = GNUNET_DISK_file_write (h->fd, h->buffer, h->size);
-    if (ret != h->size)
-    {
-      GNUNET_DISK_file_close (h->fd);
-      h->fd = NULL;
+    if (GNUNET_OK != GNUNET_BIO_flush (h))
       return GNUNET_SYSERR;     /* error */
-    }
-    h->have = 0;
   }
   while (pos < n);              /* should always be true */
   GNUNET_break (0);
@@ -470,10 +474,9 @@ GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h,
   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);
+  size =
+      GNUNET_CONTAINER_meta_data_serialize (m, &buf, MAX_META_DATA,
+                                            GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
   if (size == -1)
   {
     GNUNET_free (buf);
@@ -494,7 +497,7 @@ GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h,
  * Write an (u)int32_t.
  *
  * @param h hande to open file
- * @param i address of 32-bit integer to write
+ * @param i 32-bit integer to write
  * @return GNUNET_OK on success, GNUNET_SYSERR on error
  */
 int
@@ -511,7 +514,7 @@ GNUNET_BIO_write_int32 (struct GNUNET_BIO_WriteHandle *h, int32_t i)
  * Write an (u)int64_t.
  *
  * @param h hande to open file
- * @param i address of 64-bit integer to write
+ * @param i 64-bit integer to write
  * @return GNUNET_OK on success, GNUNET_SYSERR on error
  */
 int