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 --- src/util/disk.c | 51 +++++++++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 22 deletions(-) (limited to 'src/util/disk.c') 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 */ -- cgit v1.2.3