2 This file is part of GNUnet.
3 (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009, 2010 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;
71 static GNUNET_SCHEDULER_TaskIdentifier kill_task;
75 do_stop_task (void *cls,
76 const struct GNUNET_SCHEDULER_TaskContext *tc)
78 struct GNUNET_FS_PublishContext *p;
84 GNUNET_FS_publish_stop (p);
90 * Called by FS client to give information about the progress of an
94 * @param info details about the event, specifying the event type
95 * and various bits about the event
96 * @return client-context (for the next progress call
97 * for this operation; should be set to NULL for
98 * SUSPEND and STOPPED events). The value returned
99 * will be passed to future callbacks in the respective
100 * field in the GNUNET_FS_ProgressInfo struct.
103 progress_cb (void *cls,
104 const struct GNUNET_FS_ProgressInfo *info)
108 switch (info->status)
110 case GNUNET_FS_STATUS_PUBLISH_START:
112 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
115 s = GNUNET_STRINGS_relative_time_to_string(info->value.publish.eta);
117 _("Publishing `%s' at %llu/%llu (%s remaining)\n"),
118 info->value.publish.filename,
119 (unsigned long long) info->value.publish.completed,
120 (unsigned long long) info->value.publish.size,
125 case GNUNET_FS_STATUS_PUBLISH_ERROR:
127 _("Error publishing: %s.\n"),
128 info->value.publish.specifics.error.message);
129 if (kill_task != GNUNET_SCHEDULER_NO_TASK)
131 GNUNET_SCHEDULER_cancel (sched,
133 kill_task = GNUNET_SCHEDULER_NO_TASK;
135 GNUNET_SCHEDULER_add_continuation (sched,
138 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
140 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
142 _("Publishing `%s' done.\n"),
143 info->value.publish.filename);
144 s = GNUNET_FS_uri_to_string (info->value.publish.specifics.completed.chk_uri);
149 if (info->value.publish.pctx == NULL)
151 if (kill_task != GNUNET_SCHEDULER_NO_TASK)
153 GNUNET_SCHEDULER_cancel (sched,
155 kill_task = GNUNET_SCHEDULER_NO_TASK;
157 GNUNET_SCHEDULER_add_continuation (sched,
160 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
163 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
164 GNUNET_break (NULL == pc);
168 _("Unexpected status: %d\n"),
172 return ""; /* non-null */
177 * Print metadata entries (except binary
178 * metadata and the filename).
181 * @param plugin_name name of the plugin that generated the meta data
182 * @param type type of the meta data
183 * @param format format of data
184 * @param data_mime_type mime type of data
185 * @param data value of the meta data
186 * @param data_size number of bytes in data
190 meta_printer (void *cls,
191 const char *plugin_name,
192 enum EXTRACTOR_MetaType type,
193 enum EXTRACTOR_MetaFormat format,
194 const char *data_mime_type,
198 if ( (format != EXTRACTOR_METAFORMAT_UTF8) &&
199 (format != EXTRACTOR_METAFORMAT_C_STRING) )
201 if (type == EXTRACTOR_METATYPE_FILENAME)
205 EXTRACTOR_metatype_to_string (type),
212 * Merge metadata entries.
214 * @param cls closure, target metadata structure
215 * @param plugin_name name of the plugin that generated the meta data
216 * @param type type of the meta data
217 * @param format format of data
218 * @param data_mime_type mime type of data
219 * @param data value of the meta data
220 * @param data_size number of bytes in data
224 meta_merger (void *cls,
225 const char *plugin_name,
226 enum EXTRACTOR_MetaType type,
227 enum EXTRACTOR_MetaFormat format,
228 const char *data_mime_type,
232 struct GNUNET_CONTAINER_MetaData *m = cls;
234 GNUNET_CONTAINER_meta_data_insert (m,
246 * Function called on all entries before the publication. This is
247 * where we perform modifications to the default based on command-line
251 * @param fi the entry in the publish-structure
252 * @param length length of the file or directory
253 * @param m metadata for the file or directory (can be modified)
254 * @param uri pointer to the keywords that will be used for this entry (can be modified)
255 * @param anonymity pointer to selected anonymity level (can be modified)
256 * @param priority pointer to selected priority (can be modified)
257 * @param expirationTime pointer to selected expiration time (can be modified)
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,
265 struct GNUNET_FS_FileInformation *fi,
267 struct GNUNET_CONTAINER_MetaData *m,
268 struct GNUNET_FS_Uri **uri,
269 unsigned int *anonymity,
270 unsigned int *priority,
271 struct GNUNET_TIME_Absolute *expirationTime,
276 struct GNUNET_FS_Uri *new_uri;
278 if (NULL != topKeywords)
282 new_uri = GNUNET_FS_uri_ksk_merge (topKeywords,
284 GNUNET_FS_uri_destroy (*uri);
286 GNUNET_FS_uri_destroy (topKeywords);
296 GNUNET_CONTAINER_meta_data_iterate (meta,
299 GNUNET_CONTAINER_meta_data_destroy (meta);
302 if (! do_disable_creation_time)
303 GNUNET_CONTAINER_meta_data_add_publication_date (m);
306 fn = GNUNET_CONTAINER_meta_data_get_by_type (m,
307 EXTRACTOR_METATYPE_FILENAME);
308 fs = GNUNET_STRINGS_byte_size_fancy (length);
310 _("Keywords for file `%s' (%s)\n"),
315 GNUNET_CONTAINER_meta_data_iterate (m,
318 fprintf (stdout, "\n");
320 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (m))
321 GNUNET_FS_file_information_inspect (fi,
329 uri_ksk_continuation (void *cls,
330 const struct GNUNET_FS_Uri *uri,
340 GNUNET_FS_stop (ctx);
345 * Main function that will be run by the scheduler.
348 * @param s the scheduler to use
349 * @param args remaining command-line arguments
350 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
351 * @param c configuration
355 struct GNUNET_SCHEDULER_Handle *s,
358 const struct GNUNET_CONFIGURATION_Handle *c)
360 struct GNUNET_FS_FileInformation *fi;
361 struct GNUNET_FS_Namespace *namespace;
362 struct EXTRACTOR_PluginList *l;
366 struct GNUNET_FS_Uri *uri;
369 /* check arguments */
370 if ( ( (uri_string == NULL) || (extract_only) )
371 && ( (args[0] == NULL) || (args[1] != NULL) ) )
374 ("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 ((uri_string != NULL) && (extract_only))
386 printf (_("Cannot extract metadata from a URI!\n"));
390 if (pseudonym != NULL)
395 _("Option `%s' is required when using option `%s'.\n"),
402 { /* ordinary insertion checks */
406 _("Option `%s' makes no sense without option `%s'.\n"),
414 _("Option `%s' makes no sense without option `%s'.\n"),
423 _("Need the name of a file to publish!\n"));
428 ctx = GNUNET_FS_start (sched,
433 GNUNET_FS_FLAGS_NONE,
434 GNUNET_FS_OPTIONS_END);
438 _("Could not initialize `%s' subsystem.\n"),
444 if (NULL != pseudonym)
446 namespace = GNUNET_FS_namespace_create (ctx,
448 if (NULL == namespace)
451 _("Could not create namespace `%s'\n"),
453 GNUNET_FS_stop (ctx);
458 if (NULL != uri_string)
461 uri = GNUNET_FS_uri_parse (uri_string,
466 _("Failed to parse URI: %s\n"),
469 GNUNET_FS_stop (ctx);
473 GNUNET_FS_publish_ksk (ctx,
477 GNUNET_TIME_relative_to_absolute (DEFAULT_EXPIRATION),
480 GNUNET_FS_PUBLISH_OPTION_NONE,
481 &uri_ksk_continuation,
486 if (! disable_extractor)
488 l = EXTRACTOR_plugin_add_defaults (EXTRACTOR_OPTION_DEFAULT_POLICY);
490 GNUNET_CONFIGURATION_get_value_string (cfg, "FS", "EXTRACTORS",
494 l = EXTRACTOR_plugin_add_config (l, ex, EXTRACTOR_OPTION_DEFAULT_POLICY);
499 if (0 != STAT (args[0], &sbuf))
501 GNUNET_asprintf (&emsg,
502 _("Could not access file: %s\n"),
506 else if (S_ISDIR (sbuf.st_mode))
508 fi = GNUNET_FS_file_information_create_from_directory (NULL,
510 &GNUNET_FS_directory_scanner_default,
515 GNUNET_TIME_relative_to_absolute (DEFAULT_EXPIRATION),
520 fi = GNUNET_FS_file_information_create_from_file (NULL,
527 GNUNET_TIME_relative_to_absolute (DEFAULT_EXPIRATION));
528 GNUNET_break (fi != NULL);
530 EXTRACTOR_plugin_remove_all (l);
534 _("Could not publish `%s': %s\n"),
538 if (namespace != NULL)
539 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
540 GNUNET_FS_stop (ctx);
544 GNUNET_FS_file_information_inspect (fi,
549 if (namespace != NULL)
550 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
551 GNUNET_FS_file_information_destroy (fi, NULL, NULL);
552 GNUNET_FS_stop (ctx);
555 pc = GNUNET_FS_publish_start (ctx,
561 ? GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY
562 : GNUNET_FS_PUBLISH_OPTION_NONE);
566 _("Could not start publishing.\n"));
567 GNUNET_FS_stop (ctx);
571 kill_task = GNUNET_SCHEDULER_add_delayed (sched,
572 GNUNET_TIME_UNIT_FOREVER_REL,
579 * gnunet-publish command line options
581 static struct GNUNET_GETOPT_CommandLineOption options[] = {
582 {'a', "anonymity", "LEVEL",
583 gettext_noop ("set the desired LEVEL of sender-anonymity"),
584 1, &GNUNET_GETOPT_set_uint, &anonymity},
585 {'d', "disable-creation-time", NULL,
587 ("disable adding the creation time to the metadata of the uploaded file"),
588 0, &GNUNET_GETOPT_set_one, &do_disable_creation_time},
589 {'D', "disable-extractor", NULL,
591 ("do not use libextractor to add keywords or metadata"),
592 0, &GNUNET_GETOPT_set_one, &disable_extractor},
593 {'e', "extract", NULL,
595 ("print list of extracted keywords that would be used, but do not perform upload"),
596 0, &GNUNET_GETOPT_set_one, &extract_only},
597 {'k', "key", "KEYWORD",
599 ("add an additional keyword for the top-level file or directory"
600 " (this option can be specified multiple times)"),
601 1, &GNUNET_FS_getopt_set_keywords, &topKeywords},
602 // *: option not yet used... (can handle in a pass over FI)
603 {'m', "meta", "TYPE:VALUE",
604 gettext_noop ("set the meta-data for the given TYPE to the given VALUE"),
605 1, &GNUNET_FS_getopt_set_metadata, &meta},
606 {'n', "noindex", NULL,
607 gettext_noop ("do not index, perform full insertion (stores entire "
608 "file in encrypted form in GNUnet database)"),
609 0, &GNUNET_GETOPT_set_one, &do_insert},
612 ("specify ID of an updated version to be published in the future"
613 " (for namespace insertions only)"),
614 1, &GNUNET_GETOPT_set_string, &next_id},
615 {'p', "priority", "PRIORITY",
616 gettext_noop ("specify the priority of the content"),
617 1, &GNUNET_GETOPT_set_uint, &priority},
618 {'P', "pseudonym", "NAME",
620 ("publish the files under the pseudonym NAME (place file into namespace)"),
621 1, &GNUNET_GETOPT_set_string, &pseudonym},
622 // *: option not yet used... (need FS API support!)
623 {'s', "simulate-only", NULL,
624 gettext_noop ("only simulate the process but do not do any "
625 "actual publishing (useful to compute URIs)"),
626 0, &GNUNET_GETOPT_set_one, &do_simulate},
628 gettext_noop ("set the ID of this version of the publication"
629 " (for namespace insertions only)"),
630 1, &GNUNET_GETOPT_set_string, &this_id},
631 // *: option not yet used... (need FS API support!)
633 gettext_noop ("URI to be published (can be used instead of passing a "
634 "file to add keywords to the file with the respective URI)"),
635 1, &GNUNET_GETOPT_set_string, &uri_string},
636 {'V', "verbose", NULL,
637 gettext_noop ("be verbose (print progress information)"),
638 0, &GNUNET_GETOPT_set_one, &verbose},
639 GNUNET_GETOPT_OPTION_END
644 * The main function to publish content to GNUnet.
646 * @param argc number of arguments from the command line
647 * @param argv command line arguments
648 * @return 0 ok, 1 on error
651 main (int argc, char *const *argv)
654 GNUNET_PROGRAM_run (argc,
658 ("Publish files on GNUnet."),
659 options, &run, NULL)) ? ret : 1;
662 /* end of gnunet-publish.c */
664 ////////////////////////////////////////////////////////////////
668 * Print progess message.
671 printstatus (void *ctx, const GNUNET_FSUI_Event * event)
673 unsigned long long delta;
678 case GNUNET_FSUI_upload_progress:
684 now = GNUNET_get_time ();
685 delta = event->data.UploadProgress.eta - now;
686 if (event->data.UploadProgress.eta < now)
688 ret = GNUNET_get_time_interval_as_fancy_string (delta);
689 PRINTF (_("%16llu of %16llu bytes inserted "
690 "(estimating %6s to completion) - %s\n"),
691 event->data.UploadProgress.completed,
692 event->data.UploadProgress.total,
693 ret, event->data.UploadProgress.filename);
697 case GNUNET_FSUI_upload_completed:
700 delta = GNUNET_get_time () - start_time;
701 PRINTF (_("Upload of `%s' complete, "
702 "%llu bytes took %llu seconds (%8.3f KiB/s).\n"),
703 event->data.UploadCompleted.filename,
704 event->data.UploadCompleted.total,
705 delta / GNUNET_CRON_SECONDS,
708 : (double) (event->data.UploadCompleted.total
709 / 1024.0 * GNUNET_CRON_SECONDS / delta));
711 fstring = GNUNET_ECRS_uri_to_string (event->data.UploadCompleted.uri);
712 printf (_("File `%s' has URI: %s\n"),
713 event->data.UploadCompleted.filename, fstring);
714 GNUNET_free (fstring);
715 if (ul == event->data.UploadCompleted.uc.pos)
717 postProcess (event->data.UploadCompleted.uri);
719 GNUNET_shutdown_initiate ();
722 case GNUNET_FSUI_upload_aborted:
723 printf (_("\nUpload aborted.\n"));
725 GNUNET_shutdown_initiate ();
727 case GNUNET_FSUI_upload_error:
728 printf (_("\nError uploading file: %s"),
729 event->data.UploadError.message);
731 GNUNET_shutdown_initiate ();
733 case GNUNET_FSUI_upload_started:
734 case GNUNET_FSUI_upload_stopped:
737 printf (_("\nUnexpected event: %d\n"), event->type);
738 GNUNET_GE_BREAK (ectx, 0);
745 /* end of gnunet-publish.c */