-help seaspider some more
[oweals/gnunet.git] / src / fs / gnunet-publish.c
index 9e060286288a4c557703028b512047722a3b9303..a1b26dbd5845da11eae06fee745c23cdd009a762 100644 (file)
@@ -68,12 +68,8 @@ static GNUNET_SCHEDULER_TaskIdentifier kill_task;
 
 static struct GNUNET_FS_DirScanner *ds;
 
-static struct GNUNET_FS_ShareTreeItem * directory_scan_intermediary_result;
-
 static struct GNUNET_FS_ShareTreeItem * directory_scan_result;
 
-static struct GNUNET_FS_ProcessMetadataContext *pmc;
-
 static struct GNUNET_FS_Namespace *namespace;
 
 /**
@@ -84,17 +80,41 @@ do_stop_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct GNUNET_FS_PublishContext *p;
 
+  kill_task = GNUNET_SCHEDULER_NO_TASK;
   if (pc != NULL)
   {
     p = pc;
     pc = NULL;
     GNUNET_FS_publish_stop (p);
-    if (NULL != meta)
-    {
-      GNUNET_CONTAINER_meta_data_destroy (meta);
-      meta = NULL;
-    }
   }
+  if (NULL != meta)
+  {
+    GNUNET_CONTAINER_meta_data_destroy (meta);
+    meta = NULL;
+  }  
+}
+
+
+/**
+ * Stop the directory scanner (we had an error).
+ *
+ * @param cls closure
+ * @param tc scheduler context
+ */
+static void
+stop_scanner_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  kill_task = GNUNET_SCHEDULER_NO_TASK;
+  GNUNET_FS_directory_scan_abort (ds);
+  ds = NULL;
+  if (namespace != NULL)
+  {
+    GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
+    namespace = NULL;
+  }
+  GNUNET_FS_stop (ctx);
+  ctx = NULL;
+  ret = 1;
 }
 
 
@@ -139,8 +159,7 @@ progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
       GNUNET_SCHEDULER_cancel (kill_task);
       kill_task = GNUNET_SCHEDULER_NO_TASK;
     }
-    GNUNET_SCHEDULER_add_continuation (&do_stop_task, NULL,
-                                       GNUNET_SCHEDULER_REASON_PREREQ_DONE);
+    kill_task = GNUNET_SCHEDULER_add_now (&do_stop_task, NULL);
     break;
   case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
     FPRINTF (stdout, _("Publishing `%s' done.\n"),
@@ -156,8 +175,7 @@ progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
         GNUNET_SCHEDULER_cancel (kill_task);
         kill_task = GNUNET_SCHEDULER_NO_TASK;
       }
-      GNUNET_SCHEDULER_add_continuation (&do_stop_task, NULL,
-                                         GNUNET_SCHEDULER_REASON_PREREQ_DONE);
+      kill_task = GNUNET_SCHEDULER_add_now (&do_stop_task, NULL);
     }
     break;
   case GNUNET_FS_STATUS_PUBLISH_STOPPED:
@@ -273,6 +291,12 @@ publish_inspector (void *cls, struct GNUNET_FS_FileInformation *fi,
   }
   if (!do_disable_creation_time)
     GNUNET_CONTAINER_meta_data_add_publication_date (m);
