Merge branch 'master' of git+ssh://gnunet.org/gnunet
[oweals/gnunet.git] / src / fs / fs_sharetree.c
index 8660c8958e407ddb017a6f8b895396cab17d19ba..906e78dd3ef95c0fb674a39728897d353242d5a9 100644 (file)
@@ -1,10 +1,10 @@
 /*
      This file is part of GNUnet
 /*
      This file is part of GNUnet
-     (C) 2005-2012 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2005-2012 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
 
      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
+     by the Free Software Foundation; either version 3, or (at your
      option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
      option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
@@ -14,8 +14,8 @@
 
      You should have received a copy of the GNU General Public License
      along with GNUnet; see the file COPYING.  If not, write to the
 
      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.
+     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+     Boston, MA 02110-1301, USA.
 */
 
 /**
 */
 
 /**
@@ -162,7 +162,7 @@ add_to_keyword_counter (void *cls, const char *keyword, int is_mandatory)
 {
   struct GNUNET_CONTAINER_MultiHashMap *mcm = cls;
   struct KeywordCounter *cnt;
 {
   struct GNUNET_CONTAINER_MultiHashMap *mcm = cls;
   struct KeywordCounter *cnt;
-  GNUNET_HashCode hc;
+  struct GNUNET_HashCode hc;
   size_t klen;
 
   klen = strlen (keyword) + 1;
   size_t klen;
 
   klen = strlen (keyword) + 1;
@@ -172,9 +172,9 @@ add_to_keyword_counter (void *cls, const char *keyword, int is_mandatory)
   {
     cnt = GNUNET_malloc (sizeof (struct KeywordCounter) + klen);
     cnt->value = (const char *) &cnt[1];
   {
     cnt = GNUNET_malloc (sizeof (struct KeywordCounter) + klen);
     cnt->value = (const char *) &cnt[1];
-    memcpy (&cnt[1], keyword, klen);
+    GNUNET_memcpy (&cnt[1], keyword, klen);
     GNUNET_assert (GNUNET_OK ==
     GNUNET_assert (GNUNET_OK ==
-                  GNUNET_CONTAINER_multihashmap_put (mcm, 
+                  GNUNET_CONTAINER_multihashmap_put (mcm,
                                                      &hc, cnt,
                                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
   }
                                                      &hc, cnt,
                                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
   }
@@ -198,7 +198,7 @@ add_to_keyword_counter (void *cls, const char *keyword, int is_mandatory)
  *        can be NULL (if mime-type is not known)
  * @param data actual meta-data found
  * @param data_len number of bytes in data
  *        can be NULL (if mime-type is not known)
  * @param data actual meta-data found
  * @param data_len number of bytes in data
- * @return GNUNET_OK to continue extracting / iterating
+ * @return 0 to continue extracting / iterating
  */
 static int
 add_to_meta_counter (void *cls, const char *plugin_name,
  */
 static int
 add_to_meta_counter (void *cls, const char *plugin_name,
@@ -206,14 +206,14 @@ add_to_meta_counter (void *cls, const char *plugin_name,
                     const char *data_mime_type, const char *data, size_t data_len)
 {
   struct GNUNET_CONTAINER_MultiHashMap *map = cls;
                     const char *data_mime_type, const char *data, size_t data_len)
 {
   struct GNUNET_CONTAINER_MultiHashMap *map = cls;
-  GNUNET_HashCode key;
+  struct GNUNET_HashCode key;
   struct MetaCounter *cnt;
 
   GNUNET_CRYPTO_hash (data, data_len, &key);
   cnt = GNUNET_CONTAINER_multihashmap_get (map, &key);
   struct MetaCounter *cnt;
 
   GNUNET_CRYPTO_hash (data, data_len, &key);
   cnt = GNUNET_CONTAINER_multihashmap_get (map, &key);
-  if (cnt == NULL)
+  if (NULL == cnt)
   {
   {
-    cnt = GNUNET_malloc (sizeof (struct MetaCounter));
+    cnt = GNUNET_new (struct MetaCounter);
     cnt->data = data;
     cnt->data_size = data_len;
     cnt->plugin_name = plugin_name;
     cnt->data = data;
     cnt->data_size = data_len;
     cnt->plugin_name = plugin_name;
@@ -243,7 +243,7 @@ remove_high_frequency_keywords (void *cls, const char *keyword, int is_mandatory
 {
   struct TrimContext *tc = cls;
   struct KeywordCounter *counter;
 {
   struct TrimContext *tc = cls;
   struct KeywordCounter *counter;
-  GNUNET_HashCode hc;
+  struct GNUNET_HashCode hc;
   size_t klen;
 
   klen = strlen (keyword) + 1;
   size_t klen;
 
   klen = strlen (keyword) + 1;
@@ -268,7 +268,7 @@ remove_high_frequency_keywords (void *cls, const char *keyword, int is_mandatory
  * @return GNUNET_YES (always)
  */
 static int
  * @return GNUNET_YES (always)
  */
 static int
-migrate_and_drop_keywords (void *cls, const GNUNET_HashCode * key, void *value)
+migrate_and_drop_keywords (void *cls, const struct GNUNET_HashCode * key, void *value)
 {
   struct TrimContext *tc = cls;
   struct KeywordCounter *counter = value;
 {
   struct TrimContext *tc = cls;
   struct KeywordCounter *counter = value;
@@ -299,18 +299,22 @@ migrate_and_drop_keywords (void *cls, const GNUNET_HashCode * key, void *value)
  * @return GNUNET_YES (always)
  */
 static int
  * @return GNUNET_YES (always)
  */
 static int
-migrate_and_drop_metadata (void *cls, const GNUNET_HashCode * key, void *value)
+migrate_and_drop_metadata (void *cls, const struct GNUNET_HashCode * key, void *value)
 {
   struct TrimContext *tc = cls;
   struct MetaCounter *counter = value;
 
   if (counter->count >= tc->move_threshold)
 {
   struct TrimContext *tc = cls;
   struct MetaCounter *counter = value;
 
   if (counter->count >= tc->move_threshold)
+  {
+    if (NULL == tc->pos->meta)
+      tc->pos->meta = GNUNET_CONTAINER_meta_data_create ();
     GNUNET_CONTAINER_meta_data_insert (tc->pos->meta,
                                       counter->plugin_name,
                                       counter->type,
                                       counter->format,
                                       counter->data_mime_type, counter->data,
     GNUNET_CONTAINER_meta_data_insert (tc->pos->meta,
                                       counter->plugin_name,
                                       counter->type,
                                       counter->format,
                                       counter->data_mime_type, counter->data,
-                                      counter->data_size); 
+                                      counter->data_size);
+  }
   GNUNET_assert (GNUNET_YES ==
                 GNUNET_CONTAINER_multihashmap_remove (tc->metacounter,
                                                       key,
   GNUNET_assert (GNUNET_YES ==
                 GNUNET_CONTAINER_multihashmap_remove (tc->metacounter,
                                                       key,
@@ -343,13 +347,15 @@ share_tree_trim (struct TrimContext *tc,
   }
 
   /* consider adding filename to directory meta data */
   }
 
   /* consider adding filename to directory meta data */
-  if (tree->is_directory)
+  if (tree->is_directory == GNUNET_YES)
   {
     const char *user = getenv ("USER");
   {
     const char *user = getenv ("USER");
-    if ( (user == NULL) || 
+    if ( (user == NULL) ||
         (0 != strncasecmp (user, tree->short_filename, strlen(user))))
     {
       /* only use filename if it doesn't match $USER */
         (0 != strncasecmp (user, tree->short_filename, strlen(user))))
     {
       /* only use filename if it doesn't match $USER */
+      if (NULL == tree->meta)
+       tree->meta = GNUNET_CONTAINER_meta_data_create ();
       GNUNET_CONTAINER_meta_data_insert (tree->meta, "<libgnunetfs>",
                                         EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME,
                                         EXTRACTOR_METAFORMAT_UTF8,
       GNUNET_CONTAINER_meta_data_insert (tree->meta, "<libgnunetfs>",
                                         EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME,
                                         EXTRACTOR_METAFORMAT_UTF8,
@@ -360,11 +366,12 @@ share_tree_trim (struct TrimContext *tc,
 
   if (1 >= num_children)
     return; /* nothing to trim */
 
   if (1 >= num_children)
     return; /* nothing to trim */
-  
+
   /* now, count keywords and meta data in children */
   for (pos = tree->children_head; NULL != pos; pos = pos->next)
   {
   /* now, count keywords and meta data in children */
   for (pos = tree->children_head; NULL != pos; pos = pos->next)
   {
-    GNUNET_CONTAINER_meta_data_iterate (pos->meta, &add_to_meta_counter, tc->metacounter);    
+    if (NULL != pos->meta)
+      GNUNET_CONTAINER_meta_data_iterate (pos->meta, &add_to_meta_counter, tc->metacounter);
     if (NULL != pos->ksk_uri)
       GNUNET_FS_uri_ksk_get_keywords (pos->ksk_uri, &add_to_keyword_counter, tc->keywordcounter);
   }
     if (NULL != pos->ksk_uri)
       GNUNET_FS_uri_ksk_get_keywords (pos->ksk_uri, &add_to_keyword_counter, tc->keywordcounter);
   }
@@ -377,15 +384,19 @@ share_tree_trim (struct TrimContext *tc,
   {
     tc->pos = pos;
     if (NULL != pos->ksk_uri)
   {
     tc->pos = pos;
     if (NULL != pos->ksk_uri)
-      GNUNET_FS_uri_ksk_get_keywords (pos->ksk_uri, &remove_high_frequency_keywords, tc);
+    {
+      struct GNUNET_FS_Uri *ksk_uri_copy = GNUNET_FS_uri_dup (pos->ksk_uri);
+      GNUNET_FS_uri_ksk_get_keywords (ksk_uri_copy, &remove_high_frequency_keywords, tc);
+      GNUNET_FS_uri_destroy (ksk_uri_copy);
+    }
   }
 
   /* add high-frequency meta data and keywords to parent */
   tc->pos = tree;
   }
 
   /* add high-frequency meta data and keywords to parent */
   tc->pos = tree;
-  GNUNET_CONTAINER_multihashmap_iterate (tc->keywordcounter, 
+  GNUNET_CONTAINER_multihashmap_iterate (tc->keywordcounter,
                                         &migrate_and_drop_keywords,
                                         tc);
                                         &migrate_and_drop_keywords,
                                         tc);
-  GNUNET_CONTAINER_multihashmap_iterate (tc->metacounter, 
+  GNUNET_CONTAINER_multihashmap_iterate (tc->metacounter,
                                         &migrate_and_drop_metadata,
                                         tc);
 }
                                         &migrate_and_drop_metadata,
                                         tc);
 }
@@ -403,9 +414,9 @@ GNUNET_FS_share_tree_trim (struct GNUNET_FS_ShareTreeItem *toplevel)
   struct TrimContext tc;
 
   if (toplevel == NULL)
   struct TrimContext tc;
 
   if (toplevel == NULL)
-    return;  
-  tc.keywordcounter = GNUNET_CONTAINER_multihashmap_create (1024);
-  tc.metacounter = GNUNET_CONTAINER_multihashmap_create (1024);
+    return;
+  tc.keywordcounter = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_NO);
+  tc.metacounter = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_NO);
   share_tree_trim (&tc, toplevel);
   GNUNET_CONTAINER_multihashmap_destroy (tc.keywordcounter);
   GNUNET_CONTAINER_multihashmap_destroy (tc.metacounter);
   share_tree_trim (&tc, toplevel);
   GNUNET_CONTAINER_multihashmap_destroy (tc.keywordcounter);
   GNUNET_CONTAINER_multihashmap_destroy (tc.metacounter);