2 This file is part of GNUnet.
3 Copyright (C) 2001-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file fs/gnunet-publish.c
22 * @brief publishing files on GNUnet
23 * @author Christian Grothoff
24 * @author Krista Bennett
25 * @author James Blackwell
26 * @author Igor Wronsky
29 #include "gnunet_fs_service.h"
30 #include "gnunet_identity_service.h"
33 * Global return value from #main().
38 * Command line option 'verbose' set
43 * Handle to our configuration.
45 static const struct GNUNET_CONFIGURATION_Handle *cfg;
48 * Handle for interaction with file-sharing service.
50 static struct GNUNET_FS_Handle *ctx;
53 * Handle to FS-publishing operation.
55 static struct GNUNET_FS_PublishContext *pc;
58 * Meta-data provided via command-line option.
60 static struct GNUNET_CONTAINER_MetaData *meta;
63 * Keywords provided via command-line option.
65 static struct GNUNET_FS_Uri *topKeywords;
68 * Options we set for published blocks.
70 static struct GNUNET_FS_BlockOptions bo = { {0LL}, 1, 365, 1 };
73 * Value of URI provided on command-line (when not publishing
74 * a file but just creating UBlocks to refer to an existing URI).
76 static char *uri_string;
79 * Value of URI provided on command-line (when not publishing
80 * a file but just creating UBlocks to refer to an existing URI);
81 * parsed version of 'uri_string'.
83 static struct GNUNET_FS_Uri *uri;
86 * Command-line option for namespace publishing: identifier for updates
87 * to this publication.
92 * Command-line option for namespace publishing: identifier for this
98 * Command-line option identifying the pseudonym to use for the publication.
100 static char *pseudonym;
103 * Command-line option for 'inserting'
105 static int do_insert;
108 * Command-line option to disable meta data extraction.
110 static int disable_extractor;
113 * Command-line option to merely simulate publishing operation.
115 static int do_simulate;
118 * Command-line option to only perform meta data extraction, but not publish.
120 static int extract_only;
123 * Command-line option to disable adding creation time.
125 static int do_disable_creation_time;
128 * Handle to the directory scanner (for recursive insertions).
130 static struct GNUNET_FS_DirScanner *ds;
133 * Which namespace do we publish to? NULL if we do not publish to
136 static struct GNUNET_IDENTITY_Ego *namespace;
139 * Handle to identity service.
141 static struct GNUNET_IDENTITY_Handle *identity;
145 * We are finished with the publishing operation, clean up all
149 * @param tc scheduler context
152 do_stop_task (void *cls,
153 const struct GNUNET_SCHEDULER_TaskContext *tc)
155 struct GNUNET_FS_PublishContext *p;
159 GNUNET_FS_directory_scan_abort (ds);
162 if (NULL != identity)
164 GNUNET_IDENTITY_disconnect (identity);
171 GNUNET_FS_publish_stop (p);
175 GNUNET_FS_stop (ctx);
180 GNUNET_CONTAINER_meta_data_destroy (meta);
185 GNUNET_FS_uri_destroy (uri);
192 * Called by FS client to give information about the progress of an
196 * @param info details about the event, specifying the event type
197 * and various bits about the event
198 * @return client-context (for the next progress call
199 * for this operation; should be set to NULL for
200 * SUSPEND and STOPPED events). The value returned
201 * will be passed to future callbacks in the respective
202 * field in the GNUNET_FS_ProgressInfo struct.
205 progress_cb (void *cls,
206 const struct GNUNET_FS_ProgressInfo *info)
211 switch (info->status)
213 case GNUNET_FS_STATUS_PUBLISH_START:
215 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
218 s = GNUNET_STRINGS_relative_time_to_string (info->value.publish.eta,
221 _("Publishing `%s' at %llu/%llu (%s remaining)\n"),
222 info->value.publish.filename,
223 (unsigned long long) info->value.publish.completed,
224 (unsigned long long) info->value.publish.size, s);
227 case GNUNET_FS_STATUS_PUBLISH_PROGRESS_DIRECTORY:
230 s = GNUNET_STRINGS_relative_time_to_string (info->value.publish.specifics.progress_directory.eta,
233 _("Publishing `%s' at %llu/%llu (%s remaining)\n"),
234 info->value.publish.filename,
235 (unsigned long long) info->value.publish.specifics.progress_directory.completed,
236 (unsigned long long) info->value.publish.specifics.progress_directory.total, s);
239 case GNUNET_FS_STATUS_PUBLISH_ERROR:
241 _("Error publishing: %s.\n"),
242 info->value.publish.specifics.error.message);
244 GNUNET_SCHEDULER_shutdown ();
246 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
248 _("Publishing `%s' done.\n"),
249 info->value.publish.filename);
250 suri = GNUNET_FS_uri_to_string (info->value.publish.specifics.
256 if (NULL != info->value.publish.specifics.completed.sks_uri)
258 suri = GNUNET_FS_uri_to_string (info->value.publish.specifics.
261 _("Namespace URI is `%s'.\n"),
265 if (NULL == info->value.publish.pctx)
268 GNUNET_SCHEDULER_shutdown ();
271 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
272 GNUNET_break (NULL == pc);
274 case GNUNET_FS_STATUS_UNINDEX_START:
277 _("Starting cleanup after abort\n"));
279 case GNUNET_FS_STATUS_UNINDEX_PROGRESS:
281 case GNUNET_FS_STATUS_UNINDEX_COMPLETED:
284 _("Cleanup after abort completed.\n"));
285 GNUNET_FS_unindex_stop (info->value.unindex.uc);
287 case GNUNET_FS_STATUS_UNINDEX_ERROR:
290 _("Cleanup after abort failed.\n"));
291 GNUNET_FS_unindex_stop (info->value.unindex.uc);
293 case GNUNET_FS_STATUS_UNINDEX_STOPPED:
297 _("Unexpected status: %d\n"),
301 return ""; /* non-null */
306 * Print metadata entries (except binary
307 * metadata and the filename).
310 * @param plugin_name name of the plugin that generated the meta data
311 * @param type type of the meta data
312 * @param format format of data
313 * @param data_mime_type mime type of @a data
314 * @param data value of the meta data
315 * @param data_size number of bytes in @a data
319 meta_printer (void *cls,
320 const char *plugin_name,
321 enum EXTRACTOR_MetaType type,
322 enum EXTRACTOR_MetaFormat format,
323 const char *data_mime_type,
324 const char *data, size_t data_size)
326 if ((EXTRACTOR_METAFORMAT_UTF8 != format) &&
327 (EXTRACTOR_METAFORMAT_C_STRING != format))
329 if (EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME == type)
331 #if HAVE_LIBEXTRACTOR
334 EXTRACTOR_metatype_to_string (type),
347 * Iterator printing keywords
350 * @param keyword the keyword
351 * @param is_mandatory is the keyword mandatory (in a search)
352 * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR to abort
355 keyword_printer (void *cls,
359 FPRINTF (stdout, "\t%s\n", keyword);
365 * Function called on all entries before the publication. This is
366 * where we perform modifications to the default based on command-line
370 * @param fi the entry in the publish-structure
371 * @param length length of the file or directory
372 * @param m metadata for the file or directory (can be modified)
373 * @param uri pointer to the keywords that will be used for this entry (can be modified)
374 * @param bo block options
375 * @param do_index should we index?
376 * @param client_info pointer to client context set upon creation (can be modified)
377 * @return #GNUNET_OK to continue, #GNUNET_NO to remove
378 * this entry from the directory, #GNUNET_SYSERR
379 * to abort the iteration
382 publish_inspector (void *cls,
383 struct GNUNET_FS_FileInformation *fi,
385 struct GNUNET_CONTAINER_MetaData *m,
386 struct GNUNET_FS_Uri **uri,
387 struct GNUNET_FS_BlockOptions *bo,
393 struct GNUNET_FS_Uri *new_uri;
397 if ( (disable_extractor) &&
400 GNUNET_FS_uri_destroy (*uri);
403 if (NULL != topKeywords)
407 new_uri = GNUNET_FS_uri_ksk_merge (topKeywords, *uri);
408 GNUNET_FS_uri_destroy (*uri);
410 GNUNET_FS_uri_destroy (topKeywords);
420 GNUNET_CONTAINER_meta_data_merge (m, meta);
421 GNUNET_CONTAINER_meta_data_destroy (meta);
424 if (!do_disable_creation_time)
425 GNUNET_CONTAINER_meta_data_add_publication_date (m);
428 fn = GNUNET_CONTAINER_meta_data_get_by_type (m,
429 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
430 fs = GNUNET_STRINGS_byte_size_fancy (length);
432 _("Meta data for file `%s' (%s)\n"),
435 GNUNET_CONTAINER_meta_data_iterate (m, &meta_printer, NULL);
437 _("Keywords for file `%s' (%s)\n"),
443 GNUNET_FS_uri_ksk_get_keywords (*uri, &keyword_printer, NULL);
448 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (m))
449 GNUNET_FS_file_information_inspect (fi,
450 &publish_inspector, fi);
456 * Function called upon completion of the publishing
457 * of the UBLOCK for the SKS URI. As this is the last
458 * step, stop our interaction with FS (clean up).
460 * @param cls NULL (closure)
461 * @param sks_uri URI for the block that was published
462 * @param emsg error message, NULL on success
465 uri_sks_continuation (void *cls,
466 const struct GNUNET_FS_Uri *sks_uri,
476 GNUNET_SCHEDULER_shutdown ();
481 * Function called upon completion of the publishing
482 * of the UBLOCK for the KSK URI. Continue with
483 * publishing the SKS URI (if applicable) or clean up.
485 * @param cls NULL (closure)
486 * @param ksk_uri URI for the block that was published
487 * @param emsg error message, NULL on success
490 uri_ksk_continuation (void *cls,
491 const struct GNUNET_FS_Uri *ksk_uri,
494 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv;
503 if (NULL != namespace)
505 priv = GNUNET_IDENTITY_ego_get_private_key (namespace);
506 GNUNET_FS_publish_sks (ctx,
513 GNUNET_FS_PUBLISH_OPTION_NONE,
514 &uri_sks_continuation, NULL);
517 GNUNET_SCHEDULER_shutdown ();
522 * Iterate over the results from the directory scan and extract
523 * the desired information for the publishing operation.
525 * @param item root with the data from the directroy scan
526 * @return handle with the information for the publishing operation
528 static struct GNUNET_FS_FileInformation *
529 get_file_information (struct GNUNET_FS_ShareTreeItem *item)
531 struct GNUNET_FS_FileInformation *fi;
532 struct GNUNET_FS_FileInformation *fic;
533 struct GNUNET_FS_ShareTreeItem *child;
535 if (GNUNET_YES == item->is_directory)
537 if (NULL == item->meta)
538 item->meta = GNUNET_CONTAINER_meta_data_create ();
539 GNUNET_CONTAINER_meta_data_delete (item->meta,
540 EXTRACTOR_METATYPE_MIMETYPE,
542 GNUNET_FS_meta_data_make_directory (item->meta);
543 if (NULL == item->ksk_uri)
545 const char *mime = GNUNET_FS_DIRECTORY_MIME;
546 item->ksk_uri = GNUNET_FS_uri_ksk_create_from_args (1, &mime);
549 GNUNET_FS_uri_ksk_add_keyword (item->ksk_uri, GNUNET_FS_DIRECTORY_MIME,
551 fi = GNUNET_FS_file_information_create_empty_directory (ctx, NULL,
554 &bo, item->filename);
555 for (child = item->children_head; child; child = child->next)
557 fic = get_file_information (child);
558 GNUNET_break (GNUNET_OK == GNUNET_FS_file_information_add (fi, fic));
563 fi = GNUNET_FS_file_information_create_from_file (ctx, NULL,
565 item->ksk_uri, item->meta,
574 * We've finished scanning the directory and optimized the meta data.
575 * Begin the publication process.
577 * @param directory_scan_result result from the directory scan, freed in this function
580 directory_trim_complete (struct GNUNET_FS_ShareTreeItem *directory_scan_result)
582 struct GNUNET_FS_FileInformation *fi;
583 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv;
585 fi = get_file_information (directory_scan_result);
586 GNUNET_FS_share_tree_free (directory_scan_result);
591 _("Could not publish\n"));
593 GNUNET_SCHEDULER_shutdown ();
596 GNUNET_FS_file_information_inspect (fi, &publish_inspector, NULL);
599 GNUNET_FS_file_information_destroy (fi, NULL, NULL);
600 GNUNET_SCHEDULER_shutdown ();
603 if (NULL == namespace)
606 priv = GNUNET_IDENTITY_ego_get_private_key (namespace);
607 pc = GNUNET_FS_publish_start (ctx, fi,
608 priv, this_id, next_id,
610 GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY :
611 GNUNET_FS_PUBLISH_OPTION_NONE);
616 _("Could not start publishing.\n"));
618 GNUNET_SCHEDULER_shutdown ();
625 * Function called by the directory scanner as we build the tree
626 * that we will need to publish later.
629 * @param filename which file we are making progress on
630 * @param is_directory #GNUNET_YES if this is a directory,
631 * #GNUNET_NO if this is a file
632 * #GNUNET_SYSERR if it is neither (or unknown)
633 * @param reason kind of progress we are making
636 directory_scan_cb (void *cls,
637 const char *filename,
639 enum GNUNET_FS_DirScannerProgressUpdateReason reason)
641 struct GNUNET_FS_ShareTreeItem *directory_scan_result;
645 case GNUNET_FS_DIRSCANNER_FILE_START:
648 if (is_directory == GNUNET_YES)
650 _("Scanning directory `%s'.\n"),
654 _("Scanning file `%s'.\n"),
658 case GNUNET_FS_DIRSCANNER_FILE_IGNORED:
660 _("There was trouble processing file `%s', skipping it.\n"),
663 case GNUNET_FS_DIRSCANNER_ALL_COUNTED:
667 _("Preprocessing complete.\n"));
669 case GNUNET_FS_DIRSCANNER_EXTRACT_FINISHED:
672 _("Extracting meta data from file `%s' complete.\n"),
675 case GNUNET_FS_DIRSCANNER_FINISHED:
679 _("Meta data extraction has finished.\n"));
680 directory_scan_result = GNUNET_FS_directory_scan_get_result (ds);
682 GNUNET_FS_share_tree_trim (directory_scan_result);
683 directory_trim_complete (directory_scan_result);
685 case GNUNET_FS_DIRSCANNER_INTERNAL_ERROR:
688 _("Internal error scanning directory.\n"));
690 GNUNET_SCHEDULER_shutdown ();
701 * Continuation proceeding with initialization after identity subsystem
702 * has been initialized.
704 * @param args0 filename to publish
707 identity_continuation (const char *args0)
712 if ( (NULL != pseudonym) &&
713 (NULL == namespace) )
716 _("Selected pseudonym `%s' unknown\n"),
719 GNUNET_SCHEDULER_shutdown ();
722 if (NULL != uri_string)
725 if (NULL == (uri = GNUNET_FS_uri_parse (uri_string, &emsg)))
728 _("Failed to parse URI: %s\n"),
732 GNUNET_SCHEDULER_shutdown ();
735 GNUNET_FS_publish_ksk (ctx, topKeywords,
738 GNUNET_FS_PUBLISH_OPTION_NONE,
739 &uri_ksk_continuation,
744 GNUNET_CONFIGURATION_get_value_string (cfg, "FS", "EXTRACTORS", &ex))
746 if (0 != ACCESS (args0, R_OK))
749 _("Failed to access `%s': %s\n"),
752 GNUNET_free_non_null (ex);
755 ds = GNUNET_FS_directory_scan_start (args0,
758 &directory_scan_cb, NULL);
763 _("Failed to start meta directory scanner. Is gnunet-helper-publish-fs installed?\n"));
764 GNUNET_free_non_null (ex);
767 GNUNET_free_non_null (ex);
772 * Function called by identity service with known pseudonyms.
774 * @param cls closure with 'const char *' of filename to publish
775 * @param ego ego handle
776 * @param ctx context for application to store data for this ego
777 * (during the lifetime of this process, initially NULL)
778 * @param name name assigned by the user for this ego,
779 * NULL if the user just deleted the ego and it
780 * must thus no longer be used
783 identity_cb (void *cls,
784 struct GNUNET_IDENTITY_Ego *ego,
788 const char *args0 = cls;
792 identity_continuation (args0);
797 if (0 == strcmp (name, pseudonym))
803 * Main function that will be run by the scheduler.
806 * @param args remaining command-line arguments
807 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
808 * @param c configuration
814 const struct GNUNET_CONFIGURATION_Handle *c)
816 /* check arguments */
817 if ((NULL != uri_string) && (extract_only))
819 printf (_("Cannot extract metadata from a URI!\n"));
823 if (((NULL == uri_string) || (extract_only)) &&
824 ((NULL == args[0]) || (NULL != args[1])))
826 printf (_("You must specify one and only one filename for insertion.\n"));
830 if ((NULL != uri_string) && (NULL != args[0]))
832 printf (_("You must NOT specify an URI and a filename.\n"));
836 if (NULL != pseudonym)
840 FPRINTF (stderr, _("Option `%s' is required when using option `%s'.\n"),
847 { /* ordinary insertion checks */
851 _("Option `%s' makes no sense without option `%s'.\n"),
859 _("Option `%s' makes no sense without option `%s'.\n"),
867 GNUNET_FS_start (cfg, "gnunet-publish", &progress_cb, NULL,
868 GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
872 _("Could not initialize `%s' subsystem.\n"),
877 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
880 if (NULL != pseudonym)
881 identity = GNUNET_IDENTITY_connect (cfg,
885 identity_continuation (args[0]);
890 * The main function to publish content to GNUnet.
892 * @param argc number of arguments from the command line
893 * @param argv command line arguments
894 * @return 0 ok, 1 on error
897 main (int argc, char *const *argv)
899 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
900 {'a', "anonymity", "LEVEL",
901 gettext_noop ("set the desired LEVEL of sender-anonymity"),
902 1, &GNUNET_GETOPT_set_uint, &bo.anonymity_level},
903 {'d', "disable-creation-time", NULL,
905 ("disable adding the creation time to the metadata of the uploaded file"),
906 0, &GNUNET_GETOPT_set_one, &do_disable_creation_time},
907 {'D', "disable-extractor", NULL,
908 gettext_noop ("do not use libextractor to add keywords or metadata"),
909 0, &GNUNET_GETOPT_set_one, &disable_extractor},
910 {'e', "extract", NULL,
912 ("print list of extracted keywords that would be used, but do not perform upload"),
913 0, &GNUNET_GETOPT_set_one, &extract_only},
914 {'k', "key", "KEYWORD",
916 ("add an additional keyword for the top-level file or directory"
917 " (this option can be specified multiple times)"),
918 1, &GNUNET_FS_getopt_set_keywords, &topKeywords},
919 {'m', "meta", "TYPE:VALUE",
920 gettext_noop ("set the meta-data for the given TYPE to the given VALUE"),
921 1, &GNUNET_FS_getopt_set_metadata, &meta},
922 {'n', "noindex", NULL,
923 gettext_noop ("do not index, perform full insertion (stores entire "
924 "file in encrypted form in GNUnet database)"),
925 0, &GNUNET_GETOPT_set_one, &do_insert},
928 ("specify ID of an updated version to be published in the future"
929 " (for namespace insertions only)"),
930 1, &GNUNET_GETOPT_set_string, &next_id},
931 {'p', "priority", "PRIORITY",
932 gettext_noop ("specify the priority of the content"),
933 1, &GNUNET_GETOPT_set_uint, &bo.content_priority},
934 {'P', "pseudonym", "NAME",
936 ("publish the files under the pseudonym NAME (place file into namespace)"),
937 1, &GNUNET_GETOPT_set_string, &pseudonym},
938 {'r', "replication", "LEVEL",
939 gettext_noop ("set the desired replication LEVEL"),
940 1, &GNUNET_GETOPT_set_uint, &bo.replication_level},
941 {'s', "simulate-only", NULL,
942 gettext_noop ("only simulate the process but do not do any "
943 "actual publishing (useful to compute URIs)"),
944 0, &GNUNET_GETOPT_set_one, &do_simulate},
946 gettext_noop ("set the ID of this version of the publication"
947 " (for namespace insertions only)"),
948 1, &GNUNET_GETOPT_set_string, &this_id},
950 gettext_noop ("URI to be published (can be used instead of passing a "
951 "file to add keywords to the file with the respective URI)"),
952 1, &GNUNET_GETOPT_set_string, &uri_string},
953 {'V', "verbose", NULL,
954 gettext_noop ("be verbose (print progress information)"),
955 0, &GNUNET_GETOPT_set_one, &verbose},
956 GNUNET_GETOPT_OPTION_END
959 GNUNET_TIME_year_to_time (GNUNET_TIME_get_current_year () + 2);
961 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
964 GNUNET_PROGRAM_run (argc, argv, "gnunet-publish [OPTIONS] FILENAME",
966 ("Publish a file or directory on GNUnet"),
967 options, &run, NULL)) ? ret : 1;
968 GNUNET_free ((void*) argv);
972 /* end of gnunet-publish.c */