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;
69 static struct GNUNET_FS_DirScanner *ds;
71 static struct GNUNET_FS_ShareTreeItem * directory_scan_result;
73 static struct GNUNET_FS_Namespace *namespace;
79 do_stop_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
81 struct GNUNET_FS_PublishContext *p;
83 kill_task = GNUNET_SCHEDULER_NO_TASK;
88 GNUNET_FS_publish_stop (p);
92 GNUNET_CONTAINER_meta_data_destroy (meta);
99 * Stop the directory scanner (we had an error).
102 * @param tc scheduler context
105 stop_scanner_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
107 kill_task = GNUNET_SCHEDULER_NO_TASK;
108 GNUNET_FS_directory_scan_abort (ds);
110 if (namespace != NULL)
112 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
115 GNUNET_FS_stop (ctx);
122 * Called by FS client to give information about the progress of an
126 * @param info details about the event, specifying the event type
127 * and various bits about the event
128 * @return client-context (for the next progress call
129 * for this operation; should be set to NULL for
130 * SUSPEND and STOPPED events). The value returned
131 * will be passed to future callbacks in the respective
132 * field in the GNUNET_FS_ProgressInfo struct.
135 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
139 switch (info->status)
141 case GNUNET_FS_STATUS_PUBLISH_START:
143 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
146 s = GNUNET_STRINGS_relative_time_to_string (info->value.publish.eta);
147 FPRINTF (stdout, _("Publishing `%s' at %llu/%llu (%s remaining)\n"),
148 info->value.publish.filename,
149 (unsigned long long) info->value.publish.completed,
150 (unsigned long long) info->value.publish.size, s);
154 case GNUNET_FS_STATUS_PUBLISH_ERROR:
155 FPRINTF (stderr, _("Error publishing: %s.\n"),
156 info->value.publish.specifics.error.message);
157 if (kill_task != GNUNET_SCHEDULER_NO_TASK)
159 GNUNET_SCHEDULER_cancel (kill_task);
160 kill_task = GNUNET_SCHEDULER_NO_TASK;
162 kill_task = GNUNET_SCHEDULER_add_now (&do_stop_task, NULL);
164 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
165 FPRINTF (stdout, _("Publishing `%s' done.\n"),
166 info->value.publish.filename);
167 s = GNUNET_FS_uri_to_string (info->value.publish.specifics.
169 FPRINTF (stdout, _("URI is `%s'.\n"), s);
171 if (info->value.publish.pctx == NULL)
173 if (kill_task != GNUNET_SCHEDULER_NO_TASK)
175 GNUNET_SCHEDULER_cancel (kill_task);
176 kill_task = GNUNET_SCHEDULER_NO_TASK;
178 kill_task = GNUNET_SCHEDULER_add_now (&do_stop_task, NULL);
181 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
182 GNUNET_break (NULL == pc);
184 case GNUNET_FS_STATUS_UNINDEX_PROGRESS:
186 case GNUNET_FS_STATUS_UNINDEX_COMPLETED:
187 FPRINTF (stderr, "%s", _("Cleanup after abort complete.\n"));
190 FPRINTF (stderr, _("Unexpected status: %d\n"), info->status);
193 return ""; /* non-null */
198 * Print metadata entries (except binary
199 * metadata and the filename).
202 * @param plugin_name name of the plugin that generated the meta data
203 * @param type type of the meta data
204 * @param format format of data
205 * @param data_mime_type mime type of data
206 * @param data value of the meta data
207 * @param data_size number of bytes in data
211 meta_printer (void *cls, const char *plugin_name, enum EXTRACTOR_MetaType type,
212 enum EXTRACTOR_MetaFormat format, const char *data_mime_type,
213 const char *data, size_t data_size)
215 if ((format != EXTRACTOR_METAFORMAT_UTF8) &&
216 (format != EXTRACTOR_METAFORMAT_C_STRING))
218 if (type == EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME)
220 FPRINTF (stdout, "\t%s - %s\n", EXTRACTOR_metatype_to_string (type), data);
226 * Iterator printing keywords
229 * @param keyword the keyword
230 * @param is_mandatory is the keyword mandatory (in a search)
231 * @return GNUNET_OK to continue to iterate, GNUNET_SYSERR to abort
234 keyword_printer (void *cls, const char *keyword, int is_mandatory)
236 FPRINTF (stdout, "\t%s\n", keyword);
242 * Function called on all entries before the publication. This is
243 * where we perform modifications to the default based on command-line
247 * @param fi the entry in the publish-structure
248 * @param length length of the file or directory
249 * @param m metadata for the file or directory (can be modified)
250 * @param uri pointer to the keywords that will be used for this entry (can be modified)
251 * @param bo block options
252 * @param do_index should we index?
253 * @param client_info pointer to client context set upon creation (can be modified)
254 * @return GNUNET_OK to continue, GNUNET_NO to remove
255 * this entry from the directory, GNUNET_SYSERR
256 * to abort the iteration
259 publish_inspector (void *cls, struct GNUNET_FS_FileInformation *fi,
260 uint64_t length, struct GNUNET_CONTAINER_MetaData *m,
261 struct GNUNET_FS_Uri **uri,
262 struct GNUNET_FS_BlockOptions *bo, int *do_index,
267 struct GNUNET_FS_Uri *new_uri;
271 if (NULL != topKeywords)
275 new_uri = GNUNET_FS_uri_ksk_merge (topKeywords, *uri);
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);
299 FPRINTF (stdout, _("Meta data for file `%s' (%s)\n"), fn, fs);
300 GNUNET_CONTAINER_meta_data_iterate (m, &meta_printer, NULL);
301 FPRINTF (stdout, _("Keywords for file `%s' (%s)\n"), fn, fs);
305 GNUNET_FS_uri_ksk_get_keywords (*uri, &keyword_printer, NULL);
306 FPRINTF (stdout, "%s", "\n");
308 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (m))
309 GNUNET_FS_file_information_inspect (fi, &publish_inspector, fi);
318 uri_sks_continuation (void *cls, const struct GNUNET_FS_Uri *ksk_uri,
323 FPRINTF (stderr, "%s\n", emsg);
326 GNUNET_FS_uri_destroy (uri);
328 GNUNET_FS_stop (ctx);
337 uri_ksk_continuation (void *cls, const struct GNUNET_FS_Uri *ksk_uri,
340 struct GNUNET_FS_Namespace *ns;
344 FPRINTF (stderr, "%s\n", emsg);
347 if (pseudonym != NULL)
349 ns = GNUNET_FS_namespace_create (ctx, pseudonym);
352 FPRINTF (stderr, _("Failed to create namespace `%s'\n"), pseudonym);
357 GNUNET_FS_publish_sks (ctx, ns, this_id, next_id, meta, uri, &bo,
358 GNUNET_FS_PUBLISH_OPTION_NONE,
359 &uri_sks_continuation, NULL);
360 GNUNET_assert (GNUNET_OK == GNUNET_FS_namespace_delete (ns, GNUNET_NO));
364 GNUNET_FS_uri_destroy (uri);
366 GNUNET_FS_stop (ctx);
374 static struct GNUNET_FS_FileInformation *
375 get_file_information (struct GNUNET_FS_ShareTreeItem *item)
377 struct GNUNET_FS_FileInformation *fi;
378 struct GNUNET_FS_FileInformation *fic;
379 struct GNUNET_FS_ShareTreeItem *child;
381 if (item->is_directory == GNUNET_YES)
383 GNUNET_CONTAINER_meta_data_delete (item->meta,
384 EXTRACTOR_METATYPE_MIMETYPE, NULL, 0);
385 GNUNET_FS_meta_data_make_directory (item->meta);
386 if (NULL == item->ksk_uri)
388 const char *mime = GNUNET_FS_DIRECTORY_MIME;
389 item->ksk_uri = GNUNET_FS_uri_ksk_create_from_args (1, &mime);
392 GNUNET_FS_uri_ksk_add_keyword (item->ksk_uri, GNUNET_FS_DIRECTORY_MIME,
394 fi = GNUNET_FS_file_information_create_empty_directory (
395 ctx, NULL, item->ksk_uri,
396 item->meta, &bo, item->filename);
397 for (child = item->children_head; child; child = child->next)
399 fic = get_file_information (child);
400 GNUNET_break (GNUNET_OK == GNUNET_FS_file_information_add (fi, fic));
405 fi = GNUNET_FS_file_information_create_from_file (
406 ctx, NULL, item->filename,
407 item->ksk_uri, item->meta, !do_insert,
418 directory_trim_complete ()
420 struct GNUNET_FS_FileInformation *fi;
422 fi = get_file_information (directory_scan_result);
423 GNUNET_FS_share_tree_free (directory_scan_result);
424 directory_scan_result = NULL;
427 FPRINTF (stderr, "%s", _("Could not publish\n"));
428 if (namespace != NULL)
429 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
430 GNUNET_FS_stop (ctx);
434 GNUNET_FS_file_information_inspect (fi, &publish_inspector, NULL);
437 if (namespace != NULL)
438 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
439 GNUNET_FS_file_information_destroy (fi, NULL, NULL);
440 GNUNET_FS_stop (ctx);
441 if (kill_task != GNUNET_SCHEDULER_NO_TASK)
443 GNUNET_SCHEDULER_cancel (kill_task);
444 kill_task = GNUNET_SCHEDULER_NO_TASK;
448 pc = GNUNET_FS_publish_start (ctx, fi, namespace, this_id, next_id,
450 GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY :
451 GNUNET_FS_PUBLISH_OPTION_NONE);
454 FPRINTF (stderr, "%s", _("Could not start publishing.\n"));
455 GNUNET_FS_stop (ctx);
463 * Function called by the directory scanner as we build the tree
464 * that we will need to publish later.
467 * @param filename which file we are making progress on
468 * @param is_directory GNUNET_YES if this is a directory,
469 * GNUNET_NO if this is a file
470 * GNUNET_SYSERR if it is neither (or unknown)
471 * @param reason kind of progress we are making
474 directory_scan_cb (void *cls,
475 const char *filename,
477 enum GNUNET_FS_DirScannerProgressUpdateReason reason)
481 case GNUNET_FS_DIRSCANNER_FILE_START:
484 if (is_directory == GNUNET_YES)
485 FPRINTF (stdout, _("Scanning directory `%s'.\n"), filename);
487 FPRINTF (stdout, _("Scanning file `%s'.\n"), filename);
490 case GNUNET_FS_DIRSCANNER_FILE_IGNORED:
492 _("There was trouble processing file `%s', skipping it.\n"),
495 case GNUNET_FS_DIRSCANNER_ALL_COUNTED:
497 FPRINTF (stdout, "%s", _("Preprocessing complete.\n"));
499 case GNUNET_FS_DIRSCANNER_EXTRACT_FINISHED:
501 FPRINTF (stdout, _("Extracting meta data from file `%s' complete.\n"), filename);
503 case GNUNET_FS_DIRSCANNER_FINISHED:
505 FPRINTF (stdout, "%s", _("Meta data extraction has finished.\n"));
506 directory_scan_result = GNUNET_FS_directory_scan_get_result (ds);
508 GNUNET_FS_share_tree_trim (directory_scan_result);
509 directory_trim_complete ();
511 case GNUNET_FS_DIRSCANNER_INTERNAL_ERROR:
512 FPRINTF (stdout, "%s", _("Internal error scanning directory.\n"));
513 if (kill_task != GNUNET_SCHEDULER_NO_TASK)
515 GNUNET_SCHEDULER_cancel (kill_task);
516 kill_task = GNUNET_SCHEDULER_NO_TASK;
518 kill_task = GNUNET_SCHEDULER_add_now (&stop_scanner_task, NULL);
529 * Main function that will be run by the scheduler.
532 * @param args remaining command-line arguments
533 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
534 * @param c configuration
537 run (void *cls, char *const *args, const char *cfgfile,
538 const struct GNUNET_CONFIGURATION_Handle *c)
543 /* check arguments */
544 if ((uri_string != NULL) && (extract_only))
546 printf (_("Cannot extract metadata from a URI!\n"));
550 if (((uri_string == NULL) || (extract_only)) &&
551 ((args[0] == NULL) || (args[1] != NULL)))
553 printf (_("You must specify one and only one filename for insertion.\n"));
557 if ((uri_string != NULL) && (args[0] != NULL))
559 printf (_("You must NOT specify an URI and a filename.\n"));
563 if (pseudonym != NULL)
567 FPRINTF (stderr, _("Option `%s' is required when using option `%s'.\n"),
574 { /* ordinary insertion checks */
577 FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
584 FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
592 GNUNET_FS_start (cfg, "gnunet-publish", &progress_cb, NULL,
593 GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
596 FPRINTF (stderr, _("Could not initialize `%s' subsystem.\n"), "FS");
601 if (NULL != pseudonym)
603 namespace = GNUNET_FS_namespace_create (ctx, pseudonym);
604 if (NULL == namespace)
606 FPRINTF (stderr, _("Could not create namespace `%s'\n"), pseudonym);
607 GNUNET_FS_stop (ctx);
612 if (NULL != uri_string)
615 uri = GNUNET_FS_uri_parse (uri_string, &emsg);
618 FPRINTF (stderr, _("Failed to parse URI: %s\n"), emsg);
620 if (namespace != NULL)
621 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
622 GNUNET_FS_stop (ctx);
626 GNUNET_FS_publish_ksk (ctx, topKeywords, meta, uri, &bo,
627 GNUNET_FS_PUBLISH_OPTION_NONE, &uri_ksk_continuation,
629 if (namespace != NULL)
630 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
634 GNUNET_CONFIGURATION_get_value_string (cfg, "FS", "EXTRACTORS", &ex))
636 if (0 != ACCESS (args[0], R_OK))
639 _("Failed to access `%s': %s\n"),
644 ds = GNUNET_FS_directory_scan_start (args[0],
647 &directory_scan_cb, NULL);
651 "%s", _("Failed to start meta directory scanner. Is gnunet-helper-publish-fs installed?\n"));
655 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_stop_task,
661 * The main function to publish content to GNUnet.
663 * @param argc number of arguments from the command line
664 * @param argv command line arguments
665 * @return 0 ok, 1 on error
668 main (int argc, char *const *argv)
670 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
671 {'a', "anonymity", "LEVEL",
672 gettext_noop ("set the desired LEVEL of sender-anonymity"),
673 1, &GNUNET_GETOPT_set_uint, &bo.anonymity_level},
674 {'d', "disable-creation-time", NULL,
676 ("disable adding the creation time to the metadata of the uploaded file"),
677 0, &GNUNET_GETOPT_set_one, &do_disable_creation_time},
678 {'D', "disable-extractor", NULL,
679 gettext_noop ("do not use libextractor to add keywords or metadata"),
680 0, &GNUNET_GETOPT_set_one, &disable_extractor},
681 {'e', "extract", NULL,
683 ("print list of extracted keywords that would be used, but do not perform upload"),
684 0, &GNUNET_GETOPT_set_one, &extract_only},
685 {'k', "key", "KEYWORD",
687 ("add an additional keyword for the top-level file or directory"
688 " (this option can be specified multiple times)"),
689 1, &GNUNET_FS_getopt_set_keywords, &topKeywords},
690 {'m', "meta", "TYPE:VALUE",
691 gettext_noop ("set the meta-data for the given TYPE to the given VALUE"),
692 1, &GNUNET_FS_getopt_set_metadata, &meta},
693 {'n', "noindex", NULL,
694 gettext_noop ("do not index, perform full insertion (stores entire "
695 "file in encrypted form in GNUnet database)"),
696 0, &GNUNET_GETOPT_set_one, &do_insert},
699 ("specify ID of an updated version to be published in the future"
700 " (for namespace insertions only)"),
701 1, &GNUNET_GETOPT_set_string, &next_id},
702 {'p', "priority", "PRIORITY",
703 gettext_noop ("specify the priority of the content"),
704 1, &GNUNET_GETOPT_set_uint, &bo.content_priority},
705 {'P', "pseudonym", "NAME",
707 ("publish the files under the pseudonym NAME (place file into namespace)"),
708 1, &GNUNET_GETOPT_set_string, &pseudonym},
709 {'r', "replication", "LEVEL",
710 gettext_noop ("set the desired replication LEVEL"),
711 1, &GNUNET_GETOPT_set_uint, &bo.replication_level},
712 {'s', "simulate-only", NULL,
713 gettext_noop ("only simulate the process but do not do any "
714 "actual publishing (useful to compute URIs)"),
715 0, &GNUNET_GETOPT_set_one, &do_simulate},
717 gettext_noop ("set the ID of this version of the publication"
718 " (for namespace insertions only)"),
719 1, &GNUNET_GETOPT_set_string, &this_id},
721 gettext_noop ("URI to be published (can be used instead of passing a "
722 "file to add keywords to the file with the respective URI)"),
723 1, &GNUNET_GETOPT_set_string, &uri_string},
724 {'V', "verbose", NULL,
725 gettext_noop ("be verbose (print progress information)"),
726 0, &GNUNET_GETOPT_set_one, &verbose},
727 GNUNET_GETOPT_OPTION_END
729 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
730 "GNUnet publish starts\n");
732 GNUNET_FS_year_to_time (GNUNET_FS_get_current_year () + 2);
734 GNUNET_PROGRAM_run (argc, argv, "gnunet-publish [OPTIONS] FILENAME",
736 ("Publish a file or directory on GNUnet"),
737 options, &run, NULL)) ? ret : 1;
740 /* end of gnunet-publish.c */