2 This file is part of GNUnet.
3 (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009, 2010, 2011 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"
35 static const struct GNUNET_CONFIGURATION_Handle *cfg;
37 static struct GNUNET_FS_Handle *ctx;
39 static struct GNUNET_FS_PublishContext *pc;
41 static struct GNUNET_CONTAINER_MetaData *meta;
43 static struct GNUNET_FS_Uri *topKeywords;
45 static struct GNUNET_FS_Uri *uri;
47 static struct GNUNET_FS_BlockOptions bo = { { 0LL }, 1, 365, 1 };
49 static char *uri_string;
55 static char *pseudonym;
59 static int disable_extractor;
61 static int do_simulate;
63 static int extract_only;
65 static int do_disable_creation_time;
67 static GNUNET_SCHEDULER_TaskIdentifier kill_task;
71 do_stop_task (void *cls,
72 const struct GNUNET_SCHEDULER_TaskContext *tc)
74 struct GNUNET_FS_PublishContext *p;
80 GNUNET_FS_publish_stop (p);
83 GNUNET_CONTAINER_meta_data_destroy (meta);
91 * Called by FS client to give information about the progress of an
95 * @param info details about the event, specifying the event type
96 * and various bits about the event
97 * @return client-context (for the next progress call
98 * for this operation; should be set to NULL for
99 * SUSPEND and STOPPED events). The value returned
100 * will be passed to future callbacks in the respective
101 * field in the GNUNET_FS_ProgressInfo struct.
104 progress_cb (void *cls,
105 const struct GNUNET_FS_ProgressInfo *info)
109 switch (info->status)
111 case GNUNET_FS_STATUS_PUBLISH_START:
113 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
116 s = GNUNET_STRINGS_relative_time_to_string(info->value.publish.eta);
118 _("Publishing `%s' at %llu/%llu (%s remaining)\n"),
119 info->value.publish.filename,
120 (unsigned long long) info->value.publish.completed,
121 (unsigned long long) info->value.publish.size,
126 case GNUNET_FS_STATUS_PUBLISH_ERROR:
128 _("Error publishing: %s.\n"),
129 info->value.publish.specifics.error.message);
130 if (kill_task != GNUNET_SCHEDULER_NO_TASK)
132 GNUNET_SCHEDULER_cancel (kill_task);
133 kill_task = GNUNET_SCHEDULER_NO_TASK;
135 GNUNET_SCHEDULER_add_continuation (&do_stop_task,
137 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
139 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
141 _("Publishing `%s' done.\n"),
142 info->value.publish.filename);
143 s = GNUNET_FS_uri_to_string (info->value.publish.specifics.completed.chk_uri);
148 if (info->value.publish.pctx == NULL)
150 if (kill_task != GNUNET_SCHEDULER_NO_TASK)
152 GNUNET_SCHEDULER_cancel (kill_task);
153 kill_task = GNUNET_SCHEDULER_NO_TASK;
155 GNUNET_SCHEDULER_add_continuation (&do_stop_task,
157 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
160 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
161 GNUNET_break (NULL == pc);
165 _("Unexpected status: %d\n"),
169 return ""; /* non-null */
174 * Print metadata entries (except binary
175 * metadata and the filename).
178 * @param plugin_name name of the plugin that generated the meta data
179 * @param type type of the meta data
180 * @param format format of data
181 * @param data_mime_type mime type of data
182 * @param data value of the meta data
183 * @param data_size number of bytes in data
187 meta_printer (void *cls,
188 const char *plugin_name,
189 enum EXTRACTOR_MetaType type,
190 enum EXTRACTOR_MetaFormat format,
191 const char *data_mime_type,
195 if ( (format != EXTRACTOR_METAFORMAT_UTF8) &&
196 (format != EXTRACTOR_METAFORMAT_C_STRING) )
198 if (type == EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME)
202 EXTRACTOR_metatype_to_string (type),
209 * Iterator printing keywords
212 * @param keyword the keyword
213 * @param is_mandatory is the keyword mandatory (in a search)
214 * @return GNUNET_OK to continue to iterate, GNUNET_SYSERR to abort
218 keyword_printer (void *cls,
230 * Function called on all entries before the publication. This is
231 * where we perform modifications to the default based on command-line
235 * @param fi the entry in the publish-structure
236 * @param length length of the file or directory
237 * @param m metadata for the file or directory (can be modified)
238 * @param uri pointer to the keywords that will be used for this entry (can be modified)
239 * @param bo block options
240 * @param do_index should we index?
241 * @param client_info pointer to client context set upon creation (can be modified)
242 * @return GNUNET_OK to continue, GNUNET_NO to remove
243 * this entry from the directory, GNUNET_SYSERR
244 * to abort the iteration
247 publish_inspector (void *cls,
248 struct GNUNET_FS_FileInformation *fi,
250 struct GNUNET_CONTAINER_MetaData *m,
251 struct GNUNET_FS_Uri **uri,
252 struct GNUNET_FS_BlockOptions *bo,
258 struct GNUNET_FS_Uri *new_uri;
262 if (NULL != topKeywords)
266 new_uri = GNUNET_FS_uri_ksk_merge (topKeywords,
268 GNUNET_FS_uri_destroy (*uri);
270 GNUNET_FS_uri_destroy (topKeywords);
280 GNUNET_CONTAINER_meta_data_merge (m, meta);
281 GNUNET_CONTAINER_meta_data_destroy (meta);
284 if (! do_disable_creation_time)
285 GNUNET_CONTAINER_meta_data_add_publication_date (m);
288 fn = GNUNET_CONTAINER_meta_data_get_by_type (m,
289 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
290 fs = GNUNET_STRINGS_byte_size_fancy (length);
292 _("Meta data for file `%s' (%s)\n"),
295 GNUNET_CONTAINER_meta_data_iterate (m,
299 _("Keywords for file `%s' (%s)\n"),
305 GNUNET_FS_uri_ksk_get_keywords (*uri,
308 fprintf (stdout, "\n");
310 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (m))
311 GNUNET_FS_file_information_inspect (fi,
319 uri_sks_continuation (void *cls,
320 const struct GNUNET_FS_Uri *ksk_uri,
330 GNUNET_FS_uri_destroy (uri);
332 GNUNET_FS_stop (ctx);
338 uri_ksk_continuation (void *cls,
339 const struct GNUNET_FS_Uri *ksk_uri,
342 struct GNUNET_FS_Namespace *ns;
351 if (pseudonym != NULL)
353 ns = GNUNET_FS_namespace_create (ctx,
358 _("Failed to create namespace `%s'\n"),
364 GNUNET_FS_publish_sks (ctx,
371 GNUNET_FS_PUBLISH_OPTION_NONE,
372 uri_sks_continuation,
374 GNUNET_assert (GNUNET_OK ==
375 GNUNET_FS_namespace_delete (ns, GNUNET_NO));
379 GNUNET_FS_uri_destroy (uri);
381 GNUNET_FS_stop (ctx);
387 * Main function that will be run by the scheduler.
390 * @param args remaining command-line arguments
391 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
392 * @param c configuration
398 const struct GNUNET_CONFIGURATION_Handle *c)
400 struct GNUNET_FS_FileInformation *fi;
401 struct GNUNET_FS_Namespace *namespace;
402 struct EXTRACTOR_PluginList *plugins;
403 struct GNUNET_FS_Uri *keywords;
408 /* check arguments */
409 if ((uri_string != NULL) && (extract_only))
411 printf (_("Cannot extract metadata from a URI!\n"));
415 if ( ( (uri_string == NULL) || (extract_only) )
416 && ( (args[0] == NULL) || (args[1] != NULL) ) )
419 ("You must specify one and only one filename for insertion.\n"));
423 if ((uri_string != NULL) && (args[0] != NULL))
425 printf (_("You must NOT specify an URI and a filename.\n"));
429 if (pseudonym != NULL)
434 _("Option `%s' is required when using option `%s'.\n"),
441 { /* ordinary insertion checks */
445 _("Option `%s' makes no sense without option `%s'.\n"),
453 _("Option `%s' makes no sense without option `%s'.\n"),
460 ctx = GNUNET_FS_start (cfg,
464 GNUNET_FS_FLAGS_NONE,
465 GNUNET_FS_OPTIONS_END);
469 _("Could not initialize `%s' subsystem.\n"),
475 if (NULL != pseudonym)
477 namespace = GNUNET_FS_namespace_create (ctx,
479 if (NULL == namespace)
482 _("Could not create namespace `%s'\n"),
484 GNUNET_FS_stop (ctx);
489 if (NULL != uri_string)
492 uri = GNUNET_FS_uri_parse (uri_string,
497 _("Failed to parse URI: %s\n"),
500 if (namespace != NULL)
501 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
502 GNUNET_FS_stop (ctx);
506 GNUNET_FS_publish_ksk (ctx,
511 GNUNET_FS_PUBLISH_OPTION_NONE,
512 &uri_ksk_continuation,
514 if (namespace != NULL)
515 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
519 if (! disable_extractor)
521 plugins = EXTRACTOR_plugin_add_defaults (EXTRACTOR_OPTION_DEFAULT_POLICY);
523 GNUNET_CONFIGURATION_get_value_string (cfg, "FS", "EXTRACTORS",
527 plugins = EXTRACTOR_plugin_add_config (plugins, ex, EXTRACTOR_OPTION_DEFAULT_POLICY);
532 GNUNET_assert (NULL != args[0]);
533 if (0 != STAT (args[0], &sbuf))
535 GNUNET_asprintf (&emsg,
536 _("Could not access file: %s\n"),
540 else if (S_ISDIR (sbuf.st_mode))
542 fi = GNUNET_FS_file_information_create_from_directory (ctx,
545 &GNUNET_FS_directory_scanner_default,
554 meta = GNUNET_CONTAINER_meta_data_create ();
555 GNUNET_FS_meta_data_extract_from_file (meta,
558 keywords = GNUNET_FS_uri_ksk_create_from_meta_data (meta);
559 fi = GNUNET_FS_file_information_create_from_file (ctx,
566 GNUNET_break (fi != NULL);
567 GNUNET_FS_uri_destroy (keywords);
569 EXTRACTOR_plugin_remove_all (plugins);
573 _("Could not publish `%s': %s\n"),
577 if (namespace != NULL)
578 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
579 GNUNET_FS_stop (ctx);
583 GNUNET_FS_file_information_inspect (fi,
588 if (namespace != NULL)
589 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
590 GNUNET_FS_file_information_destroy (fi, NULL, NULL);
591 GNUNET_FS_stop (ctx);
594 pc = GNUNET_FS_publish_start (ctx,
600 ? GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY
601 : GNUNET_FS_PUBLISH_OPTION_NONE);
605 _("Could not start publishing.\n"));
606 GNUNET_FS_stop (ctx);
610 kill_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
619 * The main function to publish content to GNUnet.
621 * @param argc number of arguments from the command line
622 * @param argv command line arguments
623 * @return 0 ok, 1 on error
626 main (int argc, char *const *argv)
628 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
629 {'a', "anonymity", "LEVEL",
630 gettext_noop ("set the desired LEVEL of sender-anonymity"),
631 1, &GNUNET_GETOPT_set_uint, &bo.anonymity_level},
632 {'d', "disable-creation-time", NULL,
634 ("disable adding the creation time to the metadata of the uploaded file"),
635 0, &GNUNET_GETOPT_set_one, &do_disable_creation_time},
636 {'D', "disable-extractor", NULL,
638 ("do not use libextractor to add keywords or metadata"),
639 0, &GNUNET_GETOPT_set_one, &disable_extractor},
640 {'e', "extract", NULL,
642 ("print list of extracted keywords that would be used, but do not perform upload"),
643 0, &GNUNET_GETOPT_set_one, &extract_only},
644 {'k', "key", "KEYWORD",
646 ("add an additional keyword for the top-level file or directory"
647 " (this option can be specified multiple times)"),
648 1, &GNUNET_FS_getopt_set_keywords, &topKeywords},
649 {'m', "meta", "TYPE:VALUE",
650 gettext_noop ("set the meta-data for the given TYPE to the given VALUE"),
651 1, &GNUNET_FS_getopt_set_metadata, &meta},
652 {'n', "noindex", NULL,
653 gettext_noop ("do not index, perform full insertion (stores entire "
654 "file in encrypted form in GNUnet database)"),
655 0, &GNUNET_GETOPT_set_one, &do_insert},
658 ("specify ID of an updated version to be published in the future"
659 " (for namespace insertions only)"),
660 1, &GNUNET_GETOPT_set_string, &next_id},
661 {'p', "priority", "PRIORITY",
662 gettext_noop ("specify the priority of the content"),
663 1, &GNUNET_GETOPT_set_uint, &bo.content_priority},
664 {'P', "pseudonym", "NAME",
666 ("publish the files under the pseudonym NAME (place file into namespace)"),
667 1, &GNUNET_GETOPT_set_string, &pseudonym},
668 {'r', "replication", "LEVEL",
669 gettext_noop ("set the desired replication LEVEL"),
670 1, &GNUNET_GETOPT_set_uint, &bo.replication_level},
671 {'s', "simulate-only", NULL,
672 gettext_noop ("only simulate the process but do not do any "
673 "actual publishing (useful to compute URIs)"),
674 0, &GNUNET_GETOPT_set_one, &do_simulate},
676 gettext_noop ("set the ID of this version of the publication"
677 " (for namespace insertions only)"),
678 1, &GNUNET_GETOPT_set_string, &this_id},
680 gettext_noop ("URI to be published (can be used instead of passing a "
681 "file to add keywords to the file with the respective URI)"),
682 1, &GNUNET_GETOPT_set_string, &uri_string},
683 {'V', "verbose", NULL,
684 gettext_noop ("be verbose (print progress information)"),
685 0, &GNUNET_GETOPT_set_one, &verbose},
686 GNUNET_GETOPT_OPTION_END
688 bo.expiration_time = GNUNET_FS_year_to_time (GNUNET_FS_get_current_year () + 2);
690 GNUNET_PROGRAM_run (argc,
694 ("Publish files on GNUnet."),
695 options, &run, NULL)) ? ret : 1;
698 /* end of gnunet-publish.c */