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