-lots
[oweals/gnunet.git] / src / fs / fs_tree.c
index 5584ad8683816a49bc7a05f46607f5020a2e1501..30cdded677d6e06a84bda4867ee53f43fa6bc6c8 100644 (file)
@@ -27,7 +27,6 @@
 #include "platform.h"
 #include "fs_tree.h"
 
-#define DEBUG_TREE GNUNET_NO
 
 /**
  * Context for an ECRS-based file encoder that computes
@@ -100,7 +99,7 @@ struct GNUNET_FS_TreeEncoder
    * In-memory cache of the current CHK tree.
    * This struct will contain the CHK values
    * from the root to the currently processed
-   * node in the tree as identified by 
+   * node in the tree as identified by
    * "current_depth" and "publish_offset".
    * The "chktree" will be initially NULL,
    * then allocated to a sufficient number of
@@ -176,8 +175,8 @@ GNUNET_FS_tree_compute_tree_size (unsigned int depth)
  *
  * @param depth depth of the IBlock in the tree, 0 would be a DBLOCK,
  *        must be > 0 (this function is for IBLOCKs only!)
- * @param end_offset current offset in the payload (!) of the overall file, 
- *        must be > 0 (since this function is called at the 
+ * @param end_offset current offset in the payload (!) of the overall file,
+ *        must be > 0 (since this function is called at the
  *        end of a block).
  * @return size of the corresponding IBlock
  */
@@ -221,8 +220,8 @@ GNUNET_FS_tree_compute_iblock_size (unsigned int depth, uint64_t end_offset)
  * @return number of bytes stored in this node
  */
 size_t
-GNUNET_FS_tree_calculate_block_size (uint64_t fsize,
-                                     uint64_t offset, unsigned int depth)
+GNUNET_FS_tree_calculate_block_size (uint64_t fsize, uint64_t offset,
+                                     unsigned int depth)
 {
   size_t ret;
   uint64_t rsize;
@@ -264,14 +263,12 @@ GNUNET_FS_tree_calculate_block_size (uint64_t fsize,
  * @param cls closure for reader, proc, progress and cont
  * @param reader function to call to read plaintext data
  * @param proc function to call on each encrypted block
- * @param progress function to call with progress information 
+ * @param progress function to call with progress information
  * @param cont function to call when done
  */
 struct GNUNET_FS_TreeEncoder *
-GNUNET_FS_tree_encoder_create (struct GNUNET_FS_Handle *h,
-                               uint64_t size,
-                               void *cls,
-                               GNUNET_FS_DataReader reader,
+GNUNET_FS_tree_encoder_create (struct GNUNET_FS_Handle *h, uint64_t size,
+                               void *cls, GNUNET_FS_DataReader reader,
                                GNUNET_FS_TreeBlockProcessor proc,
                                GNUNET_FS_TreeProgressCallback progress,
                                GNUNET_SCHEDULER_Task cont)
@@ -287,8 +284,13 @@ GNUNET_FS_tree_encoder_create (struct GNUNET_FS_Handle *h,
   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_malloc (te->chk_tree_depth * CHK_PER_INODE *
+                     sizeof (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;
 }
 
@@ -299,7 +301,7 @@ GNUNET_FS_tree_encoder_create (struct GNUNET_FS_Handle *h,
  *
  * @param depth depth of the IBlock in the tree (aka overall
  *               number of tree levels minus depth); 0 == DBlock
- * @param end_offset current offset in the overall file, 
+ * @param end_offset current offset in the overall file,
  *               at the *beginning* of the block for DBLOCKs (depth==0),
  *               otherwise at the *end* of the block (exclusive)
  * @return (array of CHKs') offset in the above IBlock
@@ -342,11 +344,8 @@ 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_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->data.chk.chk = te->chk_tree[off];
@@ -362,49 +361,41 @@ 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, NULL);
       return;
     }
     pt_block = iob;
   }
   else
   {
-    pt_size = GNUNET_FS_tree_compute_iblock_size (te->current_depth,
-                                                  te->publish_offset);
+    pt_size =
+        GNUNET_FS_tree_compute_iblock_size (te->current_depth,
+                                            te->publish_offset);
     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
+              (unsigned long long) te->publish_offset, te->current_depth,
+              (unsigned int) pt_size, (unsigned int) off);
   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_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 == te->current_depth)
-              ? GNUNET_BLOCK_TYPE_FS_DBLOCK
-              : GNUNET_BLOCK_TYPE_FS_IBLOCK, enc, pt_size);
+    te->proc (te->cls, mychk, te->publish_offset, te->current_depth,
+              (0 ==
+               te->current_depth) ? GNUNET_BLOCK_TYPE_FS_DBLOCK :
+              GNUNET_BLOCK_TYPE_FS_IBLOCK, enc, pt_size);
   if (NULL != te->progress)
-    te->progress (te->cls,
-                  te->publish_offset, pt_block, pt_size, te->current_depth);
+    te->progress (te->cls, te->publish_offset, pt_block, pt_size,
+                  te->current_depth);
   if (0 == te->current_depth)
   {
     te->publish_offset += pt_size;
@@ -426,7 +417,7 @@ GNUNET_FS_tree_encoder_next (struct GNUNET_FS_TreeEncoder *te)
 /**
  * Clean up a tree encoder and return information
  * about the resulting URI or an error message.
- * 
+ *
  * @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
@@ -438,6 +429,7 @@ void
 GNUNET_FS_tree_encoder_finish (struct GNUNET_FS_TreeEncoder *te,
                                struct GNUNET_FS_Uri **uri, char **emsg)
 {
+  (void) te->reader (te->cls, UINT64_MAX, 0, 0, NULL);
   GNUNET_assert (GNUNET_NO == te->in_next);
   if (uri != NULL)
     *uri = te->uri;