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_intermediary_result;
73 static struct GNUNET_FS_ShareTreeItem * directory_scan_result;
75 static struct GNUNET_FS_ProcessMetadataContext *pmc;
77 static struct GNUNET_FS_Namespace *namespace;
83 do_stop_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
85 struct GNUNET_FS_PublishContext *p;
87 kill_task = GNUNET_SCHEDULER_NO_TASK;
92 GNUNET_FS_publish_stop (p);
96 GNUNET_CONTAINER_meta_data_destroy (meta);
103 * Called by FS client to give information about the progress of an
107 * @param info details about the event, specifying the event type
108 * and various bits about the event
109 * @return client-context (for the next progress call
110 * for this operation; should be set to NULL for
111 * SUSPEND and STOPPED events). The value returned
112 * will be passed to future callbacks in the respective
113 * field in the GNUNET_FS_ProgressInfo struct.
116 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
120 switch (info->status)
122 case GNUNET_FS_STATUS_PUBLISH_START:
124 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
127 s = GNUNET_STRINGS_relative_time_to_string (info->value.publish.eta);
128 FPRINTF (stdout, _("Publishing `%s' at %llu/%llu (%s remaining)\n"),
129 info->value.publish.filename,
130 (unsigned long long) info->value.publish.completed,
131 (unsigned long long) info->value.publish.size, s);
135 case GNUNET_FS_STATUS_PUBLISH_ERROR:
136 FPRINTF (stderr, _("Error publishing: %s.\n"),
137 info->value.publish.specifics.error.message);
138 if (kill_task != GNUNET_SCHEDULER_NO_TASK)
140 GNUNET_SCHEDULER_cancel (kill_task);
141 kill_task = GNUNET_SCHEDULER_NO_TASK;
143 kill_task = GNUNET_SCHEDULER_add_now (&do_stop_task, NULL);
145 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
146 FPRINTF (stdout, _("Publishing `%s' done.\n"),
147 info->value.publish.filename);
148 s = GNUNET_FS_uri_to_string (info->value.publish.specifics.
150 FPRINTF (stdout, _("URI is `%s'.\n"), s);
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 kill_task = GNUNET_SCHEDULER_add_now (&do_stop_task, NULL);
162 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
163 GNUNET_break (NULL == pc);
165 case GNUNET_FS_STATUS_UNINDEX_PROGRESS:
167 case GNUNET_FS_STATUS_UNINDEX_COMPLETED:
168 FPRINTF (stderr, "%s", _("Cleanup after abort complete.\n"));
171 FPRINTF (stderr, _("Unexpected status: %d\n"), info->status);
174 return ""; /* non-null */
179 * Print metadata entries (except binary
180 * metadata and the filename).
183 * @param plugin_name name of the plugin that generated the meta data
184 * @param type type of the meta data
185 * @param format format of data
186 * @param data_mime_type mime type of data
187 * @param data value of the meta data
188 * @param data_size number of bytes in data
192 meta_printer (void *cls, const char *plugin_name, enum EXTRACTOR_MetaType type,
193 enum EXTRACTOR_MetaFormat format, const char *data_mime_type,
194 const char *data, size_t data_size)
196 if ((format != EXTRACTOR_METAFORMAT_UTF8) &&
197 (format != EXTRACTOR_METAFORMAT_C_STRING))
199 if (type == EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME)
201 FPRINTF (stdout, "\t%s - %s\n", EXTRACTOR_metatype_to_string (type), data);
207 * Iterator printing keywords
210 * @param keyword the keyword
211 * @param is_mandatory is the keyword mandatory (in a search)
212 * @return GNUNET_OK to continue to iterate, GNUNET_SYSERR to abort
215 keyword_printer (void *cls, const char *keyword, int is_mandatory)
217 FPRINTF (stdout, "\t%s\n", keyword);
223 * Function called on all entries before the publication. This is
224 * where we perform modifications to the default based on command-line
228 * @param fi the entry in the publish-structure
229 * @param length length of the file or directory
230 * @param m metadata for the file or directory (can be modified)
231 * @param uri pointer to the keywords that will be used for this entry (can be modified)
232 * @param bo block options
233 * @param do_index should we index?
234 * @param client_info pointer to client context set upon creation (can be modified)
235 * @return GNUNET_OK to continue, GNUNET_NO to remove
236 * this entry from the directory, GNUNET_SYSERR
237 * to abort the iteration
240 publish_inspector (void *cls, struct GNUNET_FS_FileInformation *fi,
241 uint64_t length, struct GNUNET_CONTAINER_MetaData *m,
242 struct GNUNET_FS_Uri **uri,
243 struct GNUNET_FS_BlockOptions *bo, int *do_index,
248 struct GNUNET_FS_Uri *new_uri;
252 if (NULL != topKeywords)
256 new_uri = GNUNET_FS_uri_ksk_merge (topKeywords, *uri);
257 GNUNET_FS_uri_destroy (*uri);
259 GNUNET_FS_uri_destroy (topKeywords);
269 GNUNET_CONTAINER_meta_data_merge (m, meta);
270 GNUNET_CONTAINER_meta_data_destroy (meta);
273 if (!do_disable_creation_time)
274 GNUNET_CONTAINER_meta_data_add_publication_date (m);
277 fn = GNUNET_CONTAINER_meta_data_get_by_type (m,
278 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
279 fs = GNUNET_STRINGS_byte_size_fancy (length);
280 FPRINTF (stdout, _("Meta data for file `%s' (%s)\n"), fn, fs);
281 GNUNET_CONTAINER_meta_data_iterate (m, &meta_printer, NULL);
282 FPRINTF (stdout, _("Keywords for file `%s' (%s)\n"), fn, fs);
286 GNUNET_FS_uri_ksk_get_keywords (*uri, &keyword_printer, NULL);
287 FPRINTF (stdout, "%s", "\n");
289 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (m))
290 GNUNET_FS_file_information_inspect (fi, &publish_inspector, fi);
299 uri_sks_continuation (void *cls, const struct GNUNET_FS_Uri *ksk_uri,
304 FPRINTF (stderr, "%s\n", emsg);
307 GNUNET_FS_uri_destroy (uri);
309 GNUNET_FS_stop (ctx);
318 uri_ksk_continuation (void *cls, const struct GNUNET_FS_Uri *ksk_uri,
321 struct GNUNET_FS_Namespace *ns;
325 FPRINTF (stderr, "%s\n", emsg);
328 if (pseudonym != NULL)
330 ns = GNUNET_FS_namespace_create (ctx, pseudonym);
333 FPRINTF (stderr, _("Failed to create namespace `%s'\n"), pseudonym);
338 GNUNET_FS_publish_sks (ctx, ns, this_id, next_id, meta, uri, &bo,
339 GNUNET_FS_PUBLISH_OPTION_NONE,
340 uri_sks_continuation, NULL);
341 GNUNET_assert (GNUNET_OK == GNUNET_FS_namespace_delete (ns, GNUNET_NO));
345 GNUNET_FS_uri_destroy (uri);
347 GNUNET_FS_stop (ctx);
351 static struct GNUNET_FS_FileInformation *
352 get_file_information (struct GNUNET_FS_ShareTreeItem *item)
354 struct GNUNET_FS_FileInformation *fi;
355 struct GNUNET_FS_FileInformation *fic;
356 struct GNUNET_FS_ShareTreeItem *child;
358 if (item->is_directory)
360 GNUNET_CONTAINER_meta_data_delete (item->meta,
361 EXTRACTOR_METATYPE_MIMETYPE, NULL, 0);
362 GNUNET_FS_meta_data_make_directory (item->meta);
363 GNUNET_FS_uri_ksk_add_keyword (item->ksk_uri, GNUNET_FS_DIRECTORY_MIME,
365 fi = GNUNET_FS_file_information_create_empty_directory (
366 ctx, NULL, item->ksk_uri,
367 item->meta, &bo, item->filename);
368 for (child = item->children_head; child; child = child->next)
370 fic = get_file_information (child);
371 GNUNET_break (GNUNET_OK == GNUNET_FS_file_information_add (fi, fic));
376 fi = GNUNET_FS_file_information_create_from_file (
377 ctx, NULL, item->filename,
378 item->ksk_uri, item->meta, !do_insert,
381 GNUNET_CONTAINER_meta_data_destroy (item->meta);
382 GNUNET_FS_uri_destroy (item->ksk_uri);
383 GNUNET_free (item->short_filename);
384 GNUNET_free (item->filename);
390 directory_trim_complete (void *cls,
391 const struct GNUNET_SCHEDULER_TaskContext *tc)
393 struct GNUNET_FS_FileInformation *fi;
394 directory_scan_result = directory_scan_intermediary_result;
395 fi = get_file_information (directory_scan_result);
396 directory_scan_result = NULL;
399 FPRINTF (stderr, "%s", _("Could not publish\n"));
400 if (namespace != NULL)
401 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
402 GNUNET_FS_stop (ctx);
406 GNUNET_FS_file_information_inspect (fi, &publish_inspector, NULL);
409 if (namespace != NULL)
410 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
411 GNUNET_FS_file_information_destroy (fi, NULL, NULL);
412 GNUNET_FS_stop (ctx);
415 pc = GNUNET_FS_publish_start (ctx, fi, namespace, this_id, next_id,
417 GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY :
418 GNUNET_FS_PUBLISH_OPTION_NONE);
421 FPRINTF (stderr, "%s", _("Could not start publishing.\n"));
422 GNUNET_FS_stop (ctx);
429 directory_scan_cb (void *cls, struct GNUNET_FS_DirScanner *ds,
430 const char *filename, char is_directory,
431 enum GNUNET_FS_DirScannerProgressUpdateReason reason)
435 case GNUNET_FS_DIRSCANNER_NEW_FILE:
436 if (filename != NULL)
439 FPRINTF (stdout, _("Scanning directory `%s'.\n"), filename);
441 FPRINTF (stdout, _("Scanning file `%s'.\n"), filename);
444 case GNUNET_FS_DIRSCANNER_DOES_NOT_EXIST:
445 if (filename != NULL)
448 _("Failed to scan `%s', because it does not exist.\n"),
452 case GNUNET_FS_DIRSCANNER_ASKED_TO_STOP:
453 if (filename != NULL)
456 _("Scanner was about to scan `%s', but is now stopping.\n"),
460 FPRINTF (stdout, "%s", _("Scanner is stopping.\n"));
462 case GNUNET_FS_DIRSCANNER_SHUTDOWN:
463 FPRINTF (stdout, "%s", _("Client is shutting down.\n"));
465 case GNUNET_FS_DIRSCANNER_FINISHED:
466 FPRINTF (stdout, "%s", _("Scanner has finished.\n"));
468 case GNUNET_FS_DIRSCANNER_PROTOCOL_ERROR:
469 FPRINTF (stdout, "%s",
470 _("There was a failure communicating with the scanner.\n"));
473 FPRINTF (stdout, _("Got unknown scanner update with filename `%s'.\n"),
477 if ((filename == NULL && GNUNET_FS_DIRSCANNER_FINISHED)
478 || reason == GNUNET_FS_DIRSCANNER_PROTOCOL_ERROR
479 || reason == GNUNET_FS_DIRSCANNER_SHUTDOWN)
481 /* Any of this causes us to try to clean up the scanner */
482 directory_scan_intermediary_result = GNUNET_FS_directory_scan_cleanup (ds);
483 pmc = GNUNET_FS_trim_share_tree (directory_scan_intermediary_result,
484 &directory_trim_complete, NULL);
487 /* FIXME: change the tree processor to be able to free untrimmed trees
488 * right here instead of waiting for trimming to complete, if we need to
497 * Main function that will be run by the scheduler.
500 * @param args remaining command-line arguments
501 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
502 * @param c configuration
505 run (void *cls, char *const *args, const char *cfgfile,
506 const struct GNUNET_CONFIGURATION_Handle *c)
511 /* check arguments */
512 if ((uri_string != NULL) && (extract_only))
514 printf (_("Cannot extract metadata from a URI!\n"));
518 if (((uri_string == NULL) || (extract_only)) &&
519 ((args[0] == NULL) || (args[1] != NULL)))
521 printf (_("You must specify one and only one filename for insertion.\n"));
525 if ((uri_string != NULL) && (args[0] != NULL))
527 printf (_("You must NOT specify an URI and a filename.\n"));
531 if (pseudonym != NULL)
535 FPRINTF (stderr, _("Option `%s' is required when using option `%s'.\n"),
542 { /* ordinary insertion checks */
545 FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
552 FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
560 GNUNET_FS_start (cfg, "gnunet-publish", &progress_cb, NULL,
561 GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
564 FPRINTF (stderr, _("Could not initialize `%s' subsystem.\n"), "FS");
569 if (NULL != pseudonym)
571 namespace = GNUNET_FS_namespace_create (ctx, pseudonym);
572 if (NULL == namespace)
574 FPRINTF (stderr, _("Could not create namespace `%s'\n"), pseudonym);
575 GNUNET_FS_stop (ctx);
580 if (NULL != uri_string)
583 uri = GNUNET_FS_uri_parse (uri_string, &emsg);
586 FPRINTF (stderr, _("Failed to parse URI: %s\n"), emsg);
588 if (namespace != NULL)
589 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
590 GNUNET_FS_stop (ctx);
594 GNUNET_FS_publish_ksk (ctx, topKeywords, meta, uri, &bo,
595 GNUNET_FS_PUBLISH_OPTION_NONE, &uri_ksk_continuation,
597 if (namespace != NULL)
598 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
602 GNUNET_CONFIGURATION_get_value_string (cfg, "FS", "EXTRACTORS", &ex))
605 ds = GNUNET_FS_directory_scan_start (args[0],
608 &directory_scan_cb, NULL);
610 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_stop_task,
616 * The main function to publish content to GNUnet.
618 * @param argc number of arguments from the command line
619 * @param argv command line arguments
620 * @return 0 ok, 1 on error
623 main (int argc, char *const *argv)
625 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
626 {'a', "anonymity", "LEVEL",
627 gettext_noop ("set the desired LEVEL of sender-anonymity"),
628 1, &GNUNET_GETOPT_set_uint, &bo.anonymity_level},
629 {'d', "disable-creation-time", NULL,
631 ("disable adding the creation time to the metadata of the uploaded file"),
632 0, &GNUNET_GETOPT_set_one, &do_disable_creation_time},
633 {'D', "disable-extractor", NULL,
634 gettext_noop ("do not use libextractor to add keywords or metadata"),
635 0, &GNUNET_GETOPT_set_one, &disable_extractor},
636 {'e', "extract", NULL,
638 ("print list of extracted keywords that would be used, but do not perform upload"),
639 0, &GNUNET_GETOPT_set_one, &extract_only},
640 {'k', "key", "KEYWORD",
642 ("add an additional keyword for the top-level file or directory"
643 " (this option can be specified multiple times)"),
644 1, &GNUNET_FS_getopt_set_keywords, &topKeywords},
645 {'m', "meta", "TYPE:VALUE",
646 gettext_noop ("set the meta-data for the given TYPE to the given VALUE"),
647 1, &GNUNET_FS_getopt_set_metadata, &meta},
648 {'n', "noindex", NULL,
649 gettext_noop ("do not index, perform full insertion (stores entire "
650 "file in encrypted form in GNUnet database)"),
651 0, &GNUNET_GETOPT_set_one, &do_insert},
654 ("specify ID of an updated version to be published in the future"
655 " (for namespace insertions only)"),
656 1, &GNUNET_GETOPT_set_string, &next_id},
657 {'p', "priority", "PRIORITY",
658 gettext_noop ("specify the priority of the content"),
659 1, &GNUNET_GETOPT_set_uint, &bo.content_priority},
660 {'P', "pseudonym", "NAME",
662 ("publish the files under the pseudonym NAME (place file into namespace)"),
663 1, &GNUNET_GETOPT_set_string, &pseudonym},
664 {'r', "replication", "LEVEL",
665 gettext_noop ("set the desired replication LEVEL"),
666 1, &GNUNET_GETOPT_set_uint, &bo.replication_level},
667 {'s', "simulate-only", NULL,
668 gettext_noop ("only simulate the process but do not do any "
669 "actual publishing (useful to compute URIs)"),
670 0, &GNUNET_GETOPT_set_one, &do_simulate},
672 gettext_noop ("set the ID of this version of the publication"
673 " (for namespace insertions only)"),
674 1, &GNUNET_GETOPT_set_string, &this_id},
676 gettext_noop ("URI to be published (can be used instead of passing a "
677 "file to add keywords to the file with the respective URI)"),
678 1, &GNUNET_GETOPT_set_string, &uri_string},
679 {'V', "verbose", NULL,
680 gettext_noop ("be verbose (print progress information)"),
681 0, &GNUNET_GETOPT_set_one, &verbose},
682 GNUNET_GETOPT_OPTION_END
684 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
685 "GNUnet publish starts\n");
687 GNUNET_FS_year_to_time (GNUNET_FS_get_current_year () + 2);
689 GNUNET_PROGRAM_run (argc, argv, "gnunet-publish [OPTIONS] FILENAME",
691 ("Publish a file or directory on GNUnet"),
692 options, &run, NULL)) ? ret : 1;
695 /* end of gnunet-publish.c */