flush peer respect value on disconnect
[oweals/gnunet.git] / src / fs / gnunet-auto-share.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001--2012 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-auto-share.c
22  * @brief automatically publish files on GNUnet
23  * @author Christian Grothoff
24  *
25  * TODO:
26  * - support loading meta data / keywords from resource file
27  * - add stability timer (a la buildbot)
28  */
29 #include "platform.h"
30 #include "gnunet_util_lib.h"
31
32 #define MIN_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
33
34 #define MAX_FREQUENCY GNUNET_TIME_UNIT_MINUTES
35
36
37 /**
38  * Item in our work queue (or in the set of files/directories
39  * we have successfully published).
40  */
41 struct WorkItem
42 {
43
44   /**
45    * PENDING Work is kept in a linked list.
46    */
47   struct WorkItem *prev;
48
49   /**
50    * PENDING Work is kept in a linked list.
51    */
52   struct WorkItem *next;
53
54   /**
55    * Filename of the work item.
56    */
57   char *filename;
58
59   /**
60    * Unique identity for this work item (used to detect
61    * if we need to do the work again).
62    */
63   struct GNUNET_HashCode id;
64 };
65
66
67 /**
68  * Global return value from 'main'.
69  */
70 static int ret;
71
72 /**
73  * Are we running 'verbosely'?
74  */
75 static int verbose;
76
77 /**
78  * Configuration to use.
79  */
80 static const struct GNUNET_CONFIGURATION_Handle *cfg;
81
82 /**
83  * Name of the configuration file.
84  */
85 static char *cfg_filename;
86
87 /**
88  * Disable extractor option to use for publishing.
89  */
90 static int disable_extractor;
91
92 /**
93  * Disable creation time option to use for publishing.
94  */
95 static int do_disable_creation_time;
96
97 /**
98  * Handle for the 'shutdown' task.
99  */
100 static GNUNET_SCHEDULER_TaskIdentifier kill_task;
101
102 /**
103  * Handle for the main task that does scanning and working.
104  */
105 static GNUNET_SCHEDULER_TaskIdentifier run_task;
106
107 /**
108  * Anonymity level option to use for publishing.
109  */
110 static unsigned int anonymity_level = 1;
111
112 /**
113  * Content priority option to use for publishing.
114  */
115 static unsigned int content_priority = 365;
116
117 /**
118  * Replication level option to use for publishing.
119  */
120 static unsigned int replication_level = 1;
121
122 /**
123  * Top-level directory we monitor to auto-publish.
124  */
125 static const char *dir_name;
126
127 /**
128  * Head of linked list of files still to publish.
129  */
130 static struct WorkItem *work_head;
131
132 /**
133  * Tail of linked list of files still to publish.
134  */
135 static struct WorkItem *work_tail;
136
137 /**
138  * Map from the hash of the filename (!) to a 'struct WorkItem'
139  * that was finished.
140  */
141 static struct GNUNET_CONTAINER_MultiHashMap *work_finished;
142
143 /**
144  * Set to GNUNET_YES if we are shutting down.
145  */
146 static int do_shutdown;
147
148 /**
149  * Start time of the current round; used to determine how long
150  * one iteration takes (which influences how fast we schedule
151  * the next one).
152  */
153 static struct GNUNET_TIME_Absolute start_time;
154
155 /**
156  * Pipe used to communicate 'gnunet-publish' completion (SIGCHLD) via signal.
157  */
158 static struct GNUNET_DISK_PipeHandle *sigpipe;
159
160 /**
161  * Handle to the 'gnunet-publish' process that we executed.
162  */
163 static struct GNUNET_OS_Process *publish_proc;
164
165
166 /**
167  * Compute the name of the state database file we will use.
168  */
169 static char *
170 get_state_file ()
171 {
172   char *ret;
173
174   GNUNET_asprintf (&ret,
175                    "%s%s.auto-share",
176                    dir_name,
177                    (DIR_SEPARATOR == dir_name[strlen(dir_name)-1]) ? "" : DIR_SEPARATOR_STR);
178   return ret;
179 }
180
181
182 /**
183  * Load the set of 'work_finished' items from disk.
184  */
185 static void
186 load_state ()
187 {
188   char *fn;
189   struct GNUNET_BIO_ReadHandle *rh;
190   uint32_t n;
191   struct GNUNET_HashCode id;
192   struct WorkItem *wi;
193   char *emsg;
194
195   emsg = NULL;
196   fn = get_state_file ();
197   rh = GNUNET_BIO_read_open (fn);
198   GNUNET_free (fn);
199   if (NULL == rh)
200     return;
201   fn = NULL;
202   if (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &n))
203     goto error;
204   while (n-- > 0)
205   {
206     if ( (GNUNET_OK !=
207           GNUNET_BIO_read_string (rh, "filename", &fn, 1024)) ||
208          (GNUNET_OK !=
209           GNUNET_BIO_read (rh, "id", &id, sizeof (struct GNUNET_HashCode))) )
210       goto error;
211     wi = GNUNET_new (struct WorkItem);
212     wi->id = id;
213     wi->filename = fn;
214     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
215                 "Loaded serialization ID for `%s' is `%s'\n",
216                 wi->filename,
217                 GNUNET_h2s (&id));
218     fn = NULL;
219     GNUNET_CRYPTO_hash (wi->filename,
220                         strlen (wi->filename),
221                         &id);
222     GNUNET_break (GNUNET_OK ==
223                   GNUNET_CONTAINER_multihashmap_put (work_finished,
224                                                      &id,
225                                                      wi,
226                                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
227   }
228   if (GNUNET_OK ==
229       GNUNET_BIO_read_close (rh, &emsg))
230     return;
231   rh = NULL;
232  error:
233   GNUNET_free_non_null (fn);
234   if (NULL != rh)
235     (void) GNUNET_BIO_read_close (rh, &emsg);
236   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
237               _("Failed to load state: %s\n"),
238               emsg);
239   GNUNET_free_non_null (emsg);
240 }
241
242
243 /**
244  * Write work item from the work_finished map to the given write handle.
245  *
246  * @param cls the 'struct GNUNET_BIO_WriteHandle*'
247  * @param key key of the item in the map (unused)
248  * @param value the 'struct WorkItem' to write
249  * @return GNUNET_OK to continue to iterate (if write worked)
250  */
251 static int
252 write_item (void *cls,
253             const struct GNUNET_HashCode *key,
254             void *value)
255 {
256   struct GNUNET_BIO_WriteHandle *wh = cls;
257   struct WorkItem *wi = value;
258
259   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
260               "Saving serialization ID of file `%s' with value `%s'\n",
261               wi->filename,
262               GNUNET_h2s (&wi->id));
263   if ( (GNUNET_OK !=
264         GNUNET_BIO_write_string (wh, wi->filename)) ||
265        (GNUNET_OK !=
266         GNUNET_BIO_write (wh,
267                           &wi->id,
268                           sizeof (struct GNUNET_HashCode))) )
269     return GNUNET_SYSERR; /* write error, abort iteration */
270   return GNUNET_OK;
271 }
272
273
274 /**
275  * Save the set of 'work_finished' items on disk.
276  */
277 static void
278 save_state ()
279 {
280   uint32_t n;
281   struct GNUNET_BIO_WriteHandle *wh;
282   char *fn;
283
284   n = GNUNET_CONTAINER_multihashmap_size (work_finished);
285   fn = get_state_file ();
286   wh = GNUNET_BIO_write_open (fn);
287   if (NULL == wh)
288   {
289     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
290                 _("Failed to save state to file %s\n"),
291                 fn);
292     GNUNET_free (fn);
293     return;
294   }
295   if (GNUNET_OK !=
296       GNUNET_BIO_write_int32 (wh, n))
297   {
298     (void) GNUNET_BIO_write_close (wh);
299     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
300                 _("Failed to save state to file %s\n"),
301                 fn);
302     GNUNET_free (fn);
303     return;
304   }
305   (void) GNUNET_CONTAINER_multihashmap_iterate (work_finished,
306                                                 &write_item,
307                                                 wh);
308   if (GNUNET_OK != GNUNET_BIO_write_close (wh))
309     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
310                 _("Failed to save state to file %s\n"),
311                 fn);
312   GNUNET_free (fn);
313 }
314
315
316 /**
317  * Task run on shutdown.  Serializes our current state to disk.
318  *
319  * @param cls closure, unused
320  * @param tc scheduler context, unused
321  */
322 static void
323 do_stop_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
324 {
325   kill_task = GNUNET_SCHEDULER_NO_TASK;
326   do_shutdown = GNUNET_YES;
327   if (NULL != publish_proc)
328   {
329     GNUNET_OS_process_kill (publish_proc, SIGKILL);
330     return;
331   }
332   if (GNUNET_SCHEDULER_NO_TASK != run_task)
333   {
334     GNUNET_SCHEDULER_cancel (run_task);
335     run_task = GNUNET_SCHEDULER_NO_TASK;
336   }
337 }
338
339
340 /**
341  * Decide what the next task is (working or scanning) and schedule it.
342  */
343 static void
344 schedule_next_task (void);
345
346
347 /**
348  * Task triggered whenever we receive a SIGCHLD (child
349  * process died).
350  *
351  * @param cls the 'struct WorkItem' we were working on
352  * @param tc context
353  */
354 static void
355 maint_child_death (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
356 {
357   struct WorkItem *wi = cls;
358   struct GNUNET_HashCode key;
359   enum GNUNET_OS_ProcessStatusType type;
360   unsigned long code;
361   int ret;
362   char c;
363   const struct GNUNET_DISK_FileHandle *pr;
364
365
366   run_task = GNUNET_SCHEDULER_NO_TASK;
367   pr = GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ);
368   if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
369   {
370     /* shutdown scheduled us, ignore! */
371     run_task =
372       GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
373                                       pr, &maint_child_death, wi);
374     return;
375   }
376
377   ret = GNUNET_OS_process_status (publish_proc,
378                                   &type,
379                                   &code);
380   GNUNET_assert (GNUNET_SYSERR != ret);
381   if (GNUNET_NO == ret)
382   {
383     GNUNET_break (0);
384     GNUNET_OS_process_kill (publish_proc, SIGKILL);
385     type = GNUNET_OS_PROCESS_SIGNALED;
386   }
387   GNUNET_OS_process_destroy (publish_proc);
388   publish_proc = NULL;
389   /* consume the signal */
390   GNUNET_break (0 < GNUNET_DISK_file_read (pr, &c, sizeof (c)));
391
392   if (GNUNET_YES == do_shutdown)
393   {
394     GNUNET_free (wi->filename);
395     GNUNET_free (wi);
396     return;
397   }
398   if ( (GNUNET_OS_PROCESS_EXITED == type) &&
399        (0 == code) )
400   {
401     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
402                 _("Publication of `%s' done\n"),
403                 wi->filename);
404     GNUNET_CRYPTO_hash (wi->filename,
405                         strlen (wi->filename),
406                         &key);
407     GNUNET_break (GNUNET_OK ==
408                   GNUNET_CONTAINER_multihashmap_put (work_finished,
409                                                      &key,
410                                                      wi,
411                                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
412   }
413   else
414   {
415     GNUNET_CONTAINER_DLL_insert_tail (work_head,
416                                       work_tail,
417                                       wi);
418   }
419   save_state ();
420   schedule_next_task ();
421 }
422
423
424 /**
425  * Signal handler called for SIGCHLD.  Triggers the
426  * respective handler by writing to the trigger pipe.
427  */
428 static void
429 sighandler_child_death ()
430 {
431   static char c;
432   int old_errno = errno;        /* back-up errno */
433
434   GNUNET_break (1 ==
435                 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
436                                         (sigpipe, GNUNET_DISK_PIPE_END_WRITE),
437                                         &c, sizeof (c)));
438   errno = old_errno;            /* restore errno */
439 }
440
441
442 /**
443  * Function called to process work items.
444  *
445  * @param cls closure, NULL
446  * @param tc scheduler context (unused)
447  */
448 static void
449 work (void *cls,
450       const struct GNUNET_SCHEDULER_TaskContext *tc)
451 {
452   static char *argv[14];
453   static char anon_level[20];
454   static char content_prio[20];
455   static char repl_level[20];
456   struct WorkItem *wi;
457   const struct GNUNET_DISK_FileHandle *pr;
458   int argc;
459
460   run_task = GNUNET_SCHEDULER_NO_TASK;
461   wi = work_head;
462   GNUNET_CONTAINER_DLL_remove (work_head,
463                                work_tail,
464                                wi);
465   argc = 0;
466   argv[argc++] = "gnunet-publish";
467   if (verbose)
468     argv[argc++] = "-V";
469   if (disable_extractor)
470     argv[argc++] = "-D";
471   if (do_disable_creation_time)
472     argv[argc++] = "-d";
473   argv[argc++] = "-c";
474   argv[argc++] = cfg_filename;
475   GNUNET_snprintf (anon_level, sizeof (anon_level),
476                    "%u", anonymity_level);
477   argv[argc++] = "-a";
478   argv[argc++] = anon_level;
479   GNUNET_snprintf (content_prio, sizeof (content_prio),
480                    "%u", content_priority);
481   argv[argc++] = "-p";
482   argv[argc++] = content_prio;
483   GNUNET_snprintf (repl_level, sizeof (repl_level),
484                    "%u", replication_level);
485   argv[argc++] = "-r";
486   argv[argc++] = repl_level;
487   argv[argc++] = wi->filename;
488   argv[argc] = NULL;
489   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
490               _("Publishing `%s'\n"),
491               wi->filename);
492   publish_proc = GNUNET_OS_start_process_vap (GNUNET_YES,
493                                               0, NULL, NULL, NULL,
494                                               "gnunet-publish",
495                                               argv);
496   if (NULL == publish_proc)
497   {
498     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
499                 _("Failed to run `%s'\n"),
500                 "gnunet-publish");
501     GNUNET_CONTAINER_DLL_insert (work_head,
502                                  work_tail,
503                                  wi);
504     run_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
505                                              &work,
506                                              NULL);
507     return;
508   }
509   pr = GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ);
510   run_task =
511     GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
512                                     pr, &maint_child_death, wi);
513 }
514
515
516 /**
517  * Recursively scan the given file/directory structure to determine
518  * a unique ID that represents the current state of the hierarchy.
519  *
520  * @param cls where to store the unique ID we are computing
521  * @param filename file to scan
522  * @return GNUNET_OK (always)
523  */
524 static int
525 determine_id (void *cls,
526               const char *filename)
527 {
528   struct GNUNET_HashCode *id = cls;
529   struct stat sbuf;
530   struct GNUNET_HashCode fx[2];
531   struct GNUNET_HashCode ft;
532
533   if (0 != STAT (filename, &sbuf))
534   {
535     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
536     return GNUNET_OK;
537   }
538   GNUNET_CRYPTO_hash (filename, strlen (filename), &fx[0]);
539   if (!S_ISDIR (sbuf.st_mode))
540   {
541     uint64_t fattr[2];
542
543     fattr[0] = GNUNET_htonll (sbuf.st_size);
544     fattr[0] = GNUNET_htonll (sbuf.st_mtime);
545
546     GNUNET_CRYPTO_hash (fattr, sizeof (fattr), &fx[1]);
547   }
548   else
549   {
550     memset (&fx[1], 1, sizeof (struct GNUNET_HashCode));
551     GNUNET_DISK_directory_scan (filename,
552                                 &determine_id,
553                                 &fx[1]);
554   }
555   /* use hash here to make hierarchical structure distinct from
556      all files on the same level */
557   GNUNET_CRYPTO_hash (fx, sizeof (fx), &ft);
558   /* use XOR here so that order of the files in the directory
559      does not matter! */
560   GNUNET_CRYPTO_hash_xor (&ft, id, id);
561   return GNUNET_OK;
562 }
563
564
565 /**
566  * Function called with a filename (or directory name) to publish
567  * (if it has changed since the last time we published it).  This function
568  * is called for the top-level files only.
569  *
570  * @param cls closure, NULL
571  * @param filename complete filename (absolute path)
572  * @return GNUNET_OK to continue to iterate, GNUNET_SYSERR during shutdown
573  */
574 static int
575 add_file (void *cls,
576           const char *filename)
577 {
578   struct WorkItem *wi;
579   struct GNUNET_HashCode key;
580   struct GNUNET_HashCode id;
581
582   if (GNUNET_YES == do_shutdown)
583     return GNUNET_SYSERR;
584   if ( (NULL != strstr (filename,
585                       "/.auto-share")) ||
586        (NULL != strstr (filename,
587                         "\\.auto-share")) )
588     return GNUNET_OK; /* skip internal file */
589   GNUNET_CRYPTO_hash (filename,
590                       strlen (filename),
591                       &key);
592   wi = GNUNET_CONTAINER_multihashmap_get (work_finished,
593                                           &key);
594   memset (&id, 0, sizeof (struct GNUNET_HashCode));
595   determine_id (&id, filename);
596   if (NULL != wi)
597   {
598     if (0 == memcmp (&id,
599                      &wi->id,
600                      sizeof (struct GNUNET_HashCode)))
601       return GNUNET_OK; /* skip: we did this one already */
602     /* contents changed, need to re-do the directory... */
603     GNUNET_assert (GNUNET_YES ==
604                    GNUNET_CONTAINER_multihashmap_remove (work_finished,
605                                                          &key,
606                                                          wi));
607   }
608   else
609   {
610     wi = GNUNET_new (struct WorkItem);
611     wi->filename = GNUNET_strdup (filename);
612   }
613   wi->id = id;
614   GNUNET_CONTAINER_DLL_insert (work_head,
615                                work_tail,
616                                wi);
617   if (GNUNET_YES == do_shutdown)
618     return GNUNET_SYSERR;
619   return GNUNET_OK;
620 }
621
622
623 /**
624  * Periodically run task to update our view of the directory to share.
625  *
626  * @param cls NULL
627  * @param tc scheduler context, unused
628  */
629 static void
630 scan (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
631 {
632   run_task = GNUNET_SCHEDULER_NO_TASK;
633   start_time = GNUNET_TIME_absolute_get ();
634   (void) GNUNET_DISK_directory_scan (dir_name,
635                                      &add_file,
636                                      NULL);
637   schedule_next_task ();
638 }
639
640
641 /**
642  * Decide what the next task is (working or scanning) and schedule it.
643  */
644 static void
645 schedule_next_task ()
646 {
647   struct GNUNET_TIME_Relative delay;
648
649   if (GNUNET_YES == do_shutdown)
650     return;
651   if (NULL == work_head)
652   {
653     /* delay by at most 4h, at least 1s, and otherwise in between depending
654        on how long it took to scan */
655     delay = GNUNET_TIME_absolute_get_duration (start_time);
656     delay = GNUNET_TIME_relative_min (MIN_FREQUENCY,
657                                       GNUNET_TIME_relative_multiply (delay,
658                                                                      100));
659     delay = GNUNET_TIME_relative_max (delay,
660                                       MAX_FREQUENCY);
661     run_task = GNUNET_SCHEDULER_add_delayed (delay,
662                                              &scan,
663                                              NULL);
664   }
665   else
666   {
667     run_task = GNUNET_SCHEDULER_add_now (&work, NULL);
668   }
669 }
670
671
672 /**
673  * Main function that will be run by the scheduler.
674  *
675  * @param cls closure
676  * @param args remaining command-line arguments
677  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
678  * @param c configuration
679  */
680 static void
681 run (void *cls, char *const *args, const char *cfgfile,
682      const struct GNUNET_CONFIGURATION_Handle *c)
683 {
684   /* check arguments */
685   if ((args[0] == NULL) || (args[1] != NULL) ||
686       (GNUNET_YES != GNUNET_DISK_directory_test (args[0], GNUNET_YES)))
687   {
688     printf (_("You must specify one and only one directory name for automatic publication.\n"));
689     ret = -1;
690     return;
691   }
692   cfg_filename = GNUNET_strdup (cfgfile);
693   cfg = c;
694   dir_name = args[0];
695   work_finished = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_NO);
696   load_state ();
697   run_task = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
698                                                  &scan, NULL);
699
700   kill_task =
701       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_stop_task,
702                                     NULL);
703 }
704
705
706 /**
707  * Free memory associated with the work item from the work_finished map.
708  *
709  * @param cls NULL (unused)
710  * @param key key of the item in the map (unused)
711  * @param value the 'struct WorkItem' to free
712  * @return GNUNET_OK to continue to iterate
713  */
714 static int
715 free_item (void *cls,
716            const struct GNUNET_HashCode *key,
717            void *value)
718 {
719   struct WorkItem *wi = value;
720
721   GNUNET_free (wi->filename);
722   GNUNET_free (wi);
723   return GNUNET_OK;
724 }
725
726
727 /**
728  * The main function to automatically publish content to GNUnet.
729  *
730  * @param argc number of arguments from the command line
731  * @param argv command line arguments
732  * @return 0 ok, 1 on error
733  */
734 int
735 main (int argc, char *const *argv)
736 {
737   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
738     {'a', "anonymity", "LEVEL",
739      gettext_noop ("set the desired LEVEL of sender-anonymity"),
740      1, &GNUNET_GETOPT_set_uint, &anonymity_level},
741     {'d', "disable-creation-time", NULL,
742      gettext_noop
743      ("disable adding the creation time to the metadata of the uploaded file"),
744      0, &GNUNET_GETOPT_set_one, &do_disable_creation_time},
745     {'D', "disable-extractor", NULL,
746      gettext_noop ("do not use libextractor to add keywords or metadata"),
747      0, &GNUNET_GETOPT_set_one, &disable_extractor},
748     {'p', "priority", "PRIORITY",
749      gettext_noop ("specify the priority of the content"),
750      1, &GNUNET_GETOPT_set_uint, &content_priority},
751     {'r', "replication", "LEVEL",
752      gettext_noop ("set the desired replication LEVEL"),
753      1, &GNUNET_GETOPT_set_uint, &replication_level},
754     {'V', "verbose", NULL,
755      gettext_noop ("be verbose (print progress information)"),
756      0, &GNUNET_GETOPT_set_one, &verbose},
757     GNUNET_GETOPT_OPTION_END
758   };
759   struct WorkItem *wi;
760   int ok;
761   struct GNUNET_SIGNAL_Context *shc_chld;
762
763   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
764     return 2;
765   sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO);
766   GNUNET_assert (sigpipe != NULL);
767   shc_chld =
768     GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death);
769   ok = (GNUNET_OK ==
770         GNUNET_PROGRAM_run (argc, argv, "gnunet-auto-share [OPTIONS] FILENAME",
771                             gettext_noop
772                             ("Automatically publish files from a directory on GNUnet"),
773                             options, &run, NULL)) ? ret : 1;
774   if (NULL != work_finished)
775   {
776     (void) GNUNET_CONTAINER_multihashmap_iterate (work_finished,
777                                                   &free_item,
778                                                   NULL);
779     GNUNET_CONTAINER_multihashmap_destroy (work_finished);
780   }
781   while (NULL != (wi = work_head))
782   {
783     GNUNET_CONTAINER_DLL_remove (work_head, work_tail, wi);
784     GNUNET_free (wi->filename);
785     GNUNET_free (wi);
786   }
787   GNUNET_SIGNAL_handler_uninstall (shc_chld);
788   shc_chld = NULL;
789   GNUNET_DISK_pipe_close (sigpipe);
790   sigpipe = NULL;
791   GNUNET_free (cfg_filename);
792   cfg_filename = NULL;
793   GNUNET_free ((void*) argv);
794   return ok;
795 }
796
797 /* end of gnunet-auto-share.c */