7 #include "private/uci_config.h"
11 bool set_config_value_and_save(const char *conf_dir_name,
12 const char *conf_file_basename,
13 const char *section_type,
14 const char *section_name,
17 struct uci_context *ctx;
18 struct uci_package *pkg;
20 char *conf_fname_full;
27 if ((conf_dir_name != NULL) &&
28 (conf_file_basename != NULL) &&
29 (section_type != NULL) &&
30 (section_name != NULL) &&
33 b_count = strlen(conf_dir_name) + strlen(conf_file_basename) + 10;
34 conf_fname_full = (char *)calloc(1, b_count);
35 if (conf_fname_full != NULL) {
36 strncpy(conf_fname_full, conf_dir_name, b_count);
37 strncat(conf_fname_full, "/", 1);
38 strncat(conf_fname_full, conf_file_basename, strlen(conf_file_basename) + 1);
39 ctx = uci_alloc_context();
41 if (access(conf_fname_full, W_OK) != 0) {
42 // if file is not writable, try to change it to be writable
43 if (access(conf_fname_full, F_OK) != 0) {
44 fp = fopen(conf_fname_full, "w+");
48 if (access(conf_fname_full, W_OK) == 0) {
51 err_flg = uci_load(ctx, conf_fname_full, &pkg);
52 if ((err_flg == UCI_OK) &&
54 ret_val = uci_set_config_value(ctx,
65 log_error("Failed to set value to configuration file: %s. Could not load current file content.\n", conf_fname_full);
69 log_error("Failed to set value to configuration file: %s. File does not exist or is not writable\n", conf_fname_full);
71 //TODO: uci_free_package(pkg) ?, or will uci_free_contect(ctx) also free the package
72 uci_free_context(ctx);
74 free(conf_fname_full);
77 log_error("Failed to set value to configuration file: %s/%s, out of memory\n", conf_dir_name, conf_file_basename);
81 log_error("Failed to set value to configuration file, invalid parameters\n");
86 char* get_config_value_and_close(const char *conf_dir_name,
87 const char *conf_file_basename,
88 const char *section_name,
90 struct uci_context *ctx;
91 struct uci_package *pkg;
93 char *conf_fname_full;
100 if ((conf_dir_name != NULL) &&
101 (conf_file_basename != NULL) &&
102 (section_name != NULL) &&
104 b_count = strlen(conf_dir_name) + strlen(conf_file_basename) + 10;
105 conf_fname_full = (char *)calloc(1, b_count);
106 if (conf_fname_full != NULL) {
107 strncpy(conf_fname_full, conf_dir_name, b_count);
108 strncat(conf_fname_full, "/", 1);
109 strncat(conf_fname_full, conf_file_basename, strlen(conf_file_basename) + 1);
110 ctx = uci_alloc_context();
112 if (access(conf_fname_full, R_OK) != 0) {
113 // if file is not writable, try to change it to be writable
114 if (access(conf_fname_full, R_OK) != 0) {
115 fp = fopen(conf_fname_full, "w+");
119 if (access(conf_fname_full, R_OK) == 0) {
122 err_flg = uci_load(ctx, conf_fname_full, &pkg);
123 if ((err_flg == UCI_OK) &&
125 ret_val = uci_get_config_value(ctx,
130 // need to duplicate response val, as uci_free_context() would free the value otherwise
132 ret_val = strdup(ret_val);
135 log_error("Failed to get value from configuration file: %s. Could not load current file content.\n", conf_fname_full);
139 log_error("Failed to get value from configuration file: %s. File does not exist or is not readable.\n", conf_fname_full);
141 //TODO: uci_free_package(pkg) ?, or will uci_free_contect(ctx) also free the package
142 uci_free_context(ctx);
144 free(conf_fname_full);
147 log_error("Failed to get value from configuration file: %s/%s, out of memory.\n", conf_dir_name, conf_file_basename);
151 log_error("Failed to get value from configuration file, invalid parameters.\n");