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