new convenience function to do operations on a configuration object to avoid repetiti...
[oweals/gnunet.git] / src / util / disk.c
index e0227be706b04d4f985aa048eb38df6f6d3c10e8..dc38d1137265acb29fc9c48f41e18aaa2bbb052b 100644 (file)
@@ -2668,28 +2668,19 @@ GNUNET_DISK_internal_file_handle_ (const struct GNUNET_DISK_FileHandle *fh,
 
 
 /**
- * Remove the directory given under @a option in
- * section [PATHS] in configuration under @a cfg_filename
+ * Helper function for #GNUNET_DISK_purge_cfg_dir.
  *
- * @param cfg_filename configuration file to parse
- * @param option option with the dir name to purge
+ * @param cls a `const char *` with the option to purge
+ * @param cfg our configuration
+ * @return #GNUNET_OK on success
  */
-void
-GNUNET_DISK_purge_cfg_dir (const char *cfg_filename,
-                           const char *option)
+static int
+purge_cfg_dir (void *cls,
+              const struct GNUNET_CONFIGURATION_Handle *cfg)
 {
-  struct GNUNET_CONFIGURATION_Handle *cfg;
+  const char *option = cls;
   char *tmpname;
-
-  cfg = GNUNET_CONFIGURATION_create ();
-  if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_load (cfg,
-                                 cfg_filename))
-  {
-    GNUNET_break (0);
-    GNUNET_CONFIGURATION_destroy (cfg);
-    return;
-  }
+  
   if (GNUNET_OK !=
       GNUNET_CONFIGURATION_get_value_filename (cfg,
                                                "PATHS",
@@ -2699,10 +2690,8 @@ GNUNET_DISK_purge_cfg_dir (const char *cfg_filename,
     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
                                "PATHS",
                                option);
-    GNUNET_CONFIGURATION_destroy (cfg);
-    return;
+    return GNUNET_NO;
   }
-  GNUNET_CONFIGURATION_destroy (cfg);
   if (GNUNET_SYSERR ==
       GNUNET_DISK_directory_remove (tmpname))
   {
@@ -2710,11 +2699,29 @@ GNUNET_DISK_purge_cfg_dir (const char *cfg_filename,
                               "remove",
                               tmpname);
     GNUNET_free (tmpname);
-    return;
+    return GNUNET_OK;
   }
   GNUNET_free (tmpname);
+  return GNUNET_OK;
 }
 
 
+/**
+ * Remove the directory given under @a option in
+ * section [PATHS] in configuration under @a cfg_filename
+ *
+ * @param cfg_filename configuration file to parse
+ * @param option option with the dir name to purge
+ */
+void
+GNUNET_DISK_purge_cfg_dir (const char *cfg_filename,
+                           const char *option)
+{
+  GNUNET_break (GNUNET_OK ==
+               GNUNET_CONFIGURATION_parse_and_run (cfg_filename,
+                                                   &purge_cfg_dir,
+                                                   (void *) option));
+}
+
 
 /* end of disk.c */