Returns now GNUNET_SYSERR
[oweals/gnunet.git] / src / util / bio.c
index 860e9e63eb63e8a93d74106df0cfe5f1733b2064..3fc7d05e620b2341a1c8adbc25cf2b43999dd29b 100644 (file)
@@ -58,6 +58,8 @@ GNUNET_BIO_read_open (const char *fn)
 
   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;
@@ -77,10 +79,16 @@ GNUNET_BIO_read_open (const char *fn)
 int
 GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h, char **emsg)
 {
-  *emsg = h->emsg;
+  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 (NULL == *emsg) ? GNUNET_OK : GNUNET_SYSERR;
+  return err;
 }
 
 
@@ -144,6 +152,30 @@ GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h,
 }
 
 
+/**
+ * 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[1024];
+  GNUNET_snprintf (what,
+                  sizeof(what),
+                  "%s:%d",
+                  file, line);
+  return GNUNET_BIO_read (h, what, result, len);
+}
+
+
 /**
  * Read 0-terminated string from a file.
  *
@@ -162,7 +194,13 @@ GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h,
   uint32_t big;
 
   if (GNUNET_OK != GNUNET_BIO_read_int32 (h, &big))
-    return GNUNET_SYSERR;
+    {
+      GNUNET_free_non_null (h->emsg);
+      GNUNET_asprintf (&h->emsg,
+                       _("Error reading length of string `%s'"),
+                       what);
+      return GNUNET_SYSERR;
+    }
   if (big == 0)
     {
       *result = NULL;
@@ -176,15 +214,16 @@ GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h,
       return GNUNET_SYSERR;
     }
   buf = GNUNET_malloc (big);
+  *result = buf;
   buf[--big] = '\0';
   if (big == 0)
     return GNUNET_OK;
   if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, big))
     {
       GNUNET_free (buf);
+      *result = NULL;
       return GNUNET_SYSERR;
     }
-  *result = buf;
   return GNUNET_OK;
 }
 
@@ -206,13 +245,17 @@ GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h,
   char *buf;
   struct GNUNET_CONTAINER_MetaData *meta;
 
-  if (GNUNET_BIO_read_int32__ (h, what, (int32_t *) & size) != GNUNET_OK)
+  if (GNUNET_BIO_read_int32 (h, (int32_t *) &size) != GNUNET_OK)
     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)"),
+                       _("Serialized metadata `%s' larger than allowed (%u>%u)"),
                        what, size, MAX_META_DATA);
       return GNUNET_SYSERR;
     }
@@ -240,17 +283,20 @@ 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)
+                         const char *file,
+                        int line,
+                        int32_t * i)
 {
   int32_t big;
 
-  if (GNUNET_OK != GNUNET_BIO_read (h, what, &big, sizeof (int32_t)))
+  if (GNUNET_OK != GNUNET_BIO_read_fn (h, file, line, &big, sizeof (int32_t)))
     return GNUNET_SYSERR;
   *i = ntohl (big);
   return GNUNET_OK;
@@ -261,17 +307,20 @@ GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h,
  * 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)
+                         const char *file, 
+                        int line,
+                        int64_t * i)
 {
   int64_t big;
 
-  if (GNUNET_OK != GNUNET_BIO_read (h, what, &big, sizeof (int64_t)))
+  if (GNUNET_OK != GNUNET_BIO_read_fn (h, file, line, &big, sizeof (int64_t)))
     return GNUNET_SYSERR;
   *i = GNUNET_ntohll (big);
   return GNUNET_OK;
@@ -308,6 +357,8 @@ GNUNET_BIO_write_open (const char *fn)
                               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];
@@ -390,6 +441,7 @@ GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h,
       h->have = 0;
     }
   while (pos < n);              /* should always be true */
+  GNUNET_break (0);
   return GNUNET_OK;
 }
 
@@ -426,24 +478,23 @@ int
 GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h,
                             const struct GNUNET_CONTAINER_MetaData *m)
 {
-  unsigned int size;
+  ssize_t size;
   char *buf;
-
-  size = GNUNET_CONTAINER_meta_data_get_serialized_size (m,
-                                                         GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL
-                                                         |
-                                                         GNUNET_CONTAINER_META_DATA_SERIALIZE_NO_COMPRESS);
-  if (size > MAX_META_DATA)
-    size = MAX_META_DATA;
-  buf = GNUNET_malloc (size);
-  GNUNET_CONTAINER_meta_data_serialize (m,
-                                        buf,
-                                        size,
-                                        GNUNET_CONTAINER_META_DATA_SERIALIZE_PART
-                                        |
-                                        GNUNET_CONTAINER_META_DATA_SERIALIZE_NO_COMPRESS);
-  if ((GNUNET_OK != GNUNET_BIO_write_int32 (h, size))
-      || (GNUNET_OK != GNUNET_BIO_write (h, buf, size)))
+  
+  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;
+    }
+  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;