use libdl
[project/uci.git] / file.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 /*
16  * This file contains the code for parsing uci config files
17  */
18
19 #define _GNU_SOURCE
20 #include <sys/types.h>
21 #include <sys/file.h>
22 #include <stdbool.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <ctype.h>
27 #include <glob.h>
28 #include <string.h>
29 #include <stdlib.h>
30
31 #include "uci.h"
32 #include "uci_internal.h"
33
34 #define LINEBUF 32
35 #define LINEBUF_MAX     4096
36
37 /*
38  * Fetch a new line from the input stream and resize buffer if necessary
39  */
40 __private void uci_getln(struct uci_context *ctx, int offset)
41 {
42         struct uci_parse_context *pctx = ctx->pctx;
43         char *p;
44         int ofs;
45
46         if (pctx->buf == NULL) {
47                 pctx->buf = uci_malloc(ctx, LINEBUF);
48                 pctx->bufsz = LINEBUF;
49         }
50
51         ofs = offset;
52         do {
53                 p = &pctx->buf[ofs];
54                 p[ofs] = 0;
55
56                 p = fgets(p, pctx->bufsz - ofs, pctx->file);
57                 if (!p || !*p)
58                         return;
59
60                 ofs += strlen(p);
61                 if (pctx->buf[ofs - 1] == '\n') {
62                         pctx->line++;
63                         pctx->buf[ofs - 1] = 0;
64                         return;
65                 }
66
67                 if (pctx->bufsz > LINEBUF_MAX/2)
68                         uci_parse_error(ctx, p, "line too long");
69
70                 pctx->bufsz *= 2;
71                 pctx->buf = uci_realloc(ctx, pctx->buf, pctx->bufsz);
72         } while (1);
73 }
74
75
76 /*
77  * parse a character escaped by '\'
78  * returns true if the escaped character is to be parsed
79  * returns false if the escaped character is to be ignored
80  */
81 static inline bool parse_backslash(struct uci_context *ctx, char **str)
82 {
83         /* skip backslash */
84         *str += 1;
85
86         /* undecoded backslash at the end of line, fetch the next line */
87         if (!**str) {
88                 *str += 1;
89                 uci_getln(ctx, *str - ctx->pctx->buf);
90                 return false;
91         }
92
93         /* FIXME: decode escaped char, necessary? */
94         return true;
95 }
96
97 /*
98  * move the string pointer forward until a non-whitespace character or
99  * EOL is reached
100  */
101 static void skip_whitespace(struct uci_context *ctx, char **str)
102 {
103 restart:
104         while (**str && isspace(**str))
105                 *str += 1;
106
107         if (**str == '\\') {
108                 if (!parse_backslash(ctx, str))
109                         goto restart;
110         }
111 }
112
113 static inline void addc(char **dest, char **src)
114 {
115         **dest = **src;
116         *dest += 1;
117         *src += 1;
118 }
119
120 /*
121  * parse a double quoted string argument from the command line
122  */
123 static void parse_double_quote(struct uci_context *ctx, char **str, char **target)
124 {
125         char c;
126
127         /* skip quote character */
128         *str += 1;
129
130         while ((c = **str)) {
131                 switch(c) {
132                 case '"':
133                         **target = 0;
134                         *str += 1;
135                         return;
136                 case '\\':
137                         if (!parse_backslash(ctx, str))
138                                 continue;
139                         /* fall through */
140                 default:
141                         addc(target, str);
142                         break;
143                 }
144         }
145         uci_parse_error(ctx, *str, "unterminated \"");
146 }
147
148 /*
149  * parse a single quoted string argument from the command line
150  */
151 static void parse_single_quote(struct uci_context *ctx, char **str, char **target)
152 {
153         char c;
154         /* skip quote character */
155         *str += 1;
156
157         while ((c = **str)) {
158                 switch(c) {
159                 case '\'':
160                         **target = 0;
161                         *str += 1;
162                         return;
163                 default:
164                         addc(target, str);
165                 }
166         }
167         uci_parse_error(ctx, *str, "unterminated '");
168 }
169
170 /*
171  * parse a string from the command line and detect the quoting style
172  */
173 static void parse_str(struct uci_context *ctx, char **str, char **target)
174 {
175         bool next = true;
176         do {
177                 switch(**str) {
178                 case '\'':
179                         parse_single_quote(ctx, str, target);
180                         break;
181                 case '"':
182                         parse_double_quote(ctx, str, target);
183                         break;
184                 case '#':
185                         **str = 0;
186                         /* fall through */
187                 case 0:
188                         goto done;
189                 case ';':
190                         next = false;
191                         goto done;
192                 case '\\':
193                         if (!parse_backslash(ctx, str))
194                                 continue;
195                         /* fall through */
196                 default:
197                         addc(target, str);
198                         break;
199                 }
200         } while (**str && !isspace(**str));
201 done:
202
203         /* 
204          * if the string was unquoted and we've stopped at a whitespace
205          * character, skip to the next one, because the whitespace will
206          * be overwritten by a null byte here
207          */
208         if (**str && next)
209                 *str += 1;
210
211         /* terminate the parsed string */
212         **target = 0;
213 }
214
215 /*
216  * extract the next argument from the command line
217  */
218 static char *next_arg(struct uci_context *ctx, char **str, bool required, bool name)
219 {
220         char *val;
221         char *ptr;
222
223         val = ptr = *str;
224         skip_whitespace(ctx, str);
225         if(*str[0] == ';') {
226                 *str[0] = 0;
227                 *str += 1;
228         } else {
229                 parse_str(ctx, str, &ptr);
230         }
231         if (!*val) {
232                 if (required)
233                         uci_parse_error(ctx, *str, "insufficient arguments");
234                 goto done;
235         }
236
237         if (name && !uci_validate_name(val))
238                 uci_parse_error(ctx, val, "invalid character in field");
239
240 done:
241         return val;
242 }
243
244 int uci_parse_argument(struct uci_context *ctx, FILE *stream, char **str, char **result)
245 {
246         UCI_HANDLE_ERR(ctx);
247         UCI_ASSERT(ctx, str != NULL);
248         UCI_ASSERT(ctx, result != NULL);
249
250         if (ctx->pctx && (ctx->pctx->file != stream))
251                 uci_cleanup(ctx);
252
253         if (!ctx->pctx)
254                 uci_alloc_parse_context(ctx);
255
256         ctx->pctx->file = stream;
257
258         if (!*str) {
259                 uci_getln(ctx, 0);
260                 *str = ctx->pctx->buf;
261         }
262
263         *result = next_arg(ctx, str, false, false);
264
265         return 0;
266 }
267
268 static int
269 uci_fill_ptr(struct uci_context *ctx, struct uci_ptr *ptr, struct uci_element *e)
270 {
271         UCI_ASSERT(ctx, ptr != NULL);
272         UCI_ASSERT(ctx, e != NULL);
273
274         memset(ptr, 0, sizeof(struct uci_ptr));
275         switch(e->type) {
276         case UCI_TYPE_OPTION:
277                 ptr->o = uci_to_option(e);
278                 goto fill_option;
279         case UCI_TYPE_SECTION:
280                 ptr->s = uci_to_section(e);
281                 goto fill_section;
282         case UCI_TYPE_PACKAGE:
283                 ptr->p = uci_to_package(e);
284                 goto fill_package;
285         default:
286                 UCI_THROW(ctx, UCI_ERR_INVAL);
287         }
288
289 fill_option:
290         ptr->option = ptr->o->e.name;
291         ptr->s = ptr->o->section;
292 fill_section:
293         ptr->section = ptr->s->e.name;
294         ptr->p = ptr->s->package;
295 fill_package:
296         ptr->package = ptr->p->e.name;
297
298         ptr->flags |= UCI_LOOKUP_DONE;
299
300         return 0;
301 }
302
303
304
305 /*
306  * verify that the end of the line or command is reached.
307  * throw an error if extra arguments are given on the command line
308  */
309 static void assert_eol(struct uci_context *ctx, char **str)
310 {
311         char *tmp;
312
313         skip_whitespace(ctx, str);
314         tmp = next_arg(ctx, str, false, false);
315         if (*tmp && (ctx->flags & UCI_FLAG_STRICT))
316                 uci_parse_error(ctx, *str, "too many arguments");
317 }
318
319 /* 
320  * switch to a different config, either triggered by uci_load, or by a
321  * 'package <...>' statement in the import file
322  */
323 static void uci_switch_config(struct uci_context *ctx)
324 {
325         struct uci_parse_context *pctx;
326         struct uci_element *e;
327         const char *name;
328
329         pctx = ctx->pctx;
330         name = pctx->name;
331
332         /* add the last config to main config file list */
333         if (pctx->package) {
334                 pctx->package->backend = ctx->backend;
335                 uci_list_add(&ctx->root, &pctx->package->e.list);
336
337                 pctx->package = NULL;
338                 pctx->section = NULL;
339         }
340
341         if (!name)
342                 return;
343
344         /* 
345          * if an older config under the same name exists, unload it
346          * ignore errors here, e.g. if the config was not found
347          */
348         e = uci_lookup_list(&ctx->root, name);
349         if (e)
350                 UCI_THROW(ctx, UCI_ERR_DUPLICATE);
351         pctx->package = uci_alloc_package(ctx, name);
352 }
353
354 /*
355  * parse the 'package' uci command (next config package)
356  */
357 static void uci_parse_package(struct uci_context *ctx, char **str, bool single)
358 {
359         char *name = NULL;
360
361         /* command string null-terminated by strtok */
362         *str += strlen(*str) + 1;
363
364         name = next_arg(ctx, str, true, true);
365         assert_eol(ctx, str);
366         if (single)
367                 return;
368
369         ctx->pctx->name = name;
370         uci_switch_config(ctx);
371 }
372
373 /*
374  * parse the 'config' uci command (open a section)
375  */
376 static void uci_parse_config(struct uci_context *ctx, char **str)
377 {
378         struct uci_parse_context *pctx = ctx->pctx;
379         struct uci_element *e;
380         struct uci_ptr ptr;
381         char *name = NULL;
382         char *type = NULL;
383
384         uci_fixup_section(ctx, ctx->pctx->section);
385         if (!ctx->pctx->package) {
386                 if (!ctx->pctx->name)
387                         uci_parse_error(ctx, *str, "attempting to import a file without a package name");
388
389                 uci_switch_config(ctx);
390         }
391
392         /* command string null-terminated by strtok */
393         *str += strlen(*str) + 1;
394
395         type = next_arg(ctx, str, true, false);
396         if (!uci_validate_type(type))
397                 uci_parse_error(ctx, type, "invalid character in field");
398         name = next_arg(ctx, str, false, true);
399         assert_eol(ctx, str);
400
401         if (!name) {
402                 ctx->internal = !pctx->merge;
403                 UCI_NESTED(uci_add_section, ctx, pctx->package, type, &pctx->section);
404         } else {
405                 uci_fill_ptr(ctx, &ptr, &pctx->package->e);
406                 e = uci_lookup_list(&pctx->package->sections, name);
407                 if (e)
408                         ptr.s = uci_to_section(e);
409                 ptr.section = name;
410                 ptr.value = type;
411
412                 ctx->internal = !pctx->merge;
413                 UCI_NESTED(uci_set, ctx, &ptr);
414                 pctx->section = uci_to_section(ptr.last);
415         }
416 }
417
418 /*
419  * parse the 'option' uci command (open a value)
420  */
421 static void uci_parse_option(struct uci_context *ctx, char **str, bool list)
422 {
423         struct uci_parse_context *pctx = ctx->pctx;
424         struct uci_element *e;
425         struct uci_ptr ptr;
426         char *name = NULL;
427         char *value = NULL;
428
429         if (!pctx->section)
430                 uci_parse_error(ctx, *str, "option/list command found before the first section");
431
432         /* command string null-terminated by strtok */
433         *str += strlen(*str) + 1;
434
435         name = next_arg(ctx, str, true, true);
436         value = next_arg(ctx, str, false, false);
437         assert_eol(ctx, str);
438
439         uci_fill_ptr(ctx, &ptr, &pctx->section->e);
440         e = uci_lookup_list(&pctx->section->options, name);
441         if (e)
442                 ptr.o = uci_to_option(e);
443         ptr.option = name;
444         ptr.value = value;
445
446         ctx->internal = !pctx->merge;
447         if (list)
448                 UCI_NESTED(uci_add_list, ctx, &ptr);
449         else
450                 UCI_NESTED(uci_set, ctx, &ptr);
451 }
452
453 /*
454  * parse a complete input line, split up combined commands by ';'
455  */
456 static void uci_parse_line(struct uci_context *ctx, bool single)
457 {
458         struct uci_parse_context *pctx = ctx->pctx;
459         char *word, *brk;
460
461         word = pctx->buf;
462         do {
463                 brk = NULL;
464                 word = strtok_r(word, " \t", &brk);
465                 if (!word)
466                         return;
467
468                 switch(word[0]) {
469                         case 0:
470                         case '#':
471                                 return;
472                         case 'p':
473                                 if ((word[1] == 0) || !strcmp(word + 1, "ackage"))
474                                         uci_parse_package(ctx, &word, single);
475                                 else
476                                         goto invalid;
477                                 break;
478                         case 'c':
479                                 if ((word[1] == 0) || !strcmp(word + 1, "onfig"))
480                                         uci_parse_config(ctx, &word);
481                                 else
482                                         goto invalid;
483                                 break;
484                         case 'o':
485                                 if ((word[1] == 0) || !strcmp(word + 1, "ption"))
486                                         uci_parse_option(ctx, &word, false);
487                                 else
488                                         goto invalid;
489                                 break;
490                         case 'l':
491                                 if ((word[1] == 0) || !strcmp(word + 1, "ist"))
492                                         uci_parse_option(ctx, &word, true);
493                                 else
494                                         goto invalid;
495                                 break;
496                         default:
497                                 goto invalid;
498                 }
499                 continue;
500 invalid:
501                 uci_parse_error(ctx, word, "invalid command");
502         } while (1);
503 }
504
505 /* max number of characters that escaping adds to the string */
506 #define UCI_QUOTE_ESCAPE        "'\\''"
507
508 /*
509  * escape an uci string for export
510  */
511 static char *uci_escape(struct uci_context *ctx, const char *str)
512 {
513         const char *end;
514         int ofs = 0;
515
516         if (!ctx->buf) {
517                 ctx->bufsz = LINEBUF;
518                 ctx->buf = malloc(LINEBUF);
519         }
520
521         while (1) {
522                 int len;
523
524                 end = strchr(str, '\'');
525                 if (!end)
526                         end = str + strlen(str);
527                 len = end - str;
528
529                 /* make sure that we have enough room in the buffer */
530                 while (ofs + len + sizeof(UCI_QUOTE_ESCAPE) + 1 > ctx->bufsz) {
531                         ctx->bufsz *= 2;
532                         ctx->buf = uci_realloc(ctx, ctx->buf, ctx->bufsz);
533                 }
534
535                 /* copy the string until the character before the quote */
536                 memcpy(&ctx->buf[ofs], str, len);
537                 ofs += len;
538
539                 /* end of string? return the buffer */
540                 if (*end == 0)
541                         break;
542
543                 memcpy(&ctx->buf[ofs], UCI_QUOTE_ESCAPE, sizeof(UCI_QUOTE_ESCAPE));
544                 ofs += strlen(&ctx->buf[ofs]);
545                 str = end + 1;
546         }
547
548         ctx->buf[ofs] = 0;
549         return ctx->buf;
550 }
551
552 /*
553  * export a single config package to a file stream
554  */
555 static void uci_export_package(struct uci_package *p, FILE *stream, bool header)
556 {
557         struct uci_context *ctx = p->ctx;
558         struct uci_element *s, *o, *i;
559
560         if (header)
561                 fprintf(stream, "package '%s'\n", uci_escape(ctx, p->e.name));
562         uci_foreach_element(&p->sections, s) {
563                 struct uci_section *sec = uci_to_section(s);
564                 fprintf(stream, "\nconfig '%s'", uci_escape(ctx, sec->type));
565                 if (!sec->anonymous || (ctx->flags & UCI_FLAG_EXPORT_NAME))
566                         fprintf(stream, " '%s'", uci_escape(ctx, sec->e.name));
567                 fprintf(stream, "\n");
568                 uci_foreach_element(&sec->options, o) {
569                         struct uci_option *opt = uci_to_option(o);
570                         switch(opt->type) {
571                         case UCI_TYPE_STRING:
572                                 fprintf(stream, "\toption '%s'", uci_escape(ctx, opt->e.name));
573                                 fprintf(stream, " '%s'\n", uci_escape(ctx, opt->v.string));
574                                 break;
575                         case UCI_TYPE_LIST:
576                                 uci_foreach_element(&opt->v.list, i) {
577                                         fprintf(stream, "\tlist '%s'", uci_escape(ctx, opt->e.name));
578                                         fprintf(stream, " '%s'\n", uci_escape(ctx, i->name));
579                                 }
580                                 break;
581                         default:
582                                 fprintf(stream, "\t# unknown type for option '%s'\n", uci_escape(ctx, opt->e.name));
583                                 break;
584                         }
585                 }
586         }
587         fprintf(stream, "\n");
588 }
589
590 int uci_export(struct uci_context *ctx, FILE *stream, struct uci_package *package, bool header)
591 {
592         struct uci_element *e;
593
594         UCI_HANDLE_ERR(ctx);
595         UCI_ASSERT(ctx, stream != NULL);
596
597         if (package)
598                 uci_export_package(package, stream, header);
599         else {
600                 uci_foreach_element(&ctx->root, e) {
601                         uci_export_package(uci_to_package(e), stream, header);
602                 }
603         }
604
605         return 0;
606 }
607
608 int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct uci_package **package, bool single)
609 {
610         struct uci_parse_context *pctx;
611         UCI_HANDLE_ERR(ctx);
612
613         /* make sure no memory from previous parse attempts is leaked */
614         uci_cleanup(ctx);
615
616         uci_alloc_parse_context(ctx);
617         pctx = ctx->pctx;
618         pctx->file = stream;
619         if (*package && single) {
620                 pctx->package = *package;
621                 pctx->merge = true;
622         }
623
624         /*
625          * If 'name' was supplied, assume that the supplied stream does not contain
626          * the appropriate 'package <name>' string to specify the config name
627          * NB: the config file can still override the package name
628          */
629         if (name) {
630                 UCI_ASSERT(ctx, uci_validate_package(name));
631                 pctx->name = name;
632         }
633
634         while (!feof(pctx->file)) {
635                 uci_getln(ctx, 0);
636                 UCI_TRAP_SAVE(ctx, error);
637                 if (pctx->buf[0])
638                         uci_parse_line(ctx, single);
639                 UCI_TRAP_RESTORE(ctx);
640                 continue;
641 error:
642                 if (ctx->flags & UCI_FLAG_PERROR)
643                         uci_perror(ctx, NULL);
644                 if ((ctx->err != UCI_ERR_PARSE) ||
645                         (ctx->flags & UCI_FLAG_STRICT))
646                         UCI_THROW(ctx, ctx->err);
647         }
648
649         uci_fixup_section(ctx, ctx->pctx->section);
650         if (!pctx->package && name)
651                 uci_switch_config(ctx);
652         if (package)
653                 *package = pctx->package;
654         if (pctx->merge)
655                 pctx->package = NULL;
656
657         pctx->name = NULL;
658         uci_switch_config(ctx);
659
660         /* no error happened, we can get rid of the parser context now */
661         uci_cleanup(ctx);
662
663         return 0;
664 }
665
666
667 static char *uci_config_path(struct uci_context *ctx, const char *name)
668 {
669         char *filename;
670
671         UCI_ASSERT(ctx, uci_validate_package(name));
672         filename = uci_malloc(ctx, strlen(name) + strlen(ctx->confdir) + 2);
673         sprintf(filename, "%s/%s", ctx->confdir, name);
674
675         return filename;
676 }
677
678 static void uci_file_commit(struct uci_context *ctx, struct uci_package **package, bool overwrite)
679 {
680         struct uci_package *p = *package;
681         FILE *f = NULL;
682         char *name = NULL;
683         char *path = NULL;
684
685         if (!p->path) {
686                 if (overwrite)
687                         p->path = uci_config_path(ctx, p->e.name);
688                 else
689                         UCI_THROW(ctx, UCI_ERR_INVAL);
690         }
691
692         /* open the config file for writing now, so that it is locked */
693         f = uci_open_stream(ctx, p->path, SEEK_SET, true, true);
694
695         /* flush unsaved changes and reload from delta file */
696         UCI_TRAP_SAVE(ctx, done);
697         if (p->has_delta) {
698                 if (!overwrite) {
699                         name = uci_strdup(ctx, p->e.name);
700                         path = uci_strdup(ctx, p->path);
701                         /* dump our own changes to the delta file */
702                         if (!uci_list_empty(&p->delta))
703                                 UCI_INTERNAL(uci_save, ctx, p);
704
705                         /* 
706                          * other processes might have modified the config 
707                          * as well. dump and reload 
708                          */
709                         uci_free_package(&p);
710                         uci_cleanup(ctx);
711                         UCI_INTERNAL(uci_import, ctx, f, name, &p, true);
712
713                         p->path = path;
714                         p->has_delta = true;
715                         *package = p;
716
717                         /* freed together with the uci_package */
718                         path = NULL;
719                 }
720
721                 /* flush delta */
722                 if (!uci_load_delta(ctx, p, true))
723                         goto done;
724         }
725
726         rewind(f);
727         if (ftruncate(fileno(f), 0) < 0)
728                 UCI_THROW(ctx, UCI_ERR_IO);
729
730         uci_export(ctx, f, p, false);
731         UCI_TRAP_RESTORE(ctx);
732
733 done:
734         if (name)
735                 free(name);
736         if (path)
737                 free(path);
738         uci_close_stream(f);
739         if (ctx->err)
740                 UCI_THROW(ctx, ctx->err);
741 }
742
743
744 /* 
745  * This function returns the filename by returning the string
746  * after the last '/' character. By checking for a non-'\0'
747  * character afterwards, directories are ignored (glob marks
748  * those with a trailing '/'
749  */
750 static inline char *get_filename(char *path)
751 {
752         char *p;
753
754         p = strrchr(path, '/');
755         p++;
756         if (!*p)
757                 return NULL;
758         return p;
759 }
760
761 static char **uci_list_config_files(struct uci_context *ctx)
762 {
763         char **configs;
764         glob_t globbuf;
765         int size, i;
766         char *buf;
767         char *dir;
768
769         dir = uci_malloc(ctx, strlen(ctx->confdir) + 1 + sizeof("/*"));
770         sprintf(dir, "%s/*", ctx->confdir);
771         if (glob(dir, GLOB_MARK, NULL, &globbuf) != 0) {
772                 free(dir);
773                 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
774         }
775
776         size = sizeof(char *) * (globbuf.gl_pathc + 1);
777         for(i = 0; i < globbuf.gl_pathc; i++) {
778                 char *p;
779
780                 p = get_filename(globbuf.gl_pathv[i]);
781                 if (!p)
782                         continue;
783
784                 size += strlen(p) + 1;
785         }
786
787         configs = uci_malloc(ctx, size);
788         buf = (char *) &configs[globbuf.gl_pathc + 1];
789         for(i = 0; i < globbuf.gl_pathc; i++) {
790                 char *p;
791
792                 p = get_filename(globbuf.gl_pathv[i]);
793                 if (!p)
794                         continue;
795
796                 if (!uci_validate_package(p))
797                         continue;
798
799                 configs[i] = buf;
800                 strcpy(buf, p);
801                 buf += strlen(buf) + 1;
802         }
803         free(dir);
804         globfree(&globbuf);
805         return configs;
806 }
807
808 static struct uci_package *uci_file_load(struct uci_context *ctx, const char *name)
809 {
810         struct uci_package *package = NULL;
811         char *filename;
812         bool confdir;
813         FILE *file = NULL;
814
815         switch (name[0]) {
816         case '.':
817                 /* relative path outside of /etc/config */
818                 if (name[1] != '/')
819                         UCI_THROW(ctx, UCI_ERR_NOTFOUND);
820                 /* fall through */
821         case '/':
822                 /* absolute path outside of /etc/config */
823                 filename = uci_strdup(ctx, name);
824                 name = strrchr(name, '/') + 1;
825                 confdir = false;
826                 break;
827         default:
828                 /* config in /etc/config */
829                 filename = uci_config_path(ctx, name);
830                 confdir = true;
831                 break;
832         }
833
834         file = uci_open_stream(ctx, filename, SEEK_SET, false, false);
835         ctx->err = 0;
836         UCI_TRAP_SAVE(ctx, done);
837         UCI_INTERNAL(uci_import, ctx, file, name, &package, true);
838         UCI_TRAP_RESTORE(ctx);
839
840         if (package) {
841                 package->path = filename;
842                 package->has_delta = confdir;
843                 uci_load_delta(ctx, package, false);
844         }
845
846 done:
847         uci_close_stream(file);
848         if (ctx->err)
849                 UCI_THROW(ctx, ctx->err);
850         return package;
851 }
852
853 __private UCI_BACKEND(uci_file_backend, "file",
854         .load = uci_file_load,
855         .commit = uci_file_commit,
856         .list_configs = uci_list_config_files,
857 );