Fixed compile warnings under windows
[oweals/gnunet.git] / src / util / pseudonym.c
index 5d9750f5b78f6572e5bf6e8b366f845555274921..6693dadd2e8b5c6ce724b3ec4374321065767d9d 100644 (file)
 #include "gnunet_container_lib.h"
 #include "gnunet_disk_lib.h"
 #include "gnunet_pseudonym_lib.h"
+#include "gnunet_bio_lib.h"
 
 /** 
- * FIXME
+ * Name of the directory which stores meta data for pseudonym
  */
-#define PS_METADATA_DIR DIR_SEPARATOR_STR "data" DIR_SEPARATOR_STR "pseudonyms/metadata" DIR_SEPARATOR_STR
+#define PS_METADATA_DIR DIR_SEPARATOR_STR "data" DIR_SEPARATOR_STR "pseudonyms" DIR_SEPARATOR_STR "metadata" DIR_SEPARATOR_STR
 
 /** 
- * FIXME
+ * Name of the directory which stores names for pseudonyms
  */
-#define PS_NAMES_DIR    DIR_SEPARATOR_STR "data" DIR_SEPARATOR_STR "pseudonyms/names"    DIR_SEPARATOR_STR
+#define PS_NAMES_DIR    DIR_SEPARATOR_STR "data" DIR_SEPARATOR_STR "pseudonyms" DIR_SEPARATOR_STR "names"    DIR_SEPARATOR_STR
 
 
 /** 
- * FIXME
+ * Registered callbacks for discovery of pseudonyms.
  */
 struct DiscoveryCallback
 {
-
   /** 
-   * FIXME
+   * This is a linked list.
    */
   struct DiscoveryCallback *next;
 
   /** 
-   * FIXME
+   * Function to call each time a pseudonym is discovered.
    */
   GNUNET_PSEUDONYM_Iterator callback;
 
   /** 
-   * FIXME
+   * Closure for callback.
    */
   void *closure;
 };
 
 
 /** 
- * FIXME
+ * Head of the linked list of functions to call when 
+ * new pseudonyms are added.
  */
 static struct DiscoveryCallback *head;
 
 /**
  * Internal notification about new tracked URI.
- * 
- * FIXME
+ * @param id a point to the hash code of pseudonym
+ * @param md meta data to be written
+ * @param rating rating of pseudonym
  */
 static void
 internal_notify (const GNUNET_HashCode * id,
@@ -91,8 +93,9 @@ internal_notify (const GNUNET_HashCode * id,
 /**
  * Register callback to be invoked whenever we discover
  * a new pseudonym.
- * 
- * FIXME
+ * @param cfg configuration to use
+ * @param iterator iterator over pseudonym
+ * @param closure point to a closure
  */
 int
 GNUNET_PSEUDONYM_discovery_callback_register (const struct
@@ -114,8 +117,8 @@ GNUNET_PSEUDONYM_discovery_callback_register (const struct
 
 /**
  * Unregister pseudonym discovery callback.
- * 
- * FIXME
+ * @param iterator iterator over pseudonym
+ * @param closure point to a closure
  */
 int
 GNUNET_PSEUDONYM_discovery_callback_unregister (GNUNET_PSEUDONYM_Iterator
@@ -146,8 +149,10 @@ GNUNET_PSEUDONYM_discovery_callback_unregister (GNUNET_PSEUDONYM_Iterator
 /**
  * Get the filename (or directory name) for the given
  * pseudonym identifier and directory prefix.
- * 
- * FIXME
+ * @param cfg configuration to use
+ * @param prefix path components to append to the private directory name
+ * @param psid hash code of pseudonym, can be NULL
+ * @return filename of the pseudonym (if psid != NULL) or directory with the data (if psid == NULL)
  */
 static char *
 get_data_filename (const struct GNUNET_CONFIGURATION_Handle
@@ -167,7 +172,12 @@ get_data_filename (const struct GNUNET_CONFIGURATION_Handle
 
 
 /**
- * FIXME
+ * Write the pseudonym infomation into a file
+ * @param cfg configuration to use
+ * @param nsid hash code of a pseudonym
+ * @param meta meta data to be written into a file
+ * @param ranking ranking of a pseudonym
+ * @param ns_name name of a pseudonym
  */
 static void
 write_pseudonym_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
@@ -175,51 +185,45 @@ write_pseudonym_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
                       const struct GNUNET_CONTAINER_MetaData *meta,
                       int32_t ranking, const char *ns_name)
 {
-  size_t size;
-  size_t tag;
-  size_t off;
-  char *buf;
   char *fn;
+  struct GNUNET_BIO_WriteHandle *fileW;
 
   fn = get_data_filename (cfg, PS_METADATA_DIR, nsid);
   GNUNET_assert (fn != NULL);
-  size = GNUNET_CONTAINER_meta_data_get_serialized_size (meta,
-                                                         GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL);
-  tag = size + sizeof (int32_t) + 1;
-  off = 0;
-  if (ns_name != NULL)
-    {
-      off = strlen (ns_name);
-      tag += off;
-    }
-  buf = GNUNET_malloc (tag);
-  ((int32_t *) buf)[0] = htonl (ranking);   /* ranking */
-  if (ns_name != NULL)
+  fileW = GNUNET_BIO_write_open(fn);
+  if (NULL != fileW) 
     {
-      memcpy (&buf[sizeof (int32_t)], ns_name, off + 1);
+      if ( (GNUNET_OK != GNUNET_BIO_write_int32(fileW, ranking)) ||
+          (GNUNET_OK != GNUNET_BIO_write_string(fileW, ns_name)) ||
+          (GNUNET_OK != GNUNET_BIO_write_meta_data(fileW, meta)) )
+       {
+         (void) GNUNET_BIO_write_close(fileW);
+         GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
+         GNUNET_free (fn);
+         return;
+       }
+      if (GNUNET_OK != GNUNET_BIO_write_close(fileW))
+       {
+         GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
+         GNUNET_free (fn);
+         return;
+       }
     }
-  else
-    {
-      buf[sizeof (int)] = '\0';
-    }
-  GNUNET_assert
-    (size == GNUNET_CONTAINER_meta_data_serialize (meta,
-                                                   &buf[sizeof
-                                                        (int32_t) +
-                                                        off + 1],
-                                                   size,
-                                                   GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL));
-  GNUNET_DISK_fn_write (fn, buf, tag, GNUNET_DISK_PERM_USER_READ
-      | GNUNET_DISK_PERM_USER_WRITE | GNUNET_DISK_PERM_GROUP_READ);
   GNUNET_free (fn);
-  GNUNET_free (buf);
   /* create entry for pseudonym name in names */
+  /* FIXME: 90% of what this call does is not needed
+     here => refactor code to only create the entry! */
   GNUNET_free_non_null (GNUNET_PSEUDONYM_id_to_name (cfg, nsid));
 }
 
 
 /**
- * FIXME
+ * read the pseudonym infomation from a file
+ * @param cfg configuration to use
+ * @param nsid hash code of a pseudonym
+ * @param meta meta data to be read from a file
+ * @param ranking ranking of a pseudonym
+ * @param ns_name name of a pseudonym
  */
 static int
 read_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
@@ -227,84 +231,41 @@ read_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
            struct GNUNET_CONTAINER_MetaData **meta,
            int32_t * ranking, char **ns_name)
 {
-  uint64_t len;
-  size_t size;
-  size_t zend;
-  struct stat sbuf;
-  char *buf;
   char *fn;
+  char *emsg;
+  struct GNUNET_BIO_ReadHandle *fileR;
 
-  if (meta != NULL)
-    *meta = NULL;
-  if (ns_name != NULL)
-    *ns_name = NULL;
   fn = get_data_filename (cfg, PS_METADATA_DIR, nsid);
   GNUNET_assert (fn != NULL);
-
-  if ((0 != STAT (fn, &sbuf))
-      || (GNUNET_OK != GNUNET_DISK_file_size (fn, &len, GNUNET_YES)))
-    {
-      GNUNET_free (fn);
-      return GNUNET_SYSERR;
-    }
-  if (len <= sizeof (int32_t) + 1)
-    {
-      GNUNET_free (fn);
-      return GNUNET_SYSERR;
-    }
-  if (len > 16 * 1024 * 1024)
+  fileR = GNUNET_BIO_read_open(fn);
+  if (fileR == NULL)
     {
-      /* too big, must be invalid! remove! */
-      GNUNET_break (0);
-      if (0 != UNLINK (fn))
-        GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
       GNUNET_free (fn);
       return GNUNET_SYSERR;
     }
-  buf = GNUNET_malloc (len);
-  if (len != GNUNET_DISK_fn_read (fn, buf, len))
+  emsg = NULL;
+  if ( (GNUNET_OK != GNUNET_BIO_read_int32 (fileR, ranking)) ||
+       (GNUNET_OK != GNUNET_BIO_read_string(fileR, "Read string error!", ns_name, 200)) ||
+       (GNUNET_OK != GNUNET_BIO_read_meta_data(fileR, "Read meta data error!", meta)) )
     {
-      GNUNET_free (buf);
+      GNUNET_BIO_read_close(fileR, &emsg);
+      GNUNET_free_non_null (emsg);
+      GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
       GNUNET_free (fn);
       return GNUNET_SYSERR;
     }
-  if (ranking != NULL)
-    *ranking = ntohl (((int32_t *) buf)[0]);
-  zend = sizeof (int32_t);
-  while ((zend < len) && (buf[zend] != '\0'))
-    zend++;
-  if (zend == len)
+  if (GNUNET_OK != GNUNET_BIO_read_close(fileR, &emsg))
     {
-      GNUNET_free (buf);
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                 _("Failed to parse metadata about pseudonym from file `%s': %s\n"),
+                 fn,
+                 emsg);
+      GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
+      GNUNET_free_non_null (emsg);
       GNUNET_free (fn);
       return GNUNET_SYSERR;
     }
-  if (ns_name != NULL)
-    {
-      if (zend != sizeof (int32_t))
-        *ns_name = GNUNET_strdup (&buf[sizeof (int32_t)]);
-      else
-        *ns_name = NULL;
-    }
-  zend++;
-  size = len - zend;
-  if (meta != NULL)
-    {
-      *meta = GNUNET_CONTAINER_meta_data_deserialize (&buf[zend], size);
-      if ((*meta) == NULL)
-        {
-          /* invalid data! remove! */
-          GNUNET_break (0);
-          if (0 != UNLINK (fn))
-            GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
-                                      "unlink", fn);
-          GNUNET_free (buf);
-          GNUNET_free (fn);
-          return GNUNET_SYSERR;
-        }
-    }
   GNUNET_free (fn);
-  GNUNET_free (buf);
   return GNUNET_OK;
 }
 
@@ -331,22 +292,23 @@ GNUNET_PSEUDONYM_id_to_name (const struct GNUNET_CONFIGURATION_Handle *cfg,
   unsigned int idx;
   char *ret;
   struct stat sbuf;
+  int32_t temp = 0;
+  int32_t *rank = &temp;
 
   meta = NULL;
   name = NULL;
-  if (GNUNET_OK == read_info (cfg, nsid, &meta, NULL, &name))
+  if (GNUNET_OK == read_info (cfg, nsid, &meta, rank, &name))
     {
       if ((meta != NULL) && (name == NULL))
         name = GNUNET_CONTAINER_meta_data_get_first_by_types (meta,
-                                                              EXTRACTOR_TITLE,
-                                                              EXTRACTOR_FILENAME,
-                                                              EXTRACTOR_DESCRIPTION,
-                                                              EXTRACTOR_SUBJECT,
-                                                              EXTRACTOR_PUBLISHER,
-                                                              EXTRACTOR_AUTHOR,
-                                                              EXTRACTOR_COMMENT,
-                                                              EXTRACTOR_SUMMARY,
-                                                              EXTRACTOR_OWNER,
+                                                              EXTRACTOR_METATYPE_TITLE,
+                                                              EXTRACTOR_METATYPE_FILENAME,
+                                                              EXTRACTOR_METATYPE_DESCRIPTION,
+                                                              EXTRACTOR_METATYPE_SUBJECT,
+                                                              EXTRACTOR_METATYPE_PUBLISHER,
+                                                              EXTRACTOR_METATYPE_AUTHOR_NAME,
+                                                              EXTRACTOR_METATYPE_COMMENT,
+                                                              EXTRACTOR_METATYPE_SUMMARY,
                                                               -1);
       if (meta != NULL)
         {
@@ -364,8 +326,9 @@ GNUNET_PSEUDONYM_id_to_name (const struct GNUNET_CONFIGURATION_Handle *cfg,
   if (0 == STAT (fn, &sbuf))
     GNUNET_DISK_file_size (fn, &len, GNUNET_YES);
   fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_CREATE
-      | GNUNET_DISK_OPEN_READWRITE, GNUNET_DISK_PERM_USER_READ
-      | GNUNET_DISK_PERM_USER_WRITE);
+                              | GNUNET_DISK_OPEN_READWRITE,
+                              GNUNET_DISK_PERM_USER_READ |
+                              GNUNET_DISK_PERM_USER_WRITE);
   i = 0;
   idx = -1;
   while ((len >= sizeof (GNUNET_HashCode)) &&
@@ -436,11 +399,14 @@ GNUNET_PSEUDONYM_name_to_id (const struct GNUNET_CONFIGURATION_Handle *cfg,
       return GNUNET_SYSERR;
     }
   fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_CREATE
-      | GNUNET_DISK_OPEN_READWRITE, GNUNET_DISK_PERM_USER_READ
-      | GNUNET_DISK_PERM_USER_WRITE);
+                              | GNUNET_DISK_OPEN_READWRITE,
+                              GNUNET_DISK_PERM_USER_READ |
+                              GNUNET_DISK_PERM_USER_WRITE);
   GNUNET_free (fn);
-  GNUNET_DISK_file_seek (fh, idx * sizeof (GNUNET_HashCode), GNUNET_DISK_SEEK_SET);
-  if (sizeof (GNUNET_HashCode) != GNUNET_DISK_file_read (fh, nsid, sizeof (GNUNET_HashCode)))
+  GNUNET_DISK_file_seek (fh, idx * sizeof (GNUNET_HashCode),
+                         GNUNET_DISK_SEEK_SET);
+  if (sizeof (GNUNET_HashCode) !=
+      GNUNET_DISK_file_read (fh, nsid, sizeof (GNUNET_HashCode)))
     {
       GNUNET_DISK_file_close (fh);
       return GNUNET_SYSERR;
@@ -452,23 +418,23 @@ GNUNET_PSEUDONYM_name_to_id (const struct GNUNET_CONFIGURATION_Handle *cfg,
 
 
 /**
- * FIXME
+ * struct used to list the pseudonym
  */
 struct ListPseudonymClosure
 {
 
   /**
-   * FIXME
+   * iterator over pseudonym
    */
   GNUNET_PSEUDONYM_Iterator iterator;
-  
+
   /**
-   * FIXME
+   * Closure for iterator.
    */
   void *closure;
-    
+
   /**
-   * FIXME
+   * Configuration to use.
    */
   const struct GNUNET_CONFIGURATION_Handle *cfg;
 };
@@ -476,7 +442,9 @@ struct ListPseudonymClosure
 
 
 /**
- * FIXME
+ * the help function to list all available pseudonyms
+ * @param cls point to a struct ListPseudonymClosure
+ * @param fullname name of pseudonym
  */
 static int
 list_pseudonym_helper (void *cls, const char *fullname)
@@ -487,6 +455,7 @@ list_pseudonym_helper (void *cls, const char *fullname)
   int rating;
   struct GNUNET_CONTAINER_MetaData *meta;
   const char *fn;
+  char *str;
 
   if (strlen (fullname) < sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded))
     return GNUNET_OK;
@@ -498,8 +467,10 @@ list_pseudonym_helper (void *cls, const char *fullname)
   ret = GNUNET_OK;
   if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (fn, &id))
     return GNUNET_OK;           /* invalid name */
-  if (GNUNET_OK != read_info (c->cfg, &id, &meta, &rating, NULL))
+  str = NULL;
+  if (GNUNET_OK != read_info (c->cfg, &id, &meta, &rating, &str))
     return GNUNET_OK;           /* ignore entry */
+  GNUNET_free_non_null (str);
   if (c->iterator != NULL)
     ret = c->iterator (c->closure, &id, meta, rating);
   GNUNET_CONTAINER_meta_data_destroy (meta);
@@ -534,6 +505,7 @@ GNUNET_PSEUDONYM_list_all (const struct GNUNET_CONFIGURATION_Handle *cfg,
   return ret;
 }
 
+
 /**
  * Change the ranking of a pseudonym.
  *
@@ -566,33 +538,15 @@ GNUNET_PSEUDONYM_rank (const struct GNUNET_CONFIGURATION_Handle *cfg,
   return ranking;
 }
 
-/**
- * Insert metadata into existing MD record (passed as cls).
- *
- * @param cls metadata to add to
- * @param type type of entry to insert
- * @param data value of entry to insert
- */
-static int
-merge_meta_helper (void *cls,
-                  EXTRACTOR_KeywordType type, 
-                  const char *data)
-{
-  struct GNUNET_CONTAINER_MetaData *meta = cls;
-  GNUNET_CONTAINER_meta_data_insert (meta, type, data);
-  return GNUNET_OK;
-}
-
-
 
 /**
  * Add a pseudonym to the set of known pseudonyms.
  * For all pseudonym advertisements that we discover
- * FSUI should automatically call this function.
+ * FS should automatically call this function.
  *
  * @param cfg overall configuration
  * @param id the pseudonym identifier
- * @param metadata for the pseudonym
+ * @param meta metadata for the pseudonym
  */
 void
 GNUNET_PSEUDONYM_add (const struct GNUNET_CONFIGURATION_Handle *cfg,
@@ -612,7 +566,7 @@ GNUNET_PSEUDONYM_add (const struct GNUNET_CONFIGURATION_Handle *cfg,
   if ((0 == STAT (fn, &sbuf)) &&
       (GNUNET_OK == read_info (cfg, id, &old, &ranking, &name)))
     {
-      GNUNET_CONTAINER_meta_data_get_contents (meta, &merge_meta_helper, old);
+      GNUNET_CONTAINER_meta_data_merge (old, meta);
       write_pseudonym_info (cfg, id, old, ranking, name);
       GNUNET_CONTAINER_meta_data_destroy (old);
       GNUNET_free_non_null (name);
@@ -626,7 +580,4 @@ GNUNET_PSEUDONYM_add (const struct GNUNET_CONFIGURATION_Handle *cfg,
 }
 
 
-
-
-
 /* end of pseudonym.c */