delta.c 11 KB

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