fs: add support for HFSX Plus file system
[project/mountd.git] / ucix.c
1 #include <string.h>
2 #include <stdlib.h>
3
4 #include "include/ucix.h"
5
6 static struct uci_ptr ptr;
7
8 static inline int ucix_get_ptr(struct uci_context *ctx, const char *p, const char *s, const char *o, const char *t)
9 {
10 memset(&ptr, 0, sizeof(ptr));
11 ptr.package = p;
12 ptr.section = s;
13 ptr.option = o;
14 ptr.value = t;
15 return uci_lookup_ptr(ctx, &ptr, NULL, true);
16 }
17
18 struct uci_context* ucix_init(const char *config_file)
19 {
20 struct uci_context *ctx = uci_alloc_context();
21 if(!ctx)
22 return NULL;
23 uci_add_delta_path(ctx, "/var/state");
24 if(uci_load(ctx, config_file, NULL) != UCI_OK)
25 {
26 printf("%s/%s is missing or corrupt\n", ctx->savedir, config_file);
27 return NULL;
28 }
29 return ctx;
30 }
31
32 struct uci_context* ucix_init_path(const char *path, const char *config_file)
33 {
34 struct uci_context *ctx = uci_alloc_context();
35 if(!ctx)
36 return NULL;
37 if(path)
38 {
39 uci_set_savedir(ctx, path);
40 }
41 if(uci_load(ctx, config_file, NULL) != UCI_OK)
42 {
43 printf("%s/%s is missing or corrupt\n", ctx->savedir, config_file);
44 return NULL;
45 }
46 return ctx;
47 }
48
49 void ucix_cleanup(struct uci_context *ctx)
50 {
51 if(ctx)
52 {
53 uci_free_context(ctx);
54 }
55 }
56
57 int ucix_save(struct uci_context *ctx, const char *p)
58 {
59 if(ucix_get_ptr(ctx, p, NULL, NULL, NULL))
60 return 1;
61 uci_set_savedir(ctx, "/tmp/.uci/");
62 uci_save(ctx, ptr.p);
63 return 0;
64 }
65
66 int ucix_save_state(struct uci_context *ctx, const char *p)
67 {
68 if(ucix_get_ptr(ctx, p, NULL, NULL, NULL))
69 return 1;
70 uci_set_savedir(ctx, "/var/state/");
71 uci_save(ctx, ptr.p);
72 return 0;
73 }
74
75 int ucix_get_option_list(struct uci_context *ctx, const char *p,
76 const char *s, const char *o, struct list_head *l)
77 {
78 struct uci_element *e = NULL;
79 if(ucix_get_ptr(ctx, p, s, o, NULL))
80 return 1;
81 if (!(ptr.flags & UCI_LOOKUP_COMPLETE))
82 return 1;
83 e = ptr.last;
84 switch (e->type)
85 {
86 case UCI_TYPE_OPTION:
87 switch(ptr.o->type) {
88 case UCI_TYPE_LIST:
89 uci_foreach_element(&ptr.o->v.list, e)
90 {
91 struct ucilist *ul = malloc(sizeof(struct ucilist));
92 ul->val = strdup((e->name)?(e->name):(""));
93 INIT_LIST_HEAD(&ul->list);
94 list_add(&ul->list, l);
95 }
96 break;
97 default:
98 break;
99 }
100 break;
101 default:
102 return 1;
103 }
104
105 return 0;
106 }
107
108 char* ucix_get_option(struct uci_context *ctx, const char *p, const char *s, const char *o)
109 {
110 struct uci_element *e = NULL;
111 const char *value = NULL;
112 if(ucix_get_ptr(ctx, p, s, o, NULL))
113 return NULL;
114 if (!(ptr.flags & UCI_LOOKUP_COMPLETE))
115 return NULL;
116 e = ptr.last;
117 switch (e->type)
118 {
119 case UCI_TYPE_SECTION:
120 value = uci_to_section(e)->type;
121 break;
122 case UCI_TYPE_OPTION:
123 switch(ptr.o->type) {
124 case UCI_TYPE_STRING:
125 value = ptr.o->v.string;
126 break;
127 default:
128 value = NULL;
129 break;
130 }
131 break;
132 default:
133 return 0;
134 }
135
136 return (value) ? (strdup(value)):(NULL);
137 }
138
139 int ucix_get_option_int(struct uci_context *ctx, const char *p, const char *s, const char *o, int def)
140 {
141 char *tmp = ucix_get_option(ctx, p, s, o);
142 int ret = def;
143
144 if (tmp)
145 {
146 ret = atoi(tmp);
147 free(tmp);
148 }
149 return ret;
150 }
151
152 void ucix_add_section(struct uci_context *ctx, const char *p, const char *s, const char *t)
153 {
154 if(ucix_get_ptr(ctx, p, s, NULL, t))
155 return;
156 uci_set(ctx, &ptr);
157 }
158
159 void ucix_add_option(struct uci_context *ctx, const char *p, const char *s, const char *o, const char *t)
160 {
161 if(ucix_get_ptr(ctx, p, s, o, (t)?(t):("")))
162 return;
163 uci_set(ctx, &ptr);
164 }
165
166 void ucix_add_option_int(struct uci_context *ctx, const char *p, const char *s, const char *o, int t)
167 {
168 char tmp[64];
169 snprintf(tmp, 64, "%d", t);
170 ucix_add_option(ctx, p, s, o, tmp);
171 }
172
173 void ucix_del(struct uci_context *ctx, const char *p, const char *s, const char *o)
174 {
175 if(!ucix_get_ptr(ctx, p, s, o, NULL))
176 uci_delete(ctx, &ptr);
177 }
178
179 void ucix_revert(struct uci_context *ctx, const char *p, const char *s, const char *o)
180 {
181 if(!ucix_get_ptr(ctx, p, s, o, NULL))
182 uci_revert(ctx, &ptr);
183 }
184
185 void ucix_for_each_section_type(struct uci_context *ctx,
186 const char *p, const char *t,
187 void (*cb)(const char*, void*), void *priv)
188 {
189 struct uci_element *e;
190 if(ucix_get_ptr(ctx, p, NULL, NULL, NULL))
191 return;
192 uci_foreach_element(&ptr.p->sections, e)
193 if (!strcmp(t, uci_to_section(e)->type))
194 cb(e->name, priv);
195 }
196
197 void ucix_for_each_section_option(struct uci_context *ctx,
198 const char *p, const char *s,
199 void (*cb)(const char*, const char*, void*), void *priv)
200 {
201 struct uci_element *e;
202 if(ucix_get_ptr(ctx, p, s, NULL, NULL))
203 return;
204 uci_foreach_element(&ptr.s->options, e)
205 {
206 struct uci_option *o = uci_to_option(e);
207 cb(o->e.name, o->v.string, priv);
208 }
209 }
210
211 int ucix_commit(struct uci_context *ctx, const char *p)
212 {
213 if(ucix_get_ptr(ctx, p, NULL, NULL, NULL))
214 return 1;
215 return uci_commit(ctx, &ptr.p, false);
216 }