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