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_FS_PublishContext *pc;
46 static struct GNUNET_CONTAINER_MetaData *meta;
48 static struct GNUNET_FS_Uri *topKeywords;
50 static unsigned int anonymity = 1;
52 static unsigned int priority = 365;
54 static char *uri_string;
60 static char *pseudonym;
64 static int disable_extractor;
66 static int do_simulate;
68 static int extract_only;
70 static int do_disable_creation_time;
74 * Called by FS client to give information about the progress of an
78 * @param info details about the event, specifying the event type
79 * and various bits about the event
80 * @return client-context (for the next progress call
81 * for this operation; should be set to NULL for
82 * SUSPEND and STOPPED events). The value returned
83 * will be passed to future callbacks in the respective
84 * field in the GNUNET_FS_ProgressInfo struct.
87 progress_cb (void *cls,
88 const struct GNUNET_FS_ProgressInfo *info)
92 case GNUNET_FS_STATUS_PUBLISH_START:
94 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
97 _("Publishing `%s' at %llu/%llu (%s remaining)\n"),
98 info->value.publish.filename,
99 (unsigned long long) info->value.publish.completed,
100 (unsigned long long) info->value.publish.size,
101 GNUNET_STRINGS_relative_time_to_string(info->value.publish.eta));
103 case GNUNET_FS_STATUS_PUBLISH_ERROR:
105 _("Error publishing: %s.\n"),
106 info->value.publish.specifics.error.message);
107 GNUNET_FS_publish_stop (pc);
109 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
111 _("Publishing `%s' done.\n"),
112 info->value.publish.filename);
113 if (info->value.publish.pctx == NULL)
114 GNUNET_FS_publish_stop (pc);
116 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
117 if (info->value.publish.sc == pc)
118 GNUNET_FS_stop (ctx);
122 _("Unexpected status: %d\n"),
126 return ""; /* non-null */
131 * Print metadata entries (except binary
132 * metadata and the filename).
135 * @param type type of the meta data
136 * @param data value of the meta data
137 * @return GNUNET_OK to continue to iterate, GNUNET_SYSERR to abort
140 meta_printer (void *cls,
141 EXTRACTOR_KeywordType type,
144 if ( (type == EXTRACTOR_FILENAME) ||
145 (EXTRACTOR_isBinaryType (type)) )
149 EXTRACTOR_getKeywordTypeAsString (type),
156 * Merge metadata entries (except binary
159 * @param cls closure, target metadata structure
160 * @param type type of the meta data
161 * @param data value of the meta data
162 * @return GNUNET_OK to continue to iterate, GNUNET_SYSERR to abort
165 meta_merger (void *cls,
166 EXTRACTOR_KeywordType type,
169 struct GNUNET_CONTAINER_MetaData *m = cls;
170 GNUNET_CONTAINER_meta_data_insert (m,
178 * Function called on all entries before the
179 * publication. This is where we perform
180 * modifications to the default based on
181 * command-line options.
184 * @param fi the entry in the publish-structure
185 * @param length length of the file or directory
186 * @param m metadata for the file or directory (can be modified)
187 * @param uri pointer to the keywords that will be used for this entry (can be modified)
188 * @param anonymity pointer to selected anonymity level (can be modified)
189 * @param priority pointer to selected priority (can be modified)
190 * @param expirationTime pointer to selected expiration time (can be modified)
191 * @param client_info pointer to client context set upon creation (can be modified)
192 * @return GNUNET_OK to continue, GNUNET_NO to remove
193 * this entry from the directory, GNUNET_SYSERR
194 * to abort the iteration
197 publish_inspector (void *cls,
198 struct GNUNET_FS_FileInformation *fi,
200 struct GNUNET_CONTAINER_MetaData *m,
201 struct GNUNET_FS_Uri **uri,
202 unsigned int *anonymity,
203 unsigned int *priority,
204 struct GNUNET_TIME_Absolute *expirationTime,
209 struct GNUNET_FS_Uri *new_uri;
211 if (! do_disable_creation_time)
212 GNUNET_CONTAINER_meta_data_add_publication_date (meta);
213 if (NULL != topKeywords)
215 new_uri = GNUNET_FS_uri_ksk_merge (topKeywords,
217 GNUNET_FS_uri_destroy (*uri);
219 GNUNET_FS_uri_destroy (topKeywords);
224 GNUNET_CONTAINER_meta_data_get_contents (meta,
227 GNUNET_CONTAINER_meta_data_destroy (meta);
232 fn = GNUNET_CONTAINER_meta_data_get_by_type (meta,
234 fs = GNUNET_STRINGS_byte_size_fancy (length);
236 _("Keywords for file `%s' (%s)\n"),
241 GNUNET_CONTAINER_meta_data_get_contents (meta,
244 fprintf (stdout, "\n");
246 if (GNUNET_FS_meta_data_test_for_directory (meta))
247 GNUNET_FS_file_information_inspect (fi,
255 * Main function that will be run by the scheduler.
258 * @param sched the scheduler to use
259 * @param args remaining command-line arguments
260 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
261 * @param c configuration
265 struct GNUNET_SCHEDULER_Handle *sched,
268 const struct GNUNET_CONFIGURATION_Handle *c)
270 struct GNUNET_FS_FileInformation *fi;
271 struct GNUNET_FS_Namespace *namespace;
272 EXTRACTOR_ExtractorList *l;
276 /* check arguments */
277 if ( ( (uri_string == NULL) || (extract_only) )
278 && ( (args[0] == NULL) || (args[1] != NULL) ) )
281 ("You must specify one and only one filename for insertion.\n"));
285 if ((uri_string != NULL) && (args[0] != NULL))
287 printf (_("You must NOT specify an URI and a filename.\n"));
291 if ((uri_string != NULL) && (extract_only))
293 printf (_("Cannot extract metadata from a URI!\n"));
297 if (pseudonym != NULL)
302 _("Option `%s' is required when using option `%s'.\n"),
309 { /* ordinary insertion checks */
313 _("Option `%s' makes no sense without option `%s'.\n"),
321 _("Option `%s' makes no sense without option `%s'.\n"),
330 _("Need the name of a file to publish!\n"));
335 ctx = GNUNET_FS_start (sched,
340 GNUNET_FS_FLAGS_NONE,
341 GNUNET_FS_OPTIONS_END);
345 _("Could not initialize `%s' subsystem.\n"),
351 if (NULL != pseudonym)
353 namespace = GNUNET_FS_namespace_create (ctx,
355 if (NULL == namespace)
358 _("Could not create namespace `%s'\n"),
360 GNUNET_FS_stop (ctx);
365 if (NULL != uri_string)
367 // FIXME -- implement!
372 if (! disable_extractor)
374 l = EXTRACTOR_loadDefaultLibraries ();
376 GNUNET_CONFIGURATION_get_value_string (cfg, "FS", "EXTRACTORS",
380 l = EXTRACTOR_loadConfigLibraries (l, ex);
384 fi = GNUNET_FS_file_information_create_from_directory (NULL,
386 &GNUNET_FS_directory_scanner_default,
391 GNUNET_TIME_relative_to_absolute (DEFAULT_EXPIRATION),
393 EXTRACTOR_removeAll (l);
397 _("Could not publish `%s': %s\n"),
401 if (namespace != NULL)
402 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
403 GNUNET_FS_stop (ctx);
407 GNUNET_FS_file_information_inspect (fi,
412 if (namespace != NULL)
413 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
414 GNUNET_FS_file_information_destroy (fi, NULL, NULL);
415 GNUNET_FS_stop (ctx);
418 pc = GNUNET_FS_publish_start (ctx,
424 ? GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY
425 : GNUNET_FS_PUBLISH_OPTION_NONE);
429 _("Could not start publishing.\n"));
430 GNUNET_FS_stop (ctx);
438 * gnunet-publish command line options
440 static struct GNUNET_GETOPT_CommandLineOption options[] = {
441 {'a', "anonymity", "LEVEL",
442 gettext_noop ("set the desired LEVEL of sender-anonymity"),
443 1, &GNUNET_GETOPT_set_uint, &anonymity},
444 {'d', "disable-creation-time", NULL,
446 ("disable adding the creation time to the metadata of the uploaded file"),
447 0, &GNUNET_GETOPT_set_one, &do_disable_creation_time},
448 {'D', "disable-extractor", NULL,
450 ("do not use libextractor to add keywords or metadata"),
451 0, &GNUNET_GETOPT_set_one, &disable_extractor},
452 {'e', "extract", NULL,
454 ("print list of extracted keywords that would be used, but do not perform upload"),
455 0, &GNUNET_GETOPT_set_one, &extract_only},
456 {'k', "key", "KEYWORD",
458 ("add an additional keyword for the top-level file or directory"
459 " (this option can be specified multiple times)"),
460 1, &GNUNET_FS_getopt_set_keywords, &topKeywords},
461 // *: option not yet used... (can handle in a pass over FI)
462 {'m', "meta", "TYPE:VALUE",
463 gettext_noop ("set the meta-data for the given TYPE to the given VALUE"),
464 1, &GNUNET_FS_getopt_set_metadata, &meta},
465 {'n', "noindex", NULL,
466 gettext_noop ("do not index, perform full insertion (stores entire "
467 "file in encrypted form in GNUnet database)"),
468 0, &GNUNET_GETOPT_set_one, &do_insert},
471 ("specify ID of an updated version to be published in the future"
472 " (for namespace insertions only)"),
473 1, &GNUNET_GETOPT_set_string, &next_id},
474 {'p', "priority", "PRIORITY",
475 gettext_noop ("specify the priority of the content"),
476 1, &GNUNET_GETOPT_set_uint, &priority},
477 {'P', "pseudonym", "NAME",
479 ("publish the files under the pseudonym NAME (place file into namespace)"),
480 1, &GNUNET_GETOPT_set_string, &pseudonym},
481 // *: option not yet used... (need FS API support!)
482 {'s', "simulate-only", NULL,
483 gettext_noop ("only simulate the process but do not do any "
484 "actual publishing (useful to compute URIs)"),
485 0, &GNUNET_GETOPT_set_one, &do_simulate},
487 gettext_noop ("set the ID of this version of the publication"
488 " (for namespace insertions only)"),
489 1, &GNUNET_GETOPT_set_string, &this_id},
490 // *: option not yet used... (need FS API support!)
492 gettext_noop ("URI to be published (can be used instead of passing a "
493 "file to add keywords to the file with the respective URI)"),
494 1, &GNUNET_GETOPT_set_string, &uri_string},
495 {'V', "verbose", NULL,
496 gettext_noop ("be verbose (print progress information)"),
497 0, &GNUNET_GETOPT_set_one, &verbose},
498 GNUNET_GETOPT_OPTION_END
503 * The main function to publish content to GNUnet.
505 * @param argc number of arguments from the command line
506 * @param argv command line arguments
507 * @return 0 ok, 1 on error
510 main (int argc, char *const *argv)
513 GNUNET_PROGRAM_run (argc,
517 ("Publish files on GNUnet."),
518 options, &run, NULL)) ? ret : 1;
521 /* end of gnunet-publish.c */
523 ////////////////////////////////////////////////////////////////
527 * Print progess message.
530 printstatus (void *ctx, const GNUNET_FSUI_Event * event)
532 unsigned long long delta;
537 case GNUNET_FSUI_upload_progress:
543 now = GNUNET_get_time ();
544 delta = event->data.UploadProgress.eta - now;
545 if (event->data.UploadProgress.eta < now)
547 ret = GNUNET_get_time_interval_as_fancy_string (delta);
548 PRINTF (_("%16llu of %16llu bytes inserted "
549 "(estimating %6s to completion) - %s\n"),
550 event->data.UploadProgress.completed,
551 event->data.UploadProgress.total,
552 ret, event->data.UploadProgress.filename);
556 case GNUNET_FSUI_upload_completed:
559 delta = GNUNET_get_time () - start_time;
560 PRINTF (_("Upload of `%s' complete, "
561 "%llu bytes took %llu seconds (%8.3f KiB/s).\n"),
562 event->data.UploadCompleted.filename,
563 event->data.UploadCompleted.total,
564 delta / GNUNET_CRON_SECONDS,
567 : (double) (event->data.UploadCompleted.total
568 / 1024.0 * GNUNET_CRON_SECONDS / delta));
570 fstring = GNUNET_ECRS_uri_to_string (event->data.UploadCompleted.uri);
571 printf (_("File `%s' has URI: %s\n"),
572 event->data.UploadCompleted.filename, fstring);
573 GNUNET_free (fstring);
574 if (ul == event->data.UploadCompleted.uc.pos)
576 postProcess (event->data.UploadCompleted.uri);
578 GNUNET_shutdown_initiate ();
581 case GNUNET_FSUI_upload_aborted:
582 printf (_("\nUpload aborted.\n"));
584 GNUNET_shutdown_initiate ();
586 case GNUNET_FSUI_upload_error:
587 printf (_("\nError uploading file: %s"),
588 event->data.UploadError.message);
590 GNUNET_shutdown_initiate ();
592 case GNUNET_FSUI_upload_started:
593 case GNUNET_FSUI_upload_stopped:
596 printf (_("\nUnexpected event: %d\n"), event->type);
597 GNUNET_GE_BREAK (ectx, 0);
604 /* end of gnunet-publish.c */