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 int uci_set_savedir(struct uci_context *ctx, const char *dir)
34 UCI_ASSERT(ctx, dir != NULL);
36 sdir = uci_strdup(ctx, dir);
37 if (ctx->savedir != uci_savedir)
43 int uci_add_history_path(struct uci_context *ctx, const char *dir)
45 struct uci_element *e;
48 UCI_ASSERT(ctx, dir != NULL);
49 e = uci_alloc_generic(ctx, UCI_TYPE_PATH, dir, sizeof(struct uci_element));
50 uci_list_add(&ctx->history_path, &e->list);
55 static inline void uci_parse_history_tuple(struct uci_context *ctx, char **buf, char **package, char **section, char **option, char **value, bool *delete, bool *rename)
61 } else if (**buf == '@') {
67 UCI_INTERNAL(uci_parse_tuple, ctx, *buf, package, section, option, value);
69 static void uci_parse_history_line(struct uci_context *ctx, struct uci_package *p, char *buf)
78 uci_parse_history_tuple(ctx, &buf, &package, §ion, &option, &value, &delete, &rename);
79 if (!package || (strcmp(package, p->e.name) != 0))
81 if (!uci_validate_name(section))
83 if (option && !uci_validate_name(option))
85 if ((rename || (!option && !delete)) && !uci_validate_name(value))
89 UCI_INTERNAL(uci_rename, ctx, p, section, option, value);
91 UCI_INTERNAL(uci_delete, ctx, p, section, option);
93 UCI_INTERNAL(uci_set, ctx, p, section, option, value);
97 UCI_THROW(ctx, UCI_ERR_PARSE);
100 static void uci_parse_history(struct uci_context *ctx, FILE *stream, struct uci_package *p)
102 struct uci_parse_context *pctx;
104 /* make sure no memory from previous parse attempts is leaked */
105 ctx->internal = true;
108 pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
112 while (!feof(pctx->file)) {
118 * ignore parse errors in single lines, we want to preserve as much
119 * history as possible
121 UCI_TRAP_SAVE(ctx, error);
122 uci_parse_history_line(ctx, p, pctx->buf);
123 UCI_TRAP_RESTORE(ctx);
128 /* no error happened, we can get rid of the parser context now */
129 ctx->internal = true;
133 static void uci_load_history_file(struct uci_context *ctx, struct uci_package *p, char *filename, FILE **f, bool flush)
137 UCI_TRAP_SAVE(ctx, done);
138 stream = uci_open_stream(ctx, filename, SEEK_SET, flush, false);
140 uci_parse_history(ctx, stream, p);
141 UCI_TRAP_RESTORE(ctx);
146 uci_close_stream(stream);
149 static void uci_load_history(struct uci_context *ctx, struct uci_package *p, bool flush)
151 struct uci_element *e;
152 char *filename = NULL;
158 uci_foreach_element(&ctx->history_path, e) {
159 if ((asprintf(&filename, "%s/%s", e->name, p->e.name) < 0) || !filename)
160 UCI_THROW(ctx, UCI_ERR_MEM);
162 uci_load_history_file(ctx, p, filename, NULL, false);
166 if ((asprintf(&filename, "%s/%s", ctx->savedir, p->e.name) < 0) || !filename)
167 UCI_THROW(ctx, UCI_ERR_MEM);
169 uci_load_history_file(ctx, p, filename, &f, flush);
172 ftruncate(fileno(f), 0);
180 static void uci_filter_history(struct uci_context *ctx, const char *name, char *section, char *option)
182 struct uci_parse_context *pctx;
183 struct uci_element *e, *tmp;
184 struct uci_list list;
185 char *filename = NULL;
192 uci_list_init(&list);
193 uci_alloc_parse_context(ctx);
196 if ((asprintf(&filename, "%s/%s", ctx->savedir, name) < 0) || !filename)
197 UCI_THROW(ctx, UCI_ERR_MEM);
199 UCI_TRAP_SAVE(ctx, done);
200 f = uci_open_stream(ctx, filename, SEEK_SET, true, false);
203 struct uci_element *e;
211 /* NB: need to allocate the element before the call to
212 * uci_parse_history_tuple, otherwise the original string
213 * gets modified before it is saved */
214 e = uci_alloc_generic(ctx, UCI_TYPE_HISTORY, pctx->buf, sizeof(struct uci_element));
215 uci_list_add(&list, &e->list);
217 uci_parse_history_tuple(ctx, &buf, &p, &s, &o, &v, NULL, NULL);
219 if (!s || (strcmp(section, s) != 0))
223 if (!o || (strcmp(option, o) != 0))
226 /* match, drop this element again */
230 /* rebuild the history file */
232 ftruncate(fileno(f), 0);
233 uci_foreach_element_safe(&list, tmp, e) {
234 fprintf(f, "%s\n", e->name);
237 UCI_TRAP_RESTORE(ctx);
243 uci_foreach_element_safe(&list, tmp, e) {
246 ctx->internal = true;
250 int uci_revert(struct uci_context *ctx, struct uci_package **pkg, char *section, char *option)
252 struct uci_package *p;
256 UCI_ASSERT(ctx, pkg != NULL);
258 UCI_ASSERT(ctx, p != NULL);
259 UCI_ASSERT(ctx, p->confdir);
262 * - flush unwritten changes
263 * - save the package name
264 * - unload the package
265 * - filter the history
266 * - reload the package
268 UCI_TRAP_SAVE(ctx, error);
269 UCI_INTERNAL(uci_save, ctx, p);
270 name = uci_strdup(ctx, p->e.name);
273 uci_free_package(&p);
274 uci_filter_history(ctx, name, section, option);
276 UCI_INTERNAL(uci_load, ctx, name, &p);
277 UCI_TRAP_RESTORE(ctx);
283 UCI_THROW(ctx, ctx->errno);
288 int uci_save(struct uci_context *ctx, struct uci_package *p)
291 char *filename = NULL;
292 struct uci_element *e, *tmp;
295 UCI_ASSERT(ctx, p != NULL);
298 * if the config file was outside of the /etc/config path,
299 * don't save the history to a file, update the real file
301 * does not modify the uci_package pointer
304 return uci_commit(ctx, &p, false);
306 if (uci_list_empty(&p->history))
309 if ((asprintf(&filename, "%s/%s", ctx->savedir, p->e.name) < 0) || !filename)
310 UCI_THROW(ctx, UCI_ERR_MEM);
313 UCI_TRAP_SAVE(ctx, done);
314 f = uci_open_stream(ctx, filename, SEEK_END, true, true);
315 UCI_TRAP_RESTORE(ctx);
317 uci_foreach_element_safe(&p->history, tmp, e) {
318 struct uci_history *h = uci_to_history(e);
320 if (h->cmd == UCI_CMD_REMOVE)
322 else if (h->cmd == UCI_CMD_RENAME)
325 fprintf(f, "%s.%s", p->e.name, h->section);
327 fprintf(f, ".%s", e->name);
329 if (h->cmd == UCI_CMD_REMOVE)
332 fprintf(f, "=%s\n", h->value);
341 UCI_THROW(ctx, ctx->errno);