list.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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->errno);
  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, char *section, 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, 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, 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. char *section;
  273. char *option;
  274. char *str;
  275. int size;
  276. UCI_HANDLE_ERR(ctx);
  277. UCI_ASSERT(ctx, (element != NULL) && (*element != NULL));
  278. /* what the 'value' of an element means depends on the type
  279. * for a section, the 'value' means its type
  280. * for an option, the 'value' means its value string
  281. * when changing the value, shrink the element to its actual size
  282. * (it may have been allocated with a bigger size, to include
  283. * its buffer)
  284. * then duplicate the string passed on the command line and
  285. * insert it into the structure.
  286. */
  287. e = *element;
  288. list = e->list.prev;
  289. switch(e->type) {
  290. case UCI_TYPE_SECTION:
  291. UCI_ASSERT(ctx, uci_validate_str(value, false));
  292. size = sizeof(struct uci_section);
  293. s = uci_to_section(e);
  294. section = e->name;
  295. option = NULL;
  296. break;
  297. case UCI_TYPE_OPTION:
  298. UCI_ASSERT(ctx, value != NULL);
  299. size = sizeof(struct uci_option);
  300. s = uci_to_option(e)->section;
  301. section = s->e.name;
  302. option = e->name;
  303. break;
  304. default:
  305. UCI_THROW(ctx, UCI_ERR_INVAL);
  306. return 0;
  307. }
  308. p = s->package;
  309. if (!internal && p->has_history)
  310. uci_add_history(ctx, &p->history, UCI_CMD_CHANGE, section, option, value);
  311. uci_list_del(&e->list);
  312. e = uci_realloc(ctx, e, size);
  313. str = uci_strdup(ctx, value);
  314. uci_list_insert(list, &e->list);
  315. *element = e;
  316. switch(e->type) {
  317. case UCI_TYPE_SECTION:
  318. uci_to_section(e)->type = str;
  319. break;
  320. case UCI_TYPE_OPTION:
  321. uci_to_option(e)->value = str;
  322. break;
  323. default:
  324. break;
  325. }
  326. return 0;
  327. }
  328. int uci_rename(struct uci_context *ctx, struct uci_package *p, char *section, char *option, char *name)
  329. {
  330. /* NB: UCI_INTERNAL use means without history tracking */
  331. bool internal = ctx->internal;
  332. struct uci_element *e;
  333. UCI_HANDLE_ERR(ctx);
  334. /* NB: p, section, option validated by uci_lookup */
  335. UCI_INTERNAL(uci_lookup, ctx, &e, p, section, option);
  336. if (!internal && p->has_history)
  337. uci_add_history(ctx, &p->history, UCI_CMD_RENAME, section, option, name);
  338. name = uci_strdup(ctx, name);
  339. if (e->name)
  340. free(e->name);
  341. e->name = name;
  342. return 0;
  343. }
  344. int uci_add_section(struct uci_context *ctx, struct uci_package *p, char *type, struct uci_section **res)
  345. {
  346. bool internal = ctx->internal;
  347. struct uci_section *s;
  348. UCI_HANDLE_ERR(ctx);
  349. UCI_ASSERT(ctx, p != NULL);
  350. s = uci_alloc_section(p, type, NULL);
  351. uci_fixup_section(ctx, s);
  352. *res = s;
  353. if (!internal && p->has_history)
  354. uci_add_history(ctx, &p->history, UCI_CMD_ADD, s->e.name, NULL, type);
  355. return 0;
  356. }
  357. int uci_delete(struct uci_context *ctx, struct uci_package *p, char *section, char *option)
  358. {
  359. /* NB: pass on internal flag to uci_del_element */
  360. bool internal = ctx->internal;
  361. struct uci_element *e;
  362. UCI_HANDLE_ERR(ctx);
  363. /* NB: p, section, option validated by uci_lookup */
  364. UCI_INTERNAL(uci_lookup, ctx, &e, p, section, option);
  365. ctx->internal = internal;
  366. return uci_del_element(ctx, e);
  367. }
  368. int uci_set(struct uci_context *ctx, struct uci_package *p, char *section, char *option, char *value, struct uci_element **result)
  369. {
  370. /* NB: UCI_INTERNAL use means without history tracking */
  371. bool internal = ctx->internal;
  372. struct uci_element *e = NULL;
  373. struct uci_section *s = NULL;
  374. struct uci_option *o = NULL;
  375. UCI_HANDLE_ERR(ctx);
  376. UCI_ASSERT(ctx, p != NULL);
  377. UCI_ASSERT(ctx, uci_validate_name(section));
  378. if (option) {
  379. UCI_ASSERT(ctx, uci_validate_name(option));
  380. UCI_ASSERT(ctx, value != NULL);
  381. } else {
  382. UCI_ASSERT(ctx, uci_validate_str(value, false));
  383. }
  384. /*
  385. * look up the package, section and option (if set)
  386. * if the section/option is to be modified and it is not found
  387. * create a new element in the appropriate list
  388. */
  389. e = uci_lookup_list(&p->sections, section);
  390. if (!e)
  391. goto notfound;
  392. s = uci_to_section(e);
  393. if (ctx->pctx && ctx->pctx->merge)
  394. ctx->pctx->section = s;
  395. if (option) {
  396. e = uci_lookup_list(&s->options, option);
  397. if (!e)
  398. goto notfound;
  399. o = uci_to_option(e);
  400. }
  401. /*
  402. * no unknown element was supplied, assume that we can just update
  403. * an existing entry
  404. */
  405. if (o)
  406. e = &o->e;
  407. else
  408. e = &s->e;
  409. if (result)
  410. *result = e;
  411. else
  412. result = &e;
  413. ctx->internal = internal;
  414. return uci_set_element_value(ctx, result, value);
  415. notfound:
  416. /*
  417. * the entry that we need to update was not found,
  418. * check if the search failed prematurely.
  419. * this can happen if the package was not found, or if
  420. * an option was supplied, but the section wasn't found
  421. */
  422. if (!p || (!s && option))
  423. UCI_THROW(ctx, UCI_ERR_NOTFOUND);
  424. /* now add the missing entry */
  425. if (!internal && p->has_history)
  426. uci_add_history(ctx, &p->history, UCI_CMD_CHANGE, section, option, value);
  427. if (s) {
  428. o = uci_alloc_option(s, option, value);
  429. if (result)
  430. *result = &o->e;
  431. } else {
  432. s = uci_alloc_section(p, value, section);
  433. if (result)
  434. *result = &s->e;
  435. if (ctx->pctx && ctx->pctx->merge)
  436. ctx->pctx->section = s;
  437. }
  438. return 0;
  439. }
  440. int uci_unload(struct uci_context *ctx, struct uci_package *p)
  441. {
  442. UCI_HANDLE_ERR(ctx);
  443. UCI_ASSERT(ctx, p != NULL);
  444. uci_free_package(&p);
  445. return 0;
  446. }