#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;
}
}
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 = 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)
{
* @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;
* @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;
* @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;
* @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;
* @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];
* @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)
{
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);
}
* @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;
* @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;
}
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];
* @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;
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])))
{
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])
*/
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)
*/
int
GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
- *cfg,
- const char *section,
+ *cfg, const char *section,
const char *option,
const char *value)
{
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))
*/
int
GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
- *cfg,
- const char *section,
+ *cfg, const char *section,
const char *option,
const char *value)
{
}
+/**
+ * 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 (((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;
}