+/**
+ * Function called upon completion of the publishing
+ * of the UBLOCK for the SKS URI. As this is the last
+ * step, stop our interaction with FS (clean up).
+ *
+ * @param cls NULL (closure)
+ * @param sks_uri URI for the block that was published
+ * @param emsg error message, NULL on success
+ */
+static void
+uri_sks_continuation (void *cls,
+ const struct GNUNET_FS_Uri *sks_uri,
+ const char *emsg)
+{
+ if (NULL != emsg)
+ {
+ FPRINTF (stderr,
+ "%s\n",
+ emsg);
+ ret = 1;
+ }
+ GNUNET_SCHEDULER_shutdown ();
+}
+
+
+/**
+ * Function called upon completion of the publishing
+ * of the UBLOCK for the KSK URI. Continue with
+ * publishing the SKS URI (if applicable) or clean up.
+ *
+ * @param cls NULL (closure)
+ * @param ksk_uri URI for the block that was published
+ * @param emsg error message, NULL on success
+ */
+static void
+uri_ksk_continuation (void *cls,
+ const struct GNUNET_FS_Uri *ksk_uri,
+ const char *emsg)
+{
+ const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv;
+
+ if (NULL != emsg)
+ {
+ FPRINTF (stderr,
+ "%s\n",
+ emsg);
+ ret = 1;
+ }
+ if (NULL != namespace)
+ {
+ priv = GNUNET_IDENTITY_ego_get_private_key (namespace);
+ GNUNET_FS_publish_sks (ctx,
+ priv,
+ this_id,
+ next_id,
+ meta,
+ uri,
+ &bo,
+ GNUNET_FS_PUBLISH_OPTION_NONE,
+ &uri_sks_continuation, NULL);
+ return;
+ }
+ GNUNET_SCHEDULER_shutdown ();
+}
+
+
+/**
+ * Iterate over the results from the directory scan and extract
+ * the desired information for the publishing operation.
+ *
+ * @param item root with the data from the directroy scan
+ * @return handle with the information for the publishing operation
+ */
+static struct GNUNET_FS_FileInformation *
+get_file_information (struct GNUNET_FS_ShareTreeItem *item)
+{
+ struct GNUNET_FS_FileInformation *fi;
+ struct GNUNET_FS_FileInformation *fic;
+ struct GNUNET_FS_ShareTreeItem *child;
+
+ if (GNUNET_YES == item->is_directory)
+ {
+ if (NULL == item->meta)
+ item->meta = GNUNET_CONTAINER_meta_data_create ();
+ GNUNET_CONTAINER_meta_data_delete (item->meta,
+ EXTRACTOR_METATYPE_MIMETYPE,
+ NULL, 0);
+ GNUNET_FS_meta_data_make_directory (item->meta);
+ 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,
+ item->meta,
+ &bo, item->filename);
+ for (child = item->children_head; child; child = child->next)
+ {
+ fic = get_file_information (child);
+ GNUNET_break (GNUNET_OK == GNUNET_FS_file_information_add (fi, fic));
+ }
+ }
+ else
+ {
+ fi = GNUNET_FS_file_information_create_from_file (ctx, NULL,
+ item->filename,
+ item->ksk_uri, item->meta,
+ !do_insert,
+ &bo);
+ }
+ return fi;
+}
+
+
+/**
+ * We've finished scanning the directory and optimized the meta data.
+ * Begin the publication process.
+ *
+ * @param directory_scan_result result from the directory scan, freed in this function
+ */
+static void
+directory_trim_complete (struct GNUNET_FS_ShareTreeItem *directory_scan_result)
+{
+ struct GNUNET_FS_FileInformation *fi;
+ const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv;
+
+ fi = get_file_information (directory_scan_result);
+ GNUNET_FS_share_tree_free (directory_scan_result);
+ if (NULL == fi)
+ {
+ FPRINTF (stderr,
+ "%s",
+ _("Could not publish\n"));
+ ret = 1;
+ GNUNET_SCHEDULER_shutdown ();
+ return;
+ }
+ GNUNET_FS_file_information_inspect (fi, &publish_inspector, NULL);
+ if (extract_only)
+ {
+ GNUNET_FS_file_information_destroy (fi, NULL, NULL);
+ GNUNET_SCHEDULER_shutdown ();
+ return;
+ }
+ if (NULL == namespace)
+ priv = NULL;
+ else
+ priv = GNUNET_IDENTITY_ego_get_private_key (namespace);
+ pc = GNUNET_FS_publish_start (ctx, fi,
+ priv, this_id, next_id,
+ (do_simulate) ?
+ GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY :
+ GNUNET_FS_PUBLISH_OPTION_NONE);
+ if (NULL == pc)
+ {
+ FPRINTF (stderr,
+ "%s",
+ _("Could not start publishing.\n"));
+ ret = 1;
+ GNUNET_SCHEDULER_shutdown ();
+ return;
+ }
+}
+
+
+/**
+ * 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)
+{
+ struct GNUNET_FS_ShareTreeItem *directory_scan_result;
+
+ switch (reason)
+ {
+ case GNUNET_FS_DIRSCANNER_FILE_START:
+ if (verbose > 1)
+ {
+ if (is_directory == GNUNET_YES)
+ FPRINTF (stdout,
+ _("Scanning directory `%s'.\n"),
+ filename);
+ else
+ 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;
+ GNUNET_FS_share_tree_trim (directory_scan_result);
+ directory_trim_complete (directory_scan_result);
+ break;
+ case GNUNET_FS_DIRSCANNER_INTERNAL_ERROR:
+ FPRINTF (stdout,
+ "%s",
+ _("Internal error scanning directory.\n"));
+ ret = 1;
+ GNUNET_SCHEDULER_shutdown ();
+ break;
+ default:
+ GNUNET_assert (0);
+ break;
+ }
+ fflush (stdout);
+}
+
+
+/**
+ * Continuation proceeding with initialization after identity subsystem
+ * has been initialized.
+ *
+ * @param args0 filename to publish
+ */
+static void
+identity_continuation (const char *args0)
+{
+ char *ex;
+ char *emsg;
+
+ if ( (NULL != pseudonym) &&
+ (NULL == namespace) )
+ {
+ FPRINTF (stderr,
+ _("Selected pseudonym `%s' unknown\n"),
+ pseudonym);
+ ret = 1;
+ GNUNET_SCHEDULER_shutdown ();
+ return;
+ }
+ if (NULL != uri_string)
+ {
+ emsg = NULL;
+ if (NULL == (uri = GNUNET_FS_uri_parse (uri_string, &emsg)))
+ {
+ FPRINTF (stderr,
+ _("Failed to parse URI: %s\n"),
+ emsg);
+ GNUNET_free (emsg);
+ ret = 1;
+ GNUNET_SCHEDULER_shutdown ();
+ return;
+ }
+ GNUNET_FS_publish_ksk (ctx, topKeywords,
+ meta, uri,
+ &bo,
+ GNUNET_FS_PUBLISH_OPTION_NONE,
+ &uri_ksk_continuation,
+ NULL);
+ return;
+ }
+ if (GNUNET_OK !=
+ GNUNET_CONFIGURATION_get_value_string (cfg, "FS", "EXTRACTORS", &ex))
+ ex = NULL;
+ if (0 != ACCESS (args0, R_OK))
+ {
+ FPRINTF (stderr,
+ _("Failed to access `%s': %s\n"),
+ args0,
+ STRERROR (errno));
+ GNUNET_free_non_null (ex);
+ return;
+ }
+ ds = GNUNET_FS_directory_scan_start (args0,
+ disable_extractor,
+ ex,
+ &directory_scan_cb, NULL);
+ if (NULL == ds)
+ {
+ FPRINTF (stderr,
+ "%s",
+ _("Failed to start meta directory scanner. Is gnunet-helper-publish-fs installed?\n"));
+ GNUNET_free_non_null (ex);
+ return;
+ }
+ GNUNET_free_non_null (ex);
+}
+
+
+/**
+ * Function called by identity service with known pseudonyms.
+ *
+ * @param cls closure with 'const char *' of filename to publish
+ * @param ego ego handle
+ * @param ctx context for application to store data for this ego
+ * (during the lifetime of this process, initially NULL)
+ * @param name name assigned by the user for this ego,
+ * NULL if the user just deleted the ego and it
+ * must thus no longer be used
+ */
+static void
+identity_cb (void *cls,
+ struct GNUNET_IDENTITY_Ego *ego,
+ void **ctx,
+ const char *name)
+{
+ const char *args0 = cls;
+
+ if (NULL == ego)
+ {
+ identity_continuation (args0);
+ return;
+ }
+ if (NULL == name)
+ return;
+ if (0 == strcmp (name, pseudonym))
+ namespace = ego;
+}
+
+