]> pilppa.org Git - libplp.git/blob - src/config.c
cleanups and error handling for set_config_value method.
[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 bool set_config_value(const char *conf_dir_name,
11                         const char *conf_file_name,
12                         const char *section_type,
13                         const char *section_name,
14                         const char *key,
15                         const char *value) {
16         struct uci_context      *ctx;
17         struct uci_package      *pkg;
18         struct uci_section      *sct;
19         struct uci_section      *tmp_sct;
20         struct uci_option       *opt;
21         int                     err_flg;
22         char                    *fname;
23         int                     b_count;
24         bool                    save;
25         struct uci_element      *elem;
26         struct uci_ptr          ptr;
27         FILE                    *fp;
28         bool                    ret_val;
29
30         ret_val = false;
31         save    = false;
32         if ((conf_dir_name != NULL) &&
33             (conf_file_name != NULL) &&
34             (section_type != NULL) &&
35             (section_name != NULL) &&
36             (key != NULL) &&
37             (value != NULL)) {
38                 b_count = strlen(conf_dir_name) + strlen(conf_file_name) + 10;
39                 fname   = (char *)calloc(1, b_count);
40                 if (fname != NULL) {
41                         strncpy(fname, conf_dir_name, b_count);
42                         strncat(fname, "/", 1);
43                         strncat(fname, conf_file_name, strlen(conf_file_name) + 1);
44                         ctx     = uci_alloc_context();
45                         if (ctx != NULL) {
46                                 sct     = NULL;
47                                 uci_set_confdir(ctx, conf_dir_name);
48                                 if (access(fname, W_OK) != 0) {
49                                         if (access(fname, F_OK) != 0) {
50                                                 fp      = fopen(fname, "w+");
51                                                 fclose(fp);
52                                         }
53                                 }
54                                 if (access(fname, W_OK) == 0) {
55                                         err_flg = uci_load(ctx, fname, &pkg);
56                                         uci_foreach_element(&pkg->sections, elem) {
57                                                 tmp_sct = uci_to_section(elem);
58                                                 if (strcmp(tmp_sct->type, section_type) == 0) {
59                                                         sct     = tmp_sct;
60                                                         break;
61                                                 }
62                                         }
63                                         //sct   = uci_lookup_section(ctx, pkg, "service");
64                                         if (sct == NULL) {
65                                                 log_debug("Creating configuration section %s to configuration file: %s\n", section_name, fname);
66                                                 err_flg = uci_add_named_section(ctx, pkg, section_type, section_name, &sct);
67                                         }
68                                         if (err_flg == 0) {
69                                                 opt     = uci_lookup_option(ctx, sct, key);
70                                                 if (opt != NULL) {
71                                                         memset(&ptr, 0, sizeof(ptr));
72                                                         ptr.package     = pkg->e.name;
73                                                         ptr.section     = sct->e.name;
74                                                         ptr.option      = key;
75
76                                                         if (uci_lookup_ptr(ctx, &ptr, NULL, false) == UCI_OK) {
77                                                                 ptr.value       = strdup(value);
78                                                                 uci_set(ctx, &ptr);
79                                                                 save    = true;
80                                                         }
81                                                 }
82                                                 else {
83                                                         opt     = uci_alloc_option(sct, key, value);
84                                                         save    = true;
85                                                 }
86                                                 if (save == true) {
87                                                         uci_save(ctx, pkg);
88                                                 }
89                                                 uci_free_context(ctx);
90                                                 ret_val = true;
91                                         }
92                                         else {
93                                                 log_error("Could not write to configuration file: %s\n. Could not create section %s.", fname, section_name);
94                                         }
95                                 }
96                                 else {
97                                         log_error("Could not write to configuration file: %s\n. File does not exist or is not writable.", fname);
98                                 }
99                         }
100                         free(fname);
101                 }
102                 else {
103                         log_error("Could not change config value, out of memory");
104                 }
105         }
106         else {
107                 log_error("Could not change config value, invalid parameters");
108         }
109         return ret_val;
110 }