2 This file is part of GNUnet.
3 (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 GNUNET_SCHEDULER_TaskIdentifier 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, const struct GNUNET_SCHEDULER_TaskContext *tc)
159 struct GNUNET_FS_PublishContext *p;
161 kill_task = GNUNET_SCHEDULER_NO_TASK;
166 GNUNET_FS_publish_stop (p);
170 GNUNET_CONTAINER_meta_data_destroy (meta);
177 * Stop the directory scanner (we had an error).
180 * @param tc scheduler context
183 stop_scanner_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
185 kill_task = GNUNET_SCHEDULER_NO_TASK;
188 GNUNET_FS_directory_scan_abort (ds);
191 if (NULL != identity)
193 GNUNET_IDENTITY_disconnect (identity);
196 GNUNET_FS_stop (ctx);
203 * Called by FS client to give information about the progress of an
207 * @param info details about the event, specifying the event type
208 * and various bits about the event
209 * @return client-context (for the next progress call
210 * for this operation; should be set to NULL for
211 * SUSPEND and STOPPED events). The value returned
212 * will be passed to future callbacks in the respective
213 * field in the GNUNET_FS_ProgressInfo struct.
216 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
221 switch (info->status)
223 case GNUNET_FS_STATUS_PUBLISH_START:
225 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
228 s = GNUNET_STRINGS_relative_time_to_string (info->value.publish.eta,
231 _("Publishing `%s' at %llu/%llu (%s remaining)\n"),
232 info->value.publish.filename,
233 (unsigned long long) info->value.publish.completed,
234 (unsigned long long) info->value.publish.size, s);
237 case GNUNET_FS_STATUS_PUBLISH_PROGRESS_DIRECTORY:
240 s = GNUNET_STRINGS_relative_time_to_string (info->value.publish.specifics.progress_directory.eta,
243 _("Publishing `%s' at %llu/%llu (%s remaining)\n"),
244 info->value.publish.filename,
245 (unsigned long long) info->value.publish.specifics.progress_directory.completed,
246 (unsigned long long) info->value.publish.specifics.progress_directory.total, s);
249 case GNUNET_FS_STATUS_PUBLISH_ERROR:
250 FPRINTF (stderr, _("Error publishing: %s.\n"),
251 info->value.publish.specifics.error.message);
252 if (kill_task != GNUNET_SCHEDULER_NO_TASK)
254 GNUNET_SCHEDULER_cancel (kill_task);
255 kill_task = GNUNET_SCHEDULER_NO_TASK;
257 kill_task = GNUNET_SCHEDULER_add_now (&do_stop_task, NULL);
259 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
260 FPRINTF (stdout, _("Publishing `%s' done.\n"),
261 info->value.publish.filename);
262 suri = GNUNET_FS_uri_to_string (info->value.publish.specifics.
264 FPRINTF (stdout, _("URI is `%s'.\n"), suri);
266 if (NULL == info->value.publish.pctx)
268 if (GNUNET_SCHEDULER_NO_TASK != kill_task)
270 GNUNET_SCHEDULER_cancel (kill_task);
271 kill_task = GNUNET_SCHEDULER_NO_TASK;
273 kill_task = GNUNET_SCHEDULER_add_now (&do_stop_task, NULL);
277 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
278 GNUNET_break (NULL == pc);
280 case GNUNET_FS_STATUS_UNINDEX_PROGRESS:
282 case GNUNET_FS_STATUS_UNINDEX_COMPLETED:
283 FPRINTF (stderr, "%s", _("Cleanup after abort complete.\n"));
286 FPRINTF (stderr, _("Unexpected status: %d\n"), info->status);
289 return ""; /* non-null */
294 * Print metadata entries (except binary
295 * metadata and the filename).
298 * @param plugin_name name of the plugin that generated the meta data
299 * @param type type of the meta data
300 * @param format format of data
301 * @param data_mime_type mime type of data
302 * @param data value of the meta data
303 * @param data_size number of bytes in data
307 meta_printer (void *cls, const char *plugin_name, enum EXTRACTOR_MetaType type,
308 enum EXTRACTOR_MetaFormat format, const char *data_mime_type,
309 const char *data, size_t data_size)
311 if ((EXTRACTOR_METAFORMAT_UTF8 != format) &&
312 (EXTRACTOR_METAFORMAT_C_STRING != format))
314 if (EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME == type)
316 FPRINTF (stdout, "\t%s - %s\n", EXTRACTOR_metatype_to_string (type), data);
322 * Iterator printing keywords
325 * @param keyword the keyword
326 * @param is_mandatory is the keyword mandatory (in a search)
327 * @return GNUNET_OK to continue to iterate, GNUNET_SYSERR to abort
330 keyword_printer (void *cls, const char *keyword, int is_mandatory)
332 FPRINTF (stdout, "\t%s\n", keyword);
338 * Function called on all entries before the publication. This is
339 * where we perform modifications to the default based on command-line
343 * @param fi the entry in the publish-structure
344 * @param length length of the file or directory
345 * @param m metadata for the file or directory (can be modified)
346 * @param uri pointer to the keywords that will be used for this entry (can be modified)
347 * @param bo block options
348 * @param do_index should we index?
349 * @param client_info pointer to client context set upon creation (can be modified)
350 * @return GNUNET_OK to continue, GNUNET_NO to remove
351 * this entry from the directory, GNUNET_SYSERR
352 * to abort the iteration
355 publish_inspector (void *cls, struct GNUNET_FS_FileInformation *fi,
356 uint64_t length, struct GNUNET_CONTAINER_MetaData *m,
357 struct GNUNET_FS_Uri **uri,
358 struct GNUNET_FS_BlockOptions *bo, int *do_index,
363 struct GNUNET_FS_Uri *new_uri;
367 if ( (disable_extractor) &&
370 GNUNET_FS_uri_destroy (*uri);
373 if (NULL != topKeywords)
377 new_uri = GNUNET_FS_uri_ksk_merge (topKeywords, *uri);
378 GNUNET_FS_uri_destroy (*uri);
380 GNUNET_FS_uri_destroy (topKeywords);
390 GNUNET_CONTAINER_meta_data_merge (m, meta);
391 GNUNET_CONTAINER_meta_data_destroy (meta);
394 if (!do_disable_creation_time)
395 GNUNET_CONTAINER_meta_data_add_publication_date (m);
398 fn = GNUNET_CONTAINER_meta_data_get_by_type (m,
399 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
400 fs = GNUNET_STRINGS_byte_size_fancy (length);
401 FPRINTF (stdout, _("Meta data for file `%s' (%s)\n"), fn, fs);
402 GNUNET_CONTAINER_meta_data_iterate (m, &meta_printer, NULL);
403 FPRINTF (stdout, _("Keywords for file `%s' (%s)\n"), fn, fs);
407 GNUNET_FS_uri_ksk_get_keywords (*uri, &keyword_printer, NULL);
408 FPRINTF (stdout, "%s", "\n");
410 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (m))
411 GNUNET_FS_file_information_inspect (fi, &publish_inspector, fi);
417 * Function called upon completion of the publishing
418 * of the UBLOCK for the SKS URI. As this is the last
419 * step, stop our interaction with FS (clean up).
421 * @param cls NULL (closure)
422 * @param sks_uri URI for the block that was published
423 * @param emsg error message, NULL on success
426 uri_sks_continuation (void *cls, const struct GNUNET_FS_Uri *sks_uri,
431 FPRINTF (stderr, "%s\n", emsg);
434 GNUNET_FS_uri_destroy (uri);
436 GNUNET_FS_stop (ctx);
442 * Function called upon completion of the publishing
443 * of the UBLOCK for the KSK URI. Continue with
444 * publishing the SKS URI (if applicable) or clean up.
446 * @param cls NULL (closure)
447 * @param ksk_uri URI for the block that was published
448 * @param emsg error message, NULL on success
451 uri_ksk_continuation (void *cls, const struct GNUNET_FS_Uri *ksk_uri,
454 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv;
458 FPRINTF (stderr, "%s\n", emsg);
461 if (NULL != namespace)
463 priv = GNUNET_IDENTITY_ego_get_private_key (namespace);
464 GNUNET_FS_publish_sks (ctx, priv, this_id, next_id, meta, uri, &bo,
465 GNUNET_FS_PUBLISH_OPTION_NONE,
466 &uri_sks_continuation, NULL);
469 GNUNET_FS_uri_destroy (uri);
471 GNUNET_FS_stop (ctx);
477 * Iterate over the results from the directory scan and extract
478 * the desired information for the publishing operation.
480 * @param item root with the data from the directroy scan
481 * @return handle with the information for the publishing operation
483 static struct GNUNET_FS_FileInformation *
484 get_file_information (struct GNUNET_FS_ShareTreeItem *item)
486 struct GNUNET_FS_FileInformation *fi;
487 struct GNUNET_FS_FileInformation *fic;
488 struct GNUNET_FS_ShareTreeItem *child;
490 if (GNUNET_YES == item->is_directory)
492 GNUNET_CONTAINER_meta_data_delete (item->meta,
493 EXTRACTOR_METATYPE_MIMETYPE,
495 GNUNET_FS_meta_data_make_directory (item->meta);
496 if (NULL == item->ksk_uri)
498 const char *mime = GNUNET_FS_DIRECTORY_MIME;
499 item->ksk_uri = GNUNET_FS_uri_ksk_create_from_args (1, &mime);
502 GNUNET_FS_uri_ksk_add_keyword (item->ksk_uri, GNUNET_FS_DIRECTORY_MIME,
504 fi = GNUNET_FS_file_information_create_empty_directory (ctx, NULL,
507 &bo, item->filename);
508 for (child = item->children_head; child; child = child->next)
510 fic = get_file_information (child);
511 GNUNET_break (GNUNET_OK == GNUNET_FS_file_information_add (fi, fic));
516 fi = GNUNET_FS_file_information_create_from_file (ctx, NULL,
518 item->ksk_uri, item->meta,
527 * We've finished scanning the directory and optimized the meta data.
528 * Begin the publication process.
530 * @param directory_scan_result result from the directory scan, freed in this function
533 directory_trim_complete (struct GNUNET_FS_ShareTreeItem *directory_scan_result)
535 struct GNUNET_FS_FileInformation *fi;
536 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv;
538 fi = get_file_information (directory_scan_result);
539 GNUNET_FS_share_tree_free (directory_scan_result);
542 FPRINTF (stderr, "%s", _("Could not publish\n"));
543 GNUNET_SCHEDULER_shutdown ();
547 GNUNET_FS_file_information_inspect (fi, &publish_inspector, NULL);
550 GNUNET_FS_file_information_destroy (fi, NULL, NULL);
551 GNUNET_SCHEDULER_shutdown ();
554 if (NULL == namespace)
557 priv = GNUNET_IDENTITY_ego_get_private_key (namespace);
558 pc = GNUNET_FS_publish_start (ctx, fi,
559 priv, this_id, next_id,
561 GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY :
562 GNUNET_FS_PUBLISH_OPTION_NONE);
565 FPRINTF (stderr, "%s", _("Could not start publishing.\n"));
566 GNUNET_SCHEDULER_shutdown ();
574 * Function called by the directory scanner as we build the tree
575 * that we will need to publish later.
578 * @param filename which file we are making progress on
579 * @param is_directory GNUNET_YES if this is a directory,
580 * GNUNET_NO if this is a file
581 * GNUNET_SYSERR if it is neither (or unknown)
582 * @param reason kind of progress we are making
585 directory_scan_cb (void *cls,
586 const char *filename,
588 enum GNUNET_FS_DirScannerProgressUpdateReason reason)
590 struct GNUNET_FS_ShareTreeItem *directory_scan_result;
594 case GNUNET_FS_DIRSCANNER_FILE_START:
597 if (is_directory == GNUNET_YES)
598 FPRINTF (stdout, _("Scanning directory `%s'.\n"), filename);
600 FPRINTF (stdout, _("Scanning file `%s'.\n"), filename);
603 case GNUNET_FS_DIRSCANNER_FILE_IGNORED:
605 _("There was trouble processing file `%s', skipping it.\n"),
608 case GNUNET_FS_DIRSCANNER_ALL_COUNTED:
610 FPRINTF (stdout, "%s", _("Preprocessing complete.\n"));
612 case GNUNET_FS_DIRSCANNER_EXTRACT_FINISHED:
614 FPRINTF (stdout, _("Extracting meta data from file `%s' complete.\n"), filename);
616 case GNUNET_FS_DIRSCANNER_FINISHED:
618 FPRINTF (stdout, "%s", _("Meta data extraction has finished.\n"));
619 directory_scan_result = GNUNET_FS_directory_scan_get_result (ds);
621 GNUNET_FS_share_tree_trim (directory_scan_result);
622 directory_trim_complete (directory_scan_result);
624 case GNUNET_FS_DIRSCANNER_INTERNAL_ERROR:
625 FPRINTF (stdout, "%s", _("Internal error scanning directory.\n"));
626 if (kill_task != GNUNET_SCHEDULER_NO_TASK)
628 GNUNET_SCHEDULER_cancel (kill_task);
629 kill_task = GNUNET_SCHEDULER_NO_TASK;
631 kill_task = GNUNET_SCHEDULER_add_now (&stop_scanner_task, NULL);
642 * Continuation proceeding with initialization after identity subsystem
643 * has been initialized.
645 * @param args0 filename to publish
648 identity_continuation (const char *args0)
653 if ( (NULL != pseudonym) &&
654 (NULL == namespace) )
656 FPRINTF (stderr, _("Selected pseudonym `%s' unknown\n"), pseudonym);
657 GNUNET_SCHEDULER_shutdown ();
660 if (NULL != uri_string)
663 if (NULL == (uri = GNUNET_FS_uri_parse (uri_string, &emsg)))
665 FPRINTF (stderr, _("Failed to parse URI: %s\n"), emsg);
667 GNUNET_SCHEDULER_shutdown ();
671 GNUNET_FS_publish_ksk (ctx, topKeywords, meta, uri, &bo,
672 GNUNET_FS_PUBLISH_OPTION_NONE, &uri_ksk_continuation,
677 GNUNET_CONFIGURATION_get_value_string (cfg, "FS", "EXTRACTORS", &ex))
679 if (0 != ACCESS (args0, R_OK))
682 _("Failed to access `%s': %s\n"),
685 GNUNET_free_non_null (ex);
688 ds = GNUNET_FS_directory_scan_start (args0,
691 &directory_scan_cb, NULL);
695 "%s", _("Failed to start meta directory scanner. Is gnunet-helper-publish-fs installed?\n"));
696 GNUNET_free_non_null (ex);
699 GNUNET_free_non_null (ex);
704 * Function called by identity service with known pseudonyms.
706 * @param cls closure with 'const char *' of filename to publish
707 * @param ego ego handle
708 * @param ctx context for application to store data for this ego
709 * (during the lifetime of this process, initially NULL)
710 * @param name name assigned by the user for this ego,
711 * NULL if the user just deleted the ego and it
712 * must thus no longer be used
715 identity_cb (void *cls,
716 struct GNUNET_IDENTITY_Ego *ego,
720 const char *args0 = cls;
724 identity_continuation (args0);
727 if (0 == strcmp (name, pseudonym))
733 * Main function that will be run by the scheduler.
736 * @param args remaining command-line arguments
737 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
738 * @param c configuration
741 run (void *cls, char *const *args, const char *cfgfile,
742 const struct GNUNET_CONFIGURATION_Handle *c)
744 /* check arguments */
745 if ((NULL != uri_string) && (extract_only))
747 printf (_("Cannot extract metadata from a URI!\n"));
751 if (((NULL == uri_string) || (extract_only)) &&
752 ((NULL == args[0]) || (NULL != args[1])))
754 printf (_("You must specify one and only one filename for insertion.\n"));
758 if ((NULL != uri_string) && (NULL != args[0]))
760 printf (_("You must NOT specify an URI and a filename.\n"));
764 if (NULL != pseudonym)
768 FPRINTF (stderr, _("Option `%s' is required when using option `%s'.\n"),
775 { /* ordinary insertion checks */
778 FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
785 FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
793 GNUNET_FS_start (cfg, "gnunet-publish", &progress_cb, NULL,
794 GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
797 FPRINTF (stderr, _("Could not initialize `%s' subsystem.\n"), "FS");
802 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_stop_task,
804 if (NULL != pseudonym)
805 identity = GNUNET_IDENTITY_connect (cfg,
806 &identity_cb, args[0]);
808 identity_continuation (args[0]);
813 * The main function to publish content to GNUnet.
815 * @param argc number of arguments from the command line
816 * @param argv command line arguments
817 * @return 0 ok, 1 on error
820 main (int argc, char *const *argv)
822 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
823 {'a', "anonymity", "LEVEL",
824 gettext_noop ("set the desired LEVEL of sender-anonymity"),
825 1, &GNUNET_GETOPT_set_uint, &bo.anonymity_level},
826 {'d', "disable-creation-time", NULL,
828 ("disable adding the creation time to the metadata of the uploaded file"),
829 0, &GNUNET_GETOPT_set_one, &do_disable_creation_time},
830 {'D', "disable-extractor", NULL,
831 gettext_noop ("do not use libextractor to add keywords or metadata"),
832 0, &GNUNET_GETOPT_set_one, &disable_extractor},
833 {'e', "extract", NULL,
835 ("print list of extracted keywords that would be used, but do not perform upload"),
836 0, &GNUNET_GETOPT_set_one, &extract_only},
837 {'k', "key", "KEYWORD",
839 ("add an additional keyword for the top-level file or directory"
840 " (this option can be specified multiple times)"),
841 1, &GNUNET_FS_getopt_set_keywords, &topKeywords},
842 {'m', "meta", "TYPE:VALUE",
843 gettext_noop ("set the meta-data for the given TYPE to the given VALUE"),
844 1, &GNUNET_FS_getopt_set_metadata, &meta},
845 {'n', "noindex", NULL,
846 gettext_noop ("do not index, perform full insertion (stores entire "
847 "file in encrypted form in GNUnet database)"),
848 0, &GNUNET_GETOPT_set_one, &do_insert},
851 ("specify ID of an updated version to be published in the future"
852 " (for namespace insertions only)"),
853 1, &GNUNET_GETOPT_set_string, &next_id},
854 {'p', "priority", "PRIORITY",
855 gettext_noop ("specify the priority of the content"),
856 1, &GNUNET_GETOPT_set_uint, &bo.content_priority},
857 {'P', "pseudonym", "NAME",
859 ("publish the files under the pseudonym NAME (place file into namespace)"),
860 1, &GNUNET_GETOPT_set_string, &pseudonym},
861 {'r', "replication", "LEVEL",
862 gettext_noop ("set the desired replication LEVEL"),
863 1, &GNUNET_GETOPT_set_uint, &bo.replication_level},
864 {'s', "simulate-only", NULL,
865 gettext_noop ("only simulate the process but do not do any "
866 "actual publishing (useful to compute URIs)"),
867 0, &GNUNET_GETOPT_set_one, &do_simulate},
869 gettext_noop ("set the ID of this version of the publication"
870 " (for namespace insertions only)"),
871 1, &GNUNET_GETOPT_set_string, &this_id},
873 gettext_noop ("URI to be published (can be used instead of passing a "
874 "file to add keywords to the file with the respective URI)"),
875 1, &GNUNET_GETOPT_set_string, &uri_string},
876 {'V', "verbose", NULL,
877 gettext_noop ("be verbose (print progress information)"),
878 0, &GNUNET_GETOPT_set_one, &verbose},
879 GNUNET_GETOPT_OPTION_END
882 GNUNET_TIME_year_to_time (GNUNET_TIME_get_current_year () + 2);
884 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
887 GNUNET_PROGRAM_run (argc, argv, "gnunet-publish [OPTIONS] FILENAME",
889 ("Publish a file or directory on GNUnet"),
890 options, &run, NULL)) ? ret : 1;
891 GNUNET_free ((void*) argv);
895 /* end of gnunet-publish.c */