delta.c 12 KB

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