2 This file is part of GNUnet.
3 (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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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 GNUNET_SCHEDULER_TaskIdentifier kill_task;
103 * Handle for the main task that does scanning and working.
105 static GNUNET_SCHEDULER_TaskIdentifier 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_malloc (sizeof (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_CONTAINER_multihashmap_put (work_finished,
225 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
228 GNUNET_BIO_read_close (rh, &emsg))
232 GNUNET_free_non_null (fn);
234 (void) GNUNET_BIO_read_close (rh, &emsg);
235 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
236 _("Failed to load state: %s\n"),
238 GNUNET_free_non_null (emsg);
243 * Write work item from the work_finished map to the given write handle.
245 * @param cls the 'struct GNUNET_BIO_WriteHandle*'
246 * @param key key of the item in the map (unused)
247 * @param value the 'struct WorkItem' to write
248 * @return GNUNET_OK to continue to iterate (if write worked)
251 write_item (void *cls,
252 const struct GNUNET_HashCode *key,
255 struct GNUNET_BIO_WriteHandle *wh = cls;
256 struct WorkItem *wi = value;
258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
259 "Saving serialization ID of file `%s' with value `%s'\n",
261 GNUNET_h2s (&wi->id));
263 GNUNET_BIO_write_string (wh, wi->filename)) ||
265 GNUNET_BIO_write (wh,
267 sizeof (struct GNUNET_HashCode))) )
268 return GNUNET_SYSERR; /* write error, abort iteration */
274 * Save the set of 'work_finished' items on disk.
280 struct GNUNET_BIO_WriteHandle *wh;
283 n = GNUNET_CONTAINER_multihashmap_size (work_finished);
284 fn = get_state_file ();
285 wh = GNUNET_BIO_write_open (fn);
288 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
289 _("Failed to save state to file %s\n"),
295 GNUNET_BIO_write_int32 (wh, n))
297 (void) GNUNET_BIO_write_close (wh);
298 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
299 _("Failed to save state to file %s\n"),
304 (void) GNUNET_CONTAINER_multihashmap_iterate (work_finished,
307 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
308 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
309 _("Failed to save state to file %s\n"),
316 * Task run on shutdown. Serializes our current state to disk.
318 * @param cls closure, unused
319 * @param tc scheduler context, unused
322 do_stop_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
324 kill_task = GNUNET_SCHEDULER_NO_TASK;
325 do_shutdown = GNUNET_YES;
326 if (NULL != publish_proc)
328 GNUNET_OS_process_kill (publish_proc, SIGKILL);
331 if (GNUNET_SCHEDULER_NO_TASK != run_task)
333 GNUNET_SCHEDULER_cancel (run_task);
334 run_task = GNUNET_SCHEDULER_NO_TASK;
340 * Decide what the next task is (working or scanning) and schedule it.
343 schedule_next_task (void);
347 * Task triggered whenever we receive a SIGCHLD (child
350 * @param cls the 'struct WorkItem' we were working on
354 maint_child_death (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
356 struct WorkItem *wi = cls;
357 struct GNUNET_HashCode key;
358 enum GNUNET_OS_ProcessStatusType type;
362 const struct GNUNET_DISK_FileHandle *pr;
365 run_task = GNUNET_SCHEDULER_NO_TASK;
366 pr = GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ);
367 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
369 /* shutdown scheduled us, ignore! */
371 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
372 pr, &maint_child_death, wi);
376 ret = GNUNET_OS_process_status (publish_proc,
379 GNUNET_assert (GNUNET_SYSERR != ret);
380 if (GNUNET_NO == ret)
383 GNUNET_OS_process_kill (publish_proc, SIGKILL);
384 type = GNUNET_OS_PROCESS_SIGNALED;
386 GNUNET_OS_process_destroy (publish_proc);
388 /* consume the signal */
389 GNUNET_break (0 < GNUNET_DISK_file_read (pr, &c, sizeof (c)));
391 if (GNUNET_YES == do_shutdown)
393 GNUNET_free (wi->filename);
397 if ( (GNUNET_OS_PROCESS_EXITED == type) &&
400 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
401 _("Publication of `%s' done\n"),
403 GNUNET_CRYPTO_hash (wi->filename,
404 strlen (wi->filename),
406 GNUNET_CONTAINER_multihashmap_put (work_finished,
409 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
413 GNUNET_CONTAINER_DLL_insert_tail (work_head,
418 schedule_next_task ();
423 * Signal handler called for SIGCHLD. Triggers the
424 * respective handler by writing to the trigger pipe.
427 sighandler_child_death ()
430 int old_errno = errno; /* back-up errno */
433 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
434 (sigpipe, GNUNET_DISK_PIPE_END_WRITE),
436 errno = old_errno; /* restore errno */
441 * Function called to process work items.
443 * @param cls closure, NULL
444 * @param tc scheduler context (unused)
448 const struct GNUNET_SCHEDULER_TaskContext *tc)
450 static char *argv[14];
451 static char anon_level[20];
452 static char content_prio[20];
453 static char repl_level[20];
455 const struct GNUNET_DISK_FileHandle *pr;
458 run_task = GNUNET_SCHEDULER_NO_TASK;
460 GNUNET_CONTAINER_DLL_remove (work_head,
464 argv[argc++] = "gnunet-publish";
467 if (disable_extractor)
469 if (do_disable_creation_time)
472 argv[argc++] = cfg_filename;
473 GNUNET_snprintf (anon_level, sizeof (anon_level),
474 "%u", anonymity_level);
476 argv[argc++] = anon_level;
477 GNUNET_snprintf (content_prio, sizeof (content_prio),
478 "%u", content_priority);
480 argv[argc++] = content_prio;
481 GNUNET_snprintf (repl_level, sizeof (repl_level),
482 "%u", replication_level);
484 argv[argc++] = repl_level;
485 argv[argc++] = wi->filename;
487 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
488 _("Publishing `%s'\n"),
490 publish_proc = GNUNET_OS_start_process_vap (GNUNET_YES,
494 if (NULL == publish_proc)
496 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
497 _("Failed to run `%s'\n"),
499 GNUNET_CONTAINER_DLL_insert (work_head,
502 run_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
507 pr = GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ);
509 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
510 pr, &maint_child_death, wi);
515 * Recursively scan the given file/directory structure to determine
516 * a unique ID that represents the current state of the hierarchy.
518 * @param cls where to store the unique ID we are computing
519 * @param filename file to scan
520 * @return GNUNET_OK (always)
523 determine_id (void *cls,
524 const char *filename)
526 struct GNUNET_HashCode *id = cls;
528 struct GNUNET_HashCode fx[2];
529 struct GNUNET_HashCode ft;
531 if (0 != STAT (filename, &sbuf))
533 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
536 GNUNET_CRYPTO_hash (filename, strlen (filename), &fx[0]);
537 if (!S_ISDIR (sbuf.st_mode))
541 fattr[0] = GNUNET_htonll (sbuf.st_size);
542 fattr[0] = GNUNET_htonll (sbuf.st_mtime);
544 GNUNET_CRYPTO_hash (fattr, sizeof (fattr), &fx[1]);
548 memset (&fx[1], 1, sizeof (struct GNUNET_HashCode));
549 GNUNET_DISK_directory_scan (filename,
553 /* use hash here to make hierarchical structure distinct from
554 all files on the same level */
555 GNUNET_CRYPTO_hash (fx, sizeof (fx), &ft);
556 /* use XOR here so that order of the files in the directory
558 GNUNET_CRYPTO_hash_xor (&ft, id, id);
564 * Function called with a filename (or directory name) to publish
565 * (if it has changed since the last time we published it). This function
566 * is called for the top-level files only.
568 * @param cls closure, NULL
569 * @param filename complete filename (absolute path)
570 * @return GNUNET_OK to continue to iterate, GNUNET_SYSERR during shutdown
574 const char *filename)
577 struct GNUNET_HashCode key;
578 struct GNUNET_HashCode id;
580 if (GNUNET_YES == do_shutdown)
581 return GNUNET_SYSERR;
582 if ( (NULL != strstr (filename,
584 (NULL != strstr (filename,
586 return GNUNET_OK; /* skip internal file */
587 GNUNET_CRYPTO_hash (filename,
590 wi = GNUNET_CONTAINER_multihashmap_get (work_finished,
592 memset (&id, 0, sizeof (struct GNUNET_HashCode));
593 determine_id (&id, filename);
596 if (0 == memcmp (&id,
598 sizeof (struct GNUNET_HashCode)))
599 return GNUNET_OK; /* skip: we did this one already */
600 /* contents changed, need to re-do the directory... */
601 GNUNET_assert (GNUNET_YES ==
602 GNUNET_CONTAINER_multihashmap_remove (work_finished,
608 wi = GNUNET_malloc (sizeof (struct WorkItem));
609 wi->filename = GNUNET_strdup (filename);
612 GNUNET_CONTAINER_DLL_insert (work_head,
615 if (GNUNET_YES == do_shutdown)
616 return GNUNET_SYSERR;
622 * Periodically run task to update our view of the directory to share.
625 * @param tc scheduler context, unused
628 scan (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
630 run_task = GNUNET_SCHEDULER_NO_TASK;
631 start_time = GNUNET_TIME_absolute_get ();
632 (void) GNUNET_DISK_directory_scan (dir_name,
635 schedule_next_task ();
640 * Decide what the next task is (working or scanning) and schedule it.
643 schedule_next_task ()
645 struct GNUNET_TIME_Relative delay;
647 if (GNUNET_YES == do_shutdown)
649 if (NULL == work_head)
651 /* delay by at most 4h, at least 1s, and otherwise in between depending
652 on how long it took to scan */
653 delay = GNUNET_TIME_absolute_get_duration (start_time);
654 delay = GNUNET_TIME_relative_min (MIN_FREQUENCY,
655 GNUNET_TIME_relative_multiply (delay,
657 delay = GNUNET_TIME_relative_max (delay,
659 run_task = GNUNET_SCHEDULER_add_delayed (delay,
665 run_task = GNUNET_SCHEDULER_add_now (&work, NULL);
671 * Main function that will be run by the scheduler.
674 * @param args remaining command-line arguments
675 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
676 * @param c configuration
679 run (void *cls, char *const *args, const char *cfgfile,
680 const struct GNUNET_CONFIGURATION_Handle *c)
682 /* check arguments */
683 if ((args[0] == NULL) || (args[1] != NULL) ||
684 (GNUNET_YES != GNUNET_DISK_directory_test (args[0], GNUNET_YES)))
686 printf (_("You must specify one and only one directory name for automatic publication.\n"));
690 cfg_filename = GNUNET_strdup (cfgfile);
693 work_finished = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_NO);
695 run_task = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
699 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_stop_task,
705 * Free memory associated with the work item from the work_finished map.
707 * @param cls NULL (unused)
708 * @param key key of the item in the map (unused)
709 * @param value the 'struct WorkItem' to free
710 * @return GNUNET_OK to continue to iterate
713 free_item (void *cls,
714 const struct GNUNET_HashCode *key,
717 struct WorkItem *wi = value;
719 GNUNET_free (wi->filename);
726 * The main function to automatically publish content to GNUnet.
728 * @param argc number of arguments from the command line
729 * @param argv command line arguments
730 * @return 0 ok, 1 on error
733 main (int argc, char *const *argv)
735 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
736 {'a', "anonymity", "LEVEL",
737 gettext_noop ("set the desired LEVEL of sender-anonymity"),
738 1, &GNUNET_GETOPT_set_uint, &anonymity_level},
739 {'d', "disable-creation-time", NULL,
741 ("disable adding the creation time to the metadata of the uploaded file"),
742 0, &GNUNET_GETOPT_set_one, &do_disable_creation_time},
743 {'D', "disable-extractor", NULL,
744 gettext_noop ("do not use libextractor to add keywords or metadata"),
745 0, &GNUNET_GETOPT_set_one, &disable_extractor},
746 {'p', "priority", "PRIORITY",
747 gettext_noop ("specify the priority of the content"),
748 1, &GNUNET_GETOPT_set_uint, &content_priority},
749 {'r', "replication", "LEVEL",
750 gettext_noop ("set the desired replication LEVEL"),
751 1, &GNUNET_GETOPT_set_uint, &replication_level},
752 {'V', "verbose", NULL,
753 gettext_noop ("be verbose (print progress information)"),
754 0, &GNUNET_GETOPT_set_one, &verbose},
755 GNUNET_GETOPT_OPTION_END
759 struct GNUNET_SIGNAL_Context *shc_chld;
761 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
763 sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO);
764 GNUNET_assert (sigpipe != NULL);
766 GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death);
768 GNUNET_PROGRAM_run (argc, argv, "gnunet-auto-share [OPTIONS] FILENAME",
770 ("Automatically publish files from a directory on GNUnet"),
771 options, &run, NULL)) ? ret : 1;
772 if (NULL != work_finished)
774 (void) GNUNET_CONTAINER_multihashmap_iterate (work_finished,
777 GNUNET_CONTAINER_multihashmap_destroy (work_finished);
779 while (NULL != (wi = work_head))
781 GNUNET_CONTAINER_DLL_remove (work_head, work_tail, wi);
782 GNUNET_free (wi->filename);
785 GNUNET_SIGNAL_handler_uninstall (shc_chld);
787 GNUNET_DISK_pipe_close (sigpipe);
789 GNUNET_free (cfg_filename);
791 GNUNET_free ((void*) argv);
795 /* end of gnunet-auto-share.c */