implement uci revert
[project/uci.git] / util.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 wrappers to standard functions, which
17 * throw exceptions upon failure.
18 */
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <sys/file.h>
22 #include <stdbool.h>
23 #include <unistd.h>
24 #include <ctype.h>
25 #include <fcntl.h>
26
27 #define LINEBUF 32
28 #define LINEBUF_MAX 4096
29
30 static void *uci_malloc(struct uci_context *ctx, size_t size)
31 {
32 void *ptr;
33
34 ptr = malloc(size);
35 if (!ptr)
36 UCI_THROW(ctx, UCI_ERR_MEM);
37 memset(ptr, 0, size);
38
39 return ptr;
40 }
41
42 static void *uci_realloc(struct uci_context *ctx, void *ptr, size_t size)
43 {
44 ptr = realloc(ptr, size);
45 if (!ptr)
46 UCI_THROW(ctx, UCI_ERR_MEM);
47
48 return ptr;
49 }
50
51 static char *uci_strdup(struct uci_context *ctx, const char *str)
52 {
53 char *ptr;
54
55 ptr = strdup(str);
56 if (!ptr)
57 UCI_THROW(ctx, UCI_ERR_MEM);
58
59 return ptr;
60 }
61
62 static bool uci_validate_name(const char *str)
63 {
64 if (!*str)
65 return false;
66
67 while (*str) {
68 if (!isalnum(*str) && (*str != '_'))
69 return false;
70 str++;
71 }
72 return true;
73 }
74
75 static void uci_alloc_parse_context(struct uci_context *ctx)
76 {
77 ctx->pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
78 }
79
80 int uci_parse_tuple(struct uci_context *ctx, char *str, char **package, char **section, char **option, char **value)
81 {
82 char *last = NULL;
83
84 UCI_HANDLE_ERR(ctx);
85 UCI_ASSERT(ctx, str && package && section && option);
86
87 *package = strtok(str, ".");
88 if (!*package || !uci_validate_name(*package))
89 goto error;
90
91 last = *package;
92 *section = strtok(NULL, ".");
93 if (!*section)
94 goto lastval;
95
96 last = *section;
97 *option = strtok(NULL, ".");
98 if (!*option)
99 goto lastval;
100
101 last = *option;
102
103 lastval:
104 last = strchr(last, '=');
105 if (last) {
106 if (!value)
107 goto error;
108
109 *last = 0;
110 last++;
111 if (!*last)
112 goto error;
113 *value = last;
114 }
115
116 if (*section && !uci_validate_name(*section))
117 goto error;
118 if (*option && !uci_validate_name(*option))
119 goto error;
120
121 goto done;
122
123 error:
124 UCI_THROW(ctx, UCI_ERR_PARSE);
125
126 done:
127 return 0;
128 }
129
130
131 static void uci_parse_error(struct uci_context *ctx, char *pos, char *reason)
132 {
133 struct uci_parse_context *pctx = ctx->pctx;
134
135 pctx->reason = reason;
136 pctx->byte = pos - pctx->buf;
137 UCI_THROW(ctx, UCI_ERR_PARSE);
138 }
139
140
141 /*
142 * Fetch a new line from the input stream and resize buffer if necessary
143 */
144 static void uci_getln(struct uci_context *ctx, int offset)
145 {
146 struct uci_parse_context *pctx = ctx->pctx;
147 char *p;
148 int ofs;
149
150 if (pctx->buf == NULL) {
151 pctx->buf = uci_malloc(ctx, LINEBUF);
152 pctx->bufsz = LINEBUF;
153 }
154
155 ofs = offset;
156 do {
157 p = &pctx->buf[ofs];
158 p[ofs] = 0;
159
160 p = fgets(p, pctx->bufsz - ofs, pctx->file);
161 if (!p || !*p)
162 return;
163
164 ofs += strlen(p);
165 if (pctx->buf[ofs - 1] == '\n') {
166 pctx->line++;
167 pctx->buf[ofs - 1] = 0;
168 return;
169 }
170
171 if (pctx->bufsz > LINEBUF_MAX/2)
172 uci_parse_error(ctx, p, "line too long");
173
174 pctx->bufsz *= 2;
175 pctx->buf = uci_realloc(ctx, pctx->buf, pctx->bufsz);
176 } while (1);
177 }
178
179 /*
180 * open a stream and go to the right position
181 *
182 * note: when opening for write and seeking to the beginning of
183 * the stream, truncate the file
184 */
185 static FILE *uci_open_stream(struct uci_context *ctx, const char *filename, int pos, bool write, bool create)
186 {
187 struct stat statbuf;
188 FILE *file = NULL;
189 int fd, ret;
190 int mode = (write ? O_RDWR : O_RDONLY);
191
192 if (create)
193 mode |= O_CREAT;
194
195 if (!write && ((stat(filename, &statbuf) < 0) ||
196 ((statbuf.st_mode & S_IFMT) != S_IFREG))) {
197 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
198 }
199
200 fd = open(filename, mode, UCI_FILEMODE);
201 if (fd <= 0)
202 goto error;
203
204 if (flock(fd, (write ? LOCK_EX : LOCK_SH)) < 0)
205 goto error;
206
207 ret = lseek(fd, 0, pos);
208
209 if (ret < 0)
210 goto error;
211
212 file = fdopen(fd, (write ? "w+" : "r"));
213 if (file)
214 goto done;
215
216 error:
217 UCI_THROW(ctx, UCI_ERR_IO);
218 done:
219 return file;
220 }
221
222 static void uci_close_stream(FILE *stream)
223 {
224 int fd;
225
226 if (!stream)
227 return;
228
229 fd = fileno(stream);
230 flock(fd, LOCK_UN);
231 fclose(stream);
232 }
233
234