adding single location for no_forcestart configuration list
[oweals/gnunet.git] / src / fs / gnunet-auto-share.c
index 0ea0647e92ba11c15f328912483cbc7b2f989eae..ca9535d1b6955d5cbdb5bf6b8ba062f33edb31f6 100644 (file)
@@ -21,7 +21,7 @@
  * @file fs/gnunet-auto-share.c
  * @brief automatically publish files on GNUnet
  * @author Christian Grothoff
- * 
+ *
  * TODO:
  * - support loading meta data / keywords from resource file
  * - add stability timer (a la buildbot)
@@ -204,11 +204,11 @@ load_state ()
   while (n-- > 0)
   {
     if ( (GNUNET_OK !=
-         GNUNET_BIO_read_string (rh, "filename", &fn, 1024)) || 
+         GNUNET_BIO_read_string (rh, "filename", &fn, 1024)) ||
         (GNUNET_OK !=
          GNUNET_BIO_read (rh, "id", &id, sizeof (struct GNUNET_HashCode))) )
       goto error;
-    wi = GNUNET_malloc (sizeof (struct WorkItem));
+    wi = GNUNET_new (struct WorkItem);
     wi->id = id;
     wi->filename = fn;
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -224,14 +224,14 @@ load_state ()
                                       wi,
                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
   }
-  if (GNUNET_OK == 
+  if (GNUNET_OK ==
       GNUNET_BIO_read_close (rh, &emsg))
     return;
   rh = NULL;
  error:
   GNUNET_free_non_null (fn);
   if (NULL != rh)
-    GNUNET_BIO_read_close (rh, &emsg);
+    (void) GNUNET_BIO_read_close (rh, &emsg);
   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
              _("Failed to load state: %s\n"),
              emsg);
@@ -259,7 +259,7 @@ write_item (void *cls,
              "Saving serialization ID of file `%s' with value `%s'\n",
              wi->filename,
              GNUNET_h2s (&wi->id));
