add units to time, use configuration time api where appropriate, fixing Mantis #1875
[oweals/gnunet.git] / src / util / configuration.c
index c69042175bd360b0a2883947a9637f741b14a859..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,9 +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;
     }
@@ -299,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;
   }
@@ -346,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)
   {
@@ -718,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);
 }
 
 
@@ -797,10 +813,10 @@ GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle
   }
   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];
@@ -1201,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).
@@ -1220,17 +1254,21 @@ 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 ((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))