]> pilppa.org Git - libplp.git/blob - src/private/uci_config.c
api fixes and cleanups
[libplp.git] / src / private / uci_config.c
1 /*
2  * uci_config.c
3  *
4  *  Created on: Aug 2, 2012
5  *      Author: lamikr
6  */
7
8 #include "uci_config.h"
9 #include "../log.h"
10
11 int uci_create_named_section(struct uci_context *ctx,
12                                 const char *conf_fname_base,
13                                 const char *section_type,
14                                 const char *section_name,
15                                 bool save_immediately)
16 {
17         struct uci_ptr  ptr;
18         int             ret_val;
19         char            *cmd_data;
20         int             len;
21
22         ret_val = -1;
23         if ((ctx != NULL) &&
24             (conf_fname_base != NULL) &&
25             (section_type != NULL) &&
26             (section_name != NULL)) {
27                 len             = strlen(conf_fname_base);
28                 len             = len + 1;
29                 len             = len + strlen(section_type);
30                 len             = len + 1;
31                 len             = len + strlen(section_name);
32                 len             = len + 1;
33                 cmd_data        = malloc(len);
34                 if (cmd_data != NULL) {
35                         snprintf(cmd_data,
36                                 len,
37                                 "%s.%s=%s",
38                                 conf_fname_base,
39                                 section_name,
40                                 section_type);
41                         if (uci_lookup_ptr(ctx, &ptr, cmd_data, true) == UCI_OK) {
42                                 ret_val = uci_set(ctx, &ptr);
43                                 if (ret_val == UCI_OK) {
44                                         if (save_immediately) {
45                                                 ret_val = uci_save(ctx, ptr.p);
46                                         }
47                                 }
48                         }
49                         free(cmd_data);
50                 }
51         }
52         return ret_val;
53 }
54
55 bool uci_set_config_value(struct uci_context *ctx,
56                         struct uci_package *pkg,
57                         const char *conf_fname_base,
58                         const char *conf_fname_full,
59                         const char *section_type,
60                         const char *section_name,
61                         const char *key,
62                         const char *value,
63                         const bool save_immediately) {
64         struct uci_section      *sct;
65         struct uci_section      *tmp_sct;
66         int                     err_flg;
67         struct uci_element      *elem;
68         struct uci_ptr          ptr;
69         bool                    ret_val;
70
71         ret_val = false;
72         err_flg = UCI_OK;
73         if ((ctx != NULL) &&
74             (pkg != NULL) &&
75             (section_type != NULL) &&
76             (section_name != NULL) &&
77             (key != NULL) &&
78             (value != NULL)) {
79                 sct     = NULL;
80                 uci_foreach_element(&pkg->sections, elem) {
81                         tmp_sct = uci_to_section(elem);
82                         if (tmp_sct != NULL) {
83                                 if (strcmp(tmp_sct->type, section_type) == 0) {
84                                         sct     = tmp_sct;
85                                         break;
86                                 }
87                                 else {
88                                         //TODO: uci_free_section(tmp_sct); ?, or will uci_free_contect(ctx) also free the section
89                                 }
90                         }
91                 }
92                 if (sct == NULL) {
93                         //log_debug("Creating new section %s to configuration file: %s\n", section_name, conf_fname_full);
94                         //err_flg       = uci_add_named_section(ctx, pkg, section_type, section_name, &sct);
95                         //err_flg       = uci_add_section(ctx, pkg, section_name, &sct);
96                         err_flg = uci_create_named_section(ctx,
97                                                 conf_fname_base,
98                                                 section_type,
99                                                 section_name,
100                                                 false);
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) {
105                                                 sct     = tmp_sct;
106                                                 break;
107                                         }
108                                 }
109                         }
110                         //TODO: uci_free_section(sct); ?, or will uci_free_contect(ctx) also free the section
111                 }
112                 if (err_flg == UCI_OK) {
113                         memset(&ptr, 0, sizeof(ptr));
114                         ptr.package     = pkg->e.name;
115                         ptr.section     = sct->e.name;
116                         ptr.option      = key;
117                         err_flg         = uci_lookup_ptr(ctx, &ptr, NULL, false);
118                         if (err_flg == UCI_OK) {
119                                 ptr.value       = value;
120                                 err_flg         = uci_set(ctx, &ptr);
121                                 log_debug("file: %s, section_key: %s/%s: value: %s.\n",
122                                         conf_fname_full,
123                                         section_name,
124                                         key,
125                                         value);
126                                 if (err_flg == UCI_OK) {
127                                         if (save_immediately == true) {
128                                                 err_flg = uci_save(ctx, pkg);
129                                                 if (err_flg == UCI_OK) {
130                                                         ret_val = true;
131                                                 }
132                                                 else {
133                                                         log_error("Failed to set value to configuration file: %s. Could not save the file.\n", conf_fname_full);
134                                                 }
135                                         }
136                                         else {
137                                                 ret_val = true;
138                                         }
139                                 }
140                                 else {
141                                         log_error("Failed to set value to configuration file: %s. Could not set new value.\n", conf_fname_full);
142                                 }
143                         }
144                         else {
145                                 log_error("Failed to set value to configuration file: %s. Could not look-up pointer for package %s section %s.\n",
146                                         conf_fname_full,
147                                         pkg->e.name,
148                                         sct->e.name);
149                         }
150                 }
151                 else {
152                         log_error("Failed to set value to configuration file: %s. Could not create section %s.\n", conf_fname_full, section_name);
153                 }
154         }
155         else {
156                 log_error("Failed to set value to configuration file, invalid parameters\n");
157         }
158         return ret_val;
159 }
160
161 char *uci_get_config_value(struct uci_context *ctx,
162                         struct uci_package *pkg,
163                         const char *section_name,
164                         const char *key_name) {
165         struct uci_section      *section;
166         struct uci_option       *option;
167         char                    *ret_val;
168
169         ret_val = NULL;
170         if ((ctx != NULL) &&
171             (pkg != NULL)) {
172                 section = uci_lookup_section(ctx,
173                                         pkg,
174                                         section_name);
175                 if (section != NULL) {
176                         option  = uci_lookup_option(ctx,
177                                                 section,
178                                                 key_name);
179                         if (option != NULL) {
180                                 switch (option->type) {
181                                         case UCI_TYPE_STRING:
182                                                 //log_debug("key: %s option name: %s, value: %s\n", key.c_str(), option->e.name, option->v.string);
183                                                 ret_val = option->v.string;
184                                                 break;
185                                         default:
186                                                 log_error("Failed to read configuration value for key: %s\n", key_name);
187                                                 break;
188                                 }
189                         }
190                         else {
191                                 log_error("Failed to find configuration key: %s\n", key_name);
192                         }
193                 }
194                 else {
195                         log_error("Failed to find configuration section name: %s\n", section_name);
196                 }
197         }
198         return ret_val;
199 }
200
201 bool uci_save_config_values(struct uci_context *ctx,
202                         struct uci_package *pkg) {
203         bool    ret_val;
204         int     err_flg;
205
206         if ((ctx != NULL) &&
207             (pkg != NULL)) {
208                 err_flg = uci_save(ctx, pkg);
209                 if (err_flg == UCI_OK) {
210                         ret_val = true;
211                 }
212                 else {
213                         log_error("Could not save configuration file.\n");
214                 }
215         }
216         return ret_val;
217 }