]> pilppa.org Git - libplp.git/blobdiff - src/config.c
revert previous save_immediately flag
[libplp.git] / src / config.c
index 4e51b43ca78db17ddc946968775567b2b6df2adf..32f0a93b63ee0d5c1b30819e69031874d44f9d65 100644 (file)
@@ -4,15 +4,18 @@
 #include <unistd.h>
 #include <uci.h>
 
-#include "log.h"
 #include "config.h"
+#include "log.h"
 
-static int uci_create_named_section(struct uci_context *ctx, const char *conf_file_name, const char *section_type, const char *section_name)
+static int uci_create_named_section(struct uci_context *ctx,
+                               const char *conf_file_name,
+                               const char *section_type,
+                               const char *section_name)
 {
-       struct uci_ptr          ptr;
-       int                     ret_val;
-       char                    *cmd_data;
-       int                     len;
+       struct uci_ptr  ptr;
+       int             ret_val;
+       char            *cmd_data;
+       int             len;
 
        ret_val = -1;
        if ((ctx != NULL) &&
@@ -31,7 +34,7 @@ static int uci_create_named_section(struct uci_context *ctx, const char *conf_fi
                        if (uci_lookup_ptr(ctx, &ptr, cmd_data, true) == UCI_OK) {
                                ret_val = uci_set(ctx, &ptr);
                                if (ret_val == UCI_OK) {
-                                       //ret_val       = uci_save(ctx, ptr.p);
+                                       ret_val = uci_save(ctx, ptr.p);
                                }
                        }
                        free(cmd_data);
@@ -40,7 +43,7 @@ static int uci_create_named_section(struct uci_context *ctx, const char *conf_fi
        return ret_val;
 }
 
-bool set_config_value(const char *conf_dir_name,
+bool set_config_value_to_section(const char *conf_dir_name,
                        const char *conf_file_name,
                        const char *section_type,
                        const char *section_name,
@@ -91,10 +94,13 @@ bool set_config_value(const char *conf_dir_name,
                                                }
                                        }
                                        if (sct == NULL) {
-                                               log_debug("Creating configuration section %s to configuration file: %s\n", section_name, fname);
+                                               log_debug("Creating new section %s to configuration file: %s\n", section_name, fname);
                                                //err_flg       = uci_add_named_section(ctx, pkg, section_type, section_name, &sct);
                                                //err_flg       = uci_add_section(ctx, pkg, section_name, &sct);
-                                               err_flg = uci_create_named_section(ctx, conf_file_name, section_type, section_name);
+                                               err_flg = uci_create_named_section(ctx,
+                                                                       conf_file_name,
+                                                                       section_type,
+                                                                       section_name);
                                                if (err_flg == UCI_OK) {
                                                        uci_foreach_element(&pkg->sections, elem) {
                                                                tmp_sct = uci_to_section(elem);
@@ -105,40 +111,51 @@ bool set_config_value(const char *conf_dir_name,
                                                        }
                                                }
                                        }
-                                       if (err_flg == 0) {
+                                       if (err_flg == UCI_OK) {
                                                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) {
+                                               err_flg = uci_lookup_ptr(ctx, &ptr, NULL, false);
+                                               if (err_flg == UCI_OK) {
                                                        ptr.value       = value;
-                                                       uci_set(ctx, &ptr);
-                                                       uci_save(ctx, pkg);
-                                                       ret_val         = true;
-                                                       log_debug("Created configuration section %s to configuration file: %s\n", section_name, fname);
+                                                       err_flg         = uci_set(ctx, &ptr);
+                                                       if (err_flg == UCI_OK) {
+                                                               err_flg = uci_save(ctx, pkg);
+                                                               if (err_flg == UCI_OK) {
+                                                                       ret_val = true;
+                                                                       log_debug("Set value to section %s in configuration file: %s\n", section_name, fname);
+                                                               }
+                                                               else {
+                                                                       log_error("Failed to set value to configuration file: %s\n. Could not save the file\n", fname);
+                                                               }
+
+                                                       }
+                                                       else {
+                                                               log_error("Failed to set value to configuration file: %s\n. Could not set new value\n", fname);
+                                                       }
                                                }
                                                else {
-                                                       log_error("Could not write to configuration file: %s\n. Could not look-up pointer for package %s section %s.\n", fname, pkg->e.name, sct->e.name);
+                                                       log_error("Failed to set value to configuration file: %s\n. Could not look-up pointer for package %s section %s.\n", fname, pkg->e.name, sct->e.name);
                                                }
                                                uci_free_context(ctx);
                                        }
                                        else {
-                                               log_error("Could not write to configuration file: %s. Could not create section %s.\n", fname, section_name);
+                                               log_error("Failed to set value to configuration file: %s. Could not create section %s.\n", fname, section_name);
                                        }
                                }
                                else {
-                                       log_error("Could not write to configuration file: %s. File does not exist or is not writable\n.", fname);
+                                       log_error("Failed to set value to configuration file: %s. File does not exist or is not writable\n.", fname);
                                }
                        }
                        free(fname);
                }
                else {
-                       log_error("Could not change config value, out of memory\n");
+                       log_error("Failed to set value to configuration file: %s, out of memory\n", fname);
                }
        }
        else {
-               log_error("Could not change config value, invalid parameters\n");
+               log_error("Failed to set value to configuration file, invalid parameters\n");
        }
        return ret_val;
 }