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;
280 if (NULL != topKeywords)
284 new_uri = GNUNET_FS_uri_ksk_merge (topKeywords,
286 GNUNET_FS_uri_destroy (*uri);
288 GNUNET_FS_uri_destroy (topKeywords);
298 GNUNET_CONTAINER_meta_data_iterate (meta,
301 GNUNET_CONTAINER_meta_data_destroy (meta);
304 if (! do_disable_creation_time)
305 GNUNET_CONTAINER_meta_data_add_publication_date (m);
308 fn = GNUNET_CONTAINER_meta_data_get_by_type (m,
309 EXTRACTOR_METATYPE_FILENAME);
310 fs = GNUNET_STRINGS_byte_size_fancy (length);
312 _("Keywords for file `%s' (%s)\n"),
317 GNUNET_CONTAINER_meta_data_iterate (m,
320 fprintf (stdout, "\n");
322 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (m))
323 GNUNET_FS_file_information_inspect (fi,
331 uri_ksk_continuation (void *cls,
332 const struct GNUNET_FS_Uri *uri,
342 GNUNET_FS_stop (ctx);
347 * Main function that will be run by the scheduler.
350 * @param s the scheduler to use
351 * @param args remaining command-line arguments
352 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
353 * @param c configuration
357 struct GNUNET_SCHEDULER_Handle *s,
360 const struct GNUNET_CONFIGURATION_Handle *c)
362 struct GNUNET_FS_FileInformation *fi;
363 struct GNUNET_FS_Namespace *namespace;
364 struct EXTRACTOR_PluginList *l;
368 struct GNUNET_FS_Uri *uri;
371 /* check arguments */
372 if ( ( (uri_string == NULL) || (extract_only) )
373 && ( (args[0] == NULL) || (args[1] != NULL) ) )
376 ("You must specify one and only one filename for insertion.\n"));
380 if ((uri_string != NULL) && (args[0] != NULL))
382 printf (_("You must NOT specify an URI and a filename.\n"));
386 if ((uri_string != NULL) && (extract_only))
388 printf (_("Cannot extract metadata from a URI!\n"));
392 if (pseudonym != NULL)
397 _("Option `%s' is required when using option `%s'.\n"),
404 { /* ordinary insertion checks */
408 _("Option `%s' makes no sense without option `%s'.\n"),
416 _("Option `%s' makes no sense without option `%s'.\n"),
425 _("Need the name of a file to publish!\n"));
430 ctx = GNUNET_FS_start (sched,
435 GNUNET_FS_FLAGS_NONE,
436 GNUNET_FS_OPTIONS_END);
440 _("Could not initialize `%s' subsystem.\n"),
446 if (NULL != pseudonym)
448 namespace = GNUNET_FS_namespace_create (ctx,
450 if (NULL == namespace)
453 _("Could not create namespace `%s'\n"),
455 GNUNET_FS_stop (ctx);
460 if (NULL != uri_string)
463 uri = GNUNET_FS_uri_parse (uri_string,
468 _("Failed to parse URI: %s\n"),
471 GNUNET_FS_stop (ctx);
475 GNUNET_FS_publish_ksk (ctx,
479 GNUNET_TIME_relative_to_absolute (DEFAULT_EXPIRATION),
482 GNUNET_FS_PUBLISH_OPTION_NONE,
483 &uri_ksk_continuation,
488 if (! disable_extractor)
490 l = EXTRACTOR_plugin_add_defaults (EXTRACTOR_OPTION_DEFAULT_POLICY);
492 GNUNET_CONFIGURATION_get_value_string (cfg, "FS", "EXTRACTORS",
496 l = EXTRACTOR_plugin_add_config (l, ex, EXTRACTOR_OPTION_DEFAULT_POLICY);
501 if (0 != STAT (args[0], &sbuf))
503 GNUNET_asprintf (&emsg,
504 _("Could not access file: %s\n"),
508 else if (S_ISDIR (sbuf.st_mode))
510 fi = GNUNET_FS_file_information_create_from_directory (NULL,
512 &GNUNET_FS_directory_scanner_default,
517 GNUNET_TIME_relative_to_absolute (DEFAULT_EXPIRATION),
522 fi = GNUNET_FS_file_information_create_from_file (NULL,
529 GNUNET_TIME_relative_to_absolute (DEFAULT_EXPIRATION));
530 GNUNET_break (fi != NULL);
532 EXTRACTOR_plugin_remove_all (l);
536 _("Could not publish `%s': %s\n"),
540 if (namespace != NULL)
541 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
542 GNUNET_FS_stop (ctx);
546 GNUNET_FS_file_information_inspect (fi,
551 if (namespace != NULL)
552 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
553 GNUNET_FS_file_information_destroy (fi, NULL, NULL);
554 GNUNET_FS_stop (ctx);
557 pc = GNUNET_FS_publish_start (ctx,
563 ? GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY
564 : GNUNET_FS_PUBLISH_OPTION_NONE);
568 _("Could not start publishing.\n"));
569 GNUNET_FS_stop (ctx);
573 kill_task = GNUNET_SCHEDULER_add_delayed (sched,
574 GNUNET_TIME_UNIT_FOREVER_REL,
581 * gnunet-publish command line options
583 static struct GNUNET_GETOPT_CommandLineOption options[] = {
584 {'a', "anonymity", "LEVEL",
585 gettext_noop ("set the desired LEVEL of sender-anonymity"),
586 1, &GNUNET_GETOPT_set_uint, &anonymity},
587 {'d', "disable-creation-time", NULL,
589 ("disable adding the creation time to the metadata of the uploaded file"),
590 0, &GNUNET_GETOPT_set_one, &do_disable_creation_time},
591 {'D', "disable-extractor", NULL,
593 ("do not use libextractor to add keywords or metadata"),
594 0, &GNUNET_GETOPT_set_one, &disable_extractor},
595 {'e', "extract", NULL,
597 ("print list of extracted keywords that would be used, but do not perform upload"),
598 0, &GNUNET_GETOPT_set_one, &extract_only},
599 {'k', "key", "KEYWORD",
601 ("add an additional keyword for the top-level file or directory"
602 " (this option can be specified multiple times)"),
603 1, &GNUNET_FS_getopt_set_keywords, &topKeywords},
604 {'m', "meta", "TYPE:VALUE",
605 gettext_noop ("set the meta-data for the given TYPE to the given VALUE"),
606 1, &GNUNET_FS_getopt_set_metadata, &meta},
607 {'n', "noindex", NULL,
608 gettext_noop ("do not index, perform full insertion (stores entire "
609 "file in encrypted form in GNUnet database)"),
610 0, &GNUNET_GETOPT_set_one, &do_insert},
613 ("specify ID of an updated version to be published in the future"
614 " (for namespace insertions only)"),
615 1, &GNUNET_GETOPT_set_string, &next_id},
616 {'p', "priority", "PRIORITY",
617 gettext_noop ("specify the priority of the content"),
618 1, &GNUNET_GETOPT_set_uint, &priority},
619 {'P', "pseudonym", "NAME",
621 ("publish the files under the pseudonym NAME (place file into namespace)"),
622 1, &GNUNET_GETOPT_set_string, &pseudonym},
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},
632 gettext_noop ("URI to be published (can be used instead of passing a "
633 "file to add keywords to the file with the respective URI)"),
634 1, &GNUNET_GETOPT_set_string, &uri_string},
635 {'V', "verbose", NULL,
636 gettext_noop ("be verbose (print progress information)"),
637 0, &GNUNET_GETOPT_set_one, &verbose},
638 GNUNET_GETOPT_OPTION_END
643 * The main function to publish content to GNUnet.
645 * @param argc number of arguments from the command line
646 * @param argv command line arguments
647 * @return 0 ok, 1 on error
650 main (int argc, char *const *argv)
653 GNUNET_PROGRAM_run (argc,
657 ("Publish files on GNUnet."),
658 options, &run, NULL)) ? ret : 1;
661 /* end of gnunet-publish.c */