From b237fcbd7e189f755dd8b372398a0ea94255d090 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sat, 10 Nov 2018 17:36:11 +0100 Subject: new convenience function to do operations on a configuration object to avoid repetitive configuration_create, _load and _destroy logic --- po/POTFILES.in | 2 ++ src/include/gnunet_configuration_lib.h | 31 ++++++++++++++++++++- src/util/configuration.c | 35 +++++++++++++++++++++++ src/util/disk.c | 51 +++++++++++++++++++--------------- src/util/service.c | 10 ++++--- 5 files changed, 102 insertions(+), 27 deletions(-) diff --git a/po/POTFILES.in b/po/POTFILES.in index caf7353be..54fe7b4e7 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -398,9 +398,11 @@ src/testing/list-keys.c src/testing/testing.c src/topology/friends.c src/topology/gnunet-daemon-topology.c +src/transport/gnunet-communicator-unix.c src/transport/gnunet-helper-transport-bluetooth.c src/transport/gnunet-helper-transport-wlan.c src/transport/gnunet-helper-transport-wlan-dummy.c +src/transport/gnunet-service-tng.c src/transport/gnunet-service-transport_ats.c src/transport/gnunet-service-transport.c src/transport/gnunet-service-transport_hello.c diff --git a/src/include/gnunet_configuration_lib.h b/src/include/gnunet_configuration_lib.h index e3eefa18d..ec3d12738 100644 --- a/src/include/gnunet_configuration_lib.h +++ b/src/include/gnunet_configuration_lib.h @@ -1,6 +1,6 @@ /* This file is part of GNUnet. - Copyright (C) 2006, 2008, 2009 GNUnet e.V. + Copyright (C) 2006, 2008, 2009, 2018 GNUnet e.V. GNUnet is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published @@ -190,6 +190,35 @@ int GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg); +/** + * Signature of a function to be run with a configuration. + * + * @param cls closure + * @param cfg the configuration + * @return status code + */ +typedef int +(*GNUNET_CONFIGURATION_Callback)(void *cls, + const struct GNUNET_CONFIGURATION_Handle *cfg); + + +/** + * Parse a configuration file @a filename and run the function + * @a cb with the resulting configuration object. Then free the + * configuration object and return the status value from @a cb. + * + * @param filename configuration to parse, NULL for "default" + * @param cb function to run + * @param cb_cls closure for @a cb + * @return #GNUNET_SYSERR if parsing the configuration failed, + * otherwise return value from @a cb. + */ +int +GNUNET_CONFIGURATION_parse_and_run (const char *filename, + GNUNET_CONFIGURATION_Callback cb, + void *cb_cls); + + /** * Function to iterate over options. * diff --git a/src/util/configuration.c b/src/util/configuration.c index 41eb3188d..a8e492a32 100644 --- a/src/util/configuration.c +++ b/src/util/configuration.c @@ -137,6 +137,41 @@ GNUNET_CONFIGURATION_destroy (struct GNUNET_CONFIGURATION_Handle *cfg) } +/** + * Parse a configuration file @a filename and run the function + * @a cb with the resulting configuration object. Then free the + * configuration object and return the status value from @a cb. + * + * @param filename configuration to parse, NULL for "default" + * @param cb function to run + * @param cb_cls closure for @a cb + * @return #GNUNET_SYSERR if parsing the configuration failed, + * otherwise return value from @a cb. + */ +int +GNUNET_CONFIGURATION_parse_and_run (const char *filename, + GNUNET_CONFIGURATION_Callback cb, + void *cb_cls) +{ + struct GNUNET_CONFIGURATION_Handle *cfg; + int ret; + + cfg = GNUNET_CONFIGURATION_create (); + if (GNUNET_OK != + GNUNET_CONFIGURATION_load (cfg, + filename)) + { + GNUNET_break (0); + GNUNET_CONFIGURATION_destroy (cfg); + return GNUNET_SYSERR; + } + ret = cb (cb_cls, + cfg); + GNUNET_CONFIGURATION_destroy (cfg); + return ret; +} + + /** * De-serializes configuration * diff --git a/src/util/disk.c b/src/util/disk.c index e0227be70..dc38d1137 100644 --- a/src/util/disk.c +++ b/src/util/disk.c @@ -2668,28 +2668,19 @@ GNUNET_DISK_internal_file_handle_ (const struct GNUNET_DISK_FileHandle *fh, /** - * Remove the directory given under @a option in - * section [PATHS] in configuration under @a cfg_filename + * Helper function for #GNUNET_DISK_purge_cfg_dir. * - * @param cfg_filename configuration file to parse - * @param option option with the dir name to purge + * @param cls a `const char *` with the option to purge + * @param cfg our configuration + * @return #GNUNET_OK on success */ -void -GNUNET_DISK_purge_cfg_dir (const char *cfg_filename, - const char *option) +static int +purge_cfg_dir (void *cls, + const struct GNUNET_CONFIGURATION_Handle *cfg) { - struct GNUNET_CONFIGURATION_Handle *cfg; + const char *option = cls; char *tmpname; - - cfg = GNUNET_CONFIGURATION_create (); - if (GNUNET_OK != - GNUNET_CONFIGURATION_load (cfg, - cfg_filename)) - { - GNUNET_break (0); - GNUNET_CONFIGURATION_destroy (cfg); - return; - } + if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "PATHS", @@ -2699,10 +2690,8 @@ GNUNET_DISK_purge_cfg_dir (const char *cfg_filename, GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "PATHS", option); - GNUNET_CONFIGURATION_destroy (cfg); - return; + return GNUNET_NO; } - GNUNET_CONFIGURATION_destroy (cfg); if (GNUNET_SYSERR == GNUNET_DISK_directory_remove (tmpname)) { @@ -2710,11 +2699,29 @@ GNUNET_DISK_purge_cfg_dir (const char *cfg_filename, "remove", tmpname); GNUNET_free (tmpname); - return; + return GNUNET_OK; } GNUNET_free (tmpname); + return GNUNET_OK; } +/** + * Remove the directory given under @a option in + * section [PATHS] in configuration under @a cfg_filename + * + * @param cfg_filename configuration file to parse + * @param option option with the dir name to purge + */ +void +GNUNET_DISK_purge_cfg_dir (const char *cfg_filename, + const char *option) +{ + GNUNET_break (GNUNET_OK == + GNUNET_CONFIGURATION_parse_and_run (cfg_filename, + &purge_cfg_dir, + (void *) option)); +} + /* end of disk.c */ diff --git a/src/util/service.c b/src/util/service.c index ea34abc6c..b61168570 100644 --- a/src/util/service.c +++ b/src/util/service.c @@ -1815,8 +1815,9 @@ GNUNET_SERVICE_run_ (int argc, opt_cfg_filename = GNUNET_strdup (cfg_filename); if (GNUNET_YES == GNUNET_DISK_file_test (opt_cfg_filename)) { - if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, - opt_cfg_filename)) + if (GNUNET_SYSERR == + GNUNET_CONFIGURATION_load (cfg, + opt_cfg_filename)) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Malformed configuration file `%s', exit ...\n"), @@ -1826,8 +1827,9 @@ GNUNET_SERVICE_run_ (int argc, } else { - if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, - NULL)) + if (GNUNET_SYSERR == + GNUNET_CONFIGURATION_load (cfg, + NULL)) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Malformed configuration, exit ...\n")); -- cgit v1.2.3