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