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 if (cmd_data != NULL) {
30 snprintf(cmd_data, len, "%s.%s=%s", conf_file_name, section_name, section_type);
31 if (uci_lookup_ptr(ctx, &ptr, cmd_data, true) == UCI_OK) {
32 ret_val = uci_set(ctx, &ptr);
33 if (ret_val == UCI_OK) {
34 //ret_val = uci_save(ctx, ptr.p);
43 bool set_config_value(const char *conf_dir_name,
44 const char *conf_file_name,
45 const char *section_type,
46 const char *section_name,
49 struct uci_context *ctx;
50 struct uci_package *pkg;
51 struct uci_section *sct;
52 struct uci_section *tmp_sct;
53 struct uci_option *opt;
58 struct uci_element *elem;
65 if ((conf_dir_name != NULL) &&
66 (conf_file_name != NULL) &&
67 (section_type != NULL) &&
68 (section_name != NULL) &&
71 b_count = strlen(conf_dir_name) + strlen(conf_file_name) + 10;
72 fname = (char *)calloc(1, b_count);
74 strncpy(fname, conf_dir_name, b_count);
75 strncat(fname, "/", 1);
76 strncat(fname, conf_file_name, strlen(conf_file_name) + 1);
77 ctx = uci_alloc_context();
80 uci_set_confdir(ctx, conf_dir_name);
81 if (access(fname, W_OK) != 0) {
82 if (access(fname, F_OK) != 0) {
83 fp = fopen(fname, "w+");
87 if (access(fname, W_OK) == 0) {
88 err_flg = uci_load(ctx, fname, &pkg);
89 uci_foreach_element(&pkg->sections, elem) {
90 tmp_sct = uci_to_section(elem);
91 if (strcmp(tmp_sct->type, section_type) == 0) {
97 log_debug("Creating configuration section %s to configuration file: %s\n", section_name, fname);
98 //err_flg = uci_add_named_section(ctx, pkg, section_type, section_name, &sct);
99 //err_flg = uci_add_section(ctx, pkg, section_name, &sct);
100 err_flg = uci_create_named_section(ctx, conf_file_name, section_type, section_name);
101 if (err_flg == UCI_OK) {
102 uci_foreach_element(&pkg->sections, elem) {
103 tmp_sct = uci_to_section(elem);
104 if (strcmp(tmp_sct->type, section_type) == 0) {
112 opt = uci_lookup_option(ctx, sct, key);
114 memset(&ptr, 0, sizeof(ptr));
115 ptr.package = pkg->e.name;
116 ptr.section = sct->e.name;
119 if (uci_lookup_ptr(ctx, &ptr, NULL, false) == UCI_OK) {
126 opt = uci_alloc_option(sct, key, value);
132 uci_free_context(ctx);
136 log_error("Could not write to configuration file: %s\n. Could not create section %s.", fname, section_name);
140 log_error("Could not write to configuration file: %s\n. File does not exist or is not writable.", fname);
146 log_error("Could not change config value, out of memory");
150 log_error("Could not change config value, invalid parameters");