]> pilppa.org Git - libplp.git/blobdiff - src/private/uci_config.c
api fixes and cleanups
[libplp.git] / src / private / uci_config.c
diff --git a/src/private/uci_config.c b/src/private/uci_config.c
new file mode 100644 (file)
index 0000000..756f2b7
--- /dev/null
@@ -0,0 +1,217 @@
+/*
+ * uci_config.c
+ *
+ *  Created on: Aug 2, 2012
+ *      Author: lamikr
+ */
+
+#include "uci_config.h"
+#include "../log.h"
+
+int uci_create_named_section(struct uci_context *ctx,
+                               const char *conf_fname_base,
+                               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_fname_base != NULL) &&
+           (section_type != NULL) &&
+           (section_name != NULL)) {
+               len             = strlen(conf_fname_base);
+               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_fname_base,
+                               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 uci_set_config_value(struct uci_context *ctx,
+                       struct uci_package *pkg,
+                       const char *conf_fname_base,
+                       const char *conf_fname_full,
+                       const char *section_type,
+                       const char *section_name,
+                       const char *key,
+                       const char *value,
+                       const bool save_immediately) {
+       struct uci_section      *sct;
+       struct uci_section      *tmp_sct;
+       int                     err_flg;
+       struct uci_element      *elem;
+       struct uci_ptr          ptr;
+       bool                    ret_val;
+
+       ret_val = false;
+       err_flg = UCI_OK;
+       if ((ctx != NULL) &&
+           (pkg != NULL) &&
+           (section_type != NULL) &&
+           (section_name != NULL) &&
+           (key != NULL) &&
+           (value != NULL)) {
+               sct     = NULL;
+               uci_foreach_element(&pkg->sections, elem) {
+                       tmp_sct = uci_to_section(elem);
+                       if (tmp_sct != NULL) {
+                               if (strcmp(tmp_sct->type, section_type) == 0) {
+                                       sct     = tmp_sct;
+                                       break;
+                               }
+                               else {
+                                       //TODO: uci_free_section(tmp_sct); ?, or will uci_free_contect(ctx) also free the section
+                               }
+                       }
+               }
+               if (sct == NULL) {
+                       //log_debug("Creating new section %s to configuration file: %s\n", section_name, conf_fname_full);
+                       //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_fname_base,
+                                               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;
+                                       }
+                               }
+                       }
+                       //TODO: uci_free_section(sct); ?, or will uci_free_contect(ctx) also free the section
+               }
+               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);
+                               log_debug("file: %s, section_key: %s/%s: value: %s.\n",
+                                       conf_fname_full,
+                                       section_name,
+                                       key,
+                                       value);
+                               if (err_flg == UCI_OK) {
+                                       if (save_immediately == true) {
+                                               err_flg = uci_save(ctx, pkg);
+                                               if (err_flg == UCI_OK) {
+                                                       ret_val = true;
+                                               }
+                                               else {
+                                                       log_error("Failed to set value to configuration file: %s. Could not save the file.\n", conf_fname_full);
+                                               }
+                                       }
+                                       else {
+                                               ret_val = true;
+                                       }
+                               }
+                               else {
+                                       log_error("Failed to set value to configuration file: %s. Could not set new value.\n", conf_fname_full);
+                               }
+                       }
+                       else {
+                               log_error("Failed to set value to configuration file: %s. Could not look-up pointer for package %s section %s.\n",
+                                       conf_fname_full,
+                                       pkg->e.name,
+                                       sct->e.name);
+                       }
+               }
+               else {
+                       log_error("Failed to set value to configuration file: %s. Could not create section %s.\n", conf_fname_full, section_name);
+               }
+       }
+       else {
+               log_error("Failed to set value to configuration file, invalid parameters\n");
+       }
+       return ret_val;
+}
+
+char *uci_get_config_value(struct uci_context *ctx,
+                       struct uci_package *pkg,
+                       const char *section_name,
+                       const char *key_name) {
+       struct uci_section      *section;
+       struct uci_option       *option;
+       char                    *ret_val;
+
+       ret_val = NULL;
+       if ((ctx != NULL) &&
+           (pkg != NULL)) {
+               section = uci_lookup_section(ctx,
+                                       pkg,
+                                       section_name);
+               if (section != NULL) {
+                       option  = uci_lookup_option(ctx,
+                                               section,
+                                               key_name);
+                       if (option != NULL) {
+                               switch (option->type) {
+                                       case UCI_TYPE_STRING:
+                                               //log_debug("key: %s option name: %s, value: %s\n", key.c_str(), option->e.name, option->v.string);
+                                               ret_val = option->v.string;
+                                               break;
+                                       default:
+                                               log_error("Failed to read configuration value for key: %s\n", key_name);
+                                               break;
+                               }
+                       }
+                       else {
+                               log_error("Failed to find configuration key: %s\n", key_name);
+                       }
+               }
+               else {
+                       log_error("Failed to find configuration section name: %s\n", section_name);
+               }
+       }
+       return ret_val;
+}
+
+bool uci_save_config_values(struct uci_context *ctx,
+                       struct uci_package *pkg) {
+       bool    ret_val;
+       int     err_flg;
+
+       if ((ctx != NULL) &&
+           (pkg != NULL)) {
+               err_flg = uci_save(ctx, pkg);
+               if (err_flg == UCI_OK) {
+                       ret_val = true;
+               }
+               else {
+                       log_error("Could not save configuration file.\n");
+               }
+       }
+       return ret_val;
+}