fix
[oweals/gnunet.git] / src / fs / gnunet-publish.c
index 70964590d69bc38eb8db016c3269ee3099f73e27..5869fcff5ffad8d815300b2898b9c45f38312bda 100644 (file)
@@ -46,6 +46,8 @@ static struct GNUNET_CONTAINER_MetaData *meta;
 
 static struct GNUNET_FS_Uri *topKeywords;
 
+static struct GNUNET_FS_Uri *uri;
+
 static unsigned int anonymity = 1;
 
 static unsigned int priority = 365;
@@ -82,6 +84,11 @@ do_stop_task (void *cls,
       p = pc;
       pc = NULL;
       GNUNET_FS_publish_stop (p);
+      if (NULL != meta) 
+       {
+         GNUNET_CONTAINER_meta_data_destroy (meta);
+         meta = NULL;
+       }
     }
 }
 
@@ -208,40 +215,6 @@ meta_printer (void *cls,
 }
 
 
-/**
- * Merge metadata entries.
- *
- * @param cls closure, target metadata structure
- * @param plugin_name name of the plugin that generated the meta data
- * @param type type of the meta data
- * @param format format of data
- * @param data_mime_type mime type of data
- * @param data value of the meta data
- * @param data_size number of bytes in data
- * @return always 0
- */
-static int
-meta_merger (void *cls,
-             const char *plugin_name,
-             enum EXTRACTOR_MetaType type, 
-             enum EXTRACTOR_MetaFormat format,
-             const char *data_mime_type,
-             const char *data,
-             size_t data_size)
-{
-  struct GNUNET_CONTAINER_MetaData *m = cls;
-
-  GNUNET_CONTAINER_meta_data_insert (m,
-                                    plugin_name,
-                                    type, 
-                                    format,
-                                    data_mime_type,
-                                    data,
-                                    data_size);
-  return 0;
-}
-
-
 /**
  * Function called on all entries before the publication.  This is
  * where we perform modifications to the default based on command-line
@@ -275,6 +248,8 @@ publish_inspector (void *cls,
   char *fs;
   struct GNUNET_FS_Uri *new_uri;
 
+  if (cls == fi)
+    return GNUNET_OK;
   if (NULL != topKeywords)
     {
       if (*uri != NULL)
@@ -293,9 +268,7 @@ publish_inspector (void *cls,
     }
   if (NULL != meta) 
     {
-      GNUNET_CONTAINER_meta_data_iterate (meta,
-                                         &meta_merger,
-                                         m);
+      GNUNET_CONTAINER_meta_data_merge (m, meta);
       GNUNET_CONTAINER_meta_data_destroy (meta);
       meta = NULL;
     }
@@ -320,16 +293,37 @@ publish_inspector (void *cls,
   if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (m))
     GNUNET_FS_file_information_inspect (fi,
                                        &publish_inspector,
-                                       NULL);
+                                       fi);
   return GNUNET_OK;
 }
 
 
+static void 
+uri_sks_continuation (void *cls,
+                     const struct GNUNET_FS_Uri *ksk_uri,
+                     const char *emsg)
+{
+  if (emsg != NULL)
+    {
+      fprintf (stderr,
+              "%s\n",
+              emsg);
+      ret = 1;
+    }
+  GNUNET_FS_uri_destroy (uri);
+  uri = NULL;
+  GNUNET_FS_stop (ctx);
+  ctx = NULL;
+}
+
+
 static void 
 uri_ksk_continuation (void *cls,
-                     const struct GNUNET_FS_Uri *uri,
+                     const struct GNUNET_FS_Uri *ksk_uri,
                      const char *emsg)
 {
+  struct GNUNET_FS_Namespace *ns;
+
   if (emsg != NULL)
     {
       fprintf (stderr,
@@ -337,7 +331,40 @@ uri_ksk_continuation (void *cls,
               emsg);
       ret = 1;
     }
+  if (pseudonym != NULL)
+    {
+      ns = GNUNET_FS_namespace_create (ctx,
+                                      pseudonym);
+      if (ns == NULL)
+       {
+         fprintf (stderr,
+                  _("Failed to create namespace `%s'\n"),
+                  pseudonym);
+         ret = 1;
+       }
+      else
+       {
+         GNUNET_FS_publish_sks (ctx,
+                                ns,
+                                this_id,
+                                next_id,
+                                meta,
+                                uri,
+                                GNUNET_TIME_relative_to_absolute (DEFAULT_EXPIRATION),
+                                anonymity,
+                                priority,
+                                GNUNET_FS_PUBLISH_OPTION_NONE,
+                                uri_sks_continuation,
+                                NULL);
+         GNUNET_assert (GNUNET_OK ==
+                        GNUNET_FS_namespace_delete (ns, GNUNET_NO));
+         return;
+       }
+    }
+  GNUNET_FS_uri_destroy (uri);
+  uri = NULL;
   GNUNET_FS_stop (ctx);
+  ctx = NULL;
 }
 
 
@@ -363,7 +390,6 @@ run (void *cls,
   struct stat sbuf;
   char *ex;
   char *emsg;
-  struct GNUNET_FS_Uri *uri;
   
   sched = s;
   /* check arguments */
@@ -417,7 +443,8 @@ run (void *cls,
          return;
         }
     }
