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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * @file fs/gnunet-auto-share.c
20 * @brief automatically publish files on GNUnet
21 * @author Christian Grothoff
24 * - support loading meta data / keywords from resource file
25 * - add stability timer (a la buildbot)
28 #include "gnunet_util_lib.h"
30 #define MAX_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
32 #define MIN_DELAY GNUNET_TIME_UNIT_MINUTES
36 * Item in our work queue (or in the set of files/directories
37 * we have successfully published).
43 * PENDING Work is kept in a linked list.
45 struct WorkItem *prev;
48 * PENDING Work is kept in a linked list.
50 struct WorkItem *next;
53 * Filename of the work item.
58 * Unique identity for this work item (used to detect
59 * if we need to do the work again).
61 struct GNUNET_HashCode id;
66 * Global return value from 'main'.
71 * Are we running 'verbosely'?
73 static unsigned int verbose;
76 * Configuration to use.
78 static const struct GNUNET_CONFIGURATION_Handle *cfg;
81 * Name of the configuration file.
83 static char *cfg_filename;
86 * Disable extractor option to use for publishing.
88 static int disable_extractor;
91 * Disable creation time option to use for publishing.
93 static int do_disable_creation_time;
96 * Handle for the main task that does scanning and working.
98 static struct GNUNET_SCHEDULER_Task *run_task;
101 * Anonymity level option to use for publishing.
103 static unsigned int anonymity_level = 1;
106 * Content priority option to use for publishing.
108 static unsigned int content_priority = 365;
111 * Replication level option to use for publishing.
113 static unsigned int replication_level = 1;
116 * Top-level directory we monitor to auto-publish.
118 static const char *dir_name;
121 * Head of linked list of files still to publish.
123 static struct WorkItem *work_head;
126 * Tail of linked list of files still to publish.
128 static struct WorkItem *work_tail;
131 * Map from the hash of the filename (!) to a `struct WorkItem`
134 static struct GNUNET_CONTAINER_MultiHashMap *work_finished;
137 * Set to #GNUNET_YES if we are shutting down.
139 static int do_shutdown;
142 * Start time of the current round; used to determine how long
143 * one iteration takes (which influences how fast we schedule
146 static struct GNUNET_TIME_Absolute start_time;
149 * Pipe used to communicate 'gnunet-publish' completion (SIGCHLD) via signal.
151 static struct GNUNET_DISK_PipeHandle *sigpipe;
154 * Handle to the 'gnunet-publish' process that we executed.
156 static struct GNUNET_OS_Process *publish_proc;
160 * Compute the name of the state database file we will use.
167 GNUNET_asprintf (&ret,
170 (DIR_SEPARATOR == dir_name[strlen(dir_name)-1]) ? "" : DIR_SEPARATOR_STR);
176 * Load the set of #work_finished items from disk.
182 struct GNUNET_BIO_ReadHandle *rh;
184 struct GNUNET_HashCode id;
189 fn = get_state_file ();
190 rh = GNUNET_BIO_read_open (fn);
195 if (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &n))
200 GNUNET_BIO_read_string (rh, "filename", &fn, 1024)) ||
202 GNUNET_BIO_read (rh, "id", &id, sizeof (struct GNUNET_HashCode))) )
204 wi = GNUNET_new (struct WorkItem);
207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
208 "Loaded serialization ID for `%s' is `%s'\n",
212 GNUNET_CRYPTO_hash (wi->filename,
213 strlen (wi->filename),
215 GNUNET_break (GNUNET_OK ==
216 GNUNET_CONTAINER_multihashmap_put (work_finished,
219 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
222 GNUNET_BIO_read_close (rh, &emsg))
226 GNUNET_free_non_null (fn);
228 (void) GNUNET_BIO_read_close (rh, &emsg);
229 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
230 _("Failed to load state: %s\n"),
232 GNUNET_free_non_null (emsg);
237 * Write work item from the #work_finished map to the given write handle.
239 * @param cls the `struct GNUNET_BIO_WriteHandle *`
240 * @param key key of the item in the map (unused)
241 * @param value the `struct WorkItem` to write
242 * @return #GNUNET_OK to continue to iterate (if write worked)
245 write_item (void *cls,
246 const struct GNUNET_HashCode *key,
249 struct GNUNET_BIO_WriteHandle *wh = cls;
250 struct WorkItem *wi = value;
252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
253 "Saving serialization ID of file `%s' with value `%s'\n",
255 GNUNET_h2s (&wi->id));
257 GNUNET_BIO_write_string (wh, wi->filename)) ||
259 GNUNET_BIO_write (wh,
261 sizeof (struct GNUNET_HashCode))) )
262 return GNUNET_SYSERR; /* write error, abort iteration */
268 * Save the set of #work_finished items on disk.
274 struct GNUNET_BIO_WriteHandle *wh;
277 n = GNUNET_CONTAINER_multihashmap_size (work_finished);
278 fn = get_state_file ();
279 wh = GNUNET_BIO_write_open (fn);
282 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
283 _("Failed to save state to file %s\n"),
289 GNUNET_BIO_write_int32 (wh, n))
291 (void) GNUNET_BIO_write_close (wh);
292 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
293 _("Failed to save state to file %s\n"),
298 (void) GNUNET_CONTAINER_multihashmap_iterate (work_finished,
301 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
302 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
303 _("Failed to save state to file %s\n"),
310 * Task run on shutdown. Serializes our current state to disk.
312 * @param cls closure, unused
315 do_stop_task (void *cls)
317 do_shutdown = GNUNET_YES;
318 if (NULL != publish_proc)
320 GNUNET_OS_process_kill (publish_proc,
324 if (NULL != run_task)
326 GNUNET_SCHEDULER_cancel (run_task);
333 * Decide what the next task is (working or scanning) and schedule it.
336 schedule_next_task (void);
340 * Task triggered whenever we receive a SIGCHLD (child
343 * @param cls the `struct WorkItem` we were working on
346 maint_child_death (void *cls)
348 struct WorkItem *wi = cls;
349 struct GNUNET_HashCode key;
350 enum GNUNET_OS_ProcessStatusType type;
354 const struct GNUNET_DISK_FileHandle *pr;
355 const struct GNUNET_SCHEDULER_TaskContext *tc;
358 pr = GNUNET_DISK_pipe_handle (sigpipe,
359 GNUNET_DISK_PIPE_END_READ);
360 tc = GNUNET_SCHEDULER_get_task_context ();
361 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
363 /* shutdown scheduled us, someone else will kill child,
364 we should just try again */
366 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
368 &maint_child_death, wi);
371 /* consume the signal */
372 GNUNET_break (0 < GNUNET_DISK_file_read (pr, &c, sizeof (c)));
374 ret = GNUNET_OS_process_status (publish_proc,
377 GNUNET_assert (GNUNET_SYSERR != ret);
378 if (GNUNET_NO == ret)
380 /* process still running? Then where did the SIGCHLD come from?
381 Well, let's declare it spurious (kernel bug?) and keep rolling.
385 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
387 &maint_child_death, wi);
390 GNUNET_assert (GNUNET_OK == ret);
392 GNUNET_OS_process_destroy (publish_proc);
395 if (GNUNET_YES == do_shutdown)
397 GNUNET_free (wi->filename);
401 if ( (GNUNET_OS_PROCESS_EXITED == type) &&
404 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
405 _("Publication of `%s' done\n"),
407 GNUNET_CRYPTO_hash (wi->filename,
408 strlen (wi->filename),
410 GNUNET_break (GNUNET_OK ==
411 GNUNET_CONTAINER_multihashmap_put (work_finished,
414 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
418 GNUNET_CONTAINER_DLL_insert_tail (work_head,
423 schedule_next_task ();
428 * Signal handler called for SIGCHLD. Triggers the
429 * respective handler by writing to the trigger pipe.
432 sighandler_child_death ()
435 int old_errno = errno; /* back-up errno */
438 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
440 GNUNET_DISK_PIPE_END_WRITE),
442 errno = old_errno; /* restore errno */
447 * Function called to process work items.
449 * @param cls closure, NULL
454 static char *argv[14];
455 static char anon_level[20];
456 static char content_prio[20];
457 static char repl_level[20];
459 const struct GNUNET_DISK_FileHandle *pr;
464 GNUNET_CONTAINER_DLL_remove (work_head,
468 argv[argc++] = "gnunet-publish";
471 if (disable_extractor)
473 if (do_disable_creation_time)
476 argv[argc++] = cfg_filename;
477 GNUNET_snprintf (anon_level, sizeof (anon_level),
478 "%u", anonymity_level);
480 argv[argc++] = anon_level;
481 GNUNET_snprintf (content_prio, sizeof (content_prio),
482 "%u", content_priority);
484 argv[argc++] = content_prio;
485 GNUNET_snprintf (repl_level, sizeof (repl_level),
486 "%u", replication_level);
488 argv[argc++] = repl_level;
489 argv[argc++] = wi->filename;
491 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
492 _("Publishing `%s'\n"),
494 GNUNET_assert (NULL == publish_proc);
495 publish_proc = GNUNET_OS_start_process_vap (GNUNET_YES,
499 if (NULL == publish_proc)
501 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
502 _("Failed to run `%s'\n"),
504 GNUNET_CONTAINER_DLL_insert (work_head,
507 run_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
512 pr = GNUNET_DISK_pipe_handle (sigpipe,
513 GNUNET_DISK_PIPE_END_READ);
515 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
517 &maint_child_death, wi);
522 * Recursively scan the given file/directory structure to determine
523 * a unique ID that represents the current state of the hierarchy.
525 * @param cls where to store the unique ID we are computing
526 * @param filename file to scan
527 * @return #GNUNET_OK (always)
530 determine_id (void *cls,
531 const char *filename)
533 struct GNUNET_HashCode *id = cls;
535 struct GNUNET_HashCode fx[2];
536 struct GNUNET_HashCode ft;
538 if (0 != STAT (filename, &sbuf))
540 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
545 GNUNET_CRYPTO_hash (filename,
548 if (!S_ISDIR (sbuf.st_mode))
552 fattr[0] = GNUNET_htonll (sbuf.st_size);
553 fattr[0] = GNUNET_htonll (sbuf.st_mtime);
555 GNUNET_CRYPTO_hash (fattr,
563 sizeof (struct GNUNET_HashCode));
564 GNUNET_DISK_directory_scan (filename,
568 /* use hash here to make hierarchical structure distinct from
569 all files on the same level */
570 GNUNET_CRYPTO_hash (fx,
573 /* use XOR here so that order of the files in the directory
575 GNUNET_CRYPTO_hash_xor (&ft,
583 * Function called with a filename (or directory name) to publish
584 * (if it has changed since the last time we published it). This function
585 * is called for the top-level files only.
587 * @param cls closure, NULL
588 * @param filename complete filename (absolute path)
589 * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR during shutdown
593 const char *filename)
596 struct GNUNET_HashCode key;
597 struct GNUNET_HashCode id;
599 if (GNUNET_YES == do_shutdown)
600 return GNUNET_SYSERR;
601 if ( (NULL != strstr (filename,
603 (NULL != strstr (filename,
605 return GNUNET_OK; /* skip internal file */
606 GNUNET_CRYPTO_hash (filename,
609 wi = GNUNET_CONTAINER_multihashmap_get (work_finished,
611 memset (&id, 0, sizeof (struct GNUNET_HashCode));
612 determine_id (&id, filename);
615 if (0 == memcmp (&id,
617 sizeof (struct GNUNET_HashCode)))
618 return GNUNET_OK; /* skip: we did this one already */
619 /* contents changed, need to re-do the directory... */
620 GNUNET_assert (GNUNET_YES ==
621 GNUNET_CONTAINER_multihashmap_remove (work_finished,
627 wi = GNUNET_new (struct WorkItem);
628 wi->filename = GNUNET_strdup (filename);
631 GNUNET_CONTAINER_DLL_insert (work_head,
634 if (GNUNET_YES == do_shutdown)
635 return GNUNET_SYSERR;
641 * Periodically run task to update our view of the directory to share.
649 start_time = GNUNET_TIME_absolute_get ();
650 (void) GNUNET_DISK_directory_scan (dir_name,
653 schedule_next_task ();
658 * Decide what the next task is (working or scanning) and schedule it.
661 schedule_next_task ()
663 struct GNUNET_TIME_Relative delay;
665 if (GNUNET_YES == do_shutdown)
667 GNUNET_assert (NULL == run_task);
668 if (NULL == work_head)
670 /* delay by at most 4h, at least 1s, and otherwise in between depending
671 on how long it took to scan */
672 delay = GNUNET_TIME_absolute_get_duration (start_time);
673 delay = GNUNET_TIME_relative_saturating_multiply (delay, 100);
674 delay = GNUNET_TIME_relative_min (delay,
676 delay = GNUNET_TIME_relative_max (delay,
678 run_task = GNUNET_SCHEDULER_add_delayed (delay,
684 run_task = GNUNET_SCHEDULER_add_now (&work,
691 * Main function that will be run by the scheduler.
694 * @param args remaining command-line arguments
695 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
696 * @param c configuration
702 const struct GNUNET_CONFIGURATION_Handle *c)
704 /* check arguments */
705 if ( (NULL == args[0]) ||
708 GNUNET_DISK_directory_test (args[0],
711 printf (_("You must specify one and only one directory name for automatic publication.\n"));
715 cfg_filename = GNUNET_strdup (cfgfile);
718 work_finished = GNUNET_CONTAINER_multihashmap_create (1024,
721 run_task = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
724 GNUNET_SCHEDULER_add_shutdown (&do_stop_task,
730 * Free memory associated with the work item from the work_finished map.
732 * @param cls NULL (unused)
733 * @param key key of the item in the map (unused)
734 * @param value the `struct WorkItem` to free
735 * @return #GNUNET_OK to continue to iterate
738 free_item (void *cls,
739 const struct GNUNET_HashCode *key,
742 struct WorkItem *wi = value;
744 GNUNET_free (wi->filename);
751 * The main function to automatically publish content to GNUnet.
753 * @param argc number of arguments from the command line
754 * @param argv command line arguments
755 * @return 0 ok, 1 on error
758 main (int argc, char *const *argv)
760 struct GNUNET_GETOPT_CommandLineOption options[] = {
762 GNUNET_GETOPT_option_uint ('a',
765 gettext_noop ("set the desired LEVEL of sender-anonymity"),
768 GNUNET_GETOPT_option_flag ('d',
769 "disable-creation-time",
770 gettext_noop ("disable adding the creation time to the metadata of the uploaded file"),
771 &do_disable_creation_time),
773 GNUNET_GETOPT_option_flag ('D',
775 gettext_noop ("do not use libextractor to add keywords or metadata"),
778 GNUNET_GETOPT_option_uint ('p',
781 gettext_noop ("specify the priority of the content"),
784 GNUNET_GETOPT_option_uint ('r',
787 gettext_noop ("set the desired replication LEVEL"),
790 GNUNET_GETOPT_option_verbose (&verbose),
792 GNUNET_GETOPT_OPTION_END
796 struct GNUNET_SIGNAL_Context *shc_chld;
799 GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
801 sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO,
802 GNUNET_NO, GNUNET_NO);
803 GNUNET_assert (NULL != sigpipe);
805 GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD,
806 &sighandler_child_death);
808 GNUNET_PROGRAM_run (argc, argv,
809 "gnunet-auto-share [OPTIONS] FILENAME",
811 ("Automatically publish files from a directory on GNUnet"),
812 options, &run, NULL)) ? ret : 1;
813 if (NULL != work_finished)
815 (void) GNUNET_CONTAINER_multihashmap_iterate (work_finished,
818 GNUNET_CONTAINER_multihashmap_destroy (work_finished);
820 while (NULL != (wi = work_head))
822 GNUNET_CONTAINER_DLL_remove (work_head,
825 GNUNET_free (wi->filename);
828 GNUNET_SIGNAL_handler_uninstall (shc_chld);
830 GNUNET_DISK_pipe_close (sigpipe);
832 GNUNET_free_non_null (cfg_filename);
834 GNUNET_free ((void*) argv);
838 /* end of gnunet-auto-share.c */