2
0

history.c 10.0 KB

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