Skip to content

Commit 2e784f8

Browse files
committed
tree data UPDATE forbid unlinking list keys
1 parent d098f5d commit 2e784f8

File tree

6 files changed

+51
-26
lines changed

6 files changed

+51
-26
lines changed

src/parser_json.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2089,7 +2089,7 @@ lyd_parse_json_restconf(const struct ly_ctx *ctx, const struct lysc_ext_instance
20892089
} else {
20902090
/* can be the only opaque node and an operation had to be parsed */
20912091
assert(!strcmp(LYD_NAME(node), "eventTime") && (*first_p)->next);
2092-
lyd_unlink_tree(node);
2092+
lyd_unlink(node);
20932093
assert(*envp);
20942094
lyd_insert_child(*envp, node);
20952095
}

src/parser_xml.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1995,7 +1995,7 @@ lyd_parse_xml_netconf(const struct ly_ctx *ctx, const struct lysc_ext_instance *
19951995
} else {
19961996
/* can be the only opaque node and an operation had to be parsed */
19971997
assert(!strcmp(LYD_NAME(node), "eventTime") && (*first_p)->next);
1998-
lyd_unlink_tree(node);
1998+
lyd_unlink(node);
19991999
assert(*envp);
20002000
lyd_insert_child(*envp, node);
20012001
}

src/tree_data.c

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -774,12 +774,12 @@ lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
774774
}
775775

776776
if (node->parent || node->prev->next) {
777-
lyd_unlink_tree(node);
777+
lyd_unlink(node);
778778
}
779779

780780
while (node) {
781781
iter = node->next;
782-
lyd_unlink_tree(node);
782+
lyd_unlink(node);
783783
lyd_insert_node(parent, NULL, node, 0);
784784
node = iter;
785785
}
@@ -801,7 +801,7 @@ lyplg_ext_insert(struct lyd_node *parent, struct lyd_node *first)
801801

802802
while (first) {
803803
iter = first->next;
804-
lyd_unlink_tree(first);
804+
lyd_unlink(first);
805805
lyd_insert_node(parent, NULL, first, 1);
806806
first = iter;
807807
}
@@ -825,7 +825,7 @@ lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_n
825825
}
826826

827827
if (node->parent || node->prev->next) {
828-
lyd_unlink_tree(node);
828+
lyd_unlink(node);
829829
}
830830

831831
while (node) {
@@ -835,7 +835,7 @@ lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_n
835835
}
836836

837837
iter = node->next;
838-
lyd_unlink_tree(node);
838+
lyd_unlink(node);
839839
lyd_insert_node(NULL, &sibling, node, 0);
840840
node = iter;
841841
}
@@ -868,7 +868,7 @@ lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
868868
return LY_EINVAL;
869869
}
870870

871-
lyd_unlink_tree(node);
871+
lyd_unlink(node);
872872
lyd_insert_before_node(sibling, node);
873873
lyd_insert_hash(node);
874874

@@ -892,26 +892,15 @@ lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
892892
return LY_EINVAL;
893893
}
894894

895-
lyd_unlink_tree(node);
895+
lyd_unlink(node);
896896
lyd_insert_after_node(sibling, node);
897897
lyd_insert_hash(node);
898898

899899
return LY_SUCCESS;
900900
}
901901

902-
LIBYANG_API_DEF void
903-
lyd_unlink_siblings(struct lyd_node *node)
904-
{
905-
struct lyd_node *next, *elem, *first = NULL;
906-
907-
LY_LIST_FOR_SAFE(node, next, elem) {
908-
lyd_unlink_tree(elem);
909-
lyd_insert_node(NULL, &first, elem, 1);
910-
}
911-
}
912-
913-
LIBYANG_API_DEF void
914-
lyd_unlink_tree(struct lyd_node *node)
902+
void
903+
lyd_unlink(struct lyd_node *node)
915904
{
916905
struct lyd_node *iter;
917906

@@ -959,6 +948,35 @@ lyd_unlink_tree(struct lyd_node *node)
959948
node->prev = node;
960949
}
961950

951+
LIBYANG_API_DEF void
952+
lyd_unlink_siblings(struct lyd_node *node)
953+
{
954+
struct lyd_node *next, *elem, *first = NULL;
955+
956+
LY_LIST_FOR_SAFE(node, next, elem) {
957+
if (lysc_is_key(elem->schema) && elem->parent) {
958+
LOGERR(LYD_CTX(elem), LY_EINVAL, "Cannot unlink a list key \"%s\", unlink the list instance instead.",
959+
LYD_NAME(elem));
960+
return;
961+
}
962+
963+
lyd_unlink(elem);
964+
lyd_insert_node(NULL, &first, elem, 1);
965+
}
966+
}
967+
968+
LIBYANG_API_DEF void
969+
lyd_unlink_tree(struct lyd_node *node)
970+
{
971+
if (node && lysc_is_key(node->schema) && node->parent) {
972+
LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot unlink a list key \"%s\", unlink the list instance instead.",
973+
LYD_NAME(node));
974+
return;
975+
}
976+
977+
lyd_unlink(node);
978+
}
979+
962980
void
963981
lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt)
964982
{
@@ -2179,7 +2197,7 @@ lyd_merge_sibling_r(struct lyd_node **first_trg, struct lyd_node *parent_trg, co
21792197
/* node not found, merge it */
21802198
if (options & LYD_MERGE_DESTRUCT) {
21812199
dup_src = (struct lyd_node *)sibling_src;
2182-
lyd_unlink_tree(dup_src);
2200+
lyd_unlink(dup_src);
21832201
/* spend it */
21842202
*sibling_src_p = NULL;
21852203
} else {

src/tree_data_free.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ lyd_free_subtree(struct lyd_node *node, ly_bool top)
189189

190190
/* unlink only the nodes from the first level, nodes in subtree are freed all, so no unlink is needed */
191191
if (top) {
192-
lyd_unlink_tree(node);
192+
lyd_unlink(node);
193193
}
194194

195195
free(node);

src/tree_data_internal.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,13 @@ void lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node);
397397
*/
398398
void lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling, struct lyd_node *node, ly_bool last);
399399

400+
/**
401+
* @brief Unlink the specified data subtree.
402+
*
403+
* @param[in] node Data tree node to be unlinked (together with all the children).
404+
*/
405+
void lyd_unlink(struct lyd_node *node);
406+
400407
/**
401408
* @brief Insert a metadata (last) into a parent
402409
*

src/validation.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2046,7 +2046,7 @@ _lyd_validate_op(struct lyd_node *op_tree, struct lyd_node *op_node, const struc
20462046
op_sibling_after = op_subtree->next;
20472047
op_parent = lyd_parent(op_subtree);
20482048

2049-
lyd_unlink_tree(op_subtree);
2049+
lyd_unlink(op_subtree);
20502050
lyd_insert_node(tree_parent, &tree_sibling, op_subtree, 0);
20512051
if (!dep_tree) {
20522052
dep_tree = tree_sibling;
@@ -2088,7 +2088,7 @@ _lyd_validate_op(struct lyd_node *op_tree, struct lyd_node *op_node, const struc
20882088

20892089
cleanup:
20902090
/* restore operation tree */
2091-
lyd_unlink_tree(op_subtree);
2091+
lyd_unlink(op_subtree);
20922092
if (op_sibling_before) {
20932093
lyd_insert_after_node(op_sibling_before, op_subtree);
20942094
lyd_insert_hash(op_subtree);

0 commit comments

Comments
 (0)