delta.c 12 KB

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