2021a98165cf05d95e74b3f466a248f71a24e5c2
[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_GNUNET_ORIGINAL_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  * Iterator printing keywords
214  *
215  * @param cls closure
216  * @param keyword the keyword
217  * @param is_mandatory is the keyword mandatory (in a search)
218  * @return GNUNET_OK to continue to iterate, GNUNET_SYSERR to abort
219  */
220
221 static int
222 keyword_printer (void *cls,
223                  const char *keyword,
224                  int is_mandatory)
225 {
226   fprintf (stdout, 
227            "\t%s\n",
228            keyword);
229   return 0;
230 }
231
232
233 /**
234  * Function called on all entries before the publication.  This is
235  * where we perform modifications to the default based on command-line
236  * options.
237  *
238  * @param cls closure
239  * @param fi the entry in the publish-structure
240  * @param length length of the file or directory
241  * @param m metadata for the file or directory (can be modified)
242  * @param uri pointer to the keywords that will be used for this entry (can be modified)
243  * @param anonymity pointer to selected anonymity level (can be modified)
244  * @param priority pointer to selected priority (can be modified)
245  * @param do_index should we index?
246  * @param expirationTime pointer to selected expiration time (can be modified)
247  * @param client_info pointer to client context set upon creation (can be modified)
248  * @return GNUNET_OK to continue, GNUNET_NO to remove
249  *         this entry from the directory, GNUNET_SYSERR
250  *         to abort the iteration
251  */
252 static int
253 publish_inspector (void *cls,
254                    struct GNUNET_FS_FileInformation *fi,
255                    uint64_t length,
256                    struct GNUNET_CONTAINER_MetaData *m,
257                    struct GNUNET_FS_Uri **uri,
258                    unsigned int *anonymity,
259                    unsigned int *priority,
260                    int *do_index,
261                    struct GNUNET_TIME_Absolute *expirationTime,
262                    void **client_info)
263 {
264   char *fn;
265   char *fs;
266   struct GNUNET_FS_Uri *new_uri;
267
268   if (cls == fi)
269     return GNUNET_OK;
270   if (NULL != topKeywords)
271     {
272       if (*uri != NULL)
273         {
274           new_uri = GNUNET_FS_uri_ksk_merge (topKeywords,
275                                              *uri);
276           GNUNET_FS_uri_destroy (*uri); 
277           *uri = new_uri;
278           GNUNET_FS_uri_destroy (topKeywords);
279         }
280       else
281         {
282           *uri = topKeywords;
283         }
284       topKeywords = NULL;
285     }
286   if (NULL != meta) 
287     {
288       GNUNET_CONTAINER_meta_data_merge (m, meta);
289       GNUNET_CONTAINER_meta_data_destroy (meta);
290       meta = NULL;
291     }
292   if (! do_disable_creation_time)
293     GNUNET_CONTAINER_meta_data_add_publication_date (m);
294   if (extract_only)
295     {
296       fn = GNUNET_CONTAINER_meta_data_get_by_type (m,
297                                                    EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
298       fs = GNUNET_STRINGS_byte_size_fancy (length);
299       fprintf (stdout,
300                _("Meta data for file `%s' (%s)\n"),
301                fn,
302                fs);
303       GNUNET_CONTAINER_meta_data_iterate (m,
304                                           &meta_printer,
305                                           NULL);
306       fprintf (stdout,
307                _("Keywords for file `%s' (%s)\n"),
308                fn,
309                fs);
310       GNUNET_free (fn);
311       GNUNET_free (fs);
312       if (NULL != *uri)
313         GNUNET_FS_uri_ksk_get_keywords (*uri,
314                                         &keyword_printer,
315                                         NULL);
316       fprintf (stdout, "\n");
317     }
318   if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (m))
319     GNUNET_FS_file_information_inspect (fi,
320                                         &publish_inspector,
321                                         fi);
322   return GNUNET_OK;
323 }
324
325
326 static void 
327 uri_sks_continuation (void *cls,
328                       const struct GNUNET_FS_Uri *ksk_uri,
329                       const char *emsg)
330 {
331   if (emsg != NULL)
332     {
333       fprintf (stderr,
334                "%s\n",
335                emsg);
336       ret = 1;
337     }
338   GNUNET_FS_uri_destroy (uri);
339   uri = NULL;
340   GNUNET_FS_stop (ctx);
341   ctx = NULL;
342 }
343
344
345 static void 
346 uri_ksk_continuation (void *cls,
347                       const struct GNUNET_FS_Uri *ksk_uri,
348                       const char *emsg)
349 {
350   struct GNUNET_FS_Namespace *ns;
351
352   if (emsg != NULL)
353     {
354       fprintf (stderr,
355                "%s\n",
356                emsg);
357       ret = 1;
358     }
359   if (pseudonym != NULL)
360     {
361       ns = GNUNET_FS_namespace_create (ctx,
362                                        pseudonym);
363       if (ns == NULL)
364         {
365           fprintf (stderr,
366                    _("Failed to create namespace `%s'\n"),
367                    pseudonym);
368           ret = 1;
369         }
370       else
371         {
372           GNUNET_FS_publish_sks (ctx,
373                                  ns,
374                                  this_id,
375                                  next_id,
376                                  meta,
377                                  uri,
378                                  GNUNET_TIME_relative_to_absolute (DEFAULT_EXPIRATION),
379                                  anonymity,
380                                  priority,
381                                  GNUNET_FS_PUBLISH_OPTION_NONE,
382                                  uri_sks_continuation,
383                                  NULL);
384           GNUNET_assert (GNUNET_OK ==
385                          GNUNET_FS_namespace_delete (ns, GNUNET_NO));
386           return;
387         }
388     }
389   GNUNET_FS_uri_destroy (uri);
390   uri = NULL;
391   GNUNET_FS_stop (ctx);
392   ctx = NULL;
393 }
394
395
396 /**
397  * Main function that will be run by the scheduler.
398  *
399  * @param cls closure
400  * @param args remaining command-line arguments
401  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
402  * @param c configuration
403  */
404 static void
405 run (void *cls,
406      char *const *args,
407      const char *cfgfile,
408      const struct GNUNET_CONFIGURATION_Handle *c)
409 {
410   struct GNUNET_FS_FileInformation *fi;
411   struct GNUNET_FS_Namespace *namespace;
412   struct EXTRACTOR_PluginList *plugins;
413   struct GNUNET_FS_Uri *keywords;
414   struct stat sbuf;
415   char *ex;
416   char *emsg;
417   
418   /* check arguments */
419   if ((uri_string != NULL) && (extract_only))
420     {
421       printf (_("Cannot extract metadata from a URI!\n"));
422       ret = -1;
423       return;
424     }
425   if ( ( (uri_string == NULL) || (extract_only) ) 
426        && ( (args[0] == NULL) || (args[1] != NULL) ) )
427     {
428       printf (_
429               ("You must specify one and only one filename for insertion.\n"));
430       ret = -1;
431       return;
432     }
433   if ((uri_string != NULL) && (args[0] != NULL))
434     {
435       printf (_("You must NOT specify an URI and a filename.\n"));
436       ret = -1;
437       return;
438     }
439   if (pseudonym != NULL)
440     {
441       if (NULL == this_id)
442         {
443           fprintf (stderr,
444                    _("Option `%s' is required when using option `%s'.\n"),
445                    "-t", "-P");
446           ret = -1;
447           return;
448         }
449     }
450   else
451     {                           /* ordinary insertion checks */
452       if (NULL != next_id)
453         {
454           fprintf (stderr,
455                    _("Option `%s' makes no sense without option `%s'.\n"),
456                    "-N", "-P");
457           ret = -1;
458           return;
459         }
460       if (NULL != this_id)
461         {
462           fprintf (stderr,
463                    _("Option `%s' makes no sense without option `%s'.\n"),
464                    "-t", "-P");
465           ret = -1;
466           return;
467         }
468     }
469   cfg = c;
470   ctx = GNUNET_FS_start (cfg,
471                          "gnunet-publish",
472                          &progress_cb,
473                          NULL,
474                          GNUNET_FS_FLAGS_NONE,
475                          GNUNET_FS_OPTIONS_END);
476   if (NULL == ctx)
477     {
478       fprintf (stderr,
479                _("Could not initialize `%s' subsystem.\n"),
480                "FS");
481       ret = 1;
482       return;
483     }
484   namespace = NULL;
485   if (NULL != pseudonym)
486     {
487       namespace = GNUNET_FS_namespace_create (ctx,
488                                               pseudonym);
489       if (NULL == namespace)
490         {
491           fprintf (stderr,
492                    _("Could not create namespace `%s'\n"),
493                    pseudonym);
494           GNUNET_FS_stop (ctx);
495           ret = 1;
496           return;
497         }
498     }
499   if (NULL != uri_string)
500     {      
501       emsg = NULL;
502       uri = GNUNET_FS_uri_parse (uri_string,
503                                  &emsg);
504       if (uri == NULL)
505         {
506           fprintf (stderr, 
507                    _("Failed to parse URI: %s\n"),
508                    emsg);
509           GNUNET_free (emsg);
510           if (namespace != NULL)
511             GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
512           GNUNET_FS_stop (ctx);
513           ret = 1;
514           return;         
515         }
516       GNUNET_FS_publish_ksk (ctx,
517                              topKeywords,
518                              meta,
519                              uri,
520                              GNUNET_TIME_relative_to_absolute (DEFAULT_EXPIRATION),
521                              anonymity,
522                              priority,
523                              GNUNET_FS_PUBLISH_OPTION_NONE,
524                              &uri_ksk_continuation,
525                              NULL);
526       if (namespace != NULL)
527         GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
528       return;
529     }
530   plugins = NULL;
531   if (! disable_extractor)
532     {
533       plugins = EXTRACTOR_plugin_add_defaults (EXTRACTOR_OPTION_DEFAULT_POLICY);
534       if (GNUNET_OK ==
535           GNUNET_CONFIGURATION_get_value_string (cfg, "FS", "EXTRACTORS",
536                                                  &ex))
537         {
538           if (strlen (ex) > 0)
539             plugins = EXTRACTOR_plugin_add_config (plugins, ex, EXTRACTOR_OPTION_DEFAULT_POLICY);
540           GNUNET_free (ex);
541         }
542     }
543   emsg = NULL;
544   GNUNET_assert (NULL != args[0]);
545   if (0 != STAT (args[0], &sbuf))
546     {
547       GNUNET_asprintf (&emsg,
548                        _("Could not access file: %s\n"),
549                        STRERROR (errno));
550       fi = NULL;
551     }
552   else if (S_ISDIR (sbuf.st_mode))
553     {
554       fi = GNUNET_FS_file_information_create_from_directory (ctx,
555                                                              NULL,
556                                                              args[0],
557                                                              &GNUNET_FS_directory_scanner_default,
558                                                              plugins,
559                                                              !do_insert,
560                                                              anonymity,
561                                                              priority,
562                                                              GNUNET_TIME_relative_to_absolute (DEFAULT_EXPIRATION),
563                                                              &emsg);
564     }
565   else
566     {
567       if (meta == NULL)
568         meta = GNUNET_CONTAINER_meta_data_create ();
569       GNUNET_FS_meta_data_extract_from_file (meta,
570                                              args[0],
571                                              plugins);
572       keywords = GNUNET_FS_uri_ksk_create_from_meta_data (meta);
573       fi = GNUNET_FS_file_information_create_from_file (ctx,
574                                                         NULL,
575                                                         args[0],
576                                                         keywords,
577                                                         NULL,
578                                                         !do_insert,
579                                                         anonymity,
580                                                         priority,
581                                                         GNUNET_TIME_relative_to_absolute (DEFAULT_EXPIRATION));
582       GNUNET_break (fi != NULL);
583       GNUNET_FS_uri_destroy (keywords);
584     }
585   EXTRACTOR_plugin_remove_all (plugins);  
586   if (fi == NULL)
587     {
588       fprintf (stderr,
589                _("Could not publish `%s': %s\n"),
590                args[0],
591                emsg);
592       GNUNET_free (emsg);
593       if (namespace != NULL)
594         GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
595       GNUNET_FS_stop (ctx);
596       ret = 1;
597       return;
598     }
599   GNUNET_FS_file_information_inspect (fi,
600                                       &publish_inspector,
601                                       NULL);
602   if (extract_only)
603     {
604       if (namespace != NULL)
605         GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
606       GNUNET_FS_file_information_destroy (fi, NULL, NULL);
607       GNUNET_FS_stop (ctx);
608       return;
609     }
610   pc = GNUNET_FS_publish_start (ctx,
611                                 fi,
612                                 namespace,
613                                 this_id,
614                                 next_id,
615                                 (do_simulate) 
616                                 ? GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY
617                                 : GNUNET_FS_PUBLISH_OPTION_NONE);
618   if (NULL == pc)
619     {
620       fprintf (stderr,
621                _("Could not start publishing.\n"));
622       GNUNET_FS_stop (ctx);
623       ret = 1;
624       return;
625     }
626   kill_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
627                                             &do_stop_task,
628                                             NULL);
629 }
630
631
632
633
634 /**
635  * The main function to publish content to GNUnet.
636  *
637  * @param argc number of arguments from the command line
638  * @param argv command line arguments
639  * @return 0 ok, 1 on error
640  */
641 int
642 main (int argc, char *const *argv)
643 {
644   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
645     {'a', "anonymity", "LEVEL",
646      gettext_noop ("set the desired LEVEL of sender-anonymity"),
647      1, &GNUNET_GETOPT_set_uint, &anonymity},
648     {'d', "disable-creation-time", NULL,
649      gettext_noop
650      ("disable adding the creation time to the metadata of the uploaded file"),
651      0, &GNUNET_GETOPT_set_one, &do_disable_creation_time},
652     {'D', "disable-extractor", NULL,
653      gettext_noop
654      ("do not use libextractor to add keywords or metadata"),
655      0, &GNUNET_GETOPT_set_one, &disable_extractor},
656     {'e', "extract", NULL,
657      gettext_noop
658      ("print list of extracted keywords that would be used, but do not perform upload"),
659      0, &GNUNET_GETOPT_set_one, &extract_only},
660     {'k', "key", "KEYWORD",
661      gettext_noop
662      ("add an additional keyword for the top-level file or directory"
663       " (this option can be specified multiple times)"),
664      1, &GNUNET_FS_getopt_set_keywords, &topKeywords},
665     {'m', "meta", "TYPE:VALUE",
666      gettext_noop ("set the meta-data for the given TYPE to the given VALUE"),
667      1, &GNUNET_FS_getopt_set_metadata, &meta},
668     {'n', "noindex", NULL,
669      gettext_noop ("do not index, perform full insertion (stores entire "
670                    "file in encrypted form in GNUnet database)"),
671      0, &GNUNET_GETOPT_set_one, &do_insert},
672     {'N', "next", "ID",
673      gettext_noop
674      ("specify ID of an updated version to be published in the future"
675       " (for namespace insertions only)"),
676      1, &GNUNET_GETOPT_set_string, &next_id},
677     {'p', "priority", "PRIORITY",
678      gettext_noop ("specify the priority of the content"),
679      1, &GNUNET_GETOPT_set_uint, &priority},
680     {'P', "pseudonym", "NAME",
681      gettext_noop
682      ("publish the files under the pseudonym NAME (place file into namespace)"),
683      1, &GNUNET_GETOPT_set_string, &pseudonym},
684     {'s', "simulate-only", NULL,
685      gettext_noop ("only simulate the process but do not do any "
686                    "actual publishing (useful to compute URIs)"),
687      0, &GNUNET_GETOPT_set_one, &do_simulate},
688     {'t', "this", "ID",
689      gettext_noop ("set the ID of this version of the publication"
690                    " (for namespace insertions only)"),
691      1, &GNUNET_GETOPT_set_string, &this_id},
692     {'u', "uri", "URI",
693      gettext_noop ("URI to be published (can be used instead of passing a "
694                    "file to add keywords to the file with the respective URI)"),
695      1, &GNUNET_GETOPT_set_string, &uri_string}, 
696     {'V', "verbose", NULL,
697      gettext_noop ("be verbose (print progress information)"),
698      0, &GNUNET_GETOPT_set_one, &verbose},
699     GNUNET_GETOPT_OPTION_END
700   };
701   return (GNUNET_OK ==
702           GNUNET_PROGRAM_run (argc,
703                               argv,
704                               "gnunet-publish",
705                               gettext_noop
706                               ("Publish files on GNUnet."),
707                               options, &run, NULL)) ? ret : 1;
708 }
709
710 /* end of gnunet-publish.c */