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