delta.c 11 KB

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