]> pilppa.org Git - libplp.git/blob - src_test/test_config2.c
32e1d931a467592e3e71afba11444f6de8f104c8
[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                 if (cmd_data != NULL) {
65                         snprintf(cmd_data, len, "%s.%s=%s", conf_file_name, section_name, section_type);
66                         if (uci_lookup_ptr(ctx, &ptr, cmd_data, true) == UCI_OK) {
67                                 ret_val = uci_set(ctx, &ptr);
68                                 if (ret_val == UCI_OK) {
69                                 }
70                         }
71                         free(cmd_data);
72                 }
73         }
74         return ret_val;
75 }
76
77 bool set_config_value2(const char *conf_dir_name,
78                         const char *conf_file_name,
79                         const char *section_type,
80                         const char *section_name,
81                         const char *key,
82                         const char *value) {
83         struct uci_context      *ctx;
84         struct uci_package      *pkg;
85         struct uci_section      *sct;
86         struct uci_section      *tmp_sct;
87         struct uci_option       *opt;
88         int                     err_flg;
89         char                    *fname;
90         int                     b_count;
91         bool                    save;
92         struct uci_element      *elem;
93         struct uci_ptr          ptr;
94         FILE                    *fp;
95         bool                    ret_val;
96
97         ret_val = false;
98         save    = false;
99         if ((conf_dir_name != NULL) &&
100             (conf_file_name != NULL) &&
101             (section_type != NULL) &&
102             (section_name != NULL) &&
103             (key != NULL) &&
104             (value != NULL)) {
105                 b_count = strlen(conf_dir_name) + strlen(conf_file_name) + 10;
106                 fname   = (char *)calloc(1, b_count);
107                 if (fname != NULL) {
108                         strncpy(fname, conf_dir_name, b_count);
109                         strncat(fname, "/", 1);
110                         strncat(fname, conf_file_name, strlen(conf_file_name) + 1);
111                         ctx     = uci_alloc_context();
112                         if (ctx != NULL) {
113                                 sct     = NULL;
114                                 uci_set_confdir(ctx, conf_dir_name);
115                                 if (access(fname, W_OK) != 0) {
116                                         if (access(fname, F_OK) != 0) {
117                                                 fp      = fopen(fname, "w+");
118                                                 fclose(fp);
119                                         }
120                                 }
121                                 if (access(fname, W_OK) == 0) {
122                                         err_flg = uci_load(ctx, fname, &pkg);
123                                         uci_foreach_element(&pkg->sections, elem) {
124                                                 tmp_sct = uci_to_section(elem);
125                                                 if (strcmp(tmp_sct->type, section_type) == 0) {
126                                                         sct     = tmp_sct;
127                                                         break;
128                                                 }
129                                         }
130                                         if (sct == NULL) {
131                                                 log_debug("Creating configuration section %s to configuration file: %s\n", section_name, fname);
132                                                 //err_flg       = uci_add_named_section(ctx, pkg, section_type, section_name, &sct);
133                                                 //err_flg       = uci_add_section(ctx, pkg, section_name, &sct);
134                                                 err_flg = uci_create_named_section(ctx, conf_file_name, section_type, section_name);
135                                                 if (err_flg == UCI_OK) {
136                                                         uci_foreach_element(&pkg->sections, elem) {
137                                                                 tmp_sct = uci_to_section(elem);
138                                                                 if (strcmp(tmp_sct->type, section_type) == 0) {
139                                                                         sct     = tmp_sct;
140                                                                         break;
141                                                                 }
142                                                         }
143                                                 }
144                                         }
145                                         if (err_flg == 0) {
146                                                 opt     = uci_lookup_option(ctx, sct, key);
147                                                 if (opt != NULL) {
148                                                         memset(&ptr, 0, sizeof(ptr));
149                                                         ptr.package     = pkg->e.name;
150                                                         ptr.section     = sct->e.name;
151                                                         ptr.option      = key;
152
153                                                         if (uci_lookup_ptr(ctx, &ptr, NULL, false) == UCI_OK) {
154                                                                 ptr.value       = value;
155                                                                 uci_set(ctx, &ptr);
156                                                                 save    = true;
157                                                         }
158                                                 }
159                                                 else {
160                                                         opt     = uci_alloc_option(sct, key, value);
161                                                         save    = true;
162                                                 }
163                                                 if (save == true) {
164                                                         uci_save(ctx, pkg);
165                                                 }
166                                                 uci_free_context(ctx);
167                                                 ret_val = true;
168                                         }
169                                         else {
170                                                 log_error("Could not write to configuration file: %s\n. Could not create section %s.", fname, section_name);
171                                         }
172                                 }
173                                 else {
174                                         log_error("Could not write to configuration file: %s\n. File does not exist or is not writable.", fname);
175                                 }
176                         }
177                         free(fname);
178                 }
179                 else {
180                         log_error("Could not change config value, out of memory");
181                 }
182         }
183         else {
184                 log_error("Could not change config value, invalid parameters");
185         }
186         return ret_val;
187 }
188
189 void test_config() {
190         char work_dir[FILENAME_MAX];
191
192         getcwd(work_dir, sizeof(work_dir));
193         printf("working directory: %s\n", work_dir);
194
195         set_config_value2(work_dir,
196                         "dev_cfg_txt",
197                         "mysection_type",
198                         "mysection_name",
199                         "myoption_name",
200                         "my_option_value");
201         set_config_value2(work_dir,
202                         "dev_cfg_txt",
203                         "mysection_type",
204                         "mysection_name",
205                         "myoption_name",
206                         "my_option_value2");
207 }
208
209 int main(int argc, char** argv) {
210         test_config();
211         return 0;
212 }