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 history files
20 #include <sys/types.h>
29 /* record a change that was done to a package */
31 uci_add_history(struct uci_context *ctx, struct uci_list *list, int cmd, const char *section, const char *option, const char *value)
33 struct uci_history *h;
34 int size = strlen(section) + 1;
38 size += strlen(value) + 1;
40 h = uci_alloc_element(ctx, history, option, size);
43 h->section = strcpy(ptr, section);
45 ptr += strlen(ptr) + 1;
46 h->value = strcpy(ptr, value);
48 uci_list_add(list, &h->e.list);
52 uci_free_history(struct uci_history *h)
56 if ((h->section != NULL) &&
57 (h->section != uci_dataptr(h))) {
61 uci_free_element(&h->e);
65 int uci_set_savedir(struct uci_context *ctx, const char *dir)
70 UCI_ASSERT(ctx, dir != NULL);
72 sdir = uci_strdup(ctx, dir);
73 if (ctx->savedir != uci_savedir)
79 int uci_add_history_path(struct uci_context *ctx, const char *dir)
81 struct uci_element *e;
84 UCI_ASSERT(ctx, dir != NULL);
85 e = uci_alloc_generic(ctx, UCI_TYPE_PATH, dir, sizeof(struct uci_element));
86 uci_list_add(&ctx->history_path, &e->list);
91 static inline void uci_parse_history_tuple(struct uci_context *ctx, char **buf, char **package, char **section, char **option, char **value, int *cmd)
93 int c = UCI_CMD_CHANGE;
103 /* UCI_CMD_ADD is used for anonymous sections or list values */
107 c = UCI_CMD_LIST_ADD;
111 if (c != UCI_CMD_CHANGE)
117 UCI_INTERNAL(uci_parse_tuple, ctx, *buf, package, section, option, value);
119 UCI_THROW(ctx, UCI_ERR_PARSE);
123 static void uci_parse_history_line(struct uci_context *ctx, struct uci_package *p, char *buf)
125 struct uci_element *e = NULL;
128 char *package = NULL;
129 char *section = NULL;
134 uci_parse_history_tuple(ctx, &buf, &package, §ion, &option, &value, &cmd);
135 if (!package || (strcmp(package, p->e.name) != 0))
137 if (!uci_validate_name(section))
139 if (option && !uci_validate_name(option))
141 if (rename && !uci_validate_str(value, (option || delete)))
144 if (ctx->flags & UCI_FLAG_SAVED_HISTORY)
145 uci_add_history(ctx, &p->saved_history, cmd, section, option, value);
149 UCI_INTERNAL(uci_rename, ctx, p, section, option, value);
152 UCI_INTERNAL(uci_delete, ctx, p, section, option);
154 case UCI_CMD_LIST_ADD:
155 UCI_INTERNAL(uci_add_list, ctx, p, section, option, value, NULL);
159 UCI_INTERNAL(uci_set, ctx, p, section, option, value, &e);
160 if (!option && e && (cmd == UCI_CMD_ADD))
161 uci_to_section(e)->anonymous = true;
166 UCI_THROW(ctx, UCI_ERR_PARSE);
169 /* returns the number of changes that were successfully parsed */
170 static int uci_parse_history(struct uci_context *ctx, FILE *stream, struct uci_package *p)
172 struct uci_parse_context *pctx;
175 /* make sure no memory from previous parse attempts is leaked */
178 pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
182 while (!feof(pctx->file)) {
188 * ignore parse errors in single lines, we want to preserve as much
189 * history as possible
191 UCI_TRAP_SAVE(ctx, error);
192 uci_parse_history_line(ctx, p, pctx->buf);
193 UCI_TRAP_RESTORE(ctx);
199 /* no error happened, we can get rid of the parser context now */
204 /* returns the number of changes that were successfully parsed */
205 static int uci_load_history_file(struct uci_context *ctx, struct uci_package *p, char *filename, FILE **f, bool flush)
210 UCI_TRAP_SAVE(ctx, done);
211 stream = uci_open_stream(ctx, filename, SEEK_SET, flush, false);
213 changes = uci_parse_history(ctx, stream, p);
214 UCI_TRAP_RESTORE(ctx);
219 uci_close_stream(stream);
223 /* returns the number of changes that were successfully parsed */
224 static int uci_load_history(struct uci_context *ctx, struct uci_package *p, bool flush)
226 struct uci_element *e;
227 char *filename = NULL;
234 uci_foreach_element(&ctx->history_path, e) {
235 if ((asprintf(&filename, "%s/%s", e->name, p->e.name) < 0) || !filename)
236 UCI_THROW(ctx, UCI_ERR_MEM);
238 uci_load_history_file(ctx, p, filename, NULL, false);
242 if ((asprintf(&filename, "%s/%s", ctx->savedir, p->e.name) < 0) || !filename)
243 UCI_THROW(ctx, UCI_ERR_MEM);
245 changes = uci_load_history_file(ctx, p, filename, &f, flush);
246 if (flush && f && (changes > 0)) {
248 ftruncate(fileno(f), 0);
257 static void uci_filter_history(struct uci_context *ctx, const char *name, const char *section, const char *option)
259 struct uci_parse_context *pctx;
260 struct uci_element *e, *tmp;
261 struct uci_list list;
262 char *filename = NULL;
269 uci_list_init(&list);
270 uci_alloc_parse_context(ctx);
273 if ((asprintf(&filename, "%s/%s", ctx->savedir, name) < 0) || !filename)
274 UCI_THROW(ctx, UCI_ERR_MEM);
276 UCI_TRAP_SAVE(ctx, done);
277 f = uci_open_stream(ctx, filename, SEEK_SET, true, false);
280 struct uci_element *e;
288 /* NB: need to allocate the element before the call to
289 * uci_parse_history_tuple, otherwise the original string
290 * gets modified before it is saved */
291 e = uci_alloc_generic(ctx, UCI_TYPE_HISTORY, pctx->buf, sizeof(struct uci_element));
292 uci_list_add(&list, &e->list);
294 uci_parse_history_tuple(ctx, &buf, &p, &s, &o, &v, NULL);
296 if (!s || (strcmp(section, s) != 0))
300 if (!o || (strcmp(option, o) != 0))
303 /* match, drop this element again */
307 /* rebuild the history file */
309 ftruncate(fileno(f), 0);
310 uci_foreach_element_safe(&list, tmp, e) {
311 fprintf(f, "%s\n", e->name);
314 UCI_TRAP_RESTORE(ctx);
320 uci_foreach_element_safe(&list, tmp, e) {
326 int uci_revert(struct uci_context *ctx, struct uci_package **pkg, const char *section, const char *option)
328 struct uci_package *p;
332 UCI_ASSERT(ctx, pkg != NULL);
334 UCI_ASSERT(ctx, p != NULL);
335 UCI_ASSERT(ctx, p->has_history);
338 * - flush unwritten changes
339 * - save the package name
340 * - unload the package
341 * - filter the history
342 * - reload the package
344 UCI_TRAP_SAVE(ctx, error);
345 UCI_INTERNAL(uci_save, ctx, p);
346 name = uci_strdup(ctx, p->e.name);
349 uci_free_package(&p);
350 uci_filter_history(ctx, name, section, option);
352 UCI_INTERNAL(uci_load, ctx, name, &p);
353 UCI_TRAP_RESTORE(ctx);
360 UCI_THROW(ctx, ctx->err);
364 int uci_save(struct uci_context *ctx, struct uci_package *p)
367 char *filename = NULL;
368 struct uci_element *e, *tmp;
372 UCI_ASSERT(ctx, p != NULL);
375 * if the config file was outside of the /etc/config path,
376 * don't save the history to a file, update the real file
378 * does not modify the uci_package pointer
381 return uci_commit(ctx, &p, false);
383 if (uci_list_empty(&p->history))
386 if (stat(ctx->savedir, &statbuf) < 0)
387 mkdir(ctx->savedir, UCI_DIRMODE);
388 else if ((statbuf.st_mode & S_IFMT) != S_IFDIR)
389 UCI_THROW(ctx, UCI_ERR_IO);
391 if ((asprintf(&filename, "%s/%s", ctx->savedir, p->e.name) < 0) || !filename)
392 UCI_THROW(ctx, UCI_ERR_MEM);
395 UCI_TRAP_SAVE(ctx, done);
396 f = uci_open_stream(ctx, filename, SEEK_END, true, true);
397 UCI_TRAP_RESTORE(ctx);
399 uci_foreach_element_safe(&p->history, tmp, e) {
400 struct uci_history *h = uci_to_history(e);
413 case UCI_CMD_LIST_ADD:
420 fprintf(f, "%s%s.%s", prefix, p->e.name, h->section);
422 fprintf(f, ".%s", e->name);
424 if (h->cmd == UCI_CMD_REMOVE)
427 fprintf(f, "=%s\n", h->value);
436 UCI_THROW(ctx, ctx->err);