fix
[oweals/gnunet.git] / src / fs / fs_publish.c
index 6364dac0ad158ce5fd5d2cc01be25c3cf4bf4ed8..2ebb96169c5933855d18be38bfc3b332cc1b293c 100644 (file)
@@ -85,7 +85,7 @@ GNUNET_FS_publish_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
                                const struct GNUNET_FS_FileInformation *p,
                                uint64_t offset)
 {
-  pi->value.publish.sc = sc;
+  pi->value.publish.pc = sc;
   pi->value.publish.fi = p;
   pi->value.publish.cctx
     = p->client_info;
@@ -109,18 +109,26 @@ GNUNET_FS_publish_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
 /**
  * Cleanup the publish context, we're done with it.
  *
- * @param pc struct to clean up after
+ * @param cls struct to clean up after
+ * @param tc scheduler context
  */
 static void
-publish_cleanup (struct GNUNET_FS_PublishContext *pc)
+publish_cleanup (void *cls,
+                const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
+  struct GNUNET_FS_PublishContext *pc = cls;
+
   GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
   if (pc->namespace != NULL)
     GNUNET_FS_namespace_delete (pc->namespace, GNUNET_NO);
   GNUNET_free_non_null (pc->nid);  
   GNUNET_free_non_null (pc->nuid);
   GNUNET_free_non_null (pc->serialization);
-  GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
+  if (pc->dsh != NULL)
+    {
+      GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
+      pc->dsh = NULL;
+    }
   if (pc->client != NULL)
     GNUNET_CLIENT_disconnect (pc->client, GNUNET_NO);
   GNUNET_free (pc);
@@ -145,9 +153,12 @@ ds_put_cont (void *cls,
 
   if (GNUNET_SYSERR == pcc->sc->in_network_wait)
     {
-      /* we were aborted in the meantime,
-        finish shutdown! */
-      publish_cleanup (pcc->sc);
+      /* we were aborted in the meantime, finish shutdown! */
+      GNUNET_SCHEDULER_add_continuation (pcc->sc->h->sched,                                     
+                                        &publish_cleanup,
+                                        pcc->sc,
+                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
+      GNUNET_free (pcc);
       return;
     }
   GNUNET_assert (GNUNET_YES == pcc->sc->in_network_wait);
@@ -293,6 +304,11 @@ publish_kblocks_cont (void *cls,
 
   if (NULL != emsg)
     {
+#if DEBUG_PUBLISH
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "Error uploading KSK blocks: %s\n",
+                 emsg);
+#endif
       signal_publish_error (p, pc, emsg);
       GNUNET_FS_file_information_sync_ (p);
       GNUNET_FS_publish_sync_ (pc);
@@ -303,6 +319,10 @@ publish_kblocks_cont (void *cls,
                                              pc);
       return;
     }
+#if DEBUG_PUBLISH
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "KSK blocks published, moving on to next file\n");
+#endif
   if (NULL != p->dir)
     signal_publish_completion (p, pc);    
   /* move on to next file */
@@ -389,7 +409,7 @@ encode_cont (void *cls,
   struct GNUNET_FS_FileInformation *p;
   struct GNUNET_FS_ProgressInfo pi;
   char *emsg;
-  
+
   p = sc->fi_pos;
   GNUNET_FS_tree_encoder_finish (p->te,
                                 &p->chk_uri,
@@ -397,6 +417,11 @@ encode_cont (void *cls,
   p->te = NULL;
   if (NULL != emsg)
     {
+#if DEBUG_PUBLISH
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "Error during tree walk: %s\n",
+                 emsg);
+#endif
       GNUNET_asprintf (&p->emsg, 
                       _("Upload failed: %s"),
                       emsg);
@@ -406,6 +431,10 @@ encode_cont (void *cls,
       pi.value.publish.specifics.error.message = p->emsg;
       p->client_info =  GNUNET_FS_publish_make_status_ (&pi, sc, p, 0);
     }
+#if DEBUG_PUBLISH
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Finished with tree encoder\n");
+#endif  
   /* continue with main */
   sc->upload_task 
     = GNUNET_SCHEDULER_add_with_priority (sc->h->sched,
@@ -444,6 +473,10 @@ block_proc (void *cls,
   p = sc->fi_pos;
   if (NULL == sc->dsh)
     {
+#if DEBUG_PUBLISH
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "Waiting for datastore connection\n");
+#endif
       sc->upload_task
        = GNUNET_SCHEDULER_add_with_priority (sc->h->sched,
                                              GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
@@ -481,6 +514,7 @@ block_proc (void *cls,
                            p->priority,
                            p->anonymity,
                            p->expirationTime,
+                           -2, 1,
                            GNUNET_CONSTANTS_SERVICE_TIMEOUT,
                            &ds_put_cont,
                            dpc_cls);     
@@ -502,6 +536,7 @@ block_proc (void *cls,
                        p->priority,
                        p->anonymity,
                        p->expirationTime,
+                       -2, 1,
                        GNUNET_CONSTANTS_SERVICE_TIMEOUT,
                        &ds_put_cont,
                        dpc_cls);
@@ -562,6 +597,10 @@ publish_content (struct GNUNET_FS_PublishContext *sc)
     {
       if (p->is_directory)
        {
+#if DEBUG_PUBLISH
+         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                     "Creating directory\n");
+#endif
          db = GNUNET_FS_directory_builder_create (p->meta);
          dirpos = p->data.dir.entries;
          while (NULL != dirpos)
@@ -607,6 +646,10 @@ publish_content (struct GNUNET_FS_PublishContext *sc)
       size = (p->is_directory) 
        ? p->data.dir.dir_size 
        : p->data.file.file_size;
+#if DEBUG_PUBLISH
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "Creating tree encoder\n");
+#endif
       p->te = GNUNET_FS_tree_encoder_create (sc->h,
                                             size,
                                             sc,
@@ -616,6 +659,10 @@ publish_content (struct GNUNET_FS_PublishContext *sc)
                                             &encode_cont);
 
     }
+#if DEBUG_PUBLISH
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Processing next block from tree\n");
+#endif
   GNUNET_FS_tree_encoder_next (p->te);
 }
 
@@ -712,6 +759,7 @@ hash_for_index_cb (void *cls,
       return;
     }
   fn = GNUNET_STRINGS_filename_expand (p->filename);
+  GNUNET_assert (fn != NULL);
   slen = strlen (fn) + 1;
   if (slen > GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof(struct IndexStartMessage))
     {
@@ -728,7 +776,7 @@ hash_for_index_cb (void *cls,
 #if DEBUG_PUBLISH
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Hash of indexed file `%s' is `%s'\n",
-             p->data.file.filename,
+             p->filename,
              GNUNET_h2s (res));
 #endif
   client = GNUNET_CLIENT_connect (sc->h->sched,
@@ -797,19 +845,23 @@ void
 GNUNET_FS_publish_main_ (void *cls,
                         const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-  struct GNUNET_FS_PublishContext *sc = cls;
+  struct GNUNET_FS_PublishContext *pc = cls;
   struct GNUNET_FS_ProgressInfo pi;
   struct GNUNET_FS_FileInformation *p;
   char *fn;
 
-  sc->upload_task = GNUNET_SCHEDULER_NO_TASK;  
-  p = sc->fi_pos;
+  pc->upload_task = GNUNET_SCHEDULER_NO_TASK;  
+  p = pc->fi_pos;
   if (NULL == p)
     {
+#if DEBUG_PUBLISH
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "Upload complete, now publishing SKS and KSK blocks.\n");
+#endif
       /* upload of entire hierarchy complete,
         publish namespace entries */
-      GNUNET_FS_publish_sync_ (sc);
-      publish_sblock (sc);
+      GNUNET_FS_publish_sync_ (pc);
+      publish_sblock (pc);
       return;
     }
   /* find starting position */
@@ -819,12 +871,17 @@ GNUNET_FS_publish_main_ (void *cls,
          (NULL == p->data.dir.entries->chk_uri) )
     {
       p = p->data.dir.entries;
-      sc->fi_pos = p;
-      GNUNET_FS_publish_sync_ (sc);
+      pc->fi_pos = p;
+      GNUNET_FS_publish_sync_ (pc);
     }
   /* abort on error */
   if (NULL != p->emsg)
     {
+#if DEBUG_PUBLISH
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "Error uploading: %s\n",
+                 p->emsg);
+#endif
       /* error with current file, abort all
         related files as well! */
       while (NULL != p->dir)
@@ -849,33 +906,37 @@ GNUNET_FS_publish_main_ (void *cls,
          pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
          pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
          pi.value.publish.specifics.error.message = p->emsg;
-         p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p, 0);
+         p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
        }
-      sc->all_done = GNUNET_YES;
-      GNUNET_FS_publish_sync_ (sc);
+      pc->all_done = GNUNET_YES;
+      GNUNET_FS_publish_sync_ (pc);
       return;
     }
   /* handle completion */
   if (NULL != p->chk_uri)
     {
-      GNUNET_FS_publish_sync_ (sc);
+#if DEBUG_PUBLISH
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "File upload complete, now publishing KSK blocks.\n");
+#endif
+      GNUNET_FS_publish_sync_ (pc);
       /* upload of "p" complete, publish KBlocks! */
       if (p->keywords != NULL)
        {
-         GNUNET_FS_publish_ksk (sc->h,
+         GNUNET_FS_publish_ksk (pc->h,
                                 p->keywords,
                                 p->meta,
                                 p->chk_uri,
                                 p->expirationTime,
                                 p->anonymity,
                                 p->priority,
-                                sc->options,
+                                pc->options,
                                 &publish_kblocks_cont,
-                                sc);
+                                pc);
        }
       else
        {
-         publish_kblocks_cont (sc,
+         publish_kblocks_cont (pc,
                                p->chk_uri,
                                NULL);
        }
@@ -892,27 +953,27 @@ GNUNET_FS_publish_main_ (void *cls,
                      "<no-name>",
                      _("needs to be an actual file"));
          GNUNET_FS_file_information_sync_ (p);
-         publish_content (sc);
+         publish_content (pc);
          return;
        }      
       if (p->data.file.have_hash)
        {
-         hash_for_index_cb (sc,
+         hash_for_index_cb (pc,
                             &p->data.file.file_id);
        }
       else
        {
          p->start_time = GNUNET_TIME_absolute_get ();
-         GNUNET_CRYPTO_hash_file (sc->h->sched,
+         GNUNET_CRYPTO_hash_file (pc->h->sched,
                                   GNUNET_SCHEDULER_PRIORITY_IDLE,
                                   p->filename,
                                   HASHING_BLOCKSIZE,
                                   &hash_for_index_cb,
-                                  sc);
+                                  pc);
        }
       return;
     }
-  publish_content (sc);
+  publish_content (pc);
 }
 
 
@@ -927,6 +988,7 @@ GNUNET_FS_publish_main_ (void *cls,
  * @param uri pointer to the keywords that will be used for this entry (can be modified)
  * @param anonymity pointer to selected anonymity level (can be modified)
  * @param priority pointer to selected priority (can be modified)
+ * @param do_index should we index?
  * @param expirationTime pointer to selected expiration time (can be modified)
  * @param client_info pointer to client context set upon creation (can be modified)
  * @return GNUNET_OK to continue (always)
@@ -939,6 +1001,7 @@ fip_signal_start(void *cls,
                 struct GNUNET_FS_Uri **uri,
                 uint32_t *anonymity,
                 uint32_t *priority,
+                int *do_index,
                 struct GNUNET_TIME_Absolute *expirationTime,
                 void **client_info)
 {
@@ -963,6 +1026,7 @@ fip_signal_start(void *cls,
  * @param uri pointer to the keywords that will be used for this entry (can be modified)
  * @param anonymity pointer to selected anonymity level (can be modified)
  * @param priority pointer to selected priority (can be modified)
+ * @param do_index should we index?
  * @param expirationTime pointer to selected expiration time (can be modified)
  * @param client_info pointer to client context set upon creation (can be modified)
  * @return GNUNET_OK to continue (always)
@@ -975,6 +1039,7 @@ fip_signal_suspend(void *cls,
                   struct GNUNET_FS_Uri **uri,
                   uint32_t *anonymity,
                   uint32_t *priority,
+                  int *do_index,
                   struct GNUNET_TIME_Absolute *expirationTime,
                   void **client_info)
 {
@@ -988,6 +1053,11 @@ fip_signal_suspend(void *cls,
   pi.status = GNUNET_FS_STATUS_PUBLISH_SUSPEND;
   GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, sc, fi, off));
   *client_info = NULL;
+  if (NULL != sc->dsh)
+    {
+      GNUNET_DATASTORE_disconnect (sc->dsh, GNUNET_NO);
+      sc->dsh = NULL;
+    }
   return GNUNET_OK;
 }
 
@@ -998,8 +1068,8 @@ fip_signal_suspend(void *cls,
  *
  * @param cls the 'struct GNUNET_FS_PublishContext' to signal for
  */
-static void
-publish_signal_suspend (void *cls)
+void
+GNUNET_FS_publish_signal_suspend_ (void *cls)
 {
   struct GNUNET_FS_PublishContext *pc = cls;
 
@@ -1012,9 +1082,10 @@ publish_signal_suspend (void *cls)
                                      &fip_signal_suspend,
                                      pc);
   GNUNET_FS_end_top (pc->h, pc->top);
-  publish_cleanup (pc);
+  publish_cleanup (pc, NULL);
 }
 
+
 /**
  * Publish a file or directory.
  *
@@ -1068,7 +1139,7 @@ GNUNET_FS_publish_start (struct GNUNET_FS_Handle *h,
                                      &fip_signal_start,
                                      ret);
   ret->fi_pos = ret->fi;
-  ret->top = GNUNET_FS_make_top (h, &publish_signal_suspend, ret);
+  ret->top = GNUNET_FS_make_top (h, &GNUNET_FS_publish_signal_suspend_, ret);
   GNUNET_FS_publish_sync_ (ret);
   // FIXME: calculate space needed for "fi"
   // and reserve as first task (then trigger
@@ -1093,6 +1164,7 @@ GNUNET_FS_publish_start (struct GNUNET_FS_Handle *h,
  * @param uri pointer to the keywords that will be used for this entry (can be modified)
  * @param anonymity pointer to selected anonymity level (can be modified)
  * @param priority pointer to selected priority (can be modified)
+ * @param do_index should we index?
  * @param expirationTime pointer to selected expiration time (can be modified)
  * @param client_info pointer to client context set upon creation (can be modified)
  * @return GNUNET_OK to continue (always)
@@ -1105,6 +1177,7 @@ fip_signal_stop(void *cls,
                struct GNUNET_FS_Uri **uri,
                uint32_t *anonymity,
                uint32_t *priority,
+               int *do_index,
                struct GNUNET_TIME_Absolute *expirationTime,
                void **client_info)
 {
@@ -1114,12 +1187,9 @@ fip_signal_stop(void *cls,
 
   if (fi->serialization != NULL) 
     {
-      if (0 != UNLINK (fi->serialization))
-       {
-         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
-                                   "unlink",
-                                   fi->serialization); 
-       }
+      GNUNET_FS_remove_sync_file_ (sc->h,
+                                  GNUNET_FS_SYNC_PATH_FILE_INFO,
+                                  fi->serialization);
       GNUNET_free (fi->serialization);
       fi->serialization = NULL;
     }
@@ -1143,6 +1213,11 @@ void
 GNUNET_FS_publish_stop (struct GNUNET_FS_PublishContext *pc)
 {
   GNUNET_FS_end_top (pc->h, pc->top);
+  if (NULL != pc->dsh)
+    {
+      GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
+      pc->dsh = NULL;
+    }
   if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
     {
       GNUNET_SCHEDULER_cancel (pc->h->sched, pc->upload_task);
@@ -1162,7 +1237,7 @@ GNUNET_FS_publish_stop (struct GNUNET_FS_PublishContext *pc)
       pc->in_network_wait = GNUNET_SYSERR;
       return;
     }
-  publish_cleanup (pc);
+  publish_cleanup (pc, NULL);
 }
 
 
@@ -1273,7 +1348,11 @@ kb_put_cont (void *cls,
 
   if (GNUNET_OK != success)
     {
-      GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
+      if (NULL != pkc->dsh)
+       {
+         GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
+         pkc->dsh = NULL;
+       }
       GNUNET_free (pkc->cpy);
       GNUNET_free (pkc->kb);
       pkc->cont (pkc->cont_cls,
@@ -1314,7 +1393,10 @@ publish_ksk_cont (void *cls,
        (NULL == pkc->dsh) )
     {
       if (NULL != pkc->dsh)
-       GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
+       {
+         GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
+         pkc->dsh = NULL;
+       }
       GNUNET_free (pkc->cpy);
       GNUNET_free (pkc->kb);
       pkc->cont (pkc->cont_cls,
@@ -1335,6 +1417,7 @@ publish_ksk_cont (void *cls,
                             &iv,
                             &pkc->cpy[1]);
   pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&key);
+  GNUNET_assert (NULL != pk);
   GNUNET_CRYPTO_rsa_key_get_public (pk, &pkc->cpy->keyspace);
   GNUNET_CRYPTO_hash (&pkc->cpy->keyspace,
                      sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
@@ -1355,6 +1438,7 @@ publish_ksk_cont (void *cls,
                        pkc->priority,
                        pkc->anonymity,
                        pkc->expirationTime,
+                       -2, 1,
                        GNUNET_CONSTANTS_SERVICE_TIMEOUT,
                        &kb_put_cont,
                        pkc);
@@ -1439,7 +1523,10 @@ GNUNET_FS_publish_ksk (struct GNUNET_FS_Handle *h,
       GNUNET_break (0);
       GNUNET_free (pkc->kb);
       if (pkc->dsh != NULL)
-       GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
+       {
+         GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
+         pkc->dsh = NULL;
+       }
       cont (cont_cls, NULL, _("Internal error."));
       GNUNET_free (pkc);
       return;
@@ -1505,7 +1592,10 @@ sb_put_cont (void *cls,
   struct PublishSksContext *psc = cls;
 
   if (NULL != psc->dsh)
-    GNUNET_DATASTORE_disconnect (psc->dsh, GNUNET_NO);
+    {
+      GNUNET_DATASTORE_disconnect (psc->dsh, GNUNET_NO);
+      psc->dsh = NULL;
+    }
   if (GNUNET_OK != success)
     psc->cont (psc->cont_cls,
               NULL,
@@ -1589,6 +1679,7 @@ GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
   memcpy (dest, update, nidlen);
   dest += nidlen;
   memcpy (dest, uris, slen);
+  GNUNET_free (uris);
   dest += slen;
   mdsize = GNUNET_CONTAINER_meta_data_serialize (mmeta,
                                                 &dest,
@@ -1598,7 +1689,6 @@ GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
   if (mdsize == -1)
     {
       GNUNET_break (0);
-      GNUNET_free (uris);
       GNUNET_free (sb);
       cont (cont_cls,
            NULL,
@@ -1668,10 +1758,10 @@ GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
                        priority,
                        anonymity,
                        expirationTime,
+                       -2, 1,
                        GNUNET_CONSTANTS_SERVICE_TIMEOUT,
                        &sb_put_cont,
                        psc);
-
   GNUNET_free (sb);
   GNUNET_free (sb_enc);
 }