+  if ( (disable_extractor) &&
+       (NULL != *uri) )
+  {
+    GNUNET_FS_uri_destroy (*uri);
+    *uri = NULL;
+  }
   if (extract_only)
   {
     fn = GNUNET_CONTAINER_meta_data_get_by_type (m,
@@ -338,7 +362,7 @@ uri_ksk_continuation (void *cls, const struct GNUNET_FS_Uri *ksk_uri,
     {
       GNUNET_FS_publish_sks (ctx, ns, this_id, next_id, meta, uri, &bo,
                              GNUNET_FS_PUBLISH_OPTION_NONE,
-                             uri_sks_continuation, NULL);
+                             &uri_sks_continuation, NULL);
       GNUNET_assert (GNUNET_OK == GNUNET_FS_namespace_delete (ns, GNUNET_NO));
       return;
     }
@@ -349,6 +373,10 @@ uri_ksk_continuation (void *cls, const struct GNUNET_FS_Uri *ksk_uri,
   ctx = NULL;
 }
 
+
+/**
+ * FIXME: docu
+ */
 static struct GNUNET_FS_FileInformation *
 get_file_information (struct GNUNET_FS_ShareTreeItem *item)
 {
@@ -356,12 +384,18 @@ get_file_information (struct GNUNET_FS_ShareTreeItem *item)
   struct GNUNET_FS_FileInformation *fic;
   struct GNUNET_FS_ShareTreeItem *child;
 
-  if (item->is_directory)
+  if (item->is_directory == GNUNET_YES)
   {
     GNUNET_CONTAINER_meta_data_delete (item->meta,
         EXTRACTOR_METATYPE_MIMETYPE, NULL, 0);
     GNUNET_FS_meta_data_make_directory (item->meta);
-    GNUNET_FS_uri_ksk_add_keyword (item->ksk_uri, GNUNET_FS_DIRECTORY_MIME,
+    if (NULL == item->ksk_uri)
+    {
+      const char *mime = GNUNET_FS_DIRECTORY_MIME;
+      item->ksk_uri = GNUNET_FS_uri_ksk_create_from_args (1, &mime);
+    }
+    else
+      GNUNET_FS_uri_ksk_add_keyword (item->ksk_uri, GNUNET_FS_DIRECTORY_MIME,
         GNUNET_NO);
     fi = GNUNET_FS_file_information_create_empty_directory (
         ctx, NULL, item->ksk_uri,
@@ -379,21 +413,20 @@ get_file_information (struct GNUNET_FS_ShareTreeItem *item)
         item->ksk_uri, item->meta, !do_insert,
         &bo);
   }
-  GNUNET_CONTAINER_meta_data_destroy (item->meta);
-  GNUNET_FS_uri_destroy (item->ksk_uri);
-  GNUNET_free (item->short_filename);
-  GNUNET_free (item->filename);
-  GNUNET_free (item);
   return fi;
 }
 
+
+/**
+ * FIXME: docu
+ */
 static void
-directory_trim_complete (void *cls,
-    const struct GNUNET_SCHEDULER_TaskContext *tc)
+directory_trim_complete ()
 {
   struct GNUNET_FS_FileInformation *fi;
-  directory_scan_result = directory_scan_intermediary_result;
+
   fi = get_file_information (directory_scan_result);
+  GNUNET_FS_share_tree_free (directory_scan_result);
   directory_scan_result = NULL;
   if (fi == NULL)
   {
@@ -411,6 +444,11 @@ directory_trim_complete (void *cls,
       GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
     GNUNET_FS_file_information_destroy (fi, NULL, NULL);
     GNUNET_FS_stop (ctx);
+    if (kill_task != GNUNET_SCHEDULER_NO_TASK)
+    {
+      GNUNET_SCHEDULER_cancel (kill_task);
+      kill_task = GNUNET_SCHEDULER_NO_TASK;
+    }
     return;
   }
   pc = GNUNET_FS_publish_start (ctx, fi, namespace, this_id, next_id,
@@ -426,71 +464,70 @@ directory_trim_complete (void *cls,
   }
 }
 
-static int
-directory_scan_cb (void *cls, struct GNUNET_FS_DirScanner *ds,
-    const char *filename, char is_directory,
-    enum GNUNET_FS_DirScannerProgressUpdateReason reason)
+
+/**
+ * Function called by the directory scanner as we build the tree
+ * that we will need to publish later.
+ *
+ * @param cls closure
+ * @param filename which file we are making progress on
+ * @param is_directory GNUNET_YES if this is a directory,
+ *                     GNUNET_NO if this is a file
+ *                     GNUNET_SYSERR if it is neither (or unknown)
+ * @param reason kind of progress we are making
+ */
+static void
+directory_scan_cb (void *cls, 
+                  const char *filename, 
+                  int is_directory,
+                  enum GNUNET_FS_DirScannerProgressUpdateReason reason)
 {
   switch (reason)
   {
-    case GNUNET_DIR_SCANNER_NEW_FILE:
-      if (filename != NULL)
-      {
-        if (is_directory)
-          FPRINTF (stdout, _("Scanning directory `%s'.\n"), filename);
-        else
-          FPRINTF (stdout, _("Scanning file `%s'.\n"), filename);
-      }
-      break;
-    case GNUNET_DIR_SCANNER_DOES_NOT_EXIST:
-      if (filename != NULL)
-      {
-        FPRINTF (stdout, 
-            _("Failed to scan `%s', because it does not exist.\n"),
-            filename);
-      }
-      break;
-    case GNUNET_DIR_SCANNER_ASKED_TO_STOP:
-      if (filename != NULL)
-      {
-        FPRINTF (stdout, 
-            _("Scanner was about to scan `%s', but is now stopping.\n"),
-            filename);
-      }
+  case GNUNET_FS_DIRSCANNER_FILE_START:
+    if (verbose > 1)
+    {
+      if (is_directory == GNUNET_YES)
+       FPRINTF (stdout, _("Scanning directory `%s'.\n"), filename);
       else
-        FPRINTF (stdout, "%s", _("Scanner is stopping.\n"));
-      break;
-    case GNUNET_DIR_SCANNER_SHUTDOWN:
-      FPRINTF (stdout, "%s", _("Client is shutting down.\n"));
-      break;
-    case GNUNET_DIR_SCANNER_FINISHED:
-      FPRINTF (stdout, "%s", _("Scanner has finished.\n"));
-      break;
-    case GNUNET_DIR_SCANNER_PROTOCOL_ERROR:
-      FPRINTF (stdout, "%s", 
-          _("There was a failure communicating with the scanner.\n"));
-      break;
-    default:
-      FPRINTF (stdout, _("Got unknown scanner update with filename `%s'.\n"),
-          filename);
-      break;
-  }
-  if ((filename == NULL && GNUNET_DIR_SCANNER_FINISHED)
-      || reason == GNUNET_DIR_SCANNER_PROTOCOL_ERROR
-      || reason == GNUNET_DIR_SCANNER_SHUTDOWN)
-  {
-    /* Any of this causes us to try to clean up the scanner */
-    directory_scan_intermediary_result = GNUNET_FS_directory_scan_cleanup (ds);
-    pmc = GNUNET_FS_trim_share_tree (directory_scan_intermediary_result,
-      &directory_trim_complete, NULL);
-
+       FPRINTF (stdout, _("Scanning file `%s'.\n"), filename);      
+    }
+    break;
+  case GNUNET_FS_DIRSCANNER_FILE_IGNORED:
+    FPRINTF (stderr, 
+            _("There was trouble processing file `%s', skipping it.\n"),
+            filename);
+    break;
+  case GNUNET_FS_DIRSCANNER_ALL_COUNTED:
+    if (verbose)
+      FPRINTF (stdout, "%s", _("Preprocessing complete.\n"));      
+    break;
+  case GNUNET_FS_DIRSCANNER_EXTRACT_FINISHED:
+    if (verbose > 2)
+      FPRINTF (stdout, _("Extracting meta data from file `%s' complete.\n"), filename);      
+    break;
+  case GNUNET_FS_DIRSCANNER_FINISHED:
+    if (verbose > 1)
+      FPRINTF (stdout, "%s", _("Meta data extraction has finished.\n"));
+    directory_scan_result = GNUNET_FS_directory_scan_get_result (ds);
     ds = NULL;
-    /* FIXME: change the tree processor to be able to free untrimmed trees
-     * right here instead of waiting for trimming to complete, if we need to
-     * cancel everything.
-     */
+    GNUNET_FS_share_tree_trim (directory_scan_result);
+    directory_trim_complete ();
+    break;
+  case GNUNET_FS_DIRSCANNER_INTERNAL_ERROR:
+    FPRINTF (stdout, "%s", _("Internal error scanning directory.\n"));
+    if (kill_task != GNUNET_SCHEDULER_NO_TASK)
+    {
+      GNUNET_SCHEDULER_cancel (kill_task);
+      kill_task = GNUNET_SCHEDULER_NO_TASK;
+    }
+    kill_task = GNUNET_SCHEDULER_add_now (&stop_scanner_task, NULL);
+    break;
+  default:
+    GNUNET_assert (0);
+    break;
   }
-  return 0;
+  fflush (stdout);
 }
 
 
@@ -506,7 +543,6 @@ static void
 run (void *cls, char *const *args, const char *cfgfile,
      const struct GNUNET_CONFIGURATION_Handle *c)
 {
-  struct EXTRACTOR_PluginList *plugins;
   char *ex;
   char *emsg;
 
@@ -600,22 +636,26 @@ run (void *cls, char *const *args, const char *cfgfile,
       GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
     return;
   }
+  if (GNUNET_OK !=
+      GNUNET_CONFIGURATION_get_value_string (cfg, "FS", "EXTRACTORS", &ex))
+    ex = NULL;
+  if (0 != ACCESS (args[0], R_OK))
+  {
+    FPRINTF (stderr,
+            _("Failed to access `%s': %s\n"),
+            args[0],
+            STRERROR (errno));
+    return;
+  }
   ds = GNUNET_FS_directory_scan_start (args[0],
-      GNUNET_NO, NULL, directory_scan_cb, NULL);
-  plugins = NULL;
-  if (!disable_extractor)
+                                      disable_extractor, 
+                                      ex, 
+                                      &directory_scan_cb, NULL);
+  if (NULL == ds)
   {
-    plugins = EXTRACTOR_plugin_add_defaults (EXTRACTOR_OPTION_DEFAULT_POLICY);
-    if (GNUNET_OK ==
-        GNUNET_CONFIGURATION_get_value_string (cfg, "FS", "EXTRACTORS", &ex))
-    {
-      if (strlen (ex) > 0)
-        plugins =
-            EXTRACTOR_plugin_add_config (plugins, ex,
-                                         EXTRACTOR_OPTION_DEFAULT_POLICY);
-      GNUNET_free (ex);
-    }
+    FPRINTF (stderr,
+            "%s", _("Failed to start meta directory scanner.  Is gnunet-helper-publish-fs installed?\n"));
+    return;
   }
   kill_task =
       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_stop_task,
@@ -692,6 +732,8 @@ main (int argc, char *const *argv)
      0, &GNUNET_GETOPT_set_one, &verbose},
     GNUNET_GETOPT_OPTION_END
   };
+  GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+             "GNUnet publish starts\n");
   bo.expiration_time =
       GNUNET_FS_year_to_time (GNUNET_FS_get_current_year () + 2);
   return (GNUNET_OK ==