-fix #2598
[oweals/gnunet.git] / src / fs / gnunet-publish.c
index 406433a71295b56b12e12af7d6e347710aa0c6e8..eb64cc4cc32c4bb6b3cdb4689fe457dd842ec9d0 100644 (file)
@@ -28,7 +28,7 @@
 #include "platform.h"
 #include "gnunet_fs_service.h"
 
-static int ret;
+static int ret = 1;
 
 static int verbose;
 
@@ -95,6 +95,29 @@ do_stop_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 }
 
 
+/**
+ * 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;
+}
+
+
 /**
  * Called by FS client to give information about the progress of an
  * operation.
@@ -111,7 +134,8 @@ do_stop_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 static void *
 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
 {
-  char *s;
+  const char *s;
+  char *suri;
 
   switch (info->status)
   {
@@ -120,12 +144,12 @@ progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
   case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
     if (verbose)
     {
-      s = GNUNET_STRINGS_relative_time_to_string (info->value.publish.eta);
+      s = GNUNET_STRINGS_relative_time_to_string (info->value.publish.eta,
+                                                 GNUNET_YES);
       FPRINTF (stdout, _("Publishing `%s' at %llu/%llu (%s remaining)\n"),
                info->value.publish.filename,
                (unsigned long long) info->value.publish.completed,
                (unsigned long long) info->value.publish.size, s);
-      GNUNET_free (s);
     }
     break;
   case GNUNET_FS_STATUS_PUBLISH_ERROR:
@@ -141,10 +165,10 @@ progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
   case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
     FPRINTF (stdout, _("Publishing `%s' done.\n"),
              info->value.publish.filename);
-    s = GNUNET_FS_uri_to_string (info->value.publish.specifics.
+    suri = GNUNET_FS_uri_to_string (info->value.publish.specifics.
                                  completed.chk_uri);
-    FPRINTF (stdout, _("URI is `%s'.\n"), s);
-    GNUNET_free (s);
+    FPRINTF (stdout, _("URI is `%s'.\n"), suri);
+    GNUNET_free (suri);
     if (info->value.publish.pctx == NULL)
     {
       if (kill_task != GNUNET_SCHEDULER_NO_TASK)
@@ -154,6 +178,7 @@ progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
       }
       kill_task = GNUNET_SCHEDULER_add_now (&do_stop_task, NULL);
     }
+    ret = 0;
     break;
   case GNUNET_FS_STATUS_PUBLISH_STOPPED:
     GNUNET_break (NULL == pc);
@@ -268,6 +293,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,
@@ -326,14 +357,14 @@ uri_ksk_continuation (void *cls, const struct GNUNET_FS_Uri *ksk_uri,
     ns = GNUNET_FS_namespace_create (ctx, pseudonym);
     if (ns == NULL)
     {
-      FPRINTF (stderr, _("Failed to create namespace `%s'\n"), pseudonym);
+      FPRINTF (stderr, _("Failed to create namespace `%s' (illegal filename?)\n"), pseudonym);
       ret = 1;
     }
     else
     {
       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;
     }
@@ -355,7 +386,7 @@ 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);
@@ -458,7 +489,7 @@ directory_scan_cb (void *cls,
   case GNUNET_FS_DIRSCANNER_FILE_START:
     if (verbose > 1)
     {
-      if (is_directory)
+      if (is_directory == GNUNET_YES)
        FPRINTF (stdout, _("Scanning directory `%s'.\n"), filename);
       else
        FPRINTF (stdout, _("Scanning file `%s'.\n"), filename);      
@@ -487,17 +518,12 @@ directory_scan_cb (void *cls,
     break;
   case GNUNET_FS_DIRSCANNER_INTERNAL_ERROR:
     FPRINTF (stdout, "%s", _("Internal error scanning directory.\n"));
-    GNUNET_FS_directory_scan_abort (ds);
-    ds = NULL;
-    if (namespace != NULL)
-      GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
-    GNUNET_FS_stop (ctx);
     if (kill_task != GNUNET_SCHEDULER_NO_TASK)
     {
       GNUNET_SCHEDULER_cancel (kill_task);
       kill_task = GNUNET_SCHEDULER_NO_TASK;
     }
-    ret = 1;
+    kill_task = GNUNET_SCHEDULER_add_now (&stop_scanner_task, NULL);
     break;
   default:
     GNUNET_assert (0);
@@ -585,7 +611,7 @@ run (void *cls, char *const *args, const char *cfgfile,
     namespace = GNUNET_FS_namespace_create (ctx, pseudonym);
     if (NULL == namespace)
     {
-      FPRINTF (stderr, _("Could not create namespace `%s'\n"), pseudonym);
+      FPRINTF (stderr, _("Failed to create namespace `%s' (illegal filename?)\n"), pseudonym);
       GNUNET_FS_stop (ctx);
       ret = 1;
       return;
@@ -708,10 +734,14 @@ main (int argc, char *const *argv)
      0, &GNUNET_GETOPT_set_one, &verbose},
     GNUNET_GETOPT_OPTION_END
   };
-  GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "GNUnet publish starts\n");
   bo.expiration_time =
       GNUNET_FS_year_to_time (GNUNET_FS_get_current_year () + 2);
+
+  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
+    return 2;
+
   return (GNUNET_OK ==
           GNUNET_PROGRAM_run (argc, argv, "gnunet-publish [OPTIONS] FILENAME",
                               gettext_noop