ae66f66c0f60ff8bcc4e54a283e83b48793ce252
[project/uci.git] / history.c
1 /*
2 * libuci - Library for the Unified Configuration Interface
3 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
4 *
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
8 *
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.
13 */
14
15 /*
16 * This file contains the code for handling uci config history files
17 */
18
19 #define _GNU_SOURCE
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <sys/file.h>
23 #include <stdbool.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <stdio.h>
27 #include <ctype.h>
28
29 /* record a change that was done to a package */
30 void
31 uci_add_history(struct uci_context *ctx, struct uci_list *list, int cmd, const char *section, const char *option, const char *value)
32 {
33 struct uci_history *h;
34 int size = strlen(section) + 1;
35 char *ptr;
36
37 if (value)
38 size += strlen(value) + 1;
39
40 h = uci_alloc_element(ctx, history, option, size);
41 ptr = uci_dataptr(h);
42 h->cmd = cmd;
43 h->section = strcpy(ptr, section);
44 if (value) {
45 ptr += strlen(ptr) + 1;
46 h->value = strcpy(ptr, value);
47 }
48 uci_list_add(list, &h->e.list);
49 }
50
51 void
52 uci_free_history(struct uci_history *h)
53 {
54 if (!h)
55 return;
56 if ((h->section != NULL) &&
57 (h->section != uci_dataptr(h))) {
58 free(h->section);
59 free(h->value);
60 }
61 uci_free_element(&h->e);
62 }
63
64
65 int uci_set_savedir(struct uci_context *ctx, const char *dir)
66 {
67 char *sdir;
68
69 UCI_HANDLE_ERR(ctx);
70 UCI_ASSERT(ctx, dir != NULL);
71
72 sdir = uci_strdup(ctx, dir);
73 if (ctx->savedir != uci_savedir)
74 free(ctx->savedir);
75 ctx->savedir = sdir;
76 return 0;
77 }
78
79 int uci_add_history_path(struct uci_context *ctx, const char *dir)
80 {
81 struct uci_element *e;
82
83 UCI_HANDLE_ERR(ctx);
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);
87
88 return 0;
89 }
90
91 static inline int uci_parse_history_tuple(struct uci_context *ctx, char **buf, struct uci_ptr *ptr)
92 {
93 int c = UCI_CMD_CHANGE;
94
95 switch(**buf) {
96 case '-':
97 c = UCI_CMD_REMOVE;
98 break;
99 case '@':
100 c = UCI_CMD_RENAME;
101 break;
102 case '+':
103 /* UCI_CMD_ADD is used for anonymous sections or list values */
104 c = UCI_CMD_ADD;
105 break;
106 case '|':
107 c = UCI_CMD_LIST_ADD;
108 break;
109 }
110
111 if (c != UCI_CMD_CHANGE)
112 *buf += 1;
113
114 UCI_INTERNAL(uci_parse_ptr, ctx, ptr, *buf);
115
116 if (!ptr->section)
117 goto error;
118 if (ptr->flags & UCI_LOOKUP_EXTENDED)
119 goto error;
120
121 switch(c) {
122 case UCI_CMD_RENAME:
123 if (!ptr->value || !uci_validate_name(ptr->value))
124 goto error;
125 break;
126 case UCI_CMD_LIST_ADD:
127 if (!ptr->option)
128 goto error;
129 }
130
131 return c;
132
133 error:
134 UCI_THROW(ctx, UCI_ERR_INVAL);
135 return 0;
136 }
137
138 static void uci_parse_history_line(struct uci_context *ctx, struct uci_package *p, char *buf)
139 {
140 struct uci_element *e = NULL;
141 struct uci_ptr ptr;
142 int cmd;
143
144 cmd = uci_parse_history_tuple(ctx, &buf, &ptr);
145 if (strcmp(ptr.package, p->e.name) != 0)
146 goto error;
147
148 if (ctx->flags & UCI_FLAG_SAVED_HISTORY)
149 uci_add_history(ctx, &p->saved_history, cmd, ptr.section, ptr.option, ptr.value);
150
151 switch(cmd) {
152 case UCI_CMD_RENAME:
153 UCI_INTERNAL(uci_rename, ctx, &ptr);
154 break;
155 case UCI_CMD_REMOVE:
156 UCI_INTERNAL(uci_delete, ctx, &ptr);
157 break;
158 case UCI_CMD_LIST_ADD:
159 UCI_INTERNAL(uci_add_list, ctx, &ptr);
160 break;
161 case UCI_CMD_ADD:
162 case UCI_CMD_CHANGE:
163 UCI_INTERNAL(uci_set, ctx, p, ptr.section, ptr.option, ptr.value, &e);
164 if (!ptr.option && e && (cmd == UCI_CMD_ADD))
165 uci_to_section(e)->anonymous = true;
166 break;
167 }
168 return;
169 error:
170 UCI_THROW(ctx, UCI_ERR_PARSE);
171 }
172
173 /* returns the number of changes that were successfully parsed */
174 static int uci_parse_history(struct uci_context *ctx, FILE *stream, struct uci_package *p)
175 {
176 struct uci_parse_context *pctx;
177 int changes = 0;
178
179 /* make sure no memory from previous parse attempts is leaked */
180 uci_cleanup(ctx);
181
182 pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
183 ctx->pctx = pctx;
184 pctx->file = stream;
185
186 while (!feof(pctx->file)) {
187 uci_getln(ctx, 0);
188 if (!pctx->buf[0])
189 continue;
190
191 /*
192 * ignore parse errors in single lines, we want to preserve as much
193 * history as possible
194 */
195 UCI_TRAP_SAVE(ctx, error);
196 uci_parse_history_line(ctx, p, pctx->buf);
197 UCI_TRAP_RESTORE(ctx);
198 changes++;
199 error:
200 continue;
201 }
202
203 /* no error happened, we can get rid of the parser context now */
204 uci_cleanup(ctx);
205 return changes;
206 }
207
208 /* returns the number of changes that were successfully parsed */
209 static int uci_load_history_file(struct uci_context *ctx, struct uci_package *p, char *filename, FILE **f, bool flush)
210 {
211 FILE *stream = NULL;
212 int changes = 0;
213
214 UCI_TRAP_SAVE(ctx, done);
215 stream = uci_open_stream(ctx, filename, SEEK_SET, flush, false);
216 if (p)
217 changes = uci_parse_history(ctx, stream, p);
218 UCI_TRAP_RESTORE(ctx);
219 done:
220 if (f)
221 *f = stream;
222 else if (stream)
223 uci_close_stream(stream);
224 return changes;
225 }
226
227 /* returns the number of changes that were successfully parsed */
228 static int uci_load_history(struct uci_context *ctx, struct uci_package *p, bool flush)
229 {
230 struct uci_element *e;
231 char *filename = NULL;
232 FILE *f = NULL;
233 int changes = 0;
234
235 if (!p->has_history)
236 return 0;
237
238 uci_foreach_element(&ctx->history_path, e) {
239 if ((asprintf(&filename, "%s/%s", e->name, p->e.name) < 0) || !filename)
240 UCI_THROW(ctx, UCI_ERR_MEM);
241
242 uci_load_history_file(ctx, p, filename, NULL, false);
243 free(filename);
244 }
245
246 if ((asprintf(&filename, "%s/%s", ctx->savedir, p->e.name) < 0) || !filename)
247 UCI_THROW(ctx, UCI_ERR_MEM);
248
249 changes = uci_load_history_file(ctx, p, filename, &f, flush);
250 if (flush && f && (changes > 0)) {
251 rewind(f);
252 ftruncate(fileno(f), 0);
253 }
254 if (filename)
255 free(filename);
256 uci_close_stream(f);
257 ctx->err = 0;
258 return changes;
259 }
260
261 static void uci_filter_history(struct uci_context *ctx, const char *name, const char *section, const char *option)
262 {
263 struct uci_parse_context *pctx;
264 struct uci_element *e, *tmp;
265 struct uci_list list;
266 char *filename = NULL;
267 struct uci_ptr ptr;
268 FILE *f = NULL;
269
270 uci_list_init(&list);
271 uci_alloc_parse_context(ctx);
272 pctx = ctx->pctx;
273
274 if ((asprintf(&filename, "%s/%s", ctx->savedir, name) < 0) || !filename)
275 UCI_THROW(ctx, UCI_ERR_MEM);
276
277 UCI_TRAP_SAVE(ctx, done);
278 f = uci_open_stream(ctx, filename, SEEK_SET, true, false);
279 pctx->file = f;
280 while (!feof(f)) {
281 struct uci_element *e;
282 char *buf;
283
284 uci_getln(ctx, 0);
285 buf = pctx->buf;
286 if (!buf[0])
287 continue;
288
289 /* NB: need to allocate the element before the call to
290 * uci_parse_history_tuple, otherwise the original string
291 * gets modified before it is saved */
292 e = uci_alloc_generic(ctx, UCI_TYPE_HISTORY, pctx->buf, sizeof(struct uci_element));
293 uci_list_add(&list, &e->list);
294
295 uci_parse_history_tuple(ctx, &buf, &ptr);
296 if (section) {
297 if (!ptr.section || (strcmp(section, ptr.section) != 0))
298 continue;
299 }
300 if (option) {
301 if (!ptr.option || (strcmp(option, ptr.option) != 0))
302 continue;
303 }
304 /* match, drop this element again */
305 uci_free_element(e);
306 }
307
308 /* rebuild the history file */
309 rewind(f);
310 ftruncate(fileno(f), 0);
311 uci_foreach_element_safe(&list, tmp, e) {
312 fprintf(f, "%s\n", e->name);
313 uci_free_element(e);
314 }
315 UCI_TRAP_RESTORE(ctx);
316
317 done:
318 if (filename)
319 free(filename);
320 uci_close_stream(f);
321 uci_foreach_element_safe(&list, tmp, e) {
322 uci_free_element(e);
323 }
324 uci_cleanup(ctx);
325 }
326
327 int uci_revert(struct uci_context *ctx, struct uci_ptr *ptr)
328 {
329 char *package = NULL;
330 char *section = NULL;
331 char *option = NULL;
332
333 UCI_HANDLE_ERR(ctx);
334 expand_ptr(ctx, ptr, true);
335 UCI_ASSERT(ctx, ptr->p->has_history);
336
337 /*
338 * - flush unwritten changes
339 * - save the package name
340 * - unload the package
341 * - filter the history
342 * - reload the package
343 */
344 UCI_TRAP_SAVE(ctx, error);
345 UCI_INTERNAL(uci_save, ctx, ptr->p);
346
347 /* NB: need to clone package, section and option names,
348 * as they may get freed on uci_free_package() */
349 package = uci_strdup(ctx, ptr->p->e.name);
350 section = uci_strdup(ctx, ptr->section);
351 option = uci_strdup(ctx, ptr->option);
352
353 uci_free_package(&ptr->p);
354 uci_filter_history(ctx, package, section, option);
355
356 UCI_INTERNAL(uci_load, ctx, package, &ptr->p);
357 UCI_TRAP_RESTORE(ctx);
358 ctx->err = 0;
359
360 error:
361 if (package)
362 free(package);
363 if (section)
364 free(section);
365 if (option)
366 free(option);
367 if (ctx->err)
368 UCI_THROW(ctx, ctx->err);
369 return 0;
370 }
371
372 int uci_save(struct uci_context *ctx, struct uci_package *p)
373 {
374 FILE *f = NULL;
375 char *filename = NULL;
376 struct uci_element *e, *tmp;
377 struct stat statbuf;
378
379 UCI_HANDLE_ERR(ctx);
380 UCI_ASSERT(ctx, p != NULL);
381
382 /*
383 * if the config file was outside of the /etc/config path,
384 * don't save the history to a file, update the real file
385 * directly.
386 * does not modify the uci_package pointer
387 */
388 if (!p->has_history)
389 return uci_commit(ctx, &p, false);
390
391 if (uci_list_empty(&p->history))
392 return 0;
393
394 if (stat(ctx->savedir, &statbuf) < 0)
395 mkdir(ctx->savedir, UCI_DIRMODE);
396 else if ((statbuf.st_mode & S_IFMT) != S_IFDIR)
397 UCI_THROW(ctx, UCI_ERR_IO);
398
399 if ((asprintf(&filename, "%s/%s", ctx->savedir, p->e.name) < 0) || !filename)
400 UCI_THROW(ctx, UCI_ERR_MEM);
401
402 ctx->err = 0;
403 UCI_TRAP_SAVE(ctx, done);
404 f = uci_open_stream(ctx, filename, SEEK_END, true, true);
405 UCI_TRAP_RESTORE(ctx);
406
407 uci_foreach_element_safe(&p->history, tmp, e) {
408 struct uci_history *h = uci_to_history(e);
409 char *prefix = "";
410
411 switch(h->cmd) {
412 case UCI_CMD_REMOVE:
413 prefix = "-";
414 break;
415 case UCI_CMD_RENAME:
416 prefix = "@";
417 break;
418 case UCI_CMD_ADD:
419 prefix = "+";
420 break;
421 case UCI_CMD_LIST_ADD:
422 prefix = "|";
423 break;
424 default:
425 break;
426 }
427
428 fprintf(f, "%s%s.%s", prefix, p->e.name, h->section);
429 if (e->name)
430 fprintf(f, ".%s", e->name);
431
432 if (h->cmd == UCI_CMD_REMOVE)
433 fprintf(f, "\n");
434 else
435 fprintf(f, "=%s\n", h->value);
436 uci_free_history(h);
437 }
438
439 done:
440 uci_close_stream(f);
441 if (filename)
442 free(filename);
443 if (ctx->err)
444 UCI_THROW(ctx, ctx->err);
445
446 return 0;
447 }
448
449