disable history tracking for non-confdir files
[project/uci.git] / list.c
1 /*
2 * libuci - Library for the Unified Configuration Interface
3 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
4 *
5 * this program is free software; you can redistribute it and/or modify
6 * it under the terms of the gnu lesser general public license version 2.1
7 * as published by the free software foundation
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15 #include <glob.h>
16
17 /* initialize a list head/item */
18 static inline void uci_list_init(struct uci_list *ptr)
19 {
20 ptr->prev = ptr;
21 ptr->next = ptr;
22 }
23
24 /* inserts a new list entry after a given entry */
25 static inline void uci_list_insert(struct uci_list *list, struct uci_list *ptr)
26 {
27 list->next->prev = ptr;
28 ptr->prev = list;
29 ptr->next = list->next;
30 list->next = ptr;
31 }
32
33 /* inserts a new list entry at the tail of the list */
34 static inline void uci_list_add(struct uci_list *head, struct uci_list *ptr)
35 {
36 /* NB: head->prev points at the tail */
37 uci_list_insert(head->prev, ptr);
38 }
39
40 static inline void uci_list_del(struct uci_list *ptr)
41 {
42 struct uci_list *next, *prev;
43
44 next = ptr->next;
45 prev = ptr->prev;
46
47 prev->next = next;
48 next->prev = prev;
49
50 uci_list_init(ptr);
51 }
52
53 /*
54 * uci_alloc_generic allocates a new uci_element with payload
55 * payload is appended to the struct to save memory and reduce fragmentation
56 */
57 static struct uci_element *
58 uci_alloc_generic(struct uci_context *ctx, int type, const char *name, int size)
59 {
60 struct uci_element *e;
61 int datalen = size;
62 void *ptr;
63
64 if (name)
65 datalen += strlen(name) + 1;
66 ptr = uci_malloc(ctx, datalen);
67 e = (struct uci_element *) ptr;
68 e->type = type;
69 if (name) {
70 e->name = (char *) ptr + size;
71 strcpy(e->name, name);
72 }
73 uci_list_init(&e->list);
74
75 return e;
76 }
77
78 static void
79 uci_free_element(struct uci_element *e)
80 {
81 if (!uci_list_empty(&e->list))
82 uci_list_del(&e->list);
83 free(e);
84 }
85
86 static struct uci_option *
87 uci_alloc_option(struct uci_section *s, const char *name, const char *value)
88 {
89 struct uci_package *p = s->package;
90 struct uci_context *ctx = p->ctx;
91 struct uci_option *o;
92
93 o = uci_alloc_element(ctx, option, name, strlen(value) + 1);
94 o->value = uci_dataptr(o);
95 o->section = s;
96 strcpy(o->value, value);
97 uci_list_add(&s->options, &o->e.list);
98
99 return o;
100 }
101
102 static inline void
103 uci_free_option(struct uci_option *o)
104 {
105 uci_free_element(&o->e);
106 }
107
108 static struct uci_section *
109 uci_alloc_section(struct uci_package *p, const char *type, const char *name)
110 {
111 struct uci_context *ctx = p->ctx;
112 struct uci_section *s;
113 char buf[16];
114
115 if (!name || !name[0]) {
116 snprintf(buf, 16, "cfg%d", p->n_section);
117 name = buf;
118 }
119
120 s = uci_alloc_element(ctx, section, name, strlen(type) + 1);
121 s->type = uci_dataptr(s);
122 s->package = p;
123 strcpy(s->type, type);
124 uci_list_init(&s->options);
125 uci_list_add(&p->sections, &s->e.list);
126
127 return s;
128 }
129
130 static void
131 uci_free_section(struct uci_section *s)
132 {
133 struct uci_element *o, *tmp;
134
135 uci_foreach_element_safe(&s->options, tmp, o) {
136 uci_free_option(uci_to_option(o));
137 }
138 uci_free_element(&s->e);
139 }
140
141 static struct uci_package *
142 uci_alloc_package(struct uci_context *ctx, const char *name)
143 {
144 struct uci_package *p;
145
146 p = uci_alloc_element(ctx, package, name, 0);
147 p->ctx = ctx;
148 uci_list_init(&p->sections);
149 uci_list_init(&p->history);
150 return p;
151 }
152
153 static void
154 uci_free_package(struct uci_package *p)
155 {
156 struct uci_element *e, *tmp;
157
158 if(!p)
159 return;
160
161 if (p->path)
162 free(p->path);
163 uci_foreach_element_safe(&p->sections, tmp, e) {
164 uci_free_section(uci_to_section(e));
165 }
166 uci_free_element(&p->e);
167 }
168
169 /* record a change that was done to a package */
170 static inline void
171 uci_add_history(struct uci_context *ctx, struct uci_package *p, int cmd, char *section, char *option, char *value)
172 {
173 struct uci_history *h;
174 int size = strlen(section) + 1;
175 char *ptr;
176
177 if (!p->confdir)
178 return;
179
180 if (value)
181 size += strlen(section) + 1;
182
183 h = uci_alloc_element(ctx, history, option, size);
184 ptr = uci_dataptr(h);
185 h->cmd = cmd;
186 h->section = strcpy(ptr, section);
187 if (value) {
188 ptr += strlen(ptr) + 1;
189 h->value = strcpy(ptr, value);
190 }
191 uci_list_add(&p->history, &h->e.list);
192 }
193
194 static struct uci_element *uci_lookup_list(struct uci_context *ctx, struct uci_list *list, const char *name)
195 {
196 struct uci_element *e;
197
198 uci_foreach_element(list, e) {
199 if (!strcmp(e->name, name))
200 return e;
201 }
202 return NULL;
203 }
204
205 int uci_lookup(struct uci_context *ctx, struct uci_element **res, struct uci_package *p, char *section, char *option)
206 {
207 struct uci_element *e;
208 struct uci_section *s;
209 struct uci_option *o;
210
211 UCI_HANDLE_ERR(ctx);
212 UCI_ASSERT(ctx, res != NULL);
213 UCI_ASSERT(ctx, p != NULL);
214 UCI_ASSERT(ctx, section != NULL);
215
216 e = uci_lookup_list(ctx, &p->sections, section);
217 if (!e)
218 goto notfound;
219
220 if (option) {
221 s = uci_to_section(e);
222 e = uci_lookup_list(ctx, &s->options, option);
223 }
224
225 *res = e;
226 return 0;
227
228 notfound:
229 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
230 return 0;
231 }
232
233 int uci_del_element(struct uci_context *ctx, struct uci_element *e)
234 {
235 bool internal = ctx->internal;
236 struct uci_package *p = NULL;
237 struct uci_section *s = NULL;
238 struct uci_option *o = NULL;
239 struct uci_element *i, *tmp;
240 char *option = NULL;
241
242 UCI_HANDLE_ERR(ctx);
243 UCI_ASSERT(ctx, e != NULL);
244
245 switch(e->type) {
246 case UCI_TYPE_SECTION:
247 s = uci_to_section(e);
248 uci_foreach_element_safe(&s->options, tmp, i) {
249 uci_del_element(ctx, i);
250 }
251 break;
252 case UCI_TYPE_OPTION:
253 o = uci_to_option(e);
254 s = o->section;
255 p = s->package;
256 option = e->name;
257 break;
258 default:
259 UCI_THROW(ctx, UCI_ERR_INVAL);
260 break;
261 }
262
263 p = s->package;
264 if (!internal)
265 uci_add_history(ctx, p, UCI_CMD_REMOVE, s->e.name, option, NULL);
266
267 switch(e->type) {
268 case UCI_TYPE_SECTION:
269 uci_free_section(s);
270 break;
271 case UCI_TYPE_OPTION:
272 uci_free_option(o);
273 break;
274 default:
275 break;
276 }
277 return 0;
278 }
279
280 int uci_set_element_value(struct uci_context *ctx, struct uci_element **element, char *value)
281 {
282 bool internal = ctx->internal;
283 struct uci_list *list;
284 struct uci_element *e;
285 struct uci_package *p;
286 struct uci_section *s;
287 char *section;
288 char *option;
289 char *str;
290 int size;
291
292 UCI_HANDLE_ERR(ctx);
293 UCI_ASSERT(ctx, value != NULL);
294 UCI_ASSERT(ctx, element != NULL);
295 UCI_ASSERT(ctx, *element != NULL);
296
297 /* what the 'value' of an element means depends on the type
298 * for a section, the 'value' means its type
299 * for an option, the 'value' means its value string
300 * when changing the value, shrink the element to its actual size
301 * (it may have been allocated with a bigger size, to include
302 * its buffer)
303 * then duplicate the string passed on the command line and
304 * insert it into the structure.
305 */
306 e = *element;
307 list = e->list.prev;
308 switch(e->type) {
309 case UCI_TYPE_SECTION:
310 size = sizeof(struct uci_section);
311 s = uci_to_section(e);
312 section = e->name;
313 option = NULL;
314 break;
315 case UCI_TYPE_OPTION:
316 size = sizeof(struct uci_option);
317 s = uci_to_option(e)->section;
318 section = s->e.name;
319 option = e->name;
320 break;
321 default:
322 UCI_THROW(ctx, UCI_ERR_INVAL);
323 return 0;
324 }
325 p = s->package;
326 if (!internal)
327 uci_add_history(ctx, p, UCI_CMD_CHANGE, section, option, value);
328
329 uci_list_del(&e->list);
330 e = uci_realloc(ctx, e, size);
331 str = uci_strdup(ctx, value);
332 uci_list_insert(list, &e->list);
333 *element = e;
334
335 switch(e->type) {
336 case UCI_TYPE_SECTION:
337 uci_to_section(e)->type = str;
338 break;
339 case UCI_TYPE_OPTION:
340 uci_to_option(e)->value = str;
341 break;
342 default:
343 break;
344 }
345
346 return 0;
347 }
348
349 int uci_del(struct uci_context *ctx, struct uci_package *p, char *section, char *option)
350 {
351 bool internal = ctx->internal;
352 struct uci_element *e;
353 struct uci_section *s = NULL;
354 struct uci_option *o = NULL;
355
356 UCI_HANDLE_ERR(ctx);
357 UCI_ASSERT(ctx, p != NULL);
358 UCI_ASSERT(ctx, section != NULL);
359
360 UCI_INTERNAL(uci_lookup, ctx, &e, p, section, option);
361
362 if (!internal)
363 return uci_del_element(ctx, e);
364 UCI_INTERNAL(uci_del_element, ctx, e);
365
366 return 0;
367 }
368
369 int uci_set(struct uci_context *ctx, struct uci_package *p, char *section, char *option, char *value)
370 {
371 bool internal = ctx->internal;
372 struct uci_element *e = NULL;
373 struct uci_section *s = NULL;
374 struct uci_option *o = NULL;
375 struct uci_history *h;
376
377 UCI_HANDLE_ERR(ctx);
378 UCI_ASSERT(ctx, p != NULL);
379 UCI_ASSERT(ctx, section != NULL);
380 UCI_ASSERT(ctx, value != NULL);
381
382 /*
383 * look up the package, section and option (if set)
384 * if the section/option is to be modified and it is not found
385 * create a new element in the appropriate list
386 */
387 UCI_INTERNAL(uci_lookup, ctx, &e, p, section, NULL);
388 s = uci_to_section(e);
389 if (option) {
390 e = uci_lookup_list(ctx, &s->options, option);
391 if (!e)
392 goto notfound;
393 o = uci_to_option(e);
394 }
395
396 /*
397 * no unknown element was supplied, assume that we can just update
398 * an existing entry
399 */
400 if (o)
401 e = &o->e;
402 else
403 e = &s->e;
404
405 if (!internal)
406 return uci_set_element_value(ctx, &e, value);
407
408 UCI_INTERNAL(uci_set_element_value, ctx, &e, value);
409 return 0;
410
411 notfound:
412 /*
413 * the entry that we need to update was not found,
414 * check if the search failed prematurely.
415 * this can happen if the package was not found, or if
416 * an option was supplied, but the section wasn't found
417 */
418 if (!p || (!s && option))
419 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
420
421 /* now add the missing entry */
422 if (!internal)
423 uci_add_history(ctx, p, UCI_CMD_ADD, section, option, value);
424 if (s)
425 uci_alloc_option(s, option, value);
426 else
427 uci_alloc_section(p, value, section);
428
429 return 0;
430 }
431
432 int uci_unload(struct uci_context *ctx, struct uci_package *p)
433 {
434 struct uci_element *e;
435
436 UCI_HANDLE_ERR(ctx);
437 UCI_ASSERT(ctx, p != NULL);
438
439 uci_free_package(p);
440 return 0;
441 }
442