From ae8cb91d9961899075a892a3110204bc139c2eb6 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Thu, 5 May 2016 15:10:48 +0000 Subject: [PATCH] add -w option to gnunet-config --- doc/man/gnunet-config.1 | 4 +- src/util/gnunet-config.c | 85 +++++++++++++++++++++++++++++++--------- 2 files changed, 69 insertions(+), 20 deletions(-) diff --git a/doc/man/gnunet-config.1 b/doc/man/gnunet-config.1 index 6b4b82001..d309e7fa0 100644 --- a/doc/man/gnunet-config.1 +++ b/doc/man/gnunet-config.1 @@ -22,6 +22,9 @@ Which configuration section should be accessed or edited. Required option. .IP "\-S, \-\-list\-sections" List available configuration sections for use with \-\-section. .B +.IP "\-W, \-\-rewrite" +Consider differences to defaults only. +.B .IP "\-o OPTION, \-\-option=OPTION" Which configuration option should be accessed or edited. Required to set a value. If not given, all values of a given section will be printed in the format "OPTION = VALUE". .B @@ -43,4 +46,3 @@ Print GNUnet version number. .SH BUGS Report bugs by using Mantis or by sending electronic mail to - diff --git a/src/util/gnunet-config.c b/src/util/gnunet-config.c index 1c6205334..7ec7162f1 100644 --- a/src/util/gnunet-config.c +++ b/src/util/gnunet-config.c @@ -57,6 +57,11 @@ static int list_sections; */ static int ret; +/** + * Should we generate a configuration file that is clean and + * only contains the deltas to the defaults? + */ +static int rewrite; /** * Print each option in a given section. @@ -99,41 +104,70 @@ print_section_name (void *cls, * @param cfg configuration */ static void -run (void *cls, char *const *args, const char *cfgfile, +run (void *cls, + char *const *args, + const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg) { - struct GNUNET_CONFIGURATION_Handle *out; + struct GNUNET_CONFIGURATION_Handle *out = NULL; + struct GNUNET_CONFIGURATION_Handle *diff = NULL; - if (NULL == section || list_sections) + if (rewrite) + { + struct GNUNET_CONFIGURATION_Handle *def; + + def = GNUNET_CONFIGURATION_create (); + if (GNUNET_OK != + GNUNET_CONFIGURATION_load (def, NULL)) + { + fprintf (stderr, + _("failed to load configuration defaults")); + ret = 1; + return; + } + diff = GNUNET_CONFIGURATION_get_diff (def, + cfg); + cfg = diff; + } + if ( ((! rewrite) && (NULL == section)) || list_sections) { if (! list_sections) { - fprintf (stderr, _("--section argument is required\n")); + fprintf (stderr, + _("--section argument is required\n")); } - fprintf (stderr, _("The following sections are available:\n")); - GNUNET_CONFIGURATION_iterate_sections (cfg, &print_section_name, NULL); + fprintf (stderr, + _("The following sections are available:\n")); + GNUNET_CONFIGURATION_iterate_sections (cfg, + &print_section_name, + NULL); ret = 1; - return; + goto cleanup; } - if (NULL == value) + if ( (NULL != section) && (NULL == value) ) { if (NULL == option) { - GNUNET_CONFIGURATION_iterate_section_values (cfg, section, - &print_option, NULL); + GNUNET_CONFIGURATION_iterate_section_values (cfg, + section, + &print_option, + NULL); } else { if (is_filename) { if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_filename (cfg, section, option, &value)) + GNUNET_CONFIGURATION_get_value_filename (cfg, + section, + option, + &value)) { GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, section, option); ret = 3; - return; + goto cleanup; } } else @@ -144,28 +178,38 @@ run (void *cls, char *const *args, const char *cfgfile, GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, section, option); ret = 3; - return; + goto cleanup; } } fprintf (stdout, "%s\n", value); } } - else + else if (NULL != section) { if (NULL == option) { fprintf (stderr, _("--option argument required to set value\n")); ret = 1; - return; + goto cleanup; } out = GNUNET_CONFIGURATION_dup (cfg); - GNUNET_CONFIGURATION_set_value_string (out, section, option, value); + GNUNET_CONFIGURATION_set_value_string (out, + section, + option, + value); + } + if ( (NULL != diff) || (NULL != out) ) + { if (GNUNET_OK != - GNUNET_CONFIGURATION_write (out, cfgfile)) + GNUNET_CONFIGURATION_write ((NULL == out) ? diff : out, + cfgfile)) ret = 2; - GNUNET_CONFIGURATION_destroy (out); - return; } + if (NULL != out) + GNUNET_CONFIGURATION_destroy (out); + cleanup: + if (NULL != diff) + GNUNET_CONFIGURATION_destroy (diff); } @@ -195,6 +239,9 @@ main (int argc, char *const *argv) { 'S', "list-sections", NULL, gettext_noop ("print available configuration sections"), 0, &GNUNET_GETOPT_set_one, &list_sections }, + { 'w', "rewrite", NULL, + gettext_noop ("write configuration file that only contains delta to defaults"), + 0, &GNUNET_GETOPT_set_one, &rewrite }, GNUNET_GETOPT_OPTION_END }; if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) -- 2.25.1