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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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 * Task run on CTRL-C to kill everything nicely.
130 static struct GNUNET_SCHEDULER_Task * kill_task;
133 * Handle to the directory scanner (for recursive insertions).
135 static struct GNUNET_FS_DirScanner *ds;
138 * Which namespace do we publish to? NULL if we do not publish to
141 static struct GNUNET_IDENTITY_Ego *namespace;
144 * Handle to identity service.
146 static struct GNUNET_IDENTITY_Handle *identity;
150 * We are finished with the publishing operation, clean up all
154 * @param tc scheduler context
157 do_stop_task (void *cls,
158 const struct GNUNET_SCHEDULER_TaskContext *tc)
160 struct GNUNET_FS_PublishContext *p;
163 if (NULL != identity)
165 GNUNET_IDENTITY_disconnect (identity);
172 GNUNET_FS_publish_stop (p);
176 GNUNET_CONTAINER_meta_data_destroy (meta);
183 * Stop the directory scanner (we had an error).
186 * @param tc scheduler context
189 stop_scanner_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
194 GNUNET_FS_directory_scan_abort (ds);
197 if (NULL != identity)
199 GNUNET_IDENTITY_disconnect (identity);
202 GNUNET_FS_stop (ctx);
209 * Called by FS client to give information about the progress of an
213 * @param info details about the event, specifying the event type
214 * and various bits about the event
215 * @return client-context (for the next progress call
216 * for this operation; should be set to NULL for
217 * SUSPEND and STOPPED events). The value returned
218 * will be passed to future callbacks in the respective
219 * field in the GNUNET_FS_ProgressInfo struct.
222 progress_cb (void *cls,
223 const struct GNUNET_FS_ProgressInfo *info)
228 switch (info->status)
230 case GNUNET_FS_STATUS_PUBLISH_START:
232 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
235 s = GNUNET_STRINGS_relative_time_to_string (info->value.publish.eta,
238 _("Publishing `%s' at %llu/%llu (%s remaining)\n"),
239 info->value.publish.filename,
240 (unsigned long long) info->value.publish.completed,
241 (unsigned long long) info->value.publish.size, s);
244 case GNUNET_FS_STATUS_PUBLISH_PROGRESS_DIRECTORY:
247 s = GNUNET_STRINGS_relative_time_to_string (info->value.publish.specifics.progress_directory.eta,
250 _("Publishing `%s' at %llu/%llu (%s remaining)\n"),
251 info->value.publish.filename,
252 (unsigned long long) info->value.publish.specifics.progress_directory.completed,
253 (unsigned long long) info->value.publish.specifics.progress_directory.total, s);
256 case GNUNET_FS_STATUS_PUBLISH_ERROR:
257 FPRINTF (stderr, _("Error publishing: %s.\n"),
258 info->value.publish.specifics.error.message);
259 if (kill_task != NULL)
261 GNUNET_SCHEDULER_cancel (kill_task);
264 kill_task = GNUNET_SCHEDULER_add_now (&do_stop_task, NULL);
266 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
268 _("Publishing `%s' done.\n"),
269 info->value.publish.filename);
270 suri = GNUNET_FS_uri_to_string (info->value.publish.specifics.
276 if (NULL != info->value.publish.specifics.completed.sks_uri)
278 suri = GNUNET_FS_uri_to_string (info->value.publish.specifics.
281 _("Namespace URI is `%s'.\n"),
285 if (NULL == info->value.publish.pctx)
287 if (NULL != kill_task)
288 GNUNET_SCHEDULER_cancel (kill_task);
289 kill_task = GNUNET_SCHEDULER_add_now (&do_stop_task, NULL);
293 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
294 GNUNET_break (NULL == pc);
296 case GNUNET_FS_STATUS_UNINDEX_START:
299 _("Starting cleanup after abort\n"));
301 case GNUNET_FS_STATUS_UNINDEX_PROGRESS:
303 case GNUNET_FS_STATUS_UNINDEX_COMPLETED:
306 _("Cleanup after abort completed.\n"));
307 GNUNET_FS_unindex_stop (info->value.unindex.uc);
309 case GNUNET_FS_STATUS_UNINDEX_ERROR:
312 _("Cleanup after abort failed.\n"));
313 GNUNET_FS_unindex_stop (info->value.unindex.uc);
317 _("Unexpected status: %d\n"),
321 return ""; /* non-null */
326 * Print metadata entries (except binary
327 * metadata and the filename).
330 * @param plugin_name name of the plugin that generated the meta data
331 * @param type type of the meta data
332 * @param format format of data
333 * @param data_mime_type mime type of @a data
334 * @param data value of the meta data
335 * @param data_size number of bytes in @a data
339 meta_printer (void *cls,
340 const char *plugin_name,
341 enum EXTRACTOR_MetaType type,
342 enum EXTRACTOR_MetaFormat format,
343 const char *data_mime_type,
344 const char *data, size_t data_size)
346 if ((EXTRACTOR_METAFORMAT_UTF8 != format) &&
347 (EXTRACTOR_METAFORMAT_C_STRING != format))
349 if (EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME == type)
351 FPRINTF (stdout, "\t%s - %s\n", EXTRACTOR_metatype_to_string (type), data);
357 * Iterator printing keywords
360 * @param keyword the keyword
361 * @param is_mandatory is the keyword mandatory (in a search)
362 * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR to abort
365 keyword_printer (void *cls,
369 FPRINTF (stdout, "\t%s\n", keyword);
375 * Function called on all entries before the publication. This is
376 * where we perform modifications to the default based on command-line
380 * @param fi the entry in the publish-structure
381 * @param length length of the file or directory
382 * @param m metadata for the file or directory (can be modified)
383 * @param uri pointer to the keywords that will be used for this entry (can be modified)
384 * @param bo block options
385 * @param do_index should we index?
386 * @param client_info pointer to client context set upon creation (can be modified)
387 * @return #GNUNET_OK to continue, #GNUNET_NO to remove
388 * this entry from the directory, #GNUNET_SYSERR
389 * to abort the iteration
392 publish_inspector (void *cls,
393 struct GNUNET_FS_FileInformation *fi,
395 struct GNUNET_CONTAINER_MetaData *m,
396 struct GNUNET_FS_Uri **uri,
397 struct GNUNET_FS_BlockOptions *bo,
403 struct GNUNET_FS_Uri *new_uri;
407 if ( (disable_extractor) &&
410 GNUNET_FS_uri_destroy (*uri);
413 if (NULL != topKeywords)
417 new_uri = GNUNET_FS_uri_ksk_merge (topKeywords, *uri);
418 GNUNET_FS_uri_destroy (*uri);
420 GNUNET_FS_uri_destroy (topKeywords);
430 GNUNET_CONTAINER_meta_data_merge (m, meta);
431 GNUNET_CONTAINER_meta_data_destroy (meta);
434 if (!do_disable_creation_time)
435 GNUNET_CONTAINER_meta_data_add_publication_date (m);
438 fn = GNUNET_CONTAINER_meta_data_get_by_type (m,
439 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
440 fs = GNUNET_STRINGS_byte_size_fancy (length);
441 FPRINTF (stdout, _("Meta data for file `%s' (%s)\n"), fn, fs);
442 GNUNET_CONTAINER_meta_data_iterate (m, &meta_printer, NULL);
443 FPRINTF (stdout, _("Keywords for file `%s' (%s)\n"), fn, fs);
447 GNUNET_FS_uri_ksk_get_keywords (*uri, &keyword_printer, NULL);
448 FPRINTF (stdout, "%s", "\n");
450 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (m))
451 GNUNET_FS_file_information_inspect (fi, &publish_inspector, fi);
457 * Function called upon completion of the publishing
458 * of the UBLOCK for the SKS URI. As this is the last
459 * step, stop our interaction with FS (clean up).
461 * @param cls NULL (closure)
462 * @param sks_uri URI for the block that was published
463 * @param emsg error message, NULL on success
466 uri_sks_continuation (void *cls,
467 const struct GNUNET_FS_Uri *sks_uri,
472 FPRINTF (stderr, "%s\n", emsg);
475 GNUNET_FS_uri_destroy (uri);
477 GNUNET_FS_stop (ctx);
483 * Function called upon completion of the publishing
484 * of the UBLOCK for the KSK URI. Continue with
485 * publishing the SKS URI (if applicable) or clean up.
487 * @param cls NULL (closure)
488 * @param ksk_uri URI for the block that was published
489 * @param emsg error message, NULL on success
492 uri_ksk_continuation (void *cls,
493 const struct GNUNET_FS_Uri *ksk_uri,
496 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv;
500 FPRINTF (stderr, "%s\n", emsg);
503 if (NULL != namespace)
505 priv = GNUNET_IDENTITY_ego_get_private_key (namespace);
506 GNUNET_FS_publish_sks (ctx, priv, this_id, next_id, meta, uri, &bo,
507 GNUNET_FS_PUBLISH_OPTION_NONE,
508 &uri_sks_continuation, NULL);
511 GNUNET_FS_uri_destroy (uri);
513 GNUNET_FS_stop (ctx);
519 * Iterate over the results from the directory scan and extract
520 * the desired information for the publishing operation.
522 * @param item root with the data from the directroy scan
523 * @return handle with the information for the publishing operation
525 static struct GNUNET_FS_FileInformation *
526 get_file_information (struct GNUNET_FS_ShareTreeItem *item)
528 struct GNUNET_FS_FileInformation *fi;
529 struct GNUNET_FS_FileInformation *fic;
530 struct GNUNET_FS_ShareTreeItem *child;
532 if (GNUNET_YES == item->is_directory)
534 if (NULL == item->meta)
535 item->meta = GNUNET_CONTAINER_meta_data_create ();
536 GNUNET_CONTAINER_meta_data_delete (item->meta,
537 EXTRACTOR_METATYPE_MIMETYPE,
539 GNUNET_FS_meta_data_make_directory (item->meta);
540 if (NULL == item->ksk_uri)
542 const char *mime = GNUNET_FS_DIRECTORY_MIME;
543 item->ksk_uri = GNUNET_FS_uri_ksk_create_from_args (1, &mime);
546 GNUNET_FS_uri_ksk_add_keyword (item->ksk_uri, GNUNET_FS_DIRECTORY_MIME,
548 fi = GNUNET_FS_file_information_create_empty_directory (ctx, NULL,
551 &bo, item->filename);
552 for (child = item->children_head; child; child = child->next)
554 fic = get_file_information (child);
555 GNUNET_break (GNUNET_OK == GNUNET_FS_file_information_add (fi, fic));
560 fi = GNUNET_FS_file_information_create_from_file (ctx, NULL,
562 item->ksk_uri, item->meta,
571 * We've finished scanning the directory and optimized the meta data.
572 * Begin the publication process.
574 * @param directory_scan_result result from the directory scan, freed in this function
577 directory_trim_complete (struct GNUNET_FS_ShareTreeItem *directory_scan_result)
579 struct GNUNET_FS_FileInformation *fi;
580 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv;
582 fi = get_file_information (directory_scan_result);
583 GNUNET_FS_share_tree_free (directory_scan_result);
588 _("Could not publish\n"));
589 GNUNET_SCHEDULER_shutdown ();
593 GNUNET_FS_file_information_inspect (fi, &publish_inspector, NULL);
596 GNUNET_FS_file_information_destroy (fi, NULL, NULL);
597 GNUNET_SCHEDULER_shutdown ();
600 if (NULL == namespace)
603 priv = GNUNET_IDENTITY_ego_get_private_key (namespace);
604 pc = GNUNET_FS_publish_start (ctx, fi,
605 priv, this_id, next_id,
607 GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY :
608 GNUNET_FS_PUBLISH_OPTION_NONE);
613 _("Could not start publishing.\n"));
614 GNUNET_SCHEDULER_shutdown ();
622 * Function called by the directory scanner as we build the tree
623 * that we will need to publish later.
626 * @param filename which file we are making progress on
627 * @param is_directory #GNUNET_YES if this is a directory,
628 * #GNUNET_NO if this is a file
629 * #GNUNET_SYSERR if it is neither (or unknown)
630 * @param reason kind of progress we are making
633 directory_scan_cb (void *cls,
634 const char *filename,
636 enum GNUNET_FS_DirScannerProgressUpdateReason reason)
638 struct GNUNET_FS_ShareTreeItem *directory_scan_result;
642 case GNUNET_FS_DIRSCANNER_FILE_START:
645 if (is_directory == GNUNET_YES)
647 _("Scanning directory `%s'.\n"),
651 _("Scanning file `%s'.\n"),
655 case GNUNET_FS_DIRSCANNER_FILE_IGNORED:
657 _("There was trouble processing file `%s', skipping it.\n"),
660 case GNUNET_FS_DIRSCANNER_ALL_COUNTED:
664 _("Preprocessing complete.\n"));
666 case GNUNET_FS_DIRSCANNER_EXTRACT_FINISHED:
669 _("Extracting meta data from file `%s' complete.\n"),
672 case GNUNET_FS_DIRSCANNER_FINISHED:
676 _("Meta data extraction has finished.\n"));
677 directory_scan_result = GNUNET_FS_directory_scan_get_result (ds);
679 GNUNET_FS_share_tree_trim (directory_scan_result);
680 directory_trim_complete (directory_scan_result);
682 case GNUNET_FS_DIRSCANNER_INTERNAL_ERROR:
685 _("Internal error scanning directory.\n"));
686 if (kill_task != NULL)
688 GNUNET_SCHEDULER_cancel (kill_task);
691 kill_task = GNUNET_SCHEDULER_add_now (&stop_scanner_task, NULL);
702 * Continuation proceeding with initialization after identity subsystem
703 * has been initialized.
705 * @param args0 filename to publish
708 identity_continuation (const char *args0)
713 if ( (NULL != pseudonym) &&
714 (NULL == namespace) )
717 _("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"),
731 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"),
878 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
881 if (NULL != pseudonym)
882 identity = GNUNET_IDENTITY_connect (cfg,
886 identity_continuation (args[0]);
891 * The main function to publish content to GNUnet.
893 * @param argc number of arguments from the command line
894 * @param argv command line arguments
895 * @return 0 ok, 1 on error
898 main (int argc, char *const *argv)
900 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
901 {'a', "anonymity", "LEVEL",
902 gettext_noop ("set the desired LEVEL of sender-anonymity"),
903 1, &GNUNET_GETOPT_set_uint, &bo.anonymity_level},
904 {'d', "disable-creation-time", NULL,
906 ("disable adding the creation time to the metadata of the uploaded file"),
907 0, &GNUNET_GETOPT_set_one, &do_disable_creation_time},
908 {'D', "disable-extractor", NULL,
909 gettext_noop ("do not use libextractor to add keywords or metadata"),
910 0, &GNUNET_GETOPT_set_one, &disable_extractor},
911 {'e', "extract", NULL,
913 ("print list of extracted keywords that would be used, but do not perform upload"),
914 0, &GNUNET_GETOPT_set_one, &extract_only},
915 {'k', "key", "KEYWORD",
917 ("add an additional keyword for the top-level file or directory"
918 " (this option can be specified multiple times)"),
919 1, &GNUNET_FS_getopt_set_keywords, &topKeywords},
920 {'m', "meta", "TYPE:VALUE",
921 gettext_noop ("set the meta-data for the given TYPE to the given VALUE"),
922 1, &GNUNET_FS_getopt_set_metadata, &meta},
923 {'n', "noindex", NULL,
924 gettext_noop ("do not index, perform full insertion (stores entire "
925 "file in encrypted form in GNUnet database)"),
926 0, &GNUNET_GETOPT_set_one, &do_insert},
929 ("specify ID of an updated version to be published in the future"
930 " (for namespace insertions only)"),
931 1, &GNUNET_GETOPT_set_string, &next_id},
932 {'p', "priority", "PRIORITY",
933 gettext_noop ("specify the priority of the content"),
934 1, &GNUNET_GETOPT_set_uint, &bo.content_priority},
935 {'P', "pseudonym", "NAME",
937 ("publish the files under the pseudonym NAME (place file into namespace)"),
938 1, &GNUNET_GETOPT_set_string, &pseudonym},
939 {'r', "replication", "LEVEL",
940 gettext_noop ("set the desired replication LEVEL"),
941 1, &GNUNET_GETOPT_set_uint, &bo.replication_level},
942 {'s', "simulate-only", NULL,
943 gettext_noop ("only simulate the process but do not do any "
944 "actual publishing (useful to compute URIs)"),
945 0, &GNUNET_GETOPT_set_one, &do_simulate},
947 gettext_noop ("set the ID of this version of the publication"
948 " (for namespace insertions only)"),
949 1, &GNUNET_GETOPT_set_string, &this_id},
951 gettext_noop ("URI to be published (can be used instead of passing a "
952 "file to add keywords to the file with the respective URI)"),
953 1, &GNUNET_GETOPT_set_string, &uri_string},
954 {'V', "verbose", NULL,
955 gettext_noop ("be verbose (print progress information)"),
956 0, &GNUNET_GETOPT_set_one, &verbose},
957 GNUNET_GETOPT_OPTION_END
960 GNUNET_TIME_year_to_time (GNUNET_TIME_get_current_year () + 2);
962 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
965 GNUNET_PROGRAM_run (argc, argv, "gnunet-publish [OPTIONS] FILENAME",
967 ("Publish a file or directory on GNUnet"),
968 options, &run, NULL)) ? ret : 1;
969 GNUNET_free ((void*) argv);
973 /* end of gnunet-publish.c */