]> pilppa.org Git - libplp.git/blobdiff - src/config.c
memory leak fixes
[libplp.git] / src / config.c
index 074604aa04ef6a9dd0c35b2fe48dc3a4e5c64a25..df77e3b236b3660b7658a2945e82c92786365d2a 100644 (file)
@@ -7,6 +7,39 @@
 #include "log.h"
 #include "config.h"
 
+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;
+
+       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) {
+                                       //ret_val       = uci_save(ctx, ptr.p);
+                               }
+                       }
+                       free(cmd_data);
+               }
+       }
+       return ret_val;
+}
+
 bool set_config_value(const char *conf_dir_name,
                        const char *conf_file_name,
                        const char *section_type,
@@ -60,10 +93,20 @@ bool set_config_value(const char *conf_dir_name,
                                                        break;
                                                }
                                        }
-                                       //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);
+                                               //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);
+                                               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 == 0) {
                                                opt     = uci_lookup_option(ctx, sct, key);
@@ -74,7 +117,7 @@ bool set_config_value(const char *conf_dir_name,
                                                        ptr.option      = key;
 
                                                        if (uci_lookup_ptr(ctx, &ptr, NULL, false) == UCI_OK) {
-                                                               ptr.value       = strdup(value);
+                                                               ptr.value       = value;
                                                                uci_set(ctx, &ptr);
                                                                save    = true;
                                                        }