2 This file is part of GNUnet.
3 (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 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 unsigned int anonymity = 1;
51 static unsigned int priority = 365;
53 static char *uri_string;
59 static char *pseudonym;
63 static int disable_extractor;
65 static int do_simulate;
67 static int extract_only;
69 static int do_disable_creation_time;
73 do_stop_task (void *cls,
74 const struct GNUNET_SCHEDULER_TaskContext *tc)
76 struct GNUNET_FS_PublishContext *p;
82 GNUNET_FS_publish_stop (p);
88 * Called by FS client to give information about the progress of an
92 * @param info details about the event, specifying the event type
93 * and various bits about the event
94 * @return client-context (for the next progress call
95 * for this operation; should be set to NULL for
96 * SUSPEND and STOPPED events). The value returned
97 * will be passed to future callbacks in the respective
98 * field in the GNUNET_FS_ProgressInfo struct.
101 progress_cb (void *cls,
102 const struct GNUNET_FS_ProgressInfo *info)
106 switch (info->status)
108 case GNUNET_FS_STATUS_PUBLISH_START:
110 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
113 s = GNUNET_STRINGS_relative_time_to_string(info->value.publish.eta);
115 _("Publishing `%s' at %llu/%llu (%s remaining)\n"),
116 info->value.publish.filename,
117 (unsigned long long) info->value.publish.completed,
118 (unsigned long long) info->value.publish.size,
123 case GNUNET_FS_STATUS_PUBLISH_ERROR:
125 _("Error publishing: %s.\n"),
126 info->value.publish.specifics.error.message);
127 GNUNET_SCHEDULER_add_continuation (sched,
130 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
132 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
134 _("Publishing `%s' done.\n"),
135 info->value.publish.filename);
136 if (info->value.publish.pctx == NULL)
137 GNUNET_SCHEDULER_add_continuation (sched,
140 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
142 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
143 GNUNET_break (NULL == pc);
147 _("Unexpected status: %d\n"),
151 return ""; /* non-null */
156 * Print metadata entries (except binary
157 * metadata and the filename).
160 * @param plugin_name name of the plugin that generated the meta data
161 * @param type type of the meta data
162 * @param format format of data
163 * @param data_mime_type mime type of data
164 * @param data value of the meta data
165 * @param data_size number of bytes in data
169 meta_printer (void *cls,
170 const char *plugin_name,
171 enum EXTRACTOR_MetaType type,
172 enum EXTRACTOR_MetaFormat format,
173 const char *data_mime_type,
177 if ( (format != EXTRACTOR_METAFORMAT_UTF8) &&
178 (format != EXTRACTOR_METAFORMAT_C_STRING) )
180 if (type == EXTRACTOR_METATYPE_FILENAME)
184 EXTRACTOR_metatype_to_string (type),
191 * Merge metadata entries.
193 * @param cls closure, target metadata structure
194 * @param plugin_name name of the plugin that generated the meta data
195 * @param type type of the meta data
196 * @param format format of data
197 * @param data_mime_type mime type of data
198 * @param data value of the meta data
199 * @param data_size number of bytes in data
203 meta_merger (void *cls,
204 const char *plugin_name,
205 enum EXTRACTOR_MetaType type,
206 enum EXTRACTOR_MetaFormat format,
207 const char *data_mime_type,
211 struct GNUNET_CONTAINER_MetaData *m = cls;
213 GNUNET_CONTAINER_meta_data_insert (m,
225 * Function called on all entries before the publication. This is
226 * where we perform modifications to the default based on command-line
230 * @param fi the entry in the publish-structure
231 * @param length length of the file or directory
232 * @param m metadata for the file or directory (can be modified)
233 * @param uri pointer to the keywords that will be used for this entry (can be modified)
234 * @param anonymity pointer to selected anonymity level (can be modified)
235 * @param priority pointer to selected priority (can be modified)
236 * @param expirationTime pointer to selected expiration time (can be modified)
237 * @param client_info pointer to client context set upon creation (can be modified)
238 * @return GNUNET_OK to continue, GNUNET_NO to remove
239 * this entry from the directory, GNUNET_SYSERR
240 * to abort the iteration
243 publish_inspector (void *cls,
244 struct GNUNET_FS_FileInformation *fi,
246 struct GNUNET_CONTAINER_MetaData *m,
247 struct GNUNET_FS_Uri **uri,
248 unsigned int *anonymity,
249 unsigned int *priority,
250 struct GNUNET_TIME_Absolute *expirationTime,
255 struct GNUNET_FS_Uri *new_uri;
257 if (NULL != topKeywords)
261 new_uri = GNUNET_FS_uri_ksk_merge (topKeywords,
263 GNUNET_FS_uri_destroy (*uri);
265 GNUNET_FS_uri_destroy (topKeywords);
275 GNUNET_CONTAINER_meta_data_iterate (meta,
278 GNUNET_CONTAINER_meta_data_destroy (meta);
281 if (! do_disable_creation_time)
282 GNUNET_CONTAINER_meta_data_add_publication_date (m);
285 fn = GNUNET_CONTAINER_meta_data_get_by_type (m,
286 EXTRACTOR_METATYPE_FILENAME);
287 fs = GNUNET_STRINGS_byte_size_fancy (length);
289 _("Keywords for file `%s' (%s)\n"),
294 GNUNET_CONTAINER_meta_data_iterate (m,
297 fprintf (stdout, "\n");
299 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (m))
300 GNUNET_FS_file_information_inspect (fi,
308 uri_ksk_continuation (void *cls,
309 const struct GNUNET_FS_Uri *uri,
319 GNUNET_FS_stop (ctx);
324 * Main function that will be run by the scheduler.
327 * @param s the scheduler to use
328 * @param args remaining command-line arguments
329 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
330 * @param c configuration
334 struct GNUNET_SCHEDULER_Handle *s,
337 const struct GNUNET_CONFIGURATION_Handle *c)
339 struct GNUNET_FS_FileInformation *fi;
340 struct GNUNET_FS_Namespace *namespace;
341 struct EXTRACTOR_PluginList *l;
345 struct GNUNET_FS_Uri *uri;
348 /* check arguments */
349 if ( ( (uri_string == NULL) || (extract_only) )
350 && ( (args[0] == NULL) || (args[1] != NULL) ) )
353 ("You must specify one and only one filename for insertion.\n"));
357 if ((uri_string != NULL) && (args[0] != NULL))
359 printf (_("You must NOT specify an URI and a filename.\n"));
363 if ((uri_string != NULL) && (extract_only))
365 printf (_("Cannot extract metadata from a URI!\n"));
369 if (pseudonym != NULL)
374 _("Option `%s' is required when using option `%s'.\n"),
381 { /* ordinary insertion checks */
385 _("Option `%s' makes no sense without option `%s'.\n"),
393 _("Option `%s' makes no sense without option `%s'.\n"),
402 _("Need the name of a file to publish!\n"));
407 ctx = GNUNET_FS_start (sched,
412 GNUNET_FS_FLAGS_NONE,
413 GNUNET_FS_OPTIONS_END);
417 _("Could not initialize `%s' subsystem.\n"),
423 if (NULL != pseudonym)
425 namespace = GNUNET_FS_namespace_create (ctx,
427 if (NULL == namespace)
430 _("Could not create namespace `%s'\n"),
432 GNUNET_FS_stop (ctx);
437 if (NULL != uri_string)
440 uri = GNUNET_FS_uri_parse (uri_string,
445 _("Failed to parse URI: %s\n"),
448 GNUNET_FS_stop (ctx);
452 GNUNET_FS_publish_ksk (ctx,
456 GNUNET_TIME_relative_to_absolute (DEFAULT_EXPIRATION),
459 GNUNET_FS_PUBLISH_OPTION_NONE,
460 &uri_ksk_continuation,
465 if (! disable_extractor)
467 l = EXTRACTOR_plugin_add_defaults (EXTRACTOR_OPTION_DEFAULT_POLICY);
469 GNUNET_CONFIGURATION_get_value_string (cfg, "FS", "EXTRACTORS",
473 l = EXTRACTOR_plugin_add_config (l, ex, EXTRACTOR_OPTION_DEFAULT_POLICY);
478 if (0 != STAT (args[0], &sbuf))
480 GNUNET_asprintf (&emsg,
481 _("Could not access file: %s\n"),
485 else if (S_ISDIR (sbuf.st_mode))
487 fi = GNUNET_FS_file_information_create_from_directory (NULL,
489 &GNUNET_FS_directory_scanner_default,
494 GNUNET_TIME_relative_to_absolute (DEFAULT_EXPIRATION),
499 fi = GNUNET_FS_file_information_create_from_file (NULL,
506 GNUNET_TIME_relative_to_absolute (DEFAULT_EXPIRATION));
507 GNUNET_break (fi != NULL);
509 EXTRACTOR_plugin_remove_all (l);
513 _("Could not publish `%s': %s\n"),
517 if (namespace != NULL)
518 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
519 GNUNET_FS_stop (ctx);
523 GNUNET_FS_file_information_inspect (fi,
528 if (namespace != NULL)
529 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
530 GNUNET_FS_file_information_destroy (fi, NULL, NULL);
531 GNUNET_FS_stop (ctx);
534 pc = GNUNET_FS_publish_start (ctx,
540 ? GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY
541 : GNUNET_FS_PUBLISH_OPTION_NONE);
545 _("Could not start publishing.\n"));
546 GNUNET_FS_stop (ctx);
554 * gnunet-publish command line options
556 static struct GNUNET_GETOPT_CommandLineOption options[] = {
557 {'a', "anonymity", "LEVEL",
558 gettext_noop ("set the desired LEVEL of sender-anonymity"),
559 1, &GNUNET_GETOPT_set_uint, &anonymity},
560 {'d', "disable-creation-time", NULL,
562 ("disable adding the creation time to the metadata of the uploaded file"),
563 0, &GNUNET_GETOPT_set_one, &do_disable_creation_time},
564 {'D', "disable-extractor", NULL,
566 ("do not use libextractor to add keywords or metadata"),
567 0, &GNUNET_GETOPT_set_one, &disable_extractor},
568 {'e', "extract", NULL,
570 ("print list of extracted keywords that would be used, but do not perform upload"),
571 0, &GNUNET_GETOPT_set_one, &extract_only},
572 {'k', "key", "KEYWORD",
574 ("add an additional keyword for the top-level file or directory"
575 " (this option can be specified multiple times)"),
576 1, &GNUNET_FS_getopt_set_keywords, &topKeywords},
577 // *: option not yet used... (can handle in a pass over FI)
578 {'m', "meta", "TYPE:VALUE",
579 gettext_noop ("set the meta-data for the given TYPE to the given VALUE"),
580 1, &GNUNET_FS_getopt_set_metadata, &meta},
581 {'n', "noindex", NULL,
582 gettext_noop ("do not index, perform full insertion (stores entire "
583 "file in encrypted form in GNUnet database)"),
584 0, &GNUNET_GETOPT_set_one, &do_insert},
587 ("specify ID of an updated version to be published in the future"
588 " (for namespace insertions only)"),
589 1, &GNUNET_GETOPT_set_string, &next_id},
590 {'p', "priority", "PRIORITY",
591 gettext_noop ("specify the priority of the content"),
592 1, &GNUNET_GETOPT_set_uint, &priority},
593 {'P', "pseudonym", "NAME",
595 ("publish the files under the pseudonym NAME (place file into namespace)"),
596 1, &GNUNET_GETOPT_set_string, &pseudonym},
597 // *: option not yet used... (need FS API support!)
598 {'s', "simulate-only", NULL,
599 gettext_noop ("only simulate the process but do not do any "
600 "actual publishing (useful to compute URIs)"),
601 0, &GNUNET_GETOPT_set_one, &do_simulate},
603 gettext_noop ("set the ID of this version of the publication"
604 " (for namespace insertions only)"),
605 1, &GNUNET_GETOPT_set_string, &this_id},
606 // *: option not yet used... (need FS API support!)
608 gettext_noop ("URI to be published (can be used instead of passing a "
609 "file to add keywords to the file with the respective URI)"),
610 1, &GNUNET_GETOPT_set_string, &uri_string},
611 {'V', "verbose", NULL,
612 gettext_noop ("be verbose (print progress information)"),
613 0, &GNUNET_GETOPT_set_one, &verbose},
614 GNUNET_GETOPT_OPTION_END
619 * The main function to publish content to GNUnet.
621 * @param argc number of arguments from the command line
622 * @param argv command line arguments
623 * @return 0 ok, 1 on error
626 main (int argc, char *const *argv)
629 GNUNET_PROGRAM_run (argc,
633 ("Publish files on GNUnet."),
634 options, &run, NULL)) ? ret : 1;
637 /* end of gnunet-publish.c */
639 ////////////////////////////////////////////////////////////////
643 * Print progess message.
646 printstatus (void *ctx, const GNUNET_FSUI_Event * event)
648 unsigned long long delta;
653 case GNUNET_FSUI_upload_progress:
659 now = GNUNET_get_time ();
660 delta = event->data.UploadProgress.eta - now;
661 if (event->data.UploadProgress.eta < now)
663 ret = GNUNET_get_time_interval_as_fancy_string (delta);
664 PRINTF (_("%16llu of %16llu bytes inserted "
665 "(estimating %6s to completion) - %s\n"),
666 event->data.UploadProgress.completed,
667 event->data.UploadProgress.total,
668 ret, event->data.UploadProgress.filename);
672 case GNUNET_FSUI_upload_completed:
675 delta = GNUNET_get_time () - start_time;
676 PRINTF (_("Upload of `%s' complete, "
677 "%llu bytes took %llu seconds (%8.3f KiB/s).\n"),
678 event->data.UploadCompleted.filename,
679 event->data.UploadCompleted.total,
680 delta / GNUNET_CRON_SECONDS,
683 : (double) (event->data.UploadCompleted.total
684 / 1024.0 * GNUNET_CRON_SECONDS / delta));
686 fstring = GNUNET_ECRS_uri_to_string (event->data.UploadCompleted.uri);
687 printf (_("File `%s' has URI: %s\n"),
688 event->data.UploadCompleted.filename, fstring);
689 GNUNET_free (fstring);
690 if (ul == event->data.UploadCompleted.uc.pos)
692 postProcess (event->data.UploadCompleted.uri);
694 GNUNET_shutdown_initiate ();
697 case GNUNET_FSUI_upload_aborted:
698 printf (_("\nUpload aborted.\n"));
700 GNUNET_shutdown_initiate ();
702 case GNUNET_FSUI_upload_error:
703 printf (_("\nError uploading file: %s"),
704 event->data.UploadError.message);
706 GNUNET_shutdown_initiate ();
708 case GNUNET_FSUI_upload_started:
709 case GNUNET_FSUI_upload_stopped:
712 printf (_("\nUnexpected event: %d\n"), event->type);
713 GNUNET_GE_BREAK (ectx, 0);
720 /* end of gnunet-publish.c */