2 This file is part of GNUnet.
3 Copyright (C) 2001--2012 GNUnet e.V.
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 MAX_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
34 #define MIN_DELAY 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 main task that does scanning and working.
100 static struct GNUNET_SCHEDULER_Task *run_task;
103 * Anonymity level option to use for publishing.
105 static unsigned int anonymity_level = 1;
108 * Content priority option to use for publishing.
110 static unsigned int content_priority = 365;
113 * Replication level option to use for publishing.
115 static unsigned int replication_level = 1;
118 * Top-level directory we monitor to auto-publish.
120 static const char *dir_name;
123 * Head of linked list of files still to publish.
125 static struct WorkItem *work_head;
128 * Tail of linked list of files still to publish.
130 static struct WorkItem *work_tail;
133 * Map from the hash of the filename (!) to a `struct WorkItem`
136 static struct GNUNET_CONTAINER_MultiHashMap *work_finished;
139 * Set to #GNUNET_YES if we are shutting down.
141 static int do_shutdown;
144 * Start time of the current round; used to determine how long
145 * one iteration takes (which influences how fast we schedule
148 static struct GNUNET_TIME_Absolute start_time;
151 * Pipe used to communicate 'gnunet-publish' completion (SIGCHLD) via signal.
153 static struct GNUNET_DISK_PipeHandle *sigpipe;
156 * Handle to the 'gnunet-publish' process that we executed.
158 static struct GNUNET_OS_Process *publish_proc;
162 * Compute the name of the state database file we will use.
169 GNUNET_asprintf (&ret,
172 (DIR_SEPARATOR == dir_name[strlen(dir_name)-1]) ? "" : DIR_SEPARATOR_STR);
178 * Load the set of #work_finished items from disk.
184 struct GNUNET_BIO_ReadHandle *rh;
186 struct GNUNET_HashCode id;
191 fn = get_state_file ();
192 rh = GNUNET_BIO_read_open (fn);
197 if (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &n))
202 GNUNET_BIO_read_string (rh, "filename", &fn, 1024)) ||
204 GNUNET_BIO_read (rh, "id", &id, sizeof (struct GNUNET_HashCode))) )
206 wi = GNUNET_new (struct WorkItem);
209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
210 "Loaded serialization ID for `%s' is `%s'\n",
214 GNUNET_CRYPTO_hash (wi->filename,
215 strlen (wi->filename),
217 GNUNET_break (GNUNET_OK ==
218 GNUNET_CONTAINER_multihashmap_put (work_finished,
221 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
224 GNUNET_BIO_read_close (rh, &emsg))
228 GNUNET_free_non_null (fn);
230 (void) GNUNET_BIO_read_close (rh, &emsg);
231 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
232 _("Failed to load state: %s\n"),
234 GNUNET_free_non_null (emsg);
239 * Write work item from the #work_finished map to the given write handle.
241 * @param cls the `struct GNUNET_BIO_WriteHandle *`
242 * @param key key of the item in the map (unused)
243 * @param value the `struct WorkItem` to write
244 * @return #GNUNET_OK to continue to iterate (if write worked)
247 write_item (void *cls,
248 const struct GNUNET_HashCode *key,
251 struct GNUNET_BIO_WriteHandle *wh = cls;
252 struct WorkItem *wi = value;
254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
255 "Saving serialization ID of file `%s' with value `%s'\n",
257 GNUNET_h2s (&wi->id));
259 GNUNET_BIO_write_string (wh, wi->filename)) ||
261 GNUNET_BIO_write (wh,
263 sizeof (struct GNUNET_HashCode))) )
264 return GNUNET_SYSERR; /* write error, abort iteration */
270 * Save the set of #work_finished items on disk.
276 struct GNUNET_BIO_WriteHandle *wh;
279 n = GNUNET_CONTAINER_multihashmap_size (work_finished);
280 fn = get_state_file ();
281 wh = GNUNET_BIO_write_open (fn);
284 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
285 _("Failed to save state to file %s\n"),
291 GNUNET_BIO_write_int32 (wh, n))
293 (void) GNUNET_BIO_write_close (wh);
294 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
295 _("Failed to save state to file %s\n"),
300 (void) GNUNET_CONTAINER_multihashmap_iterate (work_finished,
303 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
304 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
305 _("Failed to save state to file %s\n"),
312 * Task run on shutdown. Serializes our current state to disk.
314 * @param cls closure, unused
317 do_stop_task (void *cls)
319 do_shutdown = GNUNET_YES;
320 if (NULL != publish_proc)
322 GNUNET_OS_process_kill (publish_proc,
326 if (NULL != run_task)
328 GNUNET_SCHEDULER_cancel (run_task);
335 * Decide what the next task is (working or scanning) and schedule it.
338 schedule_next_task (void);
342 * Task triggered whenever we receive a SIGCHLD (child
345 * @param cls the `struct WorkItem` we were working on
348 maint_child_death (void *cls)
350 struct WorkItem *wi = cls;
351 struct GNUNET_HashCode key;
352 enum GNUNET_OS_ProcessStatusType type;
356 const struct GNUNET_DISK_FileHandle *pr;
357 const struct GNUNET_SCHEDULER_TaskContext *tc;
360 pr = GNUNET_DISK_pipe_handle (sigpipe,
361 GNUNET_DISK_PIPE_END_READ);
362 tc = GNUNET_SCHEDULER_get_task_context ();
363 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
365 /* shutdown scheduled us, someone else will kill child,
366 we should just try again */
368 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
370 &maint_child_death, wi);
373 /* consume the signal */
374 GNUNET_break (0 < GNUNET_DISK_file_read (pr, &c, sizeof (c)));
376 ret = GNUNET_OS_process_status (publish_proc,
379 GNUNET_assert (GNUNET_SYSERR != ret);
380 if (GNUNET_NO == ret)
382 /* process still running? Then where did the SIGCHLD come from?
383 Well, let's declare it spurious (kernel bug?) and keep rolling.
387 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
389 &maint_child_death, wi);
392 GNUNET_assert (GNUNET_OK == ret);
394 GNUNET_OS_process_destroy (publish_proc);
397 if (GNUNET_YES == do_shutdown)
399 GNUNET_free (wi->filename);
403 if ( (GNUNET_OS_PROCESS_EXITED == type) &&
406 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
407 _("Publication of `%s' done\n"),
409 GNUNET_CRYPTO_hash (wi->filename,
410 strlen (wi->filename),
412 GNUNET_break (GNUNET_OK ==
413 GNUNET_CONTAINER_multihashmap_put (work_finished,
416 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
420 GNUNET_CONTAINER_DLL_insert_tail (work_head,
425 schedule_next_task ();
430 * Signal handler called for SIGCHLD. Triggers the
431 * respective handler by writing to the trigger pipe.
434 sighandler_child_death ()
437 int old_errno = errno; /* back-up errno */
440 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
442 GNUNET_DISK_PIPE_END_WRITE),
444 errno = old_errno; /* restore errno */
449 * Function called to process work items.
451 * @param cls closure, NULL
456 static char *argv[14];
457 static char anon_level[20];
458 static char content_prio[20];
459 static char repl_level[20];
461 const struct GNUNET_DISK_FileHandle *pr;
466 GNUNET_CONTAINER_DLL_remove (work_head,
470 argv[argc++] = "gnunet-publish";
473 if (disable_extractor)
475 if (do_disable_creation_time)
478 argv[argc++] = cfg_filename;
479 GNUNET_snprintf (anon_level, sizeof (anon_level),
480 "%u", anonymity_level);
482 argv[argc++] = anon_level;
483 GNUNET_snprintf (content_prio, sizeof (content_prio),
484 "%u", content_priority);
486 argv[argc++] = content_prio;
487 GNUNET_snprintf (repl_level, sizeof (repl_level),
488 "%u", replication_level);
490 argv[argc++] = repl_level;
491 argv[argc++] = wi->filename;
493 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
494 _("Publishing `%s'\n"),
496 GNUNET_assert (NULL == publish_proc);
497 publish_proc = GNUNET_OS_start_process_vap (GNUNET_YES,
501 if (NULL == publish_proc)
503 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
504 _("Failed to run `%s'\n"),
506 GNUNET_CONTAINER_DLL_insert (work_head,
509 run_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
514 pr = GNUNET_DISK_pipe_handle (sigpipe,
515 GNUNET_DISK_PIPE_END_READ);
517 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
519 &maint_child_death, wi);
524 * Recursively scan the given file/directory structure to determine
525 * a unique ID that represents the current state of the hierarchy.
527 * @param cls where to store the unique ID we are computing
528 * @param filename file to scan
529 * @return #GNUNET_OK (always)
532 determine_id (void *cls,
533 const char *filename)
535 struct GNUNET_HashCode *id = cls;
537 struct GNUNET_HashCode fx[2];
538 struct GNUNET_HashCode ft;
540 if (0 != STAT (filename, &sbuf))
542 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
547 GNUNET_CRYPTO_hash (filename,
550 if (!S_ISDIR (sbuf.st_mode))
554 fattr[0] = GNUNET_htonll (sbuf.st_size);
555 fattr[0] = GNUNET_htonll (sbuf.st_mtime);
557 GNUNET_CRYPTO_hash (fattr,
565 sizeof (struct GNUNET_HashCode));
566 GNUNET_DISK_directory_scan (filename,
570 /* use hash here to make hierarchical structure distinct from
571 all files on the same level */
572 GNUNET_CRYPTO_hash (fx,
575 /* use XOR here so that order of the files in the directory
577 GNUNET_CRYPTO_hash_xor (&ft,
585 * Function called with a filename (or directory name) to publish
586 * (if it has changed since the last time we published it). This function
587 * is called for the top-level files only.
589 * @param cls closure, NULL
590 * @param filename complete filename (absolute path)
591 * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR during shutdown
595 const char *filename)
598 struct GNUNET_HashCode key;
599 struct GNUNET_HashCode id;
601 if (GNUNET_YES == do_shutdown)
602 return GNUNET_SYSERR;
603 if ( (NULL != strstr (filename,
605 (NULL != strstr (filename,
607 return GNUNET_OK; /* skip internal file */
608 GNUNET_CRYPTO_hash (filename,
611 wi = GNUNET_CONTAINER_multihashmap_get (work_finished,
613 memset (&id, 0, sizeof (struct GNUNET_HashCode));
614 determine_id (&id, filename);
617 if (0 == memcmp (&id,
619 sizeof (struct GNUNET_HashCode)))
620 return GNUNET_OK; /* skip: we did this one already */
621 /* contents changed, need to re-do the directory... */
622 GNUNET_assert (GNUNET_YES ==
623 GNUNET_CONTAINER_multihashmap_remove (work_finished,
629 wi = GNUNET_new (struct WorkItem);
630 wi->filename = GNUNET_strdup (filename);
633 GNUNET_CONTAINER_DLL_insert (work_head,
636 if (GNUNET_YES == do_shutdown)
637 return GNUNET_SYSERR;
643 * Periodically run task to update our view of the directory to share.
651 start_time = GNUNET_TIME_absolute_get ();
652 (void) GNUNET_DISK_directory_scan (dir_name,
655 schedule_next_task ();
660 * Decide what the next task is (working or scanning) and schedule it.
663 schedule_next_task ()
665 struct GNUNET_TIME_Relative delay;
667 if (GNUNET_YES == do_shutdown)
669 GNUNET_assert (NULL == run_task);
670 if (NULL == work_head)
672 /* delay by at most 4h, at least 1s, and otherwise in between depending
673 on how long it took to scan */
674 delay = GNUNET_TIME_absolute_get_duration (start_time);
675 delay = GNUNET_TIME_relative_saturating_multiply (delay, 100);
676 delay = GNUNET_TIME_relative_min (delay,
678 delay = GNUNET_TIME_relative_max (delay,
680 run_task = GNUNET_SCHEDULER_add_delayed (delay,
686 run_task = GNUNET_SCHEDULER_add_now (&work,
693 * Main function that will be run by the scheduler.
696 * @param args remaining command-line arguments
697 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
698 * @param c configuration
704 const struct GNUNET_CONFIGURATION_Handle *c)
706 /* check arguments */
707 if ( (NULL == args[0]) ||
710 GNUNET_DISK_directory_test (args[0],
713 printf (_("You must specify one and only one directory name for automatic publication.\n"));
717 cfg_filename = GNUNET_strdup (cfgfile);
720 work_finished = GNUNET_CONTAINER_multihashmap_create (1024,
723 run_task = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
726 GNUNET_SCHEDULER_add_shutdown (&do_stop_task,
732 * Free memory associated with the work item from the work_finished map.
734 * @param cls NULL (unused)
735 * @param key key of the item in the map (unused)
736 * @param value the `struct WorkItem` to free
737 * @return #GNUNET_OK to continue to iterate
740 free_item (void *cls,
741 const struct GNUNET_HashCode *key,
744 struct WorkItem *wi = value;
746 GNUNET_free (wi->filename);
753 * The main function to automatically publish content to GNUnet.
755 * @param argc number of arguments from the command line
756 * @param argv command line arguments
757 * @return 0 ok, 1 on error
760 main (int argc, char *const *argv)
762 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
763 {'a', "anonymity", "LEVEL",
764 gettext_noop ("set the desired LEVEL of sender-anonymity"),
765 1, &GNUNET_GETOPT_set_uint, &anonymity_level},
766 {'d', "disable-creation-time", NULL,
768 ("disable adding the creation time to the metadata of the uploaded file"),
769 0, &GNUNET_GETOPT_set_one, &do_disable_creation_time},
770 {'D', "disable-extractor", NULL,
771 gettext_noop ("do not use libextractor to add keywords or metadata"),
772 0, &GNUNET_GETOPT_set_one, &disable_extractor},
773 {'p', "priority", "PRIORITY",
774 gettext_noop ("specify the priority of the content"),
775 1, &GNUNET_GETOPT_set_uint, &content_priority},
776 {'r', "replication", "LEVEL",
777 gettext_noop ("set the desired replication LEVEL"),
778 1, &GNUNET_GETOPT_set_uint, &replication_level},
779 {'V', "verbose", NULL,
780 gettext_noop ("be verbose (print progress information)"),
781 0, &GNUNET_GETOPT_set_one, &verbose},
782 GNUNET_GETOPT_OPTION_END
786 struct GNUNET_SIGNAL_Context *shc_chld;
789 GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
791 sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO,
792 GNUNET_NO, GNUNET_NO);
793 GNUNET_assert (NULL != sigpipe);
795 GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD,
796 &sighandler_child_death);
798 GNUNET_PROGRAM_run (argc, argv,
799 "gnunet-auto-share [OPTIONS] FILENAME",
801 ("Automatically publish files from a directory on GNUnet"),
802 options, &run, NULL)) ? ret : 1;
803 if (NULL != work_finished)
805 (void) GNUNET_CONTAINER_multihashmap_iterate (work_finished,
808 GNUNET_CONTAINER_multihashmap_destroy (work_finished);
810 while (NULL != (wi = work_head))
812 GNUNET_CONTAINER_DLL_remove (work_head,
815 GNUNET_free (wi->filename);
818 GNUNET_SIGNAL_handler_uninstall (shc_chld);
820 GNUNET_DISK_pipe_close (sigpipe);
822 GNUNET_free_non_null (cfg_filename);
824 GNUNET_free ((void*) argv);
828 /* end of gnunet-auto-share.c */