Merge branch 'master' of git+ssh://gnunet.org/gnunet
[oweals/gnunet.git] / src / fs / fs_tree.c
index fc04446c9a7a472c8098a5f35d7d01bc2a6fd63a..e57e4e4943e806adcc2e8ba4367203f23076bd5c 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2009-2011 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2009-2011 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
@@ -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
-     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.
 */
 /**
  * @file fs/fs_tree.c
@@ -27,7 +27,6 @@
 #include "platform.h"
 #include "fs_tree.h"
 
-#define DEBUG_TREE GNUNET_EXTRA_LOGGING
 
 /**
  * Context for an ECRS-based file encoder that computes
@@ -64,7 +63,7 @@ struct GNUNET_FS_TreeEncoder
   /**
    * Function to call once we're done with processing.
    */
-  GNUNET_SCHEDULER_Task cont;
+  GNUNET_SCHEDULER_TaskCallback cont;
 
   /**
    * Set to an error message (if we had an error).
@@ -251,13 +250,13 @@ GNUNET_FS_tree_calculate_block_size (uint64_t fsize, uint64_t offset,
 
 
 /**
- * Initialize a tree encoder.  This function will call "proc" and
+ * Initialize a tree encoder.  This function will call @a proc and
  * "progress" on each block in the tree.  Once all blocks have been
- * processed, "cont" will be scheduled.  The "reader" will be called
+ * processed, "cont" will be scheduled.  The @a reader will be called
  * to obtain the (plaintext) blocks for the file.  Note that this
- * function will not actually call "proc".  The client must
- * call "GNUNET_FS_tree_encoder_next" to trigger encryption (and
- * calling of "proc") for the each block.
+ * function will not actually call @a proc.  The client must
+ * call #GNUNET_FS_tree_encoder_next to trigger encryption (and
+ * calling of @a proc) for the each block.
  *
  * @param h the global FS context
  * @param size overall size of the file to encode
@@ -269,14 +268,15 @@ GNUNET_FS_tree_calculate_block_size (uint64_t fsize, uint64_t offset,
  */
 struct GNUNET_FS_TreeEncoder *
 GNUNET_FS_tree_encoder_create (struct GNUNET_FS_Handle *h, uint64_t size,
-                               void *cls, GNUNET_FS_DataReader reader,
+                               void *cls,
+                              GNUNET_FS_DataReader reader,
                                GNUNET_FS_TreeBlockProcessor proc,
                                GNUNET_FS_TreeProgressCallback progress,
-                               GNUNET_SCHEDULER_Task cont)
+                               GNUNET_SCHEDULER_TaskCallback cont)
 {
   struct GNUNET_FS_TreeEncoder *te;
 
-  te = GNUNET_malloc (sizeof (struct GNUNET_FS_TreeEncoder));
+  te = GNUNET_new (struct GNUNET_FS_TreeEncoder);
   te->h = h;
   te->size = size;
   te->cls = cls;
@@ -285,9 +285,13 @@ GNUNET_FS_tree_encoder_create (struct GNUNET_FS_Handle *h, uint64_t size,
   te->progress = progress;
   te->cont = cont;
   te->chk_tree_depth = GNUNET_FS_compute_depth (size);
-  te->chk_tree =
-      GNUNET_malloc (te->chk_tree_depth * CHK_PER_INODE *
-                     sizeof (struct ContentHashKey));
+  te->chk_tree
+    = GNUNET_new_array (te->chk_tree_depth * CHK_PER_INODE,
+                        struct ContentHashKey);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Created tree encoder for file with %llu bytes and depth %u\n",
+             (unsigned long long) size,
+             te->chk_tree_depth);
   return te;
 }
 
@@ -332,8 +336,8 @@ GNUNET_FS_tree_encoder_next (struct GNUNET_FS_TreeEncoder *te)
   uint16_t pt_size;
   char iob[DBLOCK_SIZE];
   char enc[DBLOCK_SIZE];
-  struct GNUNET_CRYPTO_AesSessionKey sk;
-  struct GNUNET_CRYPTO_AesInitializationVector iv;
+  struct GNUNET_CRYPTO_SymmetricSessionKey sk;
+  struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
   unsigned int off;
 
   GNUNET_assert (GNUNET_NO == te->in_next);
