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 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 e
= uci_alloc_generic(ctx
, UCI_TYPE_PATH
, dir
, sizeof(struct uci_element
));
91 uci_list_add(&ctx
->delta_path
, &e
->list
);
96 static inline int uci_parse_delta_tuple(struct uci_context
*ctx
, char **buf
, struct uci_ptr
*ptr
)
98 int c
= UCI_CMD_CHANGE
;
111 /* UCI_CMD_ADD is used for anonymous sections or list values */
115 c
= UCI_CMD_LIST_ADD
;
119 if (c
!= UCI_CMD_CHANGE
)
122 UCI_INTERNAL(uci_parse_ptr
, ctx
, ptr
, *buf
);
126 if (ptr
->flags
& UCI_LOOKUP_EXTENDED
)
130 case UCI_CMD_REORDER
:
131 if (!ptr
->value
|| ptr
->option
)
135 if (!ptr
->value
|| !uci_validate_name(ptr
->value
))
138 case UCI_CMD_LIST_ADD
:
146 UCI_THROW(ctx
, UCI_ERR_INVAL
);
150 static void uci_parse_delta_line(struct uci_context
*ctx
, struct uci_package
*p
, char *buf
)
152 struct uci_element
*e
= NULL
;
156 cmd
= uci_parse_delta_tuple(ctx
, &buf
, &ptr
);
157 if (strcmp(ptr
.package
, p
->e
.name
) != 0)
160 if (ctx
->flags
& UCI_FLAG_SAVED_DELTA
)
161 uci_add_delta(ctx
, &p
->saved_delta
, cmd
, ptr
.section
, ptr
.option
, ptr
.value
);
164 case UCI_CMD_REORDER
:
165 uci_expand_ptr(ctx
, &ptr
, true);
167 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
168 UCI_INTERNAL(uci_reorder_section
, ctx
, ptr
.s
, strtoul(ptr
.value
, NULL
, 10));
171 UCI_INTERNAL(uci_rename
, ctx
, &ptr
);
174 UCI_INTERNAL(uci_delete
, ctx
, &ptr
);
176 case UCI_CMD_LIST_ADD
:
177 UCI_INTERNAL(uci_add_list
, ctx
, &ptr
);
181 UCI_INTERNAL(uci_set
, ctx
, &ptr
);
183 if (!ptr
.option
&& e
&& (cmd
== UCI_CMD_ADD
))
184 uci_to_section(e
)->anonymous
= true;
189 UCI_THROW(ctx
, UCI_ERR_PARSE
);
192 /* returns the number of changes that were successfully parsed */
193 static int uci_parse_delta(struct uci_context
*ctx
, FILE *stream
, struct uci_package
*p
)
195 struct uci_parse_context
*pctx
;
198 /* make sure no memory from previous parse attempts is leaked */
201 pctx
= (struct uci_parse_context
*) uci_malloc(ctx
, sizeof(struct uci_parse_context
));
205 while (!feof(pctx
->file
)) {
211 * ignore parse errors in single lines, we want to preserve as much
214 UCI_TRAP_SAVE(ctx
, error
);
215 uci_parse_delta_line(ctx
, p
, pctx
->buf
);
216 UCI_TRAP_RESTORE(ctx
);
222 /* no error happened, we can get rid of the parser context now */
227 /* returns the number of changes that were successfully parsed */
228 static int uci_load_delta_file(struct uci_context
*ctx
, struct uci_package
*p
, char *filename
, FILE **f
, bool flush
)
233 UCI_TRAP_SAVE(ctx
, done
);
234 stream
= uci_open_stream(ctx
, filename
, SEEK_SET
, flush
, false);
236 changes
= uci_parse_delta(ctx
, stream
, p
);
237 UCI_TRAP_RESTORE(ctx
);
242 uci_close_stream(stream
);
246 /* returns the number of changes that were successfully parsed */
247 __private
int uci_load_delta(struct uci_context
*ctx
, struct uci_package
*p
, bool flush
)
249 struct uci_element
*e
;
250 char *filename
= NULL
;
257 uci_foreach_element(&ctx
->delta_path
, e
) {
258 if ((asprintf(&filename
, "%s/%s", e
->name
, p
->e
.name
) < 0) || !filename
)
259 UCI_THROW(ctx
, UCI_ERR_MEM
);
261 uci_load_delta_file(ctx
, p
, filename
, NULL
, false);
265 if ((asprintf(&filename
, "%s/%s", ctx
->savedir
, p
->e
.name
) < 0) || !filename
)
266 UCI_THROW(ctx
, UCI_ERR_MEM
);
268 changes
= uci_load_delta_file(ctx
, p
, filename
, &f
, flush
);
269 if (flush
&& f
&& (changes
> 0)) {
271 if (ftruncate(fileno(f
), 0) < 0) {
273 UCI_THROW(ctx
, UCI_ERR_IO
);
283 static void uci_filter_delta(struct uci_context
*ctx
, const char *name
, const char *section
, const char *option
)
285 struct uci_parse_context
*pctx
;
286 struct uci_element
*e
, *tmp
;
287 struct uci_list list
;
288 char *filename
= NULL
;
292 uci_list_init(&list
);
293 uci_alloc_parse_context(ctx
);
296 if ((asprintf(&filename
, "%s/%s", ctx
->savedir
, name
) < 0) || !filename
)
297 UCI_THROW(ctx
, UCI_ERR_MEM
);
299 UCI_TRAP_SAVE(ctx
, done
);
300 f
= uci_open_stream(ctx
, filename
, SEEK_SET
, true, false);
303 struct uci_element
*e
;
311 /* NB: need to allocate the element before the call to
312 * uci_parse_delta_tuple, otherwise the original string
313 * gets modified before it is saved */
314 e
= uci_alloc_generic(ctx
, UCI_TYPE_DELTA
, pctx
->buf
, sizeof(struct uci_element
));
315 uci_list_add(&list
, &e
->list
);
317 uci_parse_delta_tuple(ctx
, &buf
, &ptr
);
319 if (!ptr
.section
|| (strcmp(section
, ptr
.section
) != 0))
323 if (!ptr
.option
|| (strcmp(option
, ptr
.option
) != 0))
326 /* match, drop this element again */
330 /* rebuild the delta file */
332 if (ftruncate(fileno(f
), 0) < 0)
333 UCI_THROW(ctx
, UCI_ERR_IO
);
334 uci_foreach_element_safe(&list
, tmp
, e
) {
335 fprintf(f
, "%s\n", e
->name
);
338 UCI_TRAP_RESTORE(ctx
);
343 uci_close_stream(pctx
->file
);
344 uci_foreach_element_safe(&list
, tmp
, e
) {
350 int uci_revert(struct uci_context
*ctx
, struct uci_ptr
*ptr
)
352 char *package
= NULL
;
353 char *section
= NULL
;
357 uci_expand_ptr(ctx
, ptr
, false);
358 UCI_ASSERT(ctx
, ptr
->p
->has_delta
);
361 * - flush unwritten changes
362 * - save the package name
363 * - unload the package
365 * - reload the package
367 UCI_TRAP_SAVE(ctx
, error
);
368 UCI_INTERNAL(uci_save
, ctx
, ptr
->p
);
370 /* NB: need to clone package, section and option names,
371 * as they may get freed on uci_free_package() */
372 package
= uci_strdup(ctx
, ptr
->p
->e
.name
);
374 section
= uci_strdup(ctx
, ptr
->section
);
376 option
= uci_strdup(ctx
, ptr
->option
);
378 uci_free_package(&ptr
->p
);
379 uci_filter_delta(ctx
, package
, section
, option
);
381 UCI_INTERNAL(uci_load
, ctx
, package
, &ptr
->p
);
382 UCI_TRAP_RESTORE(ctx
);
393 UCI_THROW(ctx
, ctx
->err
);
397 int uci_save(struct uci_context
*ctx
, struct uci_package
*p
)
400 char *filename
= NULL
;
401 struct uci_element
*e
, *tmp
;
405 UCI_ASSERT(ctx
, p
!= NULL
);
408 * if the config file was outside of the /etc/config path,
409 * don't save the delta to a file, update the real file
411 * does not modify the uci_package pointer
414 return uci_commit(ctx
, &p
, false);
416 if (uci_list_empty(&p
->delta
))
419 if (stat(ctx
->savedir
, &statbuf
) < 0)
420 mkdir(ctx
->savedir
, UCI_DIRMODE
);
421 else if ((statbuf
.st_mode
& S_IFMT
) != S_IFDIR
)
422 UCI_THROW(ctx
, UCI_ERR_IO
);
424 if ((asprintf(&filename
, "%s/%s", ctx
->savedir
, p
->e
.name
) < 0) || !filename
)
425 UCI_THROW(ctx
, UCI_ERR_MEM
);
427 uci_foreach_element(&ctx
->hooks
, tmp
) {
428 struct uci_hook
*hook
= uci_to_hook(tmp
);
433 uci_foreach_element(&p
->delta
, e
) {
434 hook
->ops
->set(hook
->ops
, p
, uci_to_delta(e
));
439 UCI_TRAP_SAVE(ctx
, done
);
440 f
= uci_open_stream(ctx
, filename
, SEEK_END
, true, true);
441 UCI_TRAP_RESTORE(ctx
);
443 uci_foreach_element_safe(&p
->delta
, tmp
, e
) {
444 struct uci_delta
*h
= uci_to_delta(e
);
457 case UCI_CMD_REORDER
:
460 case UCI_CMD_LIST_ADD
:
467 fprintf(f
, "%s%s.%s", prefix
, p
->e
.name
, h
->section
);
469 fprintf(f
, ".%s", e
->name
);
471 if (h
->cmd
== UCI_CMD_REMOVE
)
474 fprintf(f
, "=%s\n", h
->value
);
483 UCI_THROW(ctx
, ctx
->err
);