Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file tree_data_free.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief Freeing functions for data tree structures |
| 5 | * |
| 6 | * Copyright (c) 2019 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 15 | #include <assert.h> |
| 16 | #include <stdlib.h> |
| 17 | |
Radek Krejci | 7711410 | 2021-03-10 15:21:57 +0100 | [diff] [blame] | 18 | #include "dict.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 19 | #include "hash_table.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 20 | #include "log.h" |
Michal Vasko | 8f702ee | 2024-02-20 15:44:24 +0100 | [diff] [blame] | 21 | #include "ly_common.h" |
Michal Vasko | b475096 | 2022-10-06 15:33:35 +0200 | [diff] [blame] | 22 | #include "plugins_exts/metadata.h" |
Radek Krejci | 849a62a | 2019-05-22 15:29:05 +0200 | [diff] [blame] | 23 | #include "plugins_types.h" |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 24 | #include "tree.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 25 | #include "tree_data.h" |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 26 | #include "tree_data_internal.h" |
aPiecek | 6cf1d16 | 2023-11-08 16:07:00 +0100 | [diff] [blame] | 27 | #include "tree_data_sorted.h" |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 28 | #include "tree_schema.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 29 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 30 | static void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 31 | lyd_free_meta(struct lyd_meta *meta, ly_bool siblings) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 32 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 33 | struct lyd_meta *iter; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 34 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 35 | if (!meta) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 36 | return; |
| 37 | } |
| 38 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 39 | if (meta->parent) { |
| 40 | if (meta->parent->meta == meta) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 41 | if (siblings) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 42 | meta->parent->meta = NULL; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 43 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 44 | meta->parent->meta = meta->next; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 45 | } |
| 46 | } else { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 47 | for (iter = meta->parent->meta; iter->next != meta; iter = iter->next) {} |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 48 | if (iter->next) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 49 | if (siblings) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 50 | iter->next = NULL; |
| 51 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 52 | iter->next = meta->next; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 58 | if (!siblings) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 59 | meta->next = NULL; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 60 | } |
| 61 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 62 | for (iter = meta; iter; ) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 63 | meta = iter; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 64 | iter = iter->next; |
| 65 | |
Michal Vasko | e180ed0 | 2021-02-05 16:31:20 +0100 | [diff] [blame] | 66 | lydict_remove(meta->annotation->module->ctx, meta->name); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 67 | meta->value.realtype->plugin->free(meta->annotation->module->ctx, &meta->value); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 68 | free(meta); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 72 | LIBYANG_API_DEF void |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 73 | lyd_free_meta_single(struct lyd_meta *meta) |
| 74 | { |
| 75 | lyd_free_meta(meta, 0); |
| 76 | } |
| 77 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 78 | LIBYANG_API_DEF void |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 79 | lyd_free_meta_siblings(struct lyd_meta *meta) |
| 80 | { |
| 81 | lyd_free_meta(meta, 1); |
| 82 | } |
| 83 | |
| 84 | static void |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 85 | lyd_free_attr(const struct ly_ctx *ctx, struct lyd_attr *attr, ly_bool siblings) |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 86 | { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 87 | struct lyd_attr *iter; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 88 | |
| 89 | LY_CHECK_ARG_RET(NULL, ctx, ); |
| 90 | if (!attr) { |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | if (attr->parent) { |
| 95 | if (attr->parent->attr == attr) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 96 | if (siblings) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 97 | attr->parent->attr = NULL; |
| 98 | } else { |
| 99 | attr->parent->attr = attr->next; |
| 100 | } |
| 101 | } else { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 102 | for (iter = attr->parent->attr; iter->next != attr; iter = iter->next) {} |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 103 | if (iter->next) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 104 | if (siblings) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 105 | iter->next = NULL; |
| 106 | } else { |
| 107 | iter->next = attr->next; |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 113 | if (!siblings) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 114 | attr->next = NULL; |
| 115 | } |
| 116 | |
| 117 | for (iter = attr; iter; ) { |
| 118 | attr = iter; |
| 119 | iter = iter->next; |
| 120 | |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 121 | ly_free_prefix_data(attr->format, attr->val_prefix_data); |
Michal Vasko | e180ed0 | 2021-02-05 16:31:20 +0100 | [diff] [blame] | 122 | lydict_remove(ctx, attr->name.name); |
| 123 | lydict_remove(ctx, attr->name.prefix); |
| 124 | lydict_remove(ctx, attr->name.module_ns); |
| 125 | lydict_remove(ctx, attr->value); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 126 | free(attr); |
| 127 | } |
| 128 | } |
| 129 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 130 | LIBYANG_API_DEF void |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 131 | lyd_free_attr_single(const struct ly_ctx *ctx, struct lyd_attr *attr) |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 132 | { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 133 | lyd_free_attr(ctx, attr, 0); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 134 | } |
| 135 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 136 | LIBYANG_API_DEF void |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 137 | lyd_free_attr_siblings(const struct ly_ctx *ctx, struct lyd_attr *attr) |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 138 | { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 139 | lyd_free_attr(ctx, attr, 1); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 140 | } |
| 141 | |
steweg | f9041a2 | 2024-01-18 13:29:12 +0100 | [diff] [blame] | 142 | void |
| 143 | lyd_free_leafref_links_rec(struct lyd_leafref_links_rec *rec) |
| 144 | { |
| 145 | LY_ARRAY_COUNT_TYPE u; |
steweg | 6738895 | 2024-01-25 12:14:50 +0100 | [diff] [blame] | 146 | struct lyd_leafref_links_rec *rec2; |
steweg | f9041a2 | 2024-01-18 13:29:12 +0100 | [diff] [blame] | 147 | |
| 148 | assert(rec); |
| 149 | |
steweg | 6738895 | 2024-01-25 12:14:50 +0100 | [diff] [blame] | 150 | /* remove links of leafref nodes */ |
steweg | f9041a2 | 2024-01-18 13:29:12 +0100 | [diff] [blame] | 151 | LY_ARRAY_FOR(rec->leafref_nodes, u) { |
steweg | 6738895 | 2024-01-25 12:14:50 +0100 | [diff] [blame] | 152 | if (lyd_get_or_create_leafref_links_record(rec->leafref_nodes[u], &rec2, 0) == LY_SUCCESS) { |
| 153 | LY_ARRAY_REMOVE_VALUE(rec2->target_nodes, rec->node); |
| 154 | if ((LY_ARRAY_COUNT(rec2->leafref_nodes) == 0) && (LY_ARRAY_COUNT(rec2->target_nodes) == 0)) { |
steweg | f9041a2 | 2024-01-18 13:29:12 +0100 | [diff] [blame] | 155 | lyd_free_leafref_nodes(rec->leafref_nodes[u]); |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | LY_ARRAY_FREE(rec->leafref_nodes); |
| 160 | rec->leafref_nodes = NULL; |
Michal Vasko | b46061d | 2024-01-18 13:58:13 +0100 | [diff] [blame] | 161 | |
steweg | 6738895 | 2024-01-25 12:14:50 +0100 | [diff] [blame] | 162 | /* remove links of target nodes */ |
| 163 | LY_ARRAY_FOR(rec->target_nodes, u) { |
| 164 | if (lyd_get_or_create_leafref_links_record(rec->target_nodes[u], &rec2, 0) == LY_SUCCESS) { |
| 165 | LY_ARRAY_REMOVE_VALUE(rec2->leafref_nodes, rec->node); |
| 166 | if ((LY_ARRAY_COUNT(rec2->leafref_nodes) == 0) && (LY_ARRAY_COUNT(rec2->target_nodes) == 0)) { |
| 167 | lyd_free_leafref_nodes(rec->target_nodes[u]); |
| 168 | } |
| 169 | } |
steweg | f9041a2 | 2024-01-18 13:29:12 +0100 | [diff] [blame] | 170 | } |
steweg | 6738895 | 2024-01-25 12:14:50 +0100 | [diff] [blame] | 171 | LY_ARRAY_FREE(rec->target_nodes); |
| 172 | rec->target_nodes = NULL; |
steweg | f9041a2 | 2024-01-18 13:29:12 +0100 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | void |
| 176 | lyd_free_leafref_nodes(const struct lyd_node_term *node) |
| 177 | { |
Michal Vasko | 7b8f35b | 2024-10-02 09:00:07 +0200 | [diff] [blame] | 178 | struct ly_ctx_data *ctx_data; |
steweg | f9041a2 | 2024-01-18 13:29:12 +0100 | [diff] [blame] | 179 | uint32_t hash; |
| 180 | struct lyd_leafref_links_rec *rec; |
| 181 | |
| 182 | assert(node); |
| 183 | |
Michal Vasko | b46061d | 2024-01-18 13:58:13 +0100 | [diff] [blame] | 184 | if (lyd_get_or_create_leafref_links_record(node, &rec, 0)) { |
steweg | f9041a2 | 2024-01-18 13:29:12 +0100 | [diff] [blame] | 185 | return; |
| 186 | } |
| 187 | |
| 188 | /* free entry content */ |
| 189 | lyd_free_leafref_links_rec(rec); |
| 190 | |
| 191 | /* free entry itself from hash table */ |
Michal Vasko | 7b8f35b | 2024-10-02 09:00:07 +0200 | [diff] [blame] | 192 | ctx_data = ly_ctx_data_get(LYD_CTX(node)); |
Michal Vasko | 67bf587 | 2024-01-18 13:58:25 +0100 | [diff] [blame] | 193 | hash = lyht_hash((const char *)&node, sizeof node); |
Michal Vasko | 7b8f35b | 2024-10-02 09:00:07 +0200 | [diff] [blame] | 194 | lyht_remove(ctx_data->leafref_links_ht, rec, hash); |
steweg | f9041a2 | 2024-01-18 13:29:12 +0100 | [diff] [blame] | 195 | } |
| 196 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 197 | /** |
| 198 | * @brief Free Data (sub)tree. |
Michal Vasko | b46061d | 2024-01-18 13:58:13 +0100 | [diff] [blame] | 199 | * |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 200 | * @param[in] node Data node to be freed. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 201 | */ |
| 202 | static void |
aPiecek | 852d93f | 2023-11-24 13:59:07 +0100 | [diff] [blame] | 203 | lyd_free_subtree(struct lyd_node *node) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 204 | { |
| 205 | struct lyd_node *iter, *next; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 206 | struct lyd_node_opaq *opaq = NULL; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 207 | |
| 208 | assert(node); |
| 209 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 210 | if (!node->schema) { |
| 211 | opaq = (struct lyd_node_opaq *)node; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 212 | |
| 213 | /* free the children */ |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 214 | LY_LIST_FOR_SAFE(lyd_child(node), next, iter) { |
aPiecek | 852d93f | 2023-11-24 13:59:07 +0100 | [diff] [blame] | 215 | lyd_free_subtree(iter); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 216 | } |
| 217 | |
Michal Vasko | e180ed0 | 2021-02-05 16:31:20 +0100 | [diff] [blame] | 218 | lydict_remove(LYD_CTX(opaq), opaq->name.name); |
| 219 | lydict_remove(LYD_CTX(opaq), opaq->name.prefix); |
| 220 | lydict_remove(LYD_CTX(opaq), opaq->name.module_ns); |
| 221 | lydict_remove(LYD_CTX(opaq), opaq->value); |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 222 | ly_free_prefix_data(opaq->format, opaq->val_prefix_data); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 223 | } else if (node->schema->nodetype & LYD_NODE_INNER) { |
| 224 | /* remove children hash table in case of inner data node */ |
Michal Vasko | 77b7f90a | 2023-01-31 15:42:41 +0100 | [diff] [blame] | 225 | lyht_free(((struct lyd_node_inner *)node)->children_ht, NULL); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 226 | |
| 227 | /* free the children */ |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 228 | LY_LIST_FOR_SAFE(lyd_child(node), next, iter) { |
aPiecek | 852d93f | 2023-11-24 13:59:07 +0100 | [diff] [blame] | 229 | lyd_free_subtree(iter); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 230 | } |
| 231 | } else if (node->schema->nodetype & LYD_NODE_ANY) { |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 232 | /* only frees the value this way */ |
| 233 | lyd_any_copy_value(node, NULL, 0); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 234 | } else if (node->schema->nodetype & LYD_NODE_TERM) { |
steweg | f9041a2 | 2024-01-18 13:29:12 +0100 | [diff] [blame] | 235 | struct lyd_node_term *node_term = (struct lyd_node_term *)node; |
| 236 | |
| 237 | ((struct lysc_node_leaf *)node->schema)->type->plugin->free(LYD_CTX(node), &node_term->value); |
| 238 | lyd_free_leafref_nodes(node_term); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 239 | } |
| 240 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 241 | if (!node->schema) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 242 | lyd_free_attr_siblings(LYD_CTX(node), opaq->attr); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 243 | } else { |
| 244 | /* free the node's metadata */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 245 | lyd_free_meta_siblings(node->meta); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 246 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 247 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 248 | free(node); |
| 249 | } |
| 250 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 251 | LIBYANG_API_DEF void |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 252 | lyd_free_tree(struct lyd_node *node) |
| 253 | { |
| 254 | if (!node) { |
| 255 | return; |
| 256 | } |
| 257 | |
Michal Vasko | 0dc4a26 | 2023-09-04 09:27:24 +0200 | [diff] [blame] | 258 | if (lysc_is_key(node->schema) && node->parent) { |
| 259 | LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot free a list key \"%s\", free the list instance instead.", LYD_NAME(node)); |
| 260 | return; |
| 261 | } |
| 262 | |
aPiecek | 852d93f | 2023-11-24 13:59:07 +0100 | [diff] [blame] | 263 | lyd_unlink(node); |
| 264 | lyd_free_subtree(node); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 265 | } |
| 266 | |
Radek Krejci | b0849a2 | 2019-07-25 12:31:04 +0200 | [diff] [blame] | 267 | static void |
aPiecek | 852d93f | 2023-11-24 13:59:07 +0100 | [diff] [blame] | 268 | lyd_free_(struct lyd_node *node) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 269 | { |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 270 | struct lyd_node *iter, *next, *first_sibling = NULL; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 271 | |
| 272 | if (!node) { |
| 273 | return; |
| 274 | } |
| 275 | |
aPiecek | 827cfac | 2023-12-01 14:56:12 +0100 | [diff] [blame] | 276 | LY_LIST_FOR_SAFE(lyd_first_sibling(node), next, iter) { |
Michal Vasko | 0dc4a26 | 2023-09-04 09:27:24 +0200 | [diff] [blame] | 277 | if (lysc_is_key(iter->schema) && iter->parent) { |
Michal Vasko | 687609f | 2023-09-04 10:29:11 +0200 | [diff] [blame] | 278 | LOGERR(LYD_CTX(iter), LY_EINVAL, "Cannot free a list key \"%s\", free the list instance instead.", LYD_NAME(iter)); |
Michal Vasko | 0dc4a26 | 2023-09-04 09:27:24 +0200 | [diff] [blame] | 279 | return; |
| 280 | } |
| 281 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 282 | /* in case of the top-level nodes (node->parent is NULL), no unlinking needed */ |
aPiecek | 852d93f | 2023-11-24 13:59:07 +0100 | [diff] [blame] | 283 | if (iter->parent) { |
aPiecek | 6cf1d16 | 2023-11-08 16:07:00 +0100 | [diff] [blame] | 284 | lyds_free_metadata(iter); |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 285 | lyd_unlink_ignore_lyds(&first_sibling, iter); |
aPiecek | 852d93f | 2023-11-24 13:59:07 +0100 | [diff] [blame] | 286 | } |
| 287 | lyd_free_subtree(iter); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 288 | } |
| 289 | } |
Radek Krejci | b0849a2 | 2019-07-25 12:31:04 +0200 | [diff] [blame] | 290 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 291 | LIBYANG_API_DEF void |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 292 | lyd_free_siblings(struct lyd_node *node) |
Radek Krejci | b0849a2 | 2019-07-25 12:31:04 +0200 | [diff] [blame] | 293 | { |
aPiecek | 852d93f | 2023-11-24 13:59:07 +0100 | [diff] [blame] | 294 | lyd_free_(node); |
Radek Krejci | b0849a2 | 2019-07-25 12:31:04 +0200 | [diff] [blame] | 295 | } |
| 296 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 297 | LIBYANG_API_DEF void |
Radek Krejci | b0849a2 | 2019-07-25 12:31:04 +0200 | [diff] [blame] | 298 | lyd_free_all(struct lyd_node *node) |
| 299 | { |
aPiecek | 852d93f | 2023-11-24 13:59:07 +0100 | [diff] [blame] | 300 | if (!node) { |
| 301 | return; |
| 302 | } |
| 303 | |
| 304 | /* get top-level node */ |
| 305 | for ( ; node->parent; node = lyd_parent(node)) {} |
| 306 | |
| 307 | lyd_free_(node); |
Radek Krejci | b0849a2 | 2019-07-25 12:31:04 +0200 | [diff] [blame] | 308 | } |