history.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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 General Public License for more details.
  13. */
  14. /*
  15. * This file contains the code for handling uci config history 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. /* record a change that was done to a package */
  27. void
  28. uci_add_history(struct uci_context *ctx, struct uci_list *list, int cmd, char *section, char *option, char *value)
  29. {
  30. struct uci_history *h;
  31. int size = strlen(section) + 1;
  32. char *ptr;
  33. if (value)
  34. size += strlen(value) + 1;
  35. h = uci_alloc_element(ctx, history, option, size);
  36. ptr = uci_dataptr(h);
  37. h->cmd = cmd;
  38. h->section = strcpy(ptr, section);
  39. if (value) {
  40. ptr += strlen(ptr) + 1;
  41. h->value = strcpy(ptr, value);
  42. }
  43. uci_list_add(list, &h->e.list);
  44. }
  45. void
  46. uci_free_history(struct uci_history *h)
  47. {
  48. if (!h)
  49. return;
  50. if ((h->section != NULL) &&
  51. (h->section != uci_dataptr(h))) {
  52. free(h->section);
  53. free(h->value);
  54. }
  55. uci_free_element(&h->e);
  56. }
  57. int uci_set_savedir(struct uci_context *ctx, const char *dir)
  58. {
  59. char *sdir;
  60. UCI_HANDLE_ERR(ctx);
  61. UCI_ASSERT(ctx, dir != NULL);
  62. sdir = uci_strdup(ctx, dir);
  63. if (ctx->savedir != uci_savedir)
  64. free(ctx->savedir);
  65. ctx->savedir = sdir;
  66. return 0;
  67. }
  68. int uci_add_history_path(struct uci_context *ctx, const char *dir)
  69. {
  70. struct uci_element *e;
  71. UCI_HANDLE_ERR(ctx);
  72. UCI_ASSERT(ctx, dir != NULL);
  73. e = uci_alloc_generic(ctx, UCI_TYPE_PATH, dir, sizeof(struct uci_element));
  74. uci_list_add(&ctx->history_path, &e->list);
  75. return 0;
  76. }
  77. static inline void uci_parse_history_tuple(struct uci_context *ctx, char **buf, char **package, char **section, char **option, char **value, int *cmd)
  78. {
  79. int c = UCI_CMD_CHANGE;
  80. if (**buf == '-') {
  81. c = UCI_CMD_REMOVE;
  82. *buf += 1;
  83. } else if (**buf == '@') {
  84. c = UCI_CMD_RENAME;
  85. *buf += 1;
  86. } else if (**buf == '+') {
  87. /* UCI_CMD_ADD is used for anonymous sections */
  88. c = UCI_CMD_ADD;
  89. *buf += 1;
  90. }
  91. if (cmd)
  92. *cmd = c;
  93. UCI_INTERNAL(uci_parse_tuple, ctx, *buf, package, section, option, value);
  94. if (!*section[0])
  95. UCI_THROW(ctx, UCI_ERR_PARSE);
  96. }
  97. static void uci_parse_history_line(struct uci_context *ctx, struct uci_package *p, char *buf)
  98. {
  99. struct uci_element *e = NULL;
  100. bool delete = false;
  101. bool rename = false;
  102. char *package = NULL;
  103. char *section = NULL;
  104. char *option = NULL;
  105. char *value = NULL;
  106. int cmd;
  107. uci_parse_history_tuple(ctx, &buf, &package, &section, &option, &value, &cmd);
  108. if (!package || (strcmp(package, p->e.name) != 0))
  109. goto error;
  110. if (!uci_validate_name(section))
  111. goto error;
  112. if (option && !uci_validate_name(option))
  113. goto error;
  114. if (rename && !uci_validate_str(value, (option || delete)))
  115. goto error;
  116. if (ctx->flags & UCI_FLAG_SAVED_HISTORY)
  117. uci_add_history(ctx, &p->saved_history, cmd, section, option, value);
  118. switch(cmd) {
  119. case UCI_CMD_RENAME:
  120. UCI_INTERNAL(uci_rename, ctx, p, section, option, value);
  121. break;
  122. case UCI_CMD_REMOVE:
  123. UCI_INTERNAL(uci_delete, ctx, p, section, option);
  124. break;
  125. case UCI_CMD_ADD:
  126. case UCI_CMD_CHANGE:
  127. UCI_INTERNAL(uci_set, ctx, p, section, option, value, &e);
  128. if (!option && e && (cmd == UCI_CMD_ADD))
  129. uci_to_section(e)->anonymous = true;
  130. break;
  131. }
  132. return;
  133. error:
  134. UCI_THROW(ctx, UCI_ERR_PARSE);
  135. }
  136. /* returns the number of changes that were successfully parsed */
  137. static int uci_parse_history(struct uci_context *ctx, FILE *stream, struct uci_package *p)
  138. {
  139. struct uci_parse_context *pctx;
  140. int changes = 0;
  141. /* make sure no memory from previous parse attempts is leaked */
  142. uci_cleanup(ctx);
  143. pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
  144. ctx->pctx = pctx;
  145. pctx->file = stream;
  146. while (!feof(pctx->file)) {
  147. uci_getln(ctx, 0);
  148. if (!pctx->buf[0])
  149. continue;
  150. /*
  151. * ignore parse errors in single lines, we want to preserve as much
  152. * history as possible
  153. */
  154. UCI_TRAP_SAVE(ctx, error);
  155. uci_parse_history_line(ctx, p, pctx->buf);
  156. UCI_TRAP_RESTORE(ctx);
  157. changes++;
  158. error:
  159. continue;
  160. }
  161. /* no error happened, we can get rid of the parser context now */
  162. uci_cleanup(ctx);
  163. return changes;
  164. }
  165. /* returns the number of changes that were successfully parsed */
  166. static int uci_load_history_file(struct uci_context *ctx, struct uci_package *p, char *filename, FILE **f, bool flush)
  167. {
  168. FILE *stream = NULL;
  169. int changes = 0;
  170. UCI_TRAP_SAVE(ctx, done);
  171. stream = uci_open_stream(ctx, filename, SEEK_SET, flush, false);
  172. if (p)
  173. changes = uci_parse_history(ctx, stream, p);
  174. UCI_TRAP_RESTORE(ctx);
  175. done:
  176. if (f)
  177. *f = stream;
  178. else if (stream)
  179. uci_close_stream(stream);
  180. return changes;
  181. }
  182. /* returns the number of changes that were successfully parsed */
  183. static int uci_load_history(struct uci_context *ctx, struct uci_package *p, bool flush)
  184. {
  185. struct uci_element *e;
  186. char *filename = NULL;
  187. FILE *f = NULL;
  188. int changes = 0;
  189. if (!p->has_history)
  190. return 0;
  191. uci_foreach_element(&ctx->history_path, e) {
  192. if ((asprintf(&filename, "%s/%s", e->name, p->e.name) < 0) || !filename)
  193. UCI_THROW(ctx, UCI_ERR_MEM);
  194. uci_load_history_file(ctx, p, filename, NULL, false);
  195. free(filename);
  196. }
  197. if ((asprintf(&filename, "%s/%s", ctx->savedir, p->e.name) < 0) || !filename)
  198. UCI_THROW(ctx, UCI_ERR_MEM);
  199. changes = uci_load_history_file(ctx, p, filename, &f, flush);
  200. if (flush && f && (changes > 0)) {
  201. rewind(f);
  202. ftruncate(fileno(f), 0);
  203. }
  204. if (filename)
  205. free(filename);
  206. uci_close_stream(f);
  207. ctx->err = 0;
  208. return changes;
  209. }
  210. static void uci_filter_history(struct uci_context *ctx, const char *name, char *section, char *option)
  211. {
  212. struct uci_parse_context *pctx;
  213. struct uci_element *e, *tmp;
  214. struct uci_list list;
  215. char *filename = NULL;
  216. char *p = NULL;
  217. char *s = NULL;
  218. char *o = NULL;
  219. char *v = NULL;
  220. FILE *f = NULL;
  221. uci_list_init(&list);
  222. uci_alloc_parse_context(ctx);
  223. pctx = ctx->pctx;
  224. if ((asprintf(&filename, "%s/%s", ctx->savedir, name) < 0) || !filename)
  225. UCI_THROW(ctx, UCI_ERR_MEM);
  226. UCI_TRAP_SAVE(ctx, done);
  227. f = uci_open_stream(ctx, filename, SEEK_SET, true, false);
  228. pctx->file = f;
  229. while (!feof(f)) {
  230. struct uci_element *e;
  231. char *buf;
  232. uci_getln(ctx, 0);
  233. buf = pctx->buf;
  234. if (!buf[0])
  235. continue;
  236. /* NB: need to allocate the element before the call to
  237. * uci_parse_history_tuple, otherwise the original string
  238. * gets modified before it is saved */
  239. e = uci_alloc_generic(ctx, UCI_TYPE_HISTORY, pctx->buf, sizeof(struct uci_element));
  240. uci_list_add(&list, &e->list);
  241. uci_parse_history_tuple(ctx, &buf, &p, &s, &o, &v, NULL);
  242. if (section) {
  243. if (!s || (strcmp(section, s) != 0))
  244. continue;
  245. }
  246. if (option) {
  247. if (!o || (strcmp(option, o) != 0))
  248. continue;
  249. }
  250. /* match, drop this element again */
  251. uci_free_element(e);
  252. }
  253. /* rebuild the history file */
  254. rewind(f);
  255. ftruncate(fileno(f), 0);
  256. uci_foreach_element_safe(&list, tmp, e) {
  257. fprintf(f, "%s\n", e->name);
  258. uci_free_element(e);
  259. }
  260. UCI_TRAP_RESTORE(ctx);
  261. done:
  262. if (filename)
  263. free(filename);
  264. uci_close_stream(f);
  265. uci_foreach_element_safe(&list, tmp, e) {
  266. uci_free_element(e);
  267. }
  268. uci_cleanup(ctx);
  269. }
  270. int uci_revert(struct uci_context *ctx, struct uci_package **pkg, char *section, char *option)
  271. {
  272. struct uci_package *p;
  273. char *name = NULL;
  274. UCI_HANDLE_ERR(ctx);
  275. UCI_ASSERT(ctx, pkg != NULL);
  276. p = *pkg;
  277. UCI_ASSERT(ctx, p != NULL);
  278. UCI_ASSERT(ctx, p->has_history);
  279. /*
  280. * - flush unwritten changes
  281. * - save the package name
  282. * - unload the package
  283. * - filter the history
  284. * - reload the package
  285. */
  286. UCI_TRAP_SAVE(ctx, error);
  287. UCI_INTERNAL(uci_save, ctx, p);
  288. name = uci_strdup(ctx, p->e.name);
  289. *pkg = NULL;
  290. uci_free_package(&p);
  291. uci_filter_history(ctx, name, section, option);
  292. UCI_INTERNAL(uci_load, ctx, name, &p);
  293. UCI_TRAP_RESTORE(ctx);
  294. ctx->err = 0;
  295. error:
  296. if (name)
  297. free(name);
  298. if (ctx->err)
  299. UCI_THROW(ctx, ctx->err);
  300. return 0;
  301. }
  302. int uci_save(struct uci_context *ctx, struct uci_package *p)
  303. {
  304. FILE *f = NULL;
  305. char *filename = NULL;
  306. struct uci_element *e, *tmp;
  307. struct stat statbuf;
  308. UCI_HANDLE_ERR(ctx);
  309. UCI_ASSERT(ctx, p != NULL);
  310. /*
  311. * if the config file was outside of the /etc/config path,
  312. * don't save the history to a file, update the real file
  313. * directly.
  314. * does not modify the uci_package pointer
  315. */
  316. if (!p->has_history)
  317. return uci_commit(ctx, &p, false);
  318. if (uci_list_empty(&p->history))
  319. return 0;
  320. if (stat(ctx->savedir, &statbuf) < 0)
  321. mkdir(ctx->savedir, UCI_DIRMODE);
  322. else if ((statbuf.st_mode & S_IFMT) != S_IFDIR)
  323. UCI_THROW(ctx, UCI_ERR_IO);
  324. if ((asprintf(&filename, "%s/%s", ctx->savedir, p->e.name) < 0) || !filename)
  325. UCI_THROW(ctx, UCI_ERR_MEM);
  326. ctx->err = 0;
  327. UCI_TRAP_SAVE(ctx, done);
  328. f = uci_open_stream(ctx, filename, SEEK_END, true, true);
  329. UCI_TRAP_RESTORE(ctx);
  330. uci_foreach_element_safe(&p->history, tmp, e) {
  331. struct uci_history *h = uci_to_history(e);
  332. switch(h->cmd) {
  333. case UCI_CMD_REMOVE:
  334. fprintf(f, "-");
  335. break;
  336. case UCI_CMD_RENAME:
  337. fprintf(f, "@");
  338. break;
  339. case UCI_CMD_ADD:
  340. fprintf(f, "+");
  341. break;
  342. default:
  343. break;
  344. }
  345. fprintf(f, "%s.%s", p->e.name, h->section);
  346. if (e->name)
  347. fprintf(f, ".%s", e->name);
  348. if (h->cmd == UCI_CMD_REMOVE)
  349. fprintf(f, "\n");
  350. else
  351. fprintf(f, "=%s\n", h->value);
  352. uci_free_history(h);
  353. }
  354. done:
  355. uci_close_stream(f);
  356. if (filename)
  357. free(filename);
  358. if (ctx->err)
  359. UCI_THROW(ctx, ctx->err);
  360. return 0;
  361. }