fold uci_file_cleanup into uci_cleanup
[project/uci.git] / file.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 parsing uci config files
17 */
18
19 #define _GNU_SOURCE
20 #include <sys/types.h>
21 #include <sys/file.h>
22 #include <stdbool.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <ctype.h>
27
28 /*
29 * Clean up all extra memory used by the parser and exporter
30 */
31 static void uci_file_cleanup(struct uci_context *ctx)
32 {
33 }
34
35
36 /*
37 * verify that the end of the line or command is reached.
38 * throw an error if extra arguments are given on the command line
39 */
40 static void assert_eol(struct uci_context *ctx, char **str)
41 {
42 char *tmp;
43
44 tmp = next_arg(ctx, str, false, false);
45 if (*tmp && (ctx->flags & UCI_FLAG_STRICT))
46 uci_parse_error(ctx, *str, "too many arguments");
47 }
48
49 /*
50 * switch to a different config, either triggered by uci_load, or by a
51 * 'package <...>' statement in the import file
52 */
53 static void uci_switch_config(struct uci_context *ctx)
54 {
55 struct uci_parse_context *pctx;
56 struct uci_element *e;
57 const char *name;
58
59 pctx = ctx->pctx;
60 name = pctx->name;
61
62 /* add the last config to main config file list */
63 if (pctx->package) {
64 uci_list_add(&ctx->root, &pctx->package->e.list);
65
66 pctx->package = NULL;
67 pctx->section = NULL;
68 }
69
70 if (!name)
71 return;
72
73 /*
74 * if an older config under the same name exists, unload it
75 * ignore errors here, e.g. if the config was not found
76 */
77 e = uci_lookup_list(ctx, &ctx->root, name);
78 if (e)
79 UCI_THROW(ctx, UCI_ERR_DUPLICATE);
80 pctx->package = uci_alloc_package(ctx, name);
81 }
82
83 /*
84 * parse the 'package' uci command (next config package)
85 */
86 static void uci_parse_package(struct uci_context *ctx, char **str, bool single)
87 {
88 char *name = NULL;
89
90 /* command string null-terminated by strtok */
91 *str += strlen(*str) + 1;
92
93 name = next_arg(ctx, str, true, true);
94 assert_eol(ctx, str);
95 if (single)
96 return;
97
98 ctx->pctx->name = name;
99 uci_switch_config(ctx);
100 }
101
102 /*
103 * parse the 'config' uci command (open a section)
104 */
105 static void uci_parse_config(struct uci_context *ctx, char **str)
106 {
107 struct uci_parse_context *pctx = ctx->pctx;
108 char *name = NULL;
109 char *type = NULL;
110
111 uci_fixup_section(ctx, ctx->pctx->section);
112 if (!ctx->pctx->package) {
113 if (!ctx->pctx->name)
114 uci_parse_error(ctx, *str, "attempting to import a file without a package name");
115
116 uci_switch_config(ctx);
117 }
118
119 /* command string null-terminated by strtok */
120 *str += strlen(*str) + 1;
121
122 type = next_arg(ctx, str, true, false);
123 if (!uci_validate_str(type, false))
124 uci_parse_error(ctx, type, "invalid character in field");
125 name = next_arg(ctx, str, false, true);
126 assert_eol(ctx, str);
127
128 if (pctx->merge) {
129 UCI_TRAP_SAVE(ctx, error);
130 uci_set(ctx, pctx->package, name, NULL, type, NULL);
131 UCI_TRAP_RESTORE(ctx);
132 return;
133 error:
134 UCI_THROW(ctx, ctx->errno);
135 } else
136 pctx->section = uci_alloc_section(pctx->package, type, name);
137 }
138
139 /*
140 * parse the 'option' uci command (open a value)
141 */
142 static void uci_parse_option(struct uci_context *ctx, char **str)
143 {
144 struct uci_parse_context *pctx = ctx->pctx;
145 char *name = NULL;
146 char *value = NULL;
147
148 if (!pctx->section)
149 uci_parse_error(ctx, *str, "option command found before the first section");
150
151 /* command string null-terminated by strtok */
152 *str += strlen(*str) + 1;
153
154 name = next_arg(ctx, str, true, true);
155 value = next_arg(ctx, str, false, false);
156 assert_eol(ctx, str);
157
158 if (pctx->merge) {
159 UCI_TRAP_SAVE(ctx, error);
160 uci_set(ctx, pctx->package, pctx->section->e.name, name, value, NULL);
161 UCI_TRAP_RESTORE(ctx);
162 return;
163 error:
164 UCI_THROW(ctx, ctx->errno);
165 } else
166 uci_alloc_option(pctx->section, name, value);
167 }
168
169
170 /*
171 * parse a complete input line, split up combined commands by ';'
172 */
173 static void uci_parse_line(struct uci_context *ctx, bool single)
174 {
175 struct uci_parse_context *pctx = ctx->pctx;
176 char *word, *brk = NULL;
177
178 for (word = strtok_r(pctx->buf, ";", &brk);
179 word;
180 word = strtok_r(NULL, ";", &brk)) {
181
182 char *pbrk = NULL;
183 word = strtok_r(word, " \t", &pbrk);
184
185 if (!word)
186 continue;
187
188 switch(word[0]) {
189 case '#':
190 return;
191 case 'p':
192 if ((word[1] == 0) || !strcmp(word + 1, "ackage"))
193 uci_parse_package(ctx, &word, single);
194 break;
195 case 'c':
196 if ((word[1] == 0) || !strcmp(word + 1, "onfig"))
197 uci_parse_config(ctx, &word);
198 break;
199 case 'o':
200 if ((word[1] == 0) || !strcmp(word + 1, "ption"))
201 uci_parse_option(ctx, &word);
202 break;
203 default:
204 uci_parse_error(ctx, word, "unterminated command");
205 break;
206 }
207 }
208 }
209
210 /* max number of characters that escaping adds to the string */
211 #define UCI_QUOTE_ESCAPE "'\\''"
212
213 /*
214 * escape an uci string for export
215 */
216 static char *uci_escape(struct uci_context *ctx, char *str)
217 {
218 char *s, *p;
219 int pos = 0;
220
221 if (!ctx->buf) {
222 ctx->bufsz = LINEBUF;
223 ctx->buf = malloc(LINEBUF);
224 }
225
226 s = str;
227 p = strchr(str, '\'');
228 if (!p)
229 return str;
230
231 do {
232 int len = p - s;
233 if (len > 0) {
234 if (p + sizeof(UCI_QUOTE_ESCAPE) - str >= ctx->bufsz) {
235 ctx->bufsz *= 2;
236 ctx->buf = realloc(ctx->buf, ctx->bufsz);
237 if (!ctx->buf)
238 UCI_THROW(ctx, UCI_ERR_MEM);
239 }
240 memcpy(&ctx->buf[pos], s, len);
241 pos += len;
242 }
243 strcpy(&ctx->buf[pos], UCI_QUOTE_ESCAPE);
244 pos += sizeof(UCI_QUOTE_ESCAPE);
245 s = p + 1;
246 } while ((p = strchr(s, '\'')));
247
248 return ctx->buf;
249 }
250
251
252 /*
253 * export a single config package to a file stream
254 */
255 static void uci_export_package(struct uci_package *p, FILE *stream, bool header)
256 {
257 struct uci_context *ctx = p->ctx;
258 struct uci_element *s, *o;
259
260 if (header)
261 fprintf(stream, "package '%s'\n", uci_escape(ctx, p->e.name));
262 uci_foreach_element(&p->sections, s) {
263 struct uci_section *sec = uci_to_section(s);
264 fprintf(stream, "\nconfig '%s'", uci_escape(ctx, sec->type));
265 if (!sec->anonymous || (ctx->flags & UCI_FLAG_EXPORT_NAME))
266 fprintf(stream, " '%s'", uci_escape(ctx, sec->e.name));
267 fprintf(stream, "\n");
268 uci_foreach_element(&sec->options, o) {
269 struct uci_option *opt = uci_to_option(o);
270 fprintf(stream, "\toption '%s'", uci_escape(ctx, opt->e.name));
271 fprintf(stream, " '%s'\n", uci_escape(ctx, opt->value));
272 }
273 }
274 fprintf(stream, "\n");
275 }
276
277 int uci_export(struct uci_context *ctx, FILE *stream, struct uci_package *package, bool header)
278 {
279 struct uci_element *e;
280
281 UCI_HANDLE_ERR(ctx);
282 UCI_ASSERT(ctx, stream != NULL);
283
284 if (package)
285 uci_export_package(package, stream, header);
286 else {
287 uci_foreach_element(&ctx->root, e) {
288 uci_export_package(uci_to_package(e), stream, header);
289 }
290 }
291
292 return 0;
293 }
294
295 int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct uci_package **package, bool single)
296 {
297 struct uci_parse_context *pctx;
298 UCI_HANDLE_ERR(ctx);
299
300 /* make sure no memory from previous parse attempts is leaked */
301 uci_file_cleanup(ctx);
302
303 uci_alloc_parse_context(ctx);
304 pctx = ctx->pctx;
305 pctx->file = stream;
306 if (*package && single) {
307 pctx->package = *package;
308 pctx->merge = true;
309 }
310
311 /*
312 * If 'name' was supplied, assume that the supplied stream does not contain
313 * the appropriate 'package <name>' string to specify the config name
314 * NB: the config file can still override the package name
315 */
316 if (name) {
317 UCI_ASSERT(ctx, uci_validate_name(name));
318 pctx->name = name;
319 }
320
321 while (!feof(pctx->file)) {
322 uci_getln(ctx, 0);
323 UCI_TRAP_SAVE(ctx, error);
324 if (pctx->buf[0])
325 uci_parse_line(ctx, single);
326 UCI_TRAP_RESTORE(ctx);
327 continue;
328 error:
329 if (ctx->flags & UCI_FLAG_PERROR)
330 uci_perror(ctx, NULL);
331 if ((ctx->errno != UCI_ERR_PARSE) ||
332 (ctx->flags & UCI_FLAG_STRICT))
333 UCI_THROW(ctx, ctx->errno);
334 }
335
336 uci_fixup_section(ctx, ctx->pctx->section);
337 if (package)
338 *package = pctx->package;
339 if (pctx->merge)
340 pctx->package = NULL;
341
342 pctx->name = NULL;
343 uci_switch_config(ctx);
344
345 /* no error happened, we can get rid of the parser context now */
346 uci_file_cleanup(ctx);
347
348 return 0;
349 }
350
351
352 static char *uci_config_path(struct uci_context *ctx, const char *name)
353 {
354 char *filename;
355
356 UCI_ASSERT(ctx, uci_validate_name(name));
357 filename = uci_malloc(ctx, strlen(name) + strlen(ctx->confdir) + 2);
358 sprintf(filename, "%s/%s", ctx->confdir, name);
359
360 return filename;
361 }
362
363 int uci_load(struct uci_context *ctx, const char *name, struct uci_package **package)
364 {
365 char *filename;
366 bool confdir;
367 FILE *file = NULL;
368
369 UCI_HANDLE_ERR(ctx);
370
371 switch (name[0]) {
372 case '.':
373 /* relative path outside of /etc/config */
374 if (name[1] != '/')
375 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
376 /* fall through */
377 case '/':
378 /* absolute path outside of /etc/config */
379 filename = uci_strdup(ctx, name);
380 name = strrchr(name, '/') + 1;
381 confdir = false;
382 break;
383 default:
384 /* config in /etc/config */
385 filename = uci_config_path(ctx, name);
386 confdir = true;
387 break;
388 }
389
390 file = uci_open_stream(ctx, filename, SEEK_SET, false, false);
391 ctx->errno = 0;
392 UCI_TRAP_SAVE(ctx, done);
393 UCI_INTERNAL(uci_import, ctx, file, name, package, true);
394 UCI_TRAP_RESTORE(ctx);
395
396 if (*package) {
397 (*package)->path = filename;
398 (*package)->confdir = confdir;
399 uci_load_history(ctx, *package, false);
400 }
401
402 done:
403 uci_close_stream(file);
404 return ctx->errno;
405 }
406
407 int uci_commit(struct uci_context *ctx, struct uci_package **package, bool overwrite)
408 {
409 struct uci_package *p;
410 FILE *f = NULL;
411 char *name = NULL;
412 char *path = NULL;
413
414 UCI_HANDLE_ERR(ctx);
415 UCI_ASSERT(ctx, package != NULL);
416 p = *package;
417
418 UCI_ASSERT(ctx, p != NULL);
419 if (!p->path) {
420 if (overwrite)
421 p->path = uci_config_path(ctx, p->e.name);
422 else
423 UCI_THROW(ctx, UCI_ERR_INVAL);
424 }
425
426
427 /* open the config file for writing now, so that it is locked */
428 f = uci_open_stream(ctx, p->path, SEEK_SET, true, true);
429
430 /* flush unsaved changes and reload from history file */
431 UCI_TRAP_SAVE(ctx, done);
432 if (p->confdir) {
433 if (!overwrite) {
434 name = uci_strdup(ctx, p->e.name);
435 path = uci_strdup(ctx, p->path);
436 /* dump our own changes to the history file */
437 if (!uci_list_empty(&p->history))
438 UCI_INTERNAL(uci_save, ctx, p);
439
440 /*
441 * other processes might have modified the config
442 * as well. dump and reload
443 */
444 uci_free_package(&p);
445 uci_file_cleanup(ctx);
446 UCI_INTERNAL(uci_import, ctx, f, name, &p, true);
447
448 p->path = path;
449 p->confdir = true;
450 *package = p;
451
452 /* freed together with the uci_package */
453 path = NULL;
454
455 /* check for updated history, flush */
456 if (!uci_load_history(ctx, p, true))
457 goto done;
458 } else {
459 /* flush history */
460 if (!uci_load_history(ctx, NULL, true))
461 goto done;
462 }
463 }
464
465 rewind(f);
466 ftruncate(fileno(f), 0);
467
468 uci_export(ctx, f, p, false);
469 UCI_TRAP_RESTORE(ctx);
470
471 done:
472 if (name)
473 free(name);
474 if (path)
475 free(path);
476 uci_close_stream(f);
477 if (ctx->errno)
478 UCI_THROW(ctx, ctx->errno);
479
480 return 0;
481 }
482
483
484 /*
485 * This function returns the filename by returning the string
486 * after the last '/' character. By checking for a non-'\0'
487 * character afterwards, directories are ignored (glob marks
488 * those with a trailing '/'
489 */
490 static inline char *get_filename(char *path)
491 {
492 char *p;
493
494 p = strrchr(path, '/');
495 p++;
496 if (!*p)
497 return NULL;
498 return p;
499 }
500
501 int uci_list_configs(struct uci_context *ctx, char ***list)
502 {
503 char **configs;
504 glob_t globbuf;
505 int size, i;
506 char *buf;
507 char *dir;
508
509 UCI_HANDLE_ERR(ctx);
510
511 dir = uci_malloc(ctx, strlen(ctx->confdir) + 1 + sizeof("/*"));
512 sprintf(dir, "%s/*", ctx->confdir);
513 if (glob(dir, GLOB_MARK, NULL, &globbuf) != 0)
514 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
515
516 size = sizeof(char *) * (globbuf.gl_pathc + 1);
517 for(i = 0; i < globbuf.gl_pathc; i++) {
518 char *p;
519
520 p = get_filename(globbuf.gl_pathv[i]);
521 if (!p)
522 continue;
523
524 size += strlen(p) + 1;
525 }
526
527 configs = uci_malloc(ctx, size);
528 buf = (char *) &configs[globbuf.gl_pathc + 1];
529 for(i = 0; i < globbuf.gl_pathc; i++) {
530 char *p;
531
532 p = get_filename(globbuf.gl_pathv[i]);
533 if (!p)
534 continue;
535
536 configs[i] = buf;
537 strcpy(buf, p);
538 buf += strlen(buf) + 1;
539 }
540 *list = configs;
541 free(dir);
542
543 return 0;
544 }
545