list.c 16 KB

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