10 static int uci_create_named_section(struct uci_context *ctx,
11 const char *conf_file_name,
12 const char *section_type,
13 const char *section_name)
22 (conf_file_name != NULL) &&
23 (section_type != NULL) &&
24 (section_name != NULL)) {
25 len = strlen(conf_file_name);
27 len = len + strlen(section_type);
29 len = len + strlen(section_name);
31 cmd_data = malloc(len);
32 if (cmd_data != NULL) {
33 snprintf(cmd_data, len, "%s.%s=%s", conf_file_name, section_name, section_type);
34 if (uci_lookup_ptr(ctx, &ptr, cmd_data, true) == UCI_OK) {
35 ret_val = uci_set(ctx, &ptr);
36 if (ret_val == UCI_OK) {
37 ret_val = uci_save(ctx, ptr.p);
46 bool set_config_value_to_section(const char *conf_dir_name,
47 const char *conf_file_name,
48 const char *section_type,
49 const char *section_name,
52 struct uci_context *ctx;
53 struct uci_package *pkg;
54 struct uci_section *sct;
55 struct uci_section *tmp_sct;
59 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 new 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,
104 if (err_flg == UCI_OK) {
105 uci_foreach_element(&pkg->sections, elem) {
106 tmp_sct = uci_to_section(elem);
107 if (strcmp(tmp_sct->type, section_type) == 0) {
114 if (err_flg == UCI_OK) {
115 memset(&ptr, 0, sizeof(ptr));
116 ptr.package = pkg->e.name;
117 ptr.section = sct->e.name;
119 err_flg = uci_lookup_ptr(ctx, &ptr, NULL, false);
120 if (err_flg == UCI_OK) {
122 err_flg = uci_set(ctx, &ptr);
123 if (err_flg == UCI_OK) {
124 err_flg = uci_save(ctx, pkg);
125 if (err_flg == UCI_OK) {
127 log_debug("Set value to section %s in configuration file: %s\n", section_name, fname);
130 log_error("Failed to set value to configuration file: %s\n. Could not save the file\n", fname);
135 log_error("Failed to set value to configuration file: %s\n. Could not set new value\n", fname);
139 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);
141 uci_free_context(ctx);
144 log_error("Failed to set value to configuration file: %s. Could not create section %s.\n", fname, section_name);
148 log_error("Failed to set value to configuration file: %s. File does not exist or is not writable\n.", fname);
154 log_error("Failed to set value to configuration file: %s, out of memory\n", fname);
158 log_error("Failed to set value to configuration file, invalid parameters\n");