file.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  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. * This file contains the code for parsing uci config files
  16. */
  17. #define _GNU_SOURCE
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. #include <sys/file.h>
  21. #include <stdbool.h>
  22. #include <unistd.h>
  23. #include <fcntl.h>
  24. #include <stdio.h>
  25. #include <ctype.h>
  26. #include <glob.h>
  27. #include <string.h>
  28. #include <stdlib.h>
  29. #include "uci.h"
  30. #include "uci_internal.h"
  31. #define LINEBUF 32
  32. #define LINEBUF_MAX 4096
  33. /*
  34. * Fetch a new line from the input stream and resize buffer if necessary
  35. */
  36. __private void uci_getln(struct uci_context *ctx, int offset)
  37. {
  38. struct uci_parse_context *pctx = ctx->pctx;
  39. char *p;
  40. int ofs;
  41. if (pctx->buf == NULL) {
  42. pctx->buf = uci_malloc(ctx, LINEBUF);
  43. pctx->bufsz = LINEBUF;
  44. }
  45. /* `offset' may off by one */
  46. if (offset >= pctx->bufsz) {
  47. pctx->bufsz *= 2;
  48. pctx->buf = uci_realloc(ctx, pctx->buf, pctx->bufsz);
  49. }
  50. ofs = offset;
  51. do {
  52. p = &pctx->buf[ofs];
  53. p[0] = 0;
  54. p = fgets(p, pctx->bufsz - ofs, pctx->file);
  55. if (!p || !*p)
  56. return;
  57. ofs += strlen(p);
  58. if (pctx->buf[ofs - 1] == '\n') {
  59. pctx->line++;
  60. return;
  61. }
  62. if (pctx->bufsz > LINEBUF_MAX/2)
  63. uci_parse_error(ctx, "line too long");
  64. pctx->bufsz *= 2;
  65. pctx->buf = uci_realloc(ctx, pctx->buf, pctx->bufsz);
  66. } while (1);
  67. }
  68. /*
  69. * parse a character escaped by '\'
  70. * returns true if the escaped character is to be parsed
  71. * returns false if the escaped character is to be ignored
  72. */
  73. static bool parse_backslash(struct uci_context *ctx)
  74. {
  75. struct uci_parse_context *pctx = ctx->pctx;
  76. /* skip backslash */
  77. pctx->pos += 1;
  78. /* undecoded backslash at the end of line, fetch the next line */
  79. if (!pctx_cur_char(pctx) ||
  80. pctx_cur_char(pctx) == '\n' ||
  81. (pctx_cur_char(pctx) == '\r' &&
  82. pctx_char(pctx, pctx_pos(pctx) + 1) == '\n' &&
  83. !pctx_char(pctx, pctx_pos(pctx) + 2))) {
  84. uci_getln(ctx, pctx->pos);
  85. return false;
  86. }
  87. /* FIXME: decode escaped char, necessary? */
  88. return true;
  89. }
  90. /*
  91. * move the string pointer forward until a non-whitespace character or
  92. * EOL is reached
  93. */
  94. static void skip_whitespace(struct uci_context *ctx)
  95. {
  96. struct uci_parse_context *pctx = ctx->pctx;
  97. while (pctx_cur_char(pctx) && isspace(pctx_cur_char(pctx)))
  98. pctx->pos += 1;
  99. }
  100. static inline void addc(struct uci_context *ctx, int *pos_dest, int *pos_src)
  101. {
  102. struct uci_parse_context *pctx = ctx->pctx;
  103. pctx_char(pctx, *pos_dest) = pctx_char(pctx, *pos_src);
  104. *pos_dest += 1;
  105. *pos_src += 1;
  106. }
  107. /*
  108. * parse a double quoted string argument from the command line
  109. */
  110. static void parse_double_quote(struct uci_context *ctx, int *target)
  111. {
  112. struct uci_parse_context *pctx = ctx->pctx;
  113. char c;
  114. /* skip quote character */
  115. pctx->pos += 1;
  116. while (1) {
  117. c = pctx_cur_char(pctx);
  118. switch(c) {
  119. case '"':
  120. pctx->pos += 1;
  121. return;
  122. case 0:
  123. /* Multi-line str value */
  124. uci_getln(ctx, pctx->pos);
  125. if (!pctx_cur_char(pctx)) {
  126. uci_parse_error(ctx, "EOF with unterminated \"");
  127. }
  128. break;
  129. case '\\':
  130. if (!parse_backslash(ctx))
  131. continue;
  132. /* fall through */
  133. default:
  134. addc(ctx, target, &pctx->pos);
  135. break;
  136. }
  137. }
  138. uci_parse_error(ctx, "unterminated \"");
  139. }
  140. /*
  141. * parse a single quoted string argument from the command line
  142. */
  143. static void parse_single_quote(struct uci_context *ctx, int *target)
  144. {
  145. struct uci_parse_context *pctx = ctx->pctx;
  146. char c;
  147. /* skip quote character */
  148. pctx->pos += 1;
  149. while (1) {
  150. c = pctx_cur_char(pctx);
  151. switch(c) {
  152. case '\'':
  153. pctx->pos += 1;
  154. return;
  155. case 0:
  156. /* Multi-line str value */
  157. uci_getln(ctx, pctx->pos);
  158. if (!pctx_cur_char(pctx))
  159. uci_parse_error(ctx, "EOF with unterminated \"");
  160. break;
  161. default:
  162. addc(ctx, target, &pctx->pos);
  163. }
  164. }
  165. uci_parse_error(ctx, "unterminated '");
  166. }
  167. /*
  168. * parse a string from the command line and detect the quoting style
  169. */
  170. static void parse_str(struct uci_context *ctx, int *target)
  171. {
  172. struct uci_parse_context *pctx = ctx->pctx;
  173. bool next = true;
  174. do {
  175. switch(pctx_cur_char(pctx)) {
  176. case '\'':
  177. parse_single_quote(ctx, target);
  178. break;
  179. case '"':
  180. parse_double_quote(ctx, target);
  181. break;
  182. case '#':
  183. pctx_cur_char(pctx) = 0;
  184. /* fall through */
  185. case 0:
  186. goto done;
  187. case ';':
  188. next = false;
  189. goto done;
  190. case '\\':
  191. if (!parse_backslash(ctx))
  192. continue;
  193. /* fall through */
  194. default:
  195. addc(ctx, target, &pctx->pos);
  196. break;
  197. }
  198. } while (pctx_cur_char(pctx) && !isspace(pctx_cur_char(pctx)));
  199. done:
  200. /*
  201. * if the string was unquoted and we've stopped at a whitespace
  202. * character, skip to the next one, because the whitespace will
  203. * be overwritten by a null byte here
  204. */
  205. if (pctx_cur_char(pctx) && next)
  206. pctx->pos += 1;
  207. /* terminate the parsed string */
  208. pctx_char(pctx, *target) = 0;
  209. }
  210. /*
  211. * extract the next argument from the command line
  212. */
  213. static char *next_arg(struct uci_context *ctx, bool required, bool name)
  214. {
  215. struct uci_parse_context *pctx = ctx->pctx;
  216. int val, ptr;
  217. skip_whitespace(ctx);
  218. val = ptr = pctx_pos(pctx);
  219. if (pctx_cur_char(pctx) == ';') {
  220. pctx_cur_char(pctx) = 0;
  221. pctx->pos += 1;
  222. } else {
  223. parse_str(ctx, &ptr);
  224. }
  225. if (!pctx_char(pctx, val)) {
  226. if (required)
  227. uci_parse_error(ctx, "insufficient arguments");
  228. goto done;
  229. }
  230. if (name && !uci_validate_name(pctx_str(pctx, val)))
  231. uci_parse_error(ctx, "invalid character in name field");
  232. done:
  233. return pctx_str(pctx, val);
  234. }
  235. int uci_parse_argument(struct uci_context *ctx, FILE *stream, char **str, char **result)
  236. {
  237. UCI_HANDLE_ERR(ctx);
  238. UCI_ASSERT(ctx, str != NULL);
  239. UCI_ASSERT(ctx, result != NULL);
  240. if (ctx->pctx && (ctx->pctx->file != stream))
  241. uci_cleanup(ctx);
  242. if (!ctx->pctx)
  243. uci_alloc_parse_context(ctx);
  244. ctx->pctx->file = stream;
  245. if (!*str) {
  246. uci_getln(ctx, 0);
  247. *str = ctx->pctx->buf;
  248. } else {
  249. UCI_ASSERT(ctx, ctx->pctx->pos == *str - ctx->pctx->buf);
  250. }
  251. *result = next_arg(ctx, false, false);
  252. return 0;
  253. }
  254. static int
  255. uci_fill_ptr(struct uci_context *ctx, struct uci_ptr *ptr, struct uci_element *e)
  256. {
  257. UCI_ASSERT(ctx, ptr != NULL);
  258. UCI_ASSERT(ctx, e != NULL);
  259. memset(ptr, 0, sizeof(struct uci_ptr));
  260. switch(e->type) {
  261. case UCI_TYPE_OPTION:
  262. ptr->o = uci_to_option(e);
  263. goto fill_option;
  264. case UCI_TYPE_SECTION:
  265. ptr->s = uci_to_section(e);
  266. goto fill_section;
  267. case UCI_TYPE_PACKAGE:
  268. ptr->p = uci_to_package(e);
  269. goto fill_package;
  270. default:
  271. UCI_THROW(ctx, UCI_ERR_INVAL);
  272. }
  273. fill_option:
  274. ptr->option = ptr->o->e.name;
  275. ptr->s = ptr->o->section;
  276. fill_section:
  277. ptr->section = ptr->s->e.name;
  278. ptr->p = ptr->s->package;
  279. fill_package:
  280. ptr->package = ptr->p->e.name;
  281. ptr->flags |= UCI_LOOKUP_DONE;
  282. return 0;
  283. }
  284. /*
  285. * verify that the end of the line or command is reached.
  286. * throw an error if extra arguments are given on the command line
  287. */
  288. static void assert_eol(struct uci_context *ctx)
  289. {
  290. char *tmp;
  291. skip_whitespace(ctx);
  292. tmp = next_arg(ctx, false, false);
  293. if (*tmp && (ctx->flags & UCI_FLAG_STRICT))
  294. uci_parse_error(ctx, "too many arguments");
  295. }
  296. /*
  297. * switch to a different config, either triggered by uci_load, or by a
  298. * 'package <...>' statement in the import file
  299. */
  300. static void uci_switch_config(struct uci_context *ctx)
  301. {
  302. struct uci_parse_context *pctx;
  303. struct uci_element *e;
  304. const char *name;
  305. pctx = ctx->pctx;
  306. name = pctx->name;
  307. /* add the last config to main config file list */
  308. if (pctx->package) {
  309. pctx->package->backend = ctx->backend;
  310. uci_list_add(&ctx->root, &pctx->package->e.list);
  311. pctx->package = NULL;
  312. pctx->section = NULL;
  313. }
  314. if (!name)
  315. return;
  316. /*
  317. * if an older config under the same name exists, unload it
  318. * ignore errors here, e.g. if the config was not found
  319. */
  320. e = uci_lookup_list(&ctx->root, name);
  321. if (e)
  322. UCI_THROW(ctx, UCI_ERR_DUPLICATE);
  323. pctx->package = uci_alloc_package(ctx, name);
  324. }
  325. /*
  326. * parse the 'package' uci command (next config package)
  327. */
  328. static void uci_parse_package(struct uci_context *ctx, bool single)
  329. {
  330. struct uci_parse_context *pctx = ctx->pctx;
  331. char *name = NULL;
  332. /* command string null-terminated by strtok */
  333. pctx->pos += strlen(pctx_cur_str(pctx)) + 1;
  334. name = next_arg(ctx, true, true);
  335. assert_eol(ctx);
  336. if (single)
  337. return;
  338. ctx->pctx->name = name;
  339. uci_switch_config(ctx);
  340. }
  341. /*
  342. * parse the 'config' uci command (open a section)
  343. */
  344. static void uci_parse_config(struct uci_context *ctx)
  345. {
  346. struct uci_parse_context *pctx = ctx->pctx;
  347. struct uci_element *e;
  348. struct uci_ptr ptr;
  349. char *name = NULL;
  350. char *type = NULL;
  351. uci_fixup_section(ctx, ctx->pctx->section);
  352. if (!ctx->pctx->package) {
  353. if (!ctx->pctx->name)
  354. uci_parse_error(ctx, "attempting to import a file without a package name");
  355. uci_switch_config(ctx);
  356. }
  357. /* command string null-terminated by strtok */
  358. pctx->pos += strlen(pctx_cur_str(pctx)) + 1;
  359. type = next_arg(ctx, true, false);
  360. if (!uci_validate_type(type))
  361. uci_parse_error(ctx, "invalid character in type field");
  362. name = next_arg(ctx, false, true);
  363. assert_eol(ctx);
  364. if (!name || !name[0]) {
  365. ctx->internal = !pctx->merge;
  366. UCI_NESTED(uci_add_section, ctx, pctx->package, type, &pctx->section);
  367. } else {
  368. uci_fill_ptr(ctx, &ptr, &pctx->package->e);
  369. e = uci_lookup_list(&pctx->package->sections, name);
  370. if (e)
  371. ptr.s = uci_to_section(e);
  372. ptr.section = name;
  373. ptr.value = type;
  374. ctx->internal = !pctx->merge;
  375. UCI_NESTED(uci_set, ctx, &ptr);
  376. pctx->section = uci_to_section(ptr.last);
  377. }
  378. }
  379. /*
  380. * parse the 'option' uci command (open a value)
  381. */
  382. static void uci_parse_option(struct uci_context *ctx, bool list)
  383. {
  384. struct uci_parse_context *pctx = ctx->pctx;
  385. struct uci_element *e;
  386. struct uci_ptr ptr;
  387. char *name = NULL;
  388. char *value = NULL;
  389. if (!pctx->section)
  390. uci_parse_error(ctx, "option/list command found before the first section");
  391. /* command string null-terminated by strtok */
  392. pctx->pos += strlen(pctx_cur_str(pctx)) + 1;
  393. name = next_arg(ctx, true, true);
  394. value = next_arg(ctx, false, false);
  395. assert_eol(ctx);
  396. uci_fill_ptr(ctx, &ptr, &pctx->section->e);
  397. e = uci_lookup_list(&pctx->section->options, name);
  398. if (e)
  399. ptr.o = uci_to_option(e);
  400. ptr.option = name;
  401. ptr.value = value;
  402. ctx->internal = !pctx->merge;
  403. if (list)
  404. UCI_NESTED(uci_add_list, ctx, &ptr);
  405. else
  406. UCI_NESTED(uci_set, ctx, &ptr);
  407. }
  408. /*
  409. * parse a complete input line, split up combined commands by ';'
  410. */
  411. static void uci_parse_line(struct uci_context *ctx, bool single)
  412. {
  413. struct uci_parse_context *pctx = ctx->pctx;
  414. char *word;
  415. /* Skip whitespace characters at the start of line */
  416. skip_whitespace(ctx);
  417. do {
  418. word = strtok(pctx_cur_str(pctx), " \t");
  419. if (!word)
  420. return;
  421. switch(word[0]) {
  422. case 0:
  423. case '#':
  424. return;
  425. case 'p':
  426. if ((word[1] == 0) || !strcmp(word + 1, "ackage"))
  427. uci_parse_package(ctx, single);
  428. else
  429. goto invalid;
  430. break;
  431. case 'c':
  432. if ((word[1] == 0) || !strcmp(word + 1, "onfig"))
  433. uci_parse_config(ctx);
  434. else
  435. goto invalid;
  436. break;
  437. case 'o':
  438. if ((word[1] == 0) || !strcmp(word + 1, "ption"))
  439. uci_parse_option(ctx, false);
  440. else
  441. goto invalid;
  442. break;
  443. case 'l':
  444. if ((word[1] == 0) || !strcmp(word + 1, "ist"))
  445. uci_parse_option(ctx, true);
  446. else
  447. goto invalid;
  448. break;
  449. default:
  450. goto invalid;
  451. }
  452. continue;
  453. invalid:
  454. uci_parse_error(ctx, "invalid command");
  455. } while (1);
  456. }
  457. /* max number of characters that escaping adds to the string */
  458. #define UCI_QUOTE_ESCAPE "'\\''"
  459. /*
  460. * escape an uci string for export
  461. */
  462. static const char *uci_escape(struct uci_context *ctx, const char *str)
  463. {
  464. const char *end;
  465. int ofs = 0;
  466. if (!ctx->buf) {
  467. ctx->bufsz = LINEBUF;
  468. ctx->buf = malloc(LINEBUF);
  469. if (!ctx->buf)
  470. return str;
  471. }
  472. while (1) {
  473. int len;
  474. end = strchr(str, '\'');
  475. if (!end)
  476. end = str + strlen(str);
  477. len = end - str;
  478. /* make sure that we have enough room in the buffer */
  479. while (ofs + len + sizeof(UCI_QUOTE_ESCAPE) + 1 > ctx->bufsz) {
  480. ctx->bufsz *= 2;
  481. ctx->buf = uci_realloc(ctx, ctx->buf, ctx->bufsz);
  482. }
  483. /* copy the string until the character before the quote */
  484. memcpy(&ctx->buf[ofs], str, len);
  485. ofs += len;
  486. /* end of string? return the buffer */
  487. if (*end == 0)
  488. break;
  489. memcpy(&ctx->buf[ofs], UCI_QUOTE_ESCAPE, sizeof(UCI_QUOTE_ESCAPE));
  490. ofs += strlen(&ctx->buf[ofs]);
  491. str = end + 1;
  492. }
  493. ctx->buf[ofs] = 0;
  494. return ctx->buf;
  495. }
  496. /*
  497. * export a single config package to a file stream
  498. */
  499. static void uci_export_package(struct uci_package *p, FILE *stream, bool header)
  500. {
  501. struct uci_context *ctx = p->ctx;
  502. struct uci_element *s, *o, *i;
  503. if (header)
  504. fprintf(stream, "package %s\n", uci_escape(ctx, p->e.name));
  505. uci_foreach_element(&p->sections, s) {
  506. struct uci_section *sec = uci_to_section(s);
  507. fprintf(stream, "\nconfig %s", uci_escape(ctx, sec->type));
  508. if (!sec->anonymous || (ctx->flags & UCI_FLAG_EXPORT_NAME))
  509. fprintf(stream, " '%s'", uci_escape(ctx, sec->e.name));
  510. fprintf(stream, "\n");
  511. uci_foreach_element(&sec->options, o) {
  512. struct uci_option *opt = uci_to_option(o);
  513. switch(opt->type) {
  514. case UCI_TYPE_STRING:
  515. fprintf(stream, "\toption %s", uci_escape(ctx, opt->e.name));
  516. fprintf(stream, " '%s'\n", uci_escape(ctx, opt->v.string));
  517. break;
  518. case UCI_TYPE_LIST:
  519. uci_foreach_element(&opt->v.list, i) {
  520. fprintf(stream, "\tlist %s", uci_escape(ctx, opt->e.name));
  521. fprintf(stream, " '%s'\n", uci_escape(ctx, i->name));
  522. }
  523. break;
  524. default:
  525. fprintf(stream, "\t# unknown type for option '%s'\n", uci_escape(ctx, opt->e.name));
  526. break;
  527. }
  528. }
  529. }
  530. fprintf(stream, "\n");
  531. }
  532. int uci_export(struct uci_context *ctx, FILE *stream, struct uci_package *package, bool header)
  533. {
  534. struct uci_element *e;
  535. UCI_HANDLE_ERR(ctx);
  536. UCI_ASSERT(ctx, stream != NULL);
  537. if (package)
  538. uci_export_package(package, stream, header);
  539. else {
  540. uci_foreach_element(&ctx->root, e) {
  541. uci_export_package(uci_to_package(e), stream, header);
  542. }
  543. }
  544. return 0;
  545. }
  546. int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct uci_package **package, bool single)
  547. {
  548. struct uci_parse_context *pctx;
  549. UCI_HANDLE_ERR(ctx);
  550. /* make sure no memory from previous parse attempts is leaked */
  551. uci_cleanup(ctx);
  552. uci_alloc_parse_context(ctx);
  553. pctx = ctx->pctx;
  554. pctx->file = stream;
  555. if (package && *package && single) {
  556. pctx->package = *package;
  557. pctx->merge = true;
  558. }
  559. /*
  560. * If 'name' was supplied, assume that the supplied stream does not contain
  561. * the appropriate 'package <name>' string to specify the config name
  562. * NB: the config file can still override the package name
  563. */
  564. if (name) {
  565. UCI_ASSERT(ctx, uci_validate_package(name));
  566. pctx->name = name;
  567. }
  568. while (!feof(pctx->file)) {
  569. pctx->pos = 0;
  570. uci_getln(ctx, 0);
  571. UCI_TRAP_SAVE(ctx, error);
  572. if (pctx->buf[0])
  573. uci_parse_line(ctx, single);
  574. UCI_TRAP_RESTORE(ctx);
  575. continue;
  576. error:
  577. if (ctx->flags & UCI_FLAG_PERROR)
  578. uci_perror(ctx, NULL);
  579. if ((ctx->err != UCI_ERR_PARSE) ||
  580. (ctx->flags & UCI_FLAG_STRICT))
  581. UCI_THROW(ctx, ctx->err);
  582. }
  583. uci_fixup_section(ctx, ctx->pctx->section);
  584. if (!pctx->package && name)
  585. uci_switch_config(ctx);
  586. if (package)
  587. *package = pctx->package;
  588. if (pctx->merge)
  589. pctx->package = NULL;
  590. pctx->name = NULL;
  591. uci_switch_config(ctx);
  592. /* no error happened, we can get rid of the parser context now */
  593. uci_cleanup(ctx);
  594. return 0;
  595. }
  596. static char *uci_config_path(struct uci_context *ctx, const char *name)
  597. {
  598. char *filename;
  599. UCI_ASSERT(ctx, uci_validate_package(name));
  600. filename = uci_malloc(ctx, strlen(name) + strlen(ctx->confdir) + 2);
  601. sprintf(filename, "%s/%s", ctx->confdir, name);
  602. return filename;
  603. }
  604. static void uci_file_commit(struct uci_context *ctx, struct uci_package **package, bool overwrite)
  605. {
  606. struct uci_package *p = *package;
  607. FILE *f1, *f2 = NULL;
  608. char *name = NULL;
  609. char *path = NULL;
  610. char *filename = NULL;
  611. struct stat statbuf;
  612. bool do_rename = false;
  613. if (!p->path) {
  614. if (overwrite)
  615. p->path = uci_config_path(ctx, p->e.name);
  616. else
  617. UCI_THROW(ctx, UCI_ERR_INVAL);
  618. }
  619. if ((asprintf(&filename, "%s/.%s.uci-XXXXXX", ctx->confdir, p->e.name) < 0) || !filename)
  620. UCI_THROW(ctx, UCI_ERR_MEM);
  621. if (!mktemp(filename))
  622. *filename = 0;
  623. if (!*filename) {
  624. free(filename);
  625. UCI_THROW(ctx, UCI_ERR_IO);
  626. }
  627. if ((stat(filename, &statbuf) == 0) && ((statbuf.st_mode & S_IFMT) != S_IFREG))
  628. UCI_THROW(ctx, UCI_ERR_IO);
  629. /* open the config file for writing now, so that it is locked */
  630. f1 = uci_open_stream(ctx, p->path, NULL, SEEK_SET, true, true);
  631. /* flush unsaved changes and reload from delta file */
  632. UCI_TRAP_SAVE(ctx, done);
  633. if (p->has_delta) {
  634. if (!overwrite) {
  635. name = uci_strdup(ctx, p->e.name);
  636. path = uci_strdup(ctx, p->path);
  637. /* dump our own changes to the delta file */
  638. if (!uci_list_empty(&p->delta))
  639. UCI_INTERNAL(uci_save, ctx, p);
  640. /*
  641. * other processes might have modified the config
  642. * as well. dump and reload
  643. */
  644. uci_free_package(&p);
  645. uci_cleanup(ctx);
  646. UCI_INTERNAL(uci_import, ctx, f1, name, &p, true);
  647. p->path = path;
  648. p->has_delta = true;
  649. *package = p;
  650. /* freed together with the uci_package */
  651. path = NULL;
  652. }
  653. /* flush delta */
  654. if (!uci_load_delta(ctx, p, true))
  655. goto done;
  656. }
  657. f2 = uci_open_stream(ctx, filename, p->path, SEEK_SET, true, true);
  658. uci_export(ctx, f2, p, false);
  659. fflush(f2);
  660. fsync(fileno(f2));
  661. uci_close_stream(f2);
  662. do_rename = true;
  663. UCI_TRAP_RESTORE(ctx);
  664. done:
  665. free(name);
  666. free(path);
  667. uci_close_stream(f1);
  668. if (do_rename && rename(filename, p->path)) {
  669. unlink(filename);
  670. UCI_THROW(ctx, UCI_ERR_IO);
  671. }
  672. free(filename);
  673. sync();
  674. if (ctx->err)
  675. UCI_THROW(ctx, ctx->err);
  676. }
  677. /*
  678. * This function returns the filename by returning the string
  679. * after the last '/' character. By checking for a non-'\0'
  680. * character afterwards, directories are ignored (glob marks
  681. * those with a trailing '/'
  682. */
  683. static inline char *get_filename(char *path)
  684. {
  685. char *p;
  686. p = strrchr(path, '/');
  687. p++;
  688. if (!*p)
  689. return NULL;
  690. return p;
  691. }
  692. static char **uci_list_config_files(struct uci_context *ctx)
  693. {
  694. char **configs;
  695. glob_t globbuf;
  696. int size, i;
  697. char *buf;
  698. char *dir;
  699. dir = uci_malloc(ctx, strlen(ctx->confdir) + 1 + sizeof("/*"));
  700. sprintf(dir, "%s/*", ctx->confdir);
  701. if (glob(dir, GLOB_MARK, NULL, &globbuf) != 0) {
  702. free(dir);
  703. UCI_THROW(ctx, UCI_ERR_NOTFOUND);
  704. }
  705. size = sizeof(char *) * (globbuf.gl_pathc + 1);
  706. for(i = 0; i < globbuf.gl_pathc; i++) {
  707. char *p;
  708. p = get_filename(globbuf.gl_pathv[i]);
  709. if (!p)
  710. continue;
  711. size += strlen(p) + 1;
  712. }
  713. configs = uci_malloc(ctx, size);
  714. buf = (char *) &configs[globbuf.gl_pathc + 1];
  715. for(i = 0; i < globbuf.gl_pathc; i++) {
  716. char *p;
  717. p = get_filename(globbuf.gl_pathv[i]);
  718. if (!p)
  719. continue;
  720. if (!uci_validate_package(p))
  721. continue;
  722. configs[i] = buf;
  723. strcpy(buf, p);
  724. buf += strlen(buf) + 1;
  725. }
  726. free(dir);
  727. globfree(&globbuf);
  728. return configs;
  729. }
  730. static struct uci_package *uci_file_load(struct uci_context *ctx, const char *name)
  731. {
  732. struct uci_package *package = NULL;
  733. char *filename;
  734. bool confdir;
  735. FILE *file = NULL;
  736. switch (name[0]) {
  737. case '.':
  738. /* relative path outside of /etc/config */
  739. if (name[1] != '/')
  740. UCI_THROW(ctx, UCI_ERR_NOTFOUND);
  741. /* fall through */
  742. case '/':
  743. /* absolute path outside of /etc/config */
  744. filename = uci_strdup(ctx, name);
  745. name = strrchr(name, '/') + 1;
  746. confdir = false;
  747. break;
  748. default:
  749. /* config in /etc/config */
  750. filename = uci_config_path(ctx, name);
  751. confdir = true;
  752. break;
  753. }
  754. UCI_TRAP_SAVE(ctx, done);
  755. file = uci_open_stream(ctx, filename, NULL, SEEK_SET, false, false);
  756. ctx->err = 0;
  757. UCI_INTERNAL(uci_import, ctx, file, name, &package, true);
  758. UCI_TRAP_RESTORE(ctx);
  759. if (package) {
  760. package->path = filename;
  761. package->has_delta = confdir;
  762. uci_load_delta(ctx, package, false);
  763. }
  764. done:
  765. uci_close_stream(file);
  766. if (ctx->err) {
  767. free(filename);
  768. UCI_THROW(ctx, ctx->err);
  769. }
  770. return package;
  771. }
  772. __private UCI_BACKEND(uci_file_backend, "file",
  773. .load = uci_file_load,
  774. .commit = uci_file_commit,
  775. .list_configs = uci_list_config_files,
  776. );