-  if ( (GNUNET_OK != 
+  if ( (GNUNET_OK !=
        GNUNET_BIO_write_string (wh, wi->filename)) ||
        (GNUNET_OK !=
        GNUNET_BIO_write (wh,
@@ -283,6 +283,14 @@ save_state ()
   n = GNUNET_CONTAINER_multihashmap_size (work_finished);
   fn = get_state_file ();
   wh = GNUNET_BIO_write_open (fn);
+  if (NULL == wh)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+               _("Failed to save state to file %s\n"),
+               fn);
+    GNUNET_free (fn);
+    return;
+  }
   if (GNUNET_OK !=
       GNUNET_BIO_write_int32 (wh, n))
   {
@@ -373,7 +381,7 @@ maint_child_death (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   {
     GNUNET_break (0);
     GNUNET_OS_process_kill (publish_proc, SIGKILL);
-    type = GNUNET_OS_PROCESS_SIGNALED;    
+    type = GNUNET_OS_PROCESS_SIGNALED;
   }
   GNUNET_OS_process_destroy (publish_proc);
   publish_proc = NULL;
@@ -407,7 +415,7 @@ maint_child_death (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
                                      wi);
   }
   save_state ();
-  schedule_next_task ();    
+  schedule_next_task ();
 }
 
 
@@ -444,7 +452,7 @@ work (void *cls,
   static char content_prio[20];
   static char repl_level[20];
   struct WorkItem *wi;
-  const struct GNUNET_DISK_FileHandle *pr;  
+  const struct GNUNET_DISK_FileHandle *pr;
   int argc;
 
   run_task = GNUNET_SCHEDULER_NO_TASK;
@@ -480,7 +488,7 @@ work (void *cls,
              _("Publishing `%s'\n"),
              wi->filename);
   publish_proc = GNUNET_OS_start_process_vap (GNUNET_YES,
-                                             NULL, NULL,
+                                              0, NULL, NULL, NULL,
                                              "gnunet-publish",
                                              argv);
   if (NULL == publish_proc)
@@ -545,7 +553,7 @@ determine_id (void *cls,
   /* use hash here to make hierarchical structure distinct from
      all files on the same level */
   GNUNET_CRYPTO_hash (fx, sizeof (fx), &ft);
-  /* use XOR here so that order of the files in the directory 
+  /* use XOR here so that order of the files in the directory
      does not matter! */
   GNUNET_CRYPTO_hash_xor (&ft, id, id);
   return GNUNET_OK;
@@ -571,8 +579,10 @@ add_file (void *cls,
 
   if (GNUNET_YES == do_shutdown)
     return GNUNET_SYSERR;
-  if (NULL != strstr (filename,
-                     DIR_SEPARATOR_STR ".auto-share"))
+  if ( (NULL != strstr (filename,
+                     "/.auto-share")) ||
+       (NULL != strstr (filename,
+                       "\\.auto-share")) )
     return GNUNET_OK; /* skip internal file */
   GNUNET_CRYPTO_hash (filename,
                      strlen (filename),
@@ -588,13 +598,14 @@ add_file (void *cls,
                     sizeof (struct GNUNET_HashCode)))
       return GNUNET_OK; /* skip: we did this one already */
     /* contents changed, need to re-do the directory... */
-    GNUNET_CONTAINER_multihashmap_remove (work_finished,
-                                         &key,
-                                         wi);
+    GNUNET_assert (GNUNET_YES ==
+                  GNUNET_CONTAINER_multihashmap_remove (work_finished,
+                                                        &key,
+                                                        wi));
   }
   else
   {
-    wi = GNUNET_malloc (sizeof (struct WorkItem));
+    wi = GNUNET_new (struct WorkItem);
     wi->filename = GNUNET_strdup (filename);
   }
   wi->id = id;
@@ -602,7 +613,7 @@ add_file (void *cls,
                               work_tail,
                               wi);
   if (GNUNET_YES == do_shutdown)
-    return GNUNET_SYSERR; 
+    return GNUNET_SYSERR;
   return GNUNET_OK;
 }
 
@@ -634,7 +645,7 @@ schedule_next_task ()
   struct GNUNET_TIME_Relative delay;
 
   if (GNUNET_YES == do_shutdown)
-    return;  
+    return;
   if (NULL == work_head)
   {
     /* delay by at most 4h, at least 1s, and otherwise in between depending
@@ -670,7 +681,7 @@ run (void *cls, char *const *args, const char *cfgfile,
 {
   /* check arguments */
   if ((args[0] == NULL) || (args[1] != NULL) ||
-      (GNUNET_YES != GNUNET_DISK_directory_test (args[0])))
+      (GNUNET_YES != GNUNET_DISK_directory_test (args[0], GNUNET_YES)))
   {
     printf (_("You must specify one and only one directory name for automatic publication.\n"));
     ret = -1;
@@ -679,11 +690,11 @@ run (void *cls, char *const *args, const char *cfgfile,
   cfg_filename = GNUNET_strdup (cfgfile);
   cfg = c;
   dir_name = args[0];
-  work_finished = GNUNET_CONTAINER_multihashmap_create (1024);
+  work_finished = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_NO);
   load_state ();
   run_task = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
                                                 &scan, NULL);
-  
+
   kill_task =
       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_stop_task,
                                     NULL);
@@ -696,7 +707,7 @@ run (void *cls, char *const *args, const char *cfgfile,
  * @param cls NULL (unused)
  * @param key key of the item in the map (unused)
  * @param value the 'struct WorkItem' to free
- * @return GNUNET_OK to continue to iterate 
+ * @return GNUNET_OK to continue to iterate
  */
 static int
 free_item (void *cls,
@@ -710,6 +721,7 @@ free_item (void *cls,
   return GNUNET_OK;
 }
 
+
 /**
  * The main function to automatically publish content to GNUnet.
  *
@@ -719,7 +731,7 @@ free_item (void *cls,
  */
 int
 main (int argc, char *const *argv)
-{  
+{
   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
     {'a', "anonymity", "LEVEL",
      gettext_noop ("set the desired LEVEL of sender-anonymity"),
@@ -757,10 +769,13 @@ main (int argc, char *const *argv)
                            gettext_noop
                            ("Automatically publish files from a directory on GNUnet"),
                            options, &run, NULL)) ? ret : 1;
-  (void) GNUNET_CONTAINER_multihashmap_iterate (work_finished,
-                                               &free_item,
-                                               NULL);
-  GNUNET_CONTAINER_multihashmap_destroy (work_finished);
+  if (NULL != work_finished)
+  {
+    (void) GNUNET_CONTAINER_multihashmap_iterate (work_finished,
+                                                 &free_item,
+                                                 NULL);
+    GNUNET_CONTAINER_multihashmap_destroy (work_finished);
+  }
   while (NULL != (wi = work_head))
   {
     GNUNET_CONTAINER_DLL_remove (work_head, work_tail, wi);
@@ -773,6 +788,7 @@ main (int argc, char *const *argv)
   sigpipe = NULL;
   GNUNET_free (cfg_filename);
   cfg_filename = NULL;
+  GNUNET_free ((void*) argv);
   return ok;
 }