]> pilppa.org Git - libplp.git/blob - src/config.c
92c8df303875424f5e455f9203f20a2006be875f
[libplp.git] / src / config.c
1 #include <string.h>
2 #include <errno.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <uci.h>
6 #include <stdbool.h>
7
8 #include "log.h"
9 #include "config.h"
10
11 static int uci_create_named_section(struct uci_context *ctx,
12                                 const char *conf_file_name,
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_file_name != NULL) &&
25             (section_type != NULL) &&
26             (section_name != NULL)) {
27                 len             = strlen(conf_file_name);
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, len, "%s.%s=%s", conf_file_name, section_name, section_type);
36                         if (uci_lookup_ptr(ctx, &ptr, cmd_data, true) == UCI_OK) {
37                                 ret_val = uci_set(ctx, &ptr);
38                                 if (ret_val == UCI_OK) {
39                                         if (save_immediately) {
40                                                 ret_val = uci_save(ctx, ptr.p);
41                                         }
42                                 }
43                         }
44                         free(cmd_data);
45                 }
46         }
47         return ret_val;
48 }
49
50 bool set_config_value_to_section(const char *conf_dir_name,
51                         const char *conf_file_name,
52                         const char *section_type,
53                         const char *section_name,
54                         const char *key,
55                         const char *value,
56                         bool save_immediately) {
57         struct uci_context      *ctx;
58         struct uci_package      *pkg;
59         struct uci_section      *sct;
60         struct uci_section      *tmp_sct;
61         int                     err_flg;
62         char                    *fname;
63         int                     b_count;
64         struct uci_element      *elem;
65         struct uci_ptr          ptr;
66         FILE                    *fp;
67         bool                    ret_val;
68
69         ret_val = false;
70         if ((conf_dir_name != NULL) &&
71             (conf_file_name != NULL) &&
72             (section_type != NULL) &&
73             (section_name != NULL) &&
74             (key != NULL) &&
75             (value != NULL)) {
76                 b_count = strlen(conf_dir_name) + strlen(conf_file_name) + 10;
77                 fname   = (char *)calloc(1, b_count);
78                 if (fname != NULL) {
79                         strncpy(fname, conf_dir_name, b_count);
80                         strncat(fname, "/", 1);
81                         strncat(fname, conf_file_name, strlen(conf_file_name) + 1);
82                         ctx     = uci_alloc_context();
83                         if (ctx != NULL) {
84                                 sct     = NULL;
85                                 uci_set_confdir(ctx, conf_dir_name);
86                                 if (access(fname, W_OK) != 0) {
87                                         if (access(fname, F_OK) != 0) {
88                                                 fp      = fopen(fname, "w+");
89                                                 fclose(fp);
90                                         }
91                                 }
92                                 if (access(fname, W_OK) == 0) {
93                                         err_flg = uci_load(ctx, fname, &pkg);
94                                         uci_foreach_element(&pkg->sections, elem) {
95                                                 tmp_sct = uci_to_section(elem);
96                                                 if (strcmp(tmp_sct->type, section_type) == 0) {
97                                                         sct     = tmp_sct;
98                                                         break;
99                                                 }
100                                         }
101                                         if (sct == NULL) {
102                                                 log_debug("Creating new section %s to configuration file: %s\n", section_name, fname);
103                                                 //err_flg       = uci_add_named_section(ctx, pkg, section_type, section_name, &sct);
104                                                 //err_flg       = uci_add_section(ctx, pkg, section_name, &sct);
105                                                 err_flg = uci_create_named_section(ctx,
106                                                                         conf_file_name,
107                                                                         section_type,
108                                                                         section_name,
109                                                                         false);
110                                                 if (err_flg == UCI_OK) {
111                                                         uci_foreach_element(&pkg->sections, elem) {
112                                                                 tmp_sct = uci_to_section(elem);
113                                                                 if (strcmp(tmp_sct->type, section_type) == 0) {
114                                                                         sct     = tmp_sct;
115                                                                         break;
116                                                                 }
117                                                         }
118                                                 }
119                                         }
120                                         if (err_flg == UCI_OK) {
121                                                 memset(&ptr, 0, sizeof(ptr));
122                                                 ptr.package     = pkg->e.name;
123                                                 ptr.section     = sct->e.name;
124                                                 ptr.option      = key;
125                                                 err_flg = uci_lookup_ptr(ctx, &ptr, NULL, false);
126                                                 if (err_flg == UCI_OK) {
127                                                         ptr.value       = value;
128                                                         err_flg         = uci_set(ctx, &ptr);
129                                                         if (err_flg == UCI_OK) {
130                                                                 if (save_immediately == true) {
131                                                                         err_flg = uci_save(ctx, pkg);
132                                                                         if (err_flg != UCI_OK) {
133                                                                                 log_error("Failed to set value to configuration file: %s\n. Could not save the file\n", fname);
134                                                                         }
135                                                                 }
136                                                                 if (err_flg == UCI_OK) {
137                                                                         ret_val = true;
138                                                                         log_debug("Set value to section %s in configuration file: %s\n", section_name, fname);
139                                                                 }
140                                                         }
141                                                         else {
142                                                                 log_error("Failed to set value to configuration file: %s\n. Could not set new value\n", fname);
143                                                         }
144                                                 }
145                                                 else {
146                                                         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);
147                                                 }
148                                                 uci_free_context(ctx);
149                                         }
150                                         else {
151                                                 log_error("Failed to set value to configuration file: %s. Could not create section %s.\n", fname, section_name);
152                                         }
153                                 }
154                                 else {
155                                         log_error("Failed to set value to configuration file: %s. File does not exist or is not writable\n.", fname);
156                                 }
157                         }
158                         free(fname);
159                 }
160                 else {
161                         log_error("Failed to set value to configuration file: %s, out of memory\n", fname);
162                 }
163         }
164         else {
165                 log_error("Failed to set value to configuration file, invalid parameters\n");
166         }
167         return ret_val;
168 }