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 void uci_parse_history_line(struct uci_context
*ctx
, struct uci_package
*p
, char *buf
)
67 } else if (buf
[0] == '@') {
72 UCI_INTERNAL(uci_parse_tuple
, ctx
, buf
, &package
, §ion
, &option
, &value
);
73 if (!package
|| (strcmp(package
, p
->e
.name
) != 0))
75 if (!uci_validate_name(section
))
77 if (option
&& !uci_validate_name(option
))
79 if ((rename
|| !delete) && !uci_validate_name(value
))
83 UCI_INTERNAL(uci_rename
, ctx
, p
, section
, option
, value
);
85 UCI_INTERNAL(uci_delete
, ctx
, p
, section
, option
);
87 UCI_INTERNAL(uci_set
, ctx
, p
, section
, option
, value
);
91 UCI_THROW(ctx
, UCI_ERR_PARSE
);
94 static void uci_parse_history(struct uci_context
*ctx
, FILE *stream
, struct uci_package
*p
)
96 struct uci_parse_context
*pctx
;
98 /* make sure no memory from previous parse attempts is leaked */
102 pctx
= (struct uci_parse_context
*) uci_malloc(ctx
, sizeof(struct uci_parse_context
));
106 while (!feof(pctx
->file
)) {
112 * ignore parse errors in single lines, we want to preserve as much
113 * history as possible
115 UCI_TRAP_SAVE(ctx
, error
);
116 uci_parse_history_line(ctx
, p
, pctx
->buf
);
117 UCI_TRAP_RESTORE(ctx
);
122 /* no error happened, we can get rid of the parser context now */
123 ctx
->internal
= true;
127 static void uci_load_history_file(struct uci_context
*ctx
, struct uci_package
*p
, char *filename
, FILE **f
, bool flush
)
131 UCI_TRAP_SAVE(ctx
, done
);
132 stream
= uci_open_stream(ctx
, filename
, SEEK_SET
, flush
, false);
134 uci_parse_history(ctx
, stream
, p
);
135 UCI_TRAP_RESTORE(ctx
);
140 uci_close_stream(stream
);
143 static void uci_load_history(struct uci_context
*ctx
, struct uci_package
*p
, bool flush
)
145 struct uci_element
*e
;
146 char *filename
= NULL
;
152 uci_foreach_element(&ctx
->history_path
, e
) {
153 if ((asprintf(&filename
, "%s/%s", e
->name
, p
->e
.name
) < 0) || !filename
)
154 UCI_THROW(ctx
, UCI_ERR_MEM
);
156 uci_load_history_file(ctx
, p
, filename
, NULL
, false);
160 if ((asprintf(&filename
, "%s/%s", ctx
->savedir
, p
->e
.name
) < 0) || !filename
)
161 UCI_THROW(ctx
, UCI_ERR_MEM
);
163 uci_load_history_file(ctx
, p
, filename
, &f
, flush
);
166 ftruncate(fileno(f
), 0);
174 int uci_save(struct uci_context
*ctx
, struct uci_package
*p
)
177 char *filename
= NULL
;
178 struct uci_element
*e
, *tmp
;
181 UCI_ASSERT(ctx
, p
!= NULL
);
184 * if the config file was outside of the /etc/config path,
185 * don't save the history to a file, update the real file
187 * does not modify the uci_package pointer
190 return uci_commit(ctx
, &p
, false);
192 if (uci_list_empty(&p
->history
))
195 if ((asprintf(&filename
, "%s/%s", ctx
->savedir
, p
->e
.name
) < 0) || !filename
)
196 UCI_THROW(ctx
, UCI_ERR_MEM
);
199 UCI_TRAP_SAVE(ctx
, done
);
200 f
= uci_open_stream(ctx
, filename
, SEEK_END
, true, true);
201 UCI_TRAP_RESTORE(ctx
);
203 uci_foreach_element_safe(&p
->history
, tmp
, e
) {
204 struct uci_history
*h
= uci_to_history(e
);
206 if (h
->cmd
== UCI_CMD_REMOVE
)
208 else if (h
->cmd
== UCI_CMD_RENAME
)
211 fprintf(f
, "%s.%s", p
->e
.name
, h
->section
);
213 fprintf(f
, ".%s", e
->name
);
215 if (h
->cmd
== UCI_CMD_REMOVE
)
218 fprintf(f
, "=%s\n", h
->value
);
227 UCI_THROW(ctx
, ctx
->errno
);