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;
56 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 memset(&ptr, 0, sizeof(ptr));
110 ptr.package = pkg->e.name;
111 ptr.section = sct->e.name;
114 if (uci_lookup_ptr(ctx, &ptr, NULL, false) == UCI_OK) {
119 log_debug("Created configuration section %s to configuration file: %s\n", section_name, fname);
122 log_error("Could not write to configuration file: %s\n. Could not look-up pointer for package %s section %s.\n", fname, pkg->e.name, sct->e.name);
124 uci_free_context(ctx);
127 log_error("Could not write to configuration file: %s. Could not create section %s.\n", fname, section_name);
131 log_error("Could not write to configuration file: %s. File does not exist or is not writable\n.", fname);
137 log_error("Could not change config value, out of memory\n");
141 log_error("Could not change config value, invalid parameters\n");