+ free(short_fname);
+ free(full_fname);
+}
+
+static int uci_create_named_section(ConfigHandle *cfg_handle,
+ const char *section_type,
+ const char *section_name,
+ bool save_immediately)
+{
+ struct uci_ptr ptr;
+ int ret_val;
+ char *cmd_dta;
+ int len;
+
+ ret_val = -1;
+ if ((cfg_handle != NULL) &&
+ (cfg_handle->_ctx != NULL) &&
+ (cfg_handle->short_fname != NULL) &&
+ (cfg_handle->full_fname != NULL) &&
+ (section_type != NULL) &&
+ (section_name != NULL)) {
+ len = strlen(cfg_handle->short_fname);
+ len = len + 1;
+ len = len + strlen(section_type);
+ len = len + 1;
+ len = len + strlen(section_name);
+ len = len + 1;
+ cmd_dta = (char *)malloc(len);
+ if (cmd_dta != NULL) {
+ snprintf(cmd_dta,
+ len,
+ "%s.%s=%s",
+ cfg_handle->short_fname,
+ section_name,
+ section_type);
+ if (uci_lookup_ptr(cfg_handle->_ctx, &ptr, cmd_dta, true) == UCI_OK) {
+ ret_val = uci_set(cfg_handle->_ctx, &ptr);
+ if (ret_val == UCI_OK) {
+ if (save_immediately) {
+ ret_val = uci_save(cfg_handle->_ctx, ptr.p);
+ }
+ }
+ }
+ free(cmd_dta);
+ }
+ }
+ return ret_val;
+}
+
+static bool set_config_value_to_section(ConfigHandle *cfg_handle,
+ const char *section_type,
+ const char *section_name,
+ const char *key,
+ const char *value) {
+ 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;
+ if ((cfg_handle != NULL) &&
+ (cfg_handle->_ctx != NULL) &&
+ (cfg_handle->_pkg != NULL) &&
+ (section_type != NULL) &&
+ (section_name != NULL) &&
+ (key != NULL) &&
+ (value != NULL)) {
+ sct = NULL;
+ uci_foreach_element(&cfg_handle->_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 new section %s to configuration file: %s\n", section_name, cfg_handle->full_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(cfg_handle,
+ section_type,
+ section_name,
+ false);
+ if (err_flg == UCI_OK) {
+ uci_foreach_element(&cfg_handle->_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 = cfg_handle->_pkg->e.name;
+ ptr.section = sct->e.name;
+ ptr.option = key;
+ err_flg = uci_lookup_ptr(cfg_handle->_ctx, &ptr, NULL, false);
+ if (err_flg == UCI_OK) {
+ ptr.value = value;
+ err_flg = uci_set(cfg_handle->_ctx, &ptr);
+ if (err_flg == UCI_OK) {
+ err_flg = uci_save(cfg_handle->_ctx, cfg_handle->_pkg);
+ if (err_flg == UCI_OK) {
+ ret_val = true;
+ log_debug("Set value to section %s in configuration file: %s\n", section_name, cfg_handle->full_fname);
+ }
+ else {
+ log_error("Failed to set value to configuration file: %s\n. Could not save the file\n", cfg_handle->full_fname);
+ }
+ }
+ else {
+ log_error("Failed to set value to configuration file: %s\n. Could not set new value\n", cfg_handle->full_fname);
+ }
+ }
+ else {
+ log_error("Failed to set value to configuration file: %s\n. Could not look-up pointer for package %s section %s.\n",
+ cfg_handle->full_fname,
+ cfg_handle->_pkg->e.name,
+ sct->e.name);
+ }
+ uci_free_context(cfg_handle->_ctx);
+ }
+ else {
+ log_error("Failed to set value to configuration file: %s. Could not create section %s.\n", cfg_handle->full_fname, section_name);
+ }
+ }
+ else {
+ log_error("Failed to set value to configuration file, invalid parameters\n");
+ }
+ return ret_val;