]> pilppa.org Git - libplp.git/blob - src/config.c
c099d97fbe022b89c12b0549667210427b38c987
[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
7 #include "log.h"
8 #include "config.h"
9
10 static int uci_create_named_section(struct uci_context *ctx, const char *conf_file_name, const char *section_type, const char *section_name)
11 {
12         struct uci_ptr          ptr;
13         int                     ret_val;
14         char                    *cmd_data;
15         int                     len;
16
17         ret_val = -1;
18         if ((ctx != NULL) &&
19             (conf_file_name != NULL) &&
20             (section_type != NULL) &&
21             (section_name != NULL)) {
22                 len             = strlen(conf_file_name);
23                 len             = len + 1;
24                 len             = len + strlen(section_type);
25                 len             = len + 1;
26                 len             = len + strlen(section_name);
27                 len             = len + 1;
28                 cmd_data        = malloc(len);
29                 snprintf(cmd_data, len, "%s.%s=%s", conf_file_name, section_name, section_type);
30                 if (uci_lookup_ptr(ctx, &ptr, cmd_data, true) == UCI_OK) {
31                         ret_val = uci_set(ctx, &ptr);
32                         if (ret_val == UCI_OK) {
33                                 //ret_val       = uci_save(ctx, ptr.p);
34                         }
35                 }
36         }
37         return ret_val;
38 }
39
40 bool set_config_value(const char *conf_dir_name,
41                         const char *conf_file_name,
42                         const char *section_type,
43                         const char *section_name,
44                         const char *key,
45                         const char *value) {
46         struct uci_context      *ctx;
47         struct uci_package      *pkg;
48         struct uci_section      *sct;
49         struct uci_section      *tmp_sct;
50         struct uci_option       *opt;
51         int                     err_flg;
52         char                    *fname;
53         int                     b_count;
54         bool                    save;
55         struct uci_element      *elem;
56         struct uci_ptr          ptr;
57         FILE                    *fp;
58         bool                    ret_val;
59
60         ret_val = false;
61         save    = false;
62         if ((conf_dir_name != NULL) &&
63             (conf_file_name != NULL) &&
64             (section_type != NULL) &&
65             (section_name != NULL) &&
66             (key != NULL) &&
67             (value != NULL)) {
68                 b_count = strlen(conf_dir_name) + strlen(conf_file_name) + 10;
69                 fname   = (char *)calloc(1, b_count);
70                 if (fname != NULL) {
71                         strncpy(fname, conf_dir_name, b_count);
72                         strncat(fname, "/", 1);
73                         strncat(fname, conf_file_name, strlen(conf_file_name) + 1);
74                         ctx     = uci_alloc_context();
75                         if (ctx != NULL) {
76                                 sct     = NULL;
77                                 uci_set_confdir(ctx, conf_dir_name);
78                                 if (access(fname, W_OK) != 0) {
79                                         if (access(fname, F_OK) != 0) {
80                                                 fp      = fopen(fname, "w+");
81                                                 fclose(fp);
82                                         }
83                                 }
84                                 if (access(fname, W_OK) == 0) {
85                                         err_flg = uci_load(ctx, fname, &pkg);
86                                         uci_foreach_element(&pkg->sections, elem) {
87                                                 tmp_sct = uci_to_section(elem);
88                                                 if (strcmp(tmp_sct->type, section_type) == 0) {
89                                                         sct     = tmp_sct;
90                                                         break;
91                                                 }
92                                         }
93                                         if (sct == NULL) {
94                                                 log_debug("Creating configuration section %s to configuration file: %s\n", section_name, fname);
95                                                 //err_flg       = uci_add_named_section(ctx, pkg, section_type, section_name, &sct);
96                                                 //err_flg       = uci_add_section(ctx, pkg, section_name, &sct);
97                                                 err_flg = uci_create_named_section(ctx, conf_file_name, section_type, section_name);
98                                                 if (err_flg == UCI_OK) {
99                                                         uci_foreach_element(&pkg->sections, elem) {
100                                                                 tmp_sct = uci_to_section(elem);
101                                                                 if (strcmp(tmp_sct->type, section_type) == 0) {
102                                                                         sct     = tmp_sct;
103                                                                         break;
104                                                                 }
105                                                         }
106                                                 }
107                                         }
108                                         if (err_flg == 0) {
109                                                 opt     = uci_lookup_option(ctx, sct, key);
110                                                 if (opt != NULL) {
111                                                         memset(&ptr, 0, sizeof(ptr));
112                                                         ptr.package     = pkg->e.name;
113                                                         ptr.section     = sct->e.name;
114                                                         ptr.option      = key;
115
116                                                         if (uci_lookup_ptr(ctx, &ptr, NULL, false) == UCI_OK) {
117                                                                 ptr.value       = strdup(value);
118                                                                 uci_set(ctx, &ptr);
119                                                                 save    = true;
120                                                         }
121                                                 }
122                                                 else {
123                                                         opt     = uci_alloc_option(sct, key, value);
124                                                         save    = true;
125                                                 }
126                                                 if (save == true) {
127                                                         uci_save(ctx, pkg);
128                                                 }
129                                                 uci_free_context(ctx);
130                                                 ret_val = true;
131                                         }
132                                         else {
133                                                 log_error("Could not write to configuration file: %s\n. Could not create section %s.", fname, section_name);
134                                         }
135                                 }
136                                 else {
137                                         log_error("Could not write to configuration file: %s\n. File does not exist or is not writable.", fname);
138                                 }
139                         }
140                         free(fname);
141                 }
142                 else {
143                         log_error("Could not change config value, out of memory");
144                 }
145         }
146         else {
147                 log_error("Could not change config value, invalid parameters");
148         }
149         return ret_val;
150 }