2eb2ae73ade2cac666de39c5a5493362d20b901e
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 static inline int uci_parse_delta_tuple(struct uci_context
*ctx
, struct uci_ptr
*ptr
)
100 struct uci_parse_context
*pctx
= ctx
->pctx
;
101 char *str
= pctx_cur_str(pctx
), *arg
;
102 int c
= UCI_CMD_CHANGE
;
104 UCI_INTERNAL(uci_parse_argument
, ctx
, ctx
->pctx
->file
, &str
, &arg
);
116 /* UCI_CMD_ADD is used for anonymous sections or list values */
120 c
= UCI_CMD_LIST_ADD
;
123 c
= UCI_CMD_LIST_DEL
;
127 if (c
!= UCI_CMD_CHANGE
)
130 UCI_INTERNAL(uci_parse_ptr
, ctx
, ptr
, arg
);
134 if (ptr
->flags
& UCI_LOOKUP_EXTENDED
)
138 case UCI_CMD_REORDER
:
139 if (!ptr
->value
|| ptr
->option
)
143 if (!ptr
->value
|| !uci_validate_name(ptr
->value
))
146 case UCI_CMD_LIST_ADD
:
149 case UCI_CMD_LIST_DEL
:
157 UCI_THROW(ctx
, UCI_ERR_INVAL
);
161 static void uci_parse_delta_line(struct uci_context
*ctx
, struct uci_package
*p
)
163 struct uci_element
*e
= NULL
;
167 cmd
= uci_parse_delta_tuple(ctx
, &ptr
);
168 if (strcmp(ptr
.package
, p
->e
.name
) != 0)
171 if (ctx
->flags
& UCI_FLAG_SAVED_DELTA
)
172 uci_add_delta(ctx
, &p
->saved_delta
, cmd
, ptr
.section
, ptr
.option
, ptr
.value
);
175 case UCI_CMD_REORDER
:
176 uci_expand_ptr(ctx
, &ptr
, true);
178 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
179 UCI_INTERNAL(uci_reorder_section
, ctx
, ptr
.s
, strtoul(ptr
.value
, NULL
, 10));
182 UCI_INTERNAL(uci_rename
, ctx
, &ptr
);
185 UCI_INTERNAL(uci_delete
, ctx
, &ptr
);
187 case UCI_CMD_LIST_ADD
:
188 UCI_INTERNAL(uci_add_list
, ctx
, &ptr
);
190 case UCI_CMD_LIST_DEL
:
191 UCI_INTERNAL(uci_del_list
, ctx
, &ptr
);
195 UCI_INTERNAL(uci_set
, ctx
, &ptr
);
197 if (!ptr
.option
&& e
&& (cmd
== UCI_CMD_ADD
))
198 uci_to_section(e
)->anonymous
= true;
203 UCI_THROW(ctx
, UCI_ERR_PARSE
);
206 /* returns the number of changes that were successfully parsed */
207 static int uci_parse_delta(struct uci_context
*ctx
, FILE *stream
, struct uci_package
*p
)
209 struct uci_parse_context
*pctx
;
212 /* make sure no memory from previous parse attempts is leaked */
215 pctx
= (struct uci_parse_context
*) uci_malloc(ctx
, sizeof(struct uci_parse_context
));
219 while (!feof(pctx
->file
)) {
226 * ignore parse errors in single lines, we want to preserve as much
229 UCI_TRAP_SAVE(ctx
, error
);
230 uci_parse_delta_line(ctx
, p
);
231 UCI_TRAP_RESTORE(ctx
);
237 /* no error happened, we can get rid of the parser context now */
242 /* returns the number of changes that were successfully parsed */
243 static int uci_load_delta_file(struct uci_context
*ctx
, struct uci_package
*p
, char *filename
, FILE **f
, bool flush
)
248 UCI_TRAP_SAVE(ctx
, done
);
249 stream
= uci_open_stream(ctx
, filename
, NULL
, SEEK_SET
, flush
, false);
251 changes
= uci_parse_delta(ctx
, stream
, p
);
252 UCI_TRAP_RESTORE(ctx
);
257 uci_close_stream(stream
);
261 /* returns the number of changes that were successfully parsed */
262 __private
int uci_load_delta(struct uci_context
*ctx
, struct uci_package
*p
, bool flush
)
264 struct uci_element
*e
;
265 char *filename
= NULL
;
272 uci_foreach_element(&ctx
->delta_path
, e
) {
273 if ((asprintf(&filename
, "%s/%s", e
->name
, p
->e
.name
) < 0) || !filename
)
274 UCI_THROW(ctx
, UCI_ERR_MEM
);
276 uci_load_delta_file(ctx
, p
, filename
, NULL
, false);
280 if ((asprintf(&filename
, "%s/%s", ctx
->savedir
, p
->e
.name
) < 0) || !filename
)
281 UCI_THROW(ctx
, UCI_ERR_MEM
);
283 changes
= uci_load_delta_file(ctx
, p
, filename
, &f
, flush
);
284 if (flush
&& f
&& (changes
> 0)) {
286 if (ftruncate(fileno(f
), 0) < 0) {
288 UCI_THROW(ctx
, UCI_ERR_IO
);
297 static void uci_filter_delta(struct uci_context
*ctx
, const char *name
, const char *section
, const char *option
)
299 struct uci_parse_context
*pctx
;
300 struct uci_element
*e
, *tmp
;
301 struct uci_list list
;
302 char *filename
= NULL
;
306 uci_list_init(&list
);
307 uci_alloc_parse_context(ctx
);
310 if ((asprintf(&filename
, "%s/%s", ctx
->savedir
, name
) < 0) || !filename
)
311 UCI_THROW(ctx
, UCI_ERR_MEM
);
313 UCI_TRAP_SAVE(ctx
, done
);
314 f
= uci_open_stream(ctx
, filename
, NULL
, SEEK_SET
, true, false);
317 struct uci_element
*e
;
324 /* NB: need to allocate the element before the call to
325 * uci_parse_delta_tuple, otherwise the original string
326 * gets modified before it is saved */
327 e
= uci_alloc_generic(ctx
, UCI_TYPE_DELTA
, pctx
->buf
, sizeof(struct uci_element
));
328 uci_list_add(&list
, &e
->list
);
330 uci_parse_delta_tuple(ctx
, &ptr
);
332 if (!ptr
.section
|| (strcmp(section
, ptr
.section
) != 0))
336 if (!ptr
.option
|| (strcmp(option
, ptr
.option
) != 0))
339 /* match, drop this element again */
343 /* rebuild the delta file */
345 if (ftruncate(fileno(f
), 0) < 0)
346 UCI_THROW(ctx
, UCI_ERR_IO
);
347 uci_foreach_element_safe(&list
, tmp
, e
) {
348 fprintf(f
, "%s\n", e
->name
);
351 UCI_TRAP_RESTORE(ctx
);
355 uci_close_stream(pctx
->file
);
356 uci_foreach_element_safe(&list
, tmp
, e
) {
362 int uci_revert(struct uci_context
*ctx
, struct uci_ptr
*ptr
)
364 char *package
= NULL
;
365 char *section
= NULL
;
369 uci_expand_ptr(ctx
, ptr
, false);
370 UCI_ASSERT(ctx
, ptr
->p
->has_delta
);
373 * - flush unwritten changes
374 * - save the package name
375 * - unload the package
377 * - reload the package
379 UCI_TRAP_SAVE(ctx
, error
);
380 UCI_INTERNAL(uci_save
, ctx
, ptr
->p
);
382 /* NB: need to clone package, section and option names,
383 * as they may get freed on uci_free_package() */
384 package
= uci_strdup(ctx
, ptr
->p
->e
.name
);
386 section
= uci_strdup(ctx
, ptr
->section
);
388 option
= uci_strdup(ctx
, ptr
->option
);
390 uci_free_package(&ptr
->p
);
391 uci_filter_delta(ctx
, package
, section
, option
);
393 UCI_INTERNAL(uci_load
, ctx
, package
, &ptr
->p
);
394 UCI_TRAP_RESTORE(ctx
);
402 UCI_THROW(ctx
, ctx
->err
);
406 int uci_save(struct uci_context
*ctx
, struct uci_package
*p
)
409 char *filename
= NULL
;
410 struct uci_element
*e
, *tmp
;
414 UCI_ASSERT(ctx
, p
!= NULL
);
417 * if the config file was outside of the /etc/config path,
418 * don't save the delta to a file, update the real file
420 * does not modify the uci_package pointer
423 return uci_commit(ctx
, &p
, false);
425 if (uci_list_empty(&p
->delta
))
428 if (stat(ctx
->savedir
, &statbuf
) < 0) {
429 if (stat(ctx
->confdir
, &statbuf
) == 0) {
430 mkdir(ctx
->savedir
, statbuf
.st_mode
);
432 mkdir(ctx
->savedir
, UCI_DIRMODE
);
434 } else if ((statbuf
.st_mode
& S_IFMT
) != S_IFDIR
) {
435 UCI_THROW(ctx
, UCI_ERR_IO
);
438 if ((asprintf(&filename
, "%s/%s", ctx
->savedir
, p
->e
.name
) < 0) || !filename
)
439 UCI_THROW(ctx
, UCI_ERR_MEM
);
442 UCI_TRAP_SAVE(ctx
, done
);
443 f
= uci_open_stream(ctx
, filename
, NULL
, SEEK_END
, true, true);
444 UCI_TRAP_RESTORE(ctx
);
446 uci_foreach_element_safe(&p
->delta
, tmp
, e
) {
447 struct uci_delta
*h
= uci_to_delta(e
);
460 case UCI_CMD_REORDER
:
463 case UCI_CMD_LIST_ADD
:
466 case UCI_CMD_LIST_DEL
:
473 fprintf(f
, "%s%s.%s", prefix
, p
->e
.name
, h
->section
);
475 fprintf(f
, ".%s", e
->name
);
477 if (h
->cmd
== UCI_CMD_REMOVE
&& !h
->value
)
483 for (i
= 0; h
->value
[i
]; i
++) {
484 unsigned char c
= h
->value
[i
];
499 UCI_THROW(ctx
, ctx
->err
);