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. 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. 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, SEEK_SET, true, false);
  265. pctx->file = f;
  266. while (!feof(f)) {
  267. struct uci_element *e;
  268. char *buf;
  269. uci_getln(ctx, 0);
  270. buf = pctx->buf;
  271. if (!buf[0])
  272. continue;
  273. /* NB: need to allocate the element before the call to
  274. * uci_parse_delta_tuple, otherwise the original string
  275. * gets modified before it is saved */
  276. e = uci_alloc_generic(ctx, UCI_TYPE_DELTA, pctx->buf, sizeof(struct uci_element));
  277. uci_list_add(&list, &e->list);
  278. uci_parse_delta_tuple(ctx, &buf, &ptr);
  279. if (section) {
  280. if (!ptr.section || (strcmp(section, ptr.section) != 0))
  281. continue;
  282. }
  283. if (option) {
  284. if (!ptr.option || (strcmp(option, ptr.option) != 0))
  285. continue;
  286. }
  287. /* match, drop this element again */
  288. uci_free_element(e);
  289. }
  290. /* rebuild the delta file */
  291. rewind(f);
  292. if (ftruncate(fileno(f), 0) < 0)
  293. UCI_THROW(ctx, UCI_ERR_IO);
  294. uci_foreach_element_safe(&list, tmp, e) {
  295. fprintf(f, "%s\n", e->name);
  296. uci_free_element(e);
  297. }
  298. UCI_TRAP_RESTORE(ctx);
  299. done:
  300. free(filename);
  301. uci_close_stream(pctx->file);
  302. uci_foreach_element_safe(&list, tmp, e) {
  303. uci_free_element(e);
  304. }
  305. uci_cleanup(ctx);
  306. }
  307. int uci_revert(struct uci_context *ctx, struct uci_ptr *ptr)
  308. {
  309. char *package = NULL;
  310. char *section = NULL;
  311. char *option = NULL;
  312. UCI_HANDLE_ERR(ctx);
  313. uci_expand_ptr(ctx, ptr, false);
  314. UCI_ASSERT(ctx, ptr->p->has_delta);
  315. /*
  316. * - flush unwritten changes
  317. * - save the package name
  318. * - unload the package
  319. * - filter the delta
  320. * - reload the package
  321. */
  322. UCI_TRAP_SAVE(ctx, error);
  323. UCI_INTERNAL(uci_save, ctx, ptr->p);
  324. /* NB: need to clone package, section and option names,
  325. * as they may get freed on uci_free_package() */
  326. package = uci_strdup(ctx, ptr->p->e.name);
  327. if (ptr->section)
  328. section = uci_strdup(ctx, ptr->section);
  329. if (ptr->option)
  330. option = uci_strdup(ctx, ptr->option);
  331. uci_free_package(&ptr->p);
  332. uci_filter_delta(ctx, package, section, option);
  333. UCI_INTERNAL(uci_load, ctx, package, &ptr->p);
  334. UCI_TRAP_RESTORE(ctx);
  335. ctx->err = 0;
  336. error:
  337. free(package);
  338. free(section);
  339. free(option);
  340. if (ctx->err)
  341. UCI_THROW(ctx, ctx->err);
  342. return 0;
  343. }
  344. int uci_save(struct uci_context *ctx, struct uci_package *p)
  345. {
  346. FILE *f = NULL;
  347. char *filename = NULL;
  348. struct uci_element *e, *tmp;
  349. struct stat statbuf;
  350. UCI_HANDLE_ERR(ctx);
  351. UCI_ASSERT(ctx, p != NULL);
  352. /*
  353. * if the config file was outside of the /etc/config path,
  354. * don't save the delta to a file, update the real file
  355. * directly.
  356. * does not modify the uci_package pointer
  357. */
  358. if (!p->has_delta)
  359. return uci_commit(ctx, &p, false);
  360. if (uci_list_empty(&p->delta))
  361. return 0;
  362. if (stat(ctx->savedir, &statbuf) < 0) {
  363. if (stat(ctx->confdir, &statbuf) == 0) {
  364. mkdir(ctx->savedir, statbuf.st_mode);
  365. } else {
  366. mkdir(ctx->savedir, UCI_DIRMODE);
  367. }
  368. } else if ((statbuf.st_mode & S_IFMT) != S_IFDIR) {
  369. UCI_THROW(ctx, UCI_ERR_IO);
  370. }
  371. if ((asprintf(&filename, "%s/%s", ctx->savedir, p->e.name) < 0) || !filename)
  372. UCI_THROW(ctx, UCI_ERR_MEM);
  373. ctx->err = 0;
  374. UCI_TRAP_SAVE(ctx, done);
  375. f = uci_open_stream(ctx, filename, SEEK_END, true, true);
  376. UCI_TRAP_RESTORE(ctx);
  377. uci_foreach_element_safe(&p->delta, tmp, e) {
  378. struct uci_delta *h = uci_to_delta(e);
  379. char *prefix = "";
  380. switch(h->cmd) {
  381. case UCI_CMD_REMOVE:
  382. prefix = "-";
  383. break;
  384. case UCI_CMD_RENAME:
  385. prefix = "@";
  386. break;
  387. case UCI_CMD_ADD:
  388. prefix = "+";
  389. break;
  390. case UCI_CMD_REORDER:
  391. prefix = "^";
  392. break;
  393. case UCI_CMD_LIST_ADD:
  394. prefix = "|";
  395. break;
  396. case UCI_CMD_LIST_DEL:
  397. prefix = "~";
  398. break;
  399. default:
  400. break;
  401. }
  402. fprintf(f, "%s%s.%s", prefix, p->e.name, h->section);
  403. if (e->name)
  404. fprintf(f, ".%s", e->name);
  405. if (h->cmd == UCI_CMD_REMOVE && !h->value)
  406. fprintf(f, "\n");
  407. else
  408. fprintf(f, "=%s\n", h->value);
  409. uci_free_delta(h);
  410. }
  411. done:
  412. uci_close_stream(f);
  413. free(filename);
  414. if (ctx->err)
  415. UCI_THROW(ctx, ctx->err);
  416. return 0;
  417. }