- }
- //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 (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);
+ 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) {
+ err_flg = uci_save(ctx, pkg);
+ 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 save the file\n", fname);
+ }