list.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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. static void uci_list_set_pos(struct uci_list *head, struct uci_list *ptr, int pos)
  15. {
  16. struct uci_list *new_head = head;
  17. struct uci_element *p = NULL;
  18. uci_list_del(ptr);
  19. uci_foreach_element(head, p) {
  20. if (pos-- <= 0)
  21. break;
  22. new_head = &p->list;
  23. }
  24. uci_list_add(new_head->next, ptr);
  25. }
  26. static inline void uci_list_fixup(struct uci_list *ptr)
  27. {
  28. ptr->prev->next = ptr;
  29. ptr->next->prev = ptr;
  30. }
  31. /*
  32. * uci_alloc_generic allocates a new uci_element with payload
  33. * payload is appended to the struct to save memory and reduce fragmentation
  34. */
  35. __private struct uci_element *
  36. uci_alloc_generic(struct uci_context *ctx, int type, const char *name, int size)
  37. {
  38. struct uci_element *e;
  39. int datalen = size;
  40. void *ptr;
  41. ptr = uci_malloc(ctx, datalen);
  42. e = (struct uci_element *) ptr;
  43. e->type = type;
  44. if (name) {
  45. UCI_TRAP_SAVE(ctx, error);
  46. e->name = uci_strdup(ctx, name);
  47. UCI_TRAP_RESTORE(ctx);
  48. }
  49. uci_list_init(&e->list);
  50. goto done;
  51. error:
  52. free(ptr);
  53. UCI_THROW(ctx, ctx->err);
  54. done:
  55. return e;
  56. }
  57. __private void
  58. uci_free_element(struct uci_element *e)
  59. {
  60. if (e->name)
  61. free(e->name);
  62. if (!uci_list_empty(&e->list))
  63. uci_list_del(&e->list);
  64. free(e);
  65. }
  66. static struct uci_option *
  67. uci_alloc_option(struct uci_section *s, const char *name, const char *value)
  68. {
  69. struct uci_package *p = s->package;
  70. struct uci_context *ctx = p->ctx;
  71. struct uci_option *o;
  72. o = uci_alloc_element(ctx, option, name, strlen(value) + 1);
  73. o->type = UCI_TYPE_STRING;
  74. o->v.string = uci_dataptr(o);
  75. o->section = s;
  76. strcpy(o->v.string, value);
  77. uci_list_add(&s->options, &o->e.list);
  78. return o;
  79. }
  80. static inline void
  81. uci_free_option(struct uci_option *o)
  82. {
  83. struct uci_element *e, *tmp;
  84. switch(o->type) {
  85. case UCI_TYPE_STRING:
  86. if ((o->v.string != uci_dataptr(o)) &&
  87. (o->v.string != NULL))
  88. free(o->v.string);
  89. break;
  90. case UCI_TYPE_LIST:
  91. uci_foreach_element_safe(&o->v.list, tmp, e) {
  92. uci_free_element(e);
  93. }
  94. break;
  95. default:
  96. break;
  97. }
  98. uci_free_element(&o->e);
  99. }
  100. static struct uci_option *
  101. uci_alloc_list(struct uci_section *s, const char *name)
  102. {
  103. struct uci_package *p = s->package;
  104. struct uci_context *ctx = p->ctx;
  105. struct uci_option *o;
  106. o = uci_alloc_element(ctx, option, name, 0);
  107. o->type = UCI_TYPE_LIST;
  108. o->section = s;
  109. uci_list_init(&o->v.list);
  110. uci_list_add(&s->options, &o->e.list);
  111. return o;
  112. }
  113. /* Based on an efficient hash function published by D. J. Bernstein */
  114. static unsigned int djbhash(unsigned int hash, char *str)
  115. {
  116. int len = strlen(str);
  117. int i;
  118. /* initial value */
  119. if (hash == ~0)
  120. hash = 5381;
  121. for(i = 0; i < len; i++) {
  122. hash = ((hash << 5) + hash) + str[i];
  123. }
  124. return (hash & 0x7FFFFFFF);
  125. }
  126. /* fix up an unnamed section, e.g. after adding options to it */
  127. __private void uci_fixup_section(struct uci_context *ctx, struct uci_section *s)
  128. {
  129. unsigned int hash = ~0;
  130. struct uci_element *e;
  131. char buf[16];
  132. if (!s || s->e.name)
  133. return;
  134. /*
  135. * Generate a name for unnamed sections. This is used as reference
  136. * when locating or updating the section from apps/scripts.
  137. * To make multiple concurrent versions somewhat safe for updating,
  138. * the name is generated from a hash of its type and name/value
  139. * pairs of its option, and it is prefixed by a counter value.
  140. * If the order of the unnamed sections changes for some reason,
  141. * updates to them will be rejected.
  142. */
  143. hash = djbhash(hash, s->type);
  144. uci_foreach_element(&s->options, e) {
  145. struct uci_option *o;
  146. hash = djbhash(hash, e->name);
  147. o = uci_to_option(e);
  148. switch(o->type) {
  149. case UCI_TYPE_STRING:
  150. hash = djbhash(hash, o->v.string);
  151. break;
  152. default:
  153. break;
  154. }
  155. }
  156. sprintf(buf, "cfg%02x%04x", ++s->package->n_section, hash % (1 << 16));
  157. s->e.name = uci_strdup(ctx, buf);
  158. }
  159. static struct uci_section *
  160. uci_alloc_section(struct uci_package *p, const char *type, const char *name)
  161. {
  162. struct uci_context *ctx = p->ctx;
  163. struct uci_section *s;
  164. if (name && !name[0])
  165. name = NULL;
  166. s = uci_alloc_element(ctx, section, name, strlen(type) + 1);
  167. uci_list_init(&s->options);
  168. s->type = uci_dataptr(s);
  169. s->package = p;
  170. strcpy(s->type, type);
  171. if (name == NULL)
  172. s->anonymous = true;
  173. p->n_section++;
  174. uci_list_add(&p->sections, &s->e.list);
  175. return s;
  176. }
  177. static void
  178. uci_free_section(struct uci_section *s)
  179. {
  180. struct uci_element *o, *tmp;
  181. uci_foreach_element_safe(&s->options, tmp, o) {
  182. uci_free_option(uci_to_option(o));
  183. }
  184. if ((s->type != uci_dataptr(s)) &&
  185. (s->type != NULL))
  186. free(s->type);
  187. uci_free_element(&s->e);
  188. }
  189. __private struct uci_package *
  190. uci_alloc_package(struct uci_context *ctx, const char *name)
  191. {
  192. struct uci_package *p;
  193. p = uci_alloc_element(ctx, package, name, 0);
  194. p->ctx = ctx;
  195. uci_list_init(&p->sections);
  196. uci_list_init(&p->delta);
  197. uci_list_init(&p->saved_delta);
  198. return p;
  199. }
  200. __private void
  201. uci_free_package(struct uci_package **package)
  202. {
  203. struct uci_element *e, *tmp;
  204. struct uci_package *p = *package;
  205. if(!p)
  206. return;
  207. if (p->path)
  208. free(p->path);
  209. uci_foreach_element_safe(&p->sections, tmp, e) {
  210. uci_free_section(uci_to_section(e));
  211. }
  212. uci_foreach_element_safe(&p->delta, tmp, e) {
  213. uci_free_delta(uci_to_delta(e));
  214. }
  215. uci_foreach_element_safe(&p->saved_delta, tmp, e) {
  216. uci_free_delta(uci_to_delta(e));
  217. }
  218. uci_free_element(&p->e);
  219. *package = NULL;
  220. }
  221. static void
  222. uci_free_any(struct uci_element **e)
  223. {
  224. switch((*e)->type) {
  225. case UCI_TYPE_SECTION:
  226. uci_free_section(uci_to_section(*e));
  227. break;
  228. case UCI_TYPE_OPTION:
  229. uci_free_option(uci_to_option(*e));
  230. break;
  231. default:
  232. break;
  233. }
  234. *e = NULL;
  235. }
  236. __private struct uci_element *
  237. uci_lookup_list(struct uci_list *list, const char *name)
  238. {
  239. struct uci_element *e;
  240. uci_foreach_element(list, e) {
  241. if (!strcmp(e->name, name))
  242. return e;
  243. }
  244. return NULL;
  245. }
  246. static struct uci_element *
  247. uci_lookup_ext_section(struct uci_context *ctx, struct uci_ptr *ptr)
  248. {
  249. char *idxstr, *t, *section, *name;
  250. struct uci_element *e = NULL;
  251. struct uci_section *s;
  252. int idx, c;
  253. section = uci_strdup(ctx, ptr->section);
  254. name = idxstr = section + 1;
  255. if (section[0] != '@')
  256. goto error;
  257. /* parse the section index part */
  258. idxstr = strchr(idxstr, '[');
  259. if (!idxstr)
  260. goto error;
  261. *idxstr = 0;
  262. idxstr++;
  263. t = strchr(idxstr, ']');
  264. if (!t)
  265. goto error;
  266. if (t[1] != 0)
  267. goto error;
  268. *t = 0;
  269. t = NULL;
  270. idx = strtol(idxstr, &t, 10);
  271. if (t && *t)
  272. goto error;
  273. if (!*name)
  274. name = NULL;
  275. else if (!uci_validate_type(name))
  276. goto error;
  277. /* if the given index is negative, it specifies the section number from
  278. * the end of the list */
  279. if (idx < 0) {
  280. c = 0;
  281. uci_foreach_element(&ptr->p->sections, e) {
  282. s = uci_to_section(e);
  283. if (name && (strcmp(s->type, name) != 0))
  284. continue;
  285. c++;
  286. }
  287. idx += c;
  288. }
  289. c = 0;
  290. uci_foreach_element(&ptr->p->sections, e) {
  291. s = uci_to_section(e);
  292. if (name && (strcmp(s->type, name) != 0))
  293. continue;
  294. if (idx == c)
  295. goto done;
  296. c++;
  297. }
  298. e = NULL;
  299. goto done;
  300. error:
  301. e = NULL;
  302. memset(ptr, 0, sizeof(struct uci_ptr));
  303. UCI_THROW(ctx, UCI_ERR_INVAL);
  304. done:
  305. free(section);
  306. if (e)
  307. ptr->section = e->name;
  308. return e;
  309. }
  310. int
  311. uci_lookup_next(struct uci_context *ctx, struct uci_element **e, struct uci_list *list, const char *name)
  312. {
  313. UCI_HANDLE_ERR(ctx);
  314. *e = uci_lookup_list(list, name);
  315. if (!*e)
  316. UCI_THROW(ctx, UCI_ERR_NOTFOUND);
  317. return 0;
  318. }
  319. int
  320. uci_lookup_ptr(struct uci_context *ctx, struct uci_ptr *ptr, char *str, bool extended)
  321. {
  322. struct uci_element *e;
  323. UCI_HANDLE_ERR(ctx);
  324. UCI_ASSERT(ctx, ptr != NULL);
  325. if (str)
  326. UCI_INTERNAL(uci_parse_ptr, ctx, ptr, str);
  327. ptr->flags |= UCI_LOOKUP_DONE;
  328. /* look up the package first */
  329. if (ptr->p)
  330. e = &ptr->p->e;
  331. else
  332. e = uci_lookup_list(&ctx->root, ptr->package);
  333. if (!e) {
  334. UCI_INTERNAL(uci_load, ctx, ptr->package, &ptr->p);
  335. if (!ptr->p)
  336. goto notfound;
  337. ptr->last = &ptr->p->e;
  338. } else {
  339. ptr->p = uci_to_package(e);
  340. ptr->last = e;
  341. }
  342. if (!ptr->section && !ptr->s)
  343. goto complete;
  344. /* if the section name validates as a regular name, pass through
  345. * to the regular uci_lookup function call */
  346. if (ptr->s) {
  347. e = &ptr->s->e;
  348. } else if (ptr->flags & UCI_LOOKUP_EXTENDED) {
  349. if (extended)
  350. e = uci_lookup_ext_section(ctx, ptr);
  351. else
  352. UCI_THROW(ctx, UCI_ERR_INVAL);
  353. } else {
  354. e = uci_lookup_list(&ptr->p->sections, ptr->section);
  355. }
  356. if (!e)
  357. goto abort;
  358. ptr->last = e;
  359. ptr->s = uci_to_section(e);
  360. if (ptr->option) {
  361. e = uci_lookup_list(&ptr->s->options, ptr->option);
  362. if (!e)
  363. goto abort;
  364. ptr->o = uci_to_option(e);
  365. ptr->last = e;
  366. }
  367. complete:
  368. ptr->flags |= UCI_LOOKUP_COMPLETE;
  369. abort:
  370. return 0;
  371. notfound:
  372. UCI_THROW(ctx, UCI_ERR_NOTFOUND);
  373. return 0;
  374. }
  375. __private struct uci_element *
  376. uci_expand_ptr(struct uci_context *ctx, struct uci_ptr *ptr, bool complete)
  377. {
  378. UCI_ASSERT(ctx, ptr != NULL);
  379. if (!(ptr->flags & UCI_LOOKUP_DONE))
  380. UCI_INTERNAL(uci_lookup_ptr, ctx, ptr, NULL, 1);
  381. if (complete && !(ptr->flags & UCI_LOOKUP_COMPLETE))
  382. UCI_THROW(ctx, UCI_ERR_NOTFOUND);
  383. UCI_ASSERT(ctx, ptr->p != NULL);
  384. /* fill in missing string info */
  385. if (ptr->p && !ptr->package)
  386. ptr->package = ptr->p->e.name;
  387. if (ptr->s && !ptr->section)
  388. ptr->section = ptr->s->e.name;
  389. if (ptr->o && !ptr->option)
  390. ptr->option = ptr->o->e.name;
  391. if (ptr->o)
  392. return &ptr->o->e;
  393. if (ptr->s)
  394. return &ptr->s->e;
  395. if (ptr->p)
  396. return &ptr->p->e;
  397. else
  398. return NULL;
  399. }
  400. static void uci_add_element_list(struct uci_context *ctx, struct uci_ptr *ptr, bool internal)
  401. {
  402. struct uci_element *e;
  403. struct uci_package *p;
  404. p = ptr->p;
  405. if (!internal && p->has_delta)
  406. uci_add_delta(ctx, &p->delta, UCI_CMD_LIST_ADD, ptr->section, ptr->option, ptr->value);
  407. e = uci_alloc_generic(ctx, UCI_TYPE_ITEM, ptr->value, sizeof(struct uci_option));
  408. uci_list_add(&ptr->o->v.list, &e->list);
  409. }
  410. int uci_rename(struct uci_context *ctx, struct uci_ptr *ptr)
  411. {
  412. /* NB: UCI_INTERNAL use means without delta tracking */
  413. bool internal = ctx && ctx->internal;
  414. struct uci_element *e;
  415. struct uci_package *p;
  416. char *n;
  417. UCI_HANDLE_ERR(ctx);
  418. e = uci_expand_ptr(ctx, ptr, true);
  419. p = ptr->p;
  420. UCI_ASSERT(ctx, ptr->s);
  421. UCI_ASSERT(ctx, ptr->value);
  422. if (!internal && p->has_delta)
  423. uci_add_delta(ctx, &p->delta, UCI_CMD_RENAME, ptr->section, ptr->option, ptr->value);
  424. n = uci_strdup(ctx, ptr->value);
  425. if (e->name)
  426. free(e->name);
  427. e->name = n;
  428. if (e->type == UCI_TYPE_SECTION)
  429. uci_to_section(e)->anonymous = false;
  430. return 0;
  431. }
  432. int uci_reorder_section(struct uci_context *ctx, struct uci_section *s, int pos)
  433. {
  434. struct uci_package *p = s->package;
  435. char order[32];
  436. UCI_HANDLE_ERR(ctx);
  437. uci_list_set_pos(&s->package->sections, &s->e.list, pos);
  438. if (!ctx->internal && p->has_delta) {
  439. sprintf(order, "%d", pos);
  440. uci_add_delta(ctx, &p->delta, UCI_CMD_REORDER, s->e.name, NULL, order);
  441. }
  442. return 0;
  443. }
  444. int uci_add_section(struct uci_context *ctx, struct uci_package *p, const char *type, struct uci_section **res)
  445. {
  446. bool internal = ctx && ctx->internal;
  447. struct uci_section *s;
  448. UCI_HANDLE_ERR(ctx);
  449. UCI_ASSERT(ctx, p != NULL);
  450. s = uci_alloc_section(p, type, NULL);
  451. uci_fixup_section(ctx, s);
  452. *res = s;
  453. if (!internal && p->has_delta)
  454. uci_add_delta(ctx, &p->delta, UCI_CMD_ADD, s->e.name, NULL, type);
  455. return 0;
  456. }
  457. int uci_delete(struct uci_context *ctx, struct uci_ptr *ptr)
  458. {
  459. /* NB: pass on internal flag to uci_del_element */
  460. bool internal = ctx && ctx->internal;
  461. struct uci_package *p;
  462. struct uci_element *e1, *e2, *tmp;
  463. int index;
  464. UCI_HANDLE_ERR(ctx);
  465. e1 = uci_expand_ptr(ctx, ptr, true);
  466. p = ptr->p;
  467. UCI_ASSERT(ctx, ptr->s);
  468. if (ptr->value && ptr->o && ptr->o->type == UCI_TYPE_LIST) {
  469. if (!sscanf(ptr->value, "%d", &index))
  470. return 1;
  471. uci_foreach_element_safe(&ptr->o->v.list, tmp, e2) {
  472. if (index == 0) {
  473. if (!internal && p->has_delta)
  474. uci_add_delta(ctx, &p->delta, UCI_CMD_REMOVE, ptr->section, ptr->option, ptr->value);
  475. uci_free_option(uci_to_option(e2));
  476. return 0;
  477. }
  478. index--;
  479. }
  480. return 0;
  481. }
  482. if (!internal && p->has_delta)
  483. uci_add_delta(ctx, &p->delta, UCI_CMD_REMOVE, ptr->section, ptr->option, NULL);
  484. uci_free_any(&e1);
  485. if (ptr->option)
  486. ptr->o = NULL;
  487. else if (ptr->section)
  488. ptr->s = NULL;
  489. return 0;
  490. }
  491. int uci_add_list(struct uci_context *ctx, struct uci_ptr *ptr)
  492. {
  493. /* NB: UCI_INTERNAL use means without delta tracking */
  494. bool internal = ctx && ctx->internal;
  495. struct uci_option *prev = NULL;
  496. const char *value2 = NULL;
  497. UCI_HANDLE_ERR(ctx);
  498. uci_expand_ptr(ctx, ptr, false);
  499. UCI_ASSERT(ctx, ptr->s);
  500. UCI_ASSERT(ctx, ptr->value);
  501. if (ptr->o) {
  502. switch (ptr->o->type) {
  503. case UCI_TYPE_STRING:
  504. /* we already have a string value, convert that to a list */
  505. prev = ptr->o;
  506. value2 = ptr->value;
  507. ptr->value = ptr->o->v.string;
  508. break;
  509. case UCI_TYPE_LIST:
  510. uci_add_element_list(ctx, ptr, internal);
  511. return 0;
  512. default:
  513. UCI_THROW(ctx, UCI_ERR_INVAL);
  514. break;
  515. }
  516. }
  517. ptr->o = uci_alloc_list(ptr->s, ptr->option);
  518. if (prev) {
  519. uci_add_element_list(ctx, ptr, true);
  520. uci_free_option(prev);
  521. ptr->value = value2;
  522. }
  523. uci_add_element_list(ctx, ptr, internal);
  524. return 0;
  525. }
  526. int uci_del_list(struct uci_context *ctx, struct uci_ptr *ptr)
  527. {
  528. /* NB: pass on internal flag to uci_del_element */
  529. bool internal = ctx && ctx->internal;
  530. struct uci_element *e, *tmp;
  531. struct uci_package *p;
  532. UCI_HANDLE_ERR(ctx);
  533. uci_expand_ptr(ctx, ptr, false);
  534. UCI_ASSERT(ctx, ptr->s);
  535. UCI_ASSERT(ctx, ptr->value);
  536. if (!(ptr->o && ptr->option))
  537. return 0;
  538. if ((ptr->o->type != UCI_TYPE_LIST))
  539. return 0;
  540. p = ptr->p;
  541. if (!internal && p->has_delta)
  542. uci_add_delta(ctx, &p->delta, UCI_CMD_LIST_DEL, ptr->section, ptr->option, ptr->value);
  543. uci_foreach_element_safe(&ptr->o->v.list, tmp, e) {
  544. if (!strcmp(ptr->value, uci_to_option(e)->e.name)) {
  545. uci_free_option(uci_to_option(e));
  546. }
  547. }
  548. return 0;
  549. }
  550. int uci_set(struct uci_context *ctx, struct uci_ptr *ptr)
  551. {
  552. /* NB: UCI_INTERNAL use means without delta tracking */
  553. bool internal = ctx && ctx->internal;
  554. UCI_HANDLE_ERR(ctx);
  555. uci_expand_ptr(ctx, ptr, false);
  556. UCI_ASSERT(ctx, ptr->value);
  557. UCI_ASSERT(ctx, ptr->s || (!ptr->option && ptr->section));
  558. if (!ptr->option && ptr->value[0]) {
  559. UCI_ASSERT(ctx, uci_validate_type(ptr->value));
  560. }
  561. if (!ptr->o && ptr->s && ptr->option) {
  562. struct uci_element *e;
  563. e = uci_lookup_list(&ptr->s->options, ptr->option);
  564. if (e)
  565. ptr->o = uci_to_option(e);
  566. }
  567. if (!ptr->value[0]) {
  568. /* if setting a nonexistant option/section to a nonexistant value,
  569. * exit without errors */
  570. if (!(ptr->flags & UCI_LOOKUP_COMPLETE))
  571. return 0;
  572. return uci_delete(ctx, ptr);
  573. } else if (!ptr->o && ptr->option) { /* new option */
  574. ptr->o = uci_alloc_option(ptr->s, ptr->option, ptr->value);
  575. ptr->last = &ptr->o->e;
  576. } else if (!ptr->s && ptr->section) { /* new section */
  577. ptr->s = uci_alloc_section(ptr->p, ptr->value, ptr->section);
  578. ptr->last = &ptr->s->e;
  579. } else if (ptr->o && ptr->option) { /* update option */
  580. if ((ptr->o->type == UCI_TYPE_STRING) &&
  581. !strcmp(ptr->o->v.string, ptr->value))
  582. return 0;
  583. uci_free_option(ptr->o);
  584. ptr->o = uci_alloc_option(ptr->s, ptr->option, ptr->value);
  585. ptr->last = &ptr->o->e;
  586. } else if (ptr->s && ptr->section) { /* update section */
  587. char *s = uci_strdup(ctx, ptr->value);
  588. if (ptr->s->type == uci_dataptr(ptr->s)) {
  589. ptr->last = NULL;
  590. ptr->last = uci_realloc(ctx, ptr->s, sizeof(struct uci_section));
  591. ptr->s = uci_to_section(ptr->last);
  592. uci_list_fixup(&ptr->s->e.list);
  593. } else {
  594. free(ptr->s->type);
  595. }
  596. ptr->s->type = s;
  597. } else {
  598. UCI_THROW(ctx, UCI_ERR_INVAL);
  599. }
  600. if (!internal && ptr->p->has_delta)
  601. uci_add_delta(ctx, &ptr->p->delta, UCI_CMD_CHANGE, ptr->section, ptr->option, ptr->value);
  602. return 0;
  603. }
  604. int uci_unload(struct uci_context *ctx, struct uci_package *p)
  605. {
  606. UCI_HANDLE_ERR(ctx);
  607. UCI_ASSERT(ctx, p != NULL);
  608. uci_free_package(&p);
  609. return 0;
  610. }