From 5a43663e66f9fb8f6f246b1f02efd3ae59b46d06 Mon Sep 17 00:00:00 2001 From: Mika Laitio Date: Mon, 24 Jan 2011 21:34:30 +0200 Subject: [PATCH] cleanups and error handling for set_config_value method. Signed-off-by: Mika Laitio --- src/config.c | 80 +++++++++++++++++++++++++++------------------------- 1 file changed, 42 insertions(+), 38 deletions(-) diff --git a/src/config.c b/src/config.c index 74bfd8b..074604a 100644 --- a/src/config.c +++ b/src/config.c @@ -22,7 +22,9 @@ bool set_config_value(const char *conf_dir_name, char *fname; int b_count; bool save; - struct uci_element *e; + struct uci_element *elem; + struct uci_ptr ptr; + FILE *fp; bool ret_val; ret_val = false; @@ -45,53 +47,55 @@ bool set_config_value(const char *conf_dir_name, uci_set_confdir(ctx, conf_dir_name); if (access(fname, W_OK) != 0) { if (access(fname, F_OK) != 0) { - FILE *fp; fp = fopen(fname, "w+"); fclose(fp); } } - err_flg = uci_load(ctx, fname, &pkg); - uci_foreach_element(&pkg->sections, e) { - tmp_sct = uci_to_section(e); - if (strcmp(tmp_sct->type, section_type) == 0) { - sct = tmp_sct; - break; + if (access(fname, W_OK) == 0) { + err_flg = uci_load(ctx, fname, &pkg); + uci_foreach_element(&pkg->sections, elem) { + tmp_sct = uci_to_section(elem); + if (strcmp(tmp_sct->type, section_type) == 0) { + sct = tmp_sct; + break; + } } - } - //sct = uci_lookup_section(ctx, pkg, "service"); - if (sct != NULL) { - log_debug("section found\n"); - } - else { - log_debug("adding new section\n"); - err_flg = uci_add_named_section(ctx, pkg, section_type, section_name, &sct); - log_debug("err_flg: %d, section name: %s\n", err_flg, sct->e.name); - } - opt = uci_lookup_option(ctx, sct, key); - if (opt != NULL) { - struct uci_ptr ptr; - memset(&ptr, 0, sizeof(ptr)); - ptr.package = pkg->e.name; - ptr.section = sct->e.name; - ptr.option = key; + //sct = uci_lookup_section(ctx, pkg, "service"); + if (sct == NULL) { + log_debug("Creating configuration section %s to configuration file: %s\n", section_name, fname); + err_flg = uci_add_named_section(ctx, pkg, section_type, section_name, &sct); + } + if (err_flg == 0) { + opt = uci_lookup_option(ctx, sct, key); + if (opt != NULL) { + memset(&ptr, 0, sizeof(ptr)); + ptr.package = pkg->e.name; + ptr.section = sct->e.name; + ptr.option = key; - if (uci_lookup_ptr(ctx, &ptr, NULL, false) == UCI_OK) { - log_debug("trying to change option value\n"); - ptr.value = strdup(value); - uci_set(ctx, &ptr); - save = true; + if (uci_lookup_ptr(ctx, &ptr, NULL, false) == UCI_OK) { + ptr.value = strdup(value); + uci_set(ctx, &ptr); + save = true; + } + } + else { + opt = uci_alloc_option(sct, key, value); + save = true; + } + if (save == true) { + uci_save(ctx, pkg); + } + uci_free_context(ctx); + ret_val = true; + } + else { + log_error("Could not write to configuration file: %s\n. Could not create section %s.", fname, section_name); } } else { - opt = uci_alloc_option(sct, key, value); - save = true; - } - if (save == true) { - log_debug("saving config file: %s\n", fname); - uci_save(ctx, pkg); + log_error("Could not write to configuration file: %s\n. File does not exist or is not writable.", fname); } - uci_free_context(ctx); - ret_val = true; } free(fname); } -- 2.41.0