split off and compile file.c separately
[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, bool complete)
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         if (complete)
300                 ptr->flags |= UCI_LOOKUP_COMPLETE;
301
302         return 0;
303 }
304
305
306
307 /*
308  * verify that the end of the line or command is reached.
309  * throw an error if extra arguments are given on the command line
310  */
311 static void assert_eol(struct uci_context *ctx, char **str)
312 {
313         char *tmp;
314
315         skip_whitespace(ctx, str);
316         tmp = next_arg(ctx, str, false, false);
317         if (*tmp && (ctx->flags & UCI_FLAG_STRICT))
318                 uci_parse_error(ctx, *str, "too many arguments");
319 }
320
321 /* 
322  * switch to a different config, either triggered by uci_load, or by a
323  * 'package <...>' statement in the import file
324  */
325 static void uci_switch_config(struct uci_context *ctx)
326 {
327         struct uci_parse_context *pctx;
328         struct uci_element *e;
329         const char *name;
330
331         pctx = ctx->pctx;
332         name = pctx->name;
333
334         /* add the last config to main config file list */
335         if (pctx->package) {
336                 pctx->package->backend = ctx->backend;
337                 uci_list_add(&ctx->root, &pctx->package->e.list);
338
339                 pctx->package = NULL;
340                 pctx->section = NULL;
341         }
342
343         if (!name)
344                 return;
345
346         /* 
347          * if an older config under the same name exists, unload it
348          * ignore errors here, e.g. if the config was not found
349          */
350         e = uci_lookup_list(&ctx->root, name);
351         if (e)
352                 UCI_THROW(ctx, UCI_ERR_DUPLICATE);
353         pctx->package = uci_alloc_package(ctx, name);
354 }
355
356 /*
357  * parse the 'package' uci command (next config package)
358  */
359 static void uci_parse_package(struct uci_context *ctx, char **str, bool single)
360 {
361         char *name = NULL;
362
363         /* command string null-terminated by strtok */
364         *str += strlen(*str) + 1;
365
366         name = next_arg(ctx, str, true, true);
367         assert_eol(ctx, str);
368         if (single)
369                 return;
370
371         ctx->pctx->name = name;
372         uci_switch_config(ctx);
373 }
374
375 /*
376  * parse the 'config' uci command (open a section)
377  */
378 static void uci_parse_config(struct uci_context *ctx, char **str)
379 {
380         struct uci_parse_context *pctx = ctx->pctx;
381         struct uci_element *e;
382         struct uci_ptr ptr;
383         char *name = NULL;
384         char *type = NULL;
385
386         uci_fixup_section(ctx, ctx->pctx->section);
387         if (!ctx->pctx->package) {
388                 if (!ctx->pctx->name)
389                         uci_parse_error(ctx, *str, "attempting to import a file without a package name");
390
391                 uci_switch_config(ctx);
392         }
393
394         /* command string null-terminated by strtok */
395         *str += strlen(*str) + 1;
396
397         type = next_arg(ctx, str, true, false);
398         if (!uci_validate_type(type))
399                 uci_parse_error(ctx, type, "invalid character in field");
400         name = next_arg(ctx, str, false, true);
401         assert_eol(ctx, str);
402
403         if (!name) {
404                 ctx->internal = !pctx->merge;
405                 UCI_NESTED(uci_add_section, ctx, pctx->package, type, &pctx->section);
406         } else {
407                 uci_fill_ptr(ctx, &ptr, &pctx->package->e, false);
408                 e = uci_lookup_list(&pctx->package->sections, name);
409                 if (e)
410                         ptr.s = uci_to_section(e);
411                 ptr.section = name;
412                 ptr.value = type;
413
414                 ctx->internal = !pctx->merge;
415                 UCI_NESTED(uci_set, ctx, &ptr);
416                 pctx->section = uci_to_section(ptr.last);
417         }
418 }
419
420 /*
421  * parse the 'option' uci command (open a value)
422  */
423 static void uci_parse_option(struct uci_context *ctx, char **str, bool list)
424 {
425         struct uci_parse_context *pctx = ctx->pctx;
426         struct uci_element *e;
427         struct uci_ptr ptr;
428         char *name = NULL;
429         char *value = NULL;
430
431         if (!pctx->section)
432                 uci_parse_error(ctx, *str, "option/list command found before the first section");
433
434         /* command string null-terminated by strtok */
435         *str += strlen(*str) + 1;
436
437         name = next_arg(ctx, str, true, true);
438         value = next_arg(ctx, str, false, false);
439         assert_eol(ctx, str);
440
441         uci_fill_ptr(ctx, &ptr, &pctx->section->e, false);
442         e = uci_lookup_list(&pctx->section->options, name);
443         if (e)
444                 ptr.o = uci_to_option(e);
445         ptr.option = name;
446         ptr.value = value;
447
448         ctx->internal = !pctx->merge;
449         if (list)
450                 UCI_NESTED(uci_add_list, ctx, &ptr);
451         else
452                 UCI_NESTED(uci_set, ctx, &ptr);
453 }
454
455 /*
456  * parse a complete input line, split up combined commands by ';'
457  */
458 static void uci_parse_line(struct uci_context *ctx, bool single)
459 {
460         struct uci_parse_context *pctx = ctx->pctx;
461         char *word, *brk;
462
463         word = pctx->buf;
464         do {
465                 brk = NULL;
466                 word = strtok_r(word, " \t", &brk);
467                 if (!word)
468                         return;
469
470                 switch(word[0]) {
471                         case 0:
472                         case '#':
473                                 return;
474                         case 'p':
475                                 if ((word[1] == 0) || !strcmp(word + 1, "ackage"))
476                                         uci_parse_package(ctx, &word, single);
477                                 else
478                                         goto invalid;
479                                 break;
480                         case 'c':
481                                 if ((word[1] == 0) || !strcmp(word + 1, "onfig"))
482                                         uci_parse_config(ctx, &word);
483                                 else
484                                         goto invalid;
485                                 break;
486                         case 'o':
487                                 if ((word[1] == 0) || !strcmp(word + 1, "ption"))
488                                         uci_parse_option(ctx, &word, false);
489                                 else
490                                         goto invalid;
491                                 break;
492                         case 'l':
493                                 if ((word[1] == 0) || !strcmp(word + 1, "ist"))
494                                         uci_parse_option(ctx, &word, true);
495                                 else
496                                         goto invalid;
497                                 break;
498                         default:
499                                 goto invalid;
500                 }
501                 continue;
502 invalid:
503                 uci_parse_error(ctx, word, "invalid command");
504         } while (1);
505 }
506
507 /* max number of characters that escaping adds to the string */
508 #define UCI_QUOTE_ESCAPE        "'\\''"
509
510 /*
511  * escape an uci string for export
512  */
513 static char *uci_escape(struct uci_context *ctx, const char *str)
514 {
515         const char *end;
516         int ofs = 0;
517
518         if (!ctx->buf) {
519                 ctx->bufsz = LINEBUF;
520                 ctx->buf = malloc(LINEBUF);
521         }
522
523         while (1) {
524                 int len;
525
526                 end = strchr(str, '\'');
527                 if (!end)
528                         end = str + strlen(str);
529                 len = end - str;
530
531                 /* make sure that we have enough room in the buffer */
532                 while (ofs + len + sizeof(UCI_QUOTE_ESCAPE) + 1 > ctx->bufsz) {
533                         ctx->bufsz *= 2;
534                         ctx->buf = uci_realloc(ctx, ctx->buf, ctx->bufsz);
535                 }
536
537                 /* copy the string until the character before the quote */
538                 memcpy(&ctx->buf[ofs], str, len);
539                 ofs += len;
540
541                 /* end of string? return the buffer */
542                 if (*end == 0)
543                         break;
544
545                 memcpy(&ctx->buf[ofs], UCI_QUOTE_ESCAPE, sizeof(UCI_QUOTE_ESCAPE));
546                 ofs += strlen(&ctx->buf[ofs]);
547                 str = end + 1;
548         }
549
550         ctx->buf[ofs] = 0;
551         return ctx->buf;
552 }
553
554 /*
555  * export a single config package to a file stream
556  */
557 static void uci_export_package(struct uci_package *p, FILE *stream, bool header)
558 {
559         struct uci_context *ctx = p->ctx;
560         struct uci_element *s, *o, *i;
561
562         if (header)
563                 fprintf(stream, "package '%s'\n", uci_escape(ctx, p->e.name));
564         uci_foreach_element(&p->sections, s) {
565                 struct uci_section *sec = uci_to_section(s);
566                 fprintf(stream, "\nconfig '%s'", uci_escape(ctx, sec->type));
567                 if (!sec->anonymous || (ctx->flags & UCI_FLAG_EXPORT_NAME))
568                         fprintf(stream, " '%s'", uci_escape(ctx, sec->e.name));
569                 fprintf(stream, "\n");
570                 uci_foreach_element(&sec->options, o) {
571                         struct uci_option *opt = uci_to_option(o);
572                         switch(opt->type) {
573                         case UCI_TYPE_STRING:
574                                 fprintf(stream, "\toption '%s'", uci_escape(ctx, opt->e.name));
575                                 fprintf(stream, " '%s'\n", uci_escape(ctx, opt->v.string));
576                                 break;
577                         case UCI_TYPE_LIST:
578                                 uci_foreach_element(&opt->v.list, i) {
579                                         fprintf(stream, "\tlist '%s'", uci_escape(ctx, opt->e.name));
580                                         fprintf(stream, " '%s'\n", uci_escape(ctx, i->name));
581                                 }
582                                 break;
583                         default:
584                                 fprintf(stream, "\t# unknown type for option '%s'\n", uci_escape(ctx, opt->e.name));
585                                 break;
586                         }
587                 }
588         }
589         fprintf(stream, "\n");
590 }
591
592 int uci_export(struct uci_context *ctx, FILE *stream, struct uci_package *package, bool header)
593 {
594         struct uci_element *e;
595
596         UCI_HANDLE_ERR(ctx);
597         UCI_ASSERT(ctx, stream != NULL);
598
599         if (package)
600                 uci_export_package(package, stream, header);
601         else {
602                 uci_foreach_element(&ctx->root, e) {
603                         uci_export_package(uci_to_package(e), stream, header);
604                 }
605         }
606
607         return 0;
608 }
609
610 int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct uci_package **package, bool single)
611 {
612         struct uci_parse_context *pctx;
613         UCI_HANDLE_ERR(ctx);
614
615         /* make sure no memory from previous parse attempts is leaked */
616         uci_cleanup(ctx);
617
618         uci_alloc_parse_context(ctx);
619         pctx = ctx->pctx;
620         pctx->file = stream;
621         if (*package && single) {
622                 pctx->package = *package;
623                 pctx->merge = true;
624         }
625
626         /*
627          * If 'name' was supplied, assume that the supplied stream does not contain
628          * the appropriate 'package <name>' string to specify the config name
629          * NB: the config file can still override the package name
630          */
631         if (name) {
632                 UCI_ASSERT(ctx, uci_validate_package(name));
633                 pctx->name = name;
634         }
635
636         while (!feof(pctx->file)) {
637                 uci_getln(ctx, 0);
638                 UCI_TRAP_SAVE(ctx, error);
639                 if (pctx->buf[0])
640                         uci_parse_line(ctx, single);
641                 UCI_TRAP_RESTORE(ctx);
642                 continue;
643 error:
644                 if (ctx->flags & UCI_FLAG_PERROR)
645                         uci_perror(ctx, NULL);
646                 if ((ctx->err != UCI_ERR_PARSE) ||
647                         (ctx->flags & UCI_FLAG_STRICT))
648                         UCI_THROW(ctx, ctx->err);
649         }
650
651         uci_fixup_section(ctx, ctx->pctx->section);
652         if (!pctx->package && name)
653                 uci_switch_config(ctx);
654         if (package)
655                 *package = pctx->package;
656         if (pctx->merge)
657                 pctx->package = NULL;
658
659         pctx->name = NULL;
660         uci_switch_config(ctx);
661
662         /* no error happened, we can get rid of the parser context now */
663         uci_cleanup(ctx);
664
665         return 0;
666 }
667
668
669 static char *uci_config_path(struct uci_context *ctx, const char *name)
670 {
671         char *filename;
672
673         UCI_ASSERT(ctx, uci_validate_package(name));
674         filename = uci_malloc(ctx, strlen(name) + strlen(ctx->confdir) + 2);
675         sprintf(filename, "%s/%s", ctx->confdir, name);
676
677         return filename;
678 }
679
680 void uci_file_commit(struct uci_context *ctx, struct uci_package **package, bool overwrite)
681 {
682         struct uci_package *p = *package;
683         FILE *f = NULL;
684         char *name = NULL;
685         char *path = NULL;
686
687         if (!p->path) {
688                 if (overwrite)
689                         p->path = uci_config_path(ctx, p->e.name);
690                 else
691                         UCI_THROW(ctx, UCI_ERR_INVAL);
692         }
693
694         /* open the config file for writing now, so that it is locked */
695         f = uci_open_stream(ctx, p->path, SEEK_SET, true, true);
696
697         /* flush unsaved changes and reload from history file */
698         UCI_TRAP_SAVE(ctx, done);
699         if (p->has_history) {
700                 if (!overwrite) {
701                         name = uci_strdup(ctx, p->e.name);
702                         path = uci_strdup(ctx, p->path);
703                         /* dump our own changes to the history file */
704                         if (!uci_list_empty(&p->history))
705                                 UCI_INTERNAL(uci_save, ctx, p);
706
707                         /* 
708                          * other processes might have modified the config 
709                          * as well. dump and reload 
710                          */
711                         uci_free_package(&p);
712                         uci_cleanup(ctx);
713                         UCI_INTERNAL(uci_import, ctx, f, name, &p, true);
714
715                         p->path = path;
716                         p->has_history = true;
717                         *package = p;
718
719                         /* freed together with the uci_package */
720                         path = NULL;
721                 }
722
723                 /* flush history */
724                 if (!uci_load_history(ctx, p, true))
725                         goto done;
726         }
727
728         rewind(f);
729         if (ftruncate(fileno(f), 0) < 0)
730                 UCI_THROW(ctx, UCI_ERR_IO);
731
732         uci_export(ctx, f, p, false);
733         UCI_TRAP_RESTORE(ctx);
734
735 done:
736         if (name)
737                 free(name);
738         if (path)
739                 free(path);
740         uci_close_stream(f);
741         if (ctx->err)
742                 UCI_THROW(ctx, ctx->err);
743 }
744
745
746 /* 
747  * This function returns the filename by returning the string
748  * after the last '/' character. By checking for a non-'\0'
749  * character afterwards, directories are ignored (glob marks
750  * those with a trailing '/'
751  */
752 static inline char *get_filename(char *path)
753 {
754         char *p;
755
756         p = strrchr(path, '/');
757         p++;
758         if (!*p)
759                 return NULL;
760         return p;
761 }
762
763 static char **uci_list_config_files(struct uci_context *ctx)
764 {
765         char **configs;
766         glob_t globbuf;
767         int size, i;
768         char *buf;
769         char *dir;
770
771         dir = uci_malloc(ctx, strlen(ctx->confdir) + 1 + sizeof("/*"));
772         sprintf(dir, "%s/*", ctx->confdir);
773         if (glob(dir, GLOB_MARK, NULL, &globbuf) != 0) {
774                 free(dir);
775                 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
776         }
777
778         size = sizeof(char *) * (globbuf.gl_pathc + 1);
779         for(i = 0; i < globbuf.gl_pathc; i++) {
780                 char *p;
781
782                 p = get_filename(globbuf.gl_pathv[i]);
783                 if (!p)
784                         continue;
785
786                 size += strlen(p) + 1;
787         }
788
789         configs = uci_malloc(ctx, size);
790         buf = (char *) &configs[globbuf.gl_pathc + 1];
791         for(i = 0; i < globbuf.gl_pathc; i++) {
792                 char *p;
793
794                 p = get_filename(globbuf.gl_pathv[i]);
795                 if (!p)
796                         continue;
797
798                 if (!uci_validate_package(p))
799                         continue;
800
801                 configs[i] = buf;
802                 strcpy(buf, p);
803                 buf += strlen(buf) + 1;
804         }
805         free(dir);
806         globfree(&globbuf);
807         return configs;
808 }
809
810 static struct uci_package *uci_file_load(struct uci_context *ctx, const char *name)
811 {
812         struct uci_package *package = NULL;
813         char *filename;
814         bool confdir;
815         FILE *file = NULL;
816
817         switch (name[0]) {
818         case '.':
819                 /* relative path outside of /etc/config */
820                 if (name[1] != '/')
821                         UCI_THROW(ctx, UCI_ERR_NOTFOUND);
822                 /* fall through */
823         case '/':
824                 /* absolute path outside of /etc/config */
825                 filename = uci_strdup(ctx, name);
826                 name = strrchr(name, '/') + 1;
827                 confdir = false;
828                 break;
829         default:
830                 /* config in /etc/config */
831                 filename = uci_config_path(ctx, name);
832                 confdir = true;
833                 break;
834         }
835
836         file = uci_open_stream(ctx, filename, SEEK_SET, false, false);
837         ctx->err = 0;
838         UCI_TRAP_SAVE(ctx, done);
839         UCI_INTERNAL(uci_import, ctx, file, name, &package, true);
840         UCI_TRAP_RESTORE(ctx);
841
842         if (package) {
843                 package->path = filename;
844                 package->has_history = confdir;
845                 uci_load_history(ctx, package, false);
846         }
847
848 done:
849         uci_close_stream(file);
850         if (ctx->err)
851                 UCI_THROW(ctx, ctx->err);
852         return package;
853 }
854
855 __private UCI_BACKEND(uci_file_backend, "file",
856         .load = uci_file_load,
857         .commit = uci_file_commit,
858         .list_configs = uci_list_config_files,
859 );