fixing 1584
[oweals/gnunet.git] / src / fs / fs_publish.c
index bd5a9ebd6e574f16b98e928c65b766ebcd9a45f0..73fc710e18dc33b73960ac6b21473ed69102aa70 100644 (file)
  * @see http://gnunet.org/encoding
  * @author Krista Bennett
  * @author Christian Grothoff
- *
- * TODO:
- * - indexing cleanup: unindex on failure (can wait)
- * - datastore reservation support (optimization)
- * - location URIs (publish with anonymity-level zero)
  */
 
 #include "platform.h"
@@ -118,6 +113,11 @@ publish_cleanup (void *cls,
 {
   struct GNUNET_FS_PublishContext *pc = cls;
 
+  if (pc->fhc != NULL)
+    {
+      GNUNET_CRYPTO_hash_file_cancel (pc->fhc);
+      pc->fhc = NULL;
+    }
   GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
   if (pc->namespace != NULL)
     GNUNET_FS_namespace_delete (pc->namespace, GNUNET_NO);
@@ -172,6 +172,15 @@ ds_put_cont (void *cls,
       pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
       pi.value.publish.specifics.error.message = pcc->p->emsg;
       pcc->p->client_info = GNUNET_FS_publish_make_status_ (&pi, pcc->sc, pcc->p, 0);
+      if ( (pcc->p->is_directory == GNUNET_NO) &&
+          (pcc->p->filename != NULL) &&
+          (pcc->p->data.file.do_index == GNUNET_YES) )
+       {
+         /* run unindex to clean up */
+         GNUNET_FS_unindex_start (pcc->sc->h,
+                                  pcc->p->filename,
+                                  NULL);
+       }          
     }
   if (NULL != pcc->cont)
     pcc->sc->upload_task 
@@ -226,6 +235,16 @@ signal_publish_error (struct GNUNET_FS_FileInformation *p,
   pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
   pi.value.publish.specifics.error.message =emsg;
   p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p, 0);
+  if ( (p->is_directory == GNUNET_NO) &&
+       (p->filename != NULL) &&
+       (p->data.file.do_index == GNUNET_YES) )
+    {
+      /* run unindex to clean up */
+      GNUNET_FS_unindex_start (sc->h,
+                              p->filename,
+                              NULL);
+    }     
+  
 }
 
 
@@ -468,6 +487,15 @@ encode_cont (void *cls,
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Finished with tree encoder\n");
 #endif  
+  /* final progress event */
+  pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
+  pi.value.publish.specifics.progress.data = NULL;
+  pi.value.publish.specifics.progress.offset = GNUNET_FS_uri_chk_get_file_size (p->chk_uri);
+  pi.value.publish.specifics.progress.data_len = 0;
+  pi.value.publish.specifics.progress.depth = 0;
+  p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p, 
+                                                  GNUNET_FS_uri_chk_get_file_size (p->chk_uri));
+
   /* continue with main */
   sc->upload_task 
     = GNUNET_SCHEDULER_add_with_priority (sc->h->sched,
@@ -539,7 +567,7 @@ block_proc (void *cls,
       odb.offset = GNUNET_htonll (offset);
       odb.file_id = p->data.file.file_id;
       GNUNET_DATASTORE_put (sc->dsh,
-                           sc->rid,
+                           (p->is_directory) ? 0 : sc->rid,
                            query,
                            sizeof(struct OnDemandBlock),
                            &odb,
@@ -561,7 +589,7 @@ block_proc (void *cls,
              (unsigned int) block_size);
 #endif
   GNUNET_DATASTORE_put (sc->dsh,
-                       sc->rid,
+                       (p->is_directory) ? 0 : sc->rid,
                        query,
                        block_size,
                        block,
@@ -774,6 +802,7 @@ hash_for_index_cb (void *cls,
   uint64_t ino;
   char *fn;
 
+  sc->fhc = NULL;
   p = sc->fi_pos;
   if (NULL == res) 
     {
@@ -883,6 +912,7 @@ GNUNET_FS_publish_main_ (void *cls,
   struct GNUNET_FS_PublishContext *pc = cls;
   struct GNUNET_FS_ProgressInfo pi;
   struct GNUNET_FS_FileInformation *p;
+  struct GNUNET_FS_Uri *loc;
   char *fn;
 
   pc->upload_task = GNUNET_SCHEDULER_NO_TASK;  
@@ -954,6 +984,15 @@ GNUNET_FS_publish_main_ (void *cls,
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "File upload complete, now publishing KSK blocks.\n");
 #endif
+      if (0 == p->anonymity)
+       {
+         /* zero anonymity, box CHK URI in LOC URI */
+         loc = GNUNET_FS_uri_loc_create (p->chk_uri,
+                                         pc->h->cfg,
+                                         p->expirationTime);
+         GNUNET_FS_uri_destroy (p->chk_uri);
+         p->chk_uri = loc;
+       }
       GNUNET_FS_publish_sync_ (pc);
       /* upload of "p" complete, publish KBlocks! */
       if (p->keywords != NULL)
@@ -999,12 +1038,12 @@ GNUNET_FS_publish_main_ (void *cls,
       else
        {
          p->start_time = GNUNET_TIME_absolute_get ();
-         GNUNET_CRYPTO_hash_file (pc->h->sched,
-                                  GNUNET_SCHEDULER_PRIORITY_IDLE,
-                                  p->filename,
-                                  HASHING_BLOCKSIZE,
-                                  &hash_for_index_cb,
-                                  pc);
+         pc->fhc = GNUNET_CRYPTO_hash_file (pc->h->sched,
+                                            GNUNET_SCHEDULER_PRIORITY_IDLE,
+                                            p->filename,
+                                            HASHING_BLOCKSIZE,
+                                            &hash_for_index_cb,
+                                            pc);
        }
       return;
     }
@@ -1066,6 +1105,7 @@ fip_signal_start(void *cls,
       left = left * sizeof (struct ContentHashKey);
       sc->reserve_space += left;
     }
+  sc->reserve_entries++;
   /* entries and space for keywords */
   if (NULL != *uri)
     {
@@ -1211,6 +1251,7 @@ GNUNET_FS_publish_start (struct GNUNET_FS_Handle *h,
   struct GNUNET_FS_PublishContext *ret;
   struct GNUNET_DATASTORE_Handle *dsh;
 
+  GNUNET_assert (NULL != h);
   if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
     {
       dsh = GNUNET_DATASTORE_connect (h->cfg,