turn ucimap-example.c into a test case
[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_REORDER;
98                 break;
99         case '-':
100                 c = UCI_CMD_REMOVE;
101                 break;
102         case '@':
103                 c = UCI_CMD_RENAME;
104                 break;
105         case '+':
106                 /* UCI_CMD_ADD is used for anonymous sections or list values */
107                 c = UCI_CMD_ADD;
108                 break;
109         case '|':
110                 c = UCI_CMD_LIST_ADD;
111                 break;
112         }
113
114         if (c != UCI_CMD_CHANGE)
115                 *buf += 1;
116
117         UCI_INTERNAL(uci_parse_ptr, ctx, ptr, *buf);
118
119         if (!ptr->section)
120                 goto error;
121         if (ptr->flags & UCI_LOOKUP_EXTENDED)
122                 goto error;
123
124         switch(c) {
125         case UCI_CMD_REORDER:
126                 if (!ptr->value || ptr->option)
127                         goto error;
128                 break;
129         case UCI_CMD_RENAME:
130                 if (!ptr->value || !uci_validate_name(ptr->value))
131                         goto error;
132                 break;
133         case UCI_CMD_LIST_ADD:
134                 if (!ptr->option)
135                         goto error;
136         }
137
138         return c;
139
140 error:
141         UCI_THROW(ctx, UCI_ERR_INVAL);
142         return 0;
143 }
144
145 static void uci_parse_history_line(struct uci_context *ctx, struct uci_package *p, char *buf)
146 {
147         struct uci_element *e = NULL;
148         struct uci_ptr ptr;
149         int cmd;
150
151         cmd = uci_parse_history_tuple(ctx, &buf, &ptr);
152         if (strcmp(ptr.package, p->e.name) != 0)
153                 goto error;
154
155         if (ctx->flags & UCI_FLAG_SAVED_HISTORY)
156                 uci_add_history(ctx, &p->saved_history, cmd, ptr.section, ptr.option, ptr.value);
157
158         switch(cmd) {
159         case UCI_CMD_REORDER:
160                 expand_ptr(ctx, &ptr, true);
161                 if (!ptr.s)
162                         UCI_THROW(ctx, UCI_ERR_NOTFOUND);
163                 UCI_INTERNAL(uci_reorder_section, ctx, ptr.s, strtoul(ptr.value, NULL, 10));
164                 break;
165         case UCI_CMD_RENAME:
166                 UCI_INTERNAL(uci_rename, ctx, &ptr);
167                 break;
168         case UCI_CMD_REMOVE:
169                 UCI_INTERNAL(uci_delete, ctx, &ptr);
170                 break;
171         case UCI_CMD_LIST_ADD:
172                 UCI_INTERNAL(uci_add_list, ctx, &ptr);
173                 break;
174         case UCI_CMD_ADD:
175         case UCI_CMD_CHANGE:
176                 UCI_INTERNAL(uci_set, ctx, &ptr);
177                 e = ptr.last;
178                 if (!ptr.option && e && (cmd == UCI_CMD_ADD))
179                         uci_to_section(e)->anonymous = true;
180                 break;
181         }
182         return;
183 error:
184         UCI_THROW(ctx, UCI_ERR_PARSE);
185 }
186
187 /* returns the number of changes that were successfully parsed */
188 static int uci_parse_history(struct uci_context *ctx, FILE *stream, struct uci_package *p)
189 {
190         struct uci_parse_context *pctx;
191         int changes = 0;
192
193         /* make sure no memory from previous parse attempts is leaked */
194         uci_cleanup(ctx);
195
196         pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
197         ctx->pctx = pctx;
198         pctx->file = stream;
199
200         while (!feof(pctx->file)) {
201                 uci_getln(ctx, 0);
202                 if (!pctx->buf[0])
203                         continue;
204
205                 /*
206                  * ignore parse errors in single lines, we want to preserve as much
207                  * history as possible
208                  */
209                 UCI_TRAP_SAVE(ctx, error);
210                 uci_parse_history_line(ctx, p, pctx->buf);
211                 UCI_TRAP_RESTORE(ctx);
212                 changes++;
213 error:
214                 continue;
215         }
216
217         /* no error happened, we can get rid of the parser context now */
218         uci_cleanup(ctx);
219         return changes;
220 }
221
222 /* returns the number of changes that were successfully parsed */
223 static int uci_load_history_file(struct uci_context *ctx, struct uci_package *p, char *filename, FILE **f, bool flush)
224 {
225         FILE *stream = NULL;
226         int changes = 0;
227
228         UCI_TRAP_SAVE(ctx, done);
229         stream = uci_open_stream(ctx, filename, SEEK_SET, flush, false);
230         if (p)
231                 changes = uci_parse_history(ctx, stream, p);
232         UCI_TRAP_RESTORE(ctx);
233 done:
234         if (f)
235                 *f = stream;
236         else if (stream)
237                 uci_close_stream(stream);
238         return changes;
239 }
240
241 /* returns the number of changes that were successfully parsed */
242 static int uci_load_history(struct uci_context *ctx, struct uci_package *p, bool flush)
243 {
244         struct uci_element *e;
245         char *filename = NULL;
246         FILE *f = NULL;
247         int changes = 0;
248
249         if (!p->has_history)
250                 return 0;
251
252         uci_foreach_element(&ctx->history_path, e) {
253                 if ((asprintf(&filename, "%s/%s", e->name, p->e.name) < 0) || !filename)
254                         UCI_THROW(ctx, UCI_ERR_MEM);
255
256                 uci_load_history_file(ctx, p, filename, NULL, false);
257                 free(filename);
258         }
259
260         if ((asprintf(&filename, "%s/%s", ctx->savedir, p->e.name) < 0) || !filename)
261                 UCI_THROW(ctx, UCI_ERR_MEM);
262
263         changes = uci_load_history_file(ctx, p, filename, &f, flush);
264         if (flush && f && (changes > 0)) {
265                 rewind(f);
266                 if (ftruncate(fileno(f), 0) < 0) {
267                         uci_close_stream(f);
268                         UCI_THROW(ctx, UCI_ERR_IO);
269                 }
270         }
271         if (filename)
272                 free(filename);
273         uci_close_stream(f);
274         ctx->err = 0;
275         return changes;
276 }
277
278 static void uci_filter_history(struct uci_context *ctx, const char *name, const char *section, const char *option)
279 {
280         struct uci_parse_context *pctx;
281         struct uci_element *e, *tmp;
282         struct uci_list list;
283         char *filename = NULL;
284         struct uci_ptr ptr;
285         FILE *f = NULL;
286
287         uci_list_init(&list);
288         uci_alloc_parse_context(ctx);
289         pctx = ctx->pctx;
290
291         if ((asprintf(&filename, "%s/%s", ctx->savedir, name) < 0) || !filename)
292                 UCI_THROW(ctx, UCI_ERR_MEM);
293
294         UCI_TRAP_SAVE(ctx, done);
295         f = uci_open_stream(ctx, filename, SEEK_SET, true, false);
296         pctx->file = f;
297         while (!feof(f)) {
298                 struct uci_element *e;
299                 char *buf;
300
301                 uci_getln(ctx, 0);
302                 buf = pctx->buf;
303                 if (!buf[0])
304                         continue;
305
306                 /* NB: need to allocate the element before the call to 
307                  * uci_parse_history_tuple, otherwise the original string 
308                  * gets modified before it is saved */
309                 e = uci_alloc_generic(ctx, UCI_TYPE_HISTORY, pctx->buf, sizeof(struct uci_element));
310                 uci_list_add(&list, &e->list);
311
312                 uci_parse_history_tuple(ctx, &buf, &ptr);
313                 if (section) {
314                         if (!ptr.section || (strcmp(section, ptr.section) != 0))
315                                 continue;
316                 }
317                 if (option) {
318                         if (!ptr.option || (strcmp(option, ptr.option) != 0))
319                                 continue;
320                 }
321                 /* match, drop this element again */
322                 uci_free_element(e);
323         }
324
325         /* rebuild the history file */
326         rewind(f);
327         if (ftruncate(fileno(f), 0) < 0)
328                 UCI_THROW(ctx, UCI_ERR_IO);
329         uci_foreach_element_safe(&list, tmp, e) {
330                 fprintf(f, "%s\n", e->name);
331                 uci_free_element(e);
332         }
333         UCI_TRAP_RESTORE(ctx);
334
335 done:
336         if (filename)
337                 free(filename);
338         uci_close_stream(pctx->file);
339         uci_foreach_element_safe(&list, tmp, e) {
340                 uci_free_element(e);
341         }
342         uci_cleanup(ctx);
343 }
344
345 int uci_revert(struct uci_context *ctx, struct uci_ptr *ptr)
346 {
347         char *package = NULL;
348         char *section = NULL;
349         char *option = NULL;
350
351         UCI_HANDLE_ERR(ctx);
352         expand_ptr(ctx, ptr, false);
353         UCI_ASSERT(ctx, ptr->p->has_history);
354
355         /* 
356          * - flush unwritten changes
357          * - save the package name
358          * - unload the package
359          * - filter the history
360          * - reload the package
361          */
362         UCI_TRAP_SAVE(ctx, error);
363         UCI_INTERNAL(uci_save, ctx, ptr->p);
364
365         /* NB: need to clone package, section and option names, 
366          * as they may get freed on uci_free_package() */
367         package = uci_strdup(ctx, ptr->p->e.name);
368         if (ptr->section)
369                 section = uci_strdup(ctx, ptr->section);
370         if (ptr->option)
371                 option = uci_strdup(ctx, ptr->option);
372
373         uci_free_package(&ptr->p);
374         uci_filter_history(ctx, package, section, option);
375
376         UCI_INTERNAL(uci_load, ctx, package, &ptr->p);
377         UCI_TRAP_RESTORE(ctx);
378         ctx->err = 0;
379
380 error:
381         if (package)
382                 free(package);
383         if (section)
384                 free(section);
385         if (option)
386                 free(option);
387         if (ctx->err)
388                 UCI_THROW(ctx, ctx->err);
389         return 0;
390 }
391
392 int uci_save(struct uci_context *ctx, struct uci_package *p)
393 {
394         FILE *f = NULL;
395         char *filename = NULL;
396         struct uci_element *e, *tmp;
397         struct stat statbuf;
398
399         UCI_HANDLE_ERR(ctx);
400         UCI_ASSERT(ctx, p != NULL);
401
402         /* 
403          * if the config file was outside of the /etc/config path,
404          * don't save the history to a file, update the real file
405          * directly.
406          * does not modify the uci_package pointer
407          */
408         if (!p->has_history)
409                 return uci_commit(ctx, &p, false);
410
411         if (uci_list_empty(&p->history))
412                 return 0;
413
414         if (stat(ctx->savedir, &statbuf) < 0)
415                 mkdir(ctx->savedir, UCI_DIRMODE);
416         else if ((statbuf.st_mode & S_IFMT) != S_IFDIR)
417                 UCI_THROW(ctx, UCI_ERR_IO);
418
419         if ((asprintf(&filename, "%s/%s", ctx->savedir, p->e.name) < 0) || !filename)
420                 UCI_THROW(ctx, UCI_ERR_MEM);
421
422         uci_foreach_element(&ctx->hooks, tmp) {
423                 struct uci_hook *hook = uci_to_hook(tmp);
424
425                 if (!hook->ops->set)
426                         continue;
427
428                 uci_foreach_element(&p->history, e) {
429                         hook->ops->set(hook->ops, p, uci_to_history(e));
430                 }
431         }
432
433         ctx->err = 0;
434         UCI_TRAP_SAVE(ctx, done);
435         f = uci_open_stream(ctx, filename, SEEK_END, true, true);
436         UCI_TRAP_RESTORE(ctx);
437
438         uci_foreach_element_safe(&p->history, tmp, e) {
439                 struct uci_history *h = uci_to_history(e);
440                 char *prefix = "";
441
442                 switch(h->cmd) {
443                 case UCI_CMD_REMOVE:
444                         prefix = "-";
445                         break;
446                 case UCI_CMD_RENAME:
447                         prefix = "@";
448                         break;
449                 case UCI_CMD_ADD:
450                         prefix = "+";
451                         break;
452                 case UCI_CMD_REORDER:
453                         prefix = "^";
454                         break;
455                 case UCI_CMD_LIST_ADD:
456                         prefix = "|";
457                         break;
458                 default:
459                         break;
460                 }
461
462                 fprintf(f, "%s%s.%s", prefix, p->e.name, h->section);
463                 if (e->name)
464                         fprintf(f, ".%s", e->name);
465
466                 if (h->cmd == UCI_CMD_REMOVE)
467                         fprintf(f, "\n");
468                 else
469                         fprintf(f, "=%s\n", h->value);
470                 uci_free_history(h);
471         }
472
473 done:
474         uci_close_stream(f);
475         if (filename)
476                 free(filename);
477         if (ctx->err)
478                 UCI_THROW(ctx, ctx->err);
479
480         return 0;
481 }
482
483