delta.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. case '_':
  102. c = UCI_CMD_LIST_DEL;
  103. break;
  104. }
  105. if (c != UCI_CMD_CHANGE)
  106. *buf += 1;
  107. UCI_INTERNAL(uci_parse_ptr, ctx, ptr, *buf);
  108. if (!ptr->section)
  109. goto error;
  110. if (ptr->flags & UCI_LOOKUP_EXTENDED)
  111. goto error;
  112. switch(c) {
  113. case UCI_CMD_REORDER:
  114. if (!ptr->value || ptr->option)
  115. goto error;
  116. break;
  117. case UCI_CMD_RENAME:
  118. if (!ptr->value || !uci_validate_name(ptr->value))
  119. goto error;
  120. break;
  121. case UCI_CMD_LIST_ADD:
  122. if (!ptr->option)
  123. goto error;
  124. case UCI_CMD_LIST_DEL:
  125. if (!ptr->option)
  126. goto error;
  127. }
  128. return c;
  129. error:
  130. UCI_THROW(ctx, UCI_ERR_INVAL);
  131. return 0;
  132. }
  133. static void uci_parse_delta_line(struct uci_context *ctx, struct uci_package *p, char *buf)
  134. {
  135. struct uci_element *e = NULL;
  136. struct uci_ptr ptr;
  137. int cmd;
  138. cmd = uci_parse_delta_tuple(ctx, &buf, &ptr);
  139. if (strcmp(ptr.package, p->e.name) != 0)
  140. goto error;
  141. if (ctx->flags & UCI_FLAG_SAVED_DELTA)
  142. uci_add_delta(ctx, &p->saved_delta, cmd, ptr.section, ptr.option, ptr.value);
  143. switch(cmd) {
  144. case UCI_CMD_REORDER:
  145. uci_expand_ptr(ctx, &ptr, true);
  146. if (!ptr.s)
  147. UCI_THROW(ctx, UCI_ERR_NOTFOUND);
  148. UCI_INTERNAL(uci_reorder_section, ctx, ptr.s, strtoul(ptr.value, NULL, 10));
  149. break;
  150. case UCI_CMD_RENAME:
  151. UCI_INTERNAL(uci_rename, ctx, &ptr);
  152. break;
  153. case UCI_CMD_REMOVE:
  154. UCI_INTERNAL(uci_delete, ctx, &ptr);
  155. break;
  156. case UCI_CMD_LIST_ADD:
  157. UCI_INTERNAL(uci_add_list, ctx, &ptr);
  158. break;
  159. case UCI_CMD_LIST_DEL:
  160. UCI_INTERNAL(uci_del_list, ctx, &ptr);
  161. break;
  162. case UCI_CMD_ADD:
  163. case UCI_CMD_CHANGE:
  164. UCI_INTERNAL(uci_set, ctx, &ptr);
  165. e = ptr.last;
  166. if (!ptr.option && e && (cmd == UCI_CMD_ADD))
  167. uci_to_section(e)->anonymous = true;
  168. break;
  169. }
  170. return;
  171. error:
  172. UCI_THROW(ctx, UCI_ERR_PARSE);
  173. }
  174. /* returns the number of changes that were successfully parsed */
  175. static int uci_parse_delta(struct uci_context *ctx, FILE *stream, struct uci_package *p)
  176. {
  177. struct uci_parse_context *pctx;
  178. int changes = 0;
  179. /* make sure no memory from previous parse attempts is leaked */
  180. uci_cleanup(ctx);
  181. pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
  182. ctx->pctx = pctx;
  183. pctx->file = stream;
  184. while (!feof(pctx->file)) {
  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, pctx->buf);
  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, 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. if (filename)
  246. free(filename);
  247. uci_close_stream(f);
  248. ctx->err = 0;
  249. return changes;
  250. }
  251. static void uci_filter_delta(struct uci_context *ctx, const char *name, const char *section, const char *option)
  252. {
  253. struct uci_parse_context *pctx;
  254. struct uci_element *e, *tmp;
  255. struct uci_list list;
  256. char *filename = NULL;
  257. struct uci_ptr ptr;
  258. FILE *f = NULL;
  259. uci_list_init(&list);
  260. uci_alloc_parse_context(ctx);
  261. pctx = ctx->pctx;
  262. if ((asprintf(&filename, "%s/%s", ctx->savedir, name) < 0) || !filename)
  263. UCI_THROW(ctx, UCI_ERR_MEM);
  264. UCI_TRAP_SAVE(ctx, done);
  265. f = uci_open_stream(ctx, filename, SEEK_SET, true, false);
  266. pctx->file = f;
  267. while (!feof(f)) {
  268. struct uci_element *e;
  269. char *buf;
  270. uci_getln(ctx, 0);
  271. buf = pctx->buf;
  272. if (!buf[0])
  273. continue;
  274. /* NB: need to allocate the element before the call to
  275. * uci_parse_delta_tuple, otherwise the original string
  276. * gets modified before it is saved */
  277. e = uci_alloc_generic(ctx, UCI_TYPE_DELTA, pctx->buf, sizeof(struct uci_element));
  278. uci_list_add(&list, &e->list);
  279. uci_parse_delta_tuple(ctx, &buf, &ptr);
  280. if (section) {
  281. if (!ptr.section || (strcmp(section, ptr.section) != 0))
  282. continue;
  283. }
  284. if (option) {
  285. if (!ptr.option || (strcmp(option, ptr.option) != 0))
  286. continue;
  287. }
  288. /* match, drop this element again */
  289. uci_free_element(e);
  290. }
  291. /* rebuild the delta file */
  292. rewind(f);
  293. if (ftruncate(fileno(f), 0) < 0)
  294. UCI_THROW(ctx, UCI_ERR_IO);
  295. uci_foreach_element_safe(&list, tmp, e) {
  296. fprintf(f, "%s\n", e->name);
  297. uci_free_element(e);
  298. }
  299. UCI_TRAP_RESTORE(ctx);
  300. done:
  301. if (filename)
  302. free(filename);
  303. uci_close_stream(pctx->file);
  304. uci_foreach_element_safe(&list, tmp, e) {
  305. uci_free_element(e);
  306. }
  307. uci_cleanup(ctx);
  308. }
  309. int uci_revert(struct uci_context *ctx, struct uci_ptr *ptr)
  310. {
  311. char *package = NULL;
  312. char *section = NULL;
  313. char *option = NULL;
  314. UCI_HANDLE_ERR(ctx);
  315. uci_expand_ptr(ctx, ptr, false);
  316. UCI_ASSERT(ctx, ptr->p->has_delta);
  317. /*
  318. * - flush unwritten changes
  319. * - save the package name
  320. * - unload the package
  321. * - filter the delta
  322. * - reload the package
  323. */
  324. UCI_TRAP_SAVE(ctx, error);
  325. UCI_INTERNAL(uci_save, ctx, ptr->p);
  326. /* NB: need to clone package, section and option names,
  327. * as they may get freed on uci_free_package() */
  328. package = uci_strdup(ctx, ptr->p->e.name);
  329. if (ptr->section)
  330. section = uci_strdup(ctx, ptr->section);
  331. if (ptr->option)
  332. option = uci_strdup(ctx, ptr->option);
  333. uci_free_package(&ptr->p);
  334. uci_filter_delta(ctx, package, section, option);
  335. UCI_INTERNAL(uci_load, ctx, package, &ptr->p);
  336. UCI_TRAP_RESTORE(ctx);
  337. ctx->err = 0;
  338. error:
  339. if (package)
  340. free(package);
  341. if (section)
  342. free(section);
  343. if (option)
  344. free(option);
  345. if (ctx->err)
  346. UCI_THROW(ctx, ctx->err);
  347. return 0;
  348. }
  349. int uci_save(struct uci_context *ctx, struct uci_package *p)
  350. {
  351. FILE *f = NULL;
  352. char *filename = NULL;
  353. struct uci_element *e, *tmp;
  354. struct stat statbuf;
  355. UCI_HANDLE_ERR(ctx);
  356. UCI_ASSERT(ctx, p != NULL);
  357. /*
  358. * if the config file was outside of the /etc/config path,
  359. * don't save the delta to a file, update the real file
  360. * directly.
  361. * does not modify the uci_package pointer
  362. */
  363. if (!p->has_delta)
  364. return uci_commit(ctx, &p, false);
  365. if (uci_list_empty(&p->delta))
  366. return 0;
  367. if (stat(ctx->savedir, &statbuf) < 0)
  368. mkdir(ctx->savedir, UCI_DIRMODE);
  369. else if ((statbuf.st_mode & S_IFMT) != S_IFDIR)
  370. UCI_THROW(ctx, UCI_ERR_IO);
  371. if ((asprintf(&filename, "%s/%s", ctx->savedir, p->e.name) < 0) || !filename)
  372. UCI_THROW(ctx, UCI_ERR_MEM);
  373. uci_foreach_element(&ctx->hooks, tmp) {
  374. struct uci_hook *hook = uci_to_hook(tmp);
  375. if (!hook->ops->set)
  376. continue;
  377. uci_foreach_element(&p->delta, e) {
  378. hook->ops->set(hook->ops, p, uci_to_delta(e));
  379. }
  380. }
  381. ctx->err = 0;
  382. UCI_TRAP_SAVE(ctx, done);
  383. f = uci_open_stream(ctx, filename, SEEK_END, true, true);
  384. UCI_TRAP_RESTORE(ctx);
  385. uci_foreach_element_safe(&p->delta, tmp, e) {
  386. struct uci_delta *h = uci_to_delta(e);
  387. char *prefix = "";
  388. switch(h->cmd) {
  389. case UCI_CMD_REMOVE:
  390. prefix = "-";
  391. break;
  392. case UCI_CMD_RENAME:
  393. prefix = "@";
  394. break;
  395. case UCI_CMD_ADD:
  396. prefix = "+";
  397. break;
  398. case UCI_CMD_REORDER:
  399. prefix = "^";
  400. break;
  401. case UCI_CMD_LIST_ADD:
  402. prefix = "|";
  403. break;
  404. case UCI_CMD_LIST_DEL:
  405. prefix = "_";
  406. break;
  407. default:
  408. break;
  409. }
  410. fprintf(f, "%s%s.%s", prefix, p->e.name, h->section);
  411. if (e->name)
  412. fprintf(f, ".%s", e->name);
  413. if (h->cmd == UCI_CMD_REMOVE && !h->value)
  414. fprintf(f, "\n");
  415. else
  416. fprintf(f, "=%s\n", h->value);
  417. uci_free_delta(h);
  418. }
  419. done:
  420. uci_close_stream(f);
  421. if (filename)
  422. free(filename);
  423. if (ctx->err)
  424. UCI_THROW(ctx, ctx->err);
  425. return 0;
  426. }