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