list.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. #include <glob.h>
  15. /* initialize a list head/item */
  16. static inline void uci_list_init(struct uci_list *ptr)
  17. {
  18. ptr->prev = ptr;
  19. ptr->next = ptr;
  20. }
  21. /* inserts a new list entry after a given entry */
  22. static inline void uci_list_insert(struct uci_list *list, struct uci_list *ptr)
  23. {
  24. list->next->prev = ptr;
  25. ptr->prev = list;
  26. ptr->next = list->next;
  27. list->next = ptr;
  28. }
  29. /* inserts a new list entry at the tail of the list */
  30. static inline void uci_list_add(struct uci_list *head, struct uci_list *ptr)
  31. {
  32. /* NB: head->prev points at the tail */
  33. uci_list_insert(head->prev, ptr);
  34. }
  35. static inline void uci_list_del(struct uci_list *ptr)
  36. {
  37. struct uci_list *next, *prev;
  38. next = ptr->next;
  39. prev = ptr->prev;
  40. prev->next = next;
  41. next->prev = prev;
  42. uci_list_init(ptr);
  43. }
  44. /*
  45. * uci_alloc_generic allocates a new uci_element with payload
  46. * payload is appended to the struct to save memory and reduce fragmentation
  47. */
  48. static struct uci_element *
  49. uci_alloc_generic(struct uci_context *ctx, int type, const char *name, int size)
  50. {
  51. struct uci_element *e;
  52. int datalen = size;
  53. void *ptr;
  54. ptr = uci_malloc(ctx, datalen);
  55. e = (struct uci_element *) ptr;
  56. e->type = type;
  57. if (name) {
  58. UCI_TRAP_SAVE(ctx, error);
  59. e->name = uci_strdup(ctx, name);
  60. UCI_TRAP_RESTORE(ctx);
  61. }
  62. uci_list_init(&e->list);
  63. goto done;
  64. error:
  65. free(ptr);
  66. UCI_THROW(ctx, ctx->err);
  67. done:
  68. return e;
  69. }
  70. static void
  71. uci_free_element(struct uci_element *e)
  72. {
  73. if (e->name)
  74. free(e->name);
  75. if (!uci_list_empty(&e->list))
  76. uci_list_del(&e->list);
  77. free(e);
  78. }
  79. static struct uci_option *
  80. uci_alloc_option(struct uci_section *s, const char *name, const char *value)
  81. {
  82. struct uci_package *p = s->package;
  83. struct uci_context *ctx = p->ctx;
  84. struct uci_option *o;
  85. o = uci_alloc_element(ctx, option, name, strlen(value) + 1);
  86. o->value = uci_dataptr(o);
  87. o->section = s;
  88. strcpy(o->value, value);
  89. uci_list_add(&s->options, &o->e.list);
  90. return o;
  91. }
  92. static inline void
  93. uci_free_option(struct uci_option *o)
  94. {
  95. if ((o->value != uci_dataptr(o)) &&
  96. (o->value != NULL))
  97. free(o->value);
  98. uci_free_element(&o->e);
  99. }
  100. /* fix up an unnamed section, e.g. after adding options to it */
  101. static void uci_fixup_section(struct uci_context *ctx, struct uci_section *s)
  102. {
  103. unsigned int hash = ~0;
  104. struct uci_element *e;
  105. char buf[16];
  106. if (!s || s->e.name)
  107. return;
  108. /*
  109. * Generate a name for unnamed sections. This is used as reference
  110. * when locating or updating the section from apps/scripts.
  111. * To make multiple concurrent versions somewhat safe for updating,
  112. * the name is generated from a hash of its type and name/value
  113. * pairs of its option, and it is prefixed by a counter value.
  114. * If the order of the unnamed sections changes for some reason,
  115. * updates to them will be rejected.
  116. */
  117. hash = djbhash(hash, s->type);
  118. uci_foreach_element(&s->options, e) {
  119. hash = djbhash(hash, e->name);
  120. hash = djbhash(hash, uci_to_option(e)->value);
  121. }
  122. sprintf(buf, "cfg%02x%04x", ++s->package->n_section, hash % (1 << 16));
  123. s->e.name = uci_strdup(ctx, buf);
  124. }
  125. static struct uci_section *
  126. uci_alloc_section(struct uci_package *p, const char *type, const char *name)
  127. {
  128. struct uci_context *ctx = p->ctx;
  129. struct uci_section *s;
  130. if (name && !name[0])
  131. name = NULL;
  132. s = uci_alloc_element(ctx, section, name, strlen(type) + 1);
  133. uci_list_init(&s->options);
  134. s->type = uci_dataptr(s);
  135. s->package = p;
  136. strcpy(s->type, type);
  137. if (name == NULL)
  138. s->anonymous = true;
  139. p->n_section++;
  140. uci_list_add(&p->sections, &s->e.list);
  141. return s;
  142. }
  143. static void
  144. uci_free_section(struct uci_section *s)
  145. {
  146. struct uci_element *o, *tmp;
  147. uci_foreach_element_safe(&s->options, tmp, o) {
  148. uci_free_option(uci_to_option(o));
  149. }
  150. if ((s->type != uci_dataptr(s)) &&
  151. (s->type != NULL))
  152. free(s->type);
  153. uci_free_element(&s->e);
  154. }
  155. __plugin struct uci_package *
  156. uci_alloc_package(struct uci_context *ctx, const char *name)
  157. {
  158. struct uci_package *p;
  159. p = uci_alloc_element(ctx, package, name, 0);
  160. p->ctx = ctx;
  161. uci_list_init(&p->sections);
  162. uci_list_init(&p->history);
  163. uci_list_init(&p->saved_history);
  164. return p;
  165. }
  166. static void
  167. uci_free_package(struct uci_package **package)
  168. {
  169. struct uci_element *e, *tmp;
  170. struct uci_package *p = *package;
  171. if(!p)
  172. return;
  173. if (p->path)
  174. free(p->path);
  175. uci_foreach_element_safe(&p->sections, tmp, e) {
  176. uci_free_section(uci_to_section(e));
  177. }
  178. uci_foreach_element_safe(&p->history, tmp, e) {
  179. uci_free_history(uci_to_history(e));
  180. }
  181. uci_foreach_element_safe(&p->saved_history, tmp, e) {
  182. uci_free_history(uci_to_history(e));
  183. }
  184. uci_free_element(&p->e);
  185. *package = NULL;
  186. }
  187. static struct uci_element *uci_lookup_list(struct uci_list *list, const char *name)
  188. {
  189. struct uci_element *e;
  190. uci_foreach_element(list, e) {
  191. if (!strcmp(e->name, name))
  192. return e;
  193. }
  194. return NULL;
  195. }
  196. int uci_lookup(struct uci_context *ctx, struct uci_element **res, struct uci_package *p, const char *section, const char *option)
  197. {
  198. struct uci_element *e;
  199. struct uci_section *s;
  200. UCI_HANDLE_ERR(ctx);
  201. UCI_ASSERT(ctx, res != NULL);
  202. UCI_ASSERT(ctx, p != NULL);
  203. UCI_ASSERT(ctx, section && uci_validate_name(section));
  204. if (option)
  205. UCI_ASSERT(ctx, uci_validate_name(option));
  206. e = uci_lookup_list(&p->sections, section);
  207. if (!e)
  208. goto notfound;
  209. if (option) {
  210. s = uci_to_section(e);
  211. e = uci_lookup_list(&s->options, option);
  212. if (!e)
  213. goto notfound;
  214. }
  215. *res = e;
  216. return 0;
  217. notfound:
  218. UCI_THROW(ctx, UCI_ERR_NOTFOUND);
  219. return 0;
  220. }
  221. int uci_del_element(struct uci_context *ctx, struct uci_element *e)
  222. {
  223. /* NB: UCI_INTERNAL use means without history tracking */
  224. bool internal = ctx->internal;
  225. struct uci_package *p = NULL;
  226. struct uci_section *s = NULL;
  227. struct uci_option *o = NULL;
  228. struct uci_element *i, *tmp;
  229. char *option = NULL;
  230. UCI_HANDLE_ERR(ctx);
  231. UCI_ASSERT(ctx, e != NULL);
  232. switch(e->type) {
  233. case UCI_TYPE_SECTION:
  234. s = uci_to_section(e);
  235. uci_foreach_element_safe(&s->options, tmp, i) {
  236. uci_del_element(ctx, i);
  237. }
  238. break;
  239. case UCI_TYPE_OPTION:
  240. o = uci_to_option(e);
  241. s = o->section;
  242. p = s->package;
  243. option = e->name;
  244. break;
  245. default:
  246. UCI_THROW(ctx, UCI_ERR_INVAL);
  247. break;
  248. }
  249. p = s->package;
  250. if (!internal && p->has_history)
  251. uci_add_history(ctx, &p->history, UCI_CMD_REMOVE, s->e.name, option, NULL);
  252. switch(e->type) {
  253. case UCI_TYPE_SECTION:
  254. uci_free_section(s);
  255. break;
  256. case UCI_TYPE_OPTION:
  257. uci_free_option(o);
  258. break;
  259. default:
  260. break;
  261. }
  262. return 0;
  263. }
  264. int uci_set_element_value(struct uci_context *ctx, struct uci_element **element, const char *value)
  265. {
  266. /* NB: UCI_INTERNAL use means without history tracking */
  267. bool internal = ctx->internal;
  268. struct uci_list *list;
  269. struct uci_element *e;
  270. struct uci_package *p;
  271. struct uci_section *s;
  272. struct uci_option *o;
  273. char *section;
  274. char *option;
  275. char *str;
  276. int size;
  277. UCI_HANDLE_ERR(ctx);
  278. UCI_ASSERT(ctx, (element != NULL) && (*element != NULL));
  279. /* what the 'value' of an element means depends on the type
  280. * for a section, the 'value' means its type
  281. * for an option, the 'value' means its value string
  282. * when changing the value, shrink the element to its actual size
  283. * (it may have been allocated with a bigger size, to include
  284. * its buffer)
  285. * then duplicate the string passed on the command line and
  286. * insert it into the structure.
  287. */
  288. e = *element;
  289. list = e->list.prev;
  290. switch(e->type) {
  291. case UCI_TYPE_SECTION:
  292. UCI_ASSERT(ctx, uci_validate_str(value, false));
  293. size = sizeof(struct uci_section);
  294. s = uci_to_section(e);
  295. section = e->name;
  296. option = NULL;
  297. /* matches the currently set value */
  298. if (!strcmp(value, s->type))
  299. return 0;
  300. break;
  301. case UCI_TYPE_OPTION:
  302. UCI_ASSERT(ctx, value != NULL);
  303. size = sizeof(struct uci_option);
  304. o = uci_to_option(e);
  305. s = o->section;
  306. section = s->e.name;
  307. option = o->e.name;
  308. /* matches the currently set value */
  309. if (!strcmp(value, o->value))
  310. return 0;
  311. break;
  312. default:
  313. UCI_THROW(ctx, UCI_ERR_INVAL);
  314. return 0;
  315. }
  316. p = s->package;
  317. if (!internal && p->has_history)
  318. uci_add_history(ctx, &p->history, UCI_CMD_CHANGE, section, option, value);
  319. uci_list_del(&e->list);
  320. e = uci_realloc(ctx, e, size);
  321. str = uci_strdup(ctx, value);
  322. uci_list_insert(list, &e->list);
  323. *element = e;
  324. switch(e->type) {
  325. case UCI_TYPE_SECTION:
  326. uci_to_section(e)->type = str;
  327. break;
  328. case UCI_TYPE_OPTION:
  329. uci_to_option(e)->value = str;
  330. break;
  331. default:
  332. break;
  333. }
  334. return 0;
  335. }
  336. int uci_rename(struct uci_context *ctx, struct uci_package *p, char *section, char *option, char *name)
  337. {
  338. /* NB: UCI_INTERNAL use means without history tracking */
  339. bool internal = ctx->internal;
  340. struct uci_element *e;
  341. UCI_HANDLE_ERR(ctx);
  342. /* NB: p, section, option validated by uci_lookup */
  343. UCI_INTERNAL(uci_lookup, ctx, &e, p, section, option);
  344. if (!internal && p->has_history)
  345. uci_add_history(ctx, &p->history, UCI_CMD_RENAME, section, option, name);
  346. name = uci_strdup(ctx, name);
  347. if (e->name)
  348. free(e->name);
  349. e->name = name;
  350. return 0;
  351. }
  352. int uci_add_section(struct uci_context *ctx, struct uci_package *p, const char *type, struct uci_section **res)
  353. {
  354. bool internal = ctx->internal;
  355. struct uci_section *s;
  356. UCI_HANDLE_ERR(ctx);
  357. UCI_ASSERT(ctx, p != NULL);
  358. s = uci_alloc_section(p, type, NULL);
  359. uci_fixup_section(ctx, s);
  360. *res = s;
  361. if (!internal && p->has_history)
  362. uci_add_history(ctx, &p->history, UCI_CMD_ADD, s->e.name, NULL, type);
  363. return 0;
  364. }
  365. int uci_delete(struct uci_context *ctx, struct uci_package *p, const char *section, const char *option)
  366. {
  367. /* NB: pass on internal flag to uci_del_element */
  368. bool internal = ctx->internal;
  369. struct uci_element *e;
  370. UCI_HANDLE_ERR(ctx);
  371. /* NB: p, section, option validated by uci_lookup */
  372. UCI_INTERNAL(uci_lookup, ctx, &e, p, section, option);
  373. ctx->internal = internal;
  374. return uci_del_element(ctx, e);
  375. }
  376. int uci_set(struct uci_context *ctx, struct uci_package *p, const char *section, const char *option, const char *value, struct uci_element **result)
  377. {
  378. /* NB: UCI_INTERNAL use means without history tracking */
  379. bool internal = ctx->internal;
  380. struct uci_element *e = NULL;
  381. struct uci_section *s = NULL;
  382. struct uci_option *o = NULL;
  383. UCI_HANDLE_ERR(ctx);
  384. UCI_ASSERT(ctx, p != NULL);
  385. UCI_ASSERT(ctx, uci_validate_name(section));
  386. if (option) {
  387. UCI_ASSERT(ctx, uci_validate_name(option));
  388. UCI_ASSERT(ctx, value != NULL);
  389. } else {
  390. UCI_ASSERT(ctx, uci_validate_str(value, false));
  391. }
  392. /*
  393. * look up the package, section and option (if set)
  394. * if the section/option is to be modified and it is not found
  395. * create a new element in the appropriate list
  396. */
  397. e = uci_lookup_list(&p->sections, section);
  398. if (!e)
  399. goto notfound;
  400. s = uci_to_section(e);
  401. if (ctx->pctx && ctx->pctx->merge)
  402. ctx->pctx->section = s;
  403. if (option) {
  404. e = uci_lookup_list(&s->options, option);
  405. if (!e)
  406. goto notfound;
  407. o = uci_to_option(e);
  408. }
  409. /*
  410. * no unknown element was supplied, assume that we can just update
  411. * an existing entry
  412. */
  413. if (o)
  414. e = &o->e;
  415. else
  416. e = &s->e;
  417. if (result)
  418. *result = e;
  419. else
  420. result = &e;
  421. ctx->internal = internal;
  422. return uci_set_element_value(ctx, result, value);
  423. notfound:
  424. /*
  425. * the entry that we need to update was not found,
  426. * check if the search failed prematurely.
  427. * this can happen if the package was not found, or if
  428. * an option was supplied, but the section wasn't found
  429. */
  430. if (!p || (!s && option))
  431. UCI_THROW(ctx, UCI_ERR_NOTFOUND);
  432. /* now add the missing entry */
  433. if (!internal && p->has_history)
  434. uci_add_history(ctx, &p->history, UCI_CMD_CHANGE, section, option, value);
  435. if (s) {
  436. o = uci_alloc_option(s, option, value);
  437. if (result)
  438. *result = &o->e;
  439. } else {
  440. s = uci_alloc_section(p, value, section);
  441. if (result)
  442. *result = &s->e;
  443. if (ctx->pctx && ctx->pctx->merge)
  444. ctx->pctx->section = s;
  445. }
  446. return 0;
  447. }
  448. int uci_unload(struct uci_context *ctx, struct uci_package *p)
  449. {
  450. UCI_HANDLE_ERR(ctx);
  451. UCI_ASSERT(ctx, p != NULL);
  452. uci_free_package(&p);
  453. return 0;
  454. }