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 * - support for some options is still missing (uri argument)
32 #include "gnunet_fs_service.h"
34 #define DEFAULT_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_YEARS, 2)
40 static const struct GNUNET_CONFIGURATION_Handle *cfg;
42 static struct GNUNET_FS_Handle *ctx;
44 static struct GNUNET_SCHEDULER_Handle *sched;
46 static struct GNUNET_FS_PublishContext *pc;
48 static struct GNUNET_CONTAINER_MetaData *meta;
50 static struct GNUNET_FS_Uri *topKeywords;
52 static unsigned int anonymity = 1;
54 static unsigned int priority = 365;
56 static char *uri_string;
62 static char *pseudonym;
66 static int disable_extractor;
68 static int do_simulate;
70 static int extract_only;
72 static int do_disable_creation_time;
76 do_stop_task (void *cls,
77 const struct GNUNET_SCHEDULER_TaskContext *tc)
79 struct GNUNET_FS_PublishContext *p;
85 GNUNET_FS_publish_stop (p);
91 * Called by FS client to give information about the progress of an
95 * @param info details about the event, specifying the event type
96 * and various bits about the event
97 * @return client-context (for the next progress call
98 * for this operation; should be set to NULL for
99 * SUSPEND and STOPPED events). The value returned
100 * will be passed to future callbacks in the respective
101 * field in the GNUNET_FS_ProgressInfo struct.
104 progress_cb (void *cls,
105 const struct GNUNET_FS_ProgressInfo *info)
109 switch (info->status)
111 case GNUNET_FS_STATUS_PUBLISH_START:
113 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
116 s = GNUNET_STRINGS_relative_time_to_string(info->value.publish.eta);
118 _("Publishing `%s' at %llu/%llu (%s remaining)\n"),
119 info->value.publish.filename,
120 (unsigned long long) info->value.publish.completed,
121 (unsigned long long) info->value.publish.size,
126 case GNUNET_FS_STATUS_PUBLISH_ERROR:
128 _("Error publishing: %s.\n"),
129 info->value.publish.specifics.error.message);
130 GNUNET_SCHEDULER_add_continuation (sched,
133 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
135 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
137 _("Publishing `%s' done.\n"),
138 info->value.publish.filename);
139 if (info->value.publish.pctx == NULL)
140 GNUNET_SCHEDULER_add_continuation (sched,
143 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
145 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
146 GNUNET_break (NULL == pc);
150 _("Unexpected status: %d\n"),
154 return ""; /* non-null */
159 * Print metadata entries (except binary
160 * metadata and the filename).
163 * @param plugin_name name of the plugin that generated the meta data
164 * @param type type of the meta data
165 * @param format format of data
166 * @param data_mime_type mime type of data
167 * @param data value of the meta data
168 * @param data_size number of bytes in data
172 meta_printer (void *cls,
173 const char *plugin_name,
174 enum EXTRACTOR_MetaType type,
175 enum EXTRACTOR_MetaFormat format,
176 const char *data_mime_type,
180 if ( (format != EXTRACTOR_METAFORMAT_UTF8) &&
181 (format != EXTRACTOR_METAFORMAT_C_STRING) )
183 if (type == EXTRACTOR_METATYPE_FILENAME)
187 EXTRACTOR_metatype_to_string (type),
194 * Merge metadata entries.
196 * @param cls closure, target metadata structure
197 * @param plugin_name name of the plugin that generated the meta data
198 * @param type type of the meta data
199 * @param format format of data
200 * @param data_mime_type mime type of data
201 * @param data value of the meta data
202 * @param data_size number of bytes in data
206 meta_merger (void *cls,
207 const char *plugin_name,
208 enum EXTRACTOR_MetaType type,
209 enum EXTRACTOR_MetaFormat format,
210 const char *data_mime_type,
214 struct GNUNET_CONTAINER_MetaData *m = cls;
216 GNUNET_CONTAINER_meta_data_insert (m,
228 * Function called on all entries before the publication. This is
229 * where we perform modifications to the default based on command-line
233 * @param fi the entry in the publish-structure
234 * @param length length of the file or directory
235 * @param m metadata for the file or directory (can be modified)
236 * @param uri pointer to the keywords that will be used for this entry (can be modified)
237 * @param anonymity pointer to selected anonymity level (can be modified)
238 * @param priority pointer to selected priority (can be modified)
239 * @param expirationTime pointer to selected expiration time (can be modified)
240 * @param client_info pointer to client context set upon creation (can be modified)
241 * @return GNUNET_OK to continue, GNUNET_NO to remove
242 * this entry from the directory, GNUNET_SYSERR
243 * to abort the iteration
246 publish_inspector (void *cls,
247 struct GNUNET_FS_FileInformation *fi,
249 struct GNUNET_CONTAINER_MetaData *m,
250 struct GNUNET_FS_Uri **uri,
251 unsigned int *anonymity,
252 unsigned int *priority,
253 struct GNUNET_TIME_Absolute *expirationTime,
258 struct GNUNET_FS_Uri *new_uri;
260 if (NULL != topKeywords)
264 new_uri = GNUNET_FS_uri_ksk_merge (topKeywords,
266 GNUNET_FS_uri_destroy (*uri);
268 GNUNET_FS_uri_destroy (topKeywords);
278 GNUNET_CONTAINER_meta_data_iterate (meta,
281 GNUNET_CONTAINER_meta_data_destroy (meta);
284 if (! do_disable_creation_time)
285 GNUNET_CONTAINER_meta_data_add_publication_date (m);
288 fn = GNUNET_CONTAINER_meta_data_get_by_type (m,
289 EXTRACTOR_METATYPE_FILENAME);
290 fs = GNUNET_STRINGS_byte_size_fancy (length);
292 _("Keywords for file `%s' (%s)\n"),
297 GNUNET_CONTAINER_meta_data_iterate (m,
300 fprintf (stdout, "\n");
302 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (m))
303 GNUNET_FS_file_information_inspect (fi,
311 * Main function that will be run by the scheduler.
314 * @param s the scheduler to use
315 * @param args remaining command-line arguments
316 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
317 * @param c configuration
321 struct GNUNET_SCHEDULER_Handle *s,
324 const struct GNUNET_CONFIGURATION_Handle *c)
326 struct GNUNET_FS_FileInformation *fi;
327 struct GNUNET_FS_Namespace *namespace;
328 struct EXTRACTOR_PluginList *l;
334 /* check arguments */
335 if ( ( (uri_string == NULL) || (extract_only) )
336 && ( (args[0] == NULL) || (args[1] != NULL) ) )
339 ("You must specify one and only one filename for insertion.\n"));
343 if ((uri_string != NULL) && (args[0] != NULL))
345 printf (_("You must NOT specify an URI and a filename.\n"));
349 if ((uri_string != NULL) && (extract_only))
351 printf (_("Cannot extract metadata from a URI!\n"));
355 if (pseudonym != NULL)
360 _("Option `%s' is required when using option `%s'.\n"),
367 { /* ordinary insertion checks */
371 _("Option `%s' makes no sense without option `%s'.\n"),
379 _("Option `%s' makes no sense without option `%s'.\n"),
388 _("Need the name of a file to publish!\n"));
393 ctx = GNUNET_FS_start (sched,
398 GNUNET_FS_FLAGS_NONE,
399 GNUNET_FS_OPTIONS_END);
403 _("Could not initialize `%s' subsystem.\n"),
409 if (NULL != pseudonym)
411 namespace = GNUNET_FS_namespace_create (ctx,
413 if (NULL == namespace)
416 _("Could not create namespace `%s'\n"),
418 GNUNET_FS_stop (ctx);
423 if (NULL != uri_string)
425 // FIXME -- implement!
430 if (! disable_extractor)
432 l = EXTRACTOR_plugin_add_defaults (EXTRACTOR_OPTION_DEFAULT_POLICY);
434 GNUNET_CONFIGURATION_get_value_string (cfg, "FS", "EXTRACTORS",
438 l = EXTRACTOR_plugin_add_config (l, ex, EXTRACTOR_OPTION_DEFAULT_POLICY);
443 if (0 != STAT (args[0], &sbuf))
445 GNUNET_asprintf (&emsg,
446 _("Could not access file: %s\n"),
450 else if (S_ISDIR (sbuf.st_mode))
452 fi = GNUNET_FS_file_information_create_from_directory (NULL,
454 &GNUNET_FS_directory_scanner_default,
459 GNUNET_TIME_relative_to_absolute (DEFAULT_EXPIRATION),
464 fi = GNUNET_FS_file_information_create_from_file (NULL,
471 GNUNET_TIME_relative_to_absolute (DEFAULT_EXPIRATION));
472 GNUNET_break (fi != NULL);
474 EXTRACTOR_plugin_remove_all (l);
478 _("Could not publish `%s': %s\n"),
482 if (namespace != NULL)
483 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
484 GNUNET_FS_stop (ctx);
488 GNUNET_FS_file_information_inspect (fi,
493 if (namespace != NULL)
494 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
495 GNUNET_FS_file_information_destroy (fi, NULL, NULL);
496 GNUNET_FS_stop (ctx);
499 pc = GNUNET_FS_publish_start (ctx,
505 ? GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY
506 : GNUNET_FS_PUBLISH_OPTION_NONE);
510 _("Could not start publishing.\n"));
511 GNUNET_FS_stop (ctx);
519 * gnunet-publish command line options
521 static struct GNUNET_GETOPT_CommandLineOption options[] = {
522 {'a', "anonymity", "LEVEL",
523 gettext_noop ("set the desired LEVEL of sender-anonymity"),
524 1, &GNUNET_GETOPT_set_uint, &anonymity},
525 {'d', "disable-creation-time", NULL,
527 ("disable adding the creation time to the metadata of the uploaded file"),
528 0, &GNUNET_GETOPT_set_one, &do_disable_creation_time},
529 {'D', "disable-extractor", NULL,
531 ("do not use libextractor to add keywords or metadata"),
532 0, &GNUNET_GETOPT_set_one, &disable_extractor},
533 {'e', "extract", NULL,
535 ("print list of extracted keywords that would be used, but do not perform upload"),
536 0, &GNUNET_GETOPT_set_one, &extract_only},
537 {'k', "key", "KEYWORD",
539 ("add an additional keyword for the top-level file or directory"
540 " (this option can be specified multiple times)"),
541 1, &GNUNET_FS_getopt_set_keywords, &topKeywords},
542 // *: option not yet used... (can handle in a pass over FI)
543 {'m', "meta", "TYPE:VALUE",
544 gettext_noop ("set the meta-data for the given TYPE to the given VALUE"),
545 1, &GNUNET_FS_getopt_set_metadata, &meta},
546 {'n', "noindex", NULL,
547 gettext_noop ("do not index, perform full insertion (stores entire "
548 "file in encrypted form in GNUnet database)"),
549 0, &GNUNET_GETOPT_set_one, &do_insert},
552 ("specify ID of an updated version to be published in the future"
553 " (for namespace insertions only)"),
554 1, &GNUNET_GETOPT_set_string, &next_id},
555 {'p', "priority", "PRIORITY",
556 gettext_noop ("specify the priority of the content"),
557 1, &GNUNET_GETOPT_set_uint, &priority},
558 {'P', "pseudonym", "NAME",
560 ("publish the files under the pseudonym NAME (place file into namespace)"),
561 1, &GNUNET_GETOPT_set_string, &pseudonym},
562 // *: option not yet used... (need FS API support!)
563 {'s', "simulate-only", NULL,
564 gettext_noop ("only simulate the process but do not do any "
565 "actual publishing (useful to compute URIs)"),
566 0, &GNUNET_GETOPT_set_one, &do_simulate},
568 gettext_noop ("set the ID of this version of the publication"
569 " (for namespace insertions only)"),
570 1, &GNUNET_GETOPT_set_string, &this_id},
571 // *: option not yet used... (need FS API support!)
573 gettext_noop ("URI to be published (can be used instead of passing a "
574 "file to add keywords to the file with the respective URI)"),
575 1, &GNUNET_GETOPT_set_string, &uri_string},
576 {'V', "verbose", NULL,
577 gettext_noop ("be verbose (print progress information)"),
578 0, &GNUNET_GETOPT_set_one, &verbose},
579 GNUNET_GETOPT_OPTION_END
584 * The main function to publish content to GNUnet.
586 * @param argc number of arguments from the command line
587 * @param argv command line arguments
588 * @return 0 ok, 1 on error
591 main (int argc, char *const *argv)
594 GNUNET_PROGRAM_run (argc,
598 ("Publish files on GNUnet."),
599 options, &run, NULL)) ? ret : 1;
602 /* end of gnunet-publish.c */
604 ////////////////////////////////////////////////////////////////
608 * Print progess message.
611 printstatus (void *ctx, const GNUNET_FSUI_Event * event)
613 unsigned long long delta;
618 case GNUNET_FSUI_upload_progress:
624 now = GNUNET_get_time ();
625 delta = event->data.UploadProgress.eta - now;
626 if (event->data.UploadProgress.eta < now)
628 ret = GNUNET_get_time_interval_as_fancy_string (delta);
629 PRINTF (_("%16llu of %16llu bytes inserted "
630 "(estimating %6s to completion) - %s\n"),
631 event->data.UploadProgress.completed,
632 event->data.UploadProgress.total,
633 ret, event->data.UploadProgress.filename);
637 case GNUNET_FSUI_upload_completed:
640 delta = GNUNET_get_time () - start_time;
641 PRINTF (_("Upload of `%s' complete, "
642 "%llu bytes took %llu seconds (%8.3f KiB/s).\n"),
643 event->data.UploadCompleted.filename,
644 event->data.UploadCompleted.total,
645 delta / GNUNET_CRON_SECONDS,
648 : (double) (event->data.UploadCompleted.total
649 / 1024.0 * GNUNET_CRON_SECONDS / delta));
651 fstring = GNUNET_ECRS_uri_to_string (event->data.UploadCompleted.uri);
652 printf (_("File `%s' has URI: %s\n"),
653 event->data.UploadCompleted.filename, fstring);
654 GNUNET_free (fstring);
655 if (ul == event->data.UploadCompleted.uc.pos)
657 postProcess (event->data.UploadCompleted.uri);
659 GNUNET_shutdown_initiate ();
662 case GNUNET_FSUI_upload_aborted:
663 printf (_("\nUpload aborted.\n"));
665 GNUNET_shutdown_initiate ();
667 case GNUNET_FSUI_upload_error:
668 printf (_("\nError uploading file: %s"),
669 event->data.UploadError.message);
671 GNUNET_shutdown_initiate ();
673 case GNUNET_FSUI_upload_started:
674 case GNUNET_FSUI_upload_stopped:
677 printf (_("\nUnexpected event: %d\n"), event->type);
678 GNUNET_GE_BREAK (ectx, 0);
685 /* end of gnunet-publish.c */