don't record a changed value if the new value matches the old one
[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 ptr = uci_malloc(ctx, datalen);
65 e = (struct uci_element *) ptr;
66 e->type = type;
67 if (name) {
68 UCI_TRAP_SAVE(ctx, error);
69 e->name = uci_strdup(ctx, name);
70 UCI_TRAP_RESTORE(ctx);
71 }
72 uci_list_init(&e->list);
73 goto done;
74
75 error:
76 free(ptr);
77 UCI_THROW(ctx, ctx->err);
78
79 done:
80 return e;
81 }
82
83 static void
84 uci_free_element(struct uci_element *e)
85 {
86 if (e->name)
87 free(e->name);
88 if (!uci_list_empty(&e->list))
89 uci_list_del(&e->list);
90 free(e);
91 }
92
93 static struct uci_option *
94 uci_alloc_option(struct uci_section *s, const char *name, const char *value)
95 {
96 struct uci_package *p = s->package;
97 struct uci_context *ctx = p->ctx;
98 struct uci_option *o;
99
100 o = uci_alloc_element(ctx, option, name, strlen(value) + 1);
101 o->value = uci_dataptr(o);
102 o->section = s;
103 strcpy(o->value, value);
104 uci_list_add(&s->options, &o->e.list);
105
106 return o;
107 }
108
109 static inline void
110 uci_free_option(struct uci_option *o)
111 {
112 if ((o->value != uci_dataptr(o)) &&
113 (o->value != NULL))
114 free(o->value);
115 uci_free_element(&o->e);
116 }
117
118 /* fix up an unnamed section, e.g. after adding options to it */
119 static void uci_fixup_section(struct uci_context *ctx, struct uci_section *s)
120 {
121 unsigned int hash = ~0;
122 struct uci_element *e;
123 char buf[16];
124
125 if (!s || s->e.name)
126 return;
127
128 /*
129 * Generate a name for unnamed sections. This is used as reference
130 * when locating or updating the section from apps/scripts.
131 * To make multiple concurrent versions somewhat safe for updating,
132 * the name is generated from a hash of its type and name/value
133 * pairs of its option, and it is prefixed by a counter value.
134 * If the order of the unnamed sections changes for some reason,
135 * updates to them will be rejected.
136 */
137 hash = djbhash(hash, s->type);
138 uci_foreach_element(&s->options, e) {
139 hash = djbhash(hash, e->name);
140 hash = djbhash(hash, uci_to_option(e)->value);
141 }
142 sprintf(buf, "cfg%02x%04x", ++s->package->n_section, hash % (1 << 16));
143 s->e.name = uci_strdup(ctx, buf);
144 }
145
146 static struct uci_section *
147 uci_alloc_section(struct uci_package *p, const char *type, const char *name)
148 {
149 struct uci_context *ctx = p->ctx;
150 struct uci_section *s;
151
152 if (name && !name[0])
153 name = NULL;
154
155 s = uci_alloc_element(ctx, section, name, strlen(type) + 1);
156 uci_list_init(&s->options);
157 s->type = uci_dataptr(s);
158 s->package = p;
159 strcpy(s->type, type);
160 if (name == NULL)
161 s->anonymous = true;
162 p->n_section++;
163
164 uci_list_add(&p->sections, &s->e.list);
165
166 return s;
167 }
168
169 static void
170 uci_free_section(struct uci_section *s)
171 {
172 struct uci_element *o, *tmp;
173
174 uci_foreach_element_safe(&s->options, tmp, o) {
175 uci_free_option(uci_to_option(o));
176 }
177 if ((s->type != uci_dataptr(s)) &&
178 (s->type != NULL))
179 free(s->type);
180 uci_free_element(&s->e);
181 }
182
183 __plugin struct uci_package *
184 uci_alloc_package(struct uci_context *ctx, const char *name)
185 {
186 struct uci_package *p;
187
188 p = uci_alloc_element(ctx, package, name, 0);
189 p->ctx = ctx;
190 uci_list_init(&p->sections);
191 uci_list_init(&p->history);
192 uci_list_init(&p->saved_history);
193 return p;
194 }
195
196 static void
197 uci_free_package(struct uci_package **package)
198 {
199 struct uci_element *e, *tmp;
200 struct uci_package *p = *package;
201
202 if(!p)
203 return;
204
205 if (p->path)
206 free(p->path);
207 uci_foreach_element_safe(&p->sections, tmp, e) {
208 uci_free_section(uci_to_section(e));
209 }
210 uci_foreach_element_safe(&p->history, tmp, e) {
211 uci_free_history(uci_to_history(e));
212 }
213 uci_foreach_element_safe(&p->saved_history, tmp, e) {
214 uci_free_history(uci_to_history(e));
215 }
216 uci_free_element(&p->e);
217 *package = NULL;
218 }
219
220 static struct uci_element *uci_lookup_list(struct uci_list *list, const char *name)
221 {
222 struct uci_element *e;
223
224 uci_foreach_element(list, e) {
225 if (!strcmp(e->name, name))
226 return e;
227 }
228 return NULL;
229 }
230
231 int uci_lookup(struct uci_context *ctx, struct uci_element **res, struct uci_package *p, const char *section, const char *option)
232 {
233 struct uci_element *e;
234 struct uci_section *s;
235
236 UCI_HANDLE_ERR(ctx);
237 UCI_ASSERT(ctx, res != NULL);
238 UCI_ASSERT(ctx, p != NULL);
239 UCI_ASSERT(ctx, section && uci_validate_name(section));
240 if (option)
241 UCI_ASSERT(ctx, uci_validate_name(option));
242
243 e = uci_lookup_list(&p->sections, section);
244 if (!e)
245 goto notfound;
246
247 if (option) {
248 s = uci_to_section(e);
249 e = uci_lookup_list(&s->options, option);
250 if (!e)
251 goto notfound;
252 }
253
254 *res = e;
255 return 0;
256
257 notfound:
258 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
259 return 0;
260 }
261
262 int uci_del_element(struct uci_context *ctx, struct uci_element *e)
263 {
264 /* NB: UCI_INTERNAL use means without history tracking */
265 bool internal = ctx->internal;
266 struct uci_package *p = NULL;
267 struct uci_section *s = NULL;
268 struct uci_option *o = NULL;
269 struct uci_element *i, *tmp;
270 char *option = NULL;
271
272 UCI_HANDLE_ERR(ctx);
273 UCI_ASSERT(ctx, e != NULL);
274
275 switch(e->type) {
276 case UCI_TYPE_SECTION:
277 s = uci_to_section(e);
278 uci_foreach_element_safe(&s->options, tmp, i) {
279 uci_del_element(ctx, i);
280 }
281 break;
282 case UCI_TYPE_OPTION:
283 o = uci_to_option(e);
284 s = o->section;
285 p = s->package;
286 option = e->name;
287 break;
288 default:
289 UCI_THROW(ctx, UCI_ERR_INVAL);
290 break;
291 }
292
293 p = s->package;
294 if (!internal && p->has_history)
295 uci_add_history(ctx, &p->history, UCI_CMD_REMOVE, s->e.name, option, NULL);
296
297 switch(e->type) {
298 case UCI_TYPE_SECTION:
299 uci_free_section(s);
300 break;
301 case UCI_TYPE_OPTION:
302 uci_free_option(o);
303 break;
304 default:
305 break;
306 }
307 return 0;
308 }
309
310 int uci_set_element_value(struct uci_context *ctx, struct uci_element **element, const char *value)
311 {
312 /* NB: UCI_INTERNAL use means without history tracking */
313 bool internal = ctx->internal;
314 struct uci_list *list;
315 struct uci_element *e;
316 struct uci_package *p;
317 struct uci_section *s;
318 char *section;
319 char *option;
320 char *str;
321 int size;
322
323 UCI_HANDLE_ERR(ctx);
324 UCI_ASSERT(ctx, (element != NULL) && (*element != NULL));
325
326 /* what the 'value' of an element means depends on the type
327 * for a section, the 'value' means its type
328 * for an option, the 'value' means its value string
329 * when changing the value, shrink the element to its actual size
330 * (it may have been allocated with a bigger size, to include
331 * its buffer)
332 * then duplicate the string passed on the command line and
333 * insert it into the structure.
334 */
335 e = *element;
336 list = e->list.prev;
337 switch(e->type) {
338 case UCI_TYPE_SECTION:
339 UCI_ASSERT(ctx, uci_validate_str(value, false));
340 size = sizeof(struct uci_section);
341 s = uci_to_section(e);
342 section = e->name;
343 option = NULL;
344 /* matches the currently set value */
345 if (!strcmp(value, s->type))
346 return 0;
347 break;
348 case UCI_TYPE_OPTION:
349 UCI_ASSERT(ctx, value != NULL);
350 size = sizeof(struct uci_option);
351 s = uci_to_option(e)->section;
352 section = s->e.name;
353 option = e->name;
354 /* matches the currently set value */
355 if (!strcmp(value, e->name))
356 return 0;
357 break;
358 default:
359 UCI_THROW(ctx, UCI_ERR_INVAL);
360 return 0;
361 }
362 p = s->package;
363 if (!internal && p->has_history)
364 uci_add_history(ctx, &p->history, UCI_CMD_CHANGE, section, option, value);
365
366 uci_list_del(&e->list);
367 e = uci_realloc(ctx, e, size);
368 str = uci_strdup(ctx, value);
369 uci_list_insert(list, &e->list);
370 *element = e;
371
372 switch(e->type) {
373 case UCI_TYPE_SECTION:
374 uci_to_section(e)->type = str;
375 break;
376 case UCI_TYPE_OPTION:
377 uci_to_option(e)->value = str;
378 break;
379 default:
380 break;
381 }
382
383 return 0;
384 }
385
386 int uci_rename(struct uci_context *ctx, struct uci_package *p, char *section, char *option, char *name)
387 {
388 /* NB: UCI_INTERNAL use means without history tracking */
389 bool internal = ctx->internal;
390 struct uci_element *e;
391
392 UCI_HANDLE_ERR(ctx);
393
394 /* NB: p, section, option validated by uci_lookup */
395 UCI_INTERNAL(uci_lookup, ctx, &e, p, section, option);
396
397 if (!internal && p->has_history)
398 uci_add_history(ctx, &p->history, UCI_CMD_RENAME, section, option, name);
399
400 name = uci_strdup(ctx, name);
401 if (e->name)
402 free(e->name);
403 e->name = name;
404
405 return 0;
406 }
407
408 int uci_add_section(struct uci_context *ctx, struct uci_package *p, const char *type, struct uci_section **res)
409 {
410 bool internal = ctx->internal;
411 struct uci_section *s;
412
413 UCI_HANDLE_ERR(ctx);
414 UCI_ASSERT(ctx, p != NULL);
415 s = uci_alloc_section(p, type, NULL);
416 uci_fixup_section(ctx, s);
417 *res = s;
418 if (!internal && p->has_history)
419 uci_add_history(ctx, &p->history, UCI_CMD_ADD, s->e.name, NULL, type);
420
421 return 0;
422 }
423
424 int uci_delete(struct uci_context *ctx, struct uci_package *p, const char *section, const char *option)
425 {
426 /* NB: pass on internal flag to uci_del_element */
427 bool internal = ctx->internal;
428 struct uci_element *e;
429
430 UCI_HANDLE_ERR(ctx);
431
432 /* NB: p, section, option validated by uci_lookup */
433 UCI_INTERNAL(uci_lookup, ctx, &e, p, section, option);
434
435 ctx->internal = internal;
436 return uci_del_element(ctx, e);
437 }
438
439 int uci_set(struct uci_context *ctx, struct uci_package *p, const char *section, const char *option, const char *value, struct uci_element **result)
440 {
441 /* NB: UCI_INTERNAL use means without history tracking */
442 bool internal = ctx->internal;
443 struct uci_element *e = NULL;
444 struct uci_section *s = NULL;
445 struct uci_option *o = NULL;
446
447 UCI_HANDLE_ERR(ctx);
448 UCI_ASSERT(ctx, p != NULL);
449 UCI_ASSERT(ctx, uci_validate_name(section));
450 if (option) {
451 UCI_ASSERT(ctx, uci_validate_name(option));
452 UCI_ASSERT(ctx, value != NULL);
453 } else {
454 UCI_ASSERT(ctx, uci_validate_str(value, false));
455 }
456
457 /*
458 * look up the package, section and option (if set)
459 * if the section/option is to be modified and it is not found
460 * create a new element in the appropriate list
461 */
462 e = uci_lookup_list(&p->sections, section);
463 if (!e)
464 goto notfound;
465
466 s = uci_to_section(e);
467 if (ctx->pctx && ctx->pctx->merge)
468 ctx->pctx->section = s;
469
470 if (option) {
471 e = uci_lookup_list(&s->options, option);
472 if (!e)
473 goto notfound;
474 o = uci_to_option(e);
475 }
476
477 /*
478 * no unknown element was supplied, assume that we can just update
479 * an existing entry
480 */
481 if (o)
482 e = &o->e;
483 else
484 e = &s->e;
485 if (result)
486 *result = e;
487 else
488 result = &e;
489
490 ctx->internal = internal;
491 return uci_set_element_value(ctx, result, value);
492
493 notfound:
494 /*
495 * the entry that we need to update was not found,
496 * check if the search failed prematurely.
497 * this can happen if the package was not found, or if
498 * an option was supplied, but the section wasn't found
499 */
500 if (!p || (!s && option))
501 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
502
503 /* now add the missing entry */
504 if (!internal && p->has_history)
505 uci_add_history(ctx, &p->history, UCI_CMD_CHANGE, section, option, value);
506 if (s) {
507 o = uci_alloc_option(s, option, value);
508 if (result)
509 *result = &o->e;
510 } else {
511 s = uci_alloc_section(p, value, section);
512 if (result)
513 *result = &s->e;
514 if (ctx->pctx && ctx->pctx->merge)
515 ctx->pctx->section = s;
516 }
517
518 return 0;
519 }
520
521 int uci_unload(struct uci_context *ctx, struct uci_package *p)
522 {
523 UCI_HANDLE_ERR(ctx);
524 UCI_ASSERT(ctx, p != NULL);
525
526 uci_free_package(&p);
527 return 0;
528 }
529