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_FILENAME)
206 EXTRACTOR_metatype_to_string (type),
213 * Function called on all entries before the publication. This is
214 * where we perform modifications to the default based on command-line
218 * @param fi the entry in the publish-structure
219 * @param length length of the file or directory
220 * @param m metadata for the file or directory (can be modified)
221 * @param uri pointer to the keywords that will be used for this entry (can be modified)
222 * @param anonymity pointer to selected anonymity level (can be modified)
223 * @param priority pointer to selected priority (can be modified)
224 * @param do_index should we index?
225 * @param expirationTime pointer to selected expiration time (can be modified)
226 * @param client_info pointer to client context set upon creation (can be modified)
227 * @return GNUNET_OK to continue, GNUNET_NO to remove
228 * this entry from the directory, GNUNET_SYSERR
229 * to abort the iteration
232 publish_inspector (void *cls,
233 struct GNUNET_FS_FileInformation *fi,
235 struct GNUNET_CONTAINER_MetaData *m,
236 struct GNUNET_FS_Uri **uri,
237 unsigned int *anonymity,
238 unsigned int *priority,
240 struct GNUNET_TIME_Absolute *expirationTime,
245 struct GNUNET_FS_Uri *new_uri;
249 if (NULL != topKeywords)
253 new_uri = GNUNET_FS_uri_ksk_merge (topKeywords,
255 GNUNET_FS_uri_destroy (*uri);
257 GNUNET_FS_uri_destroy (topKeywords);
267 GNUNET_CONTAINER_meta_data_merge (m, meta);
268 GNUNET_CONTAINER_meta_data_destroy (meta);
271 if (! do_disable_creation_time)
272 GNUNET_CONTAINER_meta_data_add_publication_date (m);
275 fn = GNUNET_CONTAINER_meta_data_get_by_type (m,
276 EXTRACTOR_METATYPE_FILENAME);
277 fs = GNUNET_STRINGS_byte_size_fancy (length);
279 _("Keywords for file `%s' (%s)\n"),
284 GNUNET_CONTAINER_meta_data_iterate (m,
287 fprintf (stdout, "\n");
289 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (m))
290 GNUNET_FS_file_information_inspect (fi,
298 uri_sks_continuation (void *cls,
299 const struct GNUNET_FS_Uri *ksk_uri,
309 GNUNET_FS_uri_destroy (uri);
311 GNUNET_FS_stop (ctx);
317 uri_ksk_continuation (void *cls,
318 const struct GNUNET_FS_Uri *ksk_uri,
321 struct GNUNET_FS_Namespace *ns;
330 if (pseudonym != NULL)
332 ns = GNUNET_FS_namespace_create (ctx,
337 _("Failed to create namespace `%s'\n"),
343 GNUNET_FS_publish_sks (ctx,
349 GNUNET_TIME_relative_to_absolute (DEFAULT_EXPIRATION),
352 GNUNET_FS_PUBLISH_OPTION_NONE,
353 uri_sks_continuation,
355 GNUNET_assert (GNUNET_OK ==
356 GNUNET_FS_namespace_delete (ns, GNUNET_NO));
360 GNUNET_FS_uri_destroy (uri);
362 GNUNET_FS_stop (ctx);
368 * Main function that will be run by the scheduler.
371 * @param args remaining command-line arguments
372 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
373 * @param c configuration
379 const struct GNUNET_CONFIGURATION_Handle *c)
381 struct GNUNET_FS_FileInformation *fi;
382 struct GNUNET_FS_Namespace *namespace;
383 struct EXTRACTOR_PluginList *plugins;
388 /* check arguments */
389 if ((uri_string != NULL) && (extract_only))
391 printf (_("Cannot extract metadata from a URI!\n"));
395 if ( ( (uri_string == NULL) || (extract_only) )
396 && ( (args[0] == NULL) || (args[1] != NULL) ) )
399 ("You must specify one and only one filename for insertion.\n"));
403 if ((uri_string != NULL) && (args[0] != NULL))
405 printf (_("You must NOT specify an URI and a filename.\n"));
409 if (pseudonym != NULL)
414 _("Option `%s' is required when using option `%s'.\n"),
421 { /* ordinary insertion checks */
425 _("Option `%s' makes no sense without option `%s'.\n"),
433 _("Option `%s' makes no sense without option `%s'.\n"),
440 ctx = GNUNET_FS_start (cfg,
444 GNUNET_FS_FLAGS_NONE,
445 GNUNET_FS_OPTIONS_END);
449 _("Could not initialize `%s' subsystem.\n"),
455 if (NULL != pseudonym)
457 namespace = GNUNET_FS_namespace_create (ctx,
459 if (NULL == namespace)
462 _("Could not create namespace `%s'\n"),
464 GNUNET_FS_stop (ctx);
469 if (NULL != uri_string)
472 uri = GNUNET_FS_uri_parse (uri_string,
477 _("Failed to parse URI: %s\n"),
480 if (namespace != NULL)
481 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
482 GNUNET_FS_stop (ctx);
486 GNUNET_FS_publish_ksk (ctx,
490 GNUNET_TIME_relative_to_absolute (DEFAULT_EXPIRATION),
493 GNUNET_FS_PUBLISH_OPTION_NONE,
494 &uri_ksk_continuation,
496 if (namespace != NULL)
497 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
501 if (! disable_extractor)
503 plugins = EXTRACTOR_plugin_add_defaults (EXTRACTOR_OPTION_DEFAULT_POLICY);
505 GNUNET_CONFIGURATION_get_value_string (cfg, "FS", "EXTRACTORS",
509 plugins = EXTRACTOR_plugin_add_config (plugins, ex, EXTRACTOR_OPTION_DEFAULT_POLICY);
514 if (0 != STAT (args[0], &sbuf))
516 GNUNET_asprintf (&emsg,
517 _("Could not access file: %s\n"),
521 else if (S_ISDIR (sbuf.st_mode))
523 fi = GNUNET_FS_file_information_create_from_directory (ctx,
526 &GNUNET_FS_directory_scanner_default,
531 GNUNET_TIME_relative_to_absolute (DEFAULT_EXPIRATION),
537 meta = GNUNET_CONTAINER_meta_data_create ();
538 GNUNET_FS_meta_data_extract_from_file (meta,
541 fi = GNUNET_FS_file_information_create_from_file (ctx,
549 GNUNET_TIME_relative_to_absolute (DEFAULT_EXPIRATION));
550 GNUNET_break (fi != NULL);
552 EXTRACTOR_plugin_remove_all (plugins);
556 _("Could not publish `%s': %s\n"),
560 if (namespace != NULL)
561 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
562 GNUNET_FS_stop (ctx);
566 GNUNET_FS_file_information_inspect (fi,
571 if (namespace != NULL)
572 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
573 GNUNET_FS_file_information_destroy (fi, NULL, NULL);
574 GNUNET_FS_stop (ctx);
577 pc = GNUNET_FS_publish_start (ctx,
583 ? GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY
584 : GNUNET_FS_PUBLISH_OPTION_NONE);
588 _("Could not start publishing.\n"));
589 GNUNET_FS_stop (ctx);
593 kill_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
602 * The main function to publish content to GNUnet.
604 * @param argc number of arguments from the command line
605 * @param argv command line arguments
606 * @return 0 ok, 1 on error
609 main (int argc, char *const *argv)
611 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
612 {'a', "anonymity", "LEVEL",
613 gettext_noop ("set the desired LEVEL of sender-anonymity"),
614 1, &GNUNET_GETOPT_set_uint, &anonymity},
615 {'d', "disable-creation-time", NULL,
617 ("disable adding the creation time to the metadata of the uploaded file"),
618 0, &GNUNET_GETOPT_set_one, &do_disable_creation_time},
619 {'D', "disable-extractor", NULL,
621 ("do not use libextractor to add keywords or metadata"),
622 0, &GNUNET_GETOPT_set_one, &disable_extractor},
623 {'e', "extract", NULL,
625 ("print list of extracted keywords that would be used, but do not perform upload"),
626 0, &GNUNET_GETOPT_set_one, &extract_only},
627 {'k', "key", "KEYWORD",
629 ("add an additional keyword for the top-level file or directory"
630 " (this option can be specified multiple times)"),
631 1, &GNUNET_FS_getopt_set_keywords, &topKeywords},
632 {'m', "meta", "TYPE:VALUE",
633 gettext_noop ("set the meta-data for the given TYPE to the given VALUE"),
634 1, &GNUNET_FS_getopt_set_metadata, &meta},
635 {'n', "noindex", NULL,
636 gettext_noop ("do not index, perform full insertion (stores entire "
637 "file in encrypted form in GNUnet database)"),
638 0, &GNUNET_GETOPT_set_one, &do_insert},
641 ("specify ID of an updated version to be published in the future"
642 " (for namespace insertions only)"),
643 1, &GNUNET_GETOPT_set_string, &next_id},
644 {'p', "priority", "PRIORITY",
645 gettext_noop ("specify the priority of the content"),
646 1, &GNUNET_GETOPT_set_uint, &priority},
647 {'P', "pseudonym", "NAME",
649 ("publish the files under the pseudonym NAME (place file into namespace)"),
650 1, &GNUNET_GETOPT_set_string, &pseudonym},
651 {'s', "simulate-only", NULL,
652 gettext_noop ("only simulate the process but do not do any "
653 "actual publishing (useful to compute URIs)"),
654 0, &GNUNET_GETOPT_set_one, &do_simulate},
656 gettext_noop ("set the ID of this version of the publication"
657 " (for namespace insertions only)"),
658 1, &GNUNET_GETOPT_set_string, &this_id},
660 gettext_noop ("URI to be published (can be used instead of passing a "
661 "file to add keywords to the file with the respective URI)"),
662 1, &GNUNET_GETOPT_set_string, &uri_string},
663 {'V', "verbose", NULL,
664 gettext_noop ("be verbose (print progress information)"),
665 0, &GNUNET_GETOPT_set_one, &verbose},
666 GNUNET_GETOPT_OPTION_END
669 GNUNET_PROGRAM_run (argc,
673 ("Publish files on GNUnet."),
674 options, &run, NULL)) ? ret : 1;
677 /* end of gnunet-publish.c */