diff options
author | Alessio Vanni <vannilla@firemail.cc> | 2020-07-03 22:37:42 +0200 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2020-07-14 15:56:00 +0200 |
commit | 0f2ac01f3d935435f9fa0e3d1fb7e038fb0c19fa (patch) | |
tree | 9481b67bd57b9fd1b1d499253c920d623393fc70 | |
parent | c15f75419f4d2b40aa787b6ad2f4d48dcd0dc9b2 (diff) | |
download | gnunet-0f2ac01f3d935435f9fa0e3d1fb7e038fb0c19fa.tar.gz gnunet-0f2ac01f3d935435f9fa0e3d1fb7e038fb0c19fa.zip |
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.
-rw-r--r-- | src/include/gnunet_configuration_lib.h | 12 | ||||
-rw-r--r-- | src/util/configuration.c | 40 |
2 files changed, 52 insertions, 0 deletions
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 | |||
@@ -101,6 +101,18 @@ GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg, | |||
101 | 101 | ||
102 | 102 | ||
103 | /** | 103 | /** |
104 | * Return GNUnet's default configuration. A new configuration is allocated | ||
105 | * each time and it's up to the caller to destroy it when done. This function | ||
106 | * returns GNUnet's configuration even when #GNUNET_OS_init has been called | ||
107 | * with a value different from #GNUNET_OS_project_data_default. | ||
108 | * | ||
109 | * @return a freshly allocated configuration | ||
110 | */ | ||
111 | struct GNUNET_CONFIGURATION_Handle * | ||
112 | GNUNET_CONFIGURATION_default(void); | ||
113 | |||
114 | |||
115 | /** | ||
104 | * Parse a configuration file, add all of the options in the | 116 | * Parse a configuration file, add all of the options in the |
105 | * file to the configuration environment. | 117 | * file to the configuration environment. |
106 | * | 118 | * |
diff --git a/src/util/configuration.c b/src/util/configuration.c index 34ecc9e73..faee9e3bf 100644 --- a/src/util/configuration.c +++ b/src/util/configuration.c | |||
@@ -1810,4 +1810,44 @@ GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg, | |||
1810 | } | 1810 | } |
1811 | 1811 | ||
1812 | 1812 | ||
1813 | /** | ||
1814 | * Return GNUnet's default configuration. A new configuration is allocated | ||
1815 | * each time and it's up to the caller to destroy it when done. This function | ||
1816 | * returns GNUnet's configuration even when #GNUNET_OS_init has been called | ||
1817 | * with a value different from #GNUNET_OS_project_data_default. | ||
1818 | * | ||
1819 | * @return a freshly allocated configuration | ||
1820 | */ | ||
1821 | struct GNUNET_CONFIGURATION_Handle * | ||
1822 | GNUNET_CONFIGURATION_default(void) | ||
1823 | { | ||
1824 | const struct GNUNET_OS_ProjectData *pd = GNUNET_OS_project_data_get (); | ||
1825 | const struct GNUNET_OS_ProjectData *dpd = GNUNET_OS_project_data_default (); | ||
1826 | |||
1827 | GNUNET_OS_init(dpd); | ||
1828 | |||
1829 | struct GNUNET_CONFIGURATION_Handle *cfg = GNUNET_CONFIGURATION_create (); | ||
1830 | const char *xdg = getenv ("XDG_CONFIG_HOME"); | ||
1831 | char *cfgname = NULL; | ||
1832 | |||
1833 | if (NULL != xdg) | ||
1834 | GNUNET_asprintf (&cfgname, "%s/%s", xdg, pd->config_file); | ||
1835 | else | ||
1836 | cfgname = GNUNET_strdup (pd->user_config_file); | ||
1837 | |||
1838 | if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, cfgname)) { | ||
1839 | GNUNET_OS_init (pd); | ||
1840 | GNUNET_CONFIGURATION_destroy (cfg); | ||
1841 | GNUNET_free (cfgname); | ||
1842 | return NULL; | ||
1843 | } | ||
1844 | |||
1845 | GNUNET_free (cfgname); | ||
1846 | |||
1847 | GNUNET_OS_init (pd); | ||
1848 | |||
1849 | return cfg; | ||
1850 | } | ||
1851 | |||
1852 | |||
1813 | /* end of configuration.c */ | 1853 | /* end of configuration.c */ |