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 2, 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_SCHEDULER_Handle *sched;
43 static struct GNUNET_FS_PublishContext *pc;
45 static struct GNUNET_CONTAINER_MetaData *meta;
47 static struct GNUNET_FS_Uri *topKeywords;
49 static struct GNUNET_FS_Uri *uri;
51 static unsigned int anonymity = 1;
53 static unsigned int priority = 365;
55 static char *uri_string;
61 static char *pseudonym;
65 static int disable_extractor;
67 static int do_simulate;
69 static int extract_only;
71 static int do_disable_creation_time;
73 static GNUNET_SCHEDULER_TaskIdentifier kill_task;
77 do_stop_task (void *cls,
78 const struct GNUNET_SCHEDULER_TaskContext *tc)
80 struct GNUNET_FS_PublishContext *p;
86 GNUNET_FS_publish_stop (p);
89 GNUNET_CONTAINER_meta_data_destroy (meta);
97 * Called by FS client to give information about the progress of an
101 * @param info details about the event, specifying the event type
102 * and various bits about the event
103 * @return client-context (for the next progress call
104 * for this operation; should be set to NULL for
105 * SUSPEND and STOPPED events). The value returned
106 * will be passed to future callbacks in the respective
107 * field in the GNUNET_FS_ProgressInfo struct.
110 progress_cb (void *cls,
111 const struct GNUNET_FS_ProgressInfo *info)
115 switch (info->status)
117 case GNUNET_FS_STATUS_PUBLISH_START:
119 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
122 s = GNUNET_STRINGS_relative_time_to_string(info->value.publish.eta);
124 _("Publishing `%s' at %llu/%llu (%s remaining)\n"),
125 info->value.publish.filename,
126 (unsigned long long) info->value.publish.completed,
127 (unsigned long long) info->value.publish.size,
132 case GNUNET_FS_STATUS_PUBLISH_ERROR:
134 _("Error publishing: %s.\n"),
135 info->value.publish.specifics.error.message);
136 if (kill_task != GNUNET_SCHEDULER_NO_TASK)
138 GNUNET_SCHEDULER_cancel (sched,
140 kill_task = GNUNET_SCHEDULER_NO_TASK;
142 GNUNET_SCHEDULER_add_continuation (sched,
145 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
147 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
149 _("Publishing `%s' done.\n"),
150 info->value.publish.filename);
151 s = GNUNET_FS_uri_to_string (info->value.publish.specifics.completed.chk_uri);
156 if (info->value.publish.pctx == NULL)
158 if (kill_task != GNUNET_SCHEDULER_NO_TASK)
160 GNUNET_SCHEDULER_cancel (sched,
162 kill_task = GNUNET_SCHEDULER_NO_TASK;
164 GNUNET_SCHEDULER_add_continuation (sched,
167 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
170 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
171 GNUNET_break (NULL == pc);
175 _("Unexpected status: %d\n"),
179 return ""; /* non-null */
184 * Print metadata entries (except binary
185 * metadata and the filename).
188 * @param plugin_name name of the plugin that generated the meta data
189 * @param type type of the meta data
190 * @param format format of data
191 * @param data_mime_type mime type of data
192 * @param data value of the meta data
193 * @param data_size number of bytes in data
197 meta_printer (void *cls,
198 const char *plugin_name,
199 enum EXTRACTOR_MetaType type,
200 enum EXTRACTOR_MetaFormat format,
201 const char *data_mime_type,
205 if ( (format != EXTRACTOR_METAFORMAT_UTF8) &&
206 (format != EXTRACTOR_METAFORMAT_C_STRING) )
208 if (type == EXTRACTOR_METATYPE_FILENAME)
212 EXTRACTOR_metatype_to_string (type),
219 * Merge metadata entries.
221 * @param cls closure, target metadata structure
222 * @param plugin_name name of the plugin that generated the meta data
223 * @param type type of the meta data
224 * @param format format of data
225 * @param data_mime_type mime type of data
226 * @param data value of the meta data
227 * @param data_size number of bytes in data
231 meta_merger (void *cls,
232 const char *plugin_name,
233 enum EXTRACTOR_MetaType type,
234 enum EXTRACTOR_MetaFormat format,
235 const char *data_mime_type,
239 struct GNUNET_CONTAINER_MetaData *m = cls;
241 GNUNET_CONTAINER_meta_data_insert (m,
253 * Function called on all entries before the publication. This is
254 * where we perform modifications to the default based on command-line
258 * @param fi the entry in the publish-structure
259 * @param length length of the file or directory
260 * @param m metadata for the file or directory (can be modified)
261 * @param uri pointer to the keywords that will be used for this entry (can be modified)
262 * @param anonymity pointer to selected anonymity level (can be modified)
263 * @param priority pointer to selected priority (can be modified)
264 * @param expirationTime pointer to selected expiration time (can be modified)
265 * @param client_info pointer to client context set upon creation (can be modified)
266 * @return GNUNET_OK to continue, GNUNET_NO to remove
267 * this entry from the directory, GNUNET_SYSERR
268 * to abort the iteration
271 publish_inspector (void *cls,
272 struct GNUNET_FS_FileInformation *fi,
274 struct GNUNET_CONTAINER_MetaData *m,
275 struct GNUNET_FS_Uri **uri,
276 unsigned int *anonymity,
277 unsigned int *priority,
278 struct GNUNET_TIME_Absolute *expirationTime,
283 struct GNUNET_FS_Uri *new_uri;
287 if (NULL != topKeywords)
291 new_uri = GNUNET_FS_uri_ksk_merge (topKeywords,
293 GNUNET_FS_uri_destroy (*uri);
295 GNUNET_FS_uri_destroy (topKeywords);
305 GNUNET_CONTAINER_meta_data_iterate (meta,
308 GNUNET_CONTAINER_meta_data_destroy (meta);
311 if (! do_disable_creation_time)
312 GNUNET_CONTAINER_meta_data_add_publication_date (m);
315 fn = GNUNET_CONTAINER_meta_data_get_by_type (m,
316 EXTRACTOR_METATYPE_FILENAME);
317 fs = GNUNET_STRINGS_byte_size_fancy (length);
319 _("Keywords for file `%s' (%s)\n"),
324 GNUNET_CONTAINER_meta_data_iterate (m,
327 fprintf (stdout, "\n");
329 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (m))
330 GNUNET_FS_file_information_inspect (fi,
338 uri_sks_continuation (void *cls,
339 const struct GNUNET_FS_Uri *ksk_uri,
349 GNUNET_FS_uri_destroy (uri);
351 GNUNET_FS_stop (ctx);
357 uri_ksk_continuation (void *cls,
358 const struct GNUNET_FS_Uri *ksk_uri,
361 struct GNUNET_FS_Namespace *ns;
370 if (pseudonym != NULL)
372 ns = GNUNET_FS_namespace_create (ctx,
377 _("Failed to create namespace `%s'\n"),
383 GNUNET_FS_publish_sks (ctx,
389 GNUNET_TIME_relative_to_absolute (DEFAULT_EXPIRATION),
392 GNUNET_FS_PUBLISH_OPTION_NONE,
393 uri_sks_continuation,
395 GNUNET_assert (GNUNET_OK ==
396 GNUNET_FS_namespace_delete (ns, GNUNET_NO));
400 GNUNET_FS_uri_destroy (uri);
402 GNUNET_FS_stop (ctx);
408 * Main function that will be run by the scheduler.
411 * @param s the scheduler to use
412 * @param args remaining command-line arguments
413 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
414 * @param c configuration
418 struct GNUNET_SCHEDULER_Handle *s,
421 const struct GNUNET_CONFIGURATION_Handle *c)
423 struct GNUNET_FS_FileInformation *fi;
424 struct GNUNET_FS_Namespace *namespace;
425 struct EXTRACTOR_PluginList *l;
431 /* check arguments */
432 if ( ( (uri_string == NULL) || (extract_only) )
433 && ( (args[0] == NULL) || (args[1] != NULL) ) )
436 ("You must specify one and only one filename for insertion.\n"));
440 if ((uri_string != NULL) && (args[0] != NULL))
442 printf (_("You must NOT specify an URI and a filename.\n"));
446 if ((uri_string != NULL) && (extract_only))
448 printf (_("Cannot extract metadata from a URI!\n"));
452 if (pseudonym != NULL)
457 _("Option `%s' is required when using option `%s'.\n"),
464 { /* ordinary insertion checks */
468 _("Option `%s' makes no sense without option `%s'.\n"),
476 _("Option `%s' makes no sense without option `%s'.\n"),
482 if ( (args[0] == NULL) &&
483 (uri_string == NULL) )
486 _("Need the name of a file to publish!\n"));
491 ctx = GNUNET_FS_start (sched,
496 GNUNET_FS_FLAGS_NONE,
497 GNUNET_FS_OPTIONS_END);
501 _("Could not initialize `%s' subsystem.\n"),
507 if (NULL != pseudonym)
509 namespace = GNUNET_FS_namespace_create (ctx,
511 if (NULL == namespace)
514 _("Could not create namespace `%s'\n"),
516 GNUNET_FS_stop (ctx);
521 if (NULL != uri_string)
524 uri = GNUNET_FS_uri_parse (uri_string,
529 _("Failed to parse URI: %s\n"),
532 GNUNET_FS_stop (ctx);
536 GNUNET_FS_publish_ksk (ctx,
540 GNUNET_TIME_relative_to_absolute (DEFAULT_EXPIRATION),
543 GNUNET_FS_PUBLISH_OPTION_NONE,
544 &uri_ksk_continuation,
549 if (! disable_extractor)
551 l = EXTRACTOR_plugin_add_defaults (EXTRACTOR_OPTION_DEFAULT_POLICY);
553 GNUNET_CONFIGURATION_get_value_string (cfg, "FS", "EXTRACTORS",
557 l = EXTRACTOR_plugin_add_config (l, ex, EXTRACTOR_OPTION_DEFAULT_POLICY);
562 if (0 != STAT (args[0], &sbuf))
564 GNUNET_asprintf (&emsg,
565 _("Could not access file: %s\n"),
569 else if (S_ISDIR (sbuf.st_mode))
571 fi = GNUNET_FS_file_information_create_from_directory (NULL,
573 &GNUNET_FS_directory_scanner_default,
578 GNUNET_TIME_relative_to_absolute (DEFAULT_EXPIRATION),
583 fi = GNUNET_FS_file_information_create_from_file (NULL,
590 GNUNET_TIME_relative_to_absolute (DEFAULT_EXPIRATION));
591 GNUNET_break (fi != NULL);
593 EXTRACTOR_plugin_remove_all (l);
597 _("Could not publish `%s': %s\n"),
601 if (namespace != NULL)
602 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
603 GNUNET_FS_stop (ctx);
607 GNUNET_FS_file_information_inspect (fi,
612 if (namespace != NULL)
613 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
614 GNUNET_FS_file_information_destroy (fi, NULL, NULL);
615 GNUNET_FS_stop (ctx);
618 pc = GNUNET_FS_publish_start (ctx,
624 ? GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY
625 : GNUNET_FS_PUBLISH_OPTION_NONE);
629 _("Could not start publishing.\n"));
630 GNUNET_FS_stop (ctx);
634 kill_task = GNUNET_SCHEDULER_add_delayed (sched,
635 GNUNET_TIME_UNIT_FOREVER_REL,
642 * gnunet-publish command line options
644 static 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
704 * The main function to publish content to GNUnet.
706 * @param argc number of arguments from the command line
707 * @param argv command line arguments
708 * @return 0 ok, 1 on error
711 main (int argc, char *const *argv)
714 GNUNET_PROGRAM_run (argc,
718 ("Publish files on GNUnet."),
719 options, &run, NULL)) ? ret : 1;
722 /* end of gnunet-publish.c */