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;
110 GNUNET_FS_directory_scan_abort (ds);
113 if (namespace != NULL)
115 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
118 GNUNET_FS_stop (ctx);
125 * Called by FS client to give information about the progress of an
129 * @param info details about the event, specifying the event type
130 * and various bits about the event
131 * @return client-context (for the next progress call
132 * for this operation; should be set to NULL for
133 * SUSPEND and STOPPED events). The value returned
134 * will be passed to future callbacks in the respective
135 * field in the GNUNET_FS_ProgressInfo struct.
138 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
143 switch (info->status)
145 case GNUNET_FS_STATUS_PUBLISH_START:
147 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
150 s = GNUNET_STRINGS_relative_time_to_string (info->value.publish.eta,
152 FPRINTF (stdout, _("Publishing `%s' at %llu/%llu (%s remaining)\n"),
153 info->value.publish.filename,
154 (unsigned long long) info->value.publish.completed,
155 (unsigned long long) info->value.publish.size, s);
158 case GNUNET_FS_STATUS_PUBLISH_ERROR:
159 FPRINTF (stderr, _("Error publishing: %s.\n"),
160 info->value.publish.specifics.error.message);
161 if (kill_task != GNUNET_SCHEDULER_NO_TASK)
163 GNUNET_SCHEDULER_cancel (kill_task);
164 kill_task = GNUNET_SCHEDULER_NO_TASK;
166 kill_task = GNUNET_SCHEDULER_add_now (&do_stop_task, NULL);
168 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
169 FPRINTF (stdout, _("Publishing `%s' done.\n"),
170 info->value.publish.filename);
171 suri = GNUNET_FS_uri_to_string (info->value.publish.specifics.
173 FPRINTF (stdout, _("URI is `%s'.\n"), suri);
175 if (info->value.publish.pctx == NULL)
177 if (kill_task != GNUNET_SCHEDULER_NO_TASK)
179 GNUNET_SCHEDULER_cancel (kill_task);
180 kill_task = GNUNET_SCHEDULER_NO_TASK;
182 kill_task = GNUNET_SCHEDULER_add_now (&do_stop_task, NULL);
186 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
187 GNUNET_break (NULL == pc);
189 case GNUNET_FS_STATUS_UNINDEX_PROGRESS:
191 case GNUNET_FS_STATUS_UNINDEX_COMPLETED:
192 FPRINTF (stderr, "%s", _("Cleanup after abort complete.\n"));
195 FPRINTF (stderr, _("Unexpected status: %d\n"), info->status);
198 return ""; /* non-null */
203 * Print metadata entries (except binary
204 * metadata and the filename).
207 * @param plugin_name name of the plugin that generated the meta data
208 * @param type type of the meta data
209 * @param format format of data
210 * @param data_mime_type mime type of data
211 * @param data value of the meta data
212 * @param data_size number of bytes in data
216 meta_printer (void *cls, const char *plugin_name, enum EXTRACTOR_MetaType type,
217 enum EXTRACTOR_MetaFormat format, const char *data_mime_type,
218 const char *data, size_t data_size)
220 if ((format != EXTRACTOR_METAFORMAT_UTF8) &&
221 (format != EXTRACTOR_METAFORMAT_C_STRING))
223 if (type == EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME)
225 FPRINTF (stdout, "\t%s - %s\n", EXTRACTOR_metatype_to_string (type), data);
231 * Iterator printing keywords
234 * @param keyword the keyword
235 * @param is_mandatory is the keyword mandatory (in a search)
236 * @return GNUNET_OK to continue to iterate, GNUNET_SYSERR to abort
239 keyword_printer (void *cls, const char *keyword, int is_mandatory)
241 FPRINTF (stdout, "\t%s\n", keyword);
247 * Function called on all entries before the publication. This is
248 * where we perform modifications to the default based on command-line
252 * @param fi the entry in the publish-structure
253 * @param length length of the file or directory
254 * @param m metadata for the file or directory (can be modified)
255 * @param uri pointer to the keywords that will be used for this entry (can be modified)
256 * @param bo block options
257 * @param do_index should we index?
258 * @param client_info pointer to client context set upon creation (can be modified)
259 * @return GNUNET_OK to continue, GNUNET_NO to remove
260 * this entry from the directory, GNUNET_SYSERR
261 * to abort the iteration
264 publish_inspector (void *cls, struct GNUNET_FS_FileInformation *fi,
265 uint64_t length, struct GNUNET_CONTAINER_MetaData *m,
266 struct GNUNET_FS_Uri **uri,
267 struct GNUNET_FS_BlockOptions *bo, int *do_index,
272 struct GNUNET_FS_Uri *new_uri;
276 if ( (disable_extractor) &&
279 GNUNET_FS_uri_destroy (*uri);
282 if (NULL != topKeywords)
286 new_uri = GNUNET_FS_uri_ksk_merge (topKeywords, *uri);
287 GNUNET_FS_uri_destroy (*uri);
289 GNUNET_FS_uri_destroy (topKeywords);
299 GNUNET_CONTAINER_meta_data_merge (m, meta);
300 GNUNET_CONTAINER_meta_data_destroy (meta);
303 if (!do_disable_creation_time)
304 GNUNET_CONTAINER_meta_data_add_publication_date (m);
307 fn = GNUNET_CONTAINER_meta_data_get_by_type (m,
308 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
309 fs = GNUNET_STRINGS_byte_size_fancy (length);
310 FPRINTF (stdout, _("Meta data for file `%s' (%s)\n"), fn, fs);
311 GNUNET_CONTAINER_meta_data_iterate (m, &meta_printer, NULL);
312 FPRINTF (stdout, _("Keywords for file `%s' (%s)\n"), fn, fs);
316 GNUNET_FS_uri_ksk_get_keywords (*uri, &keyword_printer, NULL);
317 FPRINTF (stdout, "%s", "\n");
319 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (m))
320 GNUNET_FS_file_information_inspect (fi, &publish_inspector, fi);
329 uri_sks_continuation (void *cls, const struct GNUNET_FS_Uri *ksk_uri,
334 FPRINTF (stderr, "%s\n", emsg);
337 GNUNET_FS_uri_destroy (uri);
339 GNUNET_FS_stop (ctx);
348 uri_ksk_continuation (void *cls, const struct GNUNET_FS_Uri *ksk_uri,
351 struct GNUNET_FS_Namespace *ns;
355 FPRINTF (stderr, "%s\n", emsg);
358 if (pseudonym != NULL)
360 ns = GNUNET_FS_namespace_create (ctx, pseudonym);
363 FPRINTF (stderr, _("Failed to create namespace `%s' (illegal filename?)\n"), pseudonym);
368 GNUNET_FS_publish_sks (ctx, ns, this_id, next_id, meta, uri, &bo,
369 GNUNET_FS_PUBLISH_OPTION_NONE,
370 &uri_sks_continuation, NULL);
371 GNUNET_assert (GNUNET_OK == GNUNET_FS_namespace_delete (ns, GNUNET_NO));
375 GNUNET_FS_uri_destroy (uri);
377 GNUNET_FS_stop (ctx);
385 static struct GNUNET_FS_FileInformation *
386 get_file_information (struct GNUNET_FS_ShareTreeItem *item)
388 struct GNUNET_FS_FileInformation *fi;
389 struct GNUNET_FS_FileInformation *fic;
390 struct GNUNET_FS_ShareTreeItem *child;
392 if (item->is_directory == GNUNET_YES)
394 GNUNET_CONTAINER_meta_data_delete (item->meta,
395 EXTRACTOR_METATYPE_MIMETYPE, NULL, 0);
396 GNUNET_FS_meta_data_make_directory (item->meta);
397 if (NULL == item->ksk_uri)
399 const char *mime = GNUNET_FS_DIRECTORY_MIME;
400 item->ksk_uri = GNUNET_FS_uri_ksk_create_from_args (1, &mime);
403 GNUNET_FS_uri_ksk_add_keyword (item->ksk_uri, GNUNET_FS_DIRECTORY_MIME,
405 fi = GNUNET_FS_file_information_create_empty_directory (
406 ctx, NULL, item->ksk_uri,
407 item->meta, &bo, item->filename);
408 for (child = item->children_head; child; child = child->next)
410 fic = get_file_information (child);
411 GNUNET_break (GNUNET_OK == GNUNET_FS_file_information_add (fi, fic));
416 fi = GNUNET_FS_file_information_create_from_file (
417 ctx, NULL, item->filename,
418 item->ksk_uri, item->meta, !do_insert,
429 directory_trim_complete ()
431 struct GNUNET_FS_FileInformation *fi;
433 fi = get_file_information (directory_scan_result);
434 GNUNET_FS_share_tree_free (directory_scan_result);
435 directory_scan_result = NULL;
438 FPRINTF (stderr, "%s", _("Could not publish\n"));
439 if (namespace != NULL)
440 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
441 GNUNET_FS_stop (ctx);
445 GNUNET_FS_file_information_inspect (fi, &publish_inspector, NULL);
448 if (namespace != NULL)
449 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
450 GNUNET_FS_file_information_destroy (fi, NULL, NULL);
451 GNUNET_FS_stop (ctx);
452 if (kill_task != GNUNET_SCHEDULER_NO_TASK)
454 GNUNET_SCHEDULER_cancel (kill_task);
455 kill_task = GNUNET_SCHEDULER_NO_TASK;
459 pc = GNUNET_FS_publish_start (ctx, fi, namespace, this_id, next_id,
461 GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY :
462 GNUNET_FS_PUBLISH_OPTION_NONE);
465 FPRINTF (stderr, "%s", _("Could not start publishing.\n"));
466 GNUNET_FS_stop (ctx);
474 * Function called by the directory scanner as we build the tree
475 * that we will need to publish later.
478 * @param filename which file we are making progress on
479 * @param is_directory GNUNET_YES if this is a directory,
480 * GNUNET_NO if this is a file
481 * GNUNET_SYSERR if it is neither (or unknown)
482 * @param reason kind of progress we are making
485 directory_scan_cb (void *cls,
486 const char *filename,
488 enum GNUNET_FS_DirScannerProgressUpdateReason reason)
492 case GNUNET_FS_DIRSCANNER_FILE_START:
495 if (is_directory == GNUNET_YES)
496 FPRINTF (stdout, _("Scanning directory `%s'.\n"), filename);
498 FPRINTF (stdout, _("Scanning file `%s'.\n"), filename);
501 case GNUNET_FS_DIRSCANNER_FILE_IGNORED:
503 _("There was trouble processing file `%s', skipping it.\n"),
506 case GNUNET_FS_DIRSCANNER_ALL_COUNTED:
508 FPRINTF (stdout, "%s", _("Preprocessing complete.\n"));
510 case GNUNET_FS_DIRSCANNER_EXTRACT_FINISHED:
512 FPRINTF (stdout, _("Extracting meta data from file `%s' complete.\n"), filename);
514 case GNUNET_FS_DIRSCANNER_FINISHED:
516 FPRINTF (stdout, "%s", _("Meta data extraction has finished.\n"));
517 directory_scan_result = GNUNET_FS_directory_scan_get_result (ds);
519 GNUNET_FS_share_tree_trim (directory_scan_result);
520 directory_trim_complete ();
522 case GNUNET_FS_DIRSCANNER_INTERNAL_ERROR:
523 FPRINTF (stdout, "%s", _("Internal error scanning directory.\n"));
524 if (kill_task != GNUNET_SCHEDULER_NO_TASK)
526 GNUNET_SCHEDULER_cancel (kill_task);
527 kill_task = GNUNET_SCHEDULER_NO_TASK;
529 kill_task = GNUNET_SCHEDULER_add_now (&stop_scanner_task, NULL);
540 * Main function that will be run by the scheduler.
543 * @param args remaining command-line arguments
544 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
545 * @param c configuration
548 run (void *cls, char *const *args, const char *cfgfile,
549 const struct GNUNET_CONFIGURATION_Handle *c)
554 /* check arguments */
555 if ((uri_string != NULL) && (extract_only))
557 printf (_("Cannot extract metadata from a URI!\n"));
561 if (((uri_string == NULL) || (extract_only)) &&
562 ((args[0] == NULL) || (args[1] != NULL)))
564 printf (_("You must specify one and only one filename for insertion.\n"));
568 if ((uri_string != NULL) && (args[0] != NULL))
570 printf (_("You must NOT specify an URI and a filename.\n"));
574 if (pseudonym != NULL)
578 FPRINTF (stderr, _("Option `%s' is required when using option `%s'.\n"),
585 { /* ordinary insertion checks */
588 FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
595 FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
603 GNUNET_FS_start (cfg, "gnunet-publish", &progress_cb, NULL,
604 GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
607 FPRINTF (stderr, _("Could not initialize `%s' subsystem.\n"), "FS");
612 if (NULL != pseudonym)
614 namespace = GNUNET_FS_namespace_create (ctx, pseudonym);
615 if (NULL == namespace)
617 FPRINTF (stderr, _("Failed to create namespace `%s' (illegal filename?)\n"), pseudonym);
618 GNUNET_FS_stop (ctx);
623 if (NULL != uri_string)
626 uri = GNUNET_FS_uri_parse (uri_string, &emsg);
629 FPRINTF (stderr, _("Failed to parse URI: %s\n"), emsg);
631 if (namespace != NULL)
632 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
633 GNUNET_FS_stop (ctx);
637 GNUNET_FS_publish_ksk (ctx, topKeywords, meta, uri, &bo,
638 GNUNET_FS_PUBLISH_OPTION_NONE, &uri_ksk_continuation,
640 if (namespace != NULL)
641 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
645 GNUNET_CONFIGURATION_get_value_string (cfg, "FS", "EXTRACTORS", &ex))
647 if (0 != ACCESS (args[0], R_OK))
650 _("Failed to access `%s': %s\n"),
655 ds = GNUNET_FS_directory_scan_start (args[0],
658 &directory_scan_cb, NULL);
662 "%s", _("Failed to start meta directory scanner. Is gnunet-helper-publish-fs installed?\n"));
666 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_stop_task,
672 * The main function to publish content to GNUnet.
674 * @param argc number of arguments from the command line
675 * @param argv command line arguments
676 * @return 0 ok, 1 on error
679 main (int argc, char *const *argv)
681 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
682 {'a', "anonymity", "LEVEL",
683 gettext_noop ("set the desired LEVEL of sender-anonymity"),
684 1, &GNUNET_GETOPT_set_uint, &bo.anonymity_level},
685 {'d', "disable-creation-time", NULL,
687 ("disable adding the creation time to the metadata of the uploaded file"),
688 0, &GNUNET_GETOPT_set_one, &do_disable_creation_time},
689 {'D', "disable-extractor", NULL,
690 gettext_noop ("do not use libextractor to add keywords or metadata"),
691 0, &GNUNET_GETOPT_set_one, &disable_extractor},
692 {'e', "extract", NULL,
694 ("print list of extracted keywords that would be used, but do not perform upload"),
695 0, &GNUNET_GETOPT_set_one, &extract_only},
696 {'k', "key", "KEYWORD",
698 ("add an additional keyword for the top-level file or directory"
699 " (this option can be specified multiple times)"),
700 1, &GNUNET_FS_getopt_set_keywords, &topKeywords},
701 {'m', "meta", "TYPE:VALUE",
702 gettext_noop ("set the meta-data for the given TYPE to the given VALUE"),
703 1, &GNUNET_FS_getopt_set_metadata, &meta},
704 {'n', "noindex", NULL,
705 gettext_noop ("do not index, perform full insertion (stores entire "
706 "file in encrypted form in GNUnet database)"),
707 0, &GNUNET_GETOPT_set_one, &do_insert},
710 ("specify ID of an updated version to be published in the future"
711 " (for namespace insertions only)"),
712 1, &GNUNET_GETOPT_set_string, &next_id},
713 {'p', "priority", "PRIORITY",
714 gettext_noop ("specify the priority of the content"),
715 1, &GNUNET_GETOPT_set_uint, &bo.content_priority},
716 {'P', "pseudonym", "NAME",
718 ("publish the files under the pseudonym NAME (place file into namespace)"),
719 1, &GNUNET_GETOPT_set_string, &pseudonym},
720 {'r', "replication", "LEVEL",
721 gettext_noop ("set the desired replication LEVEL"),
722 1, &GNUNET_GETOPT_set_uint, &bo.replication_level},
723 {'s', "simulate-only", NULL,
724 gettext_noop ("only simulate the process but do not do any "
725 "actual publishing (useful to compute URIs)"),
726 0, &GNUNET_GETOPT_set_one, &do_simulate},
728 gettext_noop ("set the ID of this version of the publication"
729 " (for namespace insertions only)"),
730 1, &GNUNET_GETOPT_set_string, &this_id},
732 gettext_noop ("URI to be published (can be used instead of passing a "
733 "file to add keywords to the file with the respective URI)"),
734 1, &GNUNET_GETOPT_set_string, &uri_string},
735 {'V', "verbose", NULL,
736 gettext_noop ("be verbose (print progress information)"),
737 0, &GNUNET_GETOPT_set_one, &verbose},
738 GNUNET_GETOPT_OPTION_END
741 GNUNET_FS_year_to_time (GNUNET_FS_get_current_year () + 2);
743 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
746 GNUNET_PROGRAM_run (argc, argv, "gnunet-publish [OPTIONS] FILENAME",
748 ("Publish a file or directory on GNUnet"),
749 options, &run, NULL)) ? ret : 1;
750 GNUNET_free ((void*) argv);
754 /* end of gnunet-publish.c */