]> pilppa.com Git - libplp.git/commitdiff
named sections can be done also without adding new api function
authorMika Laitio <lamikr@pilppa.org>
Sun, 13 Mar 2011 23:45:15 +0000 (01:45 +0200)
committerMika Laitio <lamikr@pilppa.org>
Sun, 13 Mar 2011 23:45:15 +0000 (01:45 +0200)
Signed-off-by: Mika Laitio <lamikr@pilppa.org>
src/config.c
src_test/Makefile.am
src_test/test_config2.c [new file with mode: 0644]

index 074604aa04ef6a9dd0c35b2fe48dc3a4e5c64a25..c099d97fbe022b89c12b0549667210427b38c987 100644 (file)
@@ -7,6 +7,36 @@
 #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);
+               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);
+                       }
+               }
+       }
+       return ret_val;
+}
+
 bool set_config_value(const char *conf_dir_name,
                        const char *conf_file_name,
                        const char *section_type,
@@ -60,10 +90,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);
index e331d77c58e3a7a8df8fbc7cdfb0884fbc55e993..f8909e60e513e60389e05735f4424b0053b880be 100644 (file)
@@ -1,4 +1,6 @@
-noinst_PROGRAMS = test_logs test_config
+noinst_PROGRAMS = test_logs \
+               test_config \
+               test_config2
 
 test_logs_SOURCES = test_logs.c
 test_logs_LDADD = $(SRC_LIBS) ../src/.libs/libplp.a
@@ -6,6 +8,9 @@ test_logs_LDADD = $(SRC_LIBS) ../src/.libs/libplp.a
 test_config_SOURCES = test_config.c
 test_config_LDADD = $(SRC_LIBS) ../src/.libs/libplp.a
 
+test_config2_SOURCES = test_config2.c
+test_config2_LDADD = $(SRC_LIBS) ../src/.libs/libplp.a
+
 AM_CPPFLAGS = $(SRC_CFLAGS) -I../src
 
 DISTCLEANFILES = Makefile.in
\ No newline at end of file
diff --git a/src_test/test_config2.c b/src_test/test_config2.c
new file mode 100644 (file)
index 0000000..2bf367b
--- /dev/null
@@ -0,0 +1,210 @@
+/*
+ * test_w1.cc
+ *
+ *  Created on: Oct 20, 2010
+ *      Author: lamikr
+ */
+#include <string.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <uci.h>
+
+#include "log.h"
+#include "config.h"
+
+/*
+static int uci_do_add(int argc, char **argv)
+{
+       struct uci_package *p = NULL;
+       struct uci_section *s = NULL;
+       int ret;
+
+       if (argc != 3)
+               return 255;
+
+       ret = uci_load(ctx, argv[1], &p);
+       if (ret != UCI_OK)
+               goto done;
+
+       ret = uci_add_section(ctx, p, argv[2], &s);
+       if (ret != UCI_OK)
+               goto done;
+
+       ret = uci_save(ctx, p);
+
+done:
+       if (ret != UCI_OK)
+               cli_perror();
+       else if (s)
+               fprintf(stdout, "%s\n", s->e.name);
+       return ret;
+}
+*/
+
+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);
+               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);
+                       }
+               }
+       }
+       return ret_val;
+}
+
+bool set_config_value2(const char *conf_dir_name,
+                       const char *conf_file_name,
+                       const char *section_type,
+                       const char *section_name,
+                       const char *key,
+                       const char *value) {
+       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      *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) &&
+           (section_name != NULL) &&
+           (key != NULL) &&
+           (value != NULL)) {
+               b_count = strlen(conf_dir_name) + strlen(conf_file_name) + 10;
+               fname   = (char *)calloc(1, b_count);
+               if (fname != NULL) {
+                       strncpy(fname, conf_dir_name, b_count);
+                       strncat(fname, "/", 1);
+                       strncat(fname, conf_file_name, strlen(conf_file_name) + 1);
+                       ctx     = uci_alloc_context();
+                       if (ctx != NULL) {
+                               sct     = NULL;
+                               uci_set_confdir(ctx, conf_dir_name);
+                               if (access(fname, W_OK) != 0) {
+                                       if (access(fname, F_OK) != 0) {
+                                               fp      = fopen(fname, "w+");
+                                               fclose(fp);
+                                       }
+                               }
+                               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;
+                                               }
+                                       }
+                                       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_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);
+                                               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) {
+                                                               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 {
+                                       log_error("Could not write to configuration file: %s\n. File does not exist or is not writable.", fname);
+                               }
+                       }
+                       free(fname);
+               }
+               else {
+                       log_error("Could not change config value, out of memory");
+               }
+       }
+       else {
+               log_error("Could not change config value, invalid parameters");
+       }
+       return ret_val;
+}
+
+void test_config() {
+       char work_dir[FILENAME_MAX];
+
+       getcwd(work_dir, sizeof(work_dir));
+       printf("working directory: %s\n", work_dir);
+
+       set_config_value2(work_dir,
+                       "dev_cfg_txt",
+                       "mysection_type",
+                       "mysection_name",
+                       "myoption_name",
+                       "my_option_value");
+       set_config_value2(work_dir,
+                       "dev_cfg_txt",
+                       "mysection_type",
+                       "mysection_name",
+                       "myoption_name",
+                       "my_option_value2");
+}
+
+int main(int argc, char** argv) {
+       test_config();
+       return 0;
+}