add units to time, use configuration time api where appropriate, fixing Mantis #1875
[oweals/gnunet.git] / src / util / configuration.c
index b3b9d5ea89b575e05af3273afc8fcc7ee549e2a9..41a4e1975ad242da6ab3afe1bd55a7d8fe5b599b 100644 (file)
 
 #include "platform.h"
 #include "gnunet_common.h"
-#include "gnunet_configuration_lib.h"
+#include "gnunet_util_lib.h"
 #include "gnunet_crypto_lib.h"
-#include "gnunet_disk_lib.h"
-#include "gnunet_os_lib.h"
 #include "gnunet_strings_lib.h"
 
+#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
+
+#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
 
 /**
  * @brief configuration entry
@@ -169,7 +170,7 @@ GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
   dirty = cfg->dirty;           /* back up value! */
   if (NULL == (fp = FOPEN (fn, "r")))
   {
-    GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen", fn);
+    LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fopen", fn);
     GNUNET_free (fn);
     return GNUNET_SYSERR;
   }
@@ -240,10 +241,9 @@ GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
     else
     {
       /* parse error */
-      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                  _
-                  ("Syntax error in configuration file `%s' at line %u.\n"),
-                  filename, nr);
+      LOG (GNUNET_ERROR_TYPE_WARNING,
+           _("Syntax error in configuration file `%s' at line %u.\n"), filename,
+           nr);
       ret = GNUNET_SYSERR;
       break;
     }
@@ -300,7 +300,7 @@ GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
   }
   if (NULL == (fp = FOPEN (fn, "w")))
   {
-    GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen", fn);
+    LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fopen", fn);
     GNUNET_free (fn);
     return GNUNET_SYSERR;
   }
@@ -347,7 +347,7 @@ GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
     sec = sec->next;
   }
   if (error != 0)
