]> pilppa.org Git - libplp.git/blobdiff - src/config.c
Updates to set_config_value parameter
[libplp.git] / src / config.c
index 74bfd8bd15a07fa3605570a1cd588da46eb7f99e..92c8df303875424f5e455f9203f20a2006be875f 100644 (file)
@@ -3,30 +3,70 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <uci.h>
+#include <stdbool.h>
 
 #include "log.h"
 #include "config.h"
 
-bool set_config_value(const char *conf_dir_name,
+static int uci_create_named_section(struct uci_context *ctx,
+                               const char *conf_file_name,
+                               const char *section_type,
+                               const char *section_name,
+                               bool save_immediately)
+{
+       struct uci_ptr  ptr;
+       int             ret_val;
+       char            *cmd_data;
+       int             len;
+
+       ret_val = -1;
+       if ((ctx != NULL) &&
+           (conf_file_name != NULL) &&
+           (section_type != NULL) &&
+           (section_name != NULL)) {
+               len             = strlen(conf_file_name);
+               len             = len + 1;
+               len             = len + strlen(section_type);
+               len             = len + 1;
+               len             = len + strlen(section_name);
+               len             = len + 1;
+               cmd_data        = malloc(len);
+               if (cmd_data != NULL) {
+                       snprintf(cmd_data, len, "%s.%s=%s", conf_file_name, section_name, section_type);
+                       if (uci_lookup_ptr(ctx, &ptr, cmd_data, true) == UCI_OK) {
+                               ret_val = uci_set(ctx, &ptr);
+                               if (ret_val == UCI_OK) {
+                                       if (save_immediately) {
+                                               ret_val = uci_save(ctx, ptr.p);
+                                       }
+                               }
+                       }
+                       free(cmd_data);
+               }
+       }
+       return ret_val;
+}
+
+bool set_config_value_to_section(const char *conf_dir_name,
                        const char *conf_file_name,
                        const char *section_type,
                        const char *section_name,
                        const char *key,
-                       const char *value) {
+                       const char *value,
+                       bool save_immediately) {
        struct uci_context      *ctx;
        struct uci_package      *pkg;
        struct uci_section      *sct;
        struct uci_section      *tmp_sct;
-       struct uci_option       *opt;
        int                     err_flg;
        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;
-       save    = false;
        if ((conf_dir_name != NULL) &&
            (conf_file_name != NULL) &&
            (section_type != NULL) &&
@@ -45,62 +85,84 @@ 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;
-
-                                       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 (sct == NULL) {
+                                               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,
+                                                                       false);
+                                               if (err_flg == UCI_OK) {
+                                                       uci_foreach_element(&pkg->sections, elem) {
+                                                               tmp_sct = uci_to_section(elem);
+                                                               if (strcmp(tmp_sct->type, section_type) == 0) {
+                                                                       sct     = tmp_sct;
+                                                                       break;
+                                                               }
+                                                       }
+                                               }
+                                       }
+                                       if (err_flg == UCI_OK) {
+                                               memset(&ptr, 0, sizeof(ptr));
+                                               ptr.package     = pkg->e.name;
+                                               ptr.section     = sct->e.name;
+                                               ptr.option      = key;
+                                               err_flg = uci_lookup_ptr(ctx, &ptr, NULL, false);
+                                               if (err_flg == UCI_OK) {
+                                                       ptr.value       = value;
+                                                       err_flg         = uci_set(ctx, &ptr);
+                                                       if (err_flg == UCI_OK) {
+                                                               if (save_immediately == true) {
+                                                                       err_flg = uci_save(ctx, pkg);
+                                                                       if (err_flg != UCI_OK) {
+                                                                               log_error("Failed to set value to configuration file: %s\n. Could not save the file\n", fname);
+                                                                       }
+                                                               }
+                                                               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 set new value\n", fname);
+                                                       }
+                                               }
+                                               else {
+                                                       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("Failed to set value to configuration file: %s. Could not create section %s.\n", 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("Failed to set value to configuration file: %s. File does not exist or is not writable\n.", fname);
                                }
-                               uci_free_context(ctx);
-                               ret_val = true;
                        }
                        free(fname);
                }
                else {
-                       log_error("Could not change config value, out of memory");
+                       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");
+               log_error("Failed to set value to configuration file, invalid parameters\n");
        }
        return ret_val;
 }