2 This file is part of GNUnet.
3 Copyright (C) 2001-2013 GNUnet e.V.
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
151 do_stop_task (void *cls)
153 struct GNUNET_FS_PublishContext *p;
157 GNUNET_FS_directory_scan_abort (ds);
160 if (NULL != identity)
162 GNUNET_IDENTITY_disconnect (identity);
169 GNUNET_FS_publish_stop (p);
173 GNUNET_FS_stop (ctx);
178 GNUNET_CONTAINER_meta_data_destroy (meta);
183 GNUNET_FS_uri_destroy (uri);
190 * Called by FS client to give information about the progress of an
194 * @param info details about the event, specifying the event type
195 * and various bits about the event
196 * @return client-context (for the next progress call
197 * for this operation; should be set to NULL for
198 * SUSPEND and STOPPED events). The value returned
199 * will be passed to future callbacks in the respective
200 * field in the GNUNET_FS_ProgressInfo struct.
203 progress_cb (void *cls,
204 const struct GNUNET_FS_ProgressInfo *info)
209 switch (info->status)
211 case GNUNET_FS_STATUS_PUBLISH_START:
213 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
216 s = GNUNET_STRINGS_relative_time_to_string (info->value.publish.eta,
219 _("Publishing `%s' at %llu/%llu (%s remaining)\n"),
220 info->value.publish.filename,
221 (unsigned long long) info->value.publish.completed,
222 (unsigned long long) info->value.publish.size, s);
225 case GNUNET_FS_STATUS_PUBLISH_PROGRESS_DIRECTORY:
228 s = GNUNET_STRINGS_relative_time_to_string (info->value.publish.specifics.progress_directory.eta,
231 _("Publishing `%s' at %llu/%llu (%s remaining)\n"),
232 info->value.publish.filename,
233 (unsigned long long) info->value.publish.specifics.progress_directory.completed,
234 (unsigned long long) info->value.publish.specifics.progress_directory.total, s);
237 case GNUNET_FS_STATUS_PUBLISH_ERROR:
239 _("Error publishing: %s.\n"),
240 info->value.publish.specifics.error.message);
242 GNUNET_SCHEDULER_shutdown ();
244 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
246 _("Publishing `%s' done.\n"),
247 info->value.publish.filename);
248 suri = GNUNET_FS_uri_to_string (info->value.publish.specifics.
254 if (NULL != info->value.publish.specifics.completed.sks_uri)
256 suri = GNUNET_FS_uri_to_string (info->value.publish.specifics.
259 _("Namespace URI is `%s'.\n"),
263 if (NULL == info->value.publish.pctx)
266 GNUNET_SCHEDULER_shutdown ();
269 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
270 GNUNET_break (NULL == pc);
272 case GNUNET_FS_STATUS_UNINDEX_START:
275 _("Starting cleanup after abort\n"));
277 case GNUNET_FS_STATUS_UNINDEX_PROGRESS:
279 case GNUNET_FS_STATUS_UNINDEX_COMPLETED:
282 _("Cleanup after abort completed.\n"));
283 GNUNET_FS_unindex_stop (info->value.unindex.uc);
285 case GNUNET_FS_STATUS_UNINDEX_ERROR:
288 _("Cleanup after abort failed.\n"));
289 GNUNET_FS_unindex_stop (info->value.unindex.uc);
291 case GNUNET_FS_STATUS_UNINDEX_STOPPED:
295 _("Unexpected status: %d\n"),
299 return ""; /* non-null */
304 * Print metadata entries (except binary
305 * metadata and the filename).
308 * @param plugin_name name of the plugin that generated the meta data
309 * @param type type of the meta data
310 * @param format format of data
311 * @param data_mime_type mime type of @a data
312 * @param data value of the meta data
313 * @param data_size number of bytes in @a data
317 meta_printer (void *cls,
318 const char *plugin_name,
319 enum EXTRACTOR_MetaType type,
320 enum EXTRACTOR_MetaFormat format,
321 const char *data_mime_type,
322 const char *data, size_t data_size)
324 if ((EXTRACTOR_METAFORMAT_UTF8 != format) &&
325 (EXTRACTOR_METAFORMAT_C_STRING != format))
327 if (EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME == type)
329 #if HAVE_LIBEXTRACTOR
332 EXTRACTOR_metatype_to_string (type),
345 * Iterator printing keywords
348 * @param keyword the keyword
349 * @param is_mandatory is the keyword mandatory (in a search)
350 * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR to abort
353 keyword_printer (void *cls,
357 FPRINTF (stdout, "\t%s\n", keyword);
363 * Function called on all entries before the publication. This is
364 * where we perform modifications to the default based on command-line
368 * @param fi the entry in the publish-structure
369 * @param length length of the file or directory
370 * @param m metadata for the file or directory (can be modified)
371 * @param uri pointer to the keywords that will be used for this entry (can be modified)
372 * @param bo block options
373 * @param do_index should we index?
374 * @param client_info pointer to client context set upon creation (can be modified)
375 * @return #GNUNET_OK to continue, #GNUNET_NO to remove
376 * this entry from the directory, #GNUNET_SYSERR
377 * to abort the iteration
380 publish_inspector (void *cls,
381 struct GNUNET_FS_FileInformation *fi,
383 struct GNUNET_CONTAINER_MetaData *m,
384 struct GNUNET_FS_Uri **uri,
385 struct GNUNET_FS_BlockOptions *bo,
391 struct GNUNET_FS_Uri *new_uri;
395 if ( (disable_extractor) &&
398 GNUNET_FS_uri_destroy (*uri);
401 if (NULL != topKeywords)
405 new_uri = GNUNET_FS_uri_ksk_merge (topKeywords, *uri);
406 GNUNET_FS_uri_destroy (*uri);
408 GNUNET_FS_uri_destroy (topKeywords);
418 GNUNET_CONTAINER_meta_data_merge (m, meta);
419 GNUNET_CONTAINER_meta_data_destroy (meta);
422 if (!do_disable_creation_time)
423 GNUNET_CONTAINER_meta_data_add_publication_date (m);
426 fn = GNUNET_CONTAINER_meta_data_get_by_type (m,
427 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
428 fs = GNUNET_STRINGS_byte_size_fancy (length);
430 _("Meta data for file `%s' (%s)\n"),
433 GNUNET_CONTAINER_meta_data_iterate (m, &meta_printer, NULL);
435 _("Keywords for file `%s' (%s)\n"),
441 GNUNET_FS_uri_ksk_get_keywords (*uri, &keyword_printer, NULL);
446 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (m))
447 GNUNET_FS_file_information_inspect (fi,
448 &publish_inspector, fi);
454 * Function called upon completion of the publishing
455 * of the UBLOCK for the SKS URI. As this is the last
456 * step, stop our interaction with FS (clean up).
458 * @param cls NULL (closure)
459 * @param sks_uri URI for the block that was published
460 * @param emsg error message, NULL on success
463 uri_sks_continuation (void *cls,
464 const struct GNUNET_FS_Uri *sks_uri,
474 GNUNET_SCHEDULER_shutdown ();
479 * Function called upon completion of the publishing
480 * of the UBLOCK for the KSK URI. Continue with
481 * publishing the SKS URI (if applicable) or clean up.
483 * @param cls NULL (closure)
484 * @param ksk_uri URI for the block that was published
485 * @param emsg error message, NULL on success
488 uri_ksk_continuation (void *cls,
489 const struct GNUNET_FS_Uri *ksk_uri,
492 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv;
501 if (NULL != namespace)
503 priv = GNUNET_IDENTITY_ego_get_private_key (namespace);
504 GNUNET_FS_publish_sks (ctx,
511 GNUNET_FS_PUBLISH_OPTION_NONE,
512 &uri_sks_continuation, NULL);
515 GNUNET_SCHEDULER_shutdown ();
520 * Iterate over the results from the directory scan and extract
521 * the desired information for the publishing operation.
523 * @param item root with the data from the directroy scan
524 * @return handle with the information for the publishing operation
526 static struct GNUNET_FS_FileInformation *
527 get_file_information (struct GNUNET_FS_ShareTreeItem *item)
529 struct GNUNET_FS_FileInformation *fi;
530 struct GNUNET_FS_FileInformation *fic;
531 struct GNUNET_FS_ShareTreeItem *child;
533 if (GNUNET_YES == item->is_directory)
535 if (NULL == item->meta)
536 item->meta = GNUNET_CONTAINER_meta_data_create ();
537 GNUNET_CONTAINER_meta_data_delete (item->meta,
538 EXTRACTOR_METATYPE_MIMETYPE,
540 GNUNET_FS_meta_data_make_directory (item->meta);
541 if (NULL == item->ksk_uri)
543 const char *mime = GNUNET_FS_DIRECTORY_MIME;
544 item->ksk_uri = GNUNET_FS_uri_ksk_create_from_args (1, &mime);
547 GNUNET_FS_uri_ksk_add_keyword (item->ksk_uri, GNUNET_FS_DIRECTORY_MIME,
549 fi = GNUNET_FS_file_information_create_empty_directory (ctx, NULL,
552 &bo, item->filename);
553 for (child = item->children_head; child; child = child->next)
555 fic = get_file_information (child);
556 GNUNET_break (GNUNET_OK == GNUNET_FS_file_information_add (fi, fic));
561 fi = GNUNET_FS_file_information_create_from_file (ctx, NULL,
563 item->ksk_uri, item->meta,
572 * We've finished scanning the directory and optimized the meta data.
573 * Begin the publication process.
575 * @param directory_scan_result result from the directory scan, freed in this function
578 directory_trim_complete (struct GNUNET_FS_ShareTreeItem *directory_scan_result)
580 struct GNUNET_FS_FileInformation *fi;
581 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv;
583 fi = get_file_information (directory_scan_result);
584 GNUNET_FS_share_tree_free (directory_scan_result);
589 _("Could not publish\n"));
591 GNUNET_SCHEDULER_shutdown ();
594 GNUNET_FS_file_information_inspect (fi, &publish_inspector, NULL);
597 GNUNET_FS_file_information_destroy (fi, NULL, NULL);
598 GNUNET_SCHEDULER_shutdown ();
601 if (NULL == namespace)
604 priv = GNUNET_IDENTITY_ego_get_private_key (namespace);
605 pc = GNUNET_FS_publish_start (ctx, fi,
606 priv, this_id, next_id,
608 GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY :
609 GNUNET_FS_PUBLISH_OPTION_NONE);
614 _("Could not start publishing.\n"));
616 GNUNET_SCHEDULER_shutdown ();
623 * Function called by the directory scanner as we build the tree
624 * that we will need to publish later.
627 * @param filename which file we are making progress on
628 * @param is_directory #GNUNET_YES if this is a directory,
629 * #GNUNET_NO if this is a file
630 * #GNUNET_SYSERR if it is neither (or unknown)
631 * @param reason kind of progress we are making
634 directory_scan_cb (void *cls,
635 const char *filename,
637 enum GNUNET_FS_DirScannerProgressUpdateReason reason)
639 struct GNUNET_FS_ShareTreeItem *directory_scan_result;
643 case GNUNET_FS_DIRSCANNER_FILE_START:
646 if (is_directory == GNUNET_YES)
648 _("Scanning directory `%s'.\n"),
652 _("Scanning file `%s'.\n"),
656 case GNUNET_FS_DIRSCANNER_FILE_IGNORED:
658 _("There was trouble processing file `%s', skipping it.\n"),
661 case GNUNET_FS_DIRSCANNER_ALL_COUNTED:
665 _("Preprocessing complete.\n"));
667 case GNUNET_FS_DIRSCANNER_EXTRACT_FINISHED:
670 _("Extracting meta data from file `%s' complete.\n"),
673 case GNUNET_FS_DIRSCANNER_FINISHED:
677 _("Meta data extraction has finished.\n"));
678 directory_scan_result = GNUNET_FS_directory_scan_get_result (ds);
680 GNUNET_FS_share_tree_trim (directory_scan_result);
681 directory_trim_complete (directory_scan_result);
683 case GNUNET_FS_DIRSCANNER_INTERNAL_ERROR:
686 _("Internal error scanning directory.\n"));
688 GNUNET_SCHEDULER_shutdown ();
699 * Continuation proceeding with initialization after identity subsystem
700 * has been initialized.
702 * @param args0 filename to publish
705 identity_continuation (const char *args0)
710 if ( (NULL != pseudonym) &&
711 (NULL == namespace) )
714 _("Selected pseudonym `%s' unknown\n"),
717 GNUNET_SCHEDULER_shutdown ();
720 if (NULL != uri_string)
723 if (NULL == (uri = GNUNET_FS_uri_parse (uri_string, &emsg)))
726 _("Failed to parse URI: %s\n"),
730 GNUNET_SCHEDULER_shutdown ();
733 GNUNET_FS_publish_ksk (ctx, topKeywords,
736 GNUNET_FS_PUBLISH_OPTION_NONE,
737 &uri_ksk_continuation,
742 GNUNET_CONFIGURATION_get_value_string (cfg, "FS", "EXTRACTORS", &ex))
744 if (0 != ACCESS (args0, R_OK))
747 _("Failed to access `%s': %s\n"),
750 GNUNET_free_non_null (ex);
753 ds = GNUNET_FS_directory_scan_start (args0,
756 &directory_scan_cb, NULL);
761 _("Failed to start meta directory scanner. Is gnunet-helper-publish-fs installed?\n"));
762 GNUNET_free_non_null (ex);
765 GNUNET_free_non_null (ex);
770 * Function called by identity service with known pseudonyms.
772 * @param cls closure with 'const char *' of filename to publish
773 * @param ego ego handle
774 * @param ctx context for application to store data for this ego
775 * (during the lifetime of this process, initially NULL)
776 * @param name name assigned by the user for this ego,
777 * NULL if the user just deleted the ego and it
778 * must thus no longer be used
781 identity_cb (void *cls,
782 struct GNUNET_IDENTITY_Ego *ego,
786 const char *args0 = cls;
790 identity_continuation (args0);
795 if (0 == strcmp (name, pseudonym))
801 * Main function that will be run by the scheduler.
804 * @param args remaining command-line arguments
805 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
806 * @param c configuration
812 const struct GNUNET_CONFIGURATION_Handle *c)
814 /* check arguments */
815 if ((NULL != uri_string) && (extract_only))
817 printf (_("Cannot extract metadata from a URI!\n"));
821 if (((NULL == uri_string) || (extract_only)) &&
822 ((NULL == args[0]) || (NULL != args[1])))
824 printf (_("You must specify one and only one filename for insertion.\n"));
828 if ((NULL != uri_string) && (NULL != args[0]))
830 printf (_("You must NOT specify an URI and a filename.\n"));
834 if (NULL != pseudonym)
838 FPRINTF (stderr, _("Option `%s' is required when using option `%s'.\n"),
845 { /* ordinary insertion checks */
849 _("Option `%s' makes no sense without option `%s'.\n"),
857 _("Option `%s' makes no sense without option `%s'.\n"),
865 GNUNET_FS_start (cfg, "gnunet-publish", &progress_cb, NULL,
866 GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
870 _("Could not initialize `%s' subsystem.\n"),
875 GNUNET_SCHEDULER_add_shutdown (&do_stop_task,
877 if (NULL != pseudonym)
878 identity = GNUNET_IDENTITY_connect (cfg,
882 identity_continuation (args[0]);
887 * The main function to publish content to GNUnet.
889 * @param argc number of arguments from the command line
890 * @param argv command line arguments
891 * @return 0 ok, 1 on error
894 main (int argc, char *const *argv)
896 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
897 {'a', "anonymity", "LEVEL",
898 gettext_noop ("set the desired LEVEL of sender-anonymity"),
899 1, &GNUNET_GETOPT_set_uint, &bo.anonymity_level},
900 {'d', "disable-creation-time", NULL,
902 ("disable adding the creation time to the metadata of the uploaded file"),
903 0, &GNUNET_GETOPT_set_one, &do_disable_creation_time},
904 {'D', "disable-extractor", NULL,
905 gettext_noop ("do not use libextractor to add keywords or metadata"),
906 0, &GNUNET_GETOPT_set_one, &disable_extractor},
907 {'e', "extract", NULL,
909 ("print list of extracted keywords that would be used, but do not perform upload"),
910 0, &GNUNET_GETOPT_set_one, &extract_only},
911 {'k', "key", "KEYWORD",
913 ("add an additional keyword for the top-level file or directory"
914 " (this option can be specified multiple times)"),
915 1, &GNUNET_FS_getopt_set_keywords, &topKeywords},
916 {'m', "meta", "TYPE:VALUE",
917 gettext_noop ("set the meta-data for the given TYPE to the given VALUE"),
918 1, &GNUNET_FS_getopt_set_metadata, &meta},
919 {'n', "noindex", NULL,
920 gettext_noop ("do not index, perform full insertion (stores entire "
921 "file in encrypted form in GNUnet database)"),
922 0, &GNUNET_GETOPT_set_one, &do_insert},
925 ("specify ID of an updated version to be published in the future"
926 " (for namespace insertions only)"),
927 1, &GNUNET_GETOPT_set_string, &next_id},
928 {'p', "priority", "PRIORITY",
929 gettext_noop ("specify the priority of the content"),
930 1, &GNUNET_GETOPT_set_uint, &bo.content_priority},
931 {'P', "pseudonym", "NAME",
933 ("publish the files under the pseudonym NAME (place file into namespace)"),
934 1, &GNUNET_GETOPT_set_string, &pseudonym},
935 {'r', "replication", "LEVEL",
936 gettext_noop ("set the desired replication LEVEL"),
937 1, &GNUNET_GETOPT_set_uint, &bo.replication_level},
938 {'s', "simulate-only", NULL,
939 gettext_noop ("only simulate the process but do not do any "
940 "actual publishing (useful to compute URIs)"),
941 0, &GNUNET_GETOPT_set_one, &do_simulate},
943 gettext_noop ("set the ID of this version of the publication"
944 " (for namespace insertions only)"),
945 1, &GNUNET_GETOPT_set_string, &this_id},
947 gettext_noop ("URI to be published (can be used instead of passing a "
948 "file to add keywords to the file with the respective URI)"),
949 1, &GNUNET_GETOPT_set_string, &uri_string},
950 {'V', "verbose", NULL,
951 gettext_noop ("be verbose (print progress information)"),
952 0, &GNUNET_GETOPT_set_one, &verbose},
953 GNUNET_GETOPT_OPTION_END
956 GNUNET_TIME_year_to_time (GNUNET_TIME_get_current_year () + 2);
958 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
961 GNUNET_PROGRAM_run (argc, argv, "gnunet-publish [OPTIONS] FILENAME",
963 ("Publish a file or directory on GNUnet"),
964 options, &run, NULL)) ? ret : 1;
965 GNUNET_free ((void*) argv);
969 /* end of gnunet-publish.c */