]> pilppa.org Git - libplp.git/blob - src/config.c
74bfd8bd15a07fa3605570a1cd588da46eb7f99e
[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      *e;
26         bool                    ret_val;
27
28         ret_val = false;
29         save    = false;
30         if ((conf_dir_name != NULL) &&
31             (conf_file_name != NULL) &&
32             (section_type != NULL) &&
33             (section_name != NULL) &&
34             (key != NULL) &&
35             (value != NULL)) {
36                 b_count = strlen(conf_dir_name) + strlen(conf_file_name) + 10;
37                 fname   = (char *)calloc(1, b_count);
38                 if (fname != NULL) {
39                         strncpy(fname, conf_dir_name, b_count);
40                         strncat(fname, "/", 1);
41                         strncat(fname, conf_file_name, strlen(conf_file_name) + 1);
42                         ctx     = uci_alloc_context();
43                         if (ctx != NULL) {
44                                 sct     = NULL;
45                                 uci_set_confdir(ctx, conf_dir_name);
46                                 if (access(fname, W_OK) != 0) {
47                                         if (access(fname, F_OK) != 0) {
48                                                 FILE *fp;
49                                                 fp      = fopen(fname, "w+");
50                                                 fclose(fp);
51                                         }
52                                 }
53                                 err_flg = uci_load(ctx, fname, &pkg);
54                                 uci_foreach_element(&pkg->sections, e) {
55                                         tmp_sct = uci_to_section(e);
56                                         if (strcmp(tmp_sct->type, section_type) == 0) {
57                                                 sct     = tmp_sct;
58                                                 break;
59                                         }
60                                 }
61                                 //sct   = uci_lookup_section(ctx, pkg, "service");
62                                 if (sct != NULL) {
63                                         log_debug("section found\n");
64                                 }
65                                 else {
66                                         log_debug("adding new section\n");
67                                         err_flg = uci_add_named_section(ctx, pkg, section_type, section_name, &sct);
68                                         log_debug("err_flg: %d, section name: %s\n", err_flg, sct->e.name);
69                                 }
70                                 opt     = uci_lookup_option(ctx, sct, key);
71                                 if (opt != NULL) {
72                                         struct uci_ptr ptr;
73                                         memset(&ptr, 0, sizeof(ptr));
74                                         ptr.package     = pkg->e.name;
75                                         ptr.section     = sct->e.name;
76                                         ptr.option      = key;
77
78                                         if (uci_lookup_ptr(ctx, &ptr, NULL, false) == UCI_OK) {
79                                                 log_debug("trying to change option value\n");
80                                                 ptr.value       = strdup(value);
81                                                 uci_set(ctx, &ptr);
82                                                 save            = true;
83                                         }
84                                 }
85                                 else {
86                                         opt     = uci_alloc_option(sct, key, value);
87                                         save    = true;
88                                 }
89                                 if (save == true) {
90                                         log_debug("saving config file: %s\n", fname);
91                                         uci_save(ctx, pkg);
92                                 }
93                                 uci_free_context(ctx);
94                                 ret_val = true;
95                         }
96                         free(fname);
97                 }
98                 else {
99                         log_error("Could not change config value, out of memory");
100                 }
101         }
102         else {
103                 log_error("Could not change config value, invalid parameters");
104         }
105         return ret_val;
106 }