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 int uci_parse_history_tuple(struct uci_context *ctx, char **buf, struct uci_ptr *ptr)
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)
114 UCI_INTERNAL(uci_parse_ptr, ctx, ptr, *buf);
118 if (ptr->flags & UCI_LOOKUP_EXTENDED)
123 if (!ptr->value || !uci_validate_name(ptr->value))
126 case UCI_CMD_LIST_ADD:
134 UCI_THROW(ctx, UCI_ERR_INVAL);
138 static void uci_parse_history_line(struct uci_context *ctx, struct uci_package *p, char *buf)
140 struct uci_element *e = NULL;
144 cmd = uci_parse_history_tuple(ctx, &buf, &ptr);
145 if (strcmp(ptr.package, p->e.name) != 0)
148 if (ctx->flags & UCI_FLAG_SAVED_HISTORY)
149 uci_add_history(ctx, &p->saved_history, cmd, ptr.section, ptr.option, ptr.value);
153 UCI_INTERNAL(uci_rename, ctx, &ptr);
156 UCI_INTERNAL(uci_delete, ctx, &ptr);
158 case UCI_CMD_LIST_ADD:
159 UCI_INTERNAL(uci_add_list, ctx, &ptr);
163 UCI_INTERNAL(uci_set, ctx, &ptr);
165 if (!ptr.option && e && (cmd == UCI_CMD_ADD))
166 uci_to_section(e)->anonymous = true;
171 UCI_THROW(ctx, UCI_ERR_PARSE);
174 /* returns the number of changes that were successfully parsed */
175 static int uci_parse_history(struct uci_context *ctx, FILE *stream, struct uci_package *p)
177 struct uci_parse_context *pctx;
180 /* make sure no memory from previous parse attempts is leaked */
183 pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
187 while (!feof(pctx->file)) {
193 * ignore parse errors in single lines, we want to preserve as much
194 * history as possible
196 UCI_TRAP_SAVE(ctx, error);
197 uci_parse_history_line(ctx, p, pctx->buf);
198 UCI_TRAP_RESTORE(ctx);
204 /* no error happened, we can get rid of the parser context now */
209 /* returns the number of changes that were successfully parsed */
210 static int uci_load_history_file(struct uci_context *ctx, struct uci_package *p, char *filename, FILE **f, bool flush)
215 UCI_TRAP_SAVE(ctx, done);
216 stream = uci_open_stream(ctx, filename, SEEK_SET, flush, false);
218 changes = uci_parse_history(ctx, stream, p);
219 UCI_TRAP_RESTORE(ctx);
224 uci_close_stream(stream);
228 /* returns the number of changes that were successfully parsed */
229 static int uci_load_history(struct uci_context *ctx, struct uci_package *p, bool flush)
231 struct uci_element *e;
232 char *filename = NULL;
239 uci_foreach_element(&ctx->history_path, e) {
240 if ((asprintf(&filename, "%s/%s", e->name, p->e.name) < 0) || !filename)
241 UCI_THROW(ctx, UCI_ERR_MEM);
243 uci_load_history_file(ctx, p, filename, NULL, false);
247 if ((asprintf(&filename, "%s/%s", ctx->savedir, p->e.name) < 0) || !filename)
248 UCI_THROW(ctx, UCI_ERR_MEM);
250 changes = uci_load_history_file(ctx, p, filename, &f, flush);
251 if (flush && f && (changes > 0)) {
253 ftruncate(fileno(f), 0);
262 static void uci_filter_history(struct uci_context *ctx, const char *name, const char *section, const char *option)
264 struct uci_parse_context *pctx;
265 struct uci_element *e, *tmp;
266 struct uci_list list;
267 char *filename = NULL;
271 uci_list_init(&list);
272 uci_alloc_parse_context(ctx);
275 if ((asprintf(&filename, "%s/%s", ctx->savedir, name) < 0) || !filename)
276 UCI_THROW(ctx, UCI_ERR_MEM);
278 UCI_TRAP_SAVE(ctx, done);
279 f = uci_open_stream(ctx, filename, SEEK_SET, true, false);
282 struct uci_element *e;
290 /* NB: need to allocate the element before the call to
291 * uci_parse_history_tuple, otherwise the original string
292 * gets modified before it is saved */
293 e = uci_alloc_generic(ctx, UCI_TYPE_HISTORY, pctx->buf, sizeof(struct uci_element));
294 uci_list_add(&list, &e->list);
296 uci_parse_history_tuple(ctx, &buf, &ptr);
298 if (!ptr.section || (strcmp(section, ptr.section) != 0))
302 if (!ptr.option || (strcmp(option, ptr.option) != 0))
305 /* match, drop this element again */
309 /* rebuild the history file */
311 ftruncate(fileno(f), 0);
312 uci_foreach_element_safe(&list, tmp, e) {
313 fprintf(f, "%s\n", e->name);
316 UCI_TRAP_RESTORE(ctx);
321 uci_close_stream(pctx->file);
322 uci_foreach_element_safe(&list, tmp, e) {
328 int uci_revert(struct uci_context *ctx, struct uci_ptr *ptr)
330 char *package = NULL;
331 char *section = NULL;
335 expand_ptr(ctx, ptr, false);
336 UCI_ASSERT(ctx, ptr->p->has_history);
339 * - flush unwritten changes
340 * - save the package name
341 * - unload the package
342 * - filter the history
343 * - reload the package
345 UCI_TRAP_SAVE(ctx, error);
346 UCI_INTERNAL(uci_save, ctx, ptr->p);
348 /* NB: need to clone package, section and option names,
349 * as they may get freed on uci_free_package() */
350 package = uci_strdup(ctx, ptr->p->e.name);
352 section = uci_strdup(ctx, ptr->section);
354 option = uci_strdup(ctx, ptr->option);
356 uci_free_package(&ptr->p);
357 uci_filter_history(ctx, package, section, option);
359 UCI_INTERNAL(uci_load, ctx, package, &ptr->p);
360 UCI_TRAP_RESTORE(ctx);
371 UCI_THROW(ctx, ctx->err);
375 int uci_save(struct uci_context *ctx, struct uci_package *p)
378 char *filename = NULL;
379 struct uci_element *e, *tmp;
383 UCI_ASSERT(ctx, p != NULL);
386 * if the config file was outside of the /etc/config path,
387 * don't save the history to a file, update the real file
389 * does not modify the uci_package pointer
392 return uci_commit(ctx, &p, false);
394 if (uci_list_empty(&p->history))
397 if (stat(ctx->savedir, &statbuf) < 0)
398 mkdir(ctx->savedir, UCI_DIRMODE);
399 else if ((statbuf.st_mode & S_IFMT) != S_IFDIR)
400 UCI_THROW(ctx, UCI_ERR_IO);
402 if ((asprintf(&filename, "%s/%s", ctx->savedir, p->e.name) < 0) || !filename)
403 UCI_THROW(ctx, UCI_ERR_MEM);
406 UCI_TRAP_SAVE(ctx, done);
407 f = uci_open_stream(ctx, filename, SEEK_END, true, true);
408 UCI_TRAP_RESTORE(ctx);
410 uci_foreach_element_safe(&p->history, tmp, e) {
411 struct uci_history *h = uci_to_history(e);
424 case UCI_CMD_LIST_ADD:
431 fprintf(f, "%s%s.%s", prefix, p->e.name, h->section);
433 fprintf(f, ".%s", e->name);
435 if (h->cmd == UCI_CMD_REMOVE)
438 fprintf(f, "=%s\n", h->value);
447 UCI_THROW(ctx, ctx->err);