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