5b652b92f63ffc00e5f9b20ebaae24879674ae01
[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 int uci_set_savedir(struct uci_context *ctx, const char *dir)
30 {
31         char *sdir;
32
33         UCI_HANDLE_ERR(ctx);
34         UCI_ASSERT(ctx, dir != NULL);
35
36         sdir = uci_strdup(ctx, dir);
37         if (ctx->savedir != uci_savedir)
38                 free(ctx->savedir);
39         ctx->savedir = sdir;
40         return 0;
41 }
42
43 int uci_add_history_path(struct uci_context *ctx, const char *dir)
44 {
45         struct uci_element *e;
46
47         UCI_HANDLE_ERR(ctx);
48         UCI_ASSERT(ctx, dir != NULL);
49         e = uci_alloc_generic(ctx, UCI_TYPE_PATH, dir, sizeof(struct uci_element));
50         uci_list_add(&ctx->history_path, &e->list);
51
52         return 0;
53 }
54
55 static void uci_parse_history_line(struct uci_context *ctx, struct uci_package *p, char *buf)
56 {
57         bool delete = false;
58         bool rename = false;
59         char *package = NULL;
60         char *section = NULL;
61         char *option = NULL;
62         char *value = NULL;
63
64         if (buf[0] == '-') {
65                 delete = true;
66                 buf++;
67         } else if (buf[0] == '@') {
68                 rename = true;
69                 buf++;
70         }
71
72         UCI_INTERNAL(uci_parse_tuple, ctx, buf, &package, &section, &option, &value);
73         if (!package || (strcmp(package, p->e.name) != 0))
74                 goto error;
75         if (!uci_validate_name(section))
76                 goto error;
77         if (option && !uci_validate_name(option))
78                 goto error;
79         if ((rename || !delete) && !uci_validate_name(value))
80                 goto error;
81
82         if (rename)
83                 UCI_INTERNAL(uci_rename, ctx, p, section, option, value);
84         else if (delete)
85                 UCI_INTERNAL(uci_delete, ctx, p, section, option);
86         else
87                 UCI_INTERNAL(uci_set, ctx, p, section, option, value);
88
89         return;
90 error:
91         UCI_THROW(ctx, UCI_ERR_PARSE);
92 }
93
94 static void uci_parse_history(struct uci_context *ctx, FILE *stream, struct uci_package *p)
95 {
96         struct uci_parse_context *pctx;
97
98         /* make sure no memory from previous parse attempts is leaked */
99         ctx->internal = true;
100         uci_cleanup(ctx);
101
102         pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
103         ctx->pctx = pctx;
104         pctx->file = stream;
105
106         while (!feof(pctx->file)) {
107                 uci_getln(ctx, 0);
108                 if (!pctx->buf[0])
109                         continue;
110
111                 /*
112                  * ignore parse errors in single lines, we want to preserve as much
113                  * history as possible
114                  */
115                 UCI_TRAP_SAVE(ctx, error);
116                 uci_parse_history_line(ctx, p, pctx->buf);
117                 UCI_TRAP_RESTORE(ctx);
118 error:
119                 continue;
120         }
121
122         /* no error happened, we can get rid of the parser context now */
123         ctx->internal = true;
124         uci_cleanup(ctx);
125 }
126
127 static void uci_load_history_file(struct uci_context *ctx, struct uci_package *p, char *filename, FILE **f, bool flush)
128 {
129         FILE *stream = NULL;
130
131         UCI_TRAP_SAVE(ctx, done);
132         stream = uci_open_stream(ctx, filename, SEEK_SET, flush, false);
133         if (p)
134                 uci_parse_history(ctx, stream, p);
135         UCI_TRAP_RESTORE(ctx);
136 done:
137         if (f)
138                 *f = stream;
139         else if (stream)
140                 uci_close_stream(stream);
141 }
142
143 static void uci_load_history(struct uci_context *ctx, struct uci_package *p, bool flush)
144 {
145         struct uci_element *e;
146         char *filename = NULL;
147         FILE *f = NULL;
148
149         if (!p->confdir)
150                 return;
151
152         uci_foreach_element(&ctx->history_path, e) {
153                 if ((asprintf(&filename, "%s/%s", e->name, p->e.name) < 0) || !filename)
154                         UCI_THROW(ctx, UCI_ERR_MEM);
155
156                 uci_load_history_file(ctx, p, filename, NULL, false);
157                 free(filename);
158         }
159
160         if ((asprintf(&filename, "%s/%s", ctx->savedir, p->e.name) < 0) || !filename)
161                 UCI_THROW(ctx, UCI_ERR_MEM);
162
163         uci_load_history_file(ctx, p, filename, &f, flush);
164         if (flush && f) {
165                 rewind(f);
166                 ftruncate(fileno(f), 0);
167         }
168         if (filename)
169                 free(filename);
170         uci_close_stream(f);
171         ctx->errno = 0;
172 }
173
174 int uci_save(struct uci_context *ctx, struct uci_package *p)
175 {
176         FILE *f = NULL;
177         char *filename = NULL;
178         struct uci_element *e, *tmp;
179
180         UCI_HANDLE_ERR(ctx);
181         UCI_ASSERT(ctx, p != NULL);
182
183         /* 
184          * if the config file was outside of the /etc/config path,
185          * don't save the history to a file, update the real file
186          * directly.
187          * does not modify the uci_package pointer
188          */
189         if (!p->confdir)
190                 return uci_commit(ctx, &p, false);
191
192         if (uci_list_empty(&p->history))
193                 return 0;
194
195         if ((asprintf(&filename, "%s/%s", ctx->savedir, p->e.name) < 0) || !filename)
196                 UCI_THROW(ctx, UCI_ERR_MEM);
197
198         ctx->errno = 0;
199         UCI_TRAP_SAVE(ctx, done);
200         f = uci_open_stream(ctx, filename, SEEK_END, true, true);
201         UCI_TRAP_RESTORE(ctx);
202
203         uci_foreach_element_safe(&p->history, tmp, e) {
204                 struct uci_history *h = uci_to_history(e);
205
206                 if (h->cmd == UCI_CMD_REMOVE)
207                         fprintf(f, "-");
208                 else if (h->cmd == UCI_CMD_RENAME)
209                         fprintf(f, "@");
210
211                 fprintf(f, "%s.%s", p->e.name, h->section);
212                 if (e->name)
213                         fprintf(f, ".%s", e->name);
214
215                 if (h->cmd == UCI_CMD_REMOVE)
216                         fprintf(f, "\n");
217                 else
218                         fprintf(f, "=%s\n", h->value);
219                 uci_free_history(h);
220         }
221
222 done:
223         uci_close_stream(f);
224         if (filename)
225                 free(filename);
226         if (ctx->errno)
227                 UCI_THROW(ctx, ctx->errno);
228
229         return 0;
230 }
231
232