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