2 This file is part of GNUnet.
3 Copyright (C) 2001--2012 Christian Grothoff (and other contributing authors)
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.
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.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file fs/gnunet-auto-share.c
22 * @brief automatically publish files on GNUnet
23 * @author Christian Grothoff
26 * - support loading meta data / keywords from resource file
27 * - add stability timer (a la buildbot)
30 #include "gnunet_util_lib.h"
32 #define MIN_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
34 #define MAX_FREQUENCY GNUNET_TIME_UNIT_MINUTES
38 * Item in our work queue (or in the set of files/directories
39 * we have successfully published).
45 * PENDING Work is kept in a linked list.
47 struct WorkItem *prev;
50 * PENDING Work is kept in a linked list.
52 struct WorkItem *next;
55 * Filename of the work item.
60 * Unique identity for this work item (used to detect
61 * if we need to do the work again).
63 struct GNUNET_HashCode id;
68 * Global return value from 'main'.
73 * Are we running 'verbosely'?
78 * Configuration to use.
80 static const struct GNUNET_CONFIGURATION_Handle *cfg;
83 * Name of the configuration file.
85 static char *cfg_filename;
88 * Disable extractor option to use for publishing.
90 static int disable_extractor;
93 * Disable creation time option to use for publishing.
95 static int do_disable_creation_time;
98 * Handle for the 'shutdown' task.
100 static struct GNUNET_SCHEDULER_Task *kill_task;
103 * Handle for the main task that does scanning and working.
105 static struct GNUNET_SCHEDULER_Task *run_task;
108 * Anonymity level option to use for publishing.
110 static unsigned int anonymity_level = 1;
113 * Content priority option to use for publishing.
115 static unsigned int content_priority = 365;
118 * Replication level option to use for publishing.
120 static unsigned int replication_level = 1;
123 * Top-level directory we monitor to auto-publish.
125 static const char *dir_name;
128 * Head of linked list of files still to publish.
130 static struct WorkItem *work_head;
133 * Tail of linked list of files still to publish.
135 static struct WorkItem *work_tail;
138 * Map from the hash of the filename (!) to a `struct WorkItem`
141 static struct GNUNET_CONTAINER_MultiHashMap *work_finished;
144 * Set to #GNUNET_YES if we are shutting down.
146 static int do_shutdown;
149 * Start time of the current round; used to determine how long
150 * one iteration takes (which influences how fast we schedule
153 static struct GNUNET_TIME_Absolute start_time;
156 * Pipe used to communicate 'gnunet-publish' completion (SIGCHLD) via signal.
158 static struct GNUNET_DISK_PipeHandle *sigpipe;
161 * Handle to the 'gnunet-publish' process that we executed.
163 static struct GNUNET_OS_Process *publish_proc;
167 * Compute the name of the state database file we will use.
174 GNUNET_asprintf (&ret,
177 (DIR_SEPARATOR == dir_name[strlen(dir_name)-1]) ? "" : DIR_SEPARATOR_STR);
183 * Load the set of #work_finished items from disk.
189 struct GNUNET_BIO_ReadHandle *rh;
191 struct GNUNET_HashCode id;
196 fn = get_state_file ();
197 rh = GNUNET_BIO_read_open (fn);
202 if (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &n))
207 GNUNET_BIO_read_string (rh, "filename", &fn, 1024)) ||
209 GNUNET_BIO_read (rh, "id", &id, sizeof (struct GNUNET_HashCode))) )
211 wi = GNUNET_new (struct WorkItem);
214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
215 "Loaded serialization ID for `%s' is `%s'\n",
219 GNUNET_CRYPTO_hash (wi->filename,
220 strlen (wi->filename),
222 GNUNET_break (GNUNET_OK ==
223 GNUNET_CONTAINER_multihashmap_put (work_finished,
226 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
229 GNUNET_BIO_read_close (rh, &emsg))
233 GNUNET_free_non_null (fn);
235 (void) GNUNET_BIO_read_close (rh, &emsg);
236 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
237 _("Failed to load state: %s\n"),
239 GNUNET_free_non_null (emsg);
244 * Write work item from the #work_finished map to the given write handle.
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)
252 write_item (void *cls,
253 const struct GNUNET_HashCode *key,
256 struct GNUNET_BIO_WriteHandle *wh = cls;
257 struct WorkItem *wi = value;
259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
260 "Saving serialization ID of file `%s' with value `%s'\n",
262 GNUNET_h2s (&wi->id));
264 GNUNET_BIO_write_string (wh, wi->filename)) ||
266 GNUNET_BIO_write (wh,
268 sizeof (struct GNUNET_HashCode))) )
269 return GNUNET_SYSERR; /* write error, abort iteration */
275 * Save the set of #work_finished items on disk.
281 struct GNUNET_BIO_WriteHandle *wh;
284 n = GNUNET_CONTAINER_multihashmap_size (work_finished);
285 fn = get_state_file ();
286 wh = GNUNET_BIO_write_open (fn);
289 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
290 _("Failed to save state to file %s\n"),
296 GNUNET_BIO_write_int32 (wh, n))
298 (void) GNUNET_BIO_write_close (wh);
299 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
300 _("Failed to save state to file %s\n"),
305 (void) GNUNET_CONTAINER_multihashmap_iterate (work_finished,
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"),
317 * Task run on shutdown. Serializes our current state to disk.
319 * @param cls closure, unused
320 * @param tc scheduler context, unused
323 do_stop_task (void *cls,
324 const struct GNUNET_SCHEDULER_TaskContext *tc)
327 do_shutdown = GNUNET_YES;
328 if (NULL != publish_proc)
330 GNUNET_OS_process_kill (publish_proc,
334 if (NULL != run_task)
336 GNUNET_SCHEDULER_cancel (run_task);
343 * Decide what the next task is (working or scanning) and schedule it.
346 schedule_next_task (void);
350 * Task triggered whenever we receive a SIGCHLD (child
353 * @param cls the `struct WorkItem` we were working on
357 maint_child_death (void *cls,
358 const struct GNUNET_SCHEDULER_TaskContext *tc)
360 struct WorkItem *wi = cls;
361 struct GNUNET_HashCode key;
362 enum GNUNET_OS_ProcessStatusType type;
366 const struct GNUNET_DISK_FileHandle *pr;
369 pr = GNUNET_DISK_pipe_handle (sigpipe,
370 GNUNET_DISK_PIPE_END_READ);
371 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
373 /* shutdown scheduled us, someone else will kill child,
374 we should just try again */
376 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
378 &maint_child_death, wi);
381 /* consume the signal */
382 GNUNET_break (0 < GNUNET_DISK_file_read (pr, &c, sizeof (c)));
384 ret = GNUNET_OS_process_status (publish_proc,
387 GNUNET_assert (GNUNET_SYSERR != ret);
388 if (GNUNET_NO == ret)
390 /* process still running? Then where did the SIGCHLD come from?
391 Well, let's declare it spurious (kernel bug?) and keep rolling.
395 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
397 &maint_child_death, wi);
400 GNUNET_assert (GNUNET_OK == ret);
402 GNUNET_OS_process_destroy (publish_proc);
405 if (GNUNET_YES == do_shutdown)
407 GNUNET_free (wi->filename);
411 if ( (GNUNET_OS_PROCESS_EXITED == type) &&
414 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
415 _("Publication of `%s' done\n"),
417 GNUNET_CRYPTO_hash (wi->filename,
418 strlen (wi->filename),
420 GNUNET_break (GNUNET_OK ==
421 GNUNET_CONTAINER_multihashmap_put (work_finished,
424 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
428 GNUNET_CONTAINER_DLL_insert_tail (work_head,
433 schedule_next_task ();
438 * Signal handler called for SIGCHLD. Triggers the
439 * respective handler by writing to the trigger pipe.
442 sighandler_child_death ()
445 int old_errno = errno; /* back-up errno */
448 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
450 GNUNET_DISK_PIPE_END_WRITE),
452 errno = old_errno; /* restore errno */
457 * Function called to process work items.
459 * @param cls closure, NULL
460 * @param tc scheduler context (unused)
464 const struct GNUNET_SCHEDULER_TaskContext *tc)
466 static char *argv[14];
467 static char anon_level[20];
468 static char content_prio[20];
469 static char repl_level[20];
471 const struct GNUNET_DISK_FileHandle *pr;
476 GNUNET_CONTAINER_DLL_remove (work_head,
480 argv[argc++] = "gnunet-publish";
483 if (disable_extractor)
485 if (do_disable_creation_time)
488 argv[argc++] = cfg_filename;
489 GNUNET_snprintf (anon_level, sizeof (anon_level),
490 "%u", anonymity_level);
492 argv[argc++] = anon_level;
493 GNUNET_snprintf (content_prio, sizeof (content_prio),
494 "%u", content_priority);
496 argv[argc++] = content_prio;
497 GNUNET_snprintf (repl_level, sizeof (repl_level),
498 "%u", replication_level);
500 argv[argc++] = repl_level;
501 argv[argc++] = wi->filename;
503 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
504 _("Publishing `%s'\n"),
506 GNUNET_assert (NULL == publish_proc);
507 publish_proc = GNUNET_OS_start_process_vap (GNUNET_YES,
511 if (NULL == publish_proc)
513 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
514 _("Failed to run `%s'\n"),
516 GNUNET_CONTAINER_DLL_insert (work_head,
519 run_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
524 pr = GNUNET_DISK_pipe_handle (sigpipe,
525 GNUNET_DISK_PIPE_END_READ);
527 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
529 &maint_child_death, wi);
534 * Recursively scan the given file/directory structure to determine
535 * a unique ID that represents the current state of the hierarchy.
537 * @param cls where to store the unique ID we are computing
538 * @param filename file to scan
539 * @return #GNUNET_OK (always)
542 determine_id (void *cls,
543 const char *filename)
545 struct GNUNET_HashCode *id = cls;
547 struct GNUNET_HashCode fx[2];
548 struct GNUNET_HashCode ft;
550 if (0 != STAT (filename, &sbuf))
552 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
557 GNUNET_CRYPTO_hash (filename,
560 if (!S_ISDIR (sbuf.st_mode))
564 fattr[0] = GNUNET_htonll (sbuf.st_size);
565 fattr[0] = GNUNET_htonll (sbuf.st_mtime);
567 GNUNET_CRYPTO_hash (fattr,
575 sizeof (struct GNUNET_HashCode));
576 GNUNET_DISK_directory_scan (filename,
580 /* use hash here to make hierarchical structure distinct from
581 all files on the same level */
582 GNUNET_CRYPTO_hash (fx,
585 /* use XOR here so that order of the files in the directory
587 GNUNET_CRYPTO_hash_xor (&ft,
595 * Function called with a filename (or directory name) to publish
596 * (if it has changed since the last time we published it). This function
597 * is called for the top-level files only.
599 * @param cls closure, NULL
600 * @param filename complete filename (absolute path)
601 * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR during shutdown
605 const char *filename)
608 struct GNUNET_HashCode key;
609 struct GNUNET_HashCode id;
611 if (GNUNET_YES == do_shutdown)
612 return GNUNET_SYSERR;
613 if ( (NULL != strstr (filename,
615 (NULL != strstr (filename,
617 return GNUNET_OK; /* skip internal file */
618 GNUNET_CRYPTO_hash (filename,
621 wi = GNUNET_CONTAINER_multihashmap_get (work_finished,
623 memset (&id, 0, sizeof (struct GNUNET_HashCode));
624 determine_id (&id, filename);
627 if (0 == memcmp (&id,
629 sizeof (struct GNUNET_HashCode)))
630 return GNUNET_OK; /* skip: we did this one already */
631 /* contents changed, need to re-do the directory... */
632 GNUNET_assert (GNUNET_YES ==
633 GNUNET_CONTAINER_multihashmap_remove (work_finished,
639 wi = GNUNET_new (struct WorkItem);
640 wi->filename = GNUNET_strdup (filename);
643 GNUNET_CONTAINER_DLL_insert (work_head,
646 if (GNUNET_YES == do_shutdown)
647 return GNUNET_SYSERR;
653 * Periodically run task to update our view of the directory to share.
656 * @param tc scheduler context, unused
660 const struct GNUNET_SCHEDULER_TaskContext *tc)
663 start_time = GNUNET_TIME_absolute_get ();
664 (void) GNUNET_DISK_directory_scan (dir_name,
667 schedule_next_task ();
672 * Decide what the next task is (working or scanning) and schedule it.
675 schedule_next_task ()
677 struct GNUNET_TIME_Relative delay;
679 if (GNUNET_YES == do_shutdown)
681 GNUNET_assert (NULL == run_task);
682 if (NULL == work_head)
684 /* delay by at most 4h, at least 1s, and otherwise in between depending
685 on how long it took to scan */
686 delay = GNUNET_TIME_absolute_get_duration (start_time);
687 delay = GNUNET_TIME_relative_min (MIN_FREQUENCY,
688 GNUNET_TIME_relative_multiply (delay,
690 delay = GNUNET_TIME_relative_max (delay,
692 run_task = GNUNET_SCHEDULER_add_delayed (delay,
698 run_task = GNUNET_SCHEDULER_add_now (&work,
705 * Main function that will be run by the scheduler.
708 * @param args remaining command-line arguments
709 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
710 * @param c configuration
716 const struct GNUNET_CONFIGURATION_Handle *c)
718 /* check arguments */
719 if ( (NULL == args[0]) ||
722 GNUNET_DISK_directory_test (args[0],
725 printf (_("You must specify one and only one directory name for automatic publication.\n"));
729 cfg_filename = GNUNET_strdup (cfgfile);
732 work_finished = GNUNET_CONTAINER_multihashmap_create (1024,
735 run_task = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
739 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
746 * Free memory associated with the work item from the work_finished map.
748 * @param cls NULL (unused)
749 * @param key key of the item in the map (unused)
750 * @param value the `struct WorkItem` to free
751 * @return #GNUNET_OK to continue to iterate
754 free_item (void *cls,
755 const struct GNUNET_HashCode *key,
758 struct WorkItem *wi = value;
760 GNUNET_free (wi->filename);
767 * The main function to automatically publish content to GNUnet.
769 * @param argc number of arguments from the command line
770 * @param argv command line arguments
771 * @return 0 ok, 1 on error
774 main (int argc, char *const *argv)
776 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
777 {'a', "anonymity", "LEVEL",
778 gettext_noop ("set the desired LEVEL of sender-anonymity"),
779 1, &GNUNET_GETOPT_set_uint, &anonymity_level},
780 {'d', "disable-creation-time", NULL,
782 ("disable adding the creation time to the metadata of the uploaded file"),
783 0, &GNUNET_GETOPT_set_one, &do_disable_creation_time},
784 {'D', "disable-extractor", NULL,
785 gettext_noop ("do not use libextractor to add keywords or metadata"),
786 0, &GNUNET_GETOPT_set_one, &disable_extractor},
787 {'p', "priority", "PRIORITY",
788 gettext_noop ("specify the priority of the content"),
789 1, &GNUNET_GETOPT_set_uint, &content_priority},
790 {'r', "replication", "LEVEL",
791 gettext_noop ("set the desired replication LEVEL"),
792 1, &GNUNET_GETOPT_set_uint, &replication_level},
793 {'V', "verbose", NULL,
794 gettext_noop ("be verbose (print progress information)"),
795 0, &GNUNET_GETOPT_set_one, &verbose},
796 GNUNET_GETOPT_OPTION_END
800 struct GNUNET_SIGNAL_Context *shc_chld;
803 GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
805 sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO,
806 GNUNET_NO, GNUNET_NO);
807 GNUNET_assert (NULL != sigpipe);
809 GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD,
810 &sighandler_child_death);
812 GNUNET_PROGRAM_run (argc, argv,
813 "gnunet-auto-share [OPTIONS] FILENAME",
815 ("Automatically publish files from a directory on GNUnet"),
816 options, &run, NULL)) ? ret : 1;
817 if (NULL != work_finished)
819 (void) GNUNET_CONTAINER_multihashmap_iterate (work_finished,
822 GNUNET_CONTAINER_multihashmap_destroy (work_finished);
824 while (NULL != (wi = work_head))
826 GNUNET_CONTAINER_DLL_remove (work_head,
829 GNUNET_free (wi->filename);
832 GNUNET_SIGNAL_handler_uninstall (shc_chld);
834 GNUNET_DISK_pipe_close (sigpipe);
836 GNUNET_free (cfg_filename);
838 GNUNET_free ((void*) argv);
842 /* end of gnunet-auto-share.c */