11 static int uci_create_named_section(struct uci_context *ctx,
12 const char *conf_file_name,
13 const char *section_type,
14 const char *section_name,
15 bool save_immediately)
24 (conf_file_name != NULL) &&
25 (section_type != NULL) &&
26 (section_name != NULL)) {
27 len = strlen(conf_file_name);
29 len = len + strlen(section_type);
31 len = len + strlen(section_name);
33 cmd_data = malloc(len);
34 if (cmd_data != NULL) {
35 snprintf(cmd_data, len, "%s.%s=%s", conf_file_name, section_name, section_type);
36 if (uci_lookup_ptr(ctx, &ptr, cmd_data, true) == UCI_OK) {
37 ret_val = uci_set(ctx, &ptr);
38 if (ret_val == UCI_OK) {
39 if (save_immediately) {
40 ret_val = uci_save(ctx, ptr.p);
50 bool set_config_value_to_section(const char *conf_dir_name,
51 const char *conf_file_name,
52 const char *section_type,
53 const char *section_name,
56 bool save_immediately) {
57 struct uci_context *ctx;
58 struct uci_package *pkg;
59 struct uci_section *sct;
60 struct uci_section *tmp_sct;
64 struct uci_element *elem;
70 if ((conf_dir_name != NULL) &&
71 (conf_file_name != NULL) &&
72 (section_type != NULL) &&
73 (section_name != NULL) &&
76 b_count = strlen(conf_dir_name) + strlen(conf_file_name) + 10;
77 fname = (char *)calloc(1, b_count);
79 strncpy(fname, conf_dir_name, b_count);
80 strncat(fname, "/", 1);
81 strncat(fname, conf_file_name, strlen(conf_file_name) + 1);
82 ctx = uci_alloc_context();
85 uci_set_confdir(ctx, conf_dir_name);
86 if (access(fname, W_OK) != 0) {
87 if (access(fname, F_OK) != 0) {
88 fp = fopen(fname, "w+");
92 if (access(fname, W_OK) == 0) {
93 err_flg = uci_load(ctx, fname, &pkg);
94 uci_foreach_element(&pkg->sections, elem) {
95 tmp_sct = uci_to_section(elem);
96 if (strcmp(tmp_sct->type, section_type) == 0) {
102 log_debug("Creating new section %s to configuration file: %s\n", section_name, fname);
103 //err_flg = uci_add_named_section(ctx, pkg, section_type, section_name, &sct);
104 //err_flg = uci_add_section(ctx, pkg, section_name, &sct);
105 err_flg = uci_create_named_section(ctx,
110 if (err_flg == UCI_OK) {
111 uci_foreach_element(&pkg->sections, elem) {
112 tmp_sct = uci_to_section(elem);
113 if (strcmp(tmp_sct->type, section_type) == 0) {
120 if (err_flg == UCI_OK) {
121 memset(&ptr, 0, sizeof(ptr));
122 ptr.package = pkg->e.name;
123 ptr.section = sct->e.name;
125 err_flg = uci_lookup_ptr(ctx, &ptr, NULL, false);
126 if (err_flg == UCI_OK) {
128 err_flg = uci_set(ctx, &ptr);
129 if (err_flg == UCI_OK) {
130 if (save_immediately == true) {
131 err_flg = uci_save(ctx, pkg);
132 if (err_flg != UCI_OK) {
133 log_error("Failed to set value to configuration file: %s\n. Could not save the file\n", fname);
136 if (err_flg == UCI_OK) {
138 log_debug("Set value to section %s in configuration file: %s\n", section_name, fname);
142 log_error("Failed to set value to configuration file: %s\n. Could not set new value\n", fname);
146 log_error("Failed to set value to configuration file: %s\n. Could not look-up pointer for package %s section %s.\n", fname, pkg->e.name, sct->e.name);
148 uci_free_context(ctx);
151 log_error("Failed to set value to configuration file: %s. Could not create section %s.\n", fname, section_name);
155 log_error("Failed to set value to configuration file: %s. File does not exist or is not writable\n.", fname);
161 log_error("Failed to set value to configuration file: %s, out of memory\n", fname);
165 log_error("Failed to set value to configuration file, invalid parameters\n");