556250dc5b5150ed5727e58a7f5bac87bd32158a
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
);
70 int uci_set_savedir(struct uci_context
*ctx
, const char *dir
)
75 UCI_ASSERT(ctx
, dir
!= NULL
);
77 sdir
= uci_strdup(ctx
, dir
);
78 if (ctx
->savedir
!= uci_savedir
)
84 int uci_add_delta_path(struct uci_context
*ctx
, const char *dir
)
86 struct uci_element
*e
;
89 UCI_ASSERT(ctx
, dir
!= NULL
);
90 if (!strcmp(dir
, ctx
->savedir
))
92 e
= uci_alloc_generic(ctx
, UCI_TYPE_PATH
, dir
, sizeof(struct uci_element
));
93 uci_list_add(&ctx
->delta_path
, &e
->list
);
98 char const uci_command_char
[] = {
100 [UCI_CMD_REMOVE
] = '-',
101 [UCI_CMD_CHANGE
] = 0,
102 [UCI_CMD_RENAME
] = '@',
103 [UCI_CMD_REORDER
] = '^',
104 [UCI_CMD_LIST_ADD
] = '|',
105 [UCI_CMD_LIST_DEL
] = '~'
108 static inline int uci_parse_delta_tuple(struct uci_context
*ctx
, struct uci_ptr
*ptr
)
110 struct uci_parse_context
*pctx
= ctx
->pctx
;
111 char *str
= pctx_cur_str(pctx
), *arg
;
114 UCI_INTERNAL(uci_parse_argument
, ctx
, ctx
->pctx
->file
, &str
, &arg
);
115 for (c
= 0; c
<= __UCI_CMD_LAST
; c
++) {
116 if (uci_command_char
[c
] == *arg
)
119 if (c
> __UCI_CMD_LAST
)
122 if (c
!= UCI_CMD_CHANGE
)
125 UCI_INTERNAL(uci_parse_ptr
, ctx
, ptr
, arg
);
129 if (ptr
->flags
& UCI_LOOKUP_EXTENDED
)
133 case UCI_CMD_REORDER
:
134 if (!ptr
->value
|| ptr
->option
)
138 if (!ptr
->value
|| !uci_validate_name(ptr
->value
))
141 case UCI_CMD_LIST_ADD
:
144 case UCI_CMD_LIST_DEL
:
152 UCI_THROW(ctx
, UCI_ERR_INVAL
);
156 static void uci_parse_delta_line(struct uci_context
*ctx
, struct uci_package
*p
)
158 struct uci_element
*e
= NULL
;
162 cmd
= uci_parse_delta_tuple(ctx
, &ptr
);
163 if (strcmp(ptr
.package
, p
->e
.name
) != 0)
166 if (ctx
->flags
& UCI_FLAG_SAVED_DELTA
)
167 uci_add_delta(ctx
, &p
->saved_delta
, cmd
, ptr
.section
, ptr
.option
, ptr
.value
);
170 case UCI_CMD_REORDER
:
171 uci_expand_ptr(ctx
, &ptr
, true);
173 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
174 UCI_INTERNAL(uci_reorder_section
, ctx
, ptr
.s
, strtoul(ptr
.value
, NULL
, 10));
177 UCI_INTERNAL(uci_rename
, ctx
, &ptr
);
180 UCI_INTERNAL(uci_delete
, ctx
, &ptr
);
182 case UCI_CMD_LIST_ADD
:
183 UCI_INTERNAL(uci_add_list
, ctx
, &ptr
);
185 case UCI_CMD_LIST_DEL
:
186 UCI_INTERNAL(uci_del_list
, ctx
, &ptr
);
190 UCI_INTERNAL(uci_set
, ctx
, &ptr
);
192 if (!ptr
.option
&& e
&& (cmd
== UCI_CMD_ADD
))
193 uci_to_section(e
)->anonymous
= true;
198 UCI_THROW(ctx
, UCI_ERR_PARSE
);
201 /* returns the number of changes that were successfully parsed */
202 static int uci_parse_delta(struct uci_context
*ctx
, FILE *stream
, struct uci_package
*p
)
204 struct uci_parse_context
*pctx
;
207 /* make sure no memory from previous parse attempts is leaked */
210 pctx
= (struct uci_parse_context
*) uci_malloc(ctx
, sizeof(struct uci_parse_context
));
214 while (!feof(pctx
->file
)) {
221 * ignore parse errors in single lines, we want to preserve as much
224 UCI_TRAP_SAVE(ctx
, error
);
225 uci_parse_delta_line(ctx
, p
);
226 UCI_TRAP_RESTORE(ctx
);
232 /* no error happened, we can get rid of the parser context now */
237 /* returns the number of changes that were successfully parsed */
238 static int uci_load_delta_file(struct uci_context
*ctx
, struct uci_package
*p
, char *filename
, FILE **f
, bool flush
)
243 UCI_TRAP_SAVE(ctx
, done
);
244 stream
= uci_open_stream(ctx
, filename
, NULL
, SEEK_SET
, flush
, false);
246 changes
= uci_parse_delta(ctx
, stream
, p
);
247 UCI_TRAP_RESTORE(ctx
);
252 uci_close_stream(stream
);
256 /* returns the number of changes that were successfully parsed */
257 __private
int uci_load_delta(struct uci_context
*ctx
, struct uci_package
*p
, bool flush
)
259 struct uci_element
*e
;
260 char *filename
= NULL
;
267 uci_foreach_element(&ctx
->delta_path
, e
) {
268 if ((asprintf(&filename
, "%s/%s", e
->name
, p
->e
.name
) < 0) || !filename
)
269 UCI_THROW(ctx
, UCI_ERR_MEM
);
271 uci_load_delta_file(ctx
, p
, filename
, NULL
, false);
275 if ((asprintf(&filename
, "%s/%s", ctx
->savedir
, p
->e
.name
) < 0) || !filename
)
276 UCI_THROW(ctx
, UCI_ERR_MEM
);
278 changes
= uci_load_delta_file(ctx
, p
, filename
, &f
, flush
);
279 if (flush
&& f
&& (changes
> 0)) {
281 if (ftruncate(fileno(f
), 0) < 0) {
283 UCI_THROW(ctx
, UCI_ERR_IO
);
292 static void uci_filter_delta(struct uci_context
*ctx
, const char *name
, const char *section
, const char *option
)
294 struct uci_parse_context
*pctx
;
295 struct uci_element
*e
, *tmp
;
296 struct uci_list list
;
297 char *filename
= NULL
;
301 uci_list_init(&list
);
302 uci_alloc_parse_context(ctx
);
305 if ((asprintf(&filename
, "%s/%s", ctx
->savedir
, name
) < 0) || !filename
)
306 UCI_THROW(ctx
, UCI_ERR_MEM
);
308 UCI_TRAP_SAVE(ctx
, done
);
309 f
= uci_open_stream(ctx
, filename
, NULL
, SEEK_SET
, true, false);
312 struct uci_element
*e
;
319 /* NB: need to allocate the element before the call to
320 * uci_parse_delta_tuple, otherwise the original string
321 * gets modified before it is saved */
322 e
= uci_alloc_generic(ctx
, UCI_TYPE_DELTA
, pctx
->buf
, sizeof(struct uci_element
));
323 uci_list_add(&list
, &e
->list
);
325 uci_parse_delta_tuple(ctx
, &ptr
);
327 if (!ptr
.section
|| (strcmp(section
, ptr
.section
) != 0))
331 if (!ptr
.option
|| (strcmp(option
, ptr
.option
) != 0))
334 /* match, drop this element again */
338 /* rebuild the delta file */
340 if (ftruncate(fileno(f
), 0) < 0)
341 UCI_THROW(ctx
, UCI_ERR_IO
);
342 uci_foreach_element_safe(&list
, tmp
, e
) {
343 fprintf(f
, "%s\n", e
->name
);
346 UCI_TRAP_RESTORE(ctx
);
350 uci_close_stream(pctx
->file
);
351 uci_foreach_element_safe(&list
, tmp
, e
) {
357 int uci_revert(struct uci_context
*ctx
, struct uci_ptr
*ptr
)
359 char *package
= NULL
;
360 char *section
= NULL
;
364 uci_expand_ptr(ctx
, ptr
, false);
365 UCI_ASSERT(ctx
, ptr
->p
->has_delta
);
368 * - flush unwritten changes
369 * - save the package name
370 * - unload the package
372 * - reload the package
374 UCI_TRAP_SAVE(ctx
, error
);
375 UCI_INTERNAL(uci_save
, ctx
, ptr
->p
);
377 /* NB: need to clone package, section and option names,
378 * as they may get freed on uci_free_package() */
379 package
= uci_strdup(ctx
, ptr
->p
->e
.name
);
381 section
= uci_strdup(ctx
, ptr
->section
);
383 option
= uci_strdup(ctx
, ptr
->option
);
385 uci_free_package(&ptr
->p
);
386 uci_filter_delta(ctx
, package
, section
, option
);
388 UCI_INTERNAL(uci_load
, ctx
, package
, &ptr
->p
);
389 UCI_TRAP_RESTORE(ctx
);
397 UCI_THROW(ctx
, ctx
->err
);
401 int uci_save(struct uci_context
*ctx
, struct uci_package
*p
)
404 char *filename
= NULL
;
405 struct uci_element
*e
, *tmp
;
409 UCI_ASSERT(ctx
, p
!= NULL
);
412 * if the config file was outside of the /etc/config path,
413 * don't save the delta to a file, update the real file
415 * does not modify the uci_package pointer
418 return uci_commit(ctx
, &p
, false);
420 if (uci_list_empty(&p
->delta
))
423 if (stat(ctx
->savedir
, &statbuf
) < 0) {
424 if (stat(ctx
->confdir
, &statbuf
) == 0) {
425 mkdir(ctx
->savedir
, statbuf
.st_mode
);
427 mkdir(ctx
->savedir
, UCI_DIRMODE
);
429 } else if ((statbuf
.st_mode
& S_IFMT
) != S_IFDIR
) {
430 UCI_THROW(ctx
, UCI_ERR_IO
);
433 if ((asprintf(&filename
, "%s/%s", ctx
->savedir
, p
->e
.name
) < 0) || !filename
)
434 UCI_THROW(ctx
, UCI_ERR_MEM
);
437 UCI_TRAP_SAVE(ctx
, done
);
438 f
= uci_open_stream(ctx
, filename
, NULL
, SEEK_END
, true, true);
439 UCI_TRAP_RESTORE(ctx
);
441 uci_foreach_element_safe(&p
->delta
, tmp
, e
) {
442 struct uci_delta
*h
= uci_to_delta(e
);
443 char prefix
[2] = {0, 0};
444 if (h
->cmd
<= __UCI_CMD_LAST
)
445 prefix
[0] = uci_command_char
[h
->cmd
];
447 fprintf(f
, "%s%s.%s", prefix
, p
->e
.name
, h
->section
);
449 fprintf(f
, ".%s", e
->name
);
451 if (h
->cmd
== UCI_CMD_REMOVE
&& !h
->value
)
457 for (i
= 0; h
->value
[i
]; i
++) {
458 unsigned char c
= h
->value
[i
];
473 UCI_THROW(ctx
, ctx
->err
);