delta.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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 handling uci config delta 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 <string.h>
  27. #include <stdlib.h>
  28. #include "uci.h"
  29. #include "uci_internal.h"
  30. /* record a change that was done to a package */
  31. void
  32. uci_add_delta(struct uci_context *ctx, struct uci_list *list, int cmd, const char *section, const char *option, const char *value)
  33. {
  34. struct uci_delta *h;
  35. int size = strlen(section) + 1;
  36. char *ptr;
  37. if (value)
  38. size += strlen(value) + 1;
  39. h = uci_alloc_element(ctx, delta, option, size);
  40. ptr = uci_dataptr(h);
  41. h->cmd = cmd;
  42. h->section = strcpy(ptr, section);
  43. if (value) {
  44. ptr += strlen(ptr) + 1;
  45. h->value = strcpy(ptr, value);
  46. }
  47. uci_list_add(list, &h->e.list);
  48. }
  49. void
  50. uci_free_delta(struct uci_delta *h)
  51. {
  52. if (!h)
  53. return;
  54. if ((h->section != NULL) &&
  55. (h->section != uci_dataptr(h))) {
  56. free(h->section);
  57. free(h->value);
  58. }
  59. uci_free_element(&h->e);
  60. }
  61. int uci_set_savedir(struct uci_context *ctx, const char *dir)
  62. {
  63. char *sdir;
  64. UCI_HANDLE_ERR(ctx);
  65. UCI_ASSERT(ctx, dir != NULL);
  66. sdir = uci_strdup(ctx, dir);
  67. if (ctx->savedir != uci_savedir)
  68. free(ctx->savedir);
  69. ctx->savedir = sdir;
  70. return 0;
  71. }
  72. int uci_add_delta_path(struct uci_context *ctx, const char *dir)
  73. {
  74. struct uci_element *e;
  75. UCI_HANDLE_ERR(ctx);
  76. UCI_ASSERT(ctx, dir != NULL);
  77. if (!strcmp(dir, ctx->savedir))
  78. return -1;
  79. e = uci_alloc_generic(ctx, UCI_TYPE_PATH, dir, sizeof(struct uci_element));
  80. uci_list_add(&ctx->delta_path, &e->list);
  81. return 0;
  82. }
  83. char const uci_command_char[] = {
  84. [UCI_CMD_ADD] = '+',
  85. [UCI_CMD_REMOVE] = '-',
  86. [UCI_CMD_CHANGE] = 0,
  87. [UCI_CMD_RENAME] = '@',
  88. [UCI_CMD_REORDER] = '^',
  89. [UCI_CMD_LIST_ADD] = '|',
  90. [UCI_CMD_LIST_DEL] = '~'
  91. };
  92. static inline int uci_parse_delta_tuple(struct uci_context *ctx, struct uci_ptr *ptr)
  93. {
  94. struct uci_parse_context *pctx = ctx->pctx;
  95. char *str = pctx_cur_str(pctx), *arg;
  96. int c;
  97. UCI_INTERNAL(uci_parse_argument, ctx, ctx->pctx->file, &str, &arg);
  98. for (c = 0; c <= __UCI_CMD_LAST; c++) {
  99. if (uci_command_char[c] == *arg)
  100. break;
  101. }
  102. if (c > __UCI_CMD_LAST)
  103. c = UCI_CMD_CHANGE;
  104. if (c != UCI_CMD_CHANGE)
  105. arg += 1;
  106. UCI_INTERNAL(uci_parse_ptr, ctx, ptr, arg);
  107. if (!ptr->section)
  108. goto error;
  109. if (ptr->flags & UCI_LOOKUP_EXTENDED)
  110. goto error;
  111. switch(c) {
  112. case UCI_CMD_REORDER:
  113. if (!ptr->value || ptr->option)
  114. goto error;
  115. break;
  116. case UCI_CMD_RENAME:
  117. if (!ptr->value || !uci_validate_name(ptr->value))
  118. goto error;
  119. break;
  120. case UCI_CMD_LIST_ADD:
  121. if (!ptr->option)
  122. goto error;
  123. case UCI_CMD_LIST_DEL:
  124. if (!ptr->option)
  125. goto error;
  126. }
  127. return c;
  128. error:
  129. UCI_THROW(ctx, UCI_ERR_INVAL);
  130. return 0;
  131. }
  132. static void uci_parse_delta_line(struct uci_context *ctx, struct uci_package *p)
  133. {
  134. struct uci_element *e = NULL;
  135. struct uci_ptr ptr;
  136. int cmd;
  137. cmd = uci_parse_delta_tuple(ctx, &ptr);
  138. if (strcmp(ptr.package, p->e.name) != 0)
  139. goto error;
  140. if (ctx->flags & UCI_FLAG_SAVED_DELTA)
  141. uci_add_delta(ctx, &p->saved_delta, cmd, ptr.section, ptr.option, ptr.value);
  142. switch(cmd) {
  143. case UCI_CMD_REORDER:
  144. uci_expand_ptr(ctx, &ptr, true);
  145. if (!ptr.s)
  146. UCI_THROW(ctx, UCI_ERR_NOTFOUND);
  147. UCI_INTERNAL(uci_reorder_section, ctx, ptr.s, strtoul(ptr.value, NULL, 10));
  148. break;
  149. case UCI_CMD_RENAME:
  150. UCI_INTERNAL(uci_rename, ctx, &ptr);
  151. break;
  152. case UCI_CMD_REMOVE:
  153. UCI_INTERNAL(uci_delete, ctx, &ptr);
  154. break;
  155. case UCI_CMD_LIST_ADD:
  156. UCI_INTERNAL(uci_add_list, ctx, &ptr);
  157. break;
  158. case UCI_CMD_LIST_DEL:
  159. UCI_INTERNAL(uci_del_list, ctx, &ptr);
  160. break;
  161. case UCI_CMD_ADD:
  162. case UCI_CMD_CHANGE:
  163. UCI_INTERNAL(uci_set, ctx, &ptr);
  164. e = ptr.last;
  165. if (!ptr.option && e && (cmd == UCI_CMD_ADD))
  166. uci_to_section(e)->anonymous = true;
  167. break;
  168. }
  169. return;
  170. error:
  171. UCI_THROW(ctx, UCI_ERR_PARSE);
  172. }
  173. /* returns the number of changes that were successfully parsed */
  174. static int uci_parse_delta(struct uci_context *ctx, FILE *stream, struct uci_package *p)
  175. {
  176. struct uci_parse_context *pctx;
  177. int changes = 0;
  178. /* make sure no memory from previous parse attempts is leaked */
  179. uci_cleanup(ctx);
  180. pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
  181. ctx->pctx = pctx;
  182. pctx->file = stream;
  183. while (!feof(pctx->file)) {
  184. pctx->pos = 0;
  185. uci_getln(ctx, 0);
  186. if (!pctx->buf[0])
  187. continue;
  188. /*
  189. * ignore parse errors in single lines, we want to preserve as much
  190. * delta as possible
  191. */
  192. UCI_TRAP_SAVE(ctx, error);
  193. uci_parse_delta_line(ctx, p);
  194. UCI_TRAP_RESTORE(ctx);
  195. changes++;
  196. error:
  197. continue;
  198. }
  199. /* no error happened, we can get rid of the parser context now */
  200. uci_cleanup(ctx);
  201. return changes;
  202. }
  203. /* returns the number of changes that were successfully parsed */
  204. static int uci_load_delta_file(struct uci_context *ctx, struct uci_package *p, char *filename, FILE **f, bool flush)
  205. {
  206. FILE *stream = NULL;
  207. int changes = 0;
  208. UCI_TRAP_SAVE(ctx, done);
  209. stream = uci_open_stream(ctx, filename, NULL, SEEK_SET, flush, false);
  210. if (p)
  211. changes = uci_parse_delta(ctx, stream, p);
  212. UCI_TRAP_RESTORE(ctx);
  213. done:
  214. if (f)
  215. *f = stream;
  216. else if (stream)
  217. uci_close_stream(stream);
  218. return changes;
  219. }
  220. /* returns the number of changes that were successfully parsed */
  221. __private int uci_load_delta(struct uci_context *ctx, struct uci_package *p, bool flush)
  222. {
  223. struct uci_element *e;
  224. char *filename = NULL;
  225. FILE *f = NULL;
  226. int changes = 0;
  227. if (!p->has_delta)
  228. return 0;
  229. uci_foreach_element(&ctx->delta_path, e) {
  230. if ((asprintf(&filename, "%s/%s", e->name, p->e.name) < 0) || !filename)
  231. UCI_THROW(ctx, UCI_ERR_MEM);
  232. uci_load_delta_file(ctx, p, filename, NULL, false);
  233. free(filename);
  234. }
  235. if ((asprintf(&filename, "%s/%s", ctx->savedir, p->e.name) < 0) || !filename)
  236. UCI_THROW(ctx, UCI_ERR_MEM);
  237. changes = uci_load_delta_file(ctx, p, filename, &f, flush);
  238. if (flush && f && (changes > 0)) {
  239. rewind(f);
  240. if (ftruncate(fileno(f), 0) < 0) {
  241. uci_close_stream(f);
  242. UCI_THROW(ctx, UCI_ERR_IO);
  243. }
  244. }
  245. free(filename);
  246. uci_close_stream(f);
  247. ctx->err = 0;
  248. return changes;
  249. }
  250. static void uci_filter_delta(struct uci_context *ctx, const char *name, const char *section, const char *option)
  251. {
  252. struct uci_parse_context *pctx;
  253. struct uci_element *e, *tmp;
  254. struct uci_list list;
  255. char *filename = NULL;
  256. struct uci_ptr ptr;
  257. FILE *f = NULL;
  258. uci_list_init(&list);
  259. uci_alloc_parse_context(ctx);
  260. pctx = ctx->pctx;
  261. if ((asprintf(&filename, "%s/%s", ctx->savedir, name) < 0) || !filename)
  262. UCI_THROW(ctx, UCI_ERR_MEM);
  263. UCI_TRAP_SAVE(ctx, done);
  264. f = uci_open_stream(ctx, filename, NULL, SEEK_SET, true, false);
  265. pctx->file = f;
  266. while (!feof(f)) {
  267. struct uci_element *e;
  268. pctx->pos = 0;
  269. uci_getln(ctx, 0);
  270. if (!pctx->buf[0])
  271. continue;
  272. /* NB: need to allocate the element before the call to
  273. * uci_parse_delta_tuple, otherwise the original string
  274. * gets modified before it is saved */
  275. e = uci_alloc_generic(ctx, UCI_TYPE_DELTA, pctx->buf, sizeof(struct uci_element));
  276. uci_list_add(&list, &e->list);
  277. uci_parse_delta_tuple(ctx, &ptr);
  278. if (section) {
  279. if (!ptr.section || (strcmp(section, ptr.section) != 0))
  280. continue;
  281. }
  282. if (option) {
  283. if (!ptr.option || (strcmp(option, ptr.option) != 0))
  284. continue;
  285. }
  286. /* match, drop this element again */
  287. uci_free_element(e);
  288. }
  289. /* rebuild the delta file */
  290. rewind(f);
  291. if (ftruncate(fileno(f), 0) < 0)
  292. UCI_THROW(ctx, UCI_ERR_IO);
  293. uci_foreach_element_safe(&list, tmp, e) {
  294. fprintf(f, "%s\n", e->name);
  295. uci_free_element(e);
  296. }
  297. UCI_TRAP_RESTORE(ctx);
  298. done:
  299. free(filename);
  300. uci_close_stream(pctx->file);
  301. uci_foreach_element_safe(&list, tmp, e) {
  302. uci_free_element(e);
  303. }
  304. uci_cleanup(ctx);
  305. }
  306. int uci_revert(struct uci_context *ctx, struct uci_ptr *ptr)
  307. {
  308. char *package = NULL;
  309. char *section = NULL;
  310. char *option = NULL;
  311. UCI_HANDLE_ERR(ctx);
  312. uci_expand_ptr(ctx, ptr, false);
  313. UCI_ASSERT(ctx, ptr->p->has_delta);
  314. /*
  315. * - flush unwritten changes
  316. * - save the package name
  317. * - unload the package
  318. * - filter the delta
  319. * - reload the package
  320. */
  321. UCI_TRAP_SAVE(ctx, error);
  322. UCI_INTERNAL(uci_save, ctx, ptr->p);
  323. /* NB: need to clone package, section and option names,
  324. * as they may get freed on uci_free_package() */
  325. package = uci_strdup(ctx, ptr->p->e.name);
  326. if (ptr->section)
  327. section = uci_strdup(ctx, ptr->section);
  328. if (ptr->option)
  329. option = uci_strdup(ctx, ptr->option);
  330. uci_free_package(&ptr->p);
  331. uci_filter_delta(ctx, package, section, option);
  332. UCI_INTERNAL(uci_load, ctx, package, &ptr->p);
  333. UCI_TRAP_RESTORE(ctx);
  334. ctx->err = 0;
  335. error:
  336. free(package);
  337. free(section);
  338. free(option);
  339. if (ctx->err)
  340. UCI_THROW(ctx, ctx->err);
  341. return 0;
  342. }
  343. int uci_save(struct uci_context *ctx, struct uci_package *p)
  344. {
  345. FILE *f = NULL;
  346. char *filename = NULL;
  347. struct uci_element *e, *tmp;
  348. struct stat statbuf;
  349. UCI_HANDLE_ERR(ctx);
  350. UCI_ASSERT(ctx, p != NULL);
  351. /*
  352. * if the config file was outside of the /etc/config path,
  353. * don't save the delta to a file, update the real file
  354. * directly.
  355. * does not modify the uci_package pointer
  356. */
  357. if (!p->has_delta)
  358. return uci_commit(ctx, &p, false);
  359. if (uci_list_empty(&p->delta))
  360. return 0;
  361. if (stat(ctx->savedir, &statbuf) < 0) {
  362. if (stat(ctx->confdir, &statbuf) == 0) {
  363. mkdir(ctx->savedir, statbuf.st_mode);
  364. } else {
  365. mkdir(ctx->savedir, UCI_DIRMODE);
  366. }
  367. } else if ((statbuf.st_mode & S_IFMT) != S_IFDIR) {
  368. UCI_THROW(ctx, UCI_ERR_IO);
  369. }
  370. if ((asprintf(&filename, "%s/%s", ctx->savedir, p->e.name) < 0) || !filename)
  371. UCI_THROW(ctx, UCI_ERR_MEM);
  372. ctx->err = 0;
  373. UCI_TRAP_SAVE(ctx, done);
  374. f = uci_open_stream(ctx, filename, NULL, SEEK_END, true, true);
  375. UCI_TRAP_RESTORE(ctx);
  376. uci_foreach_element_safe(&p->delta, tmp, e) {
  377. struct uci_delta *h = uci_to_delta(e);
  378. char prefix[2] = {0, 0};
  379. if (h->cmd <= __UCI_CMD_LAST)
  380. prefix[0] = uci_command_char[h->cmd];
  381. fprintf(f, "%s%s.%s", prefix, p->e.name, h->section);
  382. if (e->name)
  383. fprintf(f, ".%s", e->name);
  384. if (h->cmd == UCI_CMD_REMOVE && !h->value)
  385. fprintf(f, "\n");
  386. else {
  387. int i;
  388. fprintf(f, "='");
  389. for (i = 0; h->value[i]; i++) {
  390. unsigned char c = h->value[i];
  391. if (c != '\'')
  392. fputc(c, f);
  393. else
  394. fprintf(f, "'\\''");
  395. }
  396. fprintf(f, "'\n");
  397. }
  398. uci_free_delta(h);
  399. }
  400. done:
  401. uci_close_stream(f);
  402. free(filename);
  403. if (ctx->err)
  404. UCI_THROW(ctx, ctx->err);
  405. return 0;
  406. }