10 static int uci_create_named_section(struct uci_context *ctx, const char *conf_file_name, const char *section_type, const char *section_name)
19 (conf_file_name != NULL) &&
20 (section_type != NULL) &&
21 (section_name != NULL)) {
22 len = strlen(conf_file_name);
24 len = len + strlen(section_type);
26 len = len + strlen(section_name);
28 cmd_data = malloc(len);
29 snprintf(cmd_data, len, "%s.%s=%s", conf_file_name, section_name, section_type);
30 if (uci_lookup_ptr(ctx, &ptr, cmd_data, true) == UCI_OK) {
31 ret_val = uci_set(ctx, &ptr);
32 if (ret_val == UCI_OK) {
33 //ret_val = uci_save(ctx, ptr.p);
40 bool set_config_value(const char *conf_dir_name,
41 const char *conf_file_name,
42 const char *section_type,
43 const char *section_name,
46 struct uci_context *ctx;
47 struct uci_package *pkg;
48 struct uci_section *sct;
49 struct uci_section *tmp_sct;
50 struct uci_option *opt;
55 struct uci_element *elem;
62 if ((conf_dir_name != NULL) &&
63 (conf_file_name != NULL) &&
64 (section_type != NULL) &&
65 (section_name != NULL) &&
68 b_count = strlen(conf_dir_name) + strlen(conf_file_name) + 10;
69 fname = (char *)calloc(1, b_count);
71 strncpy(fname, conf_dir_name, b_count);
72 strncat(fname, "/", 1);
73 strncat(fname, conf_file_name, strlen(conf_file_name) + 1);
74 ctx = uci_alloc_context();
77 uci_set_confdir(ctx, conf_dir_name);
78 if (access(fname, W_OK) != 0) {
79 if (access(fname, F_OK) != 0) {
80 fp = fopen(fname, "w+");
84 if (access(fname, W_OK) == 0) {
85 err_flg = uci_load(ctx, fname, &pkg);
86 uci_foreach_element(&pkg->sections, elem) {
87 tmp_sct = uci_to_section(elem);
88 if (strcmp(tmp_sct->type, section_type) == 0) {
94 log_debug("Creating configuration section %s to configuration file: %s\n", section_name, fname);
95 //err_flg = uci_add_named_section(ctx, pkg, section_type, section_name, &sct);
96 //err_flg = uci_add_section(ctx, pkg, section_name, &sct);
97 err_flg = uci_create_named_section(ctx, conf_file_name, section_type, section_name);
98 if (err_flg == UCI_OK) {
99 uci_foreach_element(&pkg->sections, elem) {
100 tmp_sct = uci_to_section(elem);
101 if (strcmp(tmp_sct->type, section_type) == 0) {
109 opt = uci_lookup_option(ctx, sct, key);
111 memset(&ptr, 0, sizeof(ptr));
112 ptr.package = pkg->e.name;
113 ptr.section = sct->e.name;
116 if (uci_lookup_ptr(ctx, &ptr, NULL, false) == UCI_OK) {
117 ptr.value = strdup(value);
123 opt = uci_alloc_option(sct, key, value);
129 uci_free_context(ctx);
133 log_error("Could not write to configuration file: %s\n. Could not create section %s.", fname, section_name);
137 log_error("Could not write to configuration file: %s\n. File does not exist or is not writable.", fname);
143 log_error("Could not change config value, out of memory");
147 log_error("Could not change config value, invalid parameters");