7844acd96a03e7650a9604f6ed6eae45a7531208
[oweals/gnunet.git] / src / fs / gnunet-publish.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2001-2013 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 #include "gnunet_identity_service.h"
31
32 /**
33  * Global return value from 'main'.
34  */
35 static int ret = 1;
36
37 /**
38  * Command line option 'verbose' set
39  */
40 static int verbose;
41
42 /**
43  * Handle to our configuration.
44  */
45 static const struct GNUNET_CONFIGURATION_Handle *cfg;
46
47 /**
48  * Handle for interaction with file-sharing service.
49  */
50 static struct GNUNET_FS_Handle *ctx;
51
52 /**
53  * Handle to FS-publishing operation.
54  */
55 static struct GNUNET_FS_PublishContext *pc;
56
57 /**
58  * Meta-data provided via command-line option.
59  */
60 static struct GNUNET_CONTAINER_MetaData *meta;
61
62 /**
63  * Keywords provided via command-line option.
64  */
65 static struct GNUNET_FS_Uri *topKeywords;
66
67 /**
68  * Options we set for published blocks.
69  */
70 static struct GNUNET_FS_BlockOptions bo = { {0LL}, 1, 365, 1 };
71
72 /**
73  * Value of URI provided on command-line (when not publishing
74  * a file but just creating UBlocks to refer to an existing URI).
75  */
76 static char *uri_string;
77
78 /**
79  * Value of URI provided on command-line (when not publishing
80  * a file but just creating UBlocks to refer to an existing URI);
81  * parsed version of 'uri_string'.
82  */
83 static struct GNUNET_FS_Uri *uri;
84
85 /**
86  * Command-line option for namespace publishing: identifier for updates
87  * to this publication.
88  */
89 static char *next_id;
90
91 /**
92  * Command-line option for namespace publishing: identifier for this
93  * publication.
94  */
95 static char *this_id;
96
97 /**
98  * Command-line option identifying the pseudonym to use for the publication.
99  */
100 static char *pseudonym;
101
102 /**
103  * Command-line option for 'inserting'
104  */
105 static int do_insert;
106
107 /**
108  * Command-line option to disable meta data extraction.
109  */
110 static int disable_extractor;
111
112 /**
113  * Command-line option to merely simulate publishing operation.
114  */
115 static int do_simulate;
116
117 /**
118  * Command-line option to only perform meta data extraction, but not publish.
119  */
120 static int extract_only;
121
122 /**
123  * Command-line option to disable adding creation time.
124  */
125 static int do_disable_creation_time;
126
127 /**
128  * Task run on CTRL-C to kill everything nicely.
129  */
130 static struct GNUNET_SCHEDULER_Task * kill_task;
131
132 /**
133  * Handle to the directory scanner (for recursive insertions).
134  */
135 static struct GNUNET_FS_DirScanner *ds;
136
137 /**
138  * Which namespace do we publish to? NULL if we do not publish to
139  * a namespace.
140  */
141 static struct GNUNET_IDENTITY_Ego *namespace;
142
143 /**
144  * Handle to identity service.
145  */
146 static struct GNUNET_IDENTITY_Handle *identity;
147
148
149 /**
150  * We are finished with the publishing operation, clean up all
151  * FS state.
152  *
153  * @param cls NULL
154  * @param tc scheduler context
155  */
156 static void
157 do_stop_task (void *cls,
158               const struct GNUNET_SCHEDULER_TaskContext *tc)
159 {
160   struct GNUNET_FS_PublishContext *p;
161
162   kill_task = NULL;
163   if (NULL != identity)
164   {
165     GNUNET_IDENTITY_disconnect (identity);
166     identity = NULL;
167   }
168   if (NULL != pc)
169   {
170     p = pc;
171     pc = NULL;
172     GNUNET_FS_publish_stop (p);
173   }
174   if (NULL != meta)
175   {
176     GNUNET_CONTAINER_meta_data_destroy (meta);
177     meta = NULL;
178   }
179 }
180
181
182 /**
183  * Stop the directory scanner (we had an error).
184  *
185  * @param cls closure
186  * @param tc scheduler context
187  */
188 static void
189 stop_scanner_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
190 {
191   kill_task = NULL;
192   if (NULL != ds)
193   {
194     GNUNET_FS_directory_scan_abort (ds);
195     ds = NULL;
196   }
197   if (NULL != identity)
198   {
199     GNUNET_IDENTITY_disconnect (identity);
200     identity = NULL;
201   }
202   GNUNET_FS_stop (ctx);
203   ctx = NULL;
204   ret = 1;
205 }
206
207
208 /**
209  * Called by FS client to give information about the progress of an
210  * operation.
211  *
212  * @param cls closure
213  * @param info details about the event, specifying the event type
214  *        and various bits about the event
215  * @return client-context (for the next progress call
216  *         for this operation; should be set to NULL for
217  *         SUSPEND and STOPPED events).  The value returned
218  *         will be passed to future callbacks in the respective
219  *         field in the GNUNET_FS_ProgressInfo struct.
220  */
221 static void *
222 progress_cb (void *cls,
223              const struct GNUNET_FS_ProgressInfo *info)
224 {
225   const char *s;
226   char *suri;
227
228   switch (info->status)
229   {
230   case GNUNET_FS_STATUS_PUBLISH_START:
231     break;
232   case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
233     if (verbose)
234     {
235       s = GNUNET_STRINGS_relative_time_to_string (info->value.publish.eta,
236                                                   GNUNET_YES);
237       FPRINTF (stdout,
238                _("Publishing `%s' at %llu/%llu (%s remaining)\n"),
239                info->value.publish.filename,
240                (unsigned long long) info->value.publish.completed,
241                (unsigned long long) info->value.publish.size, s);
242     }
243     break;
244   case GNUNET_FS_STATUS_PUBLISH_PROGRESS_DIRECTORY:
245     if (verbose)
246     {
247       s = GNUNET_STRINGS_relative_time_to_string (info->value.publish.specifics.progress_directory.eta,
248                                                   GNUNET_YES);
249       FPRINTF (stdout,
250                _("Publishing `%s' at %llu/%llu (%s remaining)\n"),
251                info->value.publish.filename,
252                (unsigned long long) info->value.publish.specifics.progress_directory.completed,
253                (unsigned long long) info->value.publish.specifics.progress_directory.total, s);
254     }
255     break;
256   case GNUNET_FS_STATUS_PUBLISH_ERROR:
257     FPRINTF (stderr, _("Error publishing: %s.\n"),
258              info->value.publish.specifics.error.message);
259     if (kill_task != NULL)
260     {
261       GNUNET_SCHEDULER_cancel (kill_task);
262       kill_task = NULL;
263     }
264     kill_task = GNUNET_SCHEDULER_add_now (&do_stop_task, NULL);
265     break;
266   case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
267     FPRINTF (stdout,
268              _("Publishing `%s' done.\n"),
269              info->value.publish.filename);
270     suri = GNUNET_FS_uri_to_string (info->value.publish.specifics.
271                                     completed.chk_uri);
272     FPRINTF (stdout,
273              _("URI is `%s'.\n"),
274              suri);
275     GNUNET_free (suri);
276     if (NULL != info->value.publish.specifics.completed.sks_uri)
277     {
278       suri = GNUNET_FS_uri_to_string (info->value.publish.specifics.
279                                       completed.sks_uri);
280       FPRINTF (stdout,
281                _("Namespace URI is `%s'.\n"),
282                suri);
283       GNUNET_free (suri);
284     }
285     if (NULL == info->value.publish.pctx)
286     {
287       if (NULL != kill_task)
288         GNUNET_SCHEDULER_cancel (kill_task);
289       kill_task = GNUNET_SCHEDULER_add_now (&do_stop_task, NULL);
290     }
291     ret = 0;
292     break;
293   case GNUNET_FS_STATUS_PUBLISH_STOPPED:
294     GNUNET_break (NULL == pc);
295     return NULL;
296   case GNUNET_FS_STATUS_UNINDEX_START:
297     FPRINTF (stderr,
298              "%s",
299              _("Starting cleanup after abort\n"));
300     return NULL;
301   case GNUNET_FS_STATUS_UNINDEX_PROGRESS:
302     return NULL;
303   case GNUNET_FS_STATUS_UNINDEX_COMPLETED:
304     FPRINTF (stderr,
305              "%s",
306              _("Cleanup after abort completed.\n"));
307     GNUNET_FS_unindex_stop (info->value.unindex.uc);
308     return NULL;
309   case GNUNET_FS_STATUS_UNINDEX_ERROR:
310     FPRINTF (stderr,
311              "%s",
312              _("Cleanup after abort failed.\n"));
313     GNUNET_FS_unindex_stop (info->value.unindex.uc);
314     return NULL;
315   default:
316     FPRINTF (stderr,
317              _("Unexpected status: %d\n"),
318              info->status);
319     return NULL;
320   }
321   return "";                    /* non-null */
322 }
323
324
325 /**
326  * Print metadata entries (except binary
327  * metadata and the filename).
328  *
329  * @param cls closure
330  * @param plugin_name name of the plugin that generated the meta data
331  * @param type type of the meta data
332  * @param format format of data
333  * @param data_mime_type mime type of @a data
334  * @param data value of the meta data
335  * @param data_size number of bytes in @a data
336  * @return always 0
337  */
338 static int
339 meta_printer (void *cls,
340               const char *plugin_name,
341               enum EXTRACTOR_MetaType type,
342               enum EXTRACTOR_MetaFormat format,
343               const char *data_mime_type,
344               const char *data, size_t data_size)
345 {
346   if ((EXTRACTOR_METAFORMAT_UTF8 != format) &&
347       (EXTRACTOR_METAFORMAT_C_STRING != format))
348     return 0;
349   if (EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME == type)
350     return 0;
351 #if HAVE_LIBEXTRACTOR
352   FPRINTF (stdout, "\t%s - %s\n", EXTRACTOR_metatype_to_string (type), data);
353 #else
354   FPRINTF (stdout, "\t%d - %s\n", type, data);
355 #endif
356   return 0;
357 }
358
359
360 /**
361  * Iterator printing keywords
362  *
363  * @param cls closure
364  * @param keyword the keyword
365  * @param is_mandatory is the keyword mandatory (in a search)
366  * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR to abort
367  */
368 static int
369 keyword_printer (void *cls,
370                  const char *keyword,
371                  int is_mandatory)
372 {
373   FPRINTF (stdout, "\t%s\n", keyword);
374   return GNUNET_OK;
375 }
376
377
378 /**
379  * Function called on all entries before the publication.  This is
380  * where we perform modifications to the default based on command-line
381  * options.
382  *
383  * @param cls closure
384  * @param fi the entry in the publish-structure
385  * @param length length of the file or directory
386  * @param m metadata for the file or directory (can be modified)
387  * @param uri pointer to the keywords that will be used for this entry (can be modified)
388  * @param bo block options
389  * @param do_index should we index?
390  * @param client_info pointer to client context set upon creation (can be modified)
391  * @return #GNUNET_OK to continue, #GNUNET_NO to remove
392  *         this entry from the directory, #GNUNET_SYSERR
393  *         to abort the iteration
394  */
395 static int
396 publish_inspector (void *cls,
397                    struct GNUNET_FS_FileInformation *fi,
398                    uint64_t length,
399                    struct GNUNET_CONTAINER_MetaData *m,
400                    struct GNUNET_FS_Uri **uri,
401                    struct GNUNET_FS_BlockOptions *bo,
402                    int *do_index,
403                    void **client_info)
404 {
405   char *fn;
406   char *fs;
407   struct GNUNET_FS_Uri *new_uri;
408
409   if (cls == fi)
410     return GNUNET_OK;
411   if ( (disable_extractor) &&
412        (NULL != *uri) )
413   {
414     GNUNET_FS_uri_destroy (*uri);
415     *uri = NULL;
416   }
417   if (NULL != topKeywords)
418   {
419     if (NULL != *uri)
420     {
421       new_uri = GNUNET_FS_uri_ksk_merge (topKeywords, *uri);
422       GNUNET_FS_uri_destroy (*uri);
423       *uri = new_uri;
424       GNUNET_FS_uri_destroy (topKeywords);
425     }
426     else
427     {
428       *uri = topKeywords;
429     }
430     topKeywords = NULL;
431   }
432   if (NULL != meta)
433   {
434     GNUNET_CONTAINER_meta_data_merge (m, meta);
435     GNUNET_CONTAINER_meta_data_destroy (meta);
436     meta = NULL;
437   }
438   if (!do_disable_creation_time)
439     GNUNET_CONTAINER_meta_data_add_publication_date (m);
440   if (extract_only)
441   {
442     fn = GNUNET_CONTAINER_meta_data_get_by_type (m,
443                                                  EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
444     fs = GNUNET_STRINGS_byte_size_fancy (length);
445     FPRINTF (stdout, _("Meta data for file `%s' (%s)\n"), fn, fs);
446     GNUNET_CONTAINER_meta_data_iterate (m, &meta_printer, NULL);
447     FPRINTF (stdout, _("Keywords for file `%s' (%s)\n"), fn, fs);
448     GNUNET_free (fn);
449     GNUNET_free (fs);
450     if (NULL != *uri)
451       GNUNET_FS_uri_ksk_get_keywords (*uri, &keyword_printer, NULL);
452     FPRINTF (stdout, "%s",  "\n");
453   }
454   if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (m))
455     GNUNET_FS_file_information_inspect (fi, &publish_inspector, fi);
456   return GNUNET_OK;
457 }
458
459
460 /**
461  * Function called upon completion of the publishing
462  * of the UBLOCK for the SKS URI.  As this is the last
463  * step, stop our interaction with FS (clean up).
464  *
465  * @param cls NULL (closure)
466  * @param sks_uri URI for the block that was published
467  * @param emsg error message, NULL on success
468  */
469 static void
470 uri_sks_continuation (void *cls,
471                       const struct GNUNET_FS_Uri *sks_uri,
472                       const char *emsg)
473 {
474   if (NULL != emsg)
475   {
476     FPRINTF (stderr, "%s\n", emsg);
477     ret = 1;
478   }
479   GNUNET_FS_uri_destroy (uri);
480   uri = NULL;
481   GNUNET_FS_stop (ctx);
482   ctx = NULL;
483 }
484
485
486 /**
487  * Function called upon completion of the publishing
488  * of the UBLOCK for the KSK URI.  Continue with
489  * publishing the SKS URI (if applicable) or clean up.
490  *
491  * @param cls NULL (closure)
492  * @param ksk_uri URI for the block that was published
493  * @param emsg error message, NULL on success
494  */
495 static void
496 uri_ksk_continuation (void *cls,
497                       const struct GNUNET_FS_Uri *ksk_uri,
498                       const char *emsg)
499 {
500   const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv;
501
502   if (NULL != emsg)
503   {
504     FPRINTF (stderr, "%s\n", emsg);
505     ret = 1;
506   }
507   if (NULL != namespace)
508   {
509     priv = GNUNET_IDENTITY_ego_get_private_key (namespace);
510     GNUNET_FS_publish_sks (ctx, priv, this_id, next_id, meta, uri, &bo,
511                            GNUNET_FS_PUBLISH_OPTION_NONE,
512                            &uri_sks_continuation, NULL);
513     return;
514   }
515   GNUNET_FS_uri_destroy (uri);
516   uri = NULL;
517   GNUNET_FS_stop (ctx);
518   ctx = NULL;
519 }
520
521
522 /**
523  * Iterate over the results from the directory scan and extract
524  * the desired information for the publishing operation.
525  *
526  * @param item root with the data from the directroy scan
527  * @return handle with the information for the publishing operation
528  */
529 static struct GNUNET_FS_FileInformation *
530 get_file_information (struct GNUNET_FS_ShareTreeItem *item)
531 {
532   struct GNUNET_FS_FileInformation *fi;
533   struct GNUNET_FS_FileInformation *fic;
534   struct GNUNET_FS_ShareTreeItem *child;
535
536   if (GNUNET_YES == item->is_directory)
537   {
538     if (NULL == item->meta)
539       item->meta = GNUNET_CONTAINER_meta_data_create ();
540     GNUNET_CONTAINER_meta_data_delete (item->meta,
541                                        EXTRACTOR_METATYPE_MIMETYPE,
542                                        NULL, 0);
543     GNUNET_FS_meta_data_make_directory (item->meta);
544     if (NULL == item->ksk_uri)
545     {
546       const char *mime = GNUNET_FS_DIRECTORY_MIME;
547       item->ksk_uri = GNUNET_FS_uri_ksk_create_from_args (1, &mime);
548     }
549     else
550       GNUNET_FS_uri_ksk_add_keyword (item->ksk_uri, GNUNET_FS_DIRECTORY_MIME,
551                                      GNUNET_NO);
552     fi = GNUNET_FS_file_information_create_empty_directory (ctx, NULL,
553                                                             item->ksk_uri,
554                                                             item->meta,
555                                                             &bo, item->filename);
556     for (child = item->children_head; child; child = child->next)
557     {
558       fic = get_file_information (child);
559       GNUNET_break (GNUNET_OK == GNUNET_FS_file_information_add (fi, fic));
560     }
561   }
562   else
563   {
564     fi = GNUNET_FS_file_information_create_from_file (ctx, NULL,
565                                                       item->filename,
566                                                       item->ksk_uri, item->meta,
567                                                       !do_insert,
568                                                       &bo);
569   }
570   return fi;
571 }
572
573
574 /**
575  * We've finished scanning the directory and optimized the meta data.
576  * Begin the publication process.
577  *
578  * @param directory_scan_result result from the directory scan, freed in this function
579  */
580 static void
581 directory_trim_complete (struct GNUNET_FS_ShareTreeItem *directory_scan_result)
582 {
583   struct GNUNET_FS_FileInformation *fi;
584   const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv;
585
586   fi = get_file_information (directory_scan_result);
587   GNUNET_FS_share_tree_free (directory_scan_result);
588   if (NULL == fi)
589   {
590     FPRINTF (stderr,
591              "%s",
592              _("Could not publish\n"));
593     GNUNET_SCHEDULER_shutdown ();
594     ret = 1;
595     return;
596   }
597   GNUNET_FS_file_information_inspect (fi, &publish_inspector, NULL);
598   if (extract_only)
599   {
600     GNUNET_FS_file_information_destroy (fi, NULL, NULL);
601     GNUNET_SCHEDULER_shutdown ();
602     return;
603   }
604   if (NULL == namespace)
605     priv = NULL;
606   else
607     priv = GNUNET_IDENTITY_ego_get_private_key (namespace);
608   pc = GNUNET_FS_publish_start (ctx, fi,
609                                 priv, this_id, next_id,
610                                 (do_simulate) ?
611                                 GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY :
612                                 GNUNET_FS_PUBLISH_OPTION_NONE);
613   if (NULL == pc)
614   {
615     FPRINTF (stderr,
616              "%s",
617              _("Could not start publishing.\n"));
618     GNUNET_SCHEDULER_shutdown ();
619     ret = 1;
620     return;
621   }
622 }
623
624
625 /**
626  * Function called by the directory scanner as we build the tree
627  * that we will need to publish later.
628  *
629  * @param cls closure
630  * @param filename which file we are making progress on
631  * @param is_directory #GNUNET_YES if this is a directory,
632  *                     #GNUNET_NO if this is a file
633  *                     #GNUNET_SYSERR if it is neither (or unknown)
634  * @param reason kind of progress we are making
635  */
636 static void
637 directory_scan_cb (void *cls,
638                    const char *filename,
639                    int is_directory,
640                    enum GNUNET_FS_DirScannerProgressUpdateReason reason)
641 {
642   struct GNUNET_FS_ShareTreeItem *directory_scan_result;
643
644   switch (reason)
645   {
646   case GNUNET_FS_DIRSCANNER_FILE_START:
647     if (verbose > 1)
648     {
649       if (is_directory == GNUNET_YES)
650         FPRINTF (stdout,
651                  _("Scanning directory `%s'.\n"),
652                  filename);
653       else
654         FPRINTF (stdout,
655                  _("Scanning file `%s'.\n"),
656                  filename);
657     }
658     break;
659   case GNUNET_FS_DIRSCANNER_FILE_IGNORED:
660     FPRINTF (stderr,
661              _("There was trouble processing file `%s', skipping it.\n"),
662              filename);
663     break;
664   case GNUNET_FS_DIRSCANNER_ALL_COUNTED:
665     if (verbose)
666       FPRINTF (stdout,
667                "%s",
668                _("Preprocessing complete.\n"));
669     break;
670   case GNUNET_FS_DIRSCANNER_EXTRACT_FINISHED:
671     if (verbose > 2)
672       FPRINTF (stdout,
673                _("Extracting meta data from file `%s' complete.\n"),
674                filename);
675     break;
676   case GNUNET_FS_DIRSCANNER_FINISHED:
677     if (verbose > 1)
678       FPRINTF (stdout,
679                "%s",
680                _("Meta data extraction has finished.\n"));
681     directory_scan_result = GNUNET_FS_directory_scan_get_result (ds);
682     ds = NULL;
683     GNUNET_FS_share_tree_trim (directory_scan_result);
684     directory_trim_complete (directory_scan_result);
685     break;
686   case GNUNET_FS_DIRSCANNER_INTERNAL_ERROR:
687     FPRINTF (stdout,
688              "%s",
689              _("Internal error scanning directory.\n"));
690     if (kill_task != NULL)
691     {
692       GNUNET_SCHEDULER_cancel (kill_task);
693       kill_task = NULL;
694     }
695     kill_task = GNUNET_SCHEDULER_add_now (&stop_scanner_task, NULL);
696     break;
697   default:
698     GNUNET_assert (0);
699     break;
700   }
701   fflush (stdout);
702 }
703
704
705 /**
706  * Continuation proceeding with initialization after identity subsystem
707  * has been initialized.
708  *
709  * @param args0 filename to publish
710  */
711 static void
712 identity_continuation (const char *args0)
713 {
714   char *ex;
715   char *emsg;
716
717   if ( (NULL != pseudonym) &&
718        (NULL == namespace) )
719   {
720     FPRINTF (stderr,
721              _("Selected pseudonym `%s' unknown\n"),
722              pseudonym);
723     GNUNET_SCHEDULER_shutdown ();
724     return;
725   }
726   if (NULL != uri_string)
727   {
728     emsg = NULL;
729     if (NULL == (uri = GNUNET_FS_uri_parse (uri_string, &emsg)))
730     {
731       FPRINTF (stderr,
732                _("Failed to parse URI: %s\n"),
733                emsg);
734       GNUNET_free (emsg);
735       GNUNET_SCHEDULER_shutdown ();
736       ret = 1;
737       return;
738     }
739     GNUNET_FS_publish_ksk (ctx, topKeywords,
740                            meta, uri,
741                            &bo,
742                            GNUNET_FS_PUBLISH_OPTION_NONE,
743                            &uri_ksk_continuation,
744                            NULL);
745     return;
746   }
747   if (GNUNET_OK !=
748       GNUNET_CONFIGURATION_get_value_string (cfg, "FS", "EXTRACTORS", &ex))
749     ex = NULL;
750   if (0 != ACCESS (args0, R_OK))
751   {
752     FPRINTF (stderr,
753              _("Failed to access `%s': %s\n"),
754              args0,
755              STRERROR (errno));
756     GNUNET_free_non_null (ex);
757     return;
758   }
759   ds = GNUNET_FS_directory_scan_start (args0,
760                                        disable_extractor,
761                                        ex,
762                                        &directory_scan_cb, NULL);
763   if (NULL == ds)
764   {
765     FPRINTF (stderr,
766              "%s",
767              _("Failed to start meta directory scanner.  Is gnunet-helper-publish-fs installed?\n"));
768     GNUNET_free_non_null (ex);
769     return;
770   }
771   GNUNET_free_non_null (ex);
772 }
773
774
775 /**
776  * Function called by identity service with known pseudonyms.
777  *
778  * @param cls closure with 'const char *' of filename to publish
779  * @param ego ego handle
780  * @param ctx context for application to store data for this ego
781  *                 (during the lifetime of this process, initially NULL)
782  * @param name name assigned by the user for this ego,
783  *                   NULL if the user just deleted the ego and it
784  *                   must thus no longer be used
785  */
786 static void
787 identity_cb (void *cls,
788              struct GNUNET_IDENTITY_Ego *ego,
789              void **ctx,
790              const char *name)
791 {
792   const char *args0 = cls;
793
794   if (NULL == ego)
795   {
796     identity_continuation (args0);
797     return;
798   }
799   if (NULL == name)
800     return;
801   if (0 == strcmp (name, pseudonym))
802     namespace = ego;
803 }
804
805
806 /**
807  * Main function that will be run by the scheduler.
808  *
809  * @param cls closure
810  * @param args remaining command-line arguments
811  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
812  * @param c configuration
813  */
814 static void
815 run (void *cls,
816      char *const *args,
817      const char *cfgfile,
818      const struct GNUNET_CONFIGURATION_Handle *c)
819 {
820   /* check arguments */
821   if ((NULL != uri_string) && (extract_only))
822   {
823     printf (_("Cannot extract metadata from a URI!\n"));
824     ret = -1;
825     return;
826   }
827   if (((NULL == uri_string) || (extract_only)) &&
828       ((NULL == args[0]) || (NULL != args[1])))
829   {
830     printf (_("You must specify one and only one filename for insertion.\n"));
831     ret = -1;
832     return;
833   }
834   if ((NULL != uri_string) && (NULL != args[0]))
835   {
836     printf (_("You must NOT specify an URI and a filename.\n"));
837     ret = -1;
838     return;
839   }
840   if (NULL != pseudonym)
841   {
842     if (NULL == this_id)
843     {
844       FPRINTF (stderr, _("Option `%s' is required when using option `%s'.\n"),
845                "-t", "-P");
846       ret = -1;
847       return;
848     }
849   }
850   else
851   {                             /* ordinary insertion checks */
852     if (NULL != next_id)
853     {
854       FPRINTF (stderr,
855                _("Option `%s' makes no sense without option `%s'.\n"),
856                "-N", "-P");
857       ret = -1;
858       return;
859     }
860     if (NULL != this_id)
861     {
862       FPRINTF (stderr,
863                _("Option `%s' makes no sense without option `%s'.\n"),
864                "-t", "-P");
865       ret = -1;
866       return;
867     }
868   }
869   cfg = c;
870   ctx =
871       GNUNET_FS_start (cfg, "gnunet-publish", &progress_cb, NULL,
872                        GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
873   if (NULL == ctx)
874   {
875     FPRINTF (stderr,
876              _("Could not initialize `%s' subsystem.\n"),
877              "FS");
878     ret = 1;
879     return;
880   }
881   kill_task =
882     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
883                                   &do_stop_task,
884                                   NULL);
885   if (NULL != pseudonym)
886     identity = GNUNET_IDENTITY_connect (cfg,
887                                         &identity_cb,
888                                         args[0]);
889   else
890     identity_continuation (args[0]);
891 }
892
893
894 /**
895  * The main function to publish content to GNUnet.
896  *
897  * @param argc number of arguments from the command line
898  * @param argv command line arguments
899  * @return 0 ok, 1 on error
900  */
901 int
902 main (int argc, char *const *argv)
903 {
904   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
905     {'a', "anonymity", "LEVEL",
906      gettext_noop ("set the desired LEVEL of sender-anonymity"),
907      1, &GNUNET_GETOPT_set_uint, &bo.anonymity_level},
908     {'d', "disable-creation-time", NULL,
909      gettext_noop
910      ("disable adding the creation time to the metadata of the uploaded file"),
911      0, &GNUNET_GETOPT_set_one, &do_disable_creation_time},
912     {'D', "disable-extractor", NULL,
913      gettext_noop ("do not use libextractor to add keywords or metadata"),
914      0, &GNUNET_GETOPT_set_one, &disable_extractor},
915     {'e', "extract", NULL,
916      gettext_noop
917      ("print list of extracted keywords that would be used, but do not perform upload"),
918      0, &GNUNET_GETOPT_set_one, &extract_only},
919     {'k', "key", "KEYWORD",
920      gettext_noop
921      ("add an additional keyword for the top-level file or directory"
922       " (this option can be specified multiple times)"),
923      1, &GNUNET_FS_getopt_set_keywords, &topKeywords},
924     {'m', "meta", "TYPE:VALUE",
925      gettext_noop ("set the meta-data for the given TYPE to the given VALUE"),
926      1, &GNUNET_FS_getopt_set_metadata, &meta},
927     {'n', "noindex", NULL,
928      gettext_noop ("do not index, perform full insertion (stores entire "
929                    "file in encrypted form in GNUnet database)"),
930      0, &GNUNET_GETOPT_set_one, &do_insert},
931     {'N', "next", "ID",
932      gettext_noop
933      ("specify ID of an updated version to be published in the future"
934       " (for namespace insertions only)"),
935      1, &GNUNET_GETOPT_set_string, &next_id},
936     {'p', "priority", "PRIORITY",
937      gettext_noop ("specify the priority of the content"),
938      1, &GNUNET_GETOPT_set_uint, &bo.content_priority},
939     {'P', "pseudonym", "NAME",
940      gettext_noop
941      ("publish the files under the pseudonym NAME (place file into namespace)"),
942      1, &GNUNET_GETOPT_set_string, &pseudonym},
943     {'r', "replication", "LEVEL",
944      gettext_noop ("set the desired replication LEVEL"),
945      1, &GNUNET_GETOPT_set_uint, &bo.replication_level},
946     {'s', "simulate-only", NULL,
947      gettext_noop ("only simulate the process but do not do any "
948                    "actual publishing (useful to compute URIs)"),
949      0, &GNUNET_GETOPT_set_one, &do_simulate},
950     {'t', "this", "ID",
951      gettext_noop ("set the ID of this version of the publication"
952                    " (for namespace insertions only)"),
953      1, &GNUNET_GETOPT_set_string, &this_id},
954     {'u', "uri", "URI",
955      gettext_noop ("URI to be published (can be used instead of passing a "
956                    "file to add keywords to the file with the respective URI)"),
957      1, &GNUNET_GETOPT_set_string, &uri_string},
958     {'V', "verbose", NULL,
959      gettext_noop ("be verbose (print progress information)"),
960      0, &GNUNET_GETOPT_set_one, &verbose},
961     GNUNET_GETOPT_OPTION_END
962   };
963   bo.expiration_time =
964       GNUNET_TIME_year_to_time (GNUNET_TIME_get_current_year () + 2);
965
966   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
967     return 2;
968   ret = (GNUNET_OK ==
969          GNUNET_PROGRAM_run (argc, argv, "gnunet-publish [OPTIONS] FILENAME",
970                              gettext_noop
971                              ("Publish a file or directory on GNUnet"),
972                              options, &run, NULL)) ? ret : 1;
973   GNUNET_free ((void*) argv);
974   return ret;
975 }
976
977 /* end of gnunet-publish.c */