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;
74 do_stop_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
76 struct GNUNET_FS_PublishContext *p;
82 GNUNET_FS_publish_stop (p);
85 GNUNET_CONTAINER_meta_data_destroy (meta);
93 * Called by FS client to give information about the progress of an
97 * @param info details about the event, specifying the event type
98 * and various bits about the event
99 * @return client-context (for the next progress call
100 * for this operation; should be set to NULL for
101 * SUSPEND and STOPPED events). The value returned
102 * will be passed to future callbacks in the respective
103 * field in the GNUNET_FS_ProgressInfo struct.
106 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
110 switch (info->status)
112 case GNUNET_FS_STATUS_PUBLISH_START:
114 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
117 s = GNUNET_STRINGS_relative_time_to_string (info->value.publish.eta);
118 FPRINTF (stdout, _("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, s);
125 case GNUNET_FS_STATUS_PUBLISH_ERROR:
126 FPRINTF (stderr, _("Error publishing: %s.\n"),
127 info->value.publish.specifics.error.message);
128 if (kill_task != GNUNET_SCHEDULER_NO_TASK)
130 GNUNET_SCHEDULER_cancel (kill_task);
131 kill_task = GNUNET_SCHEDULER_NO_TASK;
133 GNUNET_SCHEDULER_add_continuation (&do_stop_task, NULL,
134 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
136 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
137 FPRINTF (stdout, _("Publishing `%s' done.\n"),
138 info->value.publish.filename);
139 s = GNUNET_FS_uri_to_string (info->value.publish.specifics.
141 FPRINTF (stdout, _("URI is `%s'.\n"), s);
143 if (info->value.publish.pctx == NULL)
145 if (kill_task != GNUNET_SCHEDULER_NO_TASK)
147 GNUNET_SCHEDULER_cancel (kill_task);
148 kill_task = GNUNET_SCHEDULER_NO_TASK;
150 GNUNET_SCHEDULER_add_continuation (&do_stop_task, NULL,
151 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
154 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
155 GNUNET_break (NULL == pc);
157 case GNUNET_FS_STATUS_UNINDEX_PROGRESS:
159 case GNUNET_FS_STATUS_UNINDEX_COMPLETED:
160 FPRINTF (stderr, "%s", _("Cleanup after abort complete.\n"));
163 FPRINTF (stderr, _("Unexpected status: %d\n"), info->status);
166 return ""; /* non-null */
171 * Print metadata entries (except binary
172 * metadata and the filename).
175 * @param plugin_name name of the plugin that generated the meta data
176 * @param type type of the meta data
177 * @param format format of data
178 * @param data_mime_type mime type of data
179 * @param data value of the meta data
180 * @param data_size number of bytes in data
184 meta_printer (void *cls, const char *plugin_name, enum EXTRACTOR_MetaType type,
185 enum EXTRACTOR_MetaFormat format, const char *data_mime_type,
186 const char *data, size_t data_size)
188 if ((format != EXTRACTOR_METAFORMAT_UTF8) &&
189 (format != EXTRACTOR_METAFORMAT_C_STRING))
191 if (type == EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME)
193 FPRINTF (stdout, "\t%s - %s\n", EXTRACTOR_metatype_to_string (type), data);
199 * Iterator printing keywords
202 * @param keyword the keyword
203 * @param is_mandatory is the keyword mandatory (in a search)
204 * @return GNUNET_OK to continue to iterate, GNUNET_SYSERR to abort
207 keyword_printer (void *cls, const char *keyword, int is_mandatory)
209 FPRINTF (stdout, "\t%s\n", keyword);
215 * Function called on all entries before the publication. This is
216 * where we perform modifications to the default based on command-line
220 * @param fi the entry in the publish-structure
221 * @param length length of the file or directory
222 * @param m metadata for the file or directory (can be modified)
223 * @param uri pointer to the keywords that will be used for this entry (can be modified)
224 * @param bo block options
225 * @param do_index should we index?
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, struct GNUNET_FS_FileInformation *fi,
233 uint64_t length, struct GNUNET_CONTAINER_MetaData *m,
234 struct GNUNET_FS_Uri **uri,
235 struct GNUNET_FS_BlockOptions *bo, int *do_index,
240 struct GNUNET_FS_Uri *new_uri;
244 if (NULL != topKeywords)
248 new_uri = GNUNET_FS_uri_ksk_merge (topKeywords, *uri);
249 GNUNET_FS_uri_destroy (*uri);
251 GNUNET_FS_uri_destroy (topKeywords);
261 GNUNET_CONTAINER_meta_data_merge (m, meta);
262 GNUNET_CONTAINER_meta_data_destroy (meta);
265 if (!do_disable_creation_time)
266 GNUNET_CONTAINER_meta_data_add_publication_date (m);
269 fn = GNUNET_CONTAINER_meta_data_get_by_type (m,
270 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
271 fs = GNUNET_STRINGS_byte_size_fancy (length);
272 FPRINTF (stdout, _("Meta data for file `%s' (%s)\n"), fn, fs);
273 GNUNET_CONTAINER_meta_data_iterate (m, &meta_printer, NULL);
274 FPRINTF (stdout, _("Keywords for file `%s' (%s)\n"), fn, fs);
278 GNUNET_FS_uri_ksk_get_keywords (*uri, &keyword_printer, NULL);
279 FPRINTF (stdout, "%s", "\n");
281 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (m))
282 GNUNET_FS_file_information_inspect (fi, &publish_inspector, fi);
291 uri_sks_continuation (void *cls, const struct GNUNET_FS_Uri *ksk_uri,
296 FPRINTF (stderr, "%s\n", emsg);
299 GNUNET_FS_uri_destroy (uri);
301 GNUNET_FS_stop (ctx);
310 uri_ksk_continuation (void *cls, const struct GNUNET_FS_Uri *ksk_uri,
313 struct GNUNET_FS_Namespace *ns;
317 FPRINTF (stderr, "%s\n", emsg);
320 if (pseudonym != NULL)
322 ns = GNUNET_FS_namespace_create (ctx, pseudonym);
325 FPRINTF (stderr, _("Failed to create namespace `%s'\n"), pseudonym);
330 GNUNET_FS_publish_sks (ctx, ns, this_id, next_id, meta, uri, &bo,
331 GNUNET_FS_PUBLISH_OPTION_NONE,
332 uri_sks_continuation, NULL);
333 GNUNET_assert (GNUNET_OK == GNUNET_FS_namespace_delete (ns, GNUNET_NO));
337 GNUNET_FS_uri_destroy (uri);
339 GNUNET_FS_stop (ctx);
345 * Main function that will be run by the scheduler.
348 * @param args remaining command-line arguments
349 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
350 * @param c configuration
353 run (void *cls, char *const *args, const char *cfgfile,
354 const struct GNUNET_CONFIGURATION_Handle *c)
356 struct GNUNET_FS_FileInformation *fi;
357 struct GNUNET_FS_Namespace *namespace;
358 struct EXTRACTOR_PluginList *plugins;
359 struct GNUNET_FS_Uri *keywords;
364 /* check arguments */
365 if ((uri_string != NULL) && (extract_only))
367 printf (_("Cannot extract metadata from a URI!\n"));
371 if (((uri_string == NULL) || (extract_only)) &&
372 ((args[0] == NULL) || (args[1] != NULL)))
374 printf (_("You must specify one and only one filename for insertion.\n"));
378 if ((uri_string != NULL) && (args[0] != NULL))
380 printf (_("You must NOT specify an URI and a filename.\n"));
384 if (pseudonym != NULL)
388 FPRINTF (stderr, _("Option `%s' is required when using option `%s'.\n"),
395 { /* ordinary insertion checks */
398 FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
405 FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
413 GNUNET_FS_start (cfg, "gnunet-publish", &progress_cb, NULL,
414 GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
417 FPRINTF (stderr, _("Could not initialize `%s' subsystem.\n"), "FS");
422 if (NULL != pseudonym)
424 namespace = GNUNET_FS_namespace_create (ctx, pseudonym);
425 if (NULL == namespace)
427 FPRINTF (stderr, _("Could not create namespace `%s'\n"), pseudonym);
428 GNUNET_FS_stop (ctx);
433 if (NULL != uri_string)
436 uri = GNUNET_FS_uri_parse (uri_string, &emsg);
439 FPRINTF (stderr, _("Failed to parse URI: %s\n"), emsg);
441 if (namespace != NULL)
442 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
443 GNUNET_FS_stop (ctx);
447 GNUNET_FS_publish_ksk (ctx, topKeywords, meta, uri, &bo,
448 GNUNET_FS_PUBLISH_OPTION_NONE, &uri_ksk_continuation,
450 if (namespace != NULL)
451 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
455 if (!disable_extractor)
457 plugins = EXTRACTOR_plugin_add_defaults (EXTRACTOR_OPTION_DEFAULT_POLICY);
459 GNUNET_CONFIGURATION_get_value_string (cfg, "FS", "EXTRACTORS", &ex))
463 EXTRACTOR_plugin_add_config (plugins, ex,
464 EXTRACTOR_OPTION_DEFAULT_POLICY);
469 GNUNET_assert (NULL != args[0]);
470 if (0 != STAT (args[0], &sbuf))
472 GNUNET_asprintf (&emsg, _("Could not access file: %s\n"), STRERROR (errno));
475 else if (S_ISDIR (sbuf.st_mode))
477 fi = GNUNET_FS_file_information_create_from_directory (ctx, NULL, args[0],
478 &GNUNET_FS_directory_scanner_default,
485 meta = GNUNET_CONTAINER_meta_data_create ();
486 GNUNET_FS_meta_data_extract_from_file (meta, args[0], plugins);
487 keywords = GNUNET_FS_uri_ksk_create_from_meta_data (meta);
488 fi = GNUNET_FS_file_information_create_from_file (ctx, NULL, args[0],
491 GNUNET_break (fi != NULL);
492 GNUNET_FS_uri_destroy (keywords);
494 EXTRACTOR_plugin_remove_all (plugins);
497 FPRINTF (stderr, _("Could not publish `%s': %s\n"), args[0], emsg);
499 if (namespace != NULL)
500 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
501 GNUNET_FS_stop (ctx);
505 GNUNET_FS_file_information_inspect (fi, &publish_inspector, NULL);
508 if (namespace != NULL)
509 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
510 GNUNET_FS_file_information_destroy (fi, NULL, NULL);
511 GNUNET_FS_stop (ctx);
514 pc = GNUNET_FS_publish_start (ctx, fi, namespace, this_id, next_id,
516 GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY :
517 GNUNET_FS_PUBLISH_OPTION_NONE);
520 FPRINTF (stderr, "%s", _("Could not start publishing.\n"));
521 GNUNET_FS_stop (ctx);
526 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_stop_task,
532 * The main function to publish content to GNUnet.
534 * @param argc number of arguments from the command line
535 * @param argv command line arguments
536 * @return 0 ok, 1 on error
539 main (int argc, char *const *argv)
541 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
542 {'a', "anonymity", "LEVEL",
543 gettext_noop ("set the desired LEVEL of sender-anonymity"),
544 1, &GNUNET_GETOPT_set_uint, &bo.anonymity_level},
545 {'d', "disable-creation-time", NULL,
547 ("disable adding the creation time to the metadata of the uploaded file"),
548 0, &GNUNET_GETOPT_set_one, &do_disable_creation_time},
549 {'D', "disable-extractor", NULL,
550 gettext_noop ("do not use libextractor to add keywords or metadata"),
551 0, &GNUNET_GETOPT_set_one, &disable_extractor},
552 {'e', "extract", NULL,
554 ("print list of extracted keywords that would be used, but do not perform upload"),
555 0, &GNUNET_GETOPT_set_one, &extract_only},
556 {'k', "key", "KEYWORD",
558 ("add an additional keyword for the top-level file or directory"
559 " (this option can be specified multiple times)"),
560 1, &GNUNET_FS_getopt_set_keywords, &topKeywords},
561 {'m', "meta", "TYPE:VALUE",
562 gettext_noop ("set the meta-data for the given TYPE to the given VALUE"),
563 1, &GNUNET_FS_getopt_set_metadata, &meta},
564 {'n', "noindex", NULL,
565 gettext_noop ("do not index, perform full insertion (stores entire "
566 "file in encrypted form in GNUnet database)"),
567 0, &GNUNET_GETOPT_set_one, &do_insert},
570 ("specify ID of an updated version to be published in the future"
571 " (for namespace insertions only)"),
572 1, &GNUNET_GETOPT_set_string, &next_id},
573 {'p', "priority", "PRIORITY",
574 gettext_noop ("specify the priority of the content"),
575 1, &GNUNET_GETOPT_set_uint, &bo.content_priority},
576 {'P', "pseudonym", "NAME",
578 ("publish the files under the pseudonym NAME (place file into namespace)"),
579 1, &GNUNET_GETOPT_set_string, &pseudonym},
580 {'r', "replication", "LEVEL",
581 gettext_noop ("set the desired replication LEVEL"),
582 1, &GNUNET_GETOPT_set_uint, &bo.replication_level},
583 {'s', "simulate-only", NULL,
584 gettext_noop ("only simulate the process but do not do any "
585 "actual publishing (useful to compute URIs)"),
586 0, &GNUNET_GETOPT_set_one, &do_simulate},
588 gettext_noop ("set the ID of this version of the publication"
589 " (for namespace insertions only)"),
590 1, &GNUNET_GETOPT_set_string, &this_id},
592 gettext_noop ("URI to be published (can be used instead of passing a "
593 "file to add keywords to the file with the respective URI)"),
594 1, &GNUNET_GETOPT_set_string, &uri_string},
595 {'V', "verbose", NULL,
596 gettext_noop ("be verbose (print progress information)"),
597 0, &GNUNET_GETOPT_set_one, &verbose},
598 GNUNET_GETOPT_OPTION_END
601 GNUNET_FS_year_to_time (GNUNET_FS_get_current_year () + 2);
603 GNUNET_PROGRAM_run (argc, argv, "gnunet-publish [OPTIONS] FILENAME",
605 ("Publish a file or directory on GNUnet"),
606 options, &run, NULL)) ? ret : 1;
609 /* end of gnunet-publish.c */