Hello,

as the subject of this mail says, the attached patch implements a
function to obtain the default configuration used by GNUnet.

The main purpose is a convenience for applications using
`GNUNET_OS_init' to change the project data.  After changing the project
data it's impossible to obtain GNUnet's configuration without manually
examining the filesystem and that can become a problem when the
application needs to connect to GNUnet's own services
(e.g. gnunet-service-cadet.)

With this new function applications can retrieve the configuration even
after calling `GNUNET_OS_init' (or at any other point in time, really.)

Please review it so I can fix anything if needed.

Thanks,
A.V.

>From 2a30dca1819c4a7fd03a4d820d16968ca296efdf Mon Sep 17 00:00:00 2001
From: Alessio Vanni <[email protected]>
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

Reply via email to