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