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)
104 UCI_ASSERT(ctx, dir != NULL);
106 sdir = uci_strdup(ctx, dir);
107 if (ctx->savedir != uci_savedir)
113 int uci_add_delta_path(struct uci_context *ctx, const char *dir)
115 struct uci_element *e;
118 UCI_ASSERT(ctx, dir != NULL);
119 if (!strcmp(dir, ctx->savedir))
121 e = uci_alloc_generic(ctx, UCI_TYPE_PATH, dir, sizeof(struct uci_element));
122 uci_list_add(&ctx->delta_path, &e->list);
127 char const uci_command_char[] = {
129 [UCI_CMD_REMOVE] = '-',
130 [UCI_CMD_CHANGE] = 0,
131 [UCI_CMD_RENAME] = '@',
132 [UCI_CMD_REORDER] = '^',
133 [UCI_CMD_LIST_ADD] = '|',
134 [UCI_CMD_LIST_DEL] = '~'
137 static inline int uci_parse_delta_tuple(struct uci_context *ctx, struct uci_ptr *ptr)
139 struct uci_parse_context *pctx = ctx->pctx;
140 char *str = pctx_cur_str(pctx), *arg;
143 UCI_INTERNAL(uci_parse_argument, ctx, ctx->pctx->file, &str, &arg);
144 for (c = 0; c <= __UCI_CMD_LAST; c++) {
145 if (uci_command_char[c] == *arg)
148 if (c > __UCI_CMD_LAST)
151 if (c != UCI_CMD_CHANGE)
154 UCI_INTERNAL(uci_parse_ptr, ctx, ptr, arg);
158 if (ptr->flags & UCI_LOOKUP_EXTENDED)
162 case UCI_CMD_REORDER:
163 if (!ptr->value || ptr->option)
167 if (!ptr->value || !uci_validate_name(ptr->value))
170 case UCI_CMD_LIST_ADD:
173 case UCI_CMD_LIST_DEL:
181 UCI_THROW(ctx, UCI_ERR_INVAL);
185 static void uci_parse_delta_line(struct uci_context *ctx, struct uci_package *p)
187 struct uci_element *e = NULL;
191 cmd = uci_parse_delta_tuple(ctx, &ptr);
192 if (strcmp(ptr.package, p->e.name) != 0)
195 if (ctx->flags & UCI_FLAG_SAVED_DELTA)
196 uci_add_delta(ctx, &p->saved_delta, cmd, ptr.section, ptr.option, ptr.value);
199 case UCI_CMD_REORDER:
200 uci_expand_ptr(ctx, &ptr, true);
202 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
203 UCI_INTERNAL(uci_reorder_section, ctx, ptr.s, strtoul(ptr.value, NULL, 10));
206 UCI_INTERNAL(uci_rename, ctx, &ptr);
209 UCI_INTERNAL(uci_delete, ctx, &ptr);
211 case UCI_CMD_LIST_ADD:
212 UCI_INTERNAL(uci_add_list, ctx, &ptr);
214 case UCI_CMD_LIST_DEL:
215 UCI_INTERNAL(uci_del_list, ctx, &ptr);
219 UCI_INTERNAL(uci_set, ctx, &ptr);
221 if (!ptr.option && e && (cmd == UCI_CMD_ADD))
222 uci_to_section(e)->anonymous = true;
227 UCI_THROW(ctx, UCI_ERR_PARSE);
230 /* returns the number of changes that were successfully parsed */
231 static int uci_parse_delta(struct uci_context *ctx, FILE *stream, struct uci_package *p)
233 struct uci_parse_context *pctx;
236 /* make sure no memory from previous parse attempts is leaked */
239 pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
243 while (!feof(pctx->file)) {
250 * ignore parse errors in single lines, we want to preserve as much
253 UCI_TRAP_SAVE(ctx, error);
254 uci_parse_delta_line(ctx, p);
255 UCI_TRAP_RESTORE(ctx);
261 /* no error happened, we can get rid of the parser context now */
266 /* returns the number of changes that were successfully parsed */
267 static int uci_load_delta_file(struct uci_context *ctx, struct uci_package *p, char *filename, FILE **f, bool flush)
272 UCI_TRAP_SAVE(ctx, done);
273 stream = uci_open_stream(ctx, filename, NULL, SEEK_SET, flush, false);
275 changes = uci_parse_delta(ctx, stream, p);
276 UCI_TRAP_RESTORE(ctx);
281 uci_close_stream(stream);
285 /* returns the number of changes that were successfully parsed */
286 __private int uci_load_delta(struct uci_context *ctx, struct uci_package *p, bool flush)
288 struct uci_element *e;
289 char *filename = NULL;
296 uci_foreach_element(&ctx->delta_path, e) {
297 if ((asprintf(&filename, "%s/%s", e->name, p->e.name) < 0) || !filename)
298 UCI_THROW(ctx, UCI_ERR_MEM);
300 uci_load_delta_file(ctx, p, filename, NULL, false);
304 if ((asprintf(&filename, "%s/%s", ctx->savedir, p->e.name) < 0) || !filename)
305 UCI_THROW(ctx, UCI_ERR_MEM);
307 changes = uci_load_delta_file(ctx, p, filename, &f, flush);
308 if (flush && f && (changes > 0)) {
310 if (ftruncate(fileno(f), 0) < 0) {
312 UCI_THROW(ctx, UCI_ERR_IO);
321 static void uci_filter_delta(struct uci_context *ctx, const char *name, const char *section, const char *option)
323 struct uci_parse_context *pctx;
324 struct uci_element *e, *tmp;
325 struct uci_list list;
326 char *filename = NULL;
330 uci_list_init(&list);
331 uci_alloc_parse_context(ctx);
334 if ((asprintf(&filename, "%s/%s", ctx->savedir, name) < 0) || !filename)
335 UCI_THROW(ctx, UCI_ERR_MEM);
337 UCI_TRAP_SAVE(ctx, done);
338 f = uci_open_stream(ctx, filename, NULL, SEEK_SET, true, false);
349 c = uci_parse_delta_tuple(ctx, &ptr);
352 if (!ptr.section || (strcmp(section, ptr.section) != 0))
355 if (match && option) {
356 if (!ptr.option || (strcmp(option, ptr.option) != 0))
361 uci_add_delta(ctx, &list, c,
362 ptr.section, ptr.option, ptr.value);
366 /* rebuild the delta file */
368 if (ftruncate(fileno(f), 0) < 0)
369 UCI_THROW(ctx, UCI_ERR_IO);
370 uci_foreach_element_safe(&list, tmp, e) {
371 struct uci_delta *h = uci_to_delta(e);
372 uci_delta_save(ctx, f, name, h);
375 UCI_TRAP_RESTORE(ctx);
379 uci_close_stream(pctx->file);
380 uci_foreach_element_safe(&list, tmp, e) {
381 uci_free_delta(uci_to_delta(e));
386 int uci_revert(struct uci_context *ctx, struct uci_ptr *ptr)
388 char *package = NULL;
389 char *section = NULL;
393 uci_expand_ptr(ctx, ptr, false);
394 UCI_ASSERT(ctx, ptr->p->has_delta);
397 * - flush unwritten changes
398 * - save the package name
399 * - unload the package
401 * - reload the package
403 UCI_TRAP_SAVE(ctx, error);
404 UCI_INTERNAL(uci_save, ctx, ptr->p);
406 /* NB: need to clone package, section and option names,
407 * as they may get freed on uci_free_package() */
408 package = uci_strdup(ctx, ptr->p->e.name);
410 section = uci_strdup(ctx, ptr->section);
412 option = uci_strdup(ctx, ptr->option);
414 uci_free_package(&ptr->p);
415 uci_filter_delta(ctx, package, section, option);
417 UCI_INTERNAL(uci_load, ctx, package, &ptr->p);
418 UCI_TRAP_RESTORE(ctx);
426 UCI_THROW(ctx, ctx->err);
430 int uci_save(struct uci_context *ctx, struct uci_package *p)
433 char *filename = NULL;
434 struct uci_element *e, *tmp;
438 UCI_ASSERT(ctx, p != NULL);
441 * if the config file was outside of the /etc/config path,
442 * don't save the delta to a file, update the real file
444 * does not modify the uci_package pointer
447 return uci_commit(ctx, &p, false);
449 if (uci_list_empty(&p->delta))
452 if (stat(ctx->savedir, &statbuf) < 0) {
453 if (stat(ctx->confdir, &statbuf) == 0) {
454 mkdir(ctx->savedir, statbuf.st_mode);
456 mkdir(ctx->savedir, UCI_DIRMODE);
458 } else if ((statbuf.st_mode & S_IFMT) != S_IFDIR) {
459 UCI_THROW(ctx, UCI_ERR_IO);
462 if ((asprintf(&filename, "%s/%s", ctx->savedir, p->e.name) < 0) || !filename)
463 UCI_THROW(ctx, UCI_ERR_MEM);
466 UCI_TRAP_SAVE(ctx, done);
467 f = uci_open_stream(ctx, filename, NULL, SEEK_END, true, true);
468 UCI_TRAP_RESTORE(ctx);
470 uci_foreach_element_safe(&p->delta, tmp, e) {
471 struct uci_delta *h = uci_to_delta(e);
472 uci_delta_save(ctx, f, p->e.name, h);
480 UCI_THROW(ctx, ctx->err);