fix compilation on later glibc/gcc versions with stricter checks
[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, &ptr);
164 e = ptr.last;
165 if (!ptr.option && e && (cmd == UCI_CMD_ADD))
166 uci_to_section(e)->anonymous = true;
167 break;
168 }
169 return;
170 error:
171 UCI_THROW(ctx, UCI_ERR_PARSE);
172 }
173
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)
176 {
177 struct uci_parse_context *pctx;
178 int changes = 0;
179
180 /* make sure no memory from previous parse attempts is leaked */
181 uci_cleanup(ctx);
182
183 pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
184 ctx->pctx = pctx;
185 pctx->file = stream;
186
187 while (!feof(pctx->file)) {
188 uci_getln(ctx, 0);
189 if (!pctx->buf[0])
190 continue;
191
192 /*
193 * ignore parse errors in single lines, we want to preserve as much
194 * history as possible
195 */
196 UCI_TRAP_SAVE(ctx, error);
197 uci_parse_history_line(ctx, p, pctx->buf);
198 UCI_TRAP_RESTORE(ctx);
199 changes++;
200 error:
201 continue;
202 }
203
204 /* no error happened, we can get rid of the parser context now */
205 uci_cleanup(ctx);
206 return changes;
207 }
208
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)
211 {
212 FILE *stream = NULL;
213 int changes = 0;
214
215 UCI_TRAP_SAVE(ctx, done);
216 stream = uci_open_stream(ctx, filename, SEEK_SET, flush, false);
217 if (p)
218 changes = uci_parse_history(ctx, stream, p);
219 UCI_TRAP_RESTORE(ctx);
220 done:
221 if (f)
222 *f = stream;
223 else if (stream)
224 uci_close_stream(stream);
225 return changes;
226 }
227
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)
230 {
231 struct uci_element *e;
232 char *filename = NULL;
233 FILE *f = NULL;
234 int changes = 0;
235
236 if (!p->has_history)
237 return 0;
238
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);
242
243 uci_load_history_file(ctx, p, filename, NULL, false);
244 free(filename);
245 }
246
247 if ((asprintf(&filename, "%s/%s", ctx->savedir, p->e.name) < 0) || !filename)
248 UCI_THROW(ctx, UCI_ERR_MEM);
249
250 changes = uci_load_history_file(ctx, p, filename, &f, flush);
251 if (flush && f && (changes > 0)) {
252 rewind(f);
253 if (ftruncate(fileno(f), 0) < 0) {
254 uci_close_stream(f);
255 UCI_THROW(ctx, UCI_ERR_IO);
256 }
257 }
258 if (filename)
259 free(filename);
260 uci_close_stream(f);
261 ctx->err = 0;
262 return changes;
263 }
264
265 static void uci_filter_history(struct uci_context *ctx, const char *name, const char *section, const char *option)
266 {
267 struct uci_parse_context *pctx;
268 struct uci_element *e, *tmp;
269 struct uci_list list;
270 char *filename = NULL;
271 struct uci_ptr ptr;
272 FILE *f = NULL;
273
274 uci_list_init(&list);
275 uci_alloc_parse_context(ctx);
276 pctx = ctx->pctx;
277
278 if ((asprintf(&filename, "%s/%s", ctx->savedir, name) < 0) || !filename)
279 UCI_THROW(ctx, UCI_ERR_MEM);
280
281 UCI_TRAP_SAVE(ctx, done);
282 f = uci_open_stream(ctx, filename, SEEK_SET, true, false);
283 pctx->file = f;
284 while (!feof(f)) {
285 struct uci_element *e;
286 char *buf;
287
288 uci_getln(ctx, 0);
289 buf = pctx->buf;
290 if (!buf[0])
291 continue;
292
293 /* NB: need to allocate the element before the call to
294 * uci_parse_history_tuple, otherwise the original string
295 * gets modified before it is saved */
296 e = uci_alloc_generic(ctx, UCI_TYPE_HISTORY, pctx->buf, sizeof(struct uci_element));
297 uci_list_add(&list, &e->list);
298
299 uci_parse_history_tuple(ctx, &buf, &ptr);
300 if (section) {
301 if (!ptr.section || (strcmp(section, ptr.section) != 0))
302 continue;
303 }
304 if (option) {
305 if (!ptr.option || (strcmp(option, ptr.option) != 0))
306 continue;
307 }
308 /* match, drop this element again */
309 uci_free_element(e);
310 }
311
312 /* rebuild the history file */
313 rewind(f);
314 if (ftruncate(fileno(f), 0) < 0)
315 UCI_THROW(ctx, UCI_ERR_IO);
316 uci_foreach_element_safe(&list, tmp, e) {
317 fprintf(f, "%s\n", e->name);
318 uci_free_element(e);
319 }
320 UCI_TRAP_RESTORE(ctx);
321
322 done:
323 if (filename)
324 free(filename);
325 uci_close_stream(pctx->file);
326 uci_foreach_element_safe(&list, tmp, e) {
327 uci_free_element(e);
328 }
329 uci_cleanup(ctx);
330 }
331
332 int uci_revert(struct uci_context *ctx, struct uci_ptr *ptr)
333 {
334 char *package = NULL;
335 char *section = NULL;
336 char *option = NULL;
337
338 UCI_HANDLE_ERR(ctx);
339 expand_ptr(ctx, ptr, false);
340 UCI_ASSERT(ctx, ptr->p->has_history);
341
342 /*
343 * - flush unwritten changes
344 * - save the package name
345 * - unload the package
346 * - filter the history
347 * - reload the package
348 */
349 UCI_TRAP_SAVE(ctx, error);
350 UCI_INTERNAL(uci_save, ctx, ptr->p);
351
352 /* NB: need to clone package, section and option names,
353 * as they may get freed on uci_free_package() */
354 package = uci_strdup(ctx, ptr->p->e.name);
355 if (ptr->section)
356 section = uci_strdup(ctx, ptr->section);
357 if (ptr->option)
358 option = uci_strdup(ctx, ptr->option);
359
360 uci_free_package(&ptr->p);
361 uci_filter_history(ctx, package, section, option);
362
363 UCI_INTERNAL(uci_load, ctx, package, &ptr->p);
364 UCI_TRAP_RESTORE(ctx);
365 ctx->err = 0;
366
367 error:
368 if (package)
369 free(package);
370 if (section)
371 free(section);
372 if (option)
373 free(option);
374 if (ctx->err)
375 UCI_THROW(ctx, ctx->err);
376 return 0;
377 }
378
379 int uci_save(struct uci_context *ctx, struct uci_package *p)
380 {
381 FILE *f = NULL;
382 char *filename = NULL;
383 struct uci_element *e, *tmp;
384 struct stat statbuf;
385
386 UCI_HANDLE_ERR(ctx);
387 UCI_ASSERT(ctx, p != NULL);
388
389 /*
390 * if the config file was outside of the /etc/config path,
391 * don't save the history to a file, update the real file
392 * directly.
393 * does not modify the uci_package pointer
394 */
395 if (!p->has_history)
396 return uci_commit(ctx, &p, false);
397
398 if (uci_list_empty(&p->history))
399 return 0;
400
401 if (stat(ctx->savedir, &statbuf) < 0)
402 mkdir(ctx->savedir, UCI_DIRMODE);
403 else if ((statbuf.st_mode & S_IFMT) != S_IFDIR)
404 UCI_THROW(ctx, UCI_ERR_IO);
405
406 if ((asprintf(&filename, "%s/%s", ctx->savedir, p->e.name) < 0) || !filename)
407 UCI_THROW(ctx, UCI_ERR_MEM);
408
409 ctx->err = 0;
410 UCI_TRAP_SAVE(ctx, done);
411 f = uci_open_stream(ctx, filename, SEEK_END, true, true);
412 UCI_TRAP_RESTORE(ctx);
413
414 uci_foreach_element_safe(&p->history, tmp, e) {
415 struct uci_history *h = uci_to_history(e);
416 char *prefix = "";
417
418 switch(h->cmd) {
419 case UCI_CMD_REMOVE:
420 prefix = "-";
421 break;
422 case UCI_CMD_RENAME:
423 prefix = "@";
424 break;
425 case UCI_CMD_ADD:
426 prefix = "+";
427 break;
428 case UCI_CMD_LIST_ADD:
429 prefix = "|";
430 break;
431 default:
432 break;
433 }
434
435 fprintf(f, "%s%s.%s", prefix, p->e.name, h->section);
436 if (e->name)
437 fprintf(f, ".%s", e->name);
438
439 if (h->cmd == UCI_CMD_REMOVE)
440 fprintf(f, "\n");
441 else
442 fprintf(f, "=%s\n", h->value);
443 uci_free_history(h);
444 }
445
446 done:
447 uci_close_stream(f);
448 if (filename)
449 free(filename);
450 if (ctx->err)
451 UCI_THROW(ctx, ctx->err);
452
453 return 0;
454 }
455
456