]> pilppa.org Git - libplp.git/blob - src/config.c
4e51b43ca78db17ddc946968775567b2b6df2adf
[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                 if (cmd_data != NULL) {
30                         snprintf(cmd_data, len, "%s.%s=%s", conf_file_name, section_name, section_type);
31                         if (uci_lookup_ptr(ctx, &ptr, cmd_data, true) == UCI_OK) {
32                                 ret_val = uci_set(ctx, &ptr);
33                                 if (ret_val == UCI_OK) {
34                                         //ret_val       = uci_save(ctx, ptr.p);
35                                 }
36                         }
37                         free(cmd_data);
38                 }
39         }
40         return ret_val;
41 }
42
43 bool set_config_value(const char *conf_dir_name,
44                         const char *conf_file_name,
45                         const char *section_type,
46                         const char *section_name,
47                         const char *key,
48                         const char *value) {
49         struct uci_context      *ctx;
50         struct uci_package      *pkg;
51         struct uci_section      *sct;
52         struct uci_section      *tmp_sct;
53         int                     err_flg;
54         char                    *fname;
55         int                     b_count;
56         struct uci_element      *elem;
57         struct uci_ptr          ptr;
58         FILE                    *fp;
59         bool                    ret_val;
60
61         ret_val = 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                                                 memset(&ptr, 0, sizeof(ptr));
110                                                 ptr.package     = pkg->e.name;
111                                                 ptr.section     = sct->e.name;
112                                                 ptr.option      = key;
113
114                                                 if (uci_lookup_ptr(ctx, &ptr, NULL, false) == UCI_OK) {
115                                                         ptr.value       = value;
116                                                         uci_set(ctx, &ptr);
117                                                         uci_save(ctx, pkg);
118                                                         ret_val         = true;
119                                                         log_debug("Created configuration section %s to configuration file: %s\n", section_name, fname);
120                                                 }
121                                                 else {
122                                                         log_error("Could not write to configuration file: %s\n. Could not look-up pointer for package %s section %s.\n", fname, pkg->e.name, sct->e.name);
123                                                 }
124                                                 uci_free_context(ctx);
125                                         }
126                                         else {
127                                                 log_error("Could not write to configuration file: %s. Could not create section %s.\n", fname, section_name);
128                                         }
129                                 }
130                                 else {
131                                         log_error("Could not write to configuration file: %s. File does not exist or is not writable\n.", fname);
132                                 }
133                         }
134                         free(fname);
135                 }
136                 else {
137                         log_error("Could not change config value, out of memory\n");
138                 }
139         }
140         else {
141                 log_error("Could not change config value, invalid parameters\n");
142         }
143         return ret_val;
144 }