2 This file is part of GNUnet.
3 (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009, 2010 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"
31 #define DEFAULT_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_YEARS, 2)
37 static const struct GNUNET_CONFIGURATION_Handle *cfg;
39 static struct GNUNET_FS_Handle *ctx;
41 static struct GNUNET_FS_PublishContext *pc;
43 static struct GNUNET_CONTAINER_MetaData *meta;
45 static struct GNUNET_FS_Uri *topKeywords;
47 static struct GNUNET_FS_Uri *uri;
49 static unsigned int anonymity = 1;
51 static unsigned int priority = 365;
53 static char *uri_string;
59 static char *pseudonym;
63 static int disable_extractor;
65 static int do_simulate;
67 static int extract_only;
69 static int do_disable_creation_time;
71 static GNUNET_SCHEDULER_TaskIdentifier kill_task;
75 do_stop_task (void *cls,
76 const struct GNUNET_SCHEDULER_TaskContext *tc)
78 struct GNUNET_FS_PublishContext *p;
84 GNUNET_FS_publish_stop (p);
87 GNUNET_CONTAINER_meta_data_destroy (meta);
95 * Called by FS client to give information about the progress of an
99 * @param info details about the event, specifying the event type
100 * and various bits about the event
101 * @return client-context (for the next progress call
102 * for this operation; should be set to NULL for
103 * SUSPEND and STOPPED events). The value returned
104 * will be passed to future callbacks in the respective
105 * field in the GNUNET_FS_ProgressInfo struct.
108 progress_cb (void *cls,
109 const struct GNUNET_FS_ProgressInfo *info)
113 switch (info->status)
115 case GNUNET_FS_STATUS_PUBLISH_START:
117 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
120 s = GNUNET_STRINGS_relative_time_to_string(info->value.publish.eta);
122 _("Publishing `%s' at %llu/%llu (%s remaining)\n"),
123 info->value.publish.filename,
124 (unsigned long long) info->value.publish.completed,
125 (unsigned long long) info->value.publish.size,
130 case GNUNET_FS_STATUS_PUBLISH_ERROR:
132 _("Error publishing: %s.\n"),
133 info->value.publish.specifics.error.message);
134 if (kill_task != GNUNET_SCHEDULER_NO_TASK)
136 GNUNET_SCHEDULER_cancel (kill_task);
137 kill_task = GNUNET_SCHEDULER_NO_TASK;
139 GNUNET_SCHEDULER_add_continuation (&do_stop_task,
141 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
143 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
145 _("Publishing `%s' done.\n"),
146 info->value.publish.filename);
147 s = GNUNET_FS_uri_to_string (info->value.publish.specifics.completed.chk_uri);
152 if (info->value.publish.pctx == NULL)
154 if (kill_task != GNUNET_SCHEDULER_NO_TASK)
156 GNUNET_SCHEDULER_cancel (kill_task);
157 kill_task = GNUNET_SCHEDULER_NO_TASK;
159 GNUNET_SCHEDULER_add_continuation (&do_stop_task,
161 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
164 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
165 GNUNET_break (NULL == pc);
169 _("Unexpected status: %d\n"),
173 return ""; /* non-null */
178 * Print metadata entries (except binary
179 * metadata and the filename).
182 * @param plugin_name name of the plugin that generated the meta data
183 * @param type type of the meta data
184 * @param format format of data
185 * @param data_mime_type mime type of data
186 * @param data value of the meta data
187 * @param data_size number of bytes in data
191 meta_printer (void *cls,
192 const char *plugin_name,
193 enum EXTRACTOR_MetaType type,
194 enum EXTRACTOR_MetaFormat format,
195 const char *data_mime_type,
199 if ( (format != EXTRACTOR_METAFORMAT_UTF8) &&
200 (format != EXTRACTOR_METAFORMAT_C_STRING) )
202 if (type == EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME)
206 EXTRACTOR_metatype_to_string (type),
213 * Iterator printing keywords
216 * @param keyword the keyword
217 * @param is_mandatory is the keyword mandatory (in a search)
218 * @return GNUNET_OK to continue to iterate, GNUNET_SYSERR to abort
222 keyword_printer (void *cls,
234 * Function called on all entries before the publication. This is
235 * where we perform modifications to the default based on command-line
239 * @param fi the entry in the publish-structure
240 * @param length length of the file or directory
241 * @param m metadata for the file or directory (can be modified)
242 * @param uri pointer to the keywords that will be used for this entry (can be modified)
243 * @param anonymity pointer to selected anonymity level (can be modified)
244 * @param priority pointer to selected priority (can be modified)
245 * @param do_index should we index?
246 * @param expirationTime pointer to selected expiration time (can be modified)
247 * @param client_info pointer to client context set upon creation (can be modified)
248 * @return GNUNET_OK to continue, GNUNET_NO to remove
249 * this entry from the directory, GNUNET_SYSERR
250 * to abort the iteration
253 publish_inspector (void *cls,
254 struct GNUNET_FS_FileInformation *fi,
256 struct GNUNET_CONTAINER_MetaData *m,
257 struct GNUNET_FS_Uri **uri,
258 unsigned int *anonymity,
259 unsigned int *priority,
261 struct GNUNET_TIME_Absolute *expirationTime,
266 struct GNUNET_FS_Uri *new_uri;
270 if (NULL != topKeywords)
274 new_uri = GNUNET_FS_uri_ksk_merge (topKeywords,
276 GNUNET_FS_uri_destroy (*uri);
278 GNUNET_FS_uri_destroy (topKeywords);
288 GNUNET_CONTAINER_meta_data_merge (m, meta);
289 GNUNET_CONTAINER_meta_data_destroy (meta);
292 if (! do_disable_creation_time)
293 GNUNET_CONTAINER_meta_data_add_publication_date (m);
296 fn = GNUNET_CONTAINER_meta_data_get_by_type (m,
297 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
298 fs = GNUNET_STRINGS_byte_size_fancy (length);
300 _("Meta data for file `%s' (%s)\n"),
303 GNUNET_CONTAINER_meta_data_iterate (m,
307 _("Keywords for file `%s' (%s)\n"),
313 GNUNET_FS_uri_ksk_get_keywords (*uri,
316 fprintf (stdout, "\n");
318 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (m))
319 GNUNET_FS_file_information_inspect (fi,
327 uri_sks_continuation (void *cls,
328 const struct GNUNET_FS_Uri *ksk_uri,
338 GNUNET_FS_uri_destroy (uri);
340 GNUNET_FS_stop (ctx);
346 uri_ksk_continuation (void *cls,
347 const struct GNUNET_FS_Uri *ksk_uri,
350 struct GNUNET_FS_Namespace *ns;
359 if (pseudonym != NULL)
361 ns = GNUNET_FS_namespace_create (ctx,
366 _("Failed to create namespace `%s'\n"),
372 GNUNET_FS_publish_sks (ctx,
378 GNUNET_TIME_relative_to_absolute (DEFAULT_EXPIRATION),
381 GNUNET_FS_PUBLISH_OPTION_NONE,
382 uri_sks_continuation,
384 GNUNET_assert (GNUNET_OK ==
385 GNUNET_FS_namespace_delete (ns, GNUNET_NO));
389 GNUNET_FS_uri_destroy (uri);
391 GNUNET_FS_stop (ctx);
397 * Main function that will be run by the scheduler.
400 * @param args remaining command-line arguments
401 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
402 * @param c configuration
408 const struct GNUNET_CONFIGURATION_Handle *c)
410 struct GNUNET_FS_FileInformation *fi;
411 struct GNUNET_FS_Namespace *namespace;
412 struct EXTRACTOR_PluginList *plugins;
413 struct GNUNET_FS_Uri *keywords;
418 /* check arguments */
419 if ((uri_string != NULL) && (extract_only))
421 printf (_("Cannot extract metadata from a URI!\n"));
425 if ( ( (uri_string == NULL) || (extract_only) )
426 && ( (args[0] == NULL) || (args[1] != NULL) ) )
429 ("You must specify one and only one filename for insertion.\n"));
433 if ((uri_string != NULL) && (args[0] != NULL))
435 printf (_("You must NOT specify an URI and a filename.\n"));
439 if (pseudonym != NULL)
444 _("Option `%s' is required when using option `%s'.\n"),
451 { /* ordinary insertion checks */
455 _("Option `%s' makes no sense without option `%s'.\n"),
463 _("Option `%s' makes no sense without option `%s'.\n"),
470 ctx = GNUNET_FS_start (cfg,
474 GNUNET_FS_FLAGS_NONE,
475 GNUNET_FS_OPTIONS_END);
479 _("Could not initialize `%s' subsystem.\n"),
485 if (NULL != pseudonym)
487 namespace = GNUNET_FS_namespace_create (ctx,
489 if (NULL == namespace)
492 _("Could not create namespace `%s'\n"),
494 GNUNET_FS_stop (ctx);
499 if (NULL != uri_string)
502 uri = GNUNET_FS_uri_parse (uri_string,
507 _("Failed to parse URI: %s\n"),
510 if (namespace != NULL)
511 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
512 GNUNET_FS_stop (ctx);
516 GNUNET_FS_publish_ksk (ctx,
520 GNUNET_TIME_relative_to_absolute (DEFAULT_EXPIRATION),
523 GNUNET_FS_PUBLISH_OPTION_NONE,
524 &uri_ksk_continuation,
526 if (namespace != NULL)
527 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
531 if (! disable_extractor)
533 plugins = EXTRACTOR_plugin_add_defaults (EXTRACTOR_OPTION_DEFAULT_POLICY);
535 GNUNET_CONFIGURATION_get_value_string (cfg, "FS", "EXTRACTORS",
539 plugins = EXTRACTOR_plugin_add_config (plugins, ex, EXTRACTOR_OPTION_DEFAULT_POLICY);
544 GNUNET_assert (NULL != args[0]);
545 if (0 != STAT (args[0], &sbuf))
547 GNUNET_asprintf (&emsg,
548 _("Could not access file: %s\n"),
552 else if (S_ISDIR (sbuf.st_mode))
554 fi = GNUNET_FS_file_information_create_from_directory (ctx,
557 &GNUNET_FS_directory_scanner_default,
562 GNUNET_TIME_relative_to_absolute (DEFAULT_EXPIRATION),
568 meta = GNUNET_CONTAINER_meta_data_create ();
569 GNUNET_FS_meta_data_extract_from_file (meta,
572 keywords = GNUNET_FS_uri_ksk_create_from_meta_data (meta);
573 fi = GNUNET_FS_file_information_create_from_file (ctx,
581 GNUNET_TIME_relative_to_absolute (DEFAULT_EXPIRATION));
582 GNUNET_break (fi != NULL);
583 GNUNET_FS_uri_destroy (keywords);
585 EXTRACTOR_plugin_remove_all (plugins);
589 _("Could not publish `%s': %s\n"),
593 if (namespace != NULL)
594 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
595 GNUNET_FS_stop (ctx);
599 GNUNET_FS_file_information_inspect (fi,
604 if (namespace != NULL)
605 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
606 GNUNET_FS_file_information_destroy (fi, NULL, NULL);
607 GNUNET_FS_stop (ctx);
610 pc = GNUNET_FS_publish_start (ctx,
616 ? GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY
617 : GNUNET_FS_PUBLISH_OPTION_NONE);
621 _("Could not start publishing.\n"));
622 GNUNET_FS_stop (ctx);
626 kill_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
635 * The main function to publish content to GNUnet.
637 * @param argc number of arguments from the command line
638 * @param argv command line arguments
639 * @return 0 ok, 1 on error
642 main (int argc, char *const *argv)
644 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
645 {'a', "anonymity", "LEVEL",
646 gettext_noop ("set the desired LEVEL of sender-anonymity"),
647 1, &GNUNET_GETOPT_set_uint, &anonymity},
648 {'d', "disable-creation-time", NULL,
650 ("disable adding the creation time to the metadata of the uploaded file"),
651 0, &GNUNET_GETOPT_set_one, &do_disable_creation_time},
652 {'D', "disable-extractor", NULL,
654 ("do not use libextractor to add keywords or metadata"),
655 0, &GNUNET_GETOPT_set_one, &disable_extractor},
656 {'e', "extract", NULL,
658 ("print list of extracted keywords that would be used, but do not perform upload"),
659 0, &GNUNET_GETOPT_set_one, &extract_only},
660 {'k', "key", "KEYWORD",
662 ("add an additional keyword for the top-level file or directory"
663 " (this option can be specified multiple times)"),
664 1, &GNUNET_FS_getopt_set_keywords, &topKeywords},
665 {'m', "meta", "TYPE:VALUE",
666 gettext_noop ("set the meta-data for the given TYPE to the given VALUE"),
667 1, &GNUNET_FS_getopt_set_metadata, &meta},
668 {'n', "noindex", NULL,
669 gettext_noop ("do not index, perform full insertion (stores entire "
670 "file in encrypted form in GNUnet database)"),
671 0, &GNUNET_GETOPT_set_one, &do_insert},
674 ("specify ID of an updated version to be published in the future"
675 " (for namespace insertions only)"),
676 1, &GNUNET_GETOPT_set_string, &next_id},
677 {'p', "priority", "PRIORITY",
678 gettext_noop ("specify the priority of the content"),
679 1, &GNUNET_GETOPT_set_uint, &priority},
680 {'P', "pseudonym", "NAME",
682 ("publish the files under the pseudonym NAME (place file into namespace)"),
683 1, &GNUNET_GETOPT_set_string, &pseudonym},
684 {'s', "simulate-only", NULL,
685 gettext_noop ("only simulate the process but do not do any "
686 "actual publishing (useful to compute URIs)"),
687 0, &GNUNET_GETOPT_set_one, &do_simulate},
689 gettext_noop ("set the ID of this version of the publication"
690 " (for namespace insertions only)"),
691 1, &GNUNET_GETOPT_set_string, &this_id},
693 gettext_noop ("URI to be published (can be used instead of passing a "
694 "file to add keywords to the file with the respective URI)"),
695 1, &GNUNET_GETOPT_set_string, &uri_string},
696 {'V', "verbose", NULL,
697 gettext_noop ("be verbose (print progress information)"),
698 0, &GNUNET_GETOPT_set_one, &verbose},
699 GNUNET_GETOPT_OPTION_END
702 GNUNET_PROGRAM_run (argc,
706 ("Publish files on GNUnet."),
707 options, &run, NULL)) ? ret : 1;
710 /* end of gnunet-publish.c */