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