-LRN: fix fip_signal_start recursion
[oweals/gnunet.git] / src / fs / gnunet-publish.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
4
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 3, or (at your
8      option) any later version.
9
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.
14
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.
19 */
20 /**
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
27  */
28 #include "platform.h"
29 #include "gnunet_fs_service.h"
30
31 static int ret;
32
33 static int verbose;
34
35 static const struct GNUNET_CONFIGURATION_Handle *cfg;
36
37 static struct GNUNET_FS_Handle *ctx;
38
39 static struct GNUNET_FS_PublishContext *pc;
40
41 static struct GNUNET_CONTAINER_MetaData *meta;
42
43 static struct GNUNET_FS_Uri *topKeywords;
44
45 static struct GNUNET_FS_Uri *uri;
46
47 static struct GNUNET_FS_BlockOptions bo = { {0LL}, 1, 365, 1 };
48
49 static char *uri_string;
50
51 static char *next_id;
52
53 static char *this_id;
54
55 static char *pseudonym;
56
57 static int do_insert;
58
59 static int disable_extractor;
60
61 static int do_simulate;
62
63 static int extract_only;
64
65 static int do_disable_creation_time;
66
67 static GNUNET_SCHEDULER_TaskIdentifier kill_task;
68
69
70 /**
71  * FIXME: docu
72  */
73 static void
74 do_stop_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
75 {
76   struct GNUNET_FS_PublishContext *p;
77
78   if (pc != NULL)
79   {
80     p = pc;
81     pc = NULL;
82     GNUNET_FS_publish_stop (p);
83     if (NULL != meta)
84     {
85       GNUNET_CONTAINER_meta_data_destroy (meta);
86       meta = NULL;
87     }
88   }
89 }
90
91
92 /**
93  * Called by FS client to give information about the progress of an
94  * operation.
95  *
96  * @param cls closure
97  * @param info details about the event, specifying the event type
98  *        and various bits about the event
99  * @return client-context (for the next progress call
100  *         for this operation; should be set to NULL for
101  *         SUSPEND and STOPPED events).  The value returned
102  *         will be passed to future callbacks in the respective
103  *         field in the GNUNET_FS_ProgressInfo struct.
104  */
105 static void *
106 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
107 {
108   char *s;
109
110   switch (info->status)
111   {
112   case GNUNET_FS_STATUS_PUBLISH_START:
113     break;
114   case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
115     if (verbose)
116     {
117       s = GNUNET_STRINGS_relative_time_to_string (info->value.publish.eta);
118       FPRINTF (stdout, _("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, s);
122       GNUNET_free (s);
123     }
124     break;
125   case GNUNET_FS_STATUS_PUBLISH_ERROR:
126     FPRINTF (stderr, _("Error publishing: %s.\n"),
127              info->value.publish.specifics.error.message);
128     if (kill_task != GNUNET_SCHEDULER_NO_TASK)
129     {
130       GNUNET_SCHEDULER_cancel (kill_task);
131       kill_task = GNUNET_SCHEDULER_NO_TASK;
132     }
133     GNUNET_SCHEDULER_add_continuation (&do_stop_task, NULL,
134                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
135     break;
136   case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
137     FPRINTF (stdout, _("Publishing `%s' done.\n"),
138              info->value.publish.filename);
139     s = GNUNET_FS_uri_to_string (info->value.publish.specifics.
140                                  completed.chk_uri);
141     FPRINTF (stdout, _("URI is `%s'.\n"), s);
142     GNUNET_free (s);
143     if (info->value.publish.pctx == NULL)
144     {
145       if (kill_task != GNUNET_SCHEDULER_NO_TASK)
146       {
147         GNUNET_SCHEDULER_cancel (kill_task);
148         kill_task = GNUNET_SCHEDULER_NO_TASK;
149       }
150       GNUNET_SCHEDULER_add_continuation (&do_stop_task, NULL,
151                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
152     }
153     break;
154   case GNUNET_FS_STATUS_PUBLISH_STOPPED:
155     GNUNET_break (NULL == pc);
156     return NULL;
157   case GNUNET_FS_STATUS_UNINDEX_PROGRESS:
158     return NULL;
159   case GNUNET_FS_STATUS_UNINDEX_COMPLETED:
160     FPRINTF (stderr, "%s",  _("Cleanup after abort complete.\n"));
161     return NULL;
162   default:
163     FPRINTF (stderr, _("Unexpected status: %d\n"), info->status);
164     return NULL;
165   }
166   return "";                    /* non-null */
167 }
168
169
170 /**
171  * Print metadata entries (except binary
172  * metadata and the filename).
173  *
174  * @param cls closure
175  * @param plugin_name name of the plugin that generated the meta data
176  * @param type type of the meta data
177  * @param format format of data
178  * @param data_mime_type mime type of data
179  * @param data value of the meta data
180  * @param data_size number of bytes in data
181  * @return always 0
182  */
183 static int
184 meta_printer (void *cls, const char *plugin_name, enum EXTRACTOR_MetaType type,
185               enum EXTRACTOR_MetaFormat format, const char *data_mime_type,
186               const char *data, size_t data_size)
187 {
188   if ((format != EXTRACTOR_METAFORMAT_UTF8) &&
189       (format != EXTRACTOR_METAFORMAT_C_STRING))
190     return 0;
191   if (type == EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME)
192     return 0;
193   FPRINTF (stdout, "\t%s - %s\n", EXTRACTOR_metatype_to_string (type), data);
194   return 0;
195 }
196
197
198 /**
199  * Iterator printing keywords
200  *
201  * @param cls closure
202  * @param keyword the keyword
203  * @param is_mandatory is the keyword mandatory (in a search)
204  * @return GNUNET_OK to continue to iterate, GNUNET_SYSERR to abort
205  */
206 static int
207 keyword_printer (void *cls, const char *keyword, int is_mandatory)
208 {
209   FPRINTF (stdout, "\t%s\n", keyword);
210   return GNUNET_OK;
211 }
212
213
214 /**
215  * Function called on all entries before the publication.  This is
216  * where we perform modifications to the default based on command-line
217  * options.
218  *
219  * @param cls closure
220  * @param fi the entry in the publish-structure
221  * @param length length of the file or directory
222  * @param m metadata for the file or directory (can be modified)
223  * @param uri pointer to the keywords that will be used for this entry (can be modified)
224  * @param bo block options
225  * @param do_index should we index?
226  * @param client_info pointer to client context set upon creation (can be modified)
227  * @return GNUNET_OK to continue, GNUNET_NO to remove
228  *         this entry from the directory, GNUNET_SYSERR
229  *         to abort the iteration
230  */
231 static int
232 publish_inspector (void *cls, struct GNUNET_FS_FileInformation *fi,
233                    uint64_t length, struct GNUNET_CONTAINER_MetaData *m,
234                    struct GNUNET_FS_Uri **uri,
235                    struct GNUNET_FS_BlockOptions *bo, int *do_index,
236                    void **client_info)
237 {
238   char *fn;
239   char *fs;
240   struct GNUNET_FS_Uri *new_uri;
241
242   if (cls == fi)
243     return GNUNET_OK;
244   if (NULL != topKeywords)
245   {
246     if (*uri != NULL)
247     {
248       new_uri = GNUNET_FS_uri_ksk_merge (topKeywords, *uri);
249       GNUNET_FS_uri_destroy (*uri);
250       *uri = new_uri;
251       GNUNET_FS_uri_destroy (topKeywords);
252     }
253     else
254     {
255       *uri = topKeywords;
256     }
257     topKeywords = NULL;
258   }
259   if (NULL != meta)
260   {
261     GNUNET_CONTAINER_meta_data_merge (m, meta);
262     GNUNET_CONTAINER_meta_data_destroy (meta);
263     meta = NULL;
264   }
265   if (!do_disable_creation_time)
266     GNUNET_CONTAINER_meta_data_add_publication_date (m);
267   if (extract_only)
268   {
269     fn = GNUNET_CONTAINER_meta_data_get_by_type (m,
270                                                  EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
271     fs = GNUNET_STRINGS_byte_size_fancy (length);
272     FPRINTF (stdout, _("Meta data for file `%s' (%s)\n"), fn, fs);
273     GNUNET_CONTAINER_meta_data_iterate (m, &meta_printer, NULL);
274     FPRINTF (stdout, _("Keywords for file `%s' (%s)\n"), fn, fs);
275     GNUNET_free (fn);
276     GNUNET_free (fs);
277     if (NULL != *uri)
278       GNUNET_FS_uri_ksk_get_keywords (*uri, &keyword_printer, NULL);
279     FPRINTF (stdout, "%s",  "\n");
280   }
281   if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (m))
282     GNUNET_FS_file_information_inspect (fi, &publish_inspector, fi);
283   return GNUNET_OK;
284 }
285
286
287 /**
288  * FIXME: docu
289  */
290 static void
291 uri_sks_continuation (void *cls, const struct GNUNET_FS_Uri *ksk_uri,
292                       const char *emsg)
293 {
294   if (emsg != NULL)
295   {
296     FPRINTF (stderr, "%s\n", emsg);
297     ret = 1;
298   }
299   GNUNET_FS_uri_destroy (uri);
300   uri = NULL;
301   GNUNET_FS_stop (ctx);
302   ctx = NULL;
303 }
304
305
306 /**
307  * FIXME: docu
308  */
309 static void
310 uri_ksk_continuation (void *cls, const struct GNUNET_FS_Uri *ksk_uri,
311                       const char *emsg)
312 {
313   struct GNUNET_FS_Namespace *ns;
314
315   if (emsg != NULL)
316   {
317     FPRINTF (stderr, "%s\n", emsg);
318     ret = 1;
319   }
320   if (pseudonym != NULL)
321   {
322     ns = GNUNET_FS_namespace_create (ctx, pseudonym);
323     if (ns == NULL)
324     {
325       FPRINTF (stderr, _("Failed to create namespace `%s'\n"), pseudonym);
326       ret = 1;
327     }
328     else
329     {
330       GNUNET_FS_publish_sks (ctx, ns, this_id, next_id, meta, uri, &bo,
331                              GNUNET_FS_PUBLISH_OPTION_NONE,
332                              uri_sks_continuation, NULL);
333       GNUNET_assert (GNUNET_OK == GNUNET_FS_namespace_delete (ns, GNUNET_NO));
334       return;
335     }
336   }
337   GNUNET_FS_uri_destroy (uri);
338   uri = NULL;
339   GNUNET_FS_stop (ctx);
340   ctx = NULL;
341 }
342
343
344 /**
345  * Main function that will be run by the scheduler.
346  *
347  * @param cls closure
348  * @param args remaining command-line arguments
349  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
350  * @param c configuration
351  */
352 static void
353 run (void *cls, char *const *args, const char *cfgfile,
354      const struct GNUNET_CONFIGURATION_Handle *c)
355 {
356   struct GNUNET_FS_FileInformation *fi;
357   struct GNUNET_FS_Namespace *namespace;
358   struct EXTRACTOR_PluginList *plugins;
359   struct GNUNET_FS_Uri *keywords;
360   struct stat sbuf;
361   char *ex;
362   char *emsg;
363
364   /* check arguments */
365   if ((uri_string != NULL) && (extract_only))
366   {
367     printf (_("Cannot extract metadata from a URI!\n"));
368     ret = -1;
369     return;
370   }
371   if (((uri_string == NULL) || (extract_only)) &&
372       ((args[0] == NULL) || (args[1] != NULL)))
373   {
374     printf (_("You must specify one and only one filename for insertion.\n"));
375     ret = -1;
376     return;
377   }
378   if ((uri_string != NULL) && (args[0] != NULL))
379   {
380     printf (_("You must NOT specify an URI and a filename.\n"));
381     ret = -1;
382     return;
383   }
384   if (pseudonym != NULL)
385   {
386     if (NULL == this_id)
387     {
388       FPRINTF (stderr, _("Option `%s' is required when using option `%s'.\n"),
389                "-t", "-P");
390       ret = -1;
391       return;
392     }
393   }
394   else
395   {                             /* ordinary insertion checks */
396     if (NULL != next_id)
397     {
398       FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
399                "-N", "-P");
400       ret = -1;
401       return;
402     }
403     if (NULL != this_id)
404     {
405       FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
406                "-t", "-P");
407       ret = -1;
408       return;
409     }
410   }
411   cfg = c;
412   ctx =
413       GNUNET_FS_start (cfg, "gnunet-publish", &progress_cb, NULL,
414                        GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
415   if (NULL == ctx)
416   {
417     FPRINTF (stderr, _("Could not initialize `%s' subsystem.\n"), "FS");
418     ret = 1;
419     return;
420   }
421   namespace = NULL;
422   if (NULL != pseudonym)
423   {
424     namespace = GNUNET_FS_namespace_create (ctx, pseudonym);
425     if (NULL == namespace)
426     {
427       FPRINTF (stderr, _("Could not create namespace `%s'\n"), pseudonym);
428       GNUNET_FS_stop (ctx);
429       ret = 1;
430       return;
431     }
432   }
433   if (NULL != uri_string)
434   {
435     emsg = NULL;
436     uri = GNUNET_FS_uri_parse (uri_string, &emsg);
437     if (uri == NULL)
438     {
439       FPRINTF (stderr, _("Failed to parse URI: %s\n"), emsg);
440       GNUNET_free (emsg);
441       if (namespace != NULL)
442         GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
443       GNUNET_FS_stop (ctx);
444       ret = 1;
445       return;
446     }
447     GNUNET_FS_publish_ksk (ctx, topKeywords, meta, uri, &bo,
448                            GNUNET_FS_PUBLISH_OPTION_NONE, &uri_ksk_continuation,
449                            NULL);
450     if (namespace != NULL)
451       GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
452     return;
453   }
454   plugins = NULL;
455   if (!disable_extractor)
456   {
457     plugins = EXTRACTOR_plugin_add_defaults (EXTRACTOR_OPTION_DEFAULT_POLICY);
458     if (GNUNET_OK ==
459         GNUNET_CONFIGURATION_get_value_string (cfg, "FS", "EXTRACTORS", &ex))
460     {
461       if (strlen (ex) > 0)
462         plugins =
463             EXTRACTOR_plugin_add_config (plugins, ex,
464                                          EXTRACTOR_OPTION_DEFAULT_POLICY);
465       GNUNET_free (ex);
466     }
467   }
468   emsg = NULL;
469   GNUNET_assert (NULL != args[0]);
470   if (0 != STAT (args[0], &sbuf))
471   {
472     GNUNET_asprintf (&emsg, _("Could not access file: %s\n"), STRERROR (errno));
473     fi = NULL;
474   }
475   else if (S_ISDIR (sbuf.st_mode))
476   {
477     fi = GNUNET_FS_file_information_create_from_directory (ctx, NULL, args[0],
478                                                            &GNUNET_FS_directory_scanner_default,
479                                                            plugins, !do_insert,
480                                                            &bo, &emsg);
481   }
482   else
483   {
484     if (meta == NULL)
485       meta = GNUNET_CONTAINER_meta_data_create ();
486     GNUNET_FS_meta_data_extract_from_file (meta, args[0], plugins);
487     keywords = GNUNET_FS_uri_ksk_create_from_meta_data (meta);
488     fi = GNUNET_FS_file_information_create_from_file (ctx, NULL, args[0],
489                                                       keywords, NULL,
490                                                       !do_insert, &bo);
491     GNUNET_break (fi != NULL);
492     GNUNET_FS_uri_destroy (keywords);
493   }
494   EXTRACTOR_plugin_remove_all (plugins);
495   if (fi == NULL)
496   {
497     FPRINTF (stderr, _("Could not publish `%s': %s\n"), args[0], emsg);
498     GNUNET_free (emsg);
499     if (namespace != NULL)
500       GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
501     GNUNET_FS_stop (ctx);
502     ret = 1;
503     return;
504   }
505   GNUNET_FS_file_information_inspect (fi, &publish_inspector, NULL);
506   if (extract_only)
507   {
508     if (namespace != NULL)
509       GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
510     GNUNET_FS_file_information_destroy (fi, NULL, NULL);
511     GNUNET_FS_stop (ctx);
512     return;
513   }
514   pc = GNUNET_FS_publish_start (ctx, fi, namespace, this_id, next_id,
515                                 (do_simulate) ?
516                                 GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY :
517                                 GNUNET_FS_PUBLISH_OPTION_NONE);
518   if (NULL == pc)
519   {
520     FPRINTF (stderr, "%s",  _("Could not start publishing.\n"));
521     GNUNET_FS_stop (ctx);
522     ret = 1;
523     return;
524   }
525   kill_task =
526       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_stop_task,
527                                     NULL);
528 }
529
530
531 /**
532  * The main function to publish content to GNUnet.
533  *
534  * @param argc number of arguments from the command line
535  * @param argv command line arguments
536  * @return 0 ok, 1 on error
537  */
538 int
539 main (int argc, char *const *argv)
540 {
541   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
542     {'a', "anonymity", "LEVEL",
543      gettext_noop ("set the desired LEVEL of sender-anonymity"),
544      1, &GNUNET_GETOPT_set_uint, &bo.anonymity_level},
545     {'d', "disable-creation-time", NULL,
546      gettext_noop
547      ("disable adding the creation time to the metadata of the uploaded file"),
548      0, &GNUNET_GETOPT_set_one, &do_disable_creation_time},
549     {'D', "disable-extractor", NULL,
550      gettext_noop ("do not use libextractor to add keywords or metadata"),
551      0, &GNUNET_GETOPT_set_one, &disable_extractor},
552     {'e', "extract", NULL,
553      gettext_noop
554      ("print list of extracted keywords that would be used, but do not perform upload"),
555      0, &GNUNET_GETOPT_set_one, &extract_only},
556     {'k', "key", "KEYWORD",
557      gettext_noop
558      ("add an additional keyword for the top-level file or directory"
559       " (this option can be specified multiple times)"),
560      1, &GNUNET_FS_getopt_set_keywords, &topKeywords},
561     {'m', "meta", "TYPE:VALUE",
562      gettext_noop ("set the meta-data for the given TYPE to the given VALUE"),
563      1, &GNUNET_FS_getopt_set_metadata, &meta},
564     {'n', "noindex", NULL,
565      gettext_noop ("do not index, perform full insertion (stores entire "
566                    "file in encrypted form in GNUnet database)"),
567      0, &GNUNET_GETOPT_set_one, &do_insert},
568     {'N', "next", "ID",
569      gettext_noop
570      ("specify ID of an updated version to be published in the future"
571       " (for namespace insertions only)"),
572      1, &GNUNET_GETOPT_set_string, &next_id},
573     {'p', "priority", "PRIORITY",
574      gettext_noop ("specify the priority of the content"),
575      1, &GNUNET_GETOPT_set_uint, &bo.content_priority},
576     {'P', "pseudonym", "NAME",
577      gettext_noop
578      ("publish the files under the pseudonym NAME (place file into namespace)"),
579      1, &GNUNET_GETOPT_set_string, &pseudonym},
580     {'r', "replication", "LEVEL",
581      gettext_noop ("set the desired replication LEVEL"),
582      1, &GNUNET_GETOPT_set_uint, &bo.replication_level},
583     {'s', "simulate-only", NULL,
584      gettext_noop ("only simulate the process but do not do any "
585                    "actual publishing (useful to compute URIs)"),
586      0, &GNUNET_GETOPT_set_one, &do_simulate},
587     {'t', "this", "ID",
588      gettext_noop ("set the ID of this version of the publication"
589                    " (for namespace insertions only)"),
590      1, &GNUNET_GETOPT_set_string, &this_id},
591     {'u', "uri", "URI",
592      gettext_noop ("URI to be published (can be used instead of passing a "
593                    "file to add keywords to the file with the respective URI)"),
594      1, &GNUNET_GETOPT_set_string, &uri_string},
595     {'V', "verbose", NULL,
596      gettext_noop ("be verbose (print progress information)"),
597      0, &GNUNET_GETOPT_set_one, &verbose},
598     GNUNET_GETOPT_OPTION_END
599   };
600   bo.expiration_time =
601       GNUNET_FS_year_to_time (GNUNET_FS_get_current_year () + 2);
602   return (GNUNET_OK ==
603           GNUNET_PROGRAM_run (argc, argv, "gnunet-publish [OPTIONS] FILENAME",
604                               gettext_noop
605                               ("Publish a file or directory on GNUnet"),
606                               options, &run, NULL)) ? ret : 1;
607 }
608
609 /* end of gnunet-publish.c */