delta.c 11 KB

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