]> pilppa.org Git - libplp.git/blob - src/config.c
api fixes and cleanups
[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 "private/uci_config.h"
8 #include "config.h"
9 #include "log.h"
10
11 bool set_config_value_and_save(const char *conf_dir_name,
12                         const char *conf_file_basename,
13                         const char *section_type,
14                         const char *section_name,
15                         const char *key,
16                         const char *value) {
17         struct uci_context      *ctx;
18         struct uci_package      *pkg;
19         int                     err_flg;
20         char                    *conf_fname_full;
21         int                     b_count;
22         FILE                    *fp;
23         bool                    ret_val;
24
25         ret_val = false;
26         pkg     = NULL;
27         if ((conf_dir_name != NULL) &&
28             (conf_file_basename != NULL) &&
29             (section_type != NULL) &&
30             (section_name != NULL) &&
31             (key != NULL) &&
32             (value != NULL)) {
33                 b_count = strlen(conf_dir_name) + strlen(conf_file_basename) + 10;
34                 conf_fname_full = (char *)calloc(1, b_count);
35                 if (conf_fname_full != NULL) {
36                         strncpy(conf_fname_full, conf_dir_name, b_count);
37                         strncat(conf_fname_full, "/", 1);
38                         strncat(conf_fname_full, conf_file_basename, strlen(conf_file_basename) + 1);
39                         ctx     = uci_alloc_context();
40                         if (ctx != NULL) {
41                                 if (access(conf_fname_full, W_OK) != 0) {
42                                         // if file is not writable, try to change it to be writable
43                                         if (access(conf_fname_full, F_OK) != 0) {
44                                                 fp      = fopen(conf_fname_full, "w+");
45                                                 fclose(fp);
46                                         }
47                                 }
48                                 if (access(conf_fname_full, W_OK) == 0) {
49                                         uci_set_confdir(ctx,
50                                                         conf_dir_name);
51                                         err_flg = uci_load(ctx, conf_fname_full, &pkg);
52                                         if ((err_flg == UCI_OK) &&
53                                             (pkg != NULL)) {
54                                                 ret_val = uci_set_config_value(ctx,
55                                                                         pkg,
56                                                                         conf_file_basename,
57                                                                         conf_fname_full,
58                                                                         section_type,
59                                                                         section_name,
60                                                                         key,
61                                                                         value,
62                                                                         true);
63                                         }
64                                         else {
65                                                 log_error("Failed to set value to configuration file: %s. Could not load current file content.\n", conf_fname_full);
66                                         }
67                                 }
68                                 else {
69                                         log_error("Failed to set value to configuration file: %s. File does not exist or is not writable\n", conf_fname_full);
70                                 }
71                                 //TODO: uci_free_package(pkg) ?, or will uci_free_contect(ctx) also free the package
72                                 uci_free_context(ctx);
73                         }
74                         free(conf_fname_full);
75                 }
76                 else {
77                         log_error("Failed to set value to configuration file: %s/%s, out of memory\n", conf_dir_name, conf_file_basename);
78                 }
79         }
80         else {
81                 log_error("Failed to set value to configuration file, invalid parameters\n");
82         }
83         return ret_val;
84 }
85
86 char* get_config_value_and_close(const char *conf_dir_name,
87                         const char *conf_file_basename,
88                         const char *section_name,
89                         const char *key) {
90         struct uci_context      *ctx;
91         struct uci_package      *pkg;
92         int                     err_flg;
93         char                    *conf_fname_full;
94         int                     b_count;
95         FILE                    *fp;
96         char                    *ret_val;
97
98         ret_val = NULL;
99         pkg     = NULL;
100         if ((conf_dir_name != NULL) &&
101             (conf_file_basename != NULL) &&
102             (section_name != NULL) &&
103             (key != NULL)) {
104                 b_count = strlen(conf_dir_name) + strlen(conf_file_basename) + 10;
105                 conf_fname_full = (char *)calloc(1, b_count);
106                 if (conf_fname_full != NULL) {
107                         strncpy(conf_fname_full, conf_dir_name, b_count);
108                         strncat(conf_fname_full, "/", 1);
109                         strncat(conf_fname_full, conf_file_basename, strlen(conf_file_basename) + 1);
110                         ctx     = uci_alloc_context();
111                         if (ctx != NULL) {
112                                 if (access(conf_fname_full, R_OK) != 0) {
113                                         // if file is not writable, try to change it to be writable
114                                         if (access(conf_fname_full, R_OK) != 0) {
115                                                 fp      = fopen(conf_fname_full, "w+");
116                                                 fclose(fp);
117                                         }
118                                 }
119                                 if (access(conf_fname_full, R_OK) == 0) {
120                                         uci_set_confdir(ctx,
121                                                         conf_dir_name);
122                                         err_flg = uci_load(ctx, conf_fname_full, &pkg);
123                                         if ((err_flg == UCI_OK) &&
124                                             (pkg != NULL)) {
125                                                 ret_val = uci_get_config_value(ctx,
126                                                                         pkg,
127                                                                         section_name,
128                                                                         key);
129                                                 // need to duplicate response val, as uci_free_context() would free the value otherwise
130                                                 if (ret_val != NULL)
131                                                         ret_val = strdup(ret_val);
132                                         }
133                                         else {
134                                                 log_error("Failed to get value from configuration file: %s. Could not load current file content.\n", conf_fname_full);
135                                         }
136                                 }
137                                 else {
138                                         log_error("Failed to get value from configuration file: %s. File does not exist or is not readable.\n", conf_fname_full);
139                                 }
140                                 //TODO: uci_free_package(pkg) ?, or will uci_free_contect(ctx) also free the package
141                                 uci_free_context(ctx);
142                         }
143                         free(conf_fname_full);
144                 }
145                 else {
146                         log_error("Failed to get value from configuration file: %s/%s, out of memory.\n", conf_dir_name, conf_file_basename);
147                 }
148         }
149         else {
150                 log_error("Failed to get value from configuration file, invalid parameters.\n");
151         }
152         return ret_val;
153 }