-    GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fprintf", filename);
+    LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fprintf", filename);
   GNUNET_assert (0 == fclose (fp));
   if (error != 0)
   {
@@ -496,11 +496,11 @@ GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg,
  * @param cls the destination configuration (struct GNUNET_CONFIGURATION_Handle*)
  * @param section section for the value
  * @param option option name of the value
- * @param value value to copy 
+ * @param value value to copy
  */
 static void
-copy_entry (void *cls,
-            const char *section, const char *option, const char *value)
+copy_entry (void *cls, const char *section, const char *option,
+            const char *value)
 {
   struct GNUNET_CONFIGURATION_Handle *dst = cls;
 
@@ -553,8 +553,8 @@ findSection (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section)
  * @return matching entry, NULL if not found
  */
 static struct ConfigEntry *
-findEntry (const struct GNUNET_CONFIGURATION_Handle *cfg,
-           const char *section, const char *key)
+findEntry (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section,
+           const char *key)
 {
   struct ConfigSection *sec;
   struct ConfigEntry *pos;
@@ -580,8 +580,8 @@ findEntry (const struct GNUNET_CONFIGURATION_Handle *cfg,
  * @param value value to copy (of the default conf.)
  */
 static void
-compareEntries (void *cls,
-                const char *section, const char *option, const char *value)
+compareEntries (void *cls, const char *section, const char *option,
+                const char *value)
 {
   struct DiffHandle *dh = cls;
   struct ConfigEntry *entNew;
@@ -627,10 +627,9 @@ GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
  * @param value value to set
  */
 void
-GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle
-                                       *cfg,
-                                       const char *section,
-                                       const char *option, const char *value)
+GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg,
+                                       const char *section, const char *option,
+                                       const char *value)
 {
   struct ConfigSection *sec;
   struct ConfigEntry *e;
@@ -667,9 +666,8 @@ GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle
  * @param number value to set
  */
 void
-GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle
-                                       *cfg, const char *section,
-                                       const char *option,
+GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
+                                       const char *section, const char *option,
                                        unsigned long long number)
 {
   char s[64];
@@ -689,9 +687,8 @@ GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle
  * @return GNUNET_OK on success, GNUNET_SYSERR on error
  */
 int
-GNUNET_CONFIGURATION_get_value_number (const struct
-                                       GNUNET_CONFIGURATION_Handle *cfg,
-                                       const char *section,
+GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle
+                                       *cfg, const char *section,
                                        const char *option,
                                        unsigned long long *number)
 {
@@ -722,21 +719,36 @@ GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle
                                      struct GNUNET_TIME_Relative *time)
 {
   struct ConfigEntry *e;
-  unsigned long long num;
 
   e = findEntry (cfg, section, option);
   if (e == NULL)
     return GNUNET_SYSERR;
-  if ((0 == strcasecmp (e->val, "infinity")) ||
-      (0 == strcasecmp (e->val, "forever")))
-  {
-    *time = GNUNET_TIME_UNIT_FOREVER_REL;
-    return GNUNET_OK;
-  }
-  if (1 != SSCANF (e->val, "%llu", &num))
+
+  return GNUNET_STRINGS_fancy_time_to_relative (e->val, time);
+}
+
+
+/**
+ * Get a configuration value that should be a size in bytes.
+ *
+ * @param cfg configuration to inspect
+ * @param section section of interest
+ * @param option option of interest
+ * @param size set to the size in bytes as stored in the configuration
+ * @return GNUNET_OK on success, GNUNET_SYSERR on error
+ */
+int
+GNUNET_CONFIGURATION_get_value_size (const struct GNUNET_CONFIGURATION_Handle
+                                     *cfg, const char *section,
+                                     const char *option,
+                                     unsigned long long *size)
+{
+  struct ConfigEntry *e;
+
+  e = findEntry (cfg, section, option);
+  if (e == NULL)
     return GNUNET_SYSERR;
-  time->rel_value = (uint64_t) num;
-  return GNUNET_OK;
+  return GNUNET_STRINGS_fancy_size_to_bytes (e->val, size);
 }
 
 
@@ -751,9 +763,8 @@ GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle
  * @return GNUNET_OK on success, GNUNET_SYSERR on error
  */
 int
-GNUNET_CONFIGURATION_get_value_string (const struct
-                                       GNUNET_CONFIGURATION_Handle *cfg,
-                                       const char *section,
+GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle
+                                       *cfg, const char *section,
                                        const char *option, char **value)
 {
   struct ConfigEntry *e;
@@ -782,11 +793,10 @@ GNUNET_CONFIGURATION_get_value_string (const struct
  * @return GNUNET_OK on success, GNUNET_SYSERR on error
  */
 int
-GNUNET_CONFIGURATION_get_value_choice (const struct
-                                       GNUNET_CONFIGURATION_Handle *cfg,
-                                       const char *section,
-                                       const char *option,
-                                       const char **choices, const char **value)
+GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle
+                                       *cfg, const char *section,
+                                       const char *option, const char **choices,
+                                       const char **value)
 {
   struct ConfigEntry *e;
   int i;
@@ -803,10 +813,10 @@ GNUNET_CONFIGURATION_get_value_choice (const struct
   }
   if (choices[i] == NULL)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                _("Configuration value '%s' for '%s'"
-                  " in section '%s' is not in set of legal choices\n"),
-                e->val, option, section);
+    LOG (GNUNET_ERROR_TYPE_ERROR,
+         _("Configuration value '%s' for '%s'"
+           " in section '%s' is not in set of legal choices\n"), e->val, option,
+         section);
     return GNUNET_SYSERR;
   }
   *value = choices[i];
@@ -822,8 +832,8 @@ GNUNET_CONFIGURATION_get_value_choice (const struct
  * @return GNUNET_YES if so, GNUNET_NO if not.
  */
 int
-GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle
-                                 *cfg, const char *section, const char *option)
+GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
+                                 const char *section, const char *option)
 {
   struct ConfigEntry *e;
 
@@ -866,9 +876,8 @@ GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle
     orig[i] = '\0';
     post = &orig[i + 1];
   }
-  if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg,
-                                                            "PATHS",
-                                                            &orig[1], &prefix))
+  if (GNUNET_OK !=
+      GNUNET_CONFIGURATION_get_value_filename (cfg, "PATHS", &orig[1], &prefix))
   {
     if (NULL == (env = getenv (&orig[1])))
     {
@@ -940,8 +949,8 @@ GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle
   const char *val;
   int ret;
 
-  ret = GNUNET_CONFIGURATION_get_value_choice (cfg,
-                                               section, option, yesno, &val);
+  ret =
+      GNUNET_CONFIGURATION_get_value_choice (cfg, section, option, yesno, &val);
   if (ret == GNUNET_SYSERR)
     return ret;
   if (val == yesno[0])
@@ -962,8 +971,8 @@ GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle
  */
 int
 GNUNET_CONFIGURATION_iterate_value_filenames (const struct
-                                              GNUNET_CONFIGURATION_Handle
-                                              *cfg, const char *section,
+                                              GNUNET_CONFIGURATION_Handle *cfg,
+                                              const char *section,
                                               const char *option,
                                               GNUNET_FileNameCallback cb,
                                               void *cb_cls)
@@ -1093,8 +1102,7 @@ test_match (void *cls, const char *fn)
  */
 int
 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
-                                            *cfg,
-                                            const char *section,
+                                            *cfg, const char *section,
                                             const char *option,
                                             const char *value)
 {
@@ -1102,12 +1110,10 @@ GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
   char *old;
   char *nw;
 
-  if (GNUNET_SYSERR
-      == GNUNET_CONFIGURATION_iterate_value_filenames (cfg,
-                                                       section,
-                                                       option,
-                                                       &test_match,
-                                                       (void *) value))
+  if (GNUNET_SYSERR ==
+      GNUNET_CONFIGURATION_iterate_value_filenames (cfg, section, option,
+                                                    &test_match,
+                                                    (void *) value))
     return GNUNET_NO;           /* already exists */
   if (GNUNET_OK !=
       GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
@@ -1140,8 +1146,7 @@ GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
  */
 int
 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
-                                            *cfg,
-                                            const char *section,
+                                            *cfg, const char *section,
                                             const char *option,
                                             const char *value)
 {
@@ -1212,6 +1217,24 @@ GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
 }
 
 
+/**
+ * Wrapper around GNUNET_CONFIGURATION_parse.
+ *
+ * @param cls the cfg
+ * @param filename file to parse
+ * @return GNUNET_OK on success
+ */
+static int
+parse_configuration_file (void *cls, const char *filename)
+{
+  struct GNUNET_CONFIGURATION_Handle *cfg = cls;
+  int ret;
+
+  ret = GNUNET_CONFIGURATION_parse (cfg, filename);
+  return ret;
+}
+
+
 /**
  * Load configuration (starts with defaults, then loads
  * system-specific configuration).
@@ -1231,30 +1254,30 @@ GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
   if (ipath == NULL)
     return GNUNET_SYSERR;
   baseconfig = NULL;
-  GNUNET_asprintf (&baseconfig,
-                   "%s%s%s", ipath, DIR_SEPARATOR_STR, "defaults.conf");
+  GNUNET_asprintf (&baseconfig, "%s%s", ipath, "config.d");
   GNUNET_free (ipath);
-  if ((GNUNET_OK !=
-       GNUNET_CONFIGURATION_parse (cfg, baseconfig)) ||
-      (!((filename == NULL) ||
-         (GNUNET_OK == GNUNET_CONFIGURATION_parse (cfg, filename)))))
+  if (GNUNET_SYSERR ==
+      GNUNET_DISK_directory_scan (baseconfig, &parse_configuration_file, cfg))
   {
     GNUNET_free (baseconfig);
-    return (filename == NULL) ? GNUNET_OK : GNUNET_SYSERR;
+    return GNUNET_SYSERR;       /* no configuration at all found */
   }
   GNUNET_free (baseconfig);
-  if (((GNUNET_YES != GNUNET_CONFIGURATION_have_value (cfg,
-                                                       "PATHS",
-                                                       "DEFAULTCONFIG"))) &&
+  if ((filename != NULL) &&
+      (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, filename)))
+  {
+    /* specified configuration not found */
+    return GNUNET_SYSERR;
+  }
+  if (((GNUNET_YES !=
+        GNUNET_CONFIGURATION_have_value (cfg, "PATHS", "DEFAULTCONFIG"))) &&
       (filename != NULL))
-    GNUNET_CONFIGURATION_set_value_string (cfg,
-                                           "PATHS", "DEFAULTCONFIG", filename);
-  if ((GNUNET_YES == GNUNET_CONFIGURATION_have_value (cfg,
-                                                      "TESTING",
-                                                      "WEAKRANDOM")) &&
-      (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (cfg,
-                                                           "TESTING",
-                                                           "WEAKRANDOM")))
+    GNUNET_CONFIGURATION_set_value_string (cfg, "PATHS", "DEFAULTCONFIG",
+                                           filename);
+  if ((GNUNET_YES ==
+       GNUNET_CONFIGURATION_have_value (cfg, "TESTING", "WEAKRANDOM")) &&
+      (GNUNET_YES ==
+       GNUNET_CONFIGURATION_get_value_yesno (cfg, "TESTING", "WEAKRANDOM")))
     GNUNET_CRYPTO_random_disable_entropy_gathering ();
   return GNUNET_OK;
 }