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