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, simulate)
30 * - progress callbacks not implemented (and need verbosity option)
31 * - clean shutdown is not implemented (stop ctx, etc.)
34 #include "gnunet_fs_service.h"
36 #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_TIME_Absolute start_time;
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 * Called by FS client to give information about the progress of an
80 * @param info details about the event, specifying the event type
81 * and various bits about the event
82 * @return client-context (for the next progress call
83 * for this operation; should be set to NULL for
84 * SUSPEND and STOPPED events). The value returned
85 * will be passed to future callbacks in the respective
86 * field in the GNUNET_FS_ProgressInfo struct.
89 progress_cb (void *cls,
90 const struct GNUNET_FS_ProgressInfo *info)
97 * Print metadata entries (except binary
98 * metadata and the filename).
101 * @param type type of the meta data
102 * @param data value of the meta data
103 * @return GNUNET_OK to continue to iterate, GNUNET_SYSERR to abort
106 meta_printer (void *cls,
107 EXTRACTOR_KeywordType type,
110 if ( (type == EXTRACTOR_FILENAME) ||
111 (EXTRACTOR_isBinaryType (type)) )
115 EXTRACTOR_getKeywordTypeAsString (type),
122 * Merge metadata entries (except binary
125 * @param cls closure, target metadata structure
126 * @param type type of the meta data
127 * @param data value of the meta data
128 * @return GNUNET_OK to continue to iterate, GNUNET_SYSERR to abort
131 meta_merger (void *cls,
132 EXTRACTOR_KeywordType type,
135 struct GNUNET_CONTAINER_MetaData *m = cls;
136 GNUNET_CONTAINER_meta_data_insert (m,
144 * Function called on all entries before the
145 * publication. This is where we perform
146 * modifications to the default based on
147 * command-line options.
150 * @param fi the entry in the publish-structure
151 * @param length length of the file or directory
152 * @param m metadata for the file or directory (can be modified)
153 * @param uri pointer to the keywords that will be used for this entry (can be modified)
154 * @param anonymity pointer to selected anonymity level (can be modified)
155 * @param priority pointer to selected priority (can be modified)
156 * @param expirationTime pointer to selected expiration time (can be modified)
157 * @param client_info pointer to client context set upon creation (can be modified)
158 * @return GNUNET_OK to continue, GNUNET_NO to remove
159 * this entry from the directory, GNUNET_SYSERR
160 * to abort the iteration
163 publish_inspector (void *cls,
164 struct GNUNET_FS_FileInformation *fi,
166 struct GNUNET_CONTAINER_MetaData *m,
167 struct GNUNET_FS_Uri **uri,
168 unsigned int *anonymity,
169 unsigned int *priority,
170 struct GNUNET_TIME_Absolute *expirationTime,
175 struct GNUNET_FS_Uri *new_uri;
177 if (! do_disable_creation_time)
178 GNUNET_CONTAINER_meta_data_add_publication_date (meta);
179 if (NULL != topKeywords)
181 new_uri = GNUNET_FS_uri_ksk_merge (topKeywords,
183 GNUNET_FS_uri_destroy (*uri);
185 GNUNET_FS_uri_destroy (topKeywords);
190 GNUNET_CONTAINER_meta_data_get_contents (meta,
193 GNUNET_CONTAINER_meta_data_destroy (meta);
198 fn = GNUNET_CONTAINER_meta_data_get_by_type (meta,
200 fs = GNUNET_STRINGS_byte_size_fancy (length);
202 _("Keywords for file `%s' (%s)\n"),
207 GNUNET_CONTAINER_meta_data_get_contents (meta,
210 fprintf (stdout, "\n");
212 if (GNUNET_FS_meta_data_test_for_directory (meta))
213 GNUNET_FS_file_information_inspect (fi,
221 * Main function that will be run by the scheduler.
224 * @param sched the scheduler to use
225 * @param args remaining command-line arguments
226 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
227 * @param cfg configuration
231 struct GNUNET_SCHEDULER_Handle *sched,
234 const struct GNUNET_CONFIGURATION_Handle *c)
236 struct GNUNET_FS_FileInformation *fi;
237 struct GNUNET_FS_Namespace *namespace;
238 EXTRACTOR_ExtractorList *l;
242 /* check arguments */
243 if ( ( (uri_string == NULL) || (extract_only) )
244 && ( (args[0] == NULL) || (args[1] != NULL) ) )
247 ("You must specify one and only one filename for insertion.\n"));
251 if ((uri_string != NULL) && (args[0] != NULL))
253 printf (_("You must NOT specify an URI and a filename.\n"));
257 if ((uri_string != NULL) && (extract_only))
259 printf (_("Cannot extract metadata from a URI!\n"));
263 if (pseudonym != NULL)
268 _("Option `%s' is required when using option `%s'.\n"),
275 { /* ordinary insertion checks */
279 _("Option `%s' makes no sense without option `%s'.\n"),
287 _("Option `%s' makes no sense without option `%s'.\n"),
296 _("Need the name of a file to publish!\n"));
301 ctx = GNUNET_FS_start (sched,
309 _("Could not initialize `%s' subsystem.\n"),
315 if (NULL != pseudonym)
317 namespace = GNUNET_FS_namespace_create (ctx,
319 if (NULL == namespace)
322 _("Could not create namespace `%s'\n"),
324 GNUNET_FS_stop (ctx);
329 if (NULL != uri_string)
331 // FIXME -- implement!
334 start_time = GNUNET_TIME_absolute_get ();
337 if (! disable_extractor)
339 l = EXTRACTOR_loadDefaultLibraries ();
341 GNUNET_CONFIGURATION_get_value_string (cfg, "FS", "EXTRACTORS",
345 l = EXTRACTOR_loadConfigLibraries (l, ex);
349 fi = GNUNET_FS_file_information_create_from_directory (NULL,
351 &GNUNET_FS_directory_scanner_default,
356 GNUNET_TIME_relative_to_absolute (DEFAULT_EXPIRATION),
358 EXTRACTOR_removeAll (l);
362 _("Could not publish `%s': %s\n"),
366 if (namespace != NULL)
367 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
368 GNUNET_FS_stop (ctx);
372 GNUNET_FS_file_information_inspect (fi,
377 if (namespace != NULL)
378 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
379 GNUNET_FS_file_information_destroy (fi, NULL, NULL);
380 GNUNET_FS_stop (ctx);
383 pc = GNUNET_FS_publish_start (ctx,
393 * gnunet-publish command line options
395 static struct GNUNET_GETOPT_CommandLineOption options[] = {
396 {'a', "anonymity", "LEVEL",
397 gettext_noop ("set the desired LEVEL of sender-anonymity"),
398 1, &GNUNET_GETOPT_set_uint, &anonymity},
399 {'d', "disable-creation-time", NULL,
401 ("disable adding the creation time to the metadata of the uploaded file"),
402 0, &GNUNET_GETOPT_set_one, &do_disable_creation_time},
403 {'D', "disable-extractor", NULL,
405 ("do not use libextractor to add keywords or metadata"),
406 0, &GNUNET_GETOPT_set_one, &disable_extractor},
407 {'e', "extract", NULL,
409 ("print list of extracted keywords that would be used, but do not perform upload"),
410 0, &GNUNET_GETOPT_set_one, &extract_only},
411 {'k', "key", "KEYWORD",
413 ("add an additional keyword for the top-level file or directory"
414 " (this option can be specified multiple times)"),
415 1, &GNUNET_FS_getopt_set_keywords, &topKeywords},
416 // *: option not yet used... (can handle in a pass over FI)
417 {'m', "meta", "TYPE:VALUE",
418 gettext_noop ("set the meta-data for the given TYPE to the given VALUE"),
419 1, &GNUNET_FS_getopt_set_metadata, &meta},
420 {'n', "noindex", NULL,
421 gettext_noop ("do not index, perform full insertion (stores entire "
422 "file in encrypted form in GNUnet database)"),
423 0, &GNUNET_GETOPT_set_one, &do_insert},
426 ("specify ID of an updated version to be published in the future"
427 " (for namespace insertions only)"),
428 1, &GNUNET_GETOPT_set_string, &next_id},
429 {'p', "priority", "PRIORITY",
430 gettext_noop ("specify the priority of the content"),
431 1, &GNUNET_GETOPT_set_uint, &priority},
432 {'P', "pseudonym", "NAME",
434 ("publish the files under the pseudonym NAME (place file into namespace)"),
435 1, &GNUNET_GETOPT_set_string, &pseudonym},
436 // *: option not yet used... (need FS API support!)
437 {'s', "simulate-only", NULL,
438 gettext_noop ("only simulate the process but do not do any "
439 "actual publishing (useful to compute URIs)"),
440 0, &GNUNET_GETOPT_set_one, &do_simulate},
442 gettext_noop ("set the ID of this version of the publication"
443 " (for namespace insertions only)"),
444 1, &GNUNET_GETOPT_set_string, &this_id},
445 // *: option not yet used... (need FS API support!)
447 gettext_noop ("URI to be published (can be used instead of passing a "
448 "file to add keywords to the file with the respective URI)"),
449 1, &GNUNET_GETOPT_set_string, &uri_string},
450 GNUNET_GETOPT_OPTION_END
455 * The main function to publish content to GNUnet.
457 * @param argc number of arguments from the command line
458 * @param argv command line arguments
459 * @return 0 ok, 1 on error
462 main (int argc, char *const *argv)
465 GNUNET_PROGRAM_run (argc,
469 ("Publish files on GNUnet."),
470 options, &run, NULL)) ? ret : 1;
473 /* end of gnunet-publish.c */
475 ////////////////////////////////////////////////////////////////
479 * Print progess message.
482 printstatus (void *ctx, const GNUNET_FSUI_Event * event)
484 unsigned long long delta;
489 case GNUNET_FSUI_upload_progress:
495 now = GNUNET_get_time ();
496 delta = event->data.UploadProgress.eta - now;
497 if (event->data.UploadProgress.eta < now)
499 ret = GNUNET_get_time_interval_as_fancy_string (delta);
500 PRINTF (_("%16llu of %16llu bytes inserted "
501 "(estimating %6s to completion) - %s\n"),
502 event->data.UploadProgress.completed,
503 event->data.UploadProgress.total,
504 ret, event->data.UploadProgress.filename);
508 case GNUNET_FSUI_upload_completed:
511 delta = GNUNET_get_time () - start_time;
512 PRINTF (_("Upload of `%s' complete, "
513 "%llu bytes took %llu seconds (%8.3f KiB/s).\n"),
514 event->data.UploadCompleted.filename,
515 event->data.UploadCompleted.total,
516 delta / GNUNET_CRON_SECONDS,
519 : (double) (event->data.UploadCompleted.total
520 / 1024.0 * GNUNET_CRON_SECONDS / delta));
522 fstring = GNUNET_ECRS_uri_to_string (event->data.UploadCompleted.uri);
523 printf (_("File `%s' has URI: %s\n"),
524 event->data.UploadCompleted.filename, fstring);
525 GNUNET_free (fstring);
526 if (ul == event->data.UploadCompleted.uc.pos)
528 postProcess (event->data.UploadCompleted.uri);
530 GNUNET_shutdown_initiate ();
533 case GNUNET_FSUI_upload_aborted:
534 printf (_("\nUpload aborted.\n"));
536 GNUNET_shutdown_initiate ();
538 case GNUNET_FSUI_upload_error:
539 printf (_("\nError uploading file: %s"),
540 event->data.UploadError.message);
542 GNUNET_shutdown_initiate ();
544 case GNUNET_FSUI_upload_started:
545 case GNUNET_FSUI_upload_stopped:
548 printf (_("\nUnexpected event: %d\n"), event->type);
549 GNUNET_GE_BREAK (ectx, 0);
556 /* end of gnunet-publish.c */