>From 2a30dca1819c4a7fd03a4d820d16968ca296efdf Mon Sep 17 00:00:00 2001 From: Alessio Vanni Date: Fri, 3 Jul 2020 22:37:42 +0200 Subject: [PATCH] Add function to return GNUnet's default configuration It's for convenience when applications call `GNUNET_OS_init', after which it's impossible to obtain GNUnet's configuration without manually checking the filesystem. With this function it's possible to get the configuration regardless of the state of the application. --- src/include/gnunet_configuration_lib.h | 12 ++++++++ src/util/configuration.c | 40 ++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/src/include/gnunet_configuration_lib.h b/src/include/gnunet_configuration_lib.h index 302429430..b5ceb5b94 100644 --- a/src/include/gnunet_configuration_lib.h +++ b/src/include/gnunet_configuration_lib.h @@ -100,6 +100,18 @@ GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg, const char *defaults_d); +/** + * Return GNUnet's default configuration. A new configuration is allocated + * each time and it's up to the caller to destroy it when done. This function + * returns GNUnet's configuration even when #GNUNET_OS_init has been called + * with a value different from #GNUNET_OS_project_data_default. + * + * @return a freshly allocated configuration + */ +struct GNUNET_CONFIGURATION_Handle * +GNUNET_CONFIGURATION_default(void); + + /** * Parse a configuration file, add all of the options in the * file to the configuration environment. diff --git a/src/util/configuration.c b/src/util/configuration.c index 7ed87cc1e..3d1d0fab0 100644 --- a/src/util/configuration.c +++ b/src/util/configuration.c @@ -1810,4 +1810,44 @@ GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg, } +/** + * Return GNUnet's default configuration. A new configuration is allocated + * each time and it's up to the caller to destroy it when done. This function + * returns GNUnet's configuration even when #GNUNET_OS_init has been called + * with a value different from #GNUNET_OS_project_data_default. + * + * @return a freshly allocated configuration + */ +struct GNUNET_CONFIGURATION_Handle * +GNUNET_CONFIGURATION_default(void) +{ + const struct GNUNET_OS_ProjectData *pd = GNUNET_OS_project_data_get (); + const struct GNUNET_OS_ProjectData *dpd = GNUNET_OS_project_data_default (); + + GNUNET_OS_init(dpd); + + struct GNUNET_CONFIGURATION_Handle *cfg = GNUNET_CONFIGURATION_create (); + const char *xdg = getenv ("XDG_CONFIG_HOME"); + char *cfgname = NULL; + + if (NULL != xdg) + GNUNET_asprintf (&cfgname, "%s/%s", xdg, pd->config_file); + else + cfgname = GNUNET_strdup (pd->user_config_file); + + if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, cfgname)) { + GNUNET_OS_init (pd); + GNUNET_CONFIGURATION_destroy (cfg); + GNUNET_free (cfgname); + return NULL; + } + + GNUNET_free (cfgname); + + GNUNET_OS_init (pd); + + return cfg; +} + + /* end of configuration.c */ -- 2.26.2