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