]> pilppa.org Git - libplp.git/blob - src_test/test_config2.c
2bf367b0d62e864c043dae1d3d7dced058200ac6
[libplp.git] / src_test / test_config2.c
1 /*
2  * test_w1.cc
3  *
4  *  Created on: Oct 20, 2010
5  *      Author: lamikr
6  */
7 #include <string.h>
8 #include <errno.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <uci.h>
12
13 #include "log.h"
14 #include "config.h"
15
16 /*
17 static int uci_do_add(int argc, char **argv)
18 {
19         struct uci_package *p = NULL;
20         struct uci_section *s = NULL;
21         int ret;
22
23         if (argc != 3)
24                 return 255;
25
26         ret = uci_load(ctx, argv[1], &p);
27         if (ret != UCI_OK)
28                 goto done;
29
30         ret = uci_add_section(ctx, p, argv[2], &s);
31         if (ret != UCI_OK)
32                 goto done;
33
34         ret = uci_save(ctx, p);
35
36 done:
37         if (ret != UCI_OK)
38                 cli_perror();
39         else if (s)
40                 fprintf(stdout, "%s\n", s->e.name);
41         return ret;
42 }
43 */
44
45 static int uci_create_named_section(struct uci_context *ctx, const char *conf_file_name, const char *section_type, const char *section_name)
46 {
47         struct uci_ptr          ptr;
48         int                     ret_val;
49         char                    *cmd_data;
50         int                     len;
51
52         ret_val = -1;
53         if ((ctx != NULL) &&
54             (conf_file_name != NULL) &&
55             (section_type != NULL) &&
56             (section_name != NULL)) {
57                 len             = strlen(conf_file_name);
58                 len             = len + 1;
59                 len             = len + strlen(section_type);
60                 len             = len + 1;
61                 len             = len + strlen(section_name);
62                 len             = len + 1;
63                 cmd_data        = malloc(len);
64                 snprintf(cmd_data, len, "%s.%s=%s", conf_file_name, section_name, section_type);
65                 if (uci_lookup_ptr(ctx, &ptr, cmd_data, true) == UCI_OK) {
66                         ret_val = uci_set(ctx, &ptr);
67                         if (ret_val == UCI_OK) {
68                                 //ret_val       = uci_save(ctx, ptr.p);
69                         }
70                 }
71         }
72         return ret_val;
73 }
74
75 bool set_config_value2(const char *conf_dir_name,
76                         const char *conf_file_name,
77                         const char *section_type,
78                         const char *section_name,
79                         const char *key,
80                         const char *value) {
81         struct uci_context      *ctx;
82         struct uci_package      *pkg;
83         struct uci_section      *sct;
84         struct uci_section      *tmp_sct;
85         struct uci_option       *opt;
86         int                     err_flg;
87         char                    *fname;
88         int                     b_count;
89         bool                    save;
90         struct uci_element      *elem;
91         struct uci_ptr          ptr;
92         FILE                    *fp;
93         bool                    ret_val;
94
95         ret_val = false;
96         save    = false;
97         if ((conf_dir_name != NULL) &&
98             (conf_file_name != NULL) &&
99             (section_type != NULL) &&
100             (section_name != NULL) &&
101             (key != NULL) &&
102             (value != NULL)) {
103                 b_count = strlen(conf_dir_name) + strlen(conf_file_name) + 10;
104                 fname   = (char *)calloc(1, b_count);
105                 if (fname != NULL) {
106                         strncpy(fname, conf_dir_name, b_count);
107                         strncat(fname, "/", 1);
108                         strncat(fname, conf_file_name, strlen(conf_file_name) + 1);
109                         ctx     = uci_alloc_context();
110                         if (ctx != NULL) {
111                                 sct     = NULL;
112                                 uci_set_confdir(ctx, conf_dir_name);
113                                 if (access(fname, W_OK) != 0) {
114                                         if (access(fname, F_OK) != 0) {
115                                                 fp      = fopen(fname, "w+");
116                                                 fclose(fp);
117                                         }
118                                 }
119                                 if (access(fname, W_OK) == 0) {
120                                         err_flg = uci_load(ctx, fname, &pkg);
121                                         uci_foreach_element(&pkg->sections, elem) {
122                                                 tmp_sct = uci_to_section(elem);
123                                                 if (strcmp(tmp_sct->type, section_type) == 0) {
124                                                         sct     = tmp_sct;
125                                                         break;
126                                                 }
127                                         }
128                                         if (sct == NULL) {
129                                                 log_debug("Creating configuration section %s to configuration file: %s\n", section_name, fname);
130                                                 //err_flg       = uci_add_named_section(ctx, pkg, section_type, section_name, &sct);
131                                                 //err_flg       = uci_add_section(ctx, pkg, section_name, &sct);
132                                                 err_flg = uci_create_named_section(ctx, conf_file_name, section_type, section_name);
133                                                 if (err_flg == UCI_OK) {
134                                                         uci_foreach_element(&pkg->sections, elem) {
135                                                                 tmp_sct = uci_to_section(elem);
136                                                                 if (strcmp(tmp_sct->type, section_type) == 0) {
137                                                                         sct     = tmp_sct;
138                                                                         break;
139                                                                 }
140                                                         }
141                                                 }
142                                         }
143                                         if (err_flg == 0) {
144                                                 opt     = uci_lookup_option(ctx, sct, key);
145                                                 if (opt != NULL) {
146                                                         memset(&ptr, 0, sizeof(ptr));
147                                                         ptr.package     = pkg->e.name;
148                                                         ptr.section     = sct->e.name;
149                                                         ptr.option      = key;
150
151                                                         if (uci_lookup_ptr(ctx, &ptr, NULL, false) == UCI_OK) {
152                                                                 ptr.value       = strdup(value);
153                                                                 uci_set(ctx, &ptr);
154                                                                 save    = true;
155                                                         }
156                                                 }
157                                                 else {
158                                                         opt     = uci_alloc_option(sct, key, value);
159                                                         save    = true;
160                                                 }
161                                                 if (save == true) {
162                                                         uci_save(ctx, pkg);
163                                                 }
164                                                 uci_free_context(ctx);
165                                                 ret_val = true;
166                                         }
167                                         else {
168                                                 log_error("Could not write to configuration file: %s\n. Could not create section %s.", fname, section_name);
169                                         }
170                                 }
171                                 else {
172                                         log_error("Could not write to configuration file: %s\n. File does not exist or is not writable.", fname);
173                                 }
174                         }
175                         free(fname);
176                 }
177                 else {
178                         log_error("Could not change config value, out of memory");
179                 }
180         }
181         else {
182                 log_error("Could not change config value, invalid parameters");
183         }
184         return ret_val;
185 }
186
187 void test_config() {
188         char work_dir[FILENAME_MAX];
189
190         getcwd(work_dir, sizeof(work_dir));
191         printf("working directory: %s\n", work_dir);
192
193         set_config_value2(work_dir,
194                         "dev_cfg_txt",
195                         "mysection_type",
196                         "mysection_name",
197                         "myoption_name",
198                         "my_option_value");
199         set_config_value2(work_dir,
200                         "dev_cfg_txt",
201                         "mysection_type",
202                         "mysection_name",
203                         "myoption_name",
204                         "my_option_value2");
205 }
206
207 int main(int argc, char** argv) {
208         test_config();
209         return 0;
210 }