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 && !uci_validate_str(value, (option || delete)))
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 /* returns the number of changes that were successfully parsed */
101 static int uci_parse_history(struct uci_context *ctx, FILE *stream, struct uci_package *p)
103 struct uci_parse_context *pctx;
106 /* make sure no memory from previous parse attempts is leaked */
107 ctx->internal = true;
110 pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
114 while (!feof(pctx->file)) {
120 * ignore parse errors in single lines, we want to preserve as much
121 * history as possible
123 UCI_TRAP_SAVE(ctx, error);
124 uci_parse_history_line(ctx, p, pctx->buf);
125 UCI_TRAP_RESTORE(ctx);
131 /* no error happened, we can get rid of the parser context now */
132 ctx->internal = true;
137 /* returns the number of changes that were successfully parsed */
138 static int uci_load_history_file(struct uci_context *ctx, struct uci_package *p, char *filename, FILE **f, bool flush)
143 UCI_TRAP_SAVE(ctx, done);
144 stream = uci_open_stream(ctx, filename, SEEK_SET, flush, false);
146 changes = uci_parse_history(ctx, stream, p);
147 UCI_TRAP_RESTORE(ctx);
152 uci_close_stream(stream);
156 /* returns the number of changes that were successfully parsed */
157 static int uci_load_history(struct uci_context *ctx, struct uci_package *p, bool flush)
159 struct uci_element *e;
160 char *filename = NULL;
167 uci_foreach_element(&ctx->history_path, e) {
168 if ((asprintf(&filename, "%s/%s", e->name, p->e.name) < 0) || !filename)
169 UCI_THROW(ctx, UCI_ERR_MEM);
171 uci_load_history_file(ctx, p, filename, NULL, false);
175 if ((asprintf(&filename, "%s/%s", ctx->savedir, p->e.name) < 0) || !filename)
176 UCI_THROW(ctx, UCI_ERR_MEM);
178 changes = uci_load_history_file(ctx, p, filename, &f, flush);
179 if (flush && f && (changes > 0)) {
181 ftruncate(fileno(f), 0);
190 static void uci_filter_history(struct uci_context *ctx, const char *name, char *section, char *option)
192 struct uci_parse_context *pctx;
193 struct uci_element *e, *tmp;
194 struct uci_list list;
195 char *filename = NULL;
202 uci_list_init(&list);
203 uci_alloc_parse_context(ctx);
206 if ((asprintf(&filename, "%s/%s", ctx->savedir, name) < 0) || !filename)
207 UCI_THROW(ctx, UCI_ERR_MEM);
209 UCI_TRAP_SAVE(ctx, done);
210 f = uci_open_stream(ctx, filename, SEEK_SET, true, false);
213 struct uci_element *e;
221 /* NB: need to allocate the element before the call to
222 * uci_parse_history_tuple, otherwise the original string
223 * gets modified before it is saved */
224 e = uci_alloc_generic(ctx, UCI_TYPE_HISTORY, pctx->buf, sizeof(struct uci_element));
225 uci_list_add(&list, &e->list);
227 uci_parse_history_tuple(ctx, &buf, &p, &s, &o, &v, NULL, NULL);
229 if (!s || (strcmp(section, s) != 0))
233 if (!o || (strcmp(option, o) != 0))
236 /* match, drop this element again */
240 /* rebuild the history file */
242 ftruncate(fileno(f), 0);
243 uci_foreach_element_safe(&list, tmp, e) {
244 fprintf(f, "%s\n", e->name);
247 UCI_TRAP_RESTORE(ctx);
253 uci_foreach_element_safe(&list, tmp, e) {
256 ctx->internal = true;
260 int uci_revert(struct uci_context *ctx, struct uci_package **pkg, char *section, char *option)
262 struct uci_package *p;
266 UCI_ASSERT(ctx, pkg != NULL);
268 UCI_ASSERT(ctx, p != NULL);
269 UCI_ASSERT(ctx, p->confdir);
272 * - flush unwritten changes
273 * - save the package name
274 * - unload the package
275 * - filter the history
276 * - reload the package
278 UCI_TRAP_SAVE(ctx, error);
279 UCI_INTERNAL(uci_save, ctx, p);
280 name = uci_strdup(ctx, p->e.name);
283 uci_free_package(&p);
284 uci_filter_history(ctx, name, section, option);
286 UCI_INTERNAL(uci_load, ctx, name, &p);
287 UCI_TRAP_RESTORE(ctx);
294 UCI_THROW(ctx, ctx->errno);
298 int uci_save(struct uci_context *ctx, struct uci_package *p)
301 char *filename = NULL;
302 struct uci_element *e, *tmp;
305 UCI_ASSERT(ctx, p != NULL);
308 * if the config file was outside of the /etc/config path,
309 * don't save the history to a file, update the real file
311 * does not modify the uci_package pointer
314 return uci_commit(ctx, &p, false);
316 if (uci_list_empty(&p->history))
319 if ((asprintf(&filename, "%s/%s", ctx->savedir, p->e.name) < 0) || !filename)
320 UCI_THROW(ctx, UCI_ERR_MEM);
323 UCI_TRAP_SAVE(ctx, done);
324 f = uci_open_stream(ctx, filename, SEEK_END, true, true);
325 UCI_TRAP_RESTORE(ctx);
327 uci_foreach_element_safe(&p->history, tmp, e) {
328 struct uci_history *h = uci_to_history(e);
330 if (h->cmd == UCI_CMD_REMOVE)
332 else if (h->cmd == UCI_CMD_RENAME)
335 fprintf(f, "%s.%s", p->e.name, h->section);
337 fprintf(f, ".%s", e->name);
339 if (h->cmd == UCI_CMD_REMOVE)
342 fprintf(f, "=%s\n", h->value);
351 UCI_THROW(ctx, ctx->errno);