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