#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
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;
}
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;
}
}
- GNUNET_assert (0 == fclose (fp));
+ GNUNET_assert (0 == FCLOSE (fp));
/* restore dirty flag - anything we set in the meantime
* came from disk */
cfg->dirty = dirty;
}
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;
}
sec = cfg->sections;
while (sec != NULL)
{
- if (0 > fprintf (fp, "[%s]\n", sec->name))
+ if (0 > FPRINTF (fp, "[%s]\n", sec->name))
{
error = 1;
break;
pos[0] = '\\';
pos[1] = 'n';
}
- if (0 > fprintf (fp, "%s = %s\n", ent->key, val))
+ if (0 > FPRINTF (fp, "%s = %s\n", ent->key, val))
{
error = 1;
GNUNET_free (val);
}
if (error != 0)
break;
- if (0 > fprintf (fp, "\n"))
+ if (0 > FPRINTF (fp, "%s\n", ""))
{
error = 1;
break;
sec = sec->next;
}
if (error != 0)
- GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fprintf", filename);
- GNUNET_assert (0 == fclose (fp));
+ LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fprintf", filename);
+ GNUNET_assert (0 == FCLOSE (fp));
if (error != 0)
{
cfg->dirty = GNUNET_SYSERR; /* last write failed */
struct ConfigEntry *epos;
spos = cfg->sections;
- while ((spos != NULL) && (0 != strcmp (spos->name, section)))
+ while ((spos != NULL) && (0 != strcasecmp (spos->name, section)))
spos = spos->next;
if (spos == NULL)
spos = cfg->sections;
while (spos != NULL)
{
- if (0 == strcmp (section, spos->name))
+ if (0 == strcasecmp (section, spos->name))
{
if (prev == NULL)
cfg->sections = spos->next;
* @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,
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);
}
}
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];
}
+/**
+ * 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).
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))