-  if (args[0] == NULL)
+  if ( (args[0] == NULL) &&
+       (uri_string == NULL) )
     {
       fprintf (stderr,
               _("Need the name of a file to publish!\n"));
@@ -505,7 +532,8 @@ run (void *cls,
     }
   else if (S_ISDIR (sbuf.st_mode))
     {
-      fi = GNUNET_FS_file_information_create_from_directory (NULL,
+      fi = GNUNET_FS_file_information_create_from_directory (ctx,
+                                                            NULL,
                                                             args[0],
                                                             &GNUNET_FS_directory_scanner_default,
                                                             l,
@@ -517,7 +545,8 @@ run (void *cls,
     }
   else
     {
-      fi = GNUNET_FS_file_information_create_from_file (NULL,
+      fi = GNUNET_FS_file_information_create_from_file (ctx,
+                                                       NULL,
                                                        args[0],
                                                        NULL,
                                                        NULL,
@@ -599,7 +628,6 @@ static struct GNUNET_GETOPT_CommandLineOption options[] = {
    ("add an additional keyword for the top-level file or directory"
     " (this option can be specified multiple times)"),
    1, &GNUNET_FS_getopt_set_keywords, &topKeywords},
-  // *: option not yet used... (can handle in a pass over FI)
   {'m', "meta", "TYPE:VALUE",
    gettext_noop ("set the meta-data for the given TYPE to the given VALUE"),
    1, &GNUNET_FS_getopt_set_metadata, &meta},
@@ -619,7 +647,6 @@ static struct GNUNET_GETOPT_CommandLineOption options[] = {
    gettext_noop
    ("publish the files under the pseudonym NAME (place file into namespace)"),
    1, &GNUNET_GETOPT_set_string, &pseudonym},
-  // *: option not yet used... (need FS API support!)
   {'s', "simulate-only", NULL,
    gettext_noop ("only simulate the process but do not do any "
                  "actual publishing (useful to compute URIs)"),
@@ -628,7 +655,6 @@ static struct GNUNET_GETOPT_CommandLineOption options[] = {
    gettext_noop ("set the ID of this version of the publication"
                  " (for namespace insertions only)"),
    1, &GNUNET_GETOPT_set_string, &this_id},
-  // *: option not yet used... (need FS API support!)
   {'u', "uri", "URI",
    gettext_noop ("URI to be published (can be used instead of passing a "
                  "file to add keywords to the file with the respective URI)"),
@@ -660,86 +686,3 @@ main (int argc, char *const *argv)
 }
 
 /* end of gnunet-publish.c */
-
-////////////////////////////////////////////////////////////////
-
-#if 0
-/**
- * Print progess message.
- */
-static void *
-printstatus (void *ctx, const GNUNET_FSUI_Event * event)
-{
-  unsigned long long delta;
-  char *fstring;
-
-  switch (event->type)
-    {
-    case GNUNET_FSUI_upload_progress:
-      if (*verboselevel)
-        {
-          char *ret;
-          GNUNET_CronTime now;
-
-          now = GNUNET_get_time ();
-          delta = event->data.UploadProgress.eta - now;
-          if (event->data.UploadProgress.eta < now)
-            delta = 0;
-          ret = GNUNET_get_time_interval_as_fancy_string (delta);
-          PRINTF (_("%16llu of %16llu bytes inserted "
-                    "(estimating %6s to completion) - %s\n"),
-                  event->data.UploadProgress.completed,
-                  event->data.UploadProgress.total,
-                  ret, event->data.UploadProgress.filename);
-          GNUNET_free (ret);
-        }
-      break;
-    case GNUNET_FSUI_upload_completed:
-      if (*verboselevel)
-        {
-          delta = GNUNET_get_time () - start_time;
-          PRINTF (_("Upload of `%s' complete, "
-                    "%llu bytes took %llu seconds (%8.3f KiB/s).\n"),
-                  event->data.UploadCompleted.filename,
-                  event->data.UploadCompleted.total,
-                  delta / GNUNET_CRON_SECONDS,
-                  (delta == 0)
-                  ? (double) (-1.0)
-                  : (double) (event->data.UploadCompleted.total
-                              / 1024.0 * GNUNET_CRON_SECONDS / delta));
-        }
-      fstring = GNUNET_ECRS_uri_to_string (event->data.UploadCompleted.uri);
-      printf (_("File `%s' has URI: %s\n"),
-              event->data.UploadCompleted.filename, fstring);
-      GNUNET_free (fstring);
-      if (ul == event->data.UploadCompleted.uc.pos)
-        {
-          postProcess (event->data.UploadCompleted.uri);
-          errorCode = 0;
-          GNUNET_shutdown_initiate ();
-        }
-      break;
-    case GNUNET_FSUI_upload_aborted:
-      printf (_("\nUpload aborted.\n"));
-      errorCode = 2;
-      GNUNET_shutdown_initiate ();
-      break;
-    case GNUNET_FSUI_upload_error:
-      printf (_("\nError uploading file: %s"),
-              event->data.UploadError.message);
-      errorCode = 3;
-      GNUNET_shutdown_initiate ();
-      break;
-    case GNUNET_FSUI_upload_started:
-    case GNUNET_FSUI_upload_stopped:
-      break;
-    default:
-      printf (_("\nUnexpected event: %d\n"), event->type);
-      GNUNET_GE_BREAK (ectx, 0);
-      break;
-    }
-  return NULL;
-}
-#endif
-
-/* end of gnunet-publish.c */