provide backwards compatibility in the lua module after the history -> delta change
[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 static void uci_list_set_pos(struct uci_list *head, struct uci_list *ptr, int pos)
16 {
17         struct uci_list *new_head = head;
18         struct uci_element *p = NULL;
19
20         uci_list_del(ptr);
21         uci_foreach_element(head, p) {
22                 new_head = &p->list;
23                 if (pos-- <= 0)
24                         break;
25         }
26         uci_list_add(new_head, ptr);
27 }
28
29 static inline void uci_list_fixup(struct uci_list *ptr)
30 {
31         ptr->prev->next = ptr;
32         ptr->next->prev = ptr;
33 }
34
35 /* 
36  * uci_alloc_generic allocates a new uci_element with payload
37  * payload is appended to the struct to save memory and reduce fragmentation
38  */
39 __private struct uci_element *
40 uci_alloc_generic(struct uci_context *ctx, int type, const char *name, int size)
41 {
42         struct uci_element *e;
43         int datalen = size;
44         void *ptr;
45
46         ptr = uci_malloc(ctx, datalen);
47         e = (struct uci_element *) ptr;
48         e->type = type;
49         if (name) {
50                 UCI_TRAP_SAVE(ctx, error);
51                 e->name = uci_strdup(ctx, name);
52                 UCI_TRAP_RESTORE(ctx);
53         }
54         uci_list_init(&e->list);
55         goto done;
56
57 error:
58         free(ptr);
59         UCI_THROW(ctx, ctx->err);
60
61 done:
62         return e;
63 }
64
65 __private void
66 uci_free_element(struct uci_element *e)
67 {
68         if (e->name)
69                 free(e->name);
70         if (!uci_list_empty(&e->list))
71                 uci_list_del(&e->list);
72         free(e);
73 }
74
75 static struct uci_option *
76 uci_alloc_option(struct uci_section *s, const char *name, const char *value)
77 {
78         struct uci_package *p = s->package;
79         struct uci_context *ctx = p->ctx;
80         struct uci_option *o;
81
82         o = uci_alloc_element(ctx, option, name, strlen(value) + 1);
83         o->type = UCI_TYPE_STRING;
84         o->v.string = uci_dataptr(o);
85         o->section = s;
86         strcpy(o->v.string, value);
87         uci_list_add(&s->options, &o->e.list);
88
89         return o;
90 }
91
92 static inline void
93 uci_free_option(struct uci_option *o)
94 {
95         struct uci_element *e, *tmp;
96
97         switch(o->type) {
98         case UCI_TYPE_STRING:
99                 if ((o->v.string != uci_dataptr(o)) &&
100                         (o->v.string != NULL))
101                         free(o->v.string);
102                 break;
103         case UCI_TYPE_LIST:
104                 uci_foreach_element_safe(&o->v.list, tmp, e) {
105                         uci_free_element(e);
106                 }
107                 break;
108         default:
109                 break;
110         }
111         uci_free_element(&o->e);
112 }
113
114 static struct uci_option *
115 uci_alloc_list(struct uci_section *s, const char *name)
116 {
117         struct uci_package *p = s->package;
118         struct uci_context *ctx = p->ctx;
119         struct uci_option *o;
120
121         o = uci_alloc_element(ctx, option, name, 0);
122         o->type = UCI_TYPE_LIST;
123         o->section = s;
124         uci_list_init(&o->v.list);
125         uci_list_add(&s->options, &o->e.list);
126
127         return o;
128 }
129
130 /* Based on an efficient hash function published by D. J. Bernstein */
131 static unsigned int djbhash(unsigned int hash, char *str)
132 {
133         int len = strlen(str);
134         int i;
135
136         /* initial value */
137         if (hash == ~0)
138                 hash = 5381;
139
140         for(i = 0; i < len; i++) {
141                 hash = ((hash << 5) + hash) + str[i];
142         }
143         return (hash & 0x7FFFFFFF);
144 }
145
146 /* fix up an unnamed section, e.g. after adding options to it */
147 __private void uci_fixup_section(struct uci_context *ctx, struct uci_section *s)
148 {
149         unsigned int hash = ~0;
150         struct uci_element *e;
151         char buf[16];
152
153         if (!s || s->e.name)
154                 return;
155
156         /*
157          * Generate a name for unnamed sections. This is used as reference
158          * when locating or updating the section from apps/scripts.
159          * To make multiple concurrent versions somewhat safe for updating,
160          * the name is generated from a hash of its type and name/value
161          * pairs of its option, and it is prefixed by a counter value.
162          * If the order of the unnamed sections changes for some reason,
163          * updates to them will be rejected.
164          */
165         hash = djbhash(hash, s->type);
166         uci_foreach_element(&s->options, e) {
167                 struct uci_option *o;
168                 hash = djbhash(hash, e->name);
169                 o = uci_to_option(e);
170                 switch(o->type) {
171                 case UCI_TYPE_STRING:
172                         hash = djbhash(hash, o->v.string);
173                         break;
174                 default:
175                         break;
176                 }
177         }
178         sprintf(buf, "cfg%02x%04x", ++s->package->n_section, hash % (1 << 16));
179         s->e.name = uci_strdup(ctx, buf);
180 }
181
182 static struct uci_section *
183 uci_alloc_section(struct uci_package *p, const char *type, const char *name)
184 {
185         struct uci_context *ctx = p->ctx;
186         struct uci_section *s;
187
188         if (name && !name[0])
189                 name = NULL;
190
191         s = uci_alloc_element(ctx, section, name, strlen(type) + 1);
192         uci_list_init(&s->options);
193         s->type = uci_dataptr(s);
194         s->package = p;
195         strcpy(s->type, type);
196         if (name == NULL)
197                 s->anonymous = true;
198         p->n_section++;
199
200         uci_list_add(&p->sections, &s->e.list);
201
202         return s;
203 }
204
205 static void
206 uci_free_section(struct uci_section *s)
207 {
208         struct uci_element *o, *tmp;
209
210         uci_foreach_element_safe(&s->options, tmp, o) {
211                 uci_free_option(uci_to_option(o));
212         }
213         if ((s->type != uci_dataptr(s)) &&
214                 (s->type != NULL))
215                 free(s->type);
216         uci_free_element(&s->e);
217 }
218
219 __plugin struct uci_package *
220 uci_alloc_package(struct uci_context *ctx, const char *name)
221 {
222         struct uci_package *p;
223
224         p = uci_alloc_element(ctx, package, name, 0);
225         p->ctx = ctx;
226         uci_list_init(&p->sections);
227         uci_list_init(&p->delta);
228         uci_list_init(&p->saved_delta);
229         return p;
230 }
231
232 __private void
233 uci_free_package(struct uci_package **package)
234 {
235         struct uci_element *e, *tmp;
236         struct uci_package *p = *package;
237
238         if(!p)
239                 return;
240
241         if (p->path)
242                 free(p->path);
243         uci_foreach_element_safe(&p->sections, tmp, e) {
244                 uci_free_section(uci_to_section(e));
245         }
246         uci_foreach_element_safe(&p->delta, tmp, e) {
247                 uci_free_delta(uci_to_delta(e));
248         }
249         uci_foreach_element_safe(&p->saved_delta, tmp, e) {
250                 uci_free_delta(uci_to_delta(e));
251         }
252         uci_free_element(&p->e);
253         *package = NULL;
254 }
255
256 static void
257 uci_free_any(struct uci_element **e)
258 {
259         switch((*e)->type) {
260         case UCI_TYPE_SECTION:
261                 uci_free_section(uci_to_section(*e));
262                 break;
263         case UCI_TYPE_OPTION:
264                 uci_free_option(uci_to_option(*e));
265                 break;
266         default:
267                 break;
268         }
269         *e = NULL;
270 }
271
272 __private struct uci_element *
273 uci_lookup_list(struct uci_list *list, const char *name)
274 {
275         struct uci_element *e;
276
277         uci_foreach_element(list, e) {
278                 if (!strcmp(e->name, name))
279                         return e;
280         }
281         return NULL;
282 }
283
284 static struct uci_element *
285 uci_lookup_ext_section(struct uci_context *ctx, struct uci_ptr *ptr)
286 {
287         char *idxstr, *t, *section, *name;
288         struct uci_element *e = NULL;
289         struct uci_section *s;
290         int idx, c;
291
292         section = uci_strdup(ctx, ptr->section);
293         name = idxstr = section + 1;
294
295         if (section[0] != '@')
296                 goto error;
297
298         /* parse the section index part */
299         idxstr = strchr(idxstr, '[');
300         if (!idxstr)
301                 goto error;
302         *idxstr = 0;
303         idxstr++;
304
305         t = strchr(idxstr, ']');
306         if (!t)
307                 goto error;
308         if (t[1] != 0)
309                 goto error;
310         *t = 0;
311
312         t = NULL;
313         idx = strtol(idxstr, &t, 10);
314         if (t && *t)
315                 goto error;
316
317         if (!*name)
318                 name = NULL;
319         else if (!uci_validate_type(name))
320                 goto error;
321
322         /* if the given index is negative, it specifies the section number from 
323          * the end of the list */
324         if (idx < 0) {
325                 c = 0;
326                 uci_foreach_element(&ptr->p->sections, e) {
327                         s = uci_to_section(e);
328                         if (name && (strcmp(s->type, name) != 0))
329                                 continue;
330
331                         c++;
332                 }
333                 idx += c;
334         }
335
336         c = 0;
337         uci_foreach_element(&ptr->p->sections, e) {
338                 s = uci_to_section(e);
339                 if (name && (strcmp(s->type, name) != 0))
340                         continue;
341
342                 if (idx == c)
343                         goto done;
344                 c++;
345         }
346         e = NULL;
347         goto done;
348
349 error:
350         e = NULL;
351         memset(ptr, 0, sizeof(struct uci_ptr));
352         UCI_THROW(ctx, UCI_ERR_INVAL);
353 done:
354         free(section);
355         if (e)
356                 ptr->section = e->name;
357         return e;
358 }
359
360 int
361 uci_lookup_ptr(struct uci_context *ctx, struct uci_ptr *ptr, char *str, bool extended)
362 {
363         struct uci_element *e;
364
365         UCI_HANDLE_ERR(ctx);
366         UCI_ASSERT(ctx, ptr != NULL);
367
368         if (str)
369                 UCI_INTERNAL(uci_parse_ptr, ctx, ptr, str);
370
371         ptr->flags |= UCI_LOOKUP_DONE;
372
373         /* look up the package first */
374         e = uci_lookup_list(&ctx->root, ptr->package);
375         if (!e) {
376                 UCI_INTERNAL(uci_load, ctx, ptr->package, &ptr->p);
377                 if (!ptr->p)
378                         goto notfound;
379                 ptr->last = &ptr->p->e;
380         } else {
381                 ptr->p = uci_to_package(e);
382                 ptr->last = e;
383         }
384
385         if (!ptr->section)
386                 goto complete;
387
388         /* if the section name validates as a regular name, pass through
389          * to the regular uci_lookup function call */
390         if (ptr->flags & UCI_LOOKUP_EXTENDED) {
391                 if (extended)
392                         e = uci_lookup_ext_section(ctx, ptr);
393                 else
394                         UCI_THROW(ctx, UCI_ERR_INVAL);
395         } else {
396                 e = uci_lookup_list(&ptr->p->sections, ptr->section);
397         }
398
399         if (!e)
400                 goto abort;
401
402         ptr->last = e;
403         ptr->s = uci_to_section(e);
404
405         if (ptr->option) {
406                 e = uci_lookup_list(&ptr->s->options, ptr->option);
407                 if (!e)
408                         goto abort;
409
410                 ptr->o = uci_to_option(e);
411                 ptr->last = e;
412         }
413
414 complete:
415         ptr->flags |= UCI_LOOKUP_COMPLETE;
416 abort:
417         return 0;
418
419 notfound:
420         UCI_THROW(ctx, UCI_ERR_NOTFOUND);
421         return 0;
422 }
423
424 __private struct uci_element *
425 uci_expand_ptr(struct uci_context *ctx, struct uci_ptr *ptr, bool complete)
426 {
427         UCI_ASSERT(ctx, ptr != NULL);
428
429         if (!(ptr->flags & UCI_LOOKUP_DONE))
430                 UCI_INTERNAL(uci_lookup_ptr, ctx, ptr, NULL, 1);
431         if (complete && !(ptr->flags & UCI_LOOKUP_COMPLETE))
432                 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
433         UCI_ASSERT(ctx, ptr->p != NULL);
434
435         /* fill in missing string info */
436         if (ptr->p && !ptr->package)
437                 ptr->package = ptr->p->e.name;
438         if (ptr->s && !ptr->section)
439                 ptr->section = ptr->s->e.name;
440         if (ptr->o && !ptr->option)
441                 ptr->option = ptr->o->e.name;
442
443         if (ptr->o)
444                 return &ptr->o->e;
445         if (ptr->s)
446                 return &ptr->s->e;
447         if (ptr->p)
448                 return &ptr->p->e;
449         else
450                 return NULL;
451 }
452
453 static void uci_add_element_list(struct uci_context *ctx, struct uci_ptr *ptr, bool internal)
454 {
455         struct uci_element *e;
456         struct uci_package *p;
457
458         p = ptr->p;
459         if (!internal && p->has_delta)
460                 uci_add_delta(ctx, &p->delta, UCI_CMD_LIST_ADD, ptr->section, ptr->option, ptr->value);
461
462         e = uci_alloc_generic(ctx, UCI_TYPE_ITEM, ptr->value, sizeof(struct uci_option));
463         uci_list_add(&ptr->o->v.list, &e->list);
464 }
465
466 int uci_rename(struct uci_context *ctx, struct uci_ptr *ptr)
467 {
468         /* NB: UCI_INTERNAL use means without delta tracking */
469         bool internal = ctx->internal;
470         struct uci_element *e;
471         struct uci_package *p;
472         char *n;
473
474         UCI_HANDLE_ERR(ctx);
475
476         e = uci_expand_ptr(ctx, ptr, true);
477         p = ptr->p;
478
479         UCI_ASSERT(ctx, ptr->s);
480         UCI_ASSERT(ctx, ptr->value);
481
482         if (!internal && p->has_delta)
483                 uci_add_delta(ctx, &p->delta, UCI_CMD_RENAME, ptr->section, ptr->option, ptr->value);
484
485         n = uci_strdup(ctx, ptr->value);
486         if (e->name)
487                 free(e->name);
488         e->name = n;
489
490         if (e->type == UCI_TYPE_SECTION)
491                 uci_to_section(e)->anonymous = false;
492
493         return 0;
494 }
495
496 int uci_reorder_section(struct uci_context *ctx, struct uci_section *s, int pos)
497 {
498         struct uci_package *p = s->package;
499         char order[32];
500
501         UCI_HANDLE_ERR(ctx);
502
503         uci_list_set_pos(&s->package->sections, &s->e.list, pos);
504         if (!ctx->internal && p->has_delta) {
505                 sprintf(order, "%d", pos);
506                 uci_add_delta(ctx, &p->delta, UCI_CMD_REORDER, s->e.name, NULL, order);
507         }
508
509         return 0;
510 }
511
512 int uci_add_section(struct uci_context *ctx, struct uci_package *p, const char *type, struct uci_section **res)
513 {
514         bool internal = ctx->internal;
515         struct uci_section *s;
516
517         UCI_HANDLE_ERR(ctx);
518         UCI_ASSERT(ctx, p != NULL);
519         s = uci_alloc_section(p, type, NULL);
520         uci_fixup_section(ctx, s);
521         *res = s;
522         if (!internal && p->has_delta)
523                 uci_add_delta(ctx, &p->delta, UCI_CMD_ADD, s->e.name, NULL, type);
524
525         return 0;
526 }
527
528 int uci_delete(struct uci_context *ctx, struct uci_ptr *ptr)
529 {
530         /* NB: pass on internal flag to uci_del_element */
531         bool internal = ctx->internal;
532         struct uci_package *p;
533         struct uci_element *e;
534
535         UCI_HANDLE_ERR(ctx);
536
537         e = uci_expand_ptr(ctx, ptr, true);
538         p = ptr->p;
539
540         UCI_ASSERT(ctx, ptr->s);
541
542         if (!internal && p->has_delta)
543                 uci_add_delta(ctx, &p->delta, UCI_CMD_REMOVE, ptr->section, ptr->option, NULL);
544
545         uci_free_any(&e);
546
547         if (ptr->option)
548                 ptr->o = NULL;
549         else if (ptr->section)
550                 ptr->s = NULL;
551
552         return 0;
553 }
554
555 int uci_add_list(struct uci_context *ctx, struct uci_ptr *ptr)
556 {
557         /* NB: UCI_INTERNAL use means without delta tracking */
558         bool internal = ctx->internal;
559         struct uci_option *prev = NULL;
560         const char *value2 = NULL;
561
562         UCI_HANDLE_ERR(ctx);
563
564         uci_expand_ptr(ctx, ptr, false);
565         UCI_ASSERT(ctx, ptr->s);
566         UCI_ASSERT(ctx, ptr->value);
567
568         if (ptr->o) {
569                 switch (ptr->o->type) {
570                 case UCI_TYPE_STRING:
571                         /* we already have a string value, convert that to a list */
572                         prev = ptr->o;
573                         value2 = ptr->value;
574                         ptr->value = ptr->o->v.string;
575                         break;
576                 case UCI_TYPE_LIST:
577                         uci_add_element_list(ctx, ptr, internal);
578                         return 0;
579                 default:
580                         UCI_THROW(ctx, UCI_ERR_INVAL);
581                         break;
582                 }
583         }
584
585         ptr->o = uci_alloc_list(ptr->s, ptr->option);
586         if (prev) {
587                 uci_add_element_list(ctx, ptr, true);
588                 uci_free_option(prev);
589                 ptr->value = value2;
590         }
591         uci_add_element_list(ctx, ptr, internal);
592
593         return 0;
594 }
595
596 int uci_set(struct uci_context *ctx, struct uci_ptr *ptr)
597 {
598         /* NB: UCI_INTERNAL use means without delta tracking */
599         bool internal = ctx->internal;
600
601         UCI_HANDLE_ERR(ctx);
602         uci_expand_ptr(ctx, ptr, false);
603         UCI_ASSERT(ctx, ptr->value);
604         UCI_ASSERT(ctx, ptr->s || (!ptr->option && ptr->section));
605         if (!ptr->option && ptr->value[0]) {
606                 UCI_ASSERT(ctx, uci_validate_type(ptr->value));
607         }
608
609         if (!ptr->o && ptr->s && ptr->option) {
610                 struct uci_element *e;
611                 e = uci_lookup_list(&ptr->s->options, ptr->option);
612                 if (e)
613                         ptr->o = uci_to_option(e);
614         }
615         if (!ptr->value[0]) {
616                 /* if setting a nonexistant option/section to a nonexistant value,
617                  * exit without errors */
618                 if (!(ptr->flags & UCI_LOOKUP_COMPLETE))
619                         return 0;
620
621                 return uci_delete(ctx, ptr);
622         } else if (!ptr->o && ptr->option) { /* new option */
623                 ptr->o = uci_alloc_option(ptr->s, ptr->option, ptr->value);
624                 ptr->last = &ptr->o->e;
625         } else if (!ptr->s && ptr->section) { /* new section */
626                 ptr->s = uci_alloc_section(ptr->p, ptr->value, ptr->section);
627                 ptr->last = &ptr->s->e;
628         } else if (ptr->o && ptr->option) { /* update option */
629                 if ((ptr->o->type == UCI_TYPE_STRING) &&
630                         !strcmp(ptr->o->v.string, ptr->value))
631                         return 0;
632                 uci_free_option(ptr->o);
633                 ptr->o = uci_alloc_option(ptr->s, ptr->option, ptr->value);
634                 ptr->last = &ptr->o->e;
635         } else if (ptr->s && ptr->section) { /* update section */
636                 char *s = uci_strdup(ctx, ptr->value);
637
638                 if (ptr->s->type == uci_dataptr(ptr->s)) {
639                         ptr->last = NULL;
640                         ptr->last = uci_realloc(ctx, ptr->s, sizeof(struct uci_section));
641                         ptr->s = uci_to_section(ptr->last);
642                         uci_list_fixup(&ptr->s->e.list);
643                 } else {
644                         free(ptr->s->type);
645                 }
646                 ptr->s->type = s;
647         } else {
648                 UCI_THROW(ctx, UCI_ERR_INVAL);
649         }
650
651         if (!internal && ptr->p->has_delta)
652                 uci_add_delta(ctx, &ptr->p->delta, UCI_CMD_CHANGE, ptr->section, ptr->option, ptr->value);
653
654         return 0;
655 }
656
657 int uci_unload(struct uci_context *ctx, struct uci_package *p)
658 {
659         UCI_HANDLE_ERR(ctx);
660         UCI_ASSERT(ctx, p != NULL);
661
662         uci_free_package(&p);
663         return 0;
664 }
665