2 * libuci - Library for the Unified Configuration Interface
3 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
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
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.
16 * This file contains the code for handling uci config delta files
20 #include <sys/types.h>
32 #include "uci_internal.h"
34 /* record a change that was done to a package */
36 uci_add_delta(struct uci_context
*ctx
, struct uci_list
*list
, int cmd
, const char *section
, const char *option
, const char *value
)
39 int size
= strlen(section
) + 1;
43 size
+= strlen(value
) + 1;
45 h
= uci_alloc_element(ctx
, delta
, option
, size
);
48 h
->section
= strcpy(ptr
, section
);
50 ptr
+= strlen(ptr
) + 1;
51 h
->value
= strcpy(ptr
, value
);
53 uci_list_add(list
, &h
->e
.list
);
57 uci_free_delta(struct uci_delta
*h
)
61 if ((h
->section
!= NULL
) &&
62 (h
->section
!= uci_dataptr(h
))) {
66 uci_free_element(&h
->e
);
69 static void uci_delta_save(struct uci_context
*ctx
, FILE *f
,
70 const char *name
, const struct uci_delta
*h
)
72 const struct uci_element
*e
= &h
->e
;
73 char prefix
[2] = {0, 0};
75 if (h
->cmd
<= __UCI_CMD_LAST
)
76 prefix
[0] = uci_command_char
[h
->cmd
];
78 fprintf(f
, "%s%s.%s", prefix
, name
, h
->section
);
80 fprintf(f
, ".%s", e
->name
);
82 if (h
->cmd
== UCI_CMD_REMOVE
&& !h
->value
)
88 for (i
= 0; h
->value
[i
]; i
++) {
89 unsigned char c
= h
->value
[i
];
99 int uci_set_savedir(struct uci_context
*ctx
, const char *dir
)
102 struct uci_element
*e
, *tmp
;
103 volatile bool exists
= false;
106 UCI_ASSERT(ctx
, dir
!= NULL
);
108 /* Move dir to the end of ctx->delta_path */
109 uci_foreach_element_safe(&ctx
->delta_path
, tmp
, e
) {
110 if (!strcmp(e
->name
, dir
)) {
112 uci_list_del(&e
->list
);
117 e
= uci_alloc_generic(ctx
, UCI_TYPE_PATH
, dir
, sizeof(struct uci_element
));
118 uci_list_add(&ctx
->delta_path
, &e
->list
);
120 sdir
= uci_strdup(ctx
, dir
);
121 if (ctx
->savedir
!= uci_savedir
)
127 int uci_add_delta_path(struct uci_context
*ctx
, const char *dir
)
129 struct uci_element
*e
;
130 struct uci_list
*savedir
;
133 UCI_ASSERT(ctx
, dir
!= NULL
);
135 /* Duplicate delta path is not allowed */
136 uci_foreach_element(&ctx
->delta_path
, e
) {
137 if (!strcmp(e
->name
, dir
))
138 UCI_THROW(ctx
, UCI_ERR_DUPLICATE
);
141 e
= uci_alloc_generic(ctx
, UCI_TYPE_PATH
, dir
, sizeof(struct uci_element
));
142 /* Keep savedir at the end of ctx->delta_path list */
143 savedir
= ctx
->delta_path
.prev
;
144 uci_list_insert(savedir
->prev
, &e
->list
);
149 char const uci_command_char
[] = {
151 [UCI_CMD_REMOVE
] = '-',
152 [UCI_CMD_CHANGE
] = 0,
153 [UCI_CMD_RENAME
] = '@',
154 [UCI_CMD_REORDER
] = '^',
155 [UCI_CMD_LIST_ADD
] = '|',
156 [UCI_CMD_LIST_DEL
] = '~'
159 static inline int uci_parse_delta_tuple(struct uci_context
*ctx
, struct uci_ptr
*ptr
)
161 struct uci_parse_context
*pctx
= ctx
->pctx
;
162 char *str
= pctx_cur_str(pctx
), *arg
;
165 UCI_INTERNAL(uci_parse_argument
, ctx
, ctx
->pctx
->file
, &str
, &arg
);
169 for (c
= 0; c
<= __UCI_CMD_LAST
; c
++) {
170 if (uci_command_char
[c
] == *arg
)
173 if (c
> __UCI_CMD_LAST
)
176 if (c
!= UCI_CMD_CHANGE
)
179 UCI_INTERNAL(uci_parse_ptr
, ctx
, ptr
, arg
);
183 if (ptr
->flags
& UCI_LOOKUP_EXTENDED
)
185 if (c
!= UCI_CMD_REMOVE
&& !ptr
->value
) {
190 case UCI_CMD_REORDER
:
191 if (!ptr
->value
|| ptr
->option
)
195 if (!ptr
->value
|| !uci_validate_name(ptr
->value
))
198 case UCI_CMD_LIST_ADD
:
202 case UCI_CMD_LIST_DEL
:
210 UCI_THROW(ctx
, UCI_ERR_INVAL
);
214 static void uci_parse_delta_line(struct uci_context
*ctx
, struct uci_package
*p
)
216 struct uci_element
*e
= NULL
;
220 cmd
= uci_parse_delta_tuple(ctx
, &ptr
);
221 if (strcmp(ptr
.package
, p
->e
.name
) != 0)
224 if (ctx
->flags
& UCI_FLAG_SAVED_DELTA
)
225 uci_add_delta(ctx
, &p
->saved_delta
, cmd
, ptr
.section
, ptr
.option
, ptr
.value
);
228 case UCI_CMD_REORDER
:
229 uci_expand_ptr(ctx
, &ptr
, true);
231 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
232 UCI_INTERNAL(uci_reorder_section
, ctx
, ptr
.s
, strtoul(ptr
.value
, NULL
, 10));
235 UCI_INTERNAL(uci_rename
, ctx
, &ptr
);
238 UCI_INTERNAL(uci_delete
, ctx
, &ptr
);
240 case UCI_CMD_LIST_ADD
:
241 UCI_INTERNAL(uci_add_list
, ctx
, &ptr
);
243 case UCI_CMD_LIST_DEL
:
244 UCI_INTERNAL(uci_del_list
, ctx
, &ptr
);
248 UCI_INTERNAL(uci_set
, ctx
, &ptr
);
250 if (!ptr
.option
&& e
&& (cmd
== UCI_CMD_ADD
))
251 uci_to_section(e
)->anonymous
= true;
256 UCI_THROW(ctx
, UCI_ERR_PARSE
);
259 /* returns the number of changes that were successfully parsed */
260 static int uci_parse_delta(struct uci_context
*ctx
, FILE *stream
, struct uci_package
*p
)
262 struct uci_parse_context
*pctx
;
263 volatile int changes
= 0;
265 /* make sure no memory from previous parse attempts is leaked */
268 pctx
= (struct uci_parse_context
*) uci_malloc(ctx
, sizeof(struct uci_parse_context
));
272 while (!feof(pctx
->file
)) {
279 * ignore parse errors in single lines, we want to preserve as much
282 UCI_TRAP_SAVE(ctx
, error
);
283 uci_parse_delta_line(ctx
, p
);
284 UCI_TRAP_RESTORE(ctx
);
290 /* no error happened, we can get rid of the parser context now */
295 /* returns the number of changes that were successfully parsed */
296 static int uci_load_delta_file(struct uci_context
*ctx
, struct uci_package
*p
, char *filename
, FILE **f
, bool flush
)
298 FILE *volatile stream
= NULL
;
299 volatile int changes
= 0;
301 UCI_TRAP_SAVE(ctx
, done
);
302 stream
= uci_open_stream(ctx
, filename
, NULL
, SEEK_SET
, flush
, false);
303 UCI_TRAP_RESTORE(ctx
);
306 changes
= uci_parse_delta(ctx
, stream
, p
);
312 uci_close_stream(stream
);
316 /* returns the number of changes that were successfully parsed */
317 __private
int uci_load_delta(struct uci_context
*ctx
, struct uci_package
*p
, bool flush
)
319 struct uci_element
*e
;
320 char *filename
= NULL
;
321 FILE *volatile f
= NULL
;
322 volatile int changes
= 0;
327 uci_foreach_element(&ctx
->delta_path
, e
) {
328 if ((asprintf(&filename
, "%s/%s", e
->name
, p
->e
.name
) < 0) || !filename
)
329 UCI_THROW(ctx
, UCI_ERR_MEM
);
331 changes
+= uci_load_delta_file(ctx
, p
, filename
, NULL
, false);
335 if ((asprintf(&filename
, "%s/%s", ctx
->savedir
, p
->e
.name
) < 0) || !filename
)
336 UCI_THROW(ctx
, UCI_ERR_MEM
);
337 UCI_TRAP_SAVE(ctx
, done
);
338 f
= uci_open_stream(ctx
, filename
, NULL
, SEEK_SET
, flush
, false);
339 UCI_TRAP_RESTORE(ctx
);
341 if (flush
&& f
&& (changes
> 0)) {
342 if (ftruncate(fileno(f
), 0) < 0) {
345 UCI_THROW(ctx
, UCI_ERR_IO
);
356 static void uci_filter_delta(struct uci_context
*ctx
, const char *name
, const char *section
, const char *option
)
358 struct uci_parse_context
*pctx
;
359 struct uci_element
*e
, *tmp
;
360 struct uci_list list
;
361 char *filename
= NULL
;
365 uci_list_init(&list
);
366 uci_alloc_parse_context(ctx
);
369 if ((asprintf(&filename
, "%s/%s", ctx
->savedir
, name
) < 0) || !filename
)
370 UCI_THROW(ctx
, UCI_ERR_MEM
);
372 UCI_TRAP_SAVE(ctx
, done
);
373 f
= uci_open_stream(ctx
, filename
, NULL
, SEEK_SET
, true, false);
384 c
= uci_parse_delta_tuple(ctx
, &ptr
);
387 if (!ptr
.section
|| (strcmp(section
, ptr
.section
) != 0))
390 if (match
&& option
) {
391 if (!ptr
.option
|| (strcmp(option
, ptr
.option
) != 0))
396 uci_add_delta(ctx
, &list
, c
,
397 ptr
.section
, ptr
.option
, ptr
.value
);
401 /* rebuild the delta file */
403 if (ftruncate(fileno(f
), 0) < 0)
404 UCI_THROW(ctx
, UCI_ERR_IO
);
405 uci_foreach_element_safe(&list
, tmp
, e
) {
406 struct uci_delta
*h
= uci_to_delta(e
);
407 uci_delta_save(ctx
, f
, name
, h
);
410 UCI_TRAP_RESTORE(ctx
);
414 uci_close_stream(pctx
->file
);
415 uci_foreach_element_safe(&list
, tmp
, e
) {
416 uci_free_delta(uci_to_delta(e
));
421 int uci_revert(struct uci_context
*ctx
, struct uci_ptr
*ptr
)
423 char *volatile package
= NULL
;
424 char *volatile section
= NULL
;
425 char *volatile option
= NULL
;
428 uci_expand_ptr(ctx
, ptr
, false);
429 UCI_ASSERT(ctx
, ptr
->p
->has_delta
);
432 * - flush unwritten changes
433 * - save the package name
434 * - unload the package
436 * - reload the package
438 UCI_TRAP_SAVE(ctx
, error
);
439 UCI_INTERNAL(uci_save
, ctx
, ptr
->p
);
441 /* NB: need to clone package, section and option names,
442 * as they may get freed on uci_free_package() */
443 package
= uci_strdup(ctx
, ptr
->p
->e
.name
);
445 section
= uci_strdup(ctx
, ptr
->section
);
447 option
= uci_strdup(ctx
, ptr
->option
);
449 uci_free_package(&ptr
->p
);
450 uci_filter_delta(ctx
, package
, section
, option
);
452 UCI_INTERNAL(uci_load
, ctx
, package
, &ptr
->p
);
453 UCI_TRAP_RESTORE(ctx
);
461 UCI_THROW(ctx
, ctx
->err
);
465 int uci_save(struct uci_context
*ctx
, struct uci_package
*p
)
467 FILE *volatile f
= NULL
;
468 char *filename
= NULL
;
469 struct uci_element
*e
, *tmp
;
473 UCI_ASSERT(ctx
, p
!= NULL
);
476 * if the config file was outside of the /etc/config path,
477 * don't save the delta to a file, update the real file
479 * does not modify the uci_package pointer
482 return uci_commit(ctx
, &p
, false);
484 if (uci_list_empty(&p
->delta
))
487 if (stat(ctx
->savedir
, &statbuf
) < 0) {
488 if (stat(ctx
->confdir
, &statbuf
) == 0) {
489 mkdir(ctx
->savedir
, statbuf
.st_mode
);
491 mkdir(ctx
->savedir
, UCI_DIRMODE
);
493 } else if ((statbuf
.st_mode
& S_IFMT
) != S_IFDIR
) {
494 UCI_THROW(ctx
, UCI_ERR_IO
);
497 if ((asprintf(&filename
, "%s/%s", ctx
->savedir
, p
->e
.name
) < 0) || !filename
)
498 UCI_THROW(ctx
, UCI_ERR_MEM
);
501 UCI_TRAP_SAVE(ctx
, done
);
502 f
= uci_open_stream(ctx
, filename
, NULL
, SEEK_END
, true, true);
503 UCI_TRAP_RESTORE(ctx
);
505 uci_foreach_element_safe(&p
->delta
, tmp
, e
) {
506 struct uci_delta
*h
= uci_to_delta(e
);
507 uci_delta_save(ctx
, f
, p
->e
.name
, h
);
515 UCI_THROW(ctx
, ctx
->err
);