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