@@ -341,16 +345,14 @@ GNUNET_FS_tree_encoder_next (struct GNUNET_FS_TreeEncoder *te)
   if (te->chk_tree_depth == te->current_depth)
   {
     off = CHK_PER_INODE * (te->chk_tree_depth - 1);
-#if DEBUG_TREE
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TE done, reading CHK `%s' from %u\n",
                 GNUNET_h2s (&te->chk_tree[off].query), off);
-#endif
-    te->uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
-    te->uri->type = chk;
+    te->uri = GNUNET_new (struct GNUNET_FS_Uri);
+    te->uri->type = GNUNET_FS_URI_CHK;
     te->uri->data.chk.chk = te->chk_tree[off];
     te->uri->data.chk.file_length = GNUNET_htonll (te->size);
     te->in_next = GNUNET_NO;
-    te->cont (te->cls, NULL);
+    te->cont (te->cls);
     return;
   }
   if (0 == te->current_depth)
@@ -360,9 +362,8 @@ GNUNET_FS_tree_encoder_next (struct GNUNET_FS_TreeEncoder *te)
     if (pt_size !=
         te->reader (te->cls, te->publish_offset, pt_size, iob, &te->emsg))
     {
-      GNUNET_SCHEDULER_add_continuation (te->cont, te->cls,
-                                         GNUNET_SCHEDULER_REASON_TIMEOUT);
       te->in_next = GNUNET_NO;
+      te->cont (te->cls);
       return;
     }
     pt_block = iob;
@@ -375,23 +376,19 @@ GNUNET_FS_tree_encoder_next (struct GNUNET_FS_TreeEncoder *te)
     pt_block = &te->chk_tree[(te->current_depth - 1) * CHK_PER_INODE];
   }
   off = compute_chk_offset (te->current_depth, te->publish_offset);
-#if DEBUG_TREE
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "TE is at offset %llu and depth %u with block size %u and target-CHK-offset %u\n",
               (unsigned long long) te->publish_offset, te->current_depth,
               (unsigned int) pt_size, (unsigned int) off);
-#endif
   mychk = &te->chk_tree[te->current_depth * CHK_PER_INODE + off];
   GNUNET_CRYPTO_hash (pt_block, pt_size, &mychk->key);
   GNUNET_CRYPTO_hash_to_aes_key (&mychk->key, &sk, &iv);
-  GNUNET_CRYPTO_aes_encrypt (pt_block, pt_size, &sk, &iv, enc);
+  GNUNET_CRYPTO_symmetric_encrypt (pt_block, pt_size, &sk, &iv, enc);
   GNUNET_CRYPTO_hash (enc, pt_size, &mychk->query);
-#if DEBUG_TREE
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "TE calculates query to be `%s', stored at %u\n",
               GNUNET_h2s (&mychk->query),
               te->current_depth * CHK_PER_INODE + off);
-#endif
   if (NULL != te->proc)
     te->proc (te->cls, mychk, te->publish_offset, te->current_depth,
               (0 ==
@@ -418,25 +415,42 @@ GNUNET_FS_tree_encoder_next (struct GNUNET_FS_TreeEncoder *te)
 }
 
 
+/**
+ * Get the resulting URI from the encoding.
+ *
+ * @param te the tree encoder to clean up
+ * @return uri set to the resulting URI (if encoding finished), NULL otherwise
+ */
+struct GNUNET_FS_Uri *
+GNUNET_FS_tree_encoder_get_uri (struct GNUNET_FS_TreeEncoder *te)
+{
+  if (NULL != te->uri)
+    return GNUNET_FS_uri_dup (te->uri);
+  return NULL;
+}
+
+
 /**
  * Clean up a tree encoder and return information
- * about the resulting URI or an error message.
+ * about possible errors.
  *
  * @param te the tree encoder to clean up
- * @param uri set to the resulting URI (if encoding finished)
  * @param emsg set to an error message (if an error occured
  *        within the tree encoder; if this function is called
  *        prior to completion and prior to an internal error,
- *        both "*uri" and "*emsg" will be set to NULL).
+ *        both "*emsg" will be set to NULL).
  */
 void
 GNUNET_FS_tree_encoder_finish (struct GNUNET_FS_TreeEncoder *te,
-                               struct GNUNET_FS_Uri **uri, char **emsg)
+                              char **emsg)
 {
+  if (NULL != te->reader)
+  {
+    (void) te->reader (te->cls, UINT64_MAX, 0, 0, NULL);
+    te->reader =  NULL;
+  }
   GNUNET_assert (GNUNET_NO == te->in_next);
-  if (uri != NULL)
-    *uri = te->uri;
-  else if (NULL != te->uri)
+  if (NULL != te->uri)
     GNUNET_FS_uri_destroy (te->uri);
   if (emsg != NULL)
     *emsg = te->emsg;