Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1 | /** |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 2 | * @file tree_data.c |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 4 | * @author Michal Vasko <mvasko@cesnet.cz> |
Michal Vasko | 6cd9b6b | 2020-06-22 10:05:22 +0200 | [diff] [blame] | 5 | * @brief Data tree functions |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 6 | * |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 7 | * Copyright (c) 2015 - 2022 CESNET, z.s.p.o. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 8 | * |
| 9 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 10 | * You may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * https://opensource.org/licenses/BSD-3-Clause |
| 14 | */ |
| 15 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 16 | #define _GNU_SOURCE |
| 17 | |
| 18 | #include "tree_data.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 19 | |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 20 | #include <assert.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 21 | #include <ctype.h> |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 22 | #include <inttypes.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 23 | #include <stdarg.h> |
| 24 | #include <stdint.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 27 | #include <string.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 28 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 29 | #include "compat.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 30 | #include "context.h" |
| 31 | #include "dict.h" |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 32 | #include "diff.h" |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 33 | #include "hash_table.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 34 | #include "in.h" |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 35 | #include "in_internal.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 36 | #include "log.h" |
Michal Vasko | 8f702ee | 2024-02-20 15:44:24 +0100 | [diff] [blame] | 37 | #include "ly_common.h" |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 38 | #include "parser_data.h" |
| 39 | #include "parser_internal.h" |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 40 | #include "path.h" |
Radek Krejci | 0aa1f70 | 2021-04-01 16:16:19 +0200 | [diff] [blame] | 41 | #include "plugins.h" |
Radek Krejci | f1ca0ac | 2021-04-12 16:00:06 +0200 | [diff] [blame] | 42 | #include "plugins_exts/metadata.h" |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 43 | #include "plugins_internal.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 44 | #include "plugins_types.h" |
| 45 | #include "set.h" |
| 46 | #include "tree.h" |
| 47 | #include "tree_data_internal.h" |
aPiecek | 6cf1d16 | 2023-11-08 16:07:00 +0100 | [diff] [blame] | 48 | #include "tree_data_sorted.h" |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 49 | #include "tree_edit.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 50 | #include "tree_schema.h" |
| 51 | #include "tree_schema_internal.h" |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 52 | #include "validation.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 53 | #include "xml.h" |
| 54 | #include "xpath.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 55 | |
Michal Vasko | f277d36 | 2023-04-24 09:08:31 +0200 | [diff] [blame] | 56 | static LY_ERR lyd_compare_siblings_(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options, |
| 57 | ly_bool parental_schemas_checked); |
| 58 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 59 | static LYD_FORMAT |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 60 | lyd_parse_get_format(const struct ly_in *in, LYD_FORMAT format) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 61 | { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 62 | if (!format && (in->type == LY_IN_FILEPATH)) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 63 | /* unknown format - try to detect it from filename's suffix */ |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 64 | const char *path = in->method.fpath.filepath; |
| 65 | size_t len = strlen(path); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 66 | |
| 67 | /* ignore trailing whitespaces */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 68 | for ( ; len > 0 && isspace(path[len - 1]); len--) {} |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 69 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 70 | if ((len >= LY_XML_SUFFIX_LEN + 1) && |
| 71 | !strncmp(&path[len - LY_XML_SUFFIX_LEN], LY_XML_SUFFIX, LY_XML_SUFFIX_LEN)) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 72 | format = LYD_XML; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 73 | } else if ((len >= LY_JSON_SUFFIX_LEN + 1) && |
| 74 | !strncmp(&path[len - LY_JSON_SUFFIX_LEN], LY_JSON_SUFFIX, LY_JSON_SUFFIX_LEN)) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 75 | format = LYD_JSON; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 76 | } else if ((len >= LY_LYB_SUFFIX_LEN + 1) && |
| 77 | !strncmp(&path[len - LY_LYB_SUFFIX_LEN], LY_LYB_SUFFIX, LY_LYB_SUFFIX_LEN)) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 78 | format = LYD_LYB; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 79 | } /* else still unknown */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 80 | } |
| 81 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 82 | return format; |
| 83 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 84 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 85 | /** |
| 86 | * @brief Parse YANG data into a data tree. |
| 87 | * |
| 88 | * @param[in] ctx libyang context. |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 89 | * @param[in] ext Optional extenion instance to parse data following the schema tree specified in the extension instance |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 90 | * @param[in] parent Parent to connect the parsed nodes to, if any. |
Michal Vasko | b36c56f | 2024-01-31 12:50:07 +0100 | [diff] [blame] | 91 | * @param[in,out] first_p Pointer to the first parsed node. |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 92 | * @param[in] in Input handle to read the input from. |
| 93 | * @param[in] format Expected format of the data in @p in. |
| 94 | * @param[in] parse_opts Options for parser. |
| 95 | * @param[in] val_opts Options for validation. |
| 96 | * @param[out] op Optional pointer to the parsed operation, if any. |
| 97 | * @return LY_ERR value. |
| 98 | */ |
| 99 | static LY_ERR |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 100 | lyd_parse(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent, struct lyd_node **first_p, |
| 101 | struct ly_in *in, LYD_FORMAT format, uint32_t parse_opts, uint32_t val_opts, struct lyd_node **op) |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 102 | { |
steweg | d8e2fc9 | 2023-05-31 09:52:56 +0200 | [diff] [blame] | 103 | LY_ERR r = LY_SUCCESS, rc = LY_SUCCESS; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 104 | struct lyd_ctx *lydctx = NULL; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 105 | struct ly_set parsed = {0}; |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 106 | uint32_t i, int_opts = 0; |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 107 | const struct ly_err_item *eitem; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 108 | ly_bool subtree_sibling = 0; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 109 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 110 | assert(ctx && (parent || first_p)); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 111 | |
| 112 | format = lyd_parse_get_format(in, format); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 113 | if (first_p) { |
| 114 | *first_p = NULL; |
| 115 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 116 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 117 | /* remember input position */ |
| 118 | in->func_start = in->current; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 119 | |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 120 | /* set internal options */ |
| 121 | if (!(parse_opts & LYD_PARSE_SUBTREE)) { |
| 122 | int_opts = LYD_INTOPT_WITH_SIBLINGS; |
| 123 | } |
| 124 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 125 | /* parse the data */ |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 126 | switch (format) { |
| 127 | case LYD_XML: |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 128 | r = lyd_parse_xml(ctx, ext, parent, first_p, in, parse_opts, val_opts, int_opts, &parsed, |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 129 | &subtree_sibling, &lydctx); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 130 | break; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 131 | case LYD_JSON: |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 132 | r = lyd_parse_json(ctx, ext, parent, first_p, in, parse_opts, val_opts, int_opts, &parsed, |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 133 | &subtree_sibling, &lydctx); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 134 | break; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 135 | case LYD_LYB: |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 136 | r = lyd_parse_lyb(ctx, ext, parent, first_p, in, parse_opts, val_opts, int_opts, &parsed, |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 137 | &subtree_sibling, &lydctx); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 138 | break; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 139 | case LYD_UNKNOWN: |
Michal Vasko | d74978f | 2021-02-12 11:59:36 +0100 | [diff] [blame] | 140 | LOGARG(ctx, format); |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 141 | r = LY_EINVAL; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 142 | break; |
| 143 | } |
Michal Vasko | 50da8cd | 2023-03-10 08:38:59 +0100 | [diff] [blame] | 144 | if (r) { |
| 145 | rc = r; |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 146 | if ((r != LY_EVALID) || !lydctx || !(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR)) { |
| 147 | goto cleanup; |
| 148 | } |
| 149 | |
| 150 | eitem = ly_err_last(ctx); |
| 151 | assert(eitem); |
| 152 | if (eitem->vecode == LYVE_SYNTAX) { |
| 153 | /* cannot get more errors on a syntax error */ |
Michal Vasko | 50da8cd | 2023-03-10 08:38:59 +0100 | [diff] [blame] | 154 | goto cleanup; |
| 155 | } |
| 156 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 157 | |
Michal Vasko | a6139e0 | 2023-10-03 14:13:22 +0200 | [diff] [blame] | 158 | if (parent && parsed.count) { |
| 159 | /* use the first parsed node */ |
Michal Vasko | b36c56f | 2024-01-31 12:50:07 +0100 | [diff] [blame] | 160 | if (first_p) { |
| 161 | *first_p = parsed.dnodes[0]; |
| 162 | } else { |
| 163 | first_p = &parsed.dnodes[0]; |
| 164 | } |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 165 | } |
| 166 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 167 | if (!(parse_opts & LYD_PARSE_ONLY)) { |
| 168 | /* validate data */ |
Michal Vasko | 9f6dd7c | 2023-04-06 08:46:30 +0200 | [diff] [blame] | 169 | r = lyd_validate(first_p, NULL, ctx, val_opts, 0, &lydctx->node_when, &lydctx->node_types, &lydctx->meta_types, |
Michal Vasko | 135719f | 2022-08-25 12:18:17 +0200 | [diff] [blame] | 170 | &lydctx->ext_node, &lydctx->ext_val, NULL); |
Michal Vasko | 9f6dd7c | 2023-04-06 08:46:30 +0200 | [diff] [blame] | 171 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 172 | } |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 173 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 174 | /* set the operation node */ |
| 175 | if (op) { |
| 176 | *op = lydctx->op_node; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | cleanup: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 180 | if (lydctx) { |
| 181 | lydctx->free(lydctx); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 182 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 183 | if (rc) { |
| 184 | if (parent) { |
| 185 | /* free all the parsed subtrees */ |
| 186 | for (i = 0; i < parsed.count; ++i) { |
| 187 | lyd_free_tree(parsed.dnodes[i]); |
| 188 | } |
| 189 | } else { |
| 190 | /* free everything */ |
| 191 | lyd_free_all(*first_p); |
| 192 | *first_p = NULL; |
| 193 | } |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 194 | } else if (subtree_sibling) { |
| 195 | rc = LY_ENOT; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 196 | } |
| 197 | ly_set_erase(&parsed, NULL); |
| 198 | return rc; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 199 | } |
| 200 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 201 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 202 | lyd_parse_ext_data(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format, |
| 203 | uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree) |
| 204 | { |
| 205 | const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL; |
| 206 | |
| 207 | LY_CHECK_ARG_RET(ctx, ext, in, parent || tree, LY_EINVAL); |
| 208 | LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL); |
| 209 | LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL); |
| 210 | |
| 211 | return lyd_parse(ctx, ext, parent, tree, in, format, parse_options, validate_options, NULL); |
| 212 | } |
| 213 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 214 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 215 | lyd_parse_data(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format, |
| 216 | uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree) |
| 217 | { |
| 218 | LY_CHECK_ARG_RET(ctx, ctx, in, parent || tree, LY_EINVAL); |
| 219 | LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL); |
| 220 | LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL); |
| 221 | |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 222 | return lyd_parse(ctx, NULL, parent, tree, in, format, parse_options, validate_options, NULL); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 223 | } |
| 224 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 225 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 226 | lyd_parse_data_mem(const struct ly_ctx *ctx, const char *data, LYD_FORMAT format, uint32_t parse_options, |
| 227 | uint32_t validate_options, struct lyd_node **tree) |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 228 | { |
| 229 | LY_ERR ret; |
| 230 | struct ly_in *in; |
| 231 | |
| 232 | LY_CHECK_RET(ly_in_new_memory(data, &in)); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 233 | ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 234 | |
| 235 | ly_in_free(in, 0); |
| 236 | return ret; |
| 237 | } |
| 238 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 239 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 240 | lyd_parse_data_fd(const struct ly_ctx *ctx, int fd, LYD_FORMAT format, uint32_t parse_options, uint32_t validate_options, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 241 | struct lyd_node **tree) |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 242 | { |
| 243 | LY_ERR ret; |
| 244 | struct ly_in *in; |
| 245 | |
| 246 | LY_CHECK_RET(ly_in_new_fd(fd, &in)); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 247 | ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 248 | |
| 249 | ly_in_free(in, 0); |
| 250 | return ret; |
| 251 | } |
| 252 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 253 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 254 | lyd_parse_data_path(const struct ly_ctx *ctx, const char *path, LYD_FORMAT format, uint32_t parse_options, |
| 255 | uint32_t validate_options, struct lyd_node **tree) |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 256 | { |
| 257 | LY_ERR ret; |
| 258 | struct ly_in *in; |
| 259 | |
| 260 | LY_CHECK_RET(ly_in_new_filepath(path, 0, &in)); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 261 | ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 262 | |
| 263 | ly_in_free(in, 0); |
| 264 | return ret; |
| 265 | } |
| 266 | |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 267 | /** |
| 268 | * @brief Parse YANG data into an operation data tree, in case the extension instance is specified, keep the searching |
| 269 | * for schema nodes locked inside the extension instance. |
| 270 | * |
| 271 | * At least one of @p parent, @p tree, or @p op must always be set. |
| 272 | * |
Michal Vasko | 820efe8 | 2023-05-12 15:47:43 +0200 | [diff] [blame] | 273 | * Specific @p data_type values have different parameter meaning as mentioned for ::lyd_parse_op(). |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 274 | * |
| 275 | * @param[in] ctx libyang context. |
| 276 | * @param[in] ext Extension instance providing the specific schema tree to match with the data being parsed. |
| 277 | * @param[in] parent Optional parent to connect the parsed nodes to. |
| 278 | * @param[in] in Input handle to read the input from. |
| 279 | * @param[in] format Expected format of the data in @p in. |
| 280 | * @param[in] data_type Expected operation to parse (@ref datatype). |
| 281 | * @param[out] tree Optional full parsed data tree. If @p parent is set, set to NULL. |
| 282 | * @param[out] op Optional parsed operation node. |
| 283 | * @return LY_ERR value. |
| 284 | * @return LY_ENOT if @p data_type is a NETCONF message and the root XML element is not the expected one. |
| 285 | */ |
| 286 | static LY_ERR |
| 287 | lyd_parse_op_(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent, |
| 288 | struct ly_in *in, LYD_FORMAT format, enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op) |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 289 | { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 290 | LY_ERR rc = LY_SUCCESS; |
| 291 | struct lyd_ctx *lydctx = NULL; |
| 292 | struct ly_set parsed = {0}; |
| 293 | struct lyd_node *first = NULL, *envp = NULL; |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 294 | uint32_t i, parse_opts, val_opts, int_opts = 0; |
Michal Vasko | 820efe8 | 2023-05-12 15:47:43 +0200 | [diff] [blame] | 295 | ly_bool proto_msg = 0; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 296 | |
Michal Vasko | 27fb026 | 2021-02-23 09:42:01 +0100 | [diff] [blame] | 297 | if (!ctx) { |
| 298 | ctx = LYD_CTX(parent); |
| 299 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 300 | if (tree) { |
| 301 | *tree = NULL; |
| 302 | } |
| 303 | if (op) { |
| 304 | *op = NULL; |
| 305 | } |
| 306 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 307 | format = lyd_parse_get_format(in, format); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 308 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 309 | /* remember input position */ |
| 310 | in->func_start = in->current; |
| 311 | |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 312 | /* set parse and validation opts */ |
Michal Vasko | 140ede9 | 2022-05-10 09:27:30 +0200 | [diff] [blame] | 313 | parse_opts = LYD_PARSE_ONLY | LYD_PARSE_STRICT; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 314 | val_opts = 0; |
| 315 | |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 316 | switch (data_type) { |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 317 | case LYD_TYPE_RPC_NETCONF: |
| 318 | case LYD_TYPE_NOTIF_NETCONF: |
| 319 | LY_CHECK_ARG_RET(ctx, format == LYD_XML, !parent, tree, op, LY_EINVAL); |
Michal Vasko | 820efe8 | 2023-05-12 15:47:43 +0200 | [diff] [blame] | 320 | proto_msg = 1; |
| 321 | break; |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 322 | case LYD_TYPE_REPLY_NETCONF: |
Michal Vasko | 820efe8 | 2023-05-12 15:47:43 +0200 | [diff] [blame] | 323 | LY_CHECK_ARG_RET(ctx, format == LYD_XML, parent, parent->schema, parent->schema->nodetype & (LYS_RPC | LYS_ACTION), |
| 324 | tree, !op, LY_EINVAL); |
| 325 | proto_msg = 1; |
| 326 | break; |
| 327 | case LYD_TYPE_RPC_RESTCONF: |
| 328 | case LYD_TYPE_REPLY_RESTCONF: |
| 329 | LY_CHECK_ARG_RET(ctx, parent, parent->schema, parent->schema->nodetype & (LYS_RPC | LYS_ACTION), tree, !op, LY_EINVAL); |
| 330 | proto_msg = 1; |
| 331 | break; |
| 332 | case LYD_TYPE_NOTIF_RESTCONF: |
| 333 | LY_CHECK_ARG_RET(ctx, format == LYD_JSON, !parent, tree, op, LY_EINVAL); |
| 334 | proto_msg = 1; |
| 335 | break; |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 336 | |
Michal Vasko | 820efe8 | 2023-05-12 15:47:43 +0200 | [diff] [blame] | 337 | /* set internal opts */ |
| 338 | case LYD_TYPE_RPC_YANG: |
Michal Vasko | 5df2852 | 2023-08-25 08:04:37 +0200 | [diff] [blame] | 339 | int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | (parent ? LYD_INTOPT_WITH_SIBLINGS : LYD_INTOPT_NO_SIBLINGS); |
Michal Vasko | 820efe8 | 2023-05-12 15:47:43 +0200 | [diff] [blame] | 340 | break; |
| 341 | case LYD_TYPE_NOTIF_YANG: |
Michal Vasko | 5df2852 | 2023-08-25 08:04:37 +0200 | [diff] [blame] | 342 | int_opts = LYD_INTOPT_NOTIF | (parent ? LYD_INTOPT_WITH_SIBLINGS : LYD_INTOPT_NO_SIBLINGS); |
Michal Vasko | 820efe8 | 2023-05-12 15:47:43 +0200 | [diff] [blame] | 343 | break; |
| 344 | case LYD_TYPE_REPLY_YANG: |
Michal Vasko | 5df2852 | 2023-08-25 08:04:37 +0200 | [diff] [blame] | 345 | int_opts = LYD_INTOPT_REPLY | (parent ? LYD_INTOPT_WITH_SIBLINGS : LYD_INTOPT_NO_SIBLINGS); |
Michal Vasko | 820efe8 | 2023-05-12 15:47:43 +0200 | [diff] [blame] | 346 | break; |
| 347 | case LYD_TYPE_DATA_YANG: |
| 348 | LOGINT(ctx); |
| 349 | rc = LY_EINT; |
| 350 | goto cleanup; |
| 351 | } |
| 352 | |
| 353 | /* parse a full protocol message */ |
| 354 | if (proto_msg) { |
| 355 | if (format == LYD_XML) { |
| 356 | /* parse the NETCONF (or RESTCONF XML) message */ |
| 357 | rc = lyd_parse_xml_netconf(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &envp, &parsed, &lydctx); |
| 358 | } else { |
| 359 | /* parse the RESTCONF message */ |
| 360 | rc = lyd_parse_json_restconf(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &envp, &parsed, &lydctx); |
| 361 | } |
Michal Vasko | 3a163bc | 2023-01-10 14:23:56 +0100 | [diff] [blame] | 362 | if (rc) { |
| 363 | if (envp) { |
| 364 | /* special situation when the envelopes were parsed successfully */ |
| 365 | *tree = envp; |
| 366 | } |
Michal Vasko | 73792a1 | 2022-05-10 09:28:45 +0200 | [diff] [blame] | 367 | goto cleanup; |
Michal Vasko | 299d5d1 | 2021-02-16 16:36:37 +0100 | [diff] [blame] | 368 | } |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 369 | |
| 370 | /* set out params correctly */ |
Michal Vasko | 472baa8 | 2022-12-01 16:17:41 +0100 | [diff] [blame] | 371 | if (envp) { |
| 372 | /* special out param meaning */ |
| 373 | *tree = envp; |
| 374 | } else { |
| 375 | *tree = parent ? NULL : first; |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 376 | } |
| 377 | if (op) { |
| 378 | *op = lydctx->op_node; |
| 379 | } |
| 380 | goto cleanup; |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | /* parse the data */ |
| 384 | switch (format) { |
| 385 | case LYD_XML: |
| 386 | rc = lyd_parse_xml(ctx, ext, parent, &first, in, parse_opts, val_opts, int_opts, &parsed, NULL, &lydctx); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 387 | break; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 388 | case LYD_JSON: |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 389 | rc = lyd_parse_json(ctx, ext, parent, &first, in, parse_opts, val_opts, int_opts, &parsed, NULL, &lydctx); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 390 | break; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 391 | case LYD_LYB: |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 392 | rc = lyd_parse_lyb(ctx, ext, parent, &first, in, parse_opts, val_opts, int_opts, &parsed, NULL, &lydctx); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 393 | break; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 394 | case LYD_UNKNOWN: |
Michal Vasko | d74978f | 2021-02-12 11:59:36 +0100 | [diff] [blame] | 395 | LOGARG(ctx, format); |
| 396 | rc = LY_EINVAL; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 397 | break; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 398 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 399 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 400 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 401 | /* set out params correctly */ |
| 402 | if (tree) { |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 403 | *tree = parent ? NULL : first; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 404 | } |
| 405 | if (op) { |
| 406 | *op = lydctx->op_node; |
| 407 | } |
| 408 | |
| 409 | cleanup: |
| 410 | if (lydctx) { |
| 411 | lydctx->free(lydctx); |
| 412 | } |
| 413 | if (rc) { |
Michal Vasko | 73792a1 | 2022-05-10 09:28:45 +0200 | [diff] [blame] | 414 | /* free all the parsed nodes */ |
| 415 | if (parsed.count) { |
| 416 | i = parsed.count; |
| 417 | do { |
| 418 | --i; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 419 | lyd_free_tree(parsed.dnodes[i]); |
Michal Vasko | 73792a1 | 2022-05-10 09:28:45 +0200 | [diff] [blame] | 420 | } while (i); |
| 421 | } |
Michal Vasko | 6fbf07b | 2023-08-08 13:54:48 +0200 | [diff] [blame] | 422 | if (tree && !envp) { |
Michal Vasko | 73792a1 | 2022-05-10 09:28:45 +0200 | [diff] [blame] | 423 | *tree = NULL; |
| 424 | } |
| 425 | if (op) { |
| 426 | *op = NULL; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | ly_set_erase(&parsed, NULL); |
| 430 | return rc; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 431 | } |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 432 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 433 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 434 | lyd_parse_op(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format, |
| 435 | enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op) |
| 436 | { |
| 437 | LY_CHECK_ARG_RET(ctx, ctx || parent, in, data_type, parent || tree || op, LY_EINVAL); |
| 438 | |
| 439 | return lyd_parse_op_(ctx, NULL, parent, in, format, data_type, tree, op); |
| 440 | } |
| 441 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 442 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 443 | lyd_parse_ext_op(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format, |
| 444 | enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op) |
| 445 | { |
| 446 | const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL; |
| 447 | |
| 448 | LY_CHECK_ARG_RET(ctx, ext, in, data_type, parent || tree || op, LY_EINVAL); |
| 449 | |
| 450 | return lyd_parse_op_(ctx, ext, parent, in, format, data_type, tree, op); |
| 451 | } |
| 452 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 453 | struct lyd_node * |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 454 | lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 455 | { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 456 | const struct lysc_node *schema, *sparent; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 457 | struct lyd_node *match = NULL; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 458 | ly_bool found; |
Michal Vasko | 93b1606 | 2020-12-09 18:14:18 +0100 | [diff] [blame] | 459 | uint32_t getnext_opts; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 460 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 461 | assert(new_node); |
| 462 | |
Michal Vasko | 9beceb8 | 2022-04-05 12:14:15 +0200 | [diff] [blame] | 463 | if (!first_sibling || !new_node->schema || (LYD_CTX(first_sibling) != LYD_CTX(new_node))) { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 464 | /* insert at the end, no next anchor */ |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 465 | return NULL; |
| 466 | } |
| 467 | |
Michal Vasko | 93b1606 | 2020-12-09 18:14:18 +0100 | [diff] [blame] | 468 | getnext_opts = 0; |
Michal Vasko | d1e53b9 | 2021-01-28 13:11:06 +0100 | [diff] [blame] | 469 | if (new_node->schema->flags & LYS_IS_OUTPUT) { |
Michal Vasko | 93b1606 | 2020-12-09 18:14:18 +0100 | [diff] [blame] | 470 | getnext_opts = LYS_GETNEXT_OUTPUT; |
| 471 | } |
| 472 | |
Michal Vasko | a2756f0 | 2021-07-09 13:20:28 +0200 | [diff] [blame] | 473 | if (first_sibling->parent && first_sibling->parent->schema && first_sibling->parent->children_ht) { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 474 | /* find the anchor using hashes */ |
| 475 | sparent = first_sibling->parent->schema; |
Michal Vasko | 93b1606 | 2020-12-09 18:14:18 +0100 | [diff] [blame] | 476 | schema = lys_getnext(new_node->schema, sparent, NULL, getnext_opts); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 477 | while (schema) { |
| 478 | /* keep trying to find the first existing instance of the closest following schema sibling, |
| 479 | * otherwise return NULL - inserting at the end */ |
| 480 | if (!lyd_find_sibling_schema(first_sibling, schema, &match)) { |
| 481 | break; |
| 482 | } |
| 483 | |
Michal Vasko | 93b1606 | 2020-12-09 18:14:18 +0100 | [diff] [blame] | 484 | schema = lys_getnext(schema, sparent, NULL, getnext_opts); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 485 | } |
| 486 | } else { |
| 487 | /* find the anchor without hashes */ |
| 488 | match = (struct lyd_node *)first_sibling; |
Michal Vasko | 3a70862 | 2021-07-15 14:20:10 +0200 | [diff] [blame] | 489 | sparent = lysc_data_parent(new_node->schema); |
| 490 | if (!sparent) { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 491 | /* we are in top-level, skip all the data from preceding modules */ |
| 492 | LY_LIST_FOR(match, match) { |
| 493 | if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) { |
| 494 | break; |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | /* get the first schema sibling */ |
Michal Vasko | 93b1606 | 2020-12-09 18:14:18 +0100 | [diff] [blame] | 500 | schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, getnext_opts); |
Michal Vasko | 9a46347 | 2023-08-08 09:43:33 +0200 | [diff] [blame] | 501 | if (!schema) { |
Michal Vasko | 60d929e | 2023-08-21 11:54:42 +0200 | [diff] [blame] | 502 | /* must be a top-level extension instance data, no anchor */ |
| 503 | return NULL; |
Michal Vasko | 9a46347 | 2023-08-08 09:43:33 +0200 | [diff] [blame] | 504 | } |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 505 | |
| 506 | found = 0; |
| 507 | LY_LIST_FOR(match, match) { |
| 508 | if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) { |
| 509 | /* we have found an opaque node, which must be at the end, so use it OR |
| 510 | * modules do not match, so we must have traversed all the data from new_node module (if any), |
| 511 | * we have found the first node of the next module, that is what we want */ |
| 512 | break; |
| 513 | } |
| 514 | |
| 515 | /* skip schema nodes until we find the instantiated one */ |
| 516 | while (!found) { |
| 517 | if (new_node->schema == schema) { |
| 518 | /* we have found the schema of the new node, continue search to find the first |
| 519 | * data node with a different schema (after our schema) */ |
| 520 | found = 1; |
| 521 | break; |
| 522 | } |
| 523 | if (match->schema == schema) { |
| 524 | /* current node (match) is a data node still before the new node, continue search in data */ |
| 525 | break; |
| 526 | } |
Michal Vasko | 9a46347 | 2023-08-08 09:43:33 +0200 | [diff] [blame] | 527 | |
Michal Vasko | 93b1606 | 2020-12-09 18:14:18 +0100 | [diff] [blame] | 528 | schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, getnext_opts); |
Michal Vasko | 9a46347 | 2023-08-08 09:43:33 +0200 | [diff] [blame] | 529 | if (!schema) { |
Michal Vasko | 60d929e | 2023-08-21 11:54:42 +0200 | [diff] [blame] | 530 | /* must be a top-level extension instance data, no anchor */ |
| 531 | return NULL; |
Michal Vasko | 9a46347 | 2023-08-08 09:43:33 +0200 | [diff] [blame] | 532 | } |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | if (found && (match->schema != new_node->schema)) { |
| 536 | /* find the next node after we have found our node schema data instance */ |
| 537 | break; |
| 538 | } |
| 539 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | return match; |
| 543 | } |
| 544 | |
Michal Vasko | c61dd06 | 2022-06-07 11:01:28 +0200 | [diff] [blame] | 545 | void |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 546 | lyd_insert_after_node(struct lyd_node **first_sibling_p, struct lyd_node *sibling, struct lyd_node *node) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 547 | { |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 548 | struct lyd_node_inner *par; |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 549 | struct lyd_node *first_sibling; |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 550 | |
aPiecek | 9aa8301 | 2024-10-17 15:40:17 +0200 | [diff] [blame^] | 551 | assert(!node->next && (node->prev == node) && (sibling != node)); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 552 | |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 553 | if (sibling->next) { |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 554 | /* sibling had a succeeding node */ |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 555 | sibling->next->prev = node; |
| 556 | node->next = sibling->next; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 557 | } else { |
| 558 | /* sibling was last, find first sibling and change its prev */ |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 559 | if (first_sibling_p && *first_sibling_p) { |
| 560 | assert(!(*first_sibling_p)->prev->next); |
| 561 | (*first_sibling_p)->prev = node; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 562 | } else { |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 563 | first_sibling = lyd_first_sibling(sibling); |
| 564 | first_sibling->prev = node; |
| 565 | if (first_sibling_p) { |
| 566 | *first_sibling_p = first_sibling; |
| 567 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 568 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 569 | } |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 570 | node->prev = sibling; |
| 571 | sibling->next = node; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 572 | node->parent = sibling->parent; |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 573 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 574 | for (par = node->parent; par; par = par->parent) { |
| 575 | if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) { |
| 576 | /* remove default flags from NP containers */ |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 577 | par->flags &= ~LYD_DEFAULT; |
| 578 | } |
| 579 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 580 | } |
| 581 | |
Michal Vasko | c61dd06 | 2022-06-07 11:01:28 +0200 | [diff] [blame] | 582 | void |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 583 | lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 584 | { |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 585 | struct lyd_node_inner *par; |
| 586 | |
aPiecek | 9aa8301 | 2024-10-17 15:40:17 +0200 | [diff] [blame^] | 587 | assert(!node->next && (node->prev == node) && (sibling != node)); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 588 | |
| 589 | node->next = sibling; |
| 590 | /* covers situation of sibling being first */ |
| 591 | node->prev = sibling->prev; |
| 592 | sibling->prev = node; |
| 593 | if (node->prev->next) { |
| 594 | /* sibling had a preceding node */ |
| 595 | node->prev->next = node; |
| 596 | } else if (sibling->parent) { |
| 597 | /* sibling was first and we must also change parent child pointer */ |
| 598 | sibling->parent->child = node; |
| 599 | } |
| 600 | node->parent = sibling->parent; |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 601 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 602 | for (par = node->parent; par; par = par->parent) { |
| 603 | if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) { |
| 604 | /* remove default flags from NP containers */ |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 605 | par->flags &= ~LYD_DEFAULT; |
| 606 | } |
| 607 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | /** |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 611 | * @brief Insert node as the first and only child of a parent. |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 612 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 613 | * Handles inserting into NP containers and key-less lists. |
| 614 | * |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 615 | * @param[in] parent Parent to insert into. |
| 616 | * @param[in] node Node to insert. |
| 617 | */ |
| 618 | static void |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 619 | lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 620 | { |
| 621 | struct lyd_node_inner *par; |
| 622 | |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 623 | assert(parent && !lyd_child(parent) && !node->next && (node->prev == node)); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 624 | assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER)); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 625 | |
| 626 | par = (struct lyd_node_inner *)parent; |
| 627 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 628 | par->child = node; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 629 | node->parent = par; |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 630 | |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 631 | for ( ; par; par = par->parent) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 632 | if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) { |
| 633 | /* remove default flags from NP containers */ |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 634 | par->flags &= ~LYD_DEFAULT; |
| 635 | } |
| 636 | } |
Michal Vasko | 751cb4d | 2020-07-14 12:25:28 +0200 | [diff] [blame] | 637 | } |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 638 | |
Michal Vasko | 751cb4d | 2020-07-14 12:25:28 +0200 | [diff] [blame] | 639 | /** |
| 640 | * @brief Learn whether a list instance has all the keys. |
| 641 | * |
| 642 | * @param[in] list List instance to check. |
| 643 | * @return non-zero if all the keys were found, |
| 644 | * @return 0 otherwise. |
| 645 | */ |
| 646 | static int |
| 647 | lyd_insert_has_keys(const struct lyd_node *list) |
| 648 | { |
| 649 | const struct lyd_node *key; |
| 650 | const struct lysc_node *skey = NULL; |
| 651 | |
| 652 | assert(list->schema->nodetype == LYS_LIST); |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 653 | key = lyd_child(list); |
Michal Vasko | 751cb4d | 2020-07-14 12:25:28 +0200 | [diff] [blame] | 654 | while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) { |
| 655 | if (!key || (key->schema != skey)) { |
| 656 | /* key missing */ |
| 657 | return 0; |
| 658 | } |
| 659 | |
| 660 | key = key->next; |
| 661 | } |
| 662 | |
| 663 | /* all keys found */ |
| 664 | return 1; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 665 | } |
| 666 | |
aPiecek | 6cf1d16 | 2023-11-08 16:07:00 +0100 | [diff] [blame] | 667 | /** |
| 668 | * @brief Get the first subsequent data node that contains a different schema definition. |
| 669 | * |
| 670 | * @param[in] first_sibling First sibling, NULL if no top-level sibling exist yet. |
| 671 | * @param[in] node Node to be inserted. |
| 672 | * @return Subsequent data node with a different schema. |
| 673 | */ |
| 674 | static struct lyd_node * |
| 675 | lyd_insert_node_find_anchor(struct lyd_node *first_sibling, struct lyd_node *node) |
| 676 | { |
| 677 | struct lyd_node *anchor; |
| 678 | |
| 679 | if (first_sibling && (first_sibling->flags & LYD_EXT)) { |
| 680 | return NULL; |
| 681 | } |
| 682 | |
| 683 | /* find the anchor, so we can insert somewhere before it */ |
| 684 | anchor = lyd_insert_get_next_anchor(first_sibling, node); |
| 685 | /* cannot insert data node after opaque nodes */ |
| 686 | if (!anchor && node->schema && first_sibling && !first_sibling->prev->schema) { |
| 687 | anchor = first_sibling->prev; |
| 688 | while ((anchor != first_sibling) && !anchor->prev->schema) { |
| 689 | anchor = anchor->prev; |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | return anchor; |
| 694 | } |
| 695 | |
| 696 | /** |
aPiecek | 6188085 | 2024-01-11 14:10:29 +0100 | [diff] [blame] | 697 | * @brief Insert @p node as the last node. |
aPiecek | 6cf1d16 | 2023-11-08 16:07:00 +0100 | [diff] [blame] | 698 | * |
| 699 | * @param[in] parent Parent to insert into, NULL for top-level sibling. |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 700 | * @param[in,out] first_sibling First sibling, NULL if no top-level sibling exist yet. |
| 701 | * Can be also NULL if @p parent is set. |
aPiecek | 6cf1d16 | 2023-11-08 16:07:00 +0100 | [diff] [blame] | 702 | * @param[in] node Individual node (without siblings) to insert. |
aPiecek | 6cf1d16 | 2023-11-08 16:07:00 +0100 | [diff] [blame] | 703 | */ |
| 704 | static void |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 705 | lyd_insert_node_last(struct lyd_node *parent, struct lyd_node **first_sibling, struct lyd_node *node) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 706 | { |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 707 | assert(first_sibling && node); |
| 708 | |
| 709 | if (*first_sibling) { |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 710 | lyd_insert_after_node(first_sibling, (*first_sibling)->prev, node); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 711 | } else if (parent) { |
| 712 | lyd_insert_only_child(parent, node); |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 713 | *first_sibling = node; |
| 714 | } else { |
| 715 | *first_sibling = node; |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 716 | } |
aPiecek | 6cf1d16 | 2023-11-08 16:07:00 +0100 | [diff] [blame] | 717 | } |
| 718 | |
aPiecek | e6955f4 | 2024-01-19 14:01:54 +0100 | [diff] [blame] | 719 | void |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 720 | lyd_insert_node_ordby_schema(struct lyd_node *parent, struct lyd_node **first_sibling, struct lyd_node *node) |
aPiecek | 6cf1d16 | 2023-11-08 16:07:00 +0100 | [diff] [blame] | 721 | { |
aPiecek | 6188085 | 2024-01-11 14:10:29 +0100 | [diff] [blame] | 722 | struct lyd_node *anchor; |
aPiecek | 6cf1d16 | 2023-11-08 16:07:00 +0100 | [diff] [blame] | 723 | |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 724 | assert(first_sibling && node); |
| 725 | |
| 726 | if ((anchor = lyd_insert_node_find_anchor(*first_sibling, node))) { |
aPiecek | 6cf1d16 | 2023-11-08 16:07:00 +0100 | [diff] [blame] | 727 | lyd_insert_before_node(anchor, node); |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 728 | *first_sibling = *first_sibling != anchor ? *first_sibling : node; |
aPiecek | 6f9253c | 2024-02-13 11:05:12 +0100 | [diff] [blame] | 729 | } else if (*first_sibling && node->schema && !(*first_sibling)->prev->schema) { |
| 730 | /* cannot insert data node after opaque nodes */ |
| 731 | anchor = (*first_sibling)->prev; |
| 732 | while ((anchor != *first_sibling) && !anchor->prev->schema) { |
| 733 | anchor = anchor->prev; |
| 734 | } |
| 735 | lyd_insert_before_node(anchor, node); |
| 736 | *first_sibling = *first_sibling != anchor ? *first_sibling : node; |
aPiecek | 6cf1d16 | 2023-11-08 16:07:00 +0100 | [diff] [blame] | 737 | } else { |
aPiecek | 6188085 | 2024-01-11 14:10:29 +0100 | [diff] [blame] | 738 | lyd_insert_node_last(parent, first_sibling, node); |
aPiecek | 6cf1d16 | 2023-11-08 16:07:00 +0100 | [diff] [blame] | 739 | } |
aPiecek | 6cf1d16 | 2023-11-08 16:07:00 +0100 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | void |
aPiecek | 1462ab1 | 2024-02-07 09:13:29 +0100 | [diff] [blame] | 743 | lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling_p, struct lyd_node *node, uint32_t order) |
aPiecek | 6cf1d16 | 2023-11-08 16:07:00 +0100 | [diff] [blame] | 744 | { |
| 745 | LY_ERR ret = LY_SUCCESS; |
aPiecek | 6188085 | 2024-01-11 14:10:29 +0100 | [diff] [blame] | 746 | struct lyd_node *first_sibling, *leader; |
aPiecek | 6cf1d16 | 2023-11-08 16:07:00 +0100 | [diff] [blame] | 747 | |
| 748 | /* inserting list without its keys is not supported */ |
| 749 | assert((parent || first_sibling_p) && node && (node->hash || !node->schema)); |
| 750 | assert(!parent || !parent->schema || |
| 751 | (parent->schema->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_RPC | LYS_ACTION | LYS_NOTIF))); |
| 752 | |
aPiecek | 6188085 | 2024-01-11 14:10:29 +0100 | [diff] [blame] | 753 | if (!parent && first_sibling_p && (*first_sibling_p)) { |
aPiecek | 6cf1d16 | 2023-11-08 16:07:00 +0100 | [diff] [blame] | 754 | parent = lyd_parent(*first_sibling_p); |
| 755 | } |
aPiecek | 6188085 | 2024-01-11 14:10:29 +0100 | [diff] [blame] | 756 | first_sibling = parent ? lyd_child(parent) : *first_sibling_p; |
aPiecek | 6cf1d16 | 2023-11-08 16:07:00 +0100 | [diff] [blame] | 757 | |
aPiecek | 6f9253c | 2024-02-13 11:05:12 +0100 | [diff] [blame] | 758 | if ((order == LYD_INSERT_NODE_LAST) || !node->schema || (first_sibling && (first_sibling->flags & LYD_EXT))) { |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 759 | lyd_insert_node_last(parent, &first_sibling, node); |
aPiecek | 1462ab1 | 2024-02-07 09:13:29 +0100 | [diff] [blame] | 760 | } else if (order == LYD_INSERT_NODE_LAST_BY_SCHEMA) { |
| 761 | lyd_insert_node_ordby_schema(parent, &first_sibling, node); |
aPiecek | 6188085 | 2024-01-11 14:10:29 +0100 | [diff] [blame] | 762 | } else if (lyds_is_supported(node) && |
| 763 | (lyd_find_sibling_schema(first_sibling, node->schema, &leader) == LY_SUCCESS)) { |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 764 | ret = lyds_insert(&first_sibling, &leader, node); |
aPiecek | 6cf1d16 | 2023-11-08 16:07:00 +0100 | [diff] [blame] | 765 | if (ret) { |
| 766 | /* The operation on the sorting tree unexpectedly failed due to some internal issue, |
| 767 | * but insert the node anyway although the nodes will not be sorted. |
| 768 | */ |
| 769 | LOGWRN(LYD_CTX(node), "Data in \"%s\" are not sorted.", node->schema->name); |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 770 | lyd_insert_node_ordby_schema(parent, &first_sibling, node); |
aPiecek | 6cf1d16 | 2023-11-08 16:07:00 +0100 | [diff] [blame] | 771 | } |
| 772 | } else { |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 773 | lyd_insert_node_ordby_schema(parent, &first_sibling, node); |
aPiecek | 6cf1d16 | 2023-11-08 16:07:00 +0100 | [diff] [blame] | 774 | } |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 775 | |
| 776 | /* insert into parent HT */ |
| 777 | lyd_insert_hash(node); |
| 778 | |
| 779 | /* finish hashes for our parent, if needed and possible */ |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 780 | if (node->schema && (node->schema->flags & LYS_KEY) && parent && parent->schema && lyd_insert_has_keys(parent)) { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 781 | lyd_hash(parent); |
| 782 | |
| 783 | /* now we can insert even the list into its parent HT */ |
| 784 | lyd_insert_hash(parent); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 785 | } |
aPiecek | 6188085 | 2024-01-11 14:10:29 +0100 | [diff] [blame] | 786 | |
| 787 | if (first_sibling_p) { |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 788 | *first_sibling_p = first_sibling; |
aPiecek | 6188085 | 2024-01-11 14:10:29 +0100 | [diff] [blame] | 789 | } |
aPiecek | 1462ab1 | 2024-02-07 09:13:29 +0100 | [diff] [blame] | 790 | |
| 791 | #ifndef NDEBUG |
| 792 | if ((order == LYD_INSERT_NODE_LAST) && lyds_is_supported(node) && |
| 793 | (node->prev->schema == node->schema) && (lyds_compare_single(node->prev, node) > 0)) { |
| 794 | LOGWRN(LYD_CTX(node), "Data in \"%s\" are not sorted, inserted node should not be added to the end.", |
| 795 | node->schema->name); |
| 796 | } |
| 797 | #endif |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 798 | } |
| 799 | |
Michal Vasko | 717a4c3 | 2020-12-07 09:40:10 +0100 | [diff] [blame] | 800 | /** |
aPiecek | cada0d7 | 2024-01-11 15:04:12 +0100 | [diff] [blame] | 801 | * @brief Check that @p node can be unlinked. |
| 802 | * |
| 803 | * @param[in] node Node to check |
| 804 | * @return LY_ERR value. |
| 805 | */ |
| 806 | static LY_ERR |
| 807 | lyd_unlink_check(struct lyd_node *node) |
| 808 | { |
| 809 | if (!node) { |
| 810 | return LY_SUCCESS; |
| 811 | } |
| 812 | |
| 813 | if (lysc_is_key(node->schema) && node->parent) { |
| 814 | LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot unlink a list key \"%s\", unlink the list instance instead.", |
| 815 | LYD_NAME(node)); |
| 816 | return LY_EINVAL; |
| 817 | } |
| 818 | |
| 819 | return LY_SUCCESS; |
| 820 | } |
| 821 | |
| 822 | /** |
| 823 | * @brief Move schema instances before anchor or as the last. |
| 824 | * |
| 825 | * The nodes will remain sorted according to the schema. |
| 826 | * |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 827 | * @param[in] first_dst First sibling, destination. |
aPiecek | cada0d7 | 2024-01-11 15:04:12 +0100 | [diff] [blame] | 828 | * @param[in] node Starting node, all following nodes with the same schema will be moved. |
| 829 | * @param[out] next_p Next node that has a different schema or NULL. |
| 830 | * @return LY_ERR value. |
| 831 | */ |
| 832 | static LY_ERR |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 833 | lyd_move_nodes_ordby_schema(struct lyd_node **first_dst, struct lyd_node *node, struct lyd_node **next_p) |
aPiecek | cada0d7 | 2024-01-11 15:04:12 +0100 | [diff] [blame] | 834 | { |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 835 | struct lyd_node *second, *anchor, *iter, *next, *dst, *src, *first_src = NULL; |
aPiecek | cada0d7 | 2024-01-11 15:04:12 +0100 | [diff] [blame] | 836 | |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 837 | assert(first_dst && *first_dst && !(*first_dst)->prev->next && node && next_p); |
aPiecek | cada0d7 | 2024-01-11 15:04:12 +0100 | [diff] [blame] | 838 | |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 839 | if ((anchor = lyd_insert_node_find_anchor(*first_dst, node))) { |
aPiecek | cada0d7 | 2024-01-11 15:04:12 +0100 | [diff] [blame] | 840 | /* move the first node to the correct place according to the schema */ |
| 841 | LY_CHECK_RET(lyd_unlink_check(node)); |
| 842 | second = node->next; |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 843 | lyd_unlink_ignore_lyds(&first_src, node); |
aPiecek | cada0d7 | 2024-01-11 15:04:12 +0100 | [diff] [blame] | 844 | lyd_insert_before_node(anchor, node); |
| 845 | lyd_insert_hash(node); |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 846 | *first_dst = *first_dst != anchor ? *first_dst : node; |
aPiecek | cada0d7 | 2024-01-11 15:04:12 +0100 | [diff] [blame] | 847 | if (!second || (node->schema != second->schema)) { |
| 848 | /* no more nodes to move */ |
| 849 | *next_p = second; |
| 850 | return LY_SUCCESS; |
| 851 | } |
| 852 | dst = node; |
| 853 | src = second; |
| 854 | } else { |
| 855 | /* just move all instances to the end */ |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 856 | dst = (*first_dst)->prev; |
aPiecek | cada0d7 | 2024-01-11 15:04:12 +0100 | [diff] [blame] | 857 | src = node; |
| 858 | } |
| 859 | |
| 860 | /* move the rest of source instances after @p node */ |
| 861 | LY_LIST_FOR_SAFE(src, next, iter) { |
| 862 | LY_CHECK_RET(lyd_unlink_check(iter)); |
| 863 | if (iter->schema != src->schema) { |
| 864 | break; |
| 865 | } |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 866 | lyd_unlink_ignore_lyds(&first_src, iter); |
| 867 | lyd_insert_after_node(first_dst, dst, iter); |
aPiecek | cada0d7 | 2024-01-11 15:04:12 +0100 | [diff] [blame] | 868 | lyd_insert_hash(iter); |
| 869 | dst = iter; |
| 870 | } |
| 871 | *next_p = iter; |
| 872 | |
| 873 | return LY_SUCCESS; |
| 874 | } |
| 875 | |
| 876 | /** |
| 877 | * @brief Move nodes regardless of schema. |
| 878 | * |
| 879 | * The destination for the move is NULL, or a childless parent. |
| 880 | * |
| 881 | * @param[in] parent Parent to insert into, NULL for top-level sibling. |
| 882 | * @param[in] first_src First sibling, all following nodes will be moved. |
| 883 | * @return LY_ERR value. |
| 884 | */ |
| 885 | static LY_ERR |
| 886 | lyd_move_nodes_at_once(struct lyd_node *parent, struct lyd_node *first_src) |
| 887 | { |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 888 | struct lyd_node *start, *next, *iter, *first_dst; |
aPiecek | cada0d7 | 2024-01-11 15:04:12 +0100 | [diff] [blame] | 889 | |
| 890 | assert(!lyd_child(parent) && first_src && !first_src->prev->next && !first_src->parent); |
| 891 | |
| 892 | LY_CHECK_RET(lyd_unlink_check(first_src)); |
| 893 | |
| 894 | /* move the first node */ |
| 895 | start = first_src->next; |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 896 | first_dst = first_src; |
aPiecek | cada0d7 | 2024-01-11 15:04:12 +0100 | [diff] [blame] | 897 | if (parent) { |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 898 | lyd_unlink_ignore_lyds(&first_src, first_dst); |
| 899 | lyd_insert_only_child(parent, first_dst); |
| 900 | lyd_insert_hash(first_dst); |
aPiecek | cada0d7 | 2024-01-11 15:04:12 +0100 | [diff] [blame] | 901 | } else { |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 902 | lyd_unlink_ignore_lyds(&first_src, first_dst); |
aPiecek | cada0d7 | 2024-01-11 15:04:12 +0100 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | /* move the rest of the nodes */ |
| 906 | LY_LIST_FOR_SAFE(start, next, iter) { |
| 907 | LY_CHECK_RET(lyd_unlink_check(iter)); |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 908 | lyd_unlink_ignore_lyds(&first_src, iter); |
| 909 | lyd_insert_after_node(&first_dst, first_dst->prev, iter); |
aPiecek | cada0d7 | 2024-01-11 15:04:12 +0100 | [diff] [blame] | 910 | lyd_insert_hash(iter); |
| 911 | } |
| 912 | |
| 913 | return LY_SUCCESS; |
| 914 | } |
| 915 | |
| 916 | /** |
| 917 | * @brief Move the nodes in parts according to the schema. |
| 918 | * |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 919 | * @param[in,out] first_dst First sibling, destination. |
aPiecek | cada0d7 | 2024-01-11 15:04:12 +0100 | [diff] [blame] | 920 | * @param[in] first_src First sibling, all following nodes will be moved. |
| 921 | * @return LY_ERR value. |
| 922 | */ |
| 923 | static LY_ERR |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 924 | lyd_move_nodes_by_schema(struct lyd_node **first_dst, struct lyd_node *first_src) |
aPiecek | cada0d7 | 2024-01-11 15:04:12 +0100 | [diff] [blame] | 925 | { |
| 926 | LY_ERR ret; |
| 927 | struct lyd_node *next, *iter, *leader; |
| 928 | |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 929 | assert(first_dst && *first_dst && !(*first_dst)->prev->next && first_src && |
| 930 | !first_src->prev->next && !first_src->parent); |
aPiecek | cada0d7 | 2024-01-11 15:04:12 +0100 | [diff] [blame] | 931 | |
| 932 | for (iter = first_src; iter; iter = next) { |
| 933 | if (lyds_is_supported(iter) && |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 934 | (lyd_find_sibling_schema(*first_dst, iter->schema, &leader) == LY_SUCCESS)) { |
| 935 | ret = lyds_merge(first_dst, &leader, &first_src, iter, &next); |
aPiecek | cada0d7 | 2024-01-11 15:04:12 +0100 | [diff] [blame] | 936 | if (ret) { |
| 937 | /* The operation on the sorting tree unexpectedly failed due to some internal issue, |
| 938 | * but insert the node anyway although the nodes will not be sorted. |
| 939 | */ |
| 940 | LOGWRN(LYD_CTX(first_src), "Data in \"%s\" are not sorted.", leader->schema->name); |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 941 | LY_CHECK_RET(lyd_move_nodes_ordby_schema(first_dst, next, &next)); |
aPiecek | cada0d7 | 2024-01-11 15:04:12 +0100 | [diff] [blame] | 942 | } |
| 943 | } else { |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 944 | LY_CHECK_RET(lyd_move_nodes_ordby_schema(first_dst, iter, &next)); |
aPiecek | cada0d7 | 2024-01-11 15:04:12 +0100 | [diff] [blame] | 945 | } |
aPiecek | cada0d7 | 2024-01-11 15:04:12 +0100 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | return LY_SUCCESS; |
| 949 | } |
| 950 | |
| 951 | /** |
| 952 | * @brief Move a nodes into parent/siblings. |
| 953 | * |
| 954 | * @param[in] parent Parent to insert into, NULL for top-level sibling. |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 955 | * @param[in,out] first_dst_p First sibling, NULL if no top-level sibling exist yet. |
| 956 | * Can be also NULL if @p parent is set. |
aPiecek | cada0d7 | 2024-01-11 15:04:12 +0100 | [diff] [blame] | 957 | * @param[in] first_src First sibling, all following nodes will be moved. |
| 958 | * @return LY_ERR value. |
| 959 | */ |
| 960 | static LY_ERR |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 961 | lyd_move_nodes(struct lyd_node *parent, struct lyd_node **first_dst_p, struct lyd_node *first_src) |
aPiecek | cada0d7 | 2024-01-11 15:04:12 +0100 | [diff] [blame] | 962 | { |
| 963 | LY_ERR ret; |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 964 | struct lyd_node *first_dst; |
aPiecek | cada0d7 | 2024-01-11 15:04:12 +0100 | [diff] [blame] | 965 | |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 966 | assert((parent || first_dst_p) && first_src && !first_src->prev->next); |
aPiecek | cada0d7 | 2024-01-11 15:04:12 +0100 | [diff] [blame] | 967 | |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 968 | if (!first_dst_p || !*first_dst_p) { |
| 969 | first_dst = lyd_child(parent); |
aPiecek | cada0d7 | 2024-01-11 15:04:12 +0100 | [diff] [blame] | 970 | } else { |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 971 | first_dst = *first_dst_p; |
| 972 | } |
| 973 | |
| 974 | if (first_dst) { |
| 975 | ret = lyd_move_nodes_by_schema(&first_dst, first_src); |
| 976 | } else { |
| 977 | ret = lyd_move_nodes_at_once(parent, first_src); |
| 978 | first_dst = first_src; |
| 979 | } |
| 980 | |
| 981 | if (first_dst_p) { |
| 982 | *first_dst_p = first_dst; |
aPiecek | cada0d7 | 2024-01-11 15:04:12 +0100 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | return ret; |
| 986 | } |
| 987 | |
| 988 | /** |
Michal Vasko | 717a4c3 | 2020-12-07 09:40:10 +0100 | [diff] [blame] | 989 | * @brief Check schema place of a node to be inserted. |
| 990 | * |
| 991 | * @param[in] parent Schema node of the parent data node. |
| 992 | * @param[in] sibling Schema node of a sibling data node. |
| 993 | * @param[in] schema Schema node if the data node to be inserted. |
| 994 | * @return LY_SUCCESS on success. |
| 995 | * @return LY_EINVAL if the place is invalid. |
| 996 | */ |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 997 | static LY_ERR |
Michal Vasko | 717a4c3 | 2020-12-07 09:40:10 +0100 | [diff] [blame] | 998 | lyd_insert_check_schema(const struct lysc_node *parent, const struct lysc_node *sibling, const struct lysc_node *schema) |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 999 | { |
| 1000 | const struct lysc_node *par2; |
| 1001 | |
Michal Vasko | 62ed12d | 2020-05-21 10:08:25 +0200 | [diff] [blame] | 1002 | assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE))); |
Michal Vasko | 717a4c3 | 2020-12-07 09:40:10 +0100 | [diff] [blame] | 1003 | assert(!sibling || !(sibling->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 1004 | assert(!schema || !(schema->nodetype & (LYS_CASE | LYS_CHOICE))); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1005 | |
Michal Vasko | 717a4c3 | 2020-12-07 09:40:10 +0100 | [diff] [blame] | 1006 | if (!schema || (!parent && !sibling)) { |
Michal Vasko | 71e795e | 2020-12-04 16:27:37 +0100 | [diff] [blame] | 1007 | /* opaque nodes can be inserted wherever */ |
| 1008 | return LY_SUCCESS; |
| 1009 | } |
| 1010 | |
Michal Vasko | 717a4c3 | 2020-12-07 09:40:10 +0100 | [diff] [blame] | 1011 | if (!parent) { |
| 1012 | parent = lysc_data_parent(sibling); |
| 1013 | } |
| 1014 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1015 | /* find schema parent */ |
Michal Vasko | 62ed12d | 2020-05-21 10:08:25 +0200 | [diff] [blame] | 1016 | par2 = lysc_data_parent(schema); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1017 | |
| 1018 | if (parent) { |
| 1019 | /* inner node */ |
| 1020 | if (par2 != parent) { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1021 | LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1022 | parent->name); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1023 | return LY_EINVAL; |
| 1024 | } |
| 1025 | } else { |
| 1026 | /* top-level node */ |
| 1027 | if (par2) { |
Radek Krejci | f6d14cb | 2020-07-02 16:11:45 +0200 | [diff] [blame] | 1028 | LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1029 | return LY_EINVAL; |
| 1030 | } |
| 1031 | } |
| 1032 | |
| 1033 | return LY_SUCCESS; |
| 1034 | } |
| 1035 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1036 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1037 | lyd_insert_child(struct lyd_node *parent, struct lyd_node *node) |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1038 | { |
Michal Vasko | 0ab974d | 2021-02-24 13:18:26 +0100 | [diff] [blame] | 1039 | LY_CHECK_ARG_RET(NULL, parent, node, !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 1040 | LY_CHECK_CTX_EQUAL_RET(LYD_CTX(parent), LYD_CTX(node), LY_EINVAL); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1041 | |
Michal Vasko | 717a4c3 | 2020-12-07 09:40:10 +0100 | [diff] [blame] | 1042 | LY_CHECK_RET(lyd_insert_check_schema(parent->schema, NULL, node->schema)); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1043 | |
aPiecek | cada0d7 | 2024-01-11 15:04:12 +0100 | [diff] [blame] | 1044 | if (node->parent || node->prev->next || !node->next) { |
aPiecek | dfde77d | 2024-01-15 15:16:01 +0100 | [diff] [blame] | 1045 | LY_CHECK_RET(lyd_unlink_tree(node)); |
aPiecek | 1462ab1 | 2024-02-07 09:13:29 +0100 | [diff] [blame] | 1046 | lyd_insert_node(parent, NULL, node, LYD_INSERT_NODE_DEFAULT); |
aPiecek | cada0d7 | 2024-01-11 15:04:12 +0100 | [diff] [blame] | 1047 | } else { |
aPiecek | dfde77d | 2024-01-15 15:16:01 +0100 | [diff] [blame] | 1048 | LY_CHECK_RET(lyd_move_nodes(parent, NULL, node)); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1049 | } |
aPiecek | cada0d7 | 2024-01-11 15:04:12 +0100 | [diff] [blame] | 1050 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1051 | return LY_SUCCESS; |
| 1052 | } |
| 1053 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1054 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1055 | lyplg_ext_insert(struct lyd_node *parent, struct lyd_node *first) |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1056 | { |
| 1057 | struct lyd_node *iter; |
| 1058 | |
| 1059 | LY_CHECK_ARG_RET(NULL, parent, first, !first->parent, !first->prev->next, |
| 1060 | !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL); |
| 1061 | |
| 1062 | if (first->schema && (first->schema->flags & LYS_KEY)) { |
| 1063 | LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", first->schema->name); |
| 1064 | return LY_EINVAL; |
| 1065 | } |
| 1066 | |
| 1067 | while (first) { |
| 1068 | iter = first->next; |
Michal Vasko | 2e784f8 | 2024-01-11 09:51:22 +0100 | [diff] [blame] | 1069 | lyd_unlink(first); |
aPiecek | 1462ab1 | 2024-02-07 09:13:29 +0100 | [diff] [blame] | 1070 | lyd_insert_node(parent, NULL, first, LYD_INSERT_NODE_LAST); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1071 | first = iter; |
| 1072 | } |
| 1073 | return LY_SUCCESS; |
| 1074 | } |
| 1075 | |
| 1076 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1077 | lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first) |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1078 | { |
aPiecek | cada0d7 | 2024-01-11 15:04:12 +0100 | [diff] [blame] | 1079 | struct lyd_node *first_sibling; |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1080 | |
Michal Vasko | 1849d4e | 2024-07-03 14:03:59 +0200 | [diff] [blame] | 1081 | LY_CHECK_ARG_RET(NULL, node, sibling != node, LY_EINVAL); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1082 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1083 | if (sibling) { |
Michal Vasko | 717a4c3 | 2020-12-07 09:40:10 +0100 | [diff] [blame] | 1084 | LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema)); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1085 | } |
| 1086 | |
aPiecek | cada0d7 | 2024-01-11 15:04:12 +0100 | [diff] [blame] | 1087 | first_sibling = lyd_first_sibling(sibling); |
| 1088 | if (node->parent || node->prev->next || !node->next) { |
aPiecek | dfde77d | 2024-01-15 15:16:01 +0100 | [diff] [blame] | 1089 | LY_CHECK_RET(lyd_unlink_tree(node)); |
aPiecek | 1462ab1 | 2024-02-07 09:13:29 +0100 | [diff] [blame] | 1090 | lyd_insert_node(NULL, &first_sibling, node, LYD_INSERT_NODE_DEFAULT); |
aPiecek | cada0d7 | 2024-01-11 15:04:12 +0100 | [diff] [blame] | 1091 | } else { |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 1092 | LY_CHECK_RET(lyd_move_nodes(NULL, &first_sibling, node)); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1093 | } |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1094 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1095 | if (first) { |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 1096 | *first = first_sibling; |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | return LY_SUCCESS; |
| 1100 | } |
| 1101 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1102 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1103 | lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node) |
| 1104 | { |
Michal Vasko | bce230b | 2022-04-19 09:55:31 +0200 | [diff] [blame] | 1105 | LY_CHECK_ARG_RET(NULL, sibling, node, sibling != node, LY_EINVAL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 1106 | LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1107 | |
Michal Vasko | 717a4c3 | 2020-12-07 09:40:10 +0100 | [diff] [blame] | 1108 | LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema)); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1109 | |
Michal Vasko | ab7a2bf | 2022-04-01 14:37:54 +0200 | [diff] [blame] | 1110 | if (node->schema && (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER))) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1111 | LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes."); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1112 | return LY_EINVAL; |
| 1113 | } |
Michal Vasko | 5c0b27a | 2022-04-19 09:56:02 +0200 | [diff] [blame] | 1114 | if (node->schema && sibling->schema && (node->schema != sibling->schema)) { |
| 1115 | LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert before a different schema node instance."); |
Michal Vasko | 7508ef2 | 2022-02-22 14:13:10 +0100 | [diff] [blame] | 1116 | return LY_EINVAL; |
| 1117 | } |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1118 | |
Michal Vasko | 2e784f8 | 2024-01-11 09:51:22 +0100 | [diff] [blame] | 1119 | lyd_unlink(node); |
Michal Vasko | 4bc2ce3 | 2020-12-08 10:06:16 +0100 | [diff] [blame] | 1120 | lyd_insert_before_node(sibling, node); |
| 1121 | lyd_insert_hash(node); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1122 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1123 | return LY_SUCCESS; |
| 1124 | } |
| 1125 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1126 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1127 | lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node) |
| 1128 | { |
Michal Vasko | bce230b | 2022-04-19 09:55:31 +0200 | [diff] [blame] | 1129 | LY_CHECK_ARG_RET(NULL, sibling, node, sibling != node, LY_EINVAL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 1130 | LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1131 | |
Michal Vasko | 717a4c3 | 2020-12-07 09:40:10 +0100 | [diff] [blame] | 1132 | LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema)); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1133 | |
Michal Vasko | ab7a2bf | 2022-04-01 14:37:54 +0200 | [diff] [blame] | 1134 | if (node->schema && (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER))) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1135 | LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes."); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1136 | return LY_EINVAL; |
| 1137 | } |
Michal Vasko | 5c0b27a | 2022-04-19 09:56:02 +0200 | [diff] [blame] | 1138 | if (node->schema && sibling->schema && (node->schema != sibling->schema)) { |
| 1139 | LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert after a different schema node instance."); |
Michal Vasko | 7508ef2 | 2022-02-22 14:13:10 +0100 | [diff] [blame] | 1140 | return LY_EINVAL; |
| 1141 | } |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1142 | |
Michal Vasko | 2e784f8 | 2024-01-11 09:51:22 +0100 | [diff] [blame] | 1143 | lyd_unlink(node); |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 1144 | lyd_insert_after_node(NULL, sibling, node); |
Michal Vasko | 4bc2ce3 | 2020-12-08 10:06:16 +0100 | [diff] [blame] | 1145 | lyd_insert_hash(node); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1146 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1147 | return LY_SUCCESS; |
| 1148 | } |
| 1149 | |
Michal Vasko | 2e784f8 | 2024-01-11 09:51:22 +0100 | [diff] [blame] | 1150 | void |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 1151 | lyd_unlink_ignore_lyds(struct lyd_node **first_sibling_p, struct lyd_node *node) |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1152 | { |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 1153 | struct lyd_node *first_sibling; |
aPiecek | 6cf1d16 | 2023-11-08 16:07:00 +0100 | [diff] [blame] | 1154 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1155 | /* update hashes while still linked into the tree */ |
| 1156 | lyd_unlink_hash(node); |
| 1157 | |
steweg | f9041a2 | 2024-01-18 13:29:12 +0100 | [diff] [blame] | 1158 | /* unlink leafref nodes */ |
| 1159 | if (node->schema && (node->schema->nodetype & LYD_NODE_TERM)) { |
| 1160 | lyd_free_leafref_nodes((struct lyd_node_term *)node); |
| 1161 | } |
| 1162 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1163 | /* unlink from siblings */ |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1164 | if (node->next) { |
| 1165 | node->next->prev = node->prev; |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 1166 | if (node->prev->next) { |
| 1167 | node->prev->next = node->next; |
| 1168 | } else if (first_sibling_p) { |
| 1169 | /* unlinking the first node */ |
| 1170 | *first_sibling_p = node->next; |
| 1171 | } |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1172 | } else { |
| 1173 | /* unlinking the last node */ |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 1174 | /* update the "last" pointer from the first node */ |
| 1175 | if (first_sibling_p && *first_sibling_p) { |
| 1176 | (*first_sibling_p)->prev = node->prev; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1177 | } else { |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 1178 | first_sibling = lyd_first_sibling(node); |
| 1179 | first_sibling->prev = node->prev; |
| 1180 | if (first_sibling_p) { |
| 1181 | *first_sibling_p = first_sibling; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1182 | } |
| 1183 | } |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 1184 | node->prev->next = NULL; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | /* unlink from parent */ |
| 1188 | if (node->parent) { |
| 1189 | if (node->parent->child == node) { |
| 1190 | /* the node is the first child */ |
| 1191 | node->parent->child = node->next; |
| 1192 | } |
| 1193 | |
Michal Vasko | ab49dbe | 2020-07-17 12:32:47 +0200 | [diff] [blame] | 1194 | /* check for NP container whether its last non-default node is not being unlinked */ |
Michal Vasko | 4754d4a | 2022-12-01 10:11:21 +0100 | [diff] [blame] | 1195 | lyd_cont_set_dflt(lyd_parent(node)); |
Michal Vasko | ab49dbe | 2020-07-17 12:32:47 +0200 | [diff] [blame] | 1196 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1197 | node->parent = NULL; |
| 1198 | } |
| 1199 | |
| 1200 | node->next = NULL; |
| 1201 | node->prev = node; |
| 1202 | } |
| 1203 | |
aPiecek | cada0d7 | 2024-01-11 15:04:12 +0100 | [diff] [blame] | 1204 | void |
| 1205 | lyd_unlink(struct lyd_node *node) |
| 1206 | { |
| 1207 | struct lyd_node *leader; |
| 1208 | |
| 1209 | if (!node) { |
| 1210 | return; |
| 1211 | } |
| 1212 | |
| 1213 | /* unlink from the lyds tree */ |
| 1214 | if (lyds_is_supported(node)) { |
| 1215 | if (!node->prev->next || (node->prev->schema != node->schema)) { |
| 1216 | leader = node; |
| 1217 | } else { |
| 1218 | lyd_find_sibling_val(node, node->schema, NULL, 0, &leader); |
| 1219 | assert(leader); |
| 1220 | } |
| 1221 | lyds_unlink(&leader, node); |
| 1222 | } |
| 1223 | |
| 1224 | /* unlink data tree */ |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 1225 | lyd_unlink_ignore_lyds(NULL, node); |
aPiecek | cada0d7 | 2024-01-11 15:04:12 +0100 | [diff] [blame] | 1226 | } |
| 1227 | |
aPiecek | dfde77d | 2024-01-15 15:16:01 +0100 | [diff] [blame] | 1228 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 2e784f8 | 2024-01-11 09:51:22 +0100 | [diff] [blame] | 1229 | lyd_unlink_siblings(struct lyd_node *node) |
| 1230 | { |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 1231 | struct lyd_node *next, *iter, *leader, *start, *first_sibling = NULL; |
Michal Vasko | 2e784f8 | 2024-01-11 09:51:22 +0100 | [diff] [blame] | 1232 | |
aPiecek | cada0d7 | 2024-01-11 15:04:12 +0100 | [diff] [blame] | 1233 | if (lyds_is_supported(node) && node->prev->next && (node->prev->schema == node->schema)) { |
| 1234 | /* unlink starts at the non-first item in the (leaf-)list */ |
| 1235 | lyd_find_sibling_val(node, node->schema, NULL, 0, &leader); |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 1236 | lyds_split(&first_sibling, leader, node, &start); |
aPiecek | cada0d7 | 2024-01-11 15:04:12 +0100 | [diff] [blame] | 1237 | } else { |
| 1238 | /* unlink @p node */ |
aPiecek | dfde77d | 2024-01-15 15:16:01 +0100 | [diff] [blame] | 1239 | LY_CHECK_RET(lyd_unlink_check(node)); |
aPiecek | cada0d7 | 2024-01-11 15:04:12 +0100 | [diff] [blame] | 1240 | start = node->next; |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 1241 | lyd_unlink_ignore_lyds(&first_sibling, node); |
aPiecek | cada0d7 | 2024-01-11 15:04:12 +0100 | [diff] [blame] | 1242 | } |
Michal Vasko | 2e784f8 | 2024-01-11 09:51:22 +0100 | [diff] [blame] | 1243 | |
aPiecek | cada0d7 | 2024-01-11 15:04:12 +0100 | [diff] [blame] | 1244 | /* continue unlinking the rest */ |
| 1245 | LY_LIST_FOR_SAFE(start, next, iter) { |
aPiecek | dfde77d | 2024-01-15 15:16:01 +0100 | [diff] [blame] | 1246 | LY_CHECK_RET(lyd_unlink_check(iter)); |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 1247 | lyd_unlink_ignore_lyds(&first_sibling, iter); |
| 1248 | lyd_insert_after_node(&node, node->prev, iter); |
aPiecek | cada0d7 | 2024-01-11 15:04:12 +0100 | [diff] [blame] | 1249 | lyd_insert_hash(iter); |
Michal Vasko | 2e784f8 | 2024-01-11 09:51:22 +0100 | [diff] [blame] | 1250 | } |
aPiecek | dfde77d | 2024-01-15 15:16:01 +0100 | [diff] [blame] | 1251 | |
| 1252 | return LY_SUCCESS; |
Michal Vasko | 2e784f8 | 2024-01-11 09:51:22 +0100 | [diff] [blame] | 1253 | } |
| 1254 | |
aPiecek | dfde77d | 2024-01-15 15:16:01 +0100 | [diff] [blame] | 1255 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 2e784f8 | 2024-01-11 09:51:22 +0100 | [diff] [blame] | 1256 | lyd_unlink_tree(struct lyd_node *node) |
| 1257 | { |
aPiecek | dfde77d | 2024-01-15 15:16:01 +0100 | [diff] [blame] | 1258 | LY_CHECK_RET(lyd_unlink_check(node)); |
Michal Vasko | 2e784f8 | 2024-01-11 09:51:22 +0100 | [diff] [blame] | 1259 | lyd_unlink(node); |
aPiecek | dfde77d | 2024-01-15 15:16:01 +0100 | [diff] [blame] | 1260 | |
| 1261 | return LY_SUCCESS; |
Michal Vasko | 2e784f8 | 2024-01-11 09:51:22 +0100 | [diff] [blame] | 1262 | } |
| 1263 | |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 1264 | void |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 1265 | lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1266 | { |
| 1267 | struct lyd_meta *last, *iter; |
| 1268 | |
| 1269 | assert(parent); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 1270 | |
| 1271 | if (!meta) { |
| 1272 | return; |
| 1273 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1274 | |
| 1275 | for (iter = meta; iter; iter = iter->next) { |
| 1276 | iter->parent = parent; |
| 1277 | } |
| 1278 | |
| 1279 | /* insert as the last attribute */ |
| 1280 | if (parent->meta) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 1281 | for (last = parent->meta; last->next; last = last->next) {} |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1282 | last->next = meta; |
| 1283 | } else { |
| 1284 | parent->meta = meta; |
| 1285 | } |
| 1286 | |
| 1287 | /* remove default flags from NP containers */ |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 1288 | while (clear_dflt && parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1289 | parent->flags &= ~LYD_DEFAULT; |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 1290 | parent = lyd_parent(parent); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1291 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1292 | } |
| 1293 | |
aPiecek | 0e92afc | 2023-11-08 10:48:02 +0100 | [diff] [blame] | 1294 | void |
| 1295 | lyd_unlink_meta_single(struct lyd_meta *meta) |
| 1296 | { |
| 1297 | struct lyd_meta *iter; |
| 1298 | |
| 1299 | if (!meta) { |
| 1300 | return; |
| 1301 | } |
| 1302 | |
| 1303 | if (meta->parent && (meta->parent->meta == meta)) { |
| 1304 | meta->parent->meta = meta->next; |
| 1305 | } else if (meta->parent) { |
| 1306 | for (iter = meta->parent->meta; iter->next && (iter->next != meta); iter = iter->next) {} |
| 1307 | if (iter->next) { |
| 1308 | iter->next = meta->next; |
| 1309 | } |
| 1310 | } |
| 1311 | |
| 1312 | meta->next = NULL; |
| 1313 | meta->parent = NULL; |
| 1314 | } |
| 1315 | |
aPiecek | 0b1df64 | 2023-11-06 16:15:33 +0100 | [diff] [blame] | 1316 | /** |
| 1317 | * @brief Get the annotation definition in the module. |
| 1318 | * |
| 1319 | * @param[in] mod Metadata module (with the annotation definition). |
| 1320 | * @param[in] name Attribute name. |
| 1321 | * @param[in] name_len Length of @p name, must be set correctly. |
| 1322 | * @return compiled YANG extension instance on success. |
| 1323 | */ |
| 1324 | static struct lysc_ext_instance * |
| 1325 | lyd_get_meta_annotation(const struct lys_module *mod, const char *name, size_t name_len) |
| 1326 | { |
| 1327 | LY_ARRAY_COUNT_TYPE u; |
| 1328 | struct lyplg_ext *plugin; |
| 1329 | |
| 1330 | if (!mod) { |
| 1331 | return NULL; |
| 1332 | } |
| 1333 | |
| 1334 | LY_ARRAY_FOR(mod->compiled->exts, u) { |
| 1335 | plugin = mod->compiled->exts[u].def->plugin; |
| 1336 | if (plugin && !strncmp(plugin->id, "ly2 metadata", 12) && |
| 1337 | !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) { |
| 1338 | return &mod->compiled->exts[u]; |
| 1339 | } |
| 1340 | } |
| 1341 | |
| 1342 | return NULL; |
| 1343 | } |
| 1344 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1345 | LY_ERR |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1346 | lyd_create_meta(struct lyd_node *parent, struct lyd_meta **meta, const struct lys_module *mod, const char *name, |
steweg | d4cde64 | 2024-02-21 08:34:16 +0100 | [diff] [blame] | 1347 | size_t name_len, const char *value, size_t value_len, ly_bool is_utf8, ly_bool store_only, ly_bool *dynamic, |
| 1348 | LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, const struct lysc_node *ctx_node, ly_bool clear_dflt, |
| 1349 | ly_bool *incomplete) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1350 | { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1351 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1352 | struct lysc_ext_instance *ant = NULL; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1353 | const struct lysc_type *ant_type; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1354 | struct lyd_meta *mt, *last; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1355 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1356 | assert((parent || meta) && mod); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 1357 | |
aPiecek | 0b1df64 | 2023-11-06 16:15:33 +0100 | [diff] [blame] | 1358 | ant = lyd_get_meta_annotation(mod, name, name_len); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1359 | if (!ant) { |
| 1360 | /* attribute is not defined as a metadata annotation (RFC 7952) */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1361 | LOGVAL(mod->ctx, LYVE_REFERENCE, "Annotation definition for attribute \"%s:%.*s\" not found.", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 1362 | mod->name, (int)name_len, name); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1363 | ret = LY_EINVAL; |
| 1364 | goto cleanup; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1365 | } |
| 1366 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1367 | mt = calloc(1, sizeof *mt); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1368 | LY_CHECK_ERR_GOTO(!mt, LOGMEM(mod->ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1369 | mt->parent = parent; |
| 1370 | mt->annotation = ant; |
Michal Vasko | fbd037c | 2022-11-08 10:34:20 +0100 | [diff] [blame] | 1371 | lyplg_ext_get_storage(ant, LY_STMT_TYPE, sizeof ant_type, (const void **)&ant_type); |
steweg | d4cde64 | 2024-02-21 08:34:16 +0100 | [diff] [blame] | 1372 | ret = lyd_value_store(mod->ctx, &mt->value, ant_type, value, value_len, is_utf8, store_only, dynamic, format, prefix_data, hints, |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1373 | ctx_node, incomplete); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1374 | LY_CHECK_ERR_GOTO(ret, free(mt), cleanup); |
| 1375 | ret = lydict_insert(mod->ctx, name, name_len, &mt->name); |
| 1376 | LY_CHECK_ERR_GOTO(ret, free(mt), cleanup); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1377 | |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 1378 | /* insert as the last attribute */ |
| 1379 | if (parent) { |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 1380 | lyd_insert_meta(parent, mt, clear_dflt); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1381 | } else if (*meta) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 1382 | for (last = *meta; last->next; last = last->next) {} |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1383 | last->next = mt; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1384 | } |
| 1385 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1386 | if (meta) { |
| 1387 | *meta = mt; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1388 | } |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1389 | |
| 1390 | cleanup: |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1391 | return ret; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1392 | } |
| 1393 | |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 1394 | void |
| 1395 | lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr) |
| 1396 | { |
| 1397 | struct lyd_attr *last, *iter; |
| 1398 | struct lyd_node_opaq *opaq; |
| 1399 | |
| 1400 | assert(parent && !parent->schema); |
| 1401 | |
| 1402 | if (!attr) { |
| 1403 | return; |
| 1404 | } |
| 1405 | |
| 1406 | opaq = (struct lyd_node_opaq *)parent; |
| 1407 | for (iter = attr; iter; iter = iter->next) { |
| 1408 | iter->parent = opaq; |
| 1409 | } |
| 1410 | |
| 1411 | /* insert as the last attribute */ |
| 1412 | if (opaq->attr) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 1413 | for (last = opaq->attr; last->next; last = last->next) {} |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 1414 | last->next = attr; |
| 1415 | } else { |
| 1416 | opaq->attr = attr; |
| 1417 | } |
| 1418 | } |
| 1419 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1420 | LY_ERR |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1421 | lyd_create_attr(struct lyd_node *parent, struct lyd_attr **attr, const struct ly_ctx *ctx, const char *name, size_t name_len, |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 1422 | const char *prefix, size_t prefix_len, const char *module_key, size_t module_key_len, const char *value, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1423 | size_t value_len, ly_bool *dynamic, LY_VALUE_FORMAT format, void *val_prefix_data, uint32_t hints) |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1424 | { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1425 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1426 | struct lyd_attr *at, *last; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1427 | |
| 1428 | assert(ctx && (parent || attr) && (!parent || !parent->schema)); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1429 | assert(name && name_len && format); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1430 | |
Michal Vasko | 2a3722d | 2021-06-16 11:52:39 +0200 | [diff] [blame] | 1431 | if (!value_len && (!dynamic || !*dynamic)) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1432 | value = ""; |
| 1433 | } |
| 1434 | |
| 1435 | at = calloc(1, sizeof *at); |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 1436 | LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_prefix_data(format, val_prefix_data), LY_EMEM); |
Radek Krejci | d46e46a | 2020-09-15 14:22:42 +0200 | [diff] [blame] | 1437 | |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 1438 | LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name.name), finish); |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 1439 | if (prefix_len) { |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 1440 | LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->name.prefix), finish); |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 1441 | } |
| 1442 | if (module_key_len) { |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 1443 | LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->name.module_ns), finish); |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 1444 | } |
| 1445 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1446 | if (dynamic && *dynamic) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1447 | ret = lydict_insert_zc(ctx, (char *)value, &at->value); |
| 1448 | LY_CHECK_GOTO(ret, finish); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1449 | *dynamic = 0; |
| 1450 | } else { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1451 | LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1452 | } |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 1453 | at->format = format; |
| 1454 | at->val_prefix_data = val_prefix_data; |
| 1455 | at->hints = hints; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1456 | |
| 1457 | /* insert as the last attribute */ |
| 1458 | if (parent) { |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 1459 | lyd_insert_attr(parent, at); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1460 | } else if (*attr) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 1461 | for (last = *attr; last->next; last = last->next) {} |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1462 | last->next = at; |
| 1463 | } |
| 1464 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1465 | finish: |
| 1466 | if (ret) { |
| 1467 | lyd_free_attr_single(ctx, at); |
| 1468 | } else if (attr) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1469 | *attr = at; |
| 1470 | } |
| 1471 | return LY_SUCCESS; |
| 1472 | } |
| 1473 | |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 1474 | /** |
| 1475 | * @brief Check the equality of the two schemas from different contexts. |
| 1476 | * |
| 1477 | * @param schema1 of first node. |
| 1478 | * @param schema2 of second node. |
| 1479 | * @return 1 if the schemas are equal otherwise 0. |
| 1480 | */ |
| 1481 | static ly_bool |
| 1482 | lyd_compare_schema_equal(const struct lysc_node *schema1, const struct lysc_node *schema2) |
| 1483 | { |
| 1484 | if (!schema1 && !schema2) { |
| 1485 | return 1; |
| 1486 | } else if (!schema1 || !schema2) { |
| 1487 | return 0; |
| 1488 | } |
| 1489 | |
| 1490 | assert(schema1->module->ctx != schema2->module->ctx); |
| 1491 | |
| 1492 | if (schema1->nodetype != schema2->nodetype) { |
| 1493 | return 0; |
| 1494 | } |
| 1495 | |
| 1496 | if (strcmp(schema1->name, schema2->name)) { |
| 1497 | return 0; |
| 1498 | } |
| 1499 | |
| 1500 | if (strcmp(schema1->module->name, schema2->module->name)) { |
| 1501 | return 0; |
| 1502 | } |
| 1503 | |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 1504 | return 1; |
| 1505 | } |
| 1506 | |
| 1507 | /** |
| 1508 | * @brief Check the equality of the schemas for all parent nodes. |
| 1509 | * |
| 1510 | * Both nodes must be from different contexts. |
| 1511 | * |
| 1512 | * @param node1 Data of first node. |
| 1513 | * @param node2 Data of second node. |
| 1514 | * @return 1 if the all related parental schemas are equal otherwise 0. |
| 1515 | */ |
| 1516 | static ly_bool |
| 1517 | lyd_compare_schema_parents_equal(const struct lyd_node *node1, const struct lyd_node *node2) |
| 1518 | { |
| 1519 | const struct lysc_node *parent1, *parent2; |
| 1520 | |
| 1521 | assert(node1 && node2); |
| 1522 | |
| 1523 | for (parent1 = node1->schema->parent, parent2 = node2->schema->parent; |
| 1524 | parent1 && parent2; |
| 1525 | parent1 = parent1->parent, parent2 = parent2->parent) { |
| 1526 | if (!lyd_compare_schema_equal(parent1, parent2)) { |
| 1527 | return 0; |
| 1528 | } |
| 1529 | } |
| 1530 | |
| 1531 | if (parent1 || parent2) { |
| 1532 | return 0; |
| 1533 | } |
| 1534 | |
| 1535 | return 1; |
| 1536 | } |
| 1537 | |
| 1538 | /** |
Michal Vasko | df8ebf6 | 2022-11-10 10:33:28 +0100 | [diff] [blame] | 1539 | * @brief Compare 2 nodes values including opaque node values. |
| 1540 | * |
| 1541 | * @param[in] node1 First node to compare. |
| 1542 | * @param[in] node2 Second node to compare. |
| 1543 | * @return LY_SUCCESS if equal. |
| 1544 | * @return LY_ENOT if not equal. |
| 1545 | * @return LY_ERR on error. |
| 1546 | */ |
| 1547 | static LY_ERR |
| 1548 | lyd_compare_single_value(const struct lyd_node *node1, const struct lyd_node *node2) |
| 1549 | { |
| 1550 | const struct lyd_node_opaq *opaq1 = NULL, *opaq2 = NULL; |
| 1551 | const char *val1, *val2, *col; |
| 1552 | const struct lys_module *mod; |
| 1553 | char *val_dyn = NULL; |
| 1554 | LY_ERR rc = LY_SUCCESS; |
| 1555 | |
| 1556 | if (!node1->schema) { |
| 1557 | opaq1 = (struct lyd_node_opaq *)node1; |
| 1558 | } |
| 1559 | if (!node2->schema) { |
| 1560 | opaq2 = (struct lyd_node_opaq *)node2; |
| 1561 | } |
| 1562 | |
| 1563 | if (opaq1 && opaq2 && (opaq1->format == LY_VALUE_XML) && (opaq2->format == LY_VALUE_XML)) { |
| 1564 | /* opaque XML and opaque XML node */ |
| 1565 | if (lyxml_value_compare(LYD_CTX(node1), opaq1->value, opaq1->val_prefix_data, LYD_CTX(node2), opaq2->value, |
| 1566 | opaq2->val_prefix_data)) { |
| 1567 | return LY_ENOT; |
| 1568 | } |
| 1569 | return LY_SUCCESS; |
| 1570 | } |
| 1571 | |
| 1572 | /* get their values */ |
| 1573 | if (opaq1 && ((opaq1->format == LY_VALUE_XML) || (opaq1->format == LY_VALUE_STR_NS)) && (col = strchr(opaq1->value, ':'))) { |
| 1574 | /* XML value with a prefix, try to transform it into a JSON (canonical) value */ |
| 1575 | mod = ly_resolve_prefix(LYD_CTX(node1), opaq1->value, col - opaq1->value, opaq1->format, opaq1->val_prefix_data); |
| 1576 | if (!mod) { |
| 1577 | /* unable to compare */ |
| 1578 | return LY_ENOT; |
| 1579 | } |
| 1580 | |
| 1581 | if (asprintf(&val_dyn, "%s%s", mod->name, col) == -1) { |
| 1582 | LOGMEM(LYD_CTX(node1)); |
| 1583 | return LY_EMEM; |
| 1584 | } |
| 1585 | val1 = val_dyn; |
| 1586 | } else { |
| 1587 | val1 = lyd_get_value(node1); |
| 1588 | } |
| 1589 | if (opaq2 && ((opaq2->format == LY_VALUE_XML) || (opaq2->format == LY_VALUE_STR_NS)) && (col = strchr(opaq2->value, ':'))) { |
| 1590 | mod = ly_resolve_prefix(LYD_CTX(node2), opaq2->value, col - opaq2->value, opaq2->format, opaq2->val_prefix_data); |
| 1591 | if (!mod) { |
| 1592 | return LY_ENOT; |
| 1593 | } |
| 1594 | |
| 1595 | assert(!val_dyn); |
| 1596 | if (asprintf(&val_dyn, "%s%s", mod->name, col) == -1) { |
| 1597 | LOGMEM(LYD_CTX(node2)); |
| 1598 | return LY_EMEM; |
| 1599 | } |
| 1600 | val2 = val_dyn; |
| 1601 | } else { |
| 1602 | val2 = lyd_get_value(node2); |
| 1603 | } |
| 1604 | |
| 1605 | /* compare values */ |
| 1606 | if (strcmp(val1, val2)) { |
| 1607 | rc = LY_ENOT; |
| 1608 | } |
| 1609 | |
| 1610 | free(val_dyn); |
| 1611 | return rc; |
| 1612 | } |
| 1613 | |
| 1614 | /** |
Michal Vasko | f277d36 | 2023-04-24 09:08:31 +0200 | [diff] [blame] | 1615 | * @brief Compare 2 data nodes if they are equivalent regarding the schema tree. |
| 1616 | * |
| 1617 | * Works correctly even if @p node1 and @p node2 have different contexts. |
| 1618 | * |
| 1619 | * @param[in] node1 The first node to compare. |
| 1620 | * @param[in] node2 The second node to compare. |
| 1621 | * @param[in] options Various @ref datacompareoptions. |
| 1622 | * @param[in] parental_schemas_checked Flag set if parent schemas were checked for match. |
| 1623 | * @return LY_SUCCESS if the nodes are equivalent. |
| 1624 | * @return LY_ENOT if the nodes are not equivalent. |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 1625 | */ |
| 1626 | static LY_ERR |
Michal Vasko | f277d36 | 2023-04-24 09:08:31 +0200 | [diff] [blame] | 1627 | lyd_compare_single_schema(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options, |
Michal Vasko | df8ebf6 | 2022-11-10 10:33:28 +0100 | [diff] [blame] | 1628 | ly_bool parental_schemas_checked) |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 1629 | { |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 1630 | if (LYD_CTX(node1) == LYD_CTX(node2)) { |
| 1631 | /* same contexts */ |
Michal Vasko | df8ebf6 | 2022-11-10 10:33:28 +0100 | [diff] [blame] | 1632 | if (options & LYD_COMPARE_OPAQ) { |
| 1633 | if (lyd_node_schema(node1) != lyd_node_schema(node2)) { |
| 1634 | return LY_ENOT; |
| 1635 | } |
| 1636 | } else { |
| 1637 | if (node1->schema != node2->schema) { |
| 1638 | return LY_ENOT; |
| 1639 | } |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 1640 | } |
| 1641 | } else { |
| 1642 | /* different contexts */ |
| 1643 | if (!lyd_compare_schema_equal(node1->schema, node2->schema)) { |
| 1644 | return LY_ENOT; |
| 1645 | } |
| 1646 | if (!parental_schemas_checked) { |
| 1647 | if (!lyd_compare_schema_parents_equal(node1, node2)) { |
| 1648 | return LY_ENOT; |
| 1649 | } |
| 1650 | parental_schemas_checked = 1; |
| 1651 | } |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 1652 | } |
| 1653 | |
Michal Vasko | f277d36 | 2023-04-24 09:08:31 +0200 | [diff] [blame] | 1654 | return LY_SUCCESS; |
| 1655 | } |
| 1656 | |
| 1657 | /** |
| 1658 | * @brief Compare 2 data nodes if they are equivalent regarding the data they contain. |
| 1659 | * |
| 1660 | * Works correctly even if @p node1 and @p node2 have different contexts. |
| 1661 | * |
| 1662 | * @param[in] node1 The first node to compare. |
| 1663 | * @param[in] node2 The second node to compare. |
| 1664 | * @param[in] options Various @ref datacompareoptions. |
| 1665 | * @return LY_SUCCESS if the nodes are equivalent. |
| 1666 | * @return LY_ENOT if the nodes are not equivalent. |
| 1667 | */ |
| 1668 | static LY_ERR |
| 1669 | lyd_compare_single_data(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options) |
| 1670 | { |
| 1671 | const struct lyd_node *iter1, *iter2; |
| 1672 | struct lyd_node_any *any1, *any2; |
| 1673 | int len1, len2; |
| 1674 | LY_ERR r; |
| 1675 | |
Michal Vasko | df8ebf6 | 2022-11-10 10:33:28 +0100 | [diff] [blame] | 1676 | if (!(options & LYD_COMPARE_OPAQ) && (node1->hash != node2->hash)) { |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 1677 | return LY_ENOT; |
| 1678 | } |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 1679 | /* equal hashes do not mean equal nodes, they can be just in collision so the nodes must be checked explicitly */ |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 1680 | |
Michal Vasko | df8ebf6 | 2022-11-10 10:33:28 +0100 | [diff] [blame] | 1681 | if (!node1->schema || !node2->schema) { |
| 1682 | if (!(options & LYD_COMPARE_OPAQ) && ((node1->schema && !node2->schema) || (!node1->schema && node2->schema))) { |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 1683 | return LY_ENOT; |
| 1684 | } |
Michal Vasko | 7b0500d | 2023-03-20 15:22:46 +0100 | [diff] [blame] | 1685 | if ((!node1->schema && !node2->schema) || (node1->schema && (node1->schema->nodetype & LYD_NODE_TERM)) || |
| 1686 | (node2->schema && (node2->schema->nodetype & LYD_NODE_TERM))) { |
| 1687 | /* compare values only if there are any to compare */ |
| 1688 | if ((r = lyd_compare_single_value(node1, node2))) { |
| 1689 | return r; |
| 1690 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1691 | } |
Michal Vasko | df8ebf6 | 2022-11-10 10:33:28 +0100 | [diff] [blame] | 1692 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1693 | if (options & LYD_COMPARE_FULL_RECURSION) { |
Michal Vasko | f277d36 | 2023-04-24 09:08:31 +0200 | [diff] [blame] | 1694 | return lyd_compare_siblings_(lyd_child(node1), lyd_child(node2), options, 1); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1695 | } |
| 1696 | return LY_SUCCESS; |
| 1697 | } else { |
| 1698 | switch (node1->schema->nodetype) { |
| 1699 | case LYS_LEAF: |
| 1700 | case LYS_LEAFLIST: |
| 1701 | if (options & LYD_COMPARE_DEFAULTS) { |
| 1702 | if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) { |
| 1703 | return LY_ENOT; |
| 1704 | } |
| 1705 | } |
Michal Vasko | df8ebf6 | 2022-11-10 10:33:28 +0100 | [diff] [blame] | 1706 | if ((r = lyd_compare_single_value(node1, node2))) { |
| 1707 | return r; |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 1708 | } |
| 1709 | |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 1710 | return LY_SUCCESS; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1711 | case LYS_CONTAINER: |
Michal Vasko | c8187dc | 2022-05-13 12:26:40 +0200 | [diff] [blame] | 1712 | case LYS_RPC: |
| 1713 | case LYS_ACTION: |
| 1714 | case LYS_NOTIF: |
Michal Vasko | a38adc7 | 2023-04-25 10:14:28 +0200 | [diff] [blame] | 1715 | /* implicit container is always equal to a container with non-default descendants */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1716 | if (options & LYD_COMPARE_FULL_RECURSION) { |
Michal Vasko | f277d36 | 2023-04-24 09:08:31 +0200 | [diff] [blame] | 1717 | return lyd_compare_siblings_(lyd_child(node1), lyd_child(node2), options, 1); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 1718 | } |
| 1719 | return LY_SUCCESS; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1720 | case LYS_LIST: |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 1721 | iter1 = lyd_child(node1); |
| 1722 | iter2 = lyd_child(node2); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1723 | |
Michal Vasko | ee9b948 | 2023-06-19 13:17:48 +0200 | [diff] [blame] | 1724 | if (options & LYD_COMPARE_FULL_RECURSION) { |
Michal Vasko | f277d36 | 2023-04-24 09:08:31 +0200 | [diff] [blame] | 1725 | return lyd_compare_siblings_(iter1, iter2, options, 1); |
Michal Vasko | ee9b948 | 2023-06-19 13:17:48 +0200 | [diff] [blame] | 1726 | } else if (node1->schema->flags & LYS_KEYLESS) { |
| 1727 | /* always equal */ |
| 1728 | return LY_SUCCESS; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1729 | } |
Michal Vasko | ee9b948 | 2023-06-19 13:17:48 +0200 | [diff] [blame] | 1730 | |
| 1731 | /* lists with keys, their equivalence is based on their keys */ |
| 1732 | for (const struct lysc_node *key = lysc_node_child(node1->schema); |
| 1733 | key && (key->flags & LYS_KEY); |
| 1734 | key = key->next) { |
| 1735 | if (!iter1 || !iter2) { |
| 1736 | return (iter1 == iter2) ? LY_SUCCESS : LY_ENOT; |
| 1737 | } |
| 1738 | r = lyd_compare_single_schema(iter1, iter2, options, 1); |
| 1739 | LY_CHECK_RET(r); |
| 1740 | r = lyd_compare_single_data(iter1, iter2, options); |
| 1741 | LY_CHECK_RET(r); |
| 1742 | |
| 1743 | iter1 = iter1->next; |
| 1744 | iter2 = iter2->next; |
| 1745 | } |
| 1746 | |
| 1747 | return LY_SUCCESS; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1748 | case LYS_ANYXML: |
| 1749 | case LYS_ANYDATA: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1750 | any1 = (struct lyd_node_any *)node1; |
| 1751 | any2 = (struct lyd_node_any *)node2; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1752 | |
| 1753 | if (any1->value_type != any2->value_type) { |
| 1754 | return LY_ENOT; |
| 1755 | } |
| 1756 | switch (any1->value_type) { |
| 1757 | case LYD_ANYDATA_DATATREE: |
Michal Vasko | f277d36 | 2023-04-24 09:08:31 +0200 | [diff] [blame] | 1758 | return lyd_compare_siblings_(any1->value.tree, any2->value.tree, options, 1); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1759 | case LYD_ANYDATA_STRING: |
| 1760 | case LYD_ANYDATA_XML: |
| 1761 | case LYD_ANYDATA_JSON: |
Michal Vasko | 3b4ec41 | 2022-09-22 15:24:14 +0200 | [diff] [blame] | 1762 | if ((!any1->value.str && any2->value.str) || (any1->value.str && !any2->value.str)) { |
| 1763 | return LY_ENOT; |
| 1764 | } else if (!any1->value.str && !any2->value.str) { |
| 1765 | return LY_SUCCESS; |
| 1766 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1767 | len1 = strlen(any1->value.str); |
| 1768 | len2 = strlen(any2->value.str); |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1769 | if ((len1 != len2) || strcmp(any1->value.str, any2->value.str)) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1770 | return LY_ENOT; |
| 1771 | } |
| 1772 | return LY_SUCCESS; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1773 | case LYD_ANYDATA_LYB: |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1774 | len1 = lyd_lyb_data_length(any1->value.mem); |
| 1775 | len2 = lyd_lyb_data_length(any2->value.mem); |
Michal Vasko | 2b97d47 | 2022-08-17 09:29:03 +0200 | [diff] [blame] | 1776 | if ((len1 == -1) || (len2 == -1) || (len1 != len2) || memcmp(any1->value.mem, any2->value.mem, len1)) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1777 | return LY_ENOT; |
| 1778 | } |
| 1779 | return LY_SUCCESS; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1780 | } |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 1781 | } |
| 1782 | } |
| 1783 | |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1784 | LOGINT(LYD_CTX(node1)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 1785 | return LY_EINT; |
| 1786 | } |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1787 | |
Michal Vasko | f277d36 | 2023-04-24 09:08:31 +0200 | [diff] [blame] | 1788 | /** |
| 1789 | * @brief Compare all siblings at a node level. |
| 1790 | * |
| 1791 | * @param[in] node1 First sibling list. |
| 1792 | * @param[in] node2 Second sibling list. |
| 1793 | * @param[in] options Various @ref datacompareoptions. |
| 1794 | * @param[in] parental_schemas_checked Flag set if parent schemas were checked for match. |
| 1795 | * @return LY_SUCCESS if equal. |
| 1796 | * @return LY_ENOT if not equal. |
| 1797 | * @return LY_ERR on error. |
| 1798 | */ |
| 1799 | static LY_ERR |
| 1800 | lyd_compare_siblings_(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options, |
| 1801 | ly_bool parental_schemas_checked) |
| 1802 | { |
| 1803 | LY_ERR r; |
| 1804 | const struct lyd_node *iter2; |
| 1805 | |
| 1806 | while (node1 && node2) { |
| 1807 | /* schema match */ |
| 1808 | r = lyd_compare_single_schema(node1, node2, options, parental_schemas_checked); |
| 1809 | LY_CHECK_RET(r); |
| 1810 | |
| 1811 | if (node1->schema && (((node1->schema->nodetype == LYS_LIST) && !(node1->schema->flags & LYS_KEYLESS)) || |
| 1812 | ((node1->schema->nodetype == LYS_LEAFLIST) && (node1->schema->flags & LYS_CONFIG_W))) && |
| 1813 | (node1->schema->flags & LYS_ORDBY_SYSTEM)) { |
| 1814 | /* find a matching instance in case they are ordered differently */ |
| 1815 | r = lyd_find_sibling_first(node2, node1, (struct lyd_node **)&iter2); |
| 1816 | if (r == LY_ENOTFOUND) { |
| 1817 | /* no matching instance, data not equal */ |
| 1818 | r = LY_ENOT; |
| 1819 | } |
| 1820 | LY_CHECK_RET(r); |
| 1821 | } else { |
| 1822 | /* compare with the current node */ |
| 1823 | iter2 = node2; |
| 1824 | } |
| 1825 | |
| 1826 | /* data match */ |
| 1827 | r = lyd_compare_single_data(node1, iter2, options | LYD_COMPARE_FULL_RECURSION); |
| 1828 | LY_CHECK_RET(r); |
| 1829 | |
| 1830 | node1 = node1->next; |
| 1831 | node2 = node2->next; |
| 1832 | } |
| 1833 | |
| 1834 | return (node1 || node2) ? LY_ENOT : LY_SUCCESS; |
| 1835 | } |
| 1836 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1837 | LIBYANG_API_DEF LY_ERR |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 1838 | lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options) |
| 1839 | { |
Michal Vasko | f277d36 | 2023-04-24 09:08:31 +0200 | [diff] [blame] | 1840 | LY_ERR r; |
| 1841 | |
| 1842 | if (!node1 || !node2) { |
| 1843 | return (node1 == node2) ? LY_SUCCESS : LY_ENOT; |
| 1844 | } |
| 1845 | |
| 1846 | /* schema match */ |
| 1847 | if ((r = lyd_compare_single_schema(node1, node2, options, 0))) { |
| 1848 | return r; |
| 1849 | } |
| 1850 | |
| 1851 | /* data match */ |
| 1852 | return lyd_compare_single_data(node1, node2, options); |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 1853 | } |
| 1854 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1855 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1856 | lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options) |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 1857 | { |
Michal Vasko | f277d36 | 2023-04-24 09:08:31 +0200 | [diff] [blame] | 1858 | return lyd_compare_siblings_(node1, node2, options, 0); |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 1859 | } |
| 1860 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1861 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 2172574 | 2020-06-29 11:49:25 +0200 | [diff] [blame] | 1862 | lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2) |
| 1863 | { |
aPiecek | 0a6705b | 2023-11-14 14:20:58 +0100 | [diff] [blame] | 1864 | const struct ly_ctx *ctx; |
| 1865 | |
Michal Vasko | 2172574 | 2020-06-29 11:49:25 +0200 | [diff] [blame] | 1866 | if (!meta1 || !meta2) { |
| 1867 | if (meta1 == meta2) { |
| 1868 | return LY_SUCCESS; |
| 1869 | } else { |
| 1870 | return LY_ENOT; |
| 1871 | } |
| 1872 | } |
| 1873 | |
aPiecek | 0a6705b | 2023-11-14 14:20:58 +0100 | [diff] [blame] | 1874 | ctx = meta1->annotation->module->ctx; |
| 1875 | if ((ctx != meta2->annotation->module->ctx) || (meta1->annotation != meta2->annotation)) { |
Michal Vasko | 2172574 | 2020-06-29 11:49:25 +0200 | [diff] [blame] | 1876 | return LY_ENOT; |
| 1877 | } |
| 1878 | |
aPiecek | 0a6705b | 2023-11-14 14:20:58 +0100 | [diff] [blame] | 1879 | return meta1->value.realtype->plugin->compare(ctx, &meta1->value, &meta2->value); |
Michal Vasko | 2172574 | 2020-06-29 11:49:25 +0200 | [diff] [blame] | 1880 | } |
| 1881 | |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1882 | /** |
Michal Vasko | 9cf6242 | 2021-07-01 13:29:32 +0200 | [diff] [blame] | 1883 | * @brief Create a copy of the attribute. |
| 1884 | * |
| 1885 | * @param[in] attr Attribute to copy. |
| 1886 | * @param[in] node Opaque where to append the new attribute. |
| 1887 | * @param[out] dup Optional created attribute copy. |
| 1888 | * @return LY_ERR value. |
| 1889 | */ |
| 1890 | static LY_ERR |
| 1891 | lyd_dup_attr_single(const struct lyd_attr *attr, struct lyd_node *node, struct lyd_attr **dup) |
| 1892 | { |
| 1893 | LY_ERR ret = LY_SUCCESS; |
| 1894 | struct lyd_attr *a, *last; |
| 1895 | struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)node; |
| 1896 | |
| 1897 | LY_CHECK_ARG_RET(NULL, attr, node, !node->schema, LY_EINVAL); |
| 1898 | |
| 1899 | /* create a copy */ |
| 1900 | a = calloc(1, sizeof *attr); |
| 1901 | LY_CHECK_ERR_RET(!a, LOGMEM(LYD_CTX(node)), LY_EMEM); |
| 1902 | |
| 1903 | LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.name, 0, &a->name.name), finish); |
| 1904 | LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.prefix, 0, &a->name.prefix), finish); |
| 1905 | LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.module_ns, 0, &a->name.module_ns), finish); |
| 1906 | LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->value, 0, &a->value), finish); |
| 1907 | a->hints = attr->hints; |
| 1908 | a->format = attr->format; |
| 1909 | if (attr->val_prefix_data) { |
| 1910 | ret = ly_dup_prefix_data(LYD_CTX(node), attr->format, attr->val_prefix_data, &a->val_prefix_data); |
| 1911 | LY_CHECK_GOTO(ret, finish); |
| 1912 | } |
| 1913 | |
| 1914 | /* insert as the last attribute */ |
| 1915 | a->parent = opaq; |
| 1916 | if (opaq->attr) { |
| 1917 | for (last = opaq->attr; last->next; last = last->next) {} |
| 1918 | last->next = a; |
| 1919 | } else { |
| 1920 | opaq->attr = a; |
| 1921 | } |
| 1922 | |
| 1923 | finish: |
| 1924 | if (ret) { |
| 1925 | lyd_free_attr_single(LYD_CTX(node), a); |
| 1926 | } else if (dup) { |
| 1927 | *dup = a; |
| 1928 | } |
| 1929 | return LY_SUCCESS; |
| 1930 | } |
| 1931 | |
| 1932 | /** |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1933 | * @brief Find @p schema equivalent in @p trg_ctx. |
| 1934 | * |
| 1935 | * @param[in] schema Schema node to find. |
| 1936 | * @param[in] trg_ctx Target context to search in. |
| 1937 | * @param[in] parent Data parent of @p schema, if any. |
Michal Vasko | af3df49 | 2022-12-02 14:03:52 +0100 | [diff] [blame] | 1938 | * @param[in] log Whether to log directly. |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1939 | * @param[out] trg_schema Found schema from @p trg_ctx to use. |
| 1940 | * @return LY_RRR value. |
| 1941 | */ |
| 1942 | static LY_ERR |
Michal Vasko | af3df49 | 2022-12-02 14:03:52 +0100 | [diff] [blame] | 1943 | lyd_find_schema_ctx(const struct lysc_node *schema, const struct ly_ctx *trg_ctx, const struct lyd_node *parent, |
| 1944 | ly_bool log, const struct lysc_node **trg_schema) |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1945 | { |
Michal Vasko | 9beceb8 | 2022-04-05 12:14:15 +0200 | [diff] [blame] | 1946 | const struct lysc_node *src_parent = NULL, *trg_parent = NULL, *sp, *tp; |
| 1947 | const struct lys_module *trg_mod = NULL; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1948 | char *path; |
| 1949 | |
| 1950 | if (!schema) { |
| 1951 | /* opaque node */ |
| 1952 | *trg_schema = NULL; |
| 1953 | return LY_SUCCESS; |
| 1954 | } |
| 1955 | |
Michal Vasko | 9beceb8 | 2022-04-05 12:14:15 +0200 | [diff] [blame] | 1956 | if (lysc_data_parent(schema) && parent && parent->schema) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1957 | /* start from schema parent */ |
Michal Vasko | 9beceb8 | 2022-04-05 12:14:15 +0200 | [diff] [blame] | 1958 | trg_parent = parent->schema; |
| 1959 | src_parent = lysc_data_parent(schema); |
| 1960 | } |
| 1961 | |
| 1962 | do { |
| 1963 | /* find the next parent */ |
| 1964 | sp = schema; |
Michal Vasko | b680820 | 2022-12-02 14:04:23 +0100 | [diff] [blame] | 1965 | while (lysc_data_parent(sp) != src_parent) { |
| 1966 | sp = lysc_data_parent(sp); |
Michal Vasko | 9beceb8 | 2022-04-05 12:14:15 +0200 | [diff] [blame] | 1967 | } |
| 1968 | src_parent = sp; |
| 1969 | |
| 1970 | if (!src_parent->parent) { |
| 1971 | /* find the module first */ |
| 1972 | trg_mod = ly_ctx_get_module_implemented(trg_ctx, src_parent->module->name); |
| 1973 | if (!trg_mod) { |
Michal Vasko | af3df49 | 2022-12-02 14:03:52 +0100 | [diff] [blame] | 1974 | if (log) { |
| 1975 | LOGERR(trg_ctx, LY_ENOTFOUND, "Module \"%s\" not present/implemented in the target context.", |
| 1976 | src_parent->module->name); |
| 1977 | } |
Michal Vasko | 9beceb8 | 2022-04-05 12:14:15 +0200 | [diff] [blame] | 1978 | return LY_ENOTFOUND; |
| 1979 | } |
| 1980 | } |
| 1981 | |
| 1982 | /* find the next parent */ |
| 1983 | assert(trg_parent || trg_mod); |
| 1984 | tp = NULL; |
| 1985 | while ((tp = lys_getnext(tp, trg_parent, trg_mod ? trg_mod->compiled : NULL, 0))) { |
| 1986 | if (!strcmp(tp->name, src_parent->name) && !strcmp(tp->module->name, src_parent->module->name)) { |
| 1987 | break; |
| 1988 | } |
| 1989 | } |
| 1990 | if (!tp) { |
| 1991 | /* schema node not found */ |
Michal Vasko | af3df49 | 2022-12-02 14:03:52 +0100 | [diff] [blame] | 1992 | if (log) { |
| 1993 | path = lysc_path(src_parent, LYSC_PATH_LOG, NULL, 0); |
| 1994 | LOGERR(trg_ctx, LY_ENOTFOUND, "Schema node \"%s\" not found in the target context.", path); |
| 1995 | free(path); |
| 1996 | } |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1997 | return LY_ENOTFOUND; |
| 1998 | } |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1999 | |
Michal Vasko | 9beceb8 | 2022-04-05 12:14:15 +0200 | [diff] [blame] | 2000 | trg_parent = tp; |
| 2001 | } while (schema != src_parent); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2002 | |
Michal Vasko | 9beceb8 | 2022-04-05 12:14:15 +0200 | [diff] [blame] | 2003 | /* success */ |
| 2004 | *trg_schema = trg_parent; |
| 2005 | return LY_SUCCESS; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2006 | } |
| 2007 | |
| 2008 | /** |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2009 | * @brief Duplicate a single node and connect it into @p parent (if present) or last of @p first siblings. |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2010 | * |
aPiecek | 55653c9 | 2023-11-09 13:43:19 +0100 | [diff] [blame] | 2011 | * Ignores ::LYD_DUP_WITH_PARENTS which is supposed to be handled by lyd_dup(). |
Radek Krejci | f8b9517 | 2020-05-15 14:51:06 +0200 | [diff] [blame] | 2012 | * |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2013 | * @param[in] node Node to duplicate. |
| 2014 | * @param[in] trg_ctx Target context for duplicated nodes. |
Radek Krejci | f8b9517 | 2020-05-15 14:51:06 +0200 | [diff] [blame] | 2015 | * @param[in] parent Parent to insert into, NULL for top-level sibling. |
aPiecek | 1462ab1 | 2024-02-07 09:13:29 +0100 | [diff] [blame] | 2016 | * @param[in] insert_order Options for inserting (sorting) duplicated node, @ref insertorder. |
Radek Krejci | f8b9517 | 2020-05-15 14:51:06 +0200 | [diff] [blame] | 2017 | * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set. |
| 2018 | * @param[in] options Bitmask of options flags, see @ref dupoptions. |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2019 | * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it to @p parent / @p first). |
| 2020 | * @return LY_ERR value. |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2021 | */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2022 | static LY_ERR |
aPiecek | 1462ab1 | 2024-02-07 09:13:29 +0100 | [diff] [blame] | 2023 | lyd_dup_r(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node *parent, uint32_t insert_order, |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2024 | struct lyd_node **first, uint32_t options, struct lyd_node **dup_p) |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2025 | { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2026 | LY_ERR ret; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2027 | struct lyd_node *dup = NULL; |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 2028 | struct lyd_meta *meta; |
Michal Vasko | 9cf6242 | 2021-07-01 13:29:32 +0200 | [diff] [blame] | 2029 | struct lyd_attr *attr; |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 2030 | struct lyd_node_any *any; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2031 | const struct lysc_type *type; |
| 2032 | const char *val_can; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2033 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2034 | LY_CHECK_ARG_RET(NULL, node, LY_EINVAL); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2035 | |
Michal Vasko | 19175b6 | 2022-04-01 09:17:07 +0200 | [diff] [blame] | 2036 | if (node->flags & LYD_EXT) { |
Michal Vasko | 53ac4dd | 2022-06-07 10:56:08 +0200 | [diff] [blame] | 2037 | if (options & LYD_DUP_NO_EXT) { |
| 2038 | /* no not duplicate this subtree */ |
| 2039 | return LY_SUCCESS; |
| 2040 | } |
| 2041 | |
Michal Vasko | 19175b6 | 2022-04-01 09:17:07 +0200 | [diff] [blame] | 2042 | /* we need to use the same context */ |
| 2043 | trg_ctx = LYD_CTX(node); |
| 2044 | } |
| 2045 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2046 | if (!node->schema) { |
| 2047 | dup = calloc(1, sizeof(struct lyd_node_opaq)); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2048 | ((struct lyd_node_opaq *)dup)->ctx = trg_ctx; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2049 | } else { |
| 2050 | switch (node->schema->nodetype) { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 2051 | case LYS_RPC: |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2052 | case LYS_ACTION: |
| 2053 | case LYS_NOTIF: |
| 2054 | case LYS_CONTAINER: |
| 2055 | case LYS_LIST: |
| 2056 | dup = calloc(1, sizeof(struct lyd_node_inner)); |
| 2057 | break; |
| 2058 | case LYS_LEAF: |
| 2059 | case LYS_LEAFLIST: |
| 2060 | dup = calloc(1, sizeof(struct lyd_node_term)); |
| 2061 | break; |
| 2062 | case LYS_ANYDATA: |
| 2063 | case LYS_ANYXML: |
| 2064 | dup = calloc(1, sizeof(struct lyd_node_any)); |
| 2065 | break; |
| 2066 | default: |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2067 | LOGINT(trg_ctx); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2068 | ret = LY_EINT; |
| 2069 | goto error; |
| 2070 | } |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2071 | } |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2072 | LY_CHECK_ERR_GOTO(!dup, LOGMEM(trg_ctx); ret = LY_EMEM, error); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2073 | |
Michal Vasko | f6df0a0 | 2020-06-16 13:08:34 +0200 | [diff] [blame] | 2074 | if (options & LYD_DUP_WITH_FLAGS) { |
| 2075 | dup->flags = node->flags; |
| 2076 | } else { |
Michal Vasko | 19175b6 | 2022-04-01 09:17:07 +0200 | [diff] [blame] | 2077 | dup->flags = (node->flags & (LYD_DEFAULT | LYD_EXT)) | LYD_NEW; |
Michal Vasko | f6df0a0 | 2020-06-16 13:08:34 +0200 | [diff] [blame] | 2078 | } |
Igor Ryzhov | 9e7af66 | 2023-10-31 13:27:56 +0200 | [diff] [blame] | 2079 | if (options & LYD_DUP_WITH_PRIV) { |
| 2080 | dup->priv = node->priv; |
| 2081 | } |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2082 | if (trg_ctx == LYD_CTX(node)) { |
| 2083 | dup->schema = node->schema; |
| 2084 | } else { |
Michal Vasko | af3df49 | 2022-12-02 14:03:52 +0100 | [diff] [blame] | 2085 | ret = lyd_find_schema_ctx(node->schema, trg_ctx, parent, 1, &dup->schema); |
Michal Vasko | 27f536f | 2022-04-01 09:17:30 +0200 | [diff] [blame] | 2086 | if (ret) { |
| 2087 | /* has no schema but is not an opaque node */ |
| 2088 | free(dup); |
| 2089 | dup = NULL; |
| 2090 | goto error; |
| 2091 | } |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2092 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2093 | dup->prev = dup; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2094 | |
Michal Vasko | 9cf6242 | 2021-07-01 13:29:32 +0200 | [diff] [blame] | 2095 | /* duplicate metadata/attributes */ |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 2096 | if (!(options & LYD_DUP_NO_META)) { |
Michal Vasko | 9cf6242 | 2021-07-01 13:29:32 +0200 | [diff] [blame] | 2097 | if (!node->schema) { |
| 2098 | LY_LIST_FOR(((struct lyd_node_opaq *)node)->attr, attr) { |
| 2099 | LY_CHECK_GOTO(ret = lyd_dup_attr_single(attr, dup, NULL), error); |
| 2100 | } |
| 2101 | } else { |
| 2102 | LY_LIST_FOR(node->meta, meta) { |
aPiecek | 4168034 | 2023-11-08 10:19:44 +0100 | [diff] [blame] | 2103 | LY_CHECK_GOTO(ret = lyd_dup_meta_single_to_ctx(trg_ctx, meta, dup, NULL), error); |
Michal Vasko | 9cf6242 | 2021-07-01 13:29:32 +0200 | [diff] [blame] | 2104 | } |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 2105 | } |
| 2106 | } |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2107 | |
| 2108 | /* nodetype-specific work */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2109 | if (!dup->schema) { |
| 2110 | struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup; |
| 2111 | struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node; |
| 2112 | struct lyd_node *child; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2113 | |
| 2114 | if (options & LYD_DUP_RECURSIVE) { |
| 2115 | /* duplicate all the children */ |
| 2116 | LY_LIST_FOR(orig->child, child) { |
aPiecek | 1462ab1 | 2024-02-07 09:13:29 +0100 | [diff] [blame] | 2117 | LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, LYD_INSERT_NODE_LAST, NULL, options, NULL), error); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2118 | } |
| 2119 | } |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2120 | LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.name, 0, &opaq->name.name), error); |
| 2121 | LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.prefix, 0, &opaq->name.prefix), error); |
| 2122 | LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.module_ns, 0, &opaq->name.module_ns), error); |
| 2123 | LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->value, 0, &opaq->value), error); |
Michal Vasko | 9cf6242 | 2021-07-01 13:29:32 +0200 | [diff] [blame] | 2124 | opaq->hints = orig->hints; |
| 2125 | opaq->format = orig->format; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 2126 | if (orig->val_prefix_data) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2127 | ret = ly_dup_prefix_data(trg_ctx, opaq->format, orig->val_prefix_data, &opaq->val_prefix_data); |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 2128 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2129 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2130 | } else if (dup->schema->nodetype & LYD_NODE_TERM) { |
| 2131 | struct lyd_node_term *term = (struct lyd_node_term *)dup; |
| 2132 | struct lyd_node_term *orig = (struct lyd_node_term *)node; |
| 2133 | |
| 2134 | term->hash = orig->hash; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2135 | if (trg_ctx == LYD_CTX(node)) { |
| 2136 | ret = orig->value.realtype->plugin->duplicate(trg_ctx, &orig->value, &term->value); |
| 2137 | LY_CHECK_ERR_GOTO(ret, LOGERR(trg_ctx, ret, "Value duplication failed."), error); |
| 2138 | } else { |
| 2139 | /* store canonical value in the target context */ |
| 2140 | val_can = lyd_get_value(node); |
| 2141 | type = ((struct lysc_node_leaf *)term->schema)->type; |
steweg | d4cde64 | 2024-02-21 08:34:16 +0100 | [diff] [blame] | 2142 | ret = lyd_value_store(trg_ctx, &term->value, type, val_can, strlen(val_can), 1, 1, NULL, LY_VALUE_CANON, NULL, |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2143 | LYD_HINT_DATA, term->schema, NULL); |
| 2144 | LY_CHECK_GOTO(ret, error); |
| 2145 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2146 | } else if (dup->schema->nodetype & LYD_NODE_INNER) { |
| 2147 | struct lyd_node_inner *orig = (struct lyd_node_inner *)node; |
| 2148 | struct lyd_node *child; |
| 2149 | |
| 2150 | if (options & LYD_DUP_RECURSIVE) { |
Ondrej Kusnirik | 747486a | 2024-08-19 10:52:44 +0200 | [diff] [blame] | 2151 | /* create a hash table with the size of the previous hash table (duplicate) */ |
| 2152 | if (orig->children_ht) { |
| 2153 | ((struct lyd_node_inner *)dup)->children_ht = lyht_new(orig->children_ht->size, sizeof(struct lyd_node *), lyd_hash_table_val_equal, NULL, 1); |
| 2154 | } |
| 2155 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2156 | /* duplicate all the children */ |
| 2157 | LY_LIST_FOR(orig->child, child) { |
aPiecek | 1462ab1 | 2024-02-07 09:13:29 +0100 | [diff] [blame] | 2158 | LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, LYD_INSERT_NODE_LAST, NULL, options, NULL), error); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2159 | } |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2160 | } else if ((dup->schema->nodetype == LYS_LIST) && !(dup->schema->flags & LYS_KEYLESS)) { |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2161 | /* always duplicate keys of a list */ |
Michal Vasko | 9beceb8 | 2022-04-05 12:14:15 +0200 | [diff] [blame] | 2162 | for (child = orig->child; child && lysc_is_key(child->schema); child = child->next) { |
aPiecek | 1462ab1 | 2024-02-07 09:13:29 +0100 | [diff] [blame] | 2163 | LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, LYD_INSERT_NODE_LAST, NULL, options, NULL), error); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2164 | } |
| 2165 | } |
| 2166 | lyd_hash(dup); |
| 2167 | } else if (dup->schema->nodetype & LYD_NODE_ANY) { |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 2168 | dup->hash = node->hash; |
| 2169 | any = (struct lyd_node_any *)node; |
| 2170 | LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2171 | } |
| 2172 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2173 | /* insert */ |
aPiecek | 1462ab1 | 2024-02-07 09:13:29 +0100 | [diff] [blame] | 2174 | lyd_insert_node(parent, first, dup, insert_order); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2175 | |
| 2176 | if (dup_p) { |
| 2177 | *dup_p = dup; |
| 2178 | } |
| 2179 | return LY_SUCCESS; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2180 | |
| 2181 | error: |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2182 | lyd_free_tree(dup); |
| 2183 | return ret; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2184 | } |
| 2185 | |
Michal Vasko | 29d674b | 2021-08-25 11:18:35 +0200 | [diff] [blame] | 2186 | /** |
| 2187 | * @brief Get a parent node to connect duplicated subtree to. |
| 2188 | * |
| 2189 | * @param[in] node Node (subtree) to duplicate. |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2190 | * @param[in] trg_ctx Target context for duplicated nodes. |
Michal Vasko | 29d674b | 2021-08-25 11:18:35 +0200 | [diff] [blame] | 2191 | * @param[in] parent Initial parent to connect to. |
| 2192 | * @param[in] options Bitmask of options flags, see @ref dupoptions. |
| 2193 | * @param[out] dup_parent First duplicated parent node, if any. |
| 2194 | * @param[out] local_parent Correct parent to directly connect duplicated @p node to. |
| 2195 | * @return LY_ERR value. |
| 2196 | */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2197 | static LY_ERR |
Michal Vasko | 58d89e9 | 2023-05-23 09:56:19 +0200 | [diff] [blame] | 2198 | lyd_dup_get_local_parent(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node *parent, |
| 2199 | uint32_t options, struct lyd_node **dup_parent, struct lyd_node **local_parent) |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2200 | { |
Michal Vasko | 58d89e9 | 2023-05-23 09:56:19 +0200 | [diff] [blame] | 2201 | const struct lyd_node *orig_parent; |
Michal Vasko | eaef7c9 | 2023-10-17 10:06:15 +0200 | [diff] [blame] | 2202 | struct lyd_node *iter = NULL; |
Michal Vasko | 8352219 | 2022-07-20 08:07:34 +0200 | [diff] [blame] | 2203 | ly_bool repeat = 1, ext_parent = 0; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2204 | |
| 2205 | *dup_parent = NULL; |
| 2206 | *local_parent = NULL; |
| 2207 | |
Michal Vasko | 8352219 | 2022-07-20 08:07:34 +0200 | [diff] [blame] | 2208 | if (node->flags & LYD_EXT) { |
| 2209 | ext_parent = 1; |
| 2210 | } |
Michal Vasko | 58d89e9 | 2023-05-23 09:56:19 +0200 | [diff] [blame] | 2211 | for (orig_parent = lyd_parent(node); repeat && orig_parent; orig_parent = lyd_parent(orig_parent)) { |
Michal Vasko | 8352219 | 2022-07-20 08:07:34 +0200 | [diff] [blame] | 2212 | if (ext_parent) { |
| 2213 | /* use the standard context */ |
| 2214 | trg_ctx = LYD_CTX(orig_parent); |
| 2215 | } |
Michal Vasko | 13c5b21 | 2023-02-14 10:03:01 +0100 | [diff] [blame] | 2216 | if (parent && (LYD_CTX(parent) == LYD_CTX(orig_parent)) && (parent->schema == orig_parent->schema)) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2217 | /* stop creating parents, connect what we have into the provided parent */ |
| 2218 | iter = parent; |
| 2219 | repeat = 0; |
Michal Vasko | 13c5b21 | 2023-02-14 10:03:01 +0100 | [diff] [blame] | 2220 | } else if (parent && (LYD_CTX(parent) != LYD_CTX(orig_parent)) && |
| 2221 | lyd_compare_schema_equal(parent->schema, orig_parent->schema) && |
Michal Vasko | 58d89e9 | 2023-05-23 09:56:19 +0200 | [diff] [blame] | 2222 | lyd_compare_schema_parents_equal(parent, orig_parent)) { |
Michal Vasko | 13c5b21 | 2023-02-14 10:03:01 +0100 | [diff] [blame] | 2223 | iter = parent; |
| 2224 | repeat = 0; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2225 | } else { |
| 2226 | iter = NULL; |
aPiecek | 1462ab1 | 2024-02-07 09:13:29 +0100 | [diff] [blame] | 2227 | LY_CHECK_RET(lyd_dup_r(orig_parent, trg_ctx, NULL, LYD_INSERT_NODE_DEFAULT, &iter, options, &iter)); |
Michal Vasko | eaef7c9 | 2023-10-17 10:06:15 +0200 | [diff] [blame] | 2228 | |
| 2229 | /* insert into the previous duplicated parent */ |
| 2230 | if (*dup_parent) { |
aPiecek | 1462ab1 | 2024-02-07 09:13:29 +0100 | [diff] [blame] | 2231 | lyd_insert_node(iter, NULL, *dup_parent, LYD_INSERT_NODE_DEFAULT); |
Michal Vasko | eaef7c9 | 2023-10-17 10:06:15 +0200 | [diff] [blame] | 2232 | } |
| 2233 | |
| 2234 | /* update the last duplicated parent */ |
| 2235 | *dup_parent = iter; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2236 | } |
Michal Vasko | 58d89e9 | 2023-05-23 09:56:19 +0200 | [diff] [blame] | 2237 | |
Michal Vasko | eaef7c9 | 2023-10-17 10:06:15 +0200 | [diff] [blame] | 2238 | /* set the first parent */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2239 | if (!*local_parent) { |
Michal Vasko | 58d89e9 | 2023-05-23 09:56:19 +0200 | [diff] [blame] | 2240 | *local_parent = iter; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2241 | } |
Michal Vasko | 58d89e9 | 2023-05-23 09:56:19 +0200 | [diff] [blame] | 2242 | |
Michal Vasko | 8352219 | 2022-07-20 08:07:34 +0200 | [diff] [blame] | 2243 | if (orig_parent->flags & LYD_EXT) { |
| 2244 | ext_parent = 1; |
| 2245 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2246 | } |
| 2247 | |
| 2248 | if (repeat && parent) { |
| 2249 | /* given parent and created parents chain actually do not interconnect */ |
Michal Vasko | 58d89e9 | 2023-05-23 09:56:19 +0200 | [diff] [blame] | 2250 | LOGERR(trg_ctx, LY_EINVAL, "None of the duplicated node \"%s\" schema parents match the provided parent \"%s\".", |
| 2251 | LYD_NAME(node), LYD_NAME(parent)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2252 | return LY_EINVAL; |
| 2253 | } |
| 2254 | |
Michal Vasko | eaef7c9 | 2023-10-17 10:06:15 +0200 | [diff] [blame] | 2255 | if (*dup_parent && parent) { |
| 2256 | /* last insert into a prevously-existing parent */ |
aPiecek | 1462ab1 | 2024-02-07 09:13:29 +0100 | [diff] [blame] | 2257 | lyd_insert_node(parent, NULL, *dup_parent, LYD_INSERT_NODE_DEFAULT); |
Michal Vasko | eaef7c9 | 2023-10-17 10:06:15 +0200 | [diff] [blame] | 2258 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2259 | return LY_SUCCESS; |
| 2260 | } |
| 2261 | |
| 2262 | static LY_ERR |
Michal Vasko | 58d89e9 | 2023-05-23 09:56:19 +0200 | [diff] [blame] | 2263 | lyd_dup(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node *parent, uint32_t options, |
aPiecek | a7a1de9 | 2024-02-12 14:07:12 +0100 | [diff] [blame] | 2264 | ly_bool nosiblings, struct lyd_node **dup_p) |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2265 | { |
| 2266 | LY_ERR rc; |
Michal Vasko | 9cd2be9 | 2024-09-06 13:29:47 +0200 | [diff] [blame] | 2267 | const struct lyd_node *orig; /* original node to be duplicated */ |
| 2268 | struct lyd_node *first_dup = NULL; /* the first duplicated node, this is returned */ |
| 2269 | struct lyd_node *top = NULL; /* the most higher created node */ |
| 2270 | struct lyd_node *local_parent = NULL; /* the direct parent node for the duplicated node(s) */ |
| 2271 | struct lyd_node *dup = NULL; /* duplicate node */ |
| 2272 | struct lyd_node *first_sibling = NULL; /* first sibling node */ |
| 2273 | const struct lyd_node *first_llist = NULL; /* first duplicated (leaf-)list node, if any */ |
| 2274 | uint32_t insert_order; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2275 | |
Michal Vasko | 1feb9bb | 2022-07-20 08:44:07 +0200 | [diff] [blame] | 2276 | assert(node && trg_ctx); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2277 | |
| 2278 | if (options & LYD_DUP_WITH_PARENTS) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2279 | LY_CHECK_GOTO(rc = lyd_dup_get_local_parent(node, trg_ctx, parent, options & (LYD_DUP_WITH_FLAGS | LYD_DUP_NO_META), |
Michal Vasko | 2090975 | 2021-05-18 16:13:38 +0200 | [diff] [blame] | 2280 | &top, &local_parent), error); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2281 | } else { |
| 2282 | local_parent = parent; |
| 2283 | } |
| 2284 | |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2285 | LY_LIST_FOR(node, orig) { |
Michal Vasko | 35f4d77 | 2021-01-12 12:08:57 +0100 | [diff] [blame] | 2286 | if (lysc_is_key(orig->schema)) { |
| 2287 | if (local_parent) { |
| 2288 | /* the key must already exist in the parent */ |
aPiecek | a7a1de9 | 2024-02-12 14:07:12 +0100 | [diff] [blame] | 2289 | rc = lyd_find_sibling_schema(lyd_child(local_parent), orig->schema, &dup); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2290 | LY_CHECK_ERR_GOTO(rc, LOGINT(trg_ctx), error); |
Michal Vasko | 35f4d77 | 2021-01-12 12:08:57 +0100 | [diff] [blame] | 2291 | } else { |
| 2292 | assert(!(options & LYD_DUP_WITH_PARENTS)); |
| 2293 | /* duplicating a single key, okay, I suppose... */ |
aPiecek | a7a1de9 | 2024-02-12 14:07:12 +0100 | [diff] [blame] | 2294 | rc = lyd_dup_r(orig, trg_ctx, NULL, LYD_INSERT_NODE_DEFAULT, &first_sibling, options, &dup); |
Michal Vasko | 35f4d77 | 2021-01-12 12:08:57 +0100 | [diff] [blame] | 2295 | LY_CHECK_GOTO(rc, error); |
| 2296 | } |
| 2297 | } else { |
Michal Vasko | 9cd2be9 | 2024-09-06 13:29:47 +0200 | [diff] [blame] | 2298 | /* decide insert order */ |
| 2299 | insert_order = (options & LYD_DUP_NO_LYDS) ? LYD_INSERT_NODE_LAST_BY_SCHEMA : LYD_INSERT_NODE_DEFAULT; |
| 2300 | if (first_llist) { |
| 2301 | if (orig->schema != first_llist->schema) { |
| 2302 | /* all the (leaf-)list instances duplicated */ |
| 2303 | first_llist = NULL; |
| 2304 | } else { |
| 2305 | /* duplicating all the instances of a (leaf-)list, no need to change their order */ |
| 2306 | insert_order = LYD_INSERT_NODE_LAST; |
| 2307 | } |
| 2308 | } else if (orig->schema && (orig->schema->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 2309 | /* duplicating the first (leaf-)list instance, duplicate the rest more efficiently */ |
| 2310 | first_llist = orig; |
| 2311 | } |
| 2312 | |
| 2313 | /* duplicate the node */ |
| 2314 | rc = lyd_dup_r(orig, trg_ctx, local_parent, insert_order, &first_sibling, options, &dup); |
Michal Vasko | 35f4d77 | 2021-01-12 12:08:57 +0100 | [diff] [blame] | 2315 | LY_CHECK_GOTO(rc, error); |
Michal Vasko | 9cd2be9 | 2024-09-06 13:29:47 +0200 | [diff] [blame] | 2316 | |
| 2317 | if (first_llist && dup->next) { |
| 2318 | /* orig was not the last node (because we are inserting into a parent with some previous instances), |
| 2319 | * we must check find the order */ |
| 2320 | first_llist = NULL; |
| 2321 | } |
Michal Vasko | 35f4d77 | 2021-01-12 12:08:57 +0100 | [diff] [blame] | 2322 | } |
aPiecek | a7a1de9 | 2024-02-12 14:07:12 +0100 | [diff] [blame] | 2323 | first_dup = first_dup ? first_dup : dup; |
| 2324 | |
Michal Vasko | 9cd2be9 | 2024-09-06 13:29:47 +0200 | [diff] [blame] | 2325 | if (nosiblings) { |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2326 | break; |
| 2327 | } |
| 2328 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2329 | |
aPiecek | a7a1de9 | 2024-02-12 14:07:12 +0100 | [diff] [blame] | 2330 | if (dup_p) { |
| 2331 | *dup_p = first_dup; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2332 | } |
| 2333 | return LY_SUCCESS; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2334 | |
| 2335 | error: |
| 2336 | if (top) { |
| 2337 | lyd_free_tree(top); |
aPiecek | a7a1de9 | 2024-02-12 14:07:12 +0100 | [diff] [blame] | 2338 | } else if (first_dup) { |
| 2339 | lyd_free_siblings(first_dup); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2340 | } else { |
aPiecek | a7a1de9 | 2024-02-12 14:07:12 +0100 | [diff] [blame] | 2341 | lyd_free_siblings(dup); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2342 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2343 | return rc; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2344 | } |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2345 | |
Michal Vasko | 1feb9bb | 2022-07-20 08:44:07 +0200 | [diff] [blame] | 2346 | /** |
| 2347 | * @brief Check the context of node and parent when duplicating nodes. |
| 2348 | * |
| 2349 | * @param[in] node Node to duplicate. |
| 2350 | * @param[in] parent Parent of the duplicated node(s). |
| 2351 | * @return LY_ERR value. |
| 2352 | */ |
| 2353 | static LY_ERR |
| 2354 | lyd_dup_ctx_check(const struct lyd_node *node, const struct lyd_node_inner *parent) |
| 2355 | { |
| 2356 | const struct lyd_node *iter; |
| 2357 | |
| 2358 | if (!node || !parent) { |
| 2359 | return LY_SUCCESS; |
| 2360 | } |
| 2361 | |
| 2362 | if ((LYD_CTX(node) != LYD_CTX(parent))) { |
| 2363 | /* try to find top-level ext data parent */ |
| 2364 | for (iter = node; iter && !(iter->flags & LYD_EXT); iter = lyd_parent(iter)) {} |
| 2365 | |
| 2366 | if (!iter || !lyd_parent(iter) || (LYD_CTX(lyd_parent(iter)) != LYD_CTX(parent))) { |
Michal Vasko | ce55b46 | 2023-02-14 10:03:32 +0100 | [diff] [blame] | 2367 | LOGERR(LYD_CTX(node), LY_EINVAL, "Different contexts used in node duplication."); |
Michal Vasko | 1feb9bb | 2022-07-20 08:44:07 +0200 | [diff] [blame] | 2368 | return LY_EINVAL; |
| 2369 | } |
| 2370 | } |
| 2371 | |
| 2372 | return LY_SUCCESS; |
| 2373 | } |
| 2374 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2375 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2376 | lyd_dup_single(const struct lyd_node *node, struct lyd_node_inner *parent, uint32_t options, struct lyd_node **dup) |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2377 | { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2378 | LY_CHECK_ARG_RET(NULL, node, LY_EINVAL); |
Michal Vasko | 1feb9bb | 2022-07-20 08:44:07 +0200 | [diff] [blame] | 2379 | LY_CHECK_RET(lyd_dup_ctx_check(node, parent)); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2380 | |
Michal Vasko | 58d89e9 | 2023-05-23 09:56:19 +0200 | [diff] [blame] | 2381 | return lyd_dup(node, LYD_CTX(node), (struct lyd_node *)parent, options, 1, dup); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2382 | } |
| 2383 | |
| 2384 | LIBYANG_API_DEF LY_ERR |
| 2385 | lyd_dup_single_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent, |
| 2386 | uint32_t options, struct lyd_node **dup) |
| 2387 | { |
| 2388 | LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL); |
| 2389 | |
Michal Vasko | 58d89e9 | 2023-05-23 09:56:19 +0200 | [diff] [blame] | 2390 | return lyd_dup(node, trg_ctx, (struct lyd_node *)parent, options, 1, dup); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2391 | } |
| 2392 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2393 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2394 | lyd_dup_siblings(const struct lyd_node *node, struct lyd_node_inner *parent, uint32_t options, struct lyd_node **dup) |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2395 | { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2396 | LY_CHECK_ARG_RET(NULL, node, LY_EINVAL); |
Michal Vasko | 1feb9bb | 2022-07-20 08:44:07 +0200 | [diff] [blame] | 2397 | LY_CHECK_RET(lyd_dup_ctx_check(node, parent)); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2398 | |
Michal Vasko | 58d89e9 | 2023-05-23 09:56:19 +0200 | [diff] [blame] | 2399 | return lyd_dup(node, LYD_CTX(node), (struct lyd_node *)parent, options, 0, dup); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2400 | } |
| 2401 | |
| 2402 | LIBYANG_API_DEF LY_ERR |
| 2403 | lyd_dup_siblings_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent, |
| 2404 | uint32_t options, struct lyd_node **dup) |
| 2405 | { |
| 2406 | LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL); |
| 2407 | |
Michal Vasko | 58d89e9 | 2023-05-23 09:56:19 +0200 | [diff] [blame] | 2408 | return lyd_dup(node, trg_ctx, (struct lyd_node *)parent, options, 0, dup); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2409 | } |
| 2410 | |
aPiecek | 4168034 | 2023-11-08 10:19:44 +0100 | [diff] [blame] | 2411 | LY_ERR |
| 2412 | lyd_dup_meta_single_to_ctx(const struct ly_ctx *parent_ctx, const struct lyd_meta *meta, struct lyd_node *parent, |
| 2413 | struct lyd_meta **dup) |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 2414 | { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2415 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 2416 | struct lyd_meta *mt, *last; |
aPiecek | 4168034 | 2023-11-08 10:19:44 +0100 | [diff] [blame] | 2417 | const struct lysc_type *ant_type; |
| 2418 | struct lys_module *mod; |
| 2419 | const char *val_can; |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 2420 | |
aPiecek | 4168034 | 2023-11-08 10:19:44 +0100 | [diff] [blame] | 2421 | LY_CHECK_ARG_RET(NULL, meta, parent, LY_EINVAL); |
Michal Vasko | 33c4897 | 2022-07-20 10:28:07 +0200 | [diff] [blame] | 2422 | |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 2423 | /* create a copy */ |
| 2424 | mt = calloc(1, sizeof *mt); |
aPiecek | 4168034 | 2023-11-08 10:19:44 +0100 | [diff] [blame] | 2425 | LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(parent)), LY_EMEM); |
| 2426 | |
| 2427 | if (parent_ctx != meta->annotation->module->ctx) { |
| 2428 | /* different contexts */ |
| 2429 | mod = ly_ctx_get_module(parent_ctx, meta->annotation->module->name, meta->annotation->module->revision); |
| 2430 | |
| 2431 | /* annotation */ |
| 2432 | mt->annotation = lyd_get_meta_annotation(mod, meta->name, strlen(meta->name)); |
| 2433 | lyplg_ext_get_storage(mt->annotation, LY_STMT_TYPE, sizeof ant_type, (const void **)&ant_type); |
| 2434 | LY_CHECK_ERR_GOTO((ret = mt->annotation ? LY_SUCCESS : LY_EINVAL), LOGERR(parent_ctx, LY_EINVAL, |
| 2435 | "Annotation for metadata %s not found, value duplication failed.", meta->name), finish); |
| 2436 | |
| 2437 | /* duplicate callback expect only the same contexts, so use the store callback */ |
| 2438 | val_can = lyd_value_get_canonical(meta->annotation->module->ctx, &meta->value); |
steweg | d4cde64 | 2024-02-21 08:34:16 +0100 | [diff] [blame] | 2439 | ret = lyd_value_store(parent_ctx, &mt->value, ant_type, val_can, strlen(val_can), 1, 1, NULL, |
aPiecek | 4168034 | 2023-11-08 10:19:44 +0100 | [diff] [blame] | 2440 | LY_VALUE_CANON, NULL, LYD_HINT_DATA, parent->schema, NULL); |
| 2441 | } else { |
| 2442 | /* annotation */ |
| 2443 | mt->annotation = meta->annotation; |
| 2444 | /* duplication of value */ |
| 2445 | ret = meta->value.realtype->plugin->duplicate(parent_ctx, &meta->value, &mt->value); |
| 2446 | } |
| 2447 | LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(parent), LY_EINT, "Value duplication failed."), finish); |
| 2448 | LY_CHECK_GOTO(ret = lydict_insert(parent_ctx, meta->name, 0, &mt->name), finish); |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 2449 | |
| 2450 | /* insert as the last attribute */ |
aPiecek | 4168034 | 2023-11-08 10:19:44 +0100 | [diff] [blame] | 2451 | mt->parent = parent; |
| 2452 | if (parent->meta) { |
| 2453 | for (last = parent->meta; last->next; last = last->next) {} |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 2454 | last->next = mt; |
| 2455 | } else { |
aPiecek | 4168034 | 2023-11-08 10:19:44 +0100 | [diff] [blame] | 2456 | parent->meta = mt; |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 2457 | } |
| 2458 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2459 | finish: |
| 2460 | if (ret) { |
| 2461 | lyd_free_meta_single(mt); |
| 2462 | } else if (dup) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2463 | *dup = mt; |
| 2464 | } |
| 2465 | return LY_SUCCESS; |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 2466 | } |
| 2467 | |
aPiecek | 4168034 | 2023-11-08 10:19:44 +0100 | [diff] [blame] | 2468 | LIBYANG_API_DEF LY_ERR |
| 2469 | lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup) |
| 2470 | { |
| 2471 | LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL); |
| 2472 | |
| 2473 | /* log to node context but value must always use the annotation context */ |
| 2474 | return lyd_dup_meta_single_to_ctx(meta->annotation->module->ctx, meta, node, dup); |
| 2475 | } |
| 2476 | |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2477 | /** |
| 2478 | * @brief Merge a source sibling into target siblings. |
| 2479 | * |
| 2480 | * @param[in,out] first_trg First target sibling, is updated if top-level. |
| 2481 | * @param[in] parent_trg Target parent. |
aPiecek | 55653c9 | 2023-11-09 13:43:19 +0100 | [diff] [blame] | 2482 | * @param[in,out] sibling_src_p Source sibling to merge, set to NULL if spent. |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 2483 | * @param[in] merge_cb Optional merge callback. |
| 2484 | * @param[in] cb_data Arbitrary callback data. |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2485 | * @param[in] options Merge options. |
aPiecek | e6955f4 | 2024-01-19 14:01:54 +0100 | [diff] [blame] | 2486 | * @param[in] lyds Pool of lyds data which can be reused. |
| 2487 | * @param[in,out] leader_p Cached first instance of target (leaf-)list. |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 2488 | * @param[in,out] dup_inst Duplicate instance cache for all @p first_trg siblings. |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2489 | * @return LY_ERR value. |
| 2490 | */ |
| 2491 | static LY_ERR |
aPiecek | e6955f4 | 2024-01-19 14:01:54 +0100 | [diff] [blame] | 2492 | lyd_merge_sibling_r(struct lyd_node **first_trg, struct lyd_node *parent_trg, |
| 2493 | const struct lyd_node **sibling_src_p, lyd_merge_cb merge_cb, void *cb_data, uint16_t options, |
| 2494 | struct lyds_pool *lyds, struct lyd_node **leader_p, struct ly_ht **dup_inst) |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2495 | { |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2496 | const struct lyd_node *child_src, *tmp, *sibling_src; |
aPiecek | e6955f4 | 2024-01-19 14:01:54 +0100 | [diff] [blame] | 2497 | struct lyd_node *match_trg, *dup_src, *elem, *leader; |
Michal Vasko | d1afa50 | 2022-01-27 16:18:12 +0100 | [diff] [blame] | 2498 | struct lyd_node_opaq *opaq_trg, *opaq_src; |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2499 | struct lysc_type *type; |
aPiecek | e6955f4 | 2024-01-19 14:01:54 +0100 | [diff] [blame] | 2500 | const struct lysc_node *schema; |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 2501 | struct ly_ht *child_dup_inst = NULL; |
Michal Vasko | 42c96e2 | 2024-01-18 08:18:06 +0100 | [diff] [blame] | 2502 | LY_ERR r; |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 2503 | ly_bool first_inst = 0; |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2504 | |
| 2505 | sibling_src = *sibling_src_p; |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 2506 | if (!sibling_src->schema) { |
| 2507 | /* try to find the same opaque node */ |
Michal Vasko | 42c96e2 | 2024-01-18 08:18:06 +0100 | [diff] [blame] | 2508 | r = lyd_find_sibling_opaq_next(*first_trg, LYD_NAME(sibling_src), &match_trg); |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 2509 | } else if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2510 | /* try to find the exact instance */ |
Michal Vasko | 42c96e2 | 2024-01-18 08:18:06 +0100 | [diff] [blame] | 2511 | r = lyd_find_sibling_first(*first_trg, sibling_src, &match_trg); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2512 | } else { |
| 2513 | /* try to simply find the node, there cannot be more instances */ |
Michal Vasko | 42c96e2 | 2024-01-18 08:18:06 +0100 | [diff] [blame] | 2514 | r = lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2515 | } |
Michal Vasko | 42c96e2 | 2024-01-18 08:18:06 +0100 | [diff] [blame] | 2516 | LY_CHECK_RET(r && (r != LY_ENOTFOUND), r); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2517 | |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 2518 | if (match_trg) { |
| 2519 | /* update match as needed */ |
| 2520 | LY_CHECK_RET(lyd_dup_inst_next(&match_trg, *first_trg, dup_inst)); |
| 2521 | } else { |
| 2522 | /* first instance of this node */ |
| 2523 | first_inst = 1; |
| 2524 | } |
| 2525 | |
| 2526 | if (match_trg) { |
| 2527 | /* call callback */ |
| 2528 | if (merge_cb) { |
| 2529 | LY_CHECK_RET(merge_cb(match_trg, sibling_src, cb_data)); |
| 2530 | } |
| 2531 | |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2532 | /* node found, make sure even value matches for all node types */ |
Michal Vasko | d1afa50 | 2022-01-27 16:18:12 +0100 | [diff] [blame] | 2533 | if (!match_trg->schema) { |
| 2534 | if (lyd_compare_single(sibling_src, match_trg, 0)) { |
| 2535 | /* update value */ |
| 2536 | opaq_trg = (struct lyd_node_opaq *)match_trg; |
| 2537 | opaq_src = (struct lyd_node_opaq *)sibling_src; |
| 2538 | |
| 2539 | lydict_remove(LYD_CTX(opaq_trg), opaq_trg->value); |
| 2540 | lydict_insert(LYD_CTX(opaq_trg), opaq_src->value, 0, &opaq_trg->value); |
| 2541 | opaq_trg->hints = opaq_src->hints; |
| 2542 | |
| 2543 | ly_free_prefix_data(opaq_trg->format, opaq_trg->val_prefix_data); |
| 2544 | opaq_trg->format = opaq_src->format; |
| 2545 | ly_dup_prefix_data(LYD_CTX(opaq_trg), opaq_src->format, opaq_src->val_prefix_data, |
| 2546 | &opaq_trg->val_prefix_data); |
| 2547 | } |
| 2548 | } else if ((match_trg->schema->nodetype == LYS_LEAF) && |
| 2549 | lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) { |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2550 | /* since they are different, they cannot both be default */ |
| 2551 | assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT)); |
| 2552 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2553 | /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */ |
| 2554 | if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) { |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2555 | type = ((struct lysc_node_leaf *)match_trg->schema)->type; |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2556 | type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value); |
| 2557 | LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2558 | &((struct lyd_node_term *)match_trg)->value)); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2559 | |
| 2560 | /* copy flags and add LYD_NEW */ |
Michal Vasko | 7e8315b | 2021-08-03 17:01:06 +0200 | [diff] [blame] | 2561 | match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2562 | } |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 2563 | } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) { |
Michal Vasko | 4c583e8 | 2020-07-17 12:16:14 +0200 | [diff] [blame] | 2564 | /* update value */ |
| 2565 | LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2566 | ((struct lyd_node_any *)sibling_src)->value_type)); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2567 | |
| 2568 | /* copy flags and add LYD_NEW */ |
Michal Vasko | 7e8315b | 2021-08-03 17:01:06 +0200 | [diff] [blame] | 2569 | match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW); |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 2570 | } |
| 2571 | |
| 2572 | /* check descendants, recursively */ |
Michal Vasko | 42c96e2 | 2024-01-18 08:18:06 +0100 | [diff] [blame] | 2573 | r = LY_SUCCESS; |
aPiecek | e6955f4 | 2024-01-19 14:01:54 +0100 | [diff] [blame] | 2574 | leader = NULL; |
| 2575 | schema = NULL; |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 2576 | LY_LIST_FOR_SAFE(lyd_child_no_keys(sibling_src), tmp, child_src) { |
aPiecek | e6955f4 | 2024-01-19 14:01:54 +0100 | [diff] [blame] | 2577 | if ((options & LYD_MERGE_DESTRUCT) && (schema != child_src->schema) && LYDS_NODE_IS_LEADER(child_src)) { |
| 2578 | schema = child_src->schema; |
| 2579 | /* unlink lyds data and add them to the pool */ |
| 2580 | lyds_pool_add((struct lyd_node *)child_src, lyds); |
| 2581 | } |
| 2582 | |
| 2583 | r = lyd_merge_sibling_r(lyd_node_child_p(match_trg), match_trg, &child_src, |
| 2584 | merge_cb, cb_data, options, lyds, &leader, &child_dup_inst); |
Michal Vasko | 42c96e2 | 2024-01-18 08:18:06 +0100 | [diff] [blame] | 2585 | if (r) { |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 2586 | break; |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2587 | } |
| 2588 | } |
aPiecek | e6955f4 | 2024-01-19 14:01:54 +0100 | [diff] [blame] | 2589 | |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 2590 | lyd_dup_inst_free(child_dup_inst); |
Michal Vasko | 42c96e2 | 2024-01-18 08:18:06 +0100 | [diff] [blame] | 2591 | LY_CHECK_RET(r); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2592 | } else { |
| 2593 | /* node not found, merge it */ |
| 2594 | if (options & LYD_MERGE_DESTRUCT) { |
| 2595 | dup_src = (struct lyd_node *)sibling_src; |
aPiecek | 743184b | 2024-02-01 13:25:56 +0100 | [diff] [blame] | 2596 | lyd_unlink_ignore_lyds(NULL, dup_src); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2597 | /* spend it */ |
| 2598 | *sibling_src_p = NULL; |
| 2599 | } else { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2600 | LY_CHECK_RET(lyd_dup_single(sibling_src, NULL, LYD_DUP_RECURSIVE | LYD_DUP_WITH_FLAGS, &dup_src)); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2601 | } |
| 2602 | |
Michal Vasko | 7e8315b | 2021-08-03 17:01:06 +0200 | [diff] [blame] | 2603 | if (!(options & LYD_MERGE_WITH_FLAGS)) { |
| 2604 | /* set LYD_NEW for all the new nodes, required for validation */ |
| 2605 | LYD_TREE_DFS_BEGIN(dup_src, elem) { |
| 2606 | elem->flags |= LYD_NEW; |
| 2607 | LYD_TREE_DFS_END(dup_src, elem); |
| 2608 | } |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2609 | } |
| 2610 | |
aPiecek | e6955f4 | 2024-01-19 14:01:54 +0100 | [diff] [blame] | 2611 | if (lyds->rbn) { |
| 2612 | /* insert node and try to reuse free lyds data */ |
| 2613 | lyds_insert2(parent_trg, first_trg, leader_p, dup_src, lyds); |
| 2614 | } else { |
| 2615 | /* generic insert node */ |
aPiecek | 1462ab1 | 2024-02-07 09:13:29 +0100 | [diff] [blame] | 2616 | lyd_insert_node(parent_trg, first_trg, dup_src, LYD_INSERT_NODE_DEFAULT); |
aPiecek | e6955f4 | 2024-01-19 14:01:54 +0100 | [diff] [blame] | 2617 | } |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 2618 | |
| 2619 | if (first_inst) { |
| 2620 | /* remember not to find this instance next time */ |
| 2621 | LY_CHECK_RET(lyd_dup_inst_next(&dup_src, *first_trg, dup_inst)); |
| 2622 | } |
| 2623 | |
| 2624 | /* call callback, no source node */ |
| 2625 | if (merge_cb) { |
| 2626 | LY_CHECK_RET(merge_cb(dup_src, NULL, cb_data)); |
| 2627 | } |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2628 | } |
| 2629 | |
| 2630 | return LY_SUCCESS; |
| 2631 | } |
| 2632 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2633 | static LY_ERR |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 2634 | lyd_merge(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod, |
| 2635 | lyd_merge_cb merge_cb, void *cb_data, uint16_t options, ly_bool nosiblings) |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2636 | { |
| 2637 | const struct lyd_node *sibling_src, *tmp; |
aPiecek | e6955f4 | 2024-01-19 14:01:54 +0100 | [diff] [blame] | 2638 | const struct lysc_node *schema; |
| 2639 | struct lyd_node *leader; |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 2640 | struct ly_ht *dup_inst = NULL; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2641 | ly_bool first; |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 2642 | LY_ERR ret = LY_SUCCESS; |
aPiecek | e6955f4 | 2024-01-19 14:01:54 +0100 | [diff] [blame] | 2643 | struct lyds_pool lyds = {0}; |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2644 | |
| 2645 | LY_CHECK_ARG_RET(NULL, target, LY_EINVAL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 2646 | LY_CHECK_CTX_EQUAL_RET(*target ? LYD_CTX(*target) : NULL, source ? LYD_CTX(source) : NULL, mod ? mod->ctx : NULL, |
| 2647 | LY_EINVAL); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2648 | |
| 2649 | if (!source) { |
| 2650 | /* nothing to merge */ |
| 2651 | return LY_SUCCESS; |
| 2652 | } |
| 2653 | |
Michal Vasko | 831422b | 2020-11-24 11:20:51 +0100 | [diff] [blame] | 2654 | if ((*target && lysc_data_parent((*target)->schema)) || lysc_data_parent(source->schema)) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2655 | LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2656 | return LY_EINVAL; |
| 2657 | } |
| 2658 | |
aPiecek | e6955f4 | 2024-01-19 14:01:54 +0100 | [diff] [blame] | 2659 | leader = NULL; |
| 2660 | schema = NULL; |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2661 | LY_LIST_FOR_SAFE(source, tmp, sibling_src) { |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 2662 | if (mod && (lyd_owner_module(sibling_src) != mod)) { |
| 2663 | /* skip data nodes from different modules */ |
| 2664 | continue; |
| 2665 | } |
| 2666 | |
aPiecek | e6955f4 | 2024-01-19 14:01:54 +0100 | [diff] [blame] | 2667 | if ((options & LYD_MERGE_DESTRUCT) && (schema != sibling_src->schema) && LYDS_NODE_IS_LEADER(sibling_src)) { |
| 2668 | schema = sibling_src->schema; |
| 2669 | /* unlink lyds data and add them to the pool */ |
| 2670 | lyds_pool_add((struct lyd_node *)sibling_src, &lyds); |
| 2671 | } |
| 2672 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2673 | first = (sibling_src == source) ? 1 : 0; |
aPiecek | e6955f4 | 2024-01-19 14:01:54 +0100 | [diff] [blame] | 2674 | ret = lyd_merge_sibling_r(target, NULL, &sibling_src, merge_cb, cb_data, options, |
| 2675 | &lyds, &leader, &dup_inst); |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 2676 | if (ret) { |
| 2677 | break; |
| 2678 | } |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2679 | if (first && !sibling_src) { |
| 2680 | /* source was spent (unlinked), move to the next node */ |
| 2681 | source = tmp; |
| 2682 | } |
| 2683 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2684 | if (nosiblings) { |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2685 | break; |
| 2686 | } |
| 2687 | } |
aPiecek | e6955f4 | 2024-01-19 14:01:54 +0100 | [diff] [blame] | 2688 | lyds_pool_clean(&lyds); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2689 | |
| 2690 | if (options & LYD_MERGE_DESTRUCT) { |
| 2691 | /* free any leftover source data that were not merged */ |
| 2692 | lyd_free_siblings((struct lyd_node *)source); |
| 2693 | } |
| 2694 | |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 2695 | lyd_dup_inst_free(dup_inst); |
| 2696 | return ret; |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2697 | } |
| 2698 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2699 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2700 | lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options) |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2701 | { |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 2702 | return lyd_merge(target, source, NULL, NULL, NULL, options, 1); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2703 | } |
| 2704 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2705 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2706 | lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options) |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2707 | { |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 2708 | return lyd_merge(target, source, NULL, NULL, NULL, options, 0); |
| 2709 | } |
| 2710 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2711 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 2712 | lyd_merge_module(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod, |
| 2713 | lyd_merge_cb merge_cb, void *cb_data, uint16_t options) |
| 2714 | { |
| 2715 | return lyd_merge(target, source, mod, merge_cb, cb_data, options, 0); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2716 | } |
| 2717 | |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2718 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2719 | lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static) |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2720 | { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 2721 | /* ending \0 */ |
| 2722 | ++reqlen; |
| 2723 | |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2724 | if (reqlen > *buflen) { |
| 2725 | if (is_static) { |
| 2726 | return LY_EINCOMPLETE; |
| 2727 | } |
| 2728 | |
| 2729 | *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer); |
| 2730 | if (!*buffer) { |
| 2731 | return LY_EMEM; |
| 2732 | } |
| 2733 | |
| 2734 | *buflen = reqlen; |
| 2735 | } |
| 2736 | |
| 2737 | return LY_SUCCESS; |
| 2738 | } |
| 2739 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 2740 | LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2741 | lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, ly_bool is_static) |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2742 | { |
| 2743 | const struct lyd_node *key; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2744 | size_t len; |
| 2745 | const char *val; |
| 2746 | char quot; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2747 | |
Michal Vasko | 824d083 | 2021-11-04 15:36:51 +0100 | [diff] [blame] | 2748 | for (key = lyd_child(node); key && key->schema && (key->schema->flags & LYS_KEY); key = key->next) { |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 2749 | val = lyd_get_value(key); |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2750 | len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2751 | LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static)); |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2752 | |
| 2753 | quot = '\''; |
| 2754 | if (strchr(val, '\'')) { |
| 2755 | quot = '"'; |
| 2756 | } |
| 2757 | *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot); |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2758 | } |
| 2759 | |
| 2760 | return LY_SUCCESS; |
| 2761 | } |
| 2762 | |
| 2763 | /** |
| 2764 | * @brief Append leaf-list value predicate to path. |
| 2765 | * |
| 2766 | * @param[in] node Node to print. |
| 2767 | * @param[in,out] buffer Buffer to print to. |
| 2768 | * @param[in,out] buflen Current buffer length. |
| 2769 | * @param[in,out] bufused Current number of characters used in @p buffer. |
| 2770 | * @param[in] is_static Whether buffer is static or can be reallocated. |
| 2771 | * @return LY_ERR |
| 2772 | */ |
| 2773 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2774 | lyd_path_leaflist_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, ly_bool is_static) |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2775 | { |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2776 | size_t len; |
| 2777 | const char *val; |
| 2778 | char quot; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2779 | |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 2780 | val = lyd_get_value(node); |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 2781 | len = 4 + strlen(val) + 2; /* "[.='" + val + "']" */ |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2782 | LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static)); |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2783 | |
| 2784 | quot = '\''; |
| 2785 | if (strchr(val, '\'')) { |
| 2786 | quot = '"'; |
| 2787 | } |
| 2788 | *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot); |
| 2789 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2790 | return LY_SUCCESS; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2791 | } |
| 2792 | |
| 2793 | /** |
| 2794 | * @brief Append node position (relative to its other instances) predicate to path. |
| 2795 | * |
| 2796 | * @param[in] node Node to print. |
| 2797 | * @param[in,out] buffer Buffer to print to. |
| 2798 | * @param[in,out] buflen Current buffer length. |
| 2799 | * @param[in,out] bufused Current number of characters used in @p buffer. |
| 2800 | * @param[in] is_static Whether buffer is static or can be reallocated. |
| 2801 | * @return LY_ERR |
| 2802 | */ |
| 2803 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2804 | lyd_path_position_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, ly_bool is_static) |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2805 | { |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2806 | size_t len; |
Michal Vasko | 50cc056 | 2021-05-18 16:15:43 +0200 | [diff] [blame] | 2807 | uint32_t pos; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2808 | char *val = NULL; |
| 2809 | LY_ERR rc; |
| 2810 | |
Michal Vasko | 50cc056 | 2021-05-18 16:15:43 +0200 | [diff] [blame] | 2811 | pos = lyd_list_pos(node); |
| 2812 | if (asprintf(&val, "%" PRIu32, pos) == -1) { |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2813 | return LY_EMEM; |
| 2814 | } |
| 2815 | |
| 2816 | len = 1 + strlen(val) + 1; |
| 2817 | rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static); |
| 2818 | if (rc != LY_SUCCESS) { |
| 2819 | goto cleanup; |
| 2820 | } |
| 2821 | |
| 2822 | *bufused += sprintf(*buffer + *bufused, "[%s]", val); |
| 2823 | |
| 2824 | cleanup: |
| 2825 | free(val); |
| 2826 | return rc; |
| 2827 | } |
| 2828 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2829 | LIBYANG_API_DEF char * |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2830 | lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen) |
| 2831 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2832 | ly_bool is_static = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2833 | uint32_t i, depth; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 2834 | size_t bufused = 0, len; |
Michal Vasko | 12eb622 | 2022-03-18 13:35:49 +0100 | [diff] [blame] | 2835 | const struct lyd_node *iter, *parent; |
| 2836 | const struct lys_module *mod, *prev_mod; |
Michal Vasko | 790b2bc | 2020-08-03 13:35:06 +0200 | [diff] [blame] | 2837 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2838 | |
| 2839 | LY_CHECK_ARG_RET(NULL, node, NULL); |
| 2840 | if (buffer) { |
Michal Vasko | 16385f4 | 2021-05-18 16:16:09 +0200 | [diff] [blame] | 2841 | LY_CHECK_ARG_RET(LYD_CTX(node), buflen > 1, NULL); |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2842 | is_static = 1; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 2843 | } else { |
| 2844 | buflen = 0; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2845 | } |
| 2846 | |
| 2847 | switch (pathtype) { |
Radek Krejci | 635d2b8 | 2021-01-04 11:26:51 +0100 | [diff] [blame] | 2848 | case LYD_PATH_STD: |
| 2849 | case LYD_PATH_STD_NO_LAST_PRED: |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 2850 | depth = 1; |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 2851 | for (iter = node; iter->parent; iter = lyd_parent(iter)) { |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2852 | ++depth; |
| 2853 | } |
| 2854 | |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2855 | goto iter_print; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 2856 | while (depth) { |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2857 | /* find the right node */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 2858 | for (iter = node, i = 1; i < depth; iter = lyd_parent(iter), ++i) {} |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2859 | iter_print: |
Michal Vasko | 12eb622 | 2022-03-18 13:35:49 +0100 | [diff] [blame] | 2860 | /* get the module */ |
Michal Vasko | 420cc25 | 2023-08-24 08:14:24 +0200 | [diff] [blame] | 2861 | mod = lyd_node_module(iter); |
Michal Vasko | 12eb622 | 2022-03-18 13:35:49 +0100 | [diff] [blame] | 2862 | parent = lyd_parent(iter); |
Michal Vasko | 420cc25 | 2023-08-24 08:14:24 +0200 | [diff] [blame] | 2863 | prev_mod = lyd_node_module(parent); |
Michal Vasko | 12eb622 | 2022-03-18 13:35:49 +0100 | [diff] [blame] | 2864 | if (prev_mod == mod) { |
| 2865 | mod = NULL; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2866 | } |
| 2867 | |
| 2868 | /* realloc string */ |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 2869 | len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) : |
| 2870 | strlen(((struct lyd_node_opaq *)iter)->name.name)); |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2871 | rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static); |
| 2872 | if (rc != LY_SUCCESS) { |
| 2873 | break; |
| 2874 | } |
| 2875 | |
| 2876 | /* print next node */ |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 2877 | bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", LYD_NAME(iter)); |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2878 | |
Michal Vasko | 790b2bc | 2020-08-03 13:35:06 +0200 | [diff] [blame] | 2879 | /* do not always print the last (first) predicate */ |
Radek Krejci | 635d2b8 | 2021-01-04 11:26:51 +0100 | [diff] [blame] | 2880 | if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) { |
Michal Vasko | 790b2bc | 2020-08-03 13:35:06 +0200 | [diff] [blame] | 2881 | switch (iter->schema->nodetype) { |
| 2882 | case LYS_LIST: |
| 2883 | if (iter->schema->flags & LYS_KEYLESS) { |
| 2884 | /* print its position */ |
| 2885 | rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static); |
| 2886 | } else { |
| 2887 | /* print all list keys in predicates */ |
| 2888 | rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static); |
| 2889 | } |
| 2890 | break; |
| 2891 | case LYS_LEAFLIST: |
| 2892 | if (iter->schema->flags & LYS_CONFIG_W) { |
| 2893 | /* print leaf-list value */ |
| 2894 | rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static); |
| 2895 | } else { |
| 2896 | /* print its position */ |
| 2897 | rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static); |
| 2898 | } |
| 2899 | break; |
| 2900 | default: |
| 2901 | /* nothing to print more */ |
| 2902 | break; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2903 | } |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2904 | } |
| 2905 | if (rc != LY_SUCCESS) { |
| 2906 | break; |
| 2907 | } |
| 2908 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 2909 | --depth; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2910 | } |
| 2911 | break; |
| 2912 | } |
| 2913 | |
| 2914 | return buffer; |
| 2915 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2916 | |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 2917 | char * |
| 2918 | lyd_path_set(const struct ly_set *dnodes, LYD_PATH_TYPE pathtype) |
| 2919 | { |
| 2920 | uint32_t depth; |
| 2921 | size_t bufused = 0, buflen = 0, len; |
| 2922 | char *buffer = NULL; |
| 2923 | const struct lyd_node *iter, *parent; |
| 2924 | const struct lys_module *mod, *prev_mod; |
| 2925 | LY_ERR rc = LY_SUCCESS; |
| 2926 | |
| 2927 | switch (pathtype) { |
| 2928 | case LYD_PATH_STD: |
| 2929 | case LYD_PATH_STD_NO_LAST_PRED: |
| 2930 | for (depth = 1; depth <= dnodes->count; ++depth) { |
| 2931 | /* current node */ |
| 2932 | iter = dnodes->dnodes[depth - 1]; |
Michal Vasko | 420cc25 | 2023-08-24 08:14:24 +0200 | [diff] [blame] | 2933 | mod = lyd_node_module(iter); |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 2934 | |
| 2935 | /* parent */ |
| 2936 | parent = (depth > 1) ? dnodes->dnodes[depth - 2] : NULL; |
Michal Vasko | 85be65e | 2023-06-13 09:44:17 +0200 | [diff] [blame] | 2937 | assert(!parent || !iter->schema || !parent->schema || (parent->schema->nodetype & LYD_NODE_ANY) || |
| 2938 | (lysc_data_parent(iter->schema) == parent->schema) || |
Michal Vasko | 539d6a9 | 2023-09-11 10:31:12 +0200 | [diff] [blame] | 2939 | (!lysc_data_parent(iter->schema) && (LYD_CTX(iter) != LYD_CTX(parent))) || |
| 2940 | (parent->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))); |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 2941 | |
| 2942 | /* get module to print, if any */ |
Michal Vasko | 420cc25 | 2023-08-24 08:14:24 +0200 | [diff] [blame] | 2943 | prev_mod = lyd_node_module(parent); |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 2944 | if (prev_mod == mod) { |
| 2945 | mod = NULL; |
| 2946 | } |
| 2947 | |
| 2948 | /* realloc string */ |
| 2949 | len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) : |
| 2950 | strlen(((struct lyd_node_opaq *)iter)->name.name)); |
| 2951 | if ((rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, 0))) { |
| 2952 | break; |
| 2953 | } |
| 2954 | |
| 2955 | /* print next node */ |
| 2956 | bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", LYD_NAME(iter)); |
| 2957 | |
| 2958 | /* do not always print the last (first) predicate */ |
| 2959 | if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) { |
| 2960 | switch (iter->schema->nodetype) { |
| 2961 | case LYS_LIST: |
| 2962 | if (iter->schema->flags & LYS_KEYLESS) { |
| 2963 | /* print its position */ |
| 2964 | rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, 0); |
| 2965 | } else { |
| 2966 | /* print all list keys in predicates */ |
| 2967 | rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, 0); |
| 2968 | } |
| 2969 | break; |
| 2970 | case LYS_LEAFLIST: |
| 2971 | if (iter->schema->flags & LYS_CONFIG_W) { |
| 2972 | /* print leaf-list value */ |
| 2973 | rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, 0); |
| 2974 | } else { |
| 2975 | /* print its position */ |
| 2976 | rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, 0); |
| 2977 | } |
| 2978 | break; |
| 2979 | default: |
| 2980 | /* nothing to print more */ |
| 2981 | break; |
| 2982 | } |
| 2983 | } |
| 2984 | if (rc) { |
| 2985 | break; |
| 2986 | } |
| 2987 | } |
| 2988 | break; |
| 2989 | } |
| 2990 | |
| 2991 | return buffer; |
| 2992 | } |
| 2993 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2994 | LIBYANG_API_DEF struct lyd_meta * |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 2995 | lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name) |
| 2996 | { |
| 2997 | struct lyd_meta *ret = NULL; |
| 2998 | const struct ly_ctx *ctx; |
| 2999 | const char *prefix, *tmp; |
| 3000 | char *str; |
| 3001 | size_t pref_len, name_len; |
| 3002 | |
| 3003 | LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 3004 | LY_CHECK_CTX_EQUAL_RET(first ? first->annotation->module->ctx : NULL, module ? module->ctx : NULL, NULL); |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 3005 | |
| 3006 | if (!first) { |
| 3007 | return NULL; |
| 3008 | } |
| 3009 | |
| 3010 | ctx = first->annotation->module->ctx; |
| 3011 | |
| 3012 | /* parse the name */ |
| 3013 | tmp = name; |
| 3014 | if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) { |
| 3015 | LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name); |
| 3016 | return NULL; |
| 3017 | } |
| 3018 | |
| 3019 | /* find the module */ |
| 3020 | if (prefix) { |
| 3021 | str = strndup(prefix, pref_len); |
| 3022 | module = ly_ctx_get_module_latest(ctx, str); |
| 3023 | free(str); |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 3024 | LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", (int)pref_len, prefix), NULL); |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 3025 | } |
| 3026 | |
| 3027 | /* find the metadata */ |
| 3028 | LY_LIST_FOR(first, first) { |
| 3029 | if ((first->annotation->module == module) && !strcmp(first->name, name)) { |
| 3030 | ret = (struct lyd_meta *)first; |
| 3031 | break; |
| 3032 | } |
| 3033 | } |
| 3034 | |
| 3035 | return ret; |
| 3036 | } |
| 3037 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 3038 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3039 | lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match) |
| 3040 | { |
Michal Vasko | 9beceb8 | 2022-04-05 12:14:15 +0200 | [diff] [blame] | 3041 | struct lyd_node **match_p, *iter, *dup = NULL; |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3042 | struct lyd_node_inner *parent; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 3043 | ly_bool found; |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3044 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 3045 | LY_CHECK_ARG_RET(NULL, target, LY_EINVAL); |
Michal Vasko | 9beceb8 | 2022-04-05 12:14:15 +0200 | [diff] [blame] | 3046 | if (!siblings) { |
| 3047 | /* no data */ |
| 3048 | if (match) { |
| 3049 | *match = NULL; |
| 3050 | } |
| 3051 | return LY_ENOTFOUND; |
| 3052 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3053 | |
Michal Vasko | 9beceb8 | 2022-04-05 12:14:15 +0200 | [diff] [blame] | 3054 | if (LYD_CTX(siblings) != LYD_CTX(target)) { |
| 3055 | /* create a duplicate in this context */ |
| 3056 | LY_CHECK_RET(lyd_dup_single_to_ctx(target, LYD_CTX(siblings), NULL, 0, &dup)); |
| 3057 | target = dup; |
| 3058 | } |
| 3059 | |
| 3060 | if ((siblings->schema && target->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) { |
| 3061 | /* schema mismatch */ |
| 3062 | lyd_free_tree(dup); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 3063 | if (match) { |
| 3064 | *match = NULL; |
| 3065 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3066 | return LY_ENOTFOUND; |
| 3067 | } |
| 3068 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 3069 | /* get first sibling */ |
| 3070 | siblings = lyd_first_sibling(siblings); |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3071 | |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 3072 | parent = siblings->parent; |
Michal Vasko | 3931115 | 2023-08-07 11:03:41 +0200 | [diff] [blame] | 3073 | if (target->schema && parent && parent->schema && parent->children_ht) { |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3074 | assert(target->hash); |
| 3075 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 3076 | if (lysc_is_dup_inst_list(target->schema)) { |
| 3077 | /* we must search the instances from beginning to find the first matching one */ |
| 3078 | found = 0; |
| 3079 | LYD_LIST_FOR_INST(siblings, target->schema, iter) { |
Michal Vasko | ee9b948 | 2023-06-19 13:17:48 +0200 | [diff] [blame] | 3080 | if (!lyd_compare_single(target, iter, LYD_COMPARE_FULL_RECURSION)) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 3081 | found = 1; |
| 3082 | break; |
| 3083 | } |
| 3084 | } |
| 3085 | if (found) { |
| 3086 | siblings = iter; |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 3087 | } else { |
| 3088 | siblings = NULL; |
| 3089 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3090 | } else { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 3091 | /* find by hash */ |
| 3092 | if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) { |
| 3093 | siblings = *match_p; |
| 3094 | } else { |
| 3095 | /* not found */ |
| 3096 | siblings = NULL; |
| 3097 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3098 | } |
| 3099 | } else { |
Michal Vasko | 3931115 | 2023-08-07 11:03:41 +0200 | [diff] [blame] | 3100 | /* no children hash table or cannot be used */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 3101 | for ( ; siblings; siblings = siblings->next) { |
Michal Vasko | ee9b948 | 2023-06-19 13:17:48 +0200 | [diff] [blame] | 3102 | if (lysc_is_dup_inst_list(target->schema)) { |
| 3103 | if (!lyd_compare_single(siblings, target, LYD_COMPARE_FULL_RECURSION)) { |
| 3104 | break; |
| 3105 | } |
| 3106 | } else { |
Michal Vasko | d8a5201 | 2023-08-15 11:38:10 +0200 | [diff] [blame] | 3107 | if (!lyd_compare_single(siblings, target, 0)) { |
Michal Vasko | ee9b948 | 2023-06-19 13:17:48 +0200 | [diff] [blame] | 3108 | break; |
| 3109 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3110 | } |
| 3111 | } |
| 3112 | } |
| 3113 | |
Michal Vasko | 9beceb8 | 2022-04-05 12:14:15 +0200 | [diff] [blame] | 3114 | lyd_free_tree(dup); |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3115 | if (!siblings) { |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 3116 | if (match) { |
| 3117 | *match = NULL; |
| 3118 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3119 | return LY_ENOTFOUND; |
| 3120 | } |
| 3121 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 3122 | if (match) { |
| 3123 | *match = (struct lyd_node *)siblings; |
| 3124 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3125 | return LY_SUCCESS; |
| 3126 | } |
| 3127 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 3128 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3129 | lyd_find_sibling_val(const struct lyd_node *siblings, const struct lysc_node *schema, const char *key_or_value, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3130 | size_t val_len, struct lyd_node **match) |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3131 | { |
| 3132 | LY_ERR rc; |
| 3133 | struct lyd_node *target = NULL; |
Michal Vasko | 9beceb8 | 2022-04-05 12:14:15 +0200 | [diff] [blame] | 3134 | const struct lyd_node *parent; |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3135 | |
Michal Vasko | 4c583e8 | 2020-07-17 12:16:14 +0200 | [diff] [blame] | 3136 | LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL); |
Michal Vasko | 9beceb8 | 2022-04-05 12:14:15 +0200 | [diff] [blame] | 3137 | if (!siblings) { |
| 3138 | /* no data */ |
| 3139 | if (match) { |
| 3140 | *match = NULL; |
| 3141 | } |
| 3142 | return LY_ENOTFOUND; |
| 3143 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3144 | |
Michal Vasko | 9beceb8 | 2022-04-05 12:14:15 +0200 | [diff] [blame] | 3145 | if ((LYD_CTX(siblings) != schema->module->ctx)) { |
| 3146 | /* parent of ext nodes is useless */ |
| 3147 | parent = (siblings->flags & LYD_EXT) ? NULL : lyd_parent(siblings); |
Michal Vasko | af3df49 | 2022-12-02 14:03:52 +0100 | [diff] [blame] | 3148 | if (lyd_find_schema_ctx(schema, LYD_CTX(siblings), parent, 0, &schema)) { |
| 3149 | /* no schema node in siblings so certainly no data node either */ |
| 3150 | if (match) { |
| 3151 | *match = NULL; |
| 3152 | } |
| 3153 | return LY_ENOTFOUND; |
| 3154 | } |
Michal Vasko | 9beceb8 | 2022-04-05 12:14:15 +0200 | [diff] [blame] | 3155 | } |
| 3156 | |
| 3157 | if (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) { |
| 3158 | /* schema mismatch */ |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 3159 | if (match) { |
| 3160 | *match = NULL; |
| 3161 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3162 | return LY_ENOTFOUND; |
| 3163 | } |
| 3164 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 3165 | if (key_or_value && !val_len) { |
| 3166 | val_len = strlen(key_or_value); |
| 3167 | } |
| 3168 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 3169 | if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) { |
| 3170 | /* create a data node and find the instance */ |
| 3171 | if (schema->nodetype == LYS_LEAFLIST) { |
| 3172 | /* target used attributes: schema, hash, value */ |
steweg | d4cde64 | 2024-02-21 08:34:16 +0100 | [diff] [blame] | 3173 | rc = lyd_create_term(schema, key_or_value, val_len, 0, 1, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, NULL, &target); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 3174 | LY_CHECK_RET(rc); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 3175 | } else { |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 3176 | /* target used attributes: schema, hash, child (all keys) */ |
steweg | d4cde64 | 2024-02-21 08:34:16 +0100 | [diff] [blame] | 3177 | LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, 1, &target)); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 3178 | } |
| 3179 | |
| 3180 | /* find it */ |
| 3181 | rc = lyd_find_sibling_first(siblings, target, match); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 3182 | } else { |
| 3183 | /* find the first schema node instance */ |
| 3184 | rc = lyd_find_sibling_schema(siblings, schema, match); |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3185 | } |
| 3186 | |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3187 | lyd_free_tree(target); |
| 3188 | return rc; |
| 3189 | } |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 3190 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 3191 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 3192 | lyd_find_sibling_dup_inst_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set) |
| 3193 | { |
| 3194 | struct lyd_node **match_p, *first, *iter; |
| 3195 | struct lyd_node_inner *parent; |
Michal Vasko | ee9b948 | 2023-06-19 13:17:48 +0200 | [diff] [blame] | 3196 | uint32_t comp_opts; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 3197 | |
Michal Vasko | 83ae777 | 2022-06-08 10:01:55 +0200 | [diff] [blame] | 3198 | LY_CHECK_ARG_RET(NULL, target, set, LY_EINVAL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 3199 | LY_CHECK_CTX_EQUAL_RET(siblings ? LYD_CTX(siblings) : NULL, LYD_CTX(target), LY_EINVAL); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 3200 | |
| 3201 | LY_CHECK_RET(ly_set_new(set)); |
| 3202 | |
| 3203 | if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) { |
| 3204 | /* no data or schema mismatch */ |
| 3205 | return LY_ENOTFOUND; |
| 3206 | } |
| 3207 | |
Michal Vasko | ee9b948 | 2023-06-19 13:17:48 +0200 | [diff] [blame] | 3208 | /* set options */ |
Michal Vasko | d8a5201 | 2023-08-15 11:38:10 +0200 | [diff] [blame] | 3209 | comp_opts = (lysc_is_dup_inst_list(target->schema) ? LYD_COMPARE_FULL_RECURSION : 0); |
Michal Vasko | ee9b948 | 2023-06-19 13:17:48 +0200 | [diff] [blame] | 3210 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 3211 | /* get first sibling */ |
| 3212 | siblings = lyd_first_sibling(siblings); |
| 3213 | |
| 3214 | parent = siblings->parent; |
| 3215 | if (parent && parent->schema && parent->children_ht) { |
| 3216 | assert(target->hash); |
| 3217 | |
| 3218 | /* find the first instance */ |
| 3219 | lyd_find_sibling_first(siblings, target, &first); |
| 3220 | if (first) { |
| 3221 | /* add it so that it is the first in the set */ |
| 3222 | if (ly_set_add(*set, first, 1, NULL)) { |
| 3223 | goto error; |
| 3224 | } |
| 3225 | |
| 3226 | /* find by hash */ |
| 3227 | if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) { |
| 3228 | iter = *match_p; |
| 3229 | } else { |
| 3230 | /* not found */ |
| 3231 | iter = NULL; |
| 3232 | } |
| 3233 | while (iter) { |
| 3234 | /* add all found nodes into the set */ |
Michal Vasko | ee9b948 | 2023-06-19 13:17:48 +0200 | [diff] [blame] | 3235 | if ((iter != first) && !lyd_compare_single(iter, target, comp_opts) && ly_set_add(*set, iter, 1, NULL)) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 3236 | goto error; |
| 3237 | } |
| 3238 | |
| 3239 | /* find next instance */ |
| 3240 | if (lyht_find_next(parent->children_ht, &iter, iter->hash, (void **)&match_p)) { |
| 3241 | iter = NULL; |
| 3242 | } else { |
| 3243 | iter = *match_p; |
| 3244 | } |
| 3245 | } |
| 3246 | } |
| 3247 | } else { |
| 3248 | /* no children hash table */ |
| 3249 | LY_LIST_FOR(siblings, siblings) { |
Michal Vasko | ee9b948 | 2023-06-19 13:17:48 +0200 | [diff] [blame] | 3250 | if (!lyd_compare_single(target, siblings, comp_opts)) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 3251 | ly_set_add(*set, (void *)siblings, 1, NULL); |
| 3252 | } |
| 3253 | } |
| 3254 | } |
| 3255 | |
| 3256 | if (!(*set)->count) { |
| 3257 | return LY_ENOTFOUND; |
| 3258 | } |
| 3259 | return LY_SUCCESS; |
| 3260 | |
| 3261 | error: |
| 3262 | ly_set_free(*set, NULL); |
| 3263 | *set = NULL; |
| 3264 | return LY_EMEM; |
| 3265 | } |
| 3266 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 3267 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 1d4af6c | 2021-02-22 13:31:26 +0100 | [diff] [blame] | 3268 | lyd_find_sibling_opaq_next(const struct lyd_node *first, const char *name, struct lyd_node **match) |
| 3269 | { |
| 3270 | LY_CHECK_ARG_RET(NULL, name, LY_EINVAL); |
| 3271 | |
Michal Vasko | e271a31 | 2023-08-15 11:46:30 +0200 | [diff] [blame] | 3272 | if (first && first->schema) { |
| 3273 | first = first->prev; |
| 3274 | if (first->schema) { |
| 3275 | /* no opaque nodes */ |
| 3276 | first = NULL; |
| 3277 | } else { |
| 3278 | /* opaque nodes are at the end, find quickly the first */ |
| 3279 | while (!first->prev->schema) { |
| 3280 | first = first->prev; |
| 3281 | } |
| 3282 | } |
| 3283 | } |
| 3284 | |
Michal Vasko | 1d4af6c | 2021-02-22 13:31:26 +0100 | [diff] [blame] | 3285 | for ( ; first; first = first->next) { |
Michal Vasko | e271a31 | 2023-08-15 11:46:30 +0200 | [diff] [blame] | 3286 | assert(!first->schema); |
| 3287 | if (!strcmp(LYD_NAME(first), name)) { |
Michal Vasko | 1d4af6c | 2021-02-22 13:31:26 +0100 | [diff] [blame] | 3288 | break; |
| 3289 | } |
| 3290 | } |
| 3291 | |
| 3292 | if (match) { |
| 3293 | *match = (struct lyd_node *)first; |
| 3294 | } |
| 3295 | return first ? LY_SUCCESS : LY_ENOTFOUND; |
| 3296 | } |
| 3297 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 3298 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | d96e237 | 2023-02-24 16:07:51 +0100 | [diff] [blame] | 3299 | lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set) |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 3300 | { |
Michal Vasko | d96e237 | 2023-02-24 16:07:51 +0100 | [diff] [blame] | 3301 | LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL); |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 3302 | |
Michal Vasko | be1b0cb | 2024-01-22 14:32:15 +0100 | [diff] [blame] | 3303 | return lyd_find_xpath3(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, NULL, set); |
Michal Vasko | d96e237 | 2023-02-24 16:07:51 +0100 | [diff] [blame] | 3304 | } |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 3305 | |
Michal Vasko | d96e237 | 2023-02-24 16:07:51 +0100 | [diff] [blame] | 3306 | LIBYANG_API_DEF LY_ERR |
| 3307 | lyd_find_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, struct ly_set **set) |
| 3308 | { |
| 3309 | LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL); |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 3310 | |
Michal Vasko | be1b0cb | 2024-01-22 14:32:15 +0100 | [diff] [blame] | 3311 | return lyd_find_xpath3(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, vars, set); |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 3312 | } |
Radek Krejci | ca98914 | 2020-11-05 11:32:22 +0100 | [diff] [blame] | 3313 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 3314 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | be1b0cb | 2024-01-22 14:32:15 +0100 | [diff] [blame] | 3315 | lyd_find_xpath3(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath, LY_VALUE_FORMAT format, |
Michal Vasko | d96e237 | 2023-02-24 16:07:51 +0100 | [diff] [blame] | 3316 | void *prefix_data, const struct lyxp_var *vars, struct ly_set **set) |
Michal Vasko | e3716b3 | 2021-12-13 16:58:25 +0100 | [diff] [blame] | 3317 | { |
Michal Vasko | d96e237 | 2023-02-24 16:07:51 +0100 | [diff] [blame] | 3318 | LY_CHECK_ARG_RET(NULL, tree, xpath, set, LY_EINVAL); |
Michal Vasko | e3716b3 | 2021-12-13 16:58:25 +0100 | [diff] [blame] | 3319 | |
Michal Vasko | d96e237 | 2023-02-24 16:07:51 +0100 | [diff] [blame] | 3320 | *set = NULL; |
| 3321 | |
| 3322 | return lyd_eval_xpath4(ctx_node, tree, NULL, xpath, format, prefix_data, vars, NULL, set, NULL, NULL, NULL); |
Michal Vasko | e3716b3 | 2021-12-13 16:58:25 +0100 | [diff] [blame] | 3323 | } |
| 3324 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 3325 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | d96e237 | 2023-02-24 16:07:51 +0100 | [diff] [blame] | 3326 | lyd_eval_xpath(const struct lyd_node *ctx_node, const char *xpath, ly_bool *result) |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 3327 | { |
Michal Vasko | d96e237 | 2023-02-24 16:07:51 +0100 | [diff] [blame] | 3328 | return lyd_eval_xpath3(ctx_node, NULL, xpath, LY_VALUE_JSON, NULL, NULL, result); |
Michal Vasko | c9eb3ca | 2021-07-16 10:20:37 +0200 | [diff] [blame] | 3329 | } |
| 3330 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 3331 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 10fabfc | 2022-08-09 08:55:43 +0200 | [diff] [blame] | 3332 | lyd_eval_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, ly_bool *result) |
| 3333 | { |
| 3334 | return lyd_eval_xpath3(ctx_node, NULL, xpath, LY_VALUE_JSON, NULL, vars, result); |
| 3335 | } |
| 3336 | |
| 3337 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | d96e237 | 2023-02-24 16:07:51 +0100 | [diff] [blame] | 3338 | lyd_eval_xpath3(const struct lyd_node *ctx_node, const struct lys_module *cur_mod, const char *xpath, |
| 3339 | LY_VALUE_FORMAT format, void *prefix_data, const struct lyxp_var *vars, ly_bool *result) |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 3340 | { |
Michal Vasko | d96e237 | 2023-02-24 16:07:51 +0100 | [diff] [blame] | 3341 | return lyd_eval_xpath4(ctx_node, ctx_node, cur_mod, xpath, format, prefix_data, vars, NULL, NULL, NULL, NULL, result); |
| 3342 | } |
| 3343 | |
| 3344 | LIBYANG_API_DEF LY_ERR |
| 3345 | lyd_eval_xpath4(const struct lyd_node *ctx_node, const struct lyd_node *tree, const struct lys_module *cur_mod, |
| 3346 | const char *xpath, LY_VALUE_FORMAT format, void *prefix_data, const struct lyxp_var *vars, LY_XPATH_TYPE *ret_type, |
| 3347 | struct ly_set **node_set, char **string, long double *number, ly_bool *boolean) |
| 3348 | { |
| 3349 | LY_ERR ret = LY_SUCCESS; |
| 3350 | struct lyxp_set xp_set = {0}; |
| 3351 | struct lyxp_expr *exp = NULL; |
| 3352 | uint32_t i; |
| 3353 | |
| 3354 | LY_CHECK_ARG_RET(NULL, tree, xpath, ((ret_type && node_set && string && number && boolean) || |
| 3355 | (node_set && !string && !number && !boolean) || (!node_set && string && !number && !boolean) || |
| 3356 | (!node_set && !string && number && !boolean) || (!node_set && !string && !number && boolean)), LY_EINVAL); |
| 3357 | |
| 3358 | /* parse expression */ |
| 3359 | ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(tree), xpath, 0, 1, &exp); |
| 3360 | LY_CHECK_GOTO(ret, cleanup); |
| 3361 | |
| 3362 | /* evaluate expression */ |
| 3363 | ret = lyxp_eval(LYD_CTX(tree), exp, cur_mod, format, prefix_data, ctx_node, ctx_node, tree, vars, &xp_set, |
| 3364 | LYXP_IGNORE_WHEN); |
| 3365 | LY_CHECK_GOTO(ret, cleanup); |
| 3366 | |
| 3367 | /* return expected result type without or with casting */ |
| 3368 | if (node_set) { |
| 3369 | /* node set */ |
| 3370 | if (xp_set.type == LYXP_SET_NODE_SET) { |
| 3371 | /* transform into a set */ |
| 3372 | LY_CHECK_GOTO(ret = ly_set_new(node_set), cleanup); |
| 3373 | (*node_set)->objs = malloc(xp_set.used * sizeof *(*node_set)->objs); |
| 3374 | LY_CHECK_ERR_GOTO(!(*node_set)->objs, LOGMEM(LYD_CTX(tree)); ret = LY_EMEM, cleanup); |
| 3375 | (*node_set)->size = xp_set.used; |
| 3376 | for (i = 0; i < xp_set.used; ++i) { |
| 3377 | if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) { |
| 3378 | ret = ly_set_add(*node_set, xp_set.val.nodes[i].node, 1, NULL); |
| 3379 | LY_CHECK_GOTO(ret, cleanup); |
| 3380 | } |
| 3381 | } |
| 3382 | if (ret_type) { |
| 3383 | *ret_type = LY_XPATH_NODE_SET; |
| 3384 | } |
| 3385 | } else if (!string && !number && !boolean) { |
| 3386 | LOGERR(LYD_CTX(tree), LY_EINVAL, "XPath \"%s\" result is not a node set.", xpath); |
| 3387 | ret = LY_EINVAL; |
| 3388 | goto cleanup; |
| 3389 | } |
| 3390 | } |
| 3391 | |
| 3392 | if (string) { |
| 3393 | if ((xp_set.type != LYXP_SET_STRING) && !node_set) { |
| 3394 | /* cast into string */ |
| 3395 | LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_STRING), cleanup); |
| 3396 | } |
| 3397 | if (xp_set.type == LYXP_SET_STRING) { |
| 3398 | /* string */ |
| 3399 | *string = xp_set.val.str; |
| 3400 | xp_set.val.str = NULL; |
| 3401 | if (ret_type) { |
| 3402 | *ret_type = LY_XPATH_STRING; |
| 3403 | } |
| 3404 | } |
| 3405 | } |
| 3406 | |
| 3407 | if (number) { |
| 3408 | if ((xp_set.type != LYXP_SET_NUMBER) && !node_set) { |
| 3409 | /* cast into number */ |
| 3410 | LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_NUMBER), cleanup); |
| 3411 | } |
| 3412 | if (xp_set.type == LYXP_SET_NUMBER) { |
| 3413 | /* number */ |
| 3414 | *number = xp_set.val.num; |
| 3415 | if (ret_type) { |
| 3416 | *ret_type = LY_XPATH_NUMBER; |
| 3417 | } |
| 3418 | } |
| 3419 | } |
| 3420 | |
| 3421 | if (boolean) { |
| 3422 | if ((xp_set.type != LYXP_SET_BOOLEAN) && !node_set) { |
| 3423 | /* cast into boolean */ |
| 3424 | LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN), cleanup); |
| 3425 | } |
| 3426 | if (xp_set.type == LYXP_SET_BOOLEAN) { |
| 3427 | /* boolean */ |
| 3428 | *boolean = xp_set.val.bln; |
| 3429 | if (ret_type) { |
| 3430 | *ret_type = LY_XPATH_BOOLEAN; |
| 3431 | } |
| 3432 | } |
| 3433 | } |
| 3434 | |
| 3435 | cleanup: |
| 3436 | lyxp_set_free_content(&xp_set); |
| 3437 | lyxp_expr_free((struct ly_ctx *)LYD_CTX(tree), exp); |
| 3438 | return ret; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 3439 | } |
| 3440 | |
Michal Vasko | 99a7788 | 2024-01-04 14:50:51 +0100 | [diff] [blame] | 3441 | /** |
| 3442 | * @brief Hash table node equal callback. |
| 3443 | */ |
| 3444 | static ly_bool |
| 3445 | lyd_trim_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data)) |
| 3446 | { |
| 3447 | struct lyd_node *node1, *node2; |
| 3448 | |
| 3449 | node1 = *(struct lyd_node **)val1_p; |
| 3450 | node2 = *(struct lyd_node **)val2_p; |
| 3451 | |
| 3452 | return node1 == node2; |
| 3453 | } |
| 3454 | |
| 3455 | LIBYANG_API_DEF LY_ERR |
| 3456 | lyd_trim_xpath(struct lyd_node **tree, const char *xpath, const struct lyxp_var *vars) |
| 3457 | { |
| 3458 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | ea4943c | 2024-01-16 15:27:29 +0100 | [diff] [blame] | 3459 | struct ly_ctx *ctx = NULL; |
Michal Vasko | 99a7788 | 2024-01-04 14:50:51 +0100 | [diff] [blame] | 3460 | struct lyxp_set xp_set = {0}; |
| 3461 | struct lyxp_expr *exp = NULL; |
| 3462 | struct lyd_node *node, *parent; |
| 3463 | struct lyxp_set_hash_node hnode; |
| 3464 | struct ly_ht *parent_ht = NULL; |
| 3465 | struct ly_set free_set = {0}; |
| 3466 | uint32_t i, hash; |
| 3467 | ly_bool is_result; |
| 3468 | |
| 3469 | LY_CHECK_ARG_RET(NULL, tree, xpath, LY_EINVAL); |
| 3470 | |
| 3471 | if (!*tree) { |
| 3472 | /* nothing to do */ |
| 3473 | goto cleanup; |
| 3474 | } |
| 3475 | |
| 3476 | *tree = lyd_first_sibling(*tree); |
| 3477 | ctx = (struct ly_ctx *)LYD_CTX(*tree); |
| 3478 | |
| 3479 | /* parse expression */ |
| 3480 | ret = lyxp_expr_parse(ctx, xpath, 0, 1, &exp); |
| 3481 | LY_CHECK_GOTO(ret, cleanup); |
| 3482 | |
| 3483 | /* evaluate expression */ |
| 3484 | ret = lyxp_eval(ctx, exp, NULL, LY_VALUE_JSON, NULL, *tree, *tree, *tree, vars, &xp_set, LYXP_IGNORE_WHEN); |
| 3485 | LY_CHECK_GOTO(ret, cleanup); |
| 3486 | |
| 3487 | /* create hash table for all the parents of results */ |
Michal Vasko | 0a0716d | 2024-01-04 15:02:12 +0100 | [diff] [blame] | 3488 | parent_ht = lyht_new(32, sizeof node, lyd_trim_equal_cb, NULL, 1); |
Michal Vasko | 99a7788 | 2024-01-04 14:50:51 +0100 | [diff] [blame] | 3489 | LY_CHECK_GOTO(!parent_ht, cleanup); |
| 3490 | |
| 3491 | for (i = 0; i < xp_set.used; ++i) { |
| 3492 | if (xp_set.val.nodes[i].type != LYXP_NODE_ELEM) { |
| 3493 | /* ignore */ |
| 3494 | continue; |
| 3495 | } |
| 3496 | |
| 3497 | for (parent = lyd_parent(xp_set.val.nodes[i].node); parent; parent = lyd_parent(parent)) { |
| 3498 | /* add the parent into parent_ht */ |
| 3499 | ret = lyht_insert(parent_ht, &parent, parent->hash, NULL); |
| 3500 | if (ret == LY_EEXIST) { |
| 3501 | /* shared parent, we are done */ |
| 3502 | break; |
| 3503 | } |
| 3504 | LY_CHECK_GOTO(ret, cleanup); |
| 3505 | } |
| 3506 | } |
| 3507 | |
| 3508 | hnode.type = LYXP_NODE_ELEM; |
| 3509 | LY_LIST_FOR(*tree, parent) { |
| 3510 | LYD_TREE_DFS_BEGIN(parent, node) { |
| 3511 | if (lysc_is_key(node->schema)) { |
| 3512 | /* ignore */ |
| 3513 | goto next_iter; |
| 3514 | } |
| 3515 | |
| 3516 | /* check the results */ |
| 3517 | is_result = 0; |
| 3518 | if (xp_set.ht) { |
| 3519 | hnode.node = node; |
| 3520 | hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 3521 | hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 3522 | hash = lyht_hash_multi(hash, NULL, 0); |
| 3523 | |
| 3524 | if (!lyht_find(xp_set.ht, &hnode, hash, NULL)) { |
| 3525 | is_result = 1; |
| 3526 | } |
| 3527 | } else { |
| 3528 | /* not enough elements for a hash table */ |
| 3529 | for (i = 0; i < xp_set.used; ++i) { |
| 3530 | if (xp_set.val.nodes[i].type != LYXP_NODE_ELEM) { |
| 3531 | /* ignore */ |
| 3532 | continue; |
| 3533 | } |
| 3534 | |
| 3535 | if (xp_set.val.nodes[i].node == node) { |
| 3536 | is_result = 1; |
| 3537 | break; |
| 3538 | } |
| 3539 | } |
| 3540 | } |
| 3541 | |
| 3542 | if (is_result) { |
| 3543 | /* keep the whole subtree if the node is in the results */ |
| 3544 | LYD_TREE_DFS_continue = 1; |
| 3545 | } else if (lyht_find(parent_ht, &node, node->hash, NULL)) { |
| 3546 | /* free the whole subtree if the node is not even among the selected parents */ |
| 3547 | ret = ly_set_add(&free_set, node, 1, NULL); |
| 3548 | LY_CHECK_GOTO(ret, cleanup); |
| 3549 | LYD_TREE_DFS_continue = 1; |
| 3550 | } /* else keep the parent node because a subtree is in the results */ |
| 3551 | |
| 3552 | next_iter: |
| 3553 | LYD_TREE_DFS_END(parent, node); |
| 3554 | } |
| 3555 | } |
| 3556 | |
| 3557 | /* free */ |
| 3558 | for (i = 0; i < free_set.count; ++i) { |
| 3559 | node = free_set.dnodes[i]; |
| 3560 | if (*tree == node) { |
| 3561 | *tree = (*tree)->next; |
| 3562 | } |
| 3563 | lyd_free_tree(node); |
| 3564 | } |
| 3565 | |
| 3566 | cleanup: |
| 3567 | lyxp_set_free_content(&xp_set); |
| 3568 | lyxp_expr_free(ctx, exp); |
| 3569 | lyht_free(parent_ht, NULL); |
| 3570 | ly_set_erase(&free_set, NULL); |
| 3571 | return ret; |
| 3572 | } |
| 3573 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 3574 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 3e1f655 | 2021-01-14 09:27:55 +0100 | [diff] [blame] | 3575 | lyd_find_path(const struct lyd_node *ctx_node, const char *path, ly_bool output, struct lyd_node **match) |
| 3576 | { |
| 3577 | LY_ERR ret = LY_SUCCESS; |
| 3578 | struct lyxp_expr *expr = NULL; |
| 3579 | struct ly_path *lypath = NULL; |
| 3580 | |
| 3581 | LY_CHECK_ARG_RET(NULL, ctx_node, ctx_node->schema, path, LY_EINVAL); |
| 3582 | |
| 3583 | /* parse the path */ |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 3584 | ret = ly_path_parse(LYD_CTX(ctx_node), ctx_node->schema, path, strlen(path), 0, LY_PATH_BEGIN_EITHER, |
Michal Vasko | 32ca49b | 2023-02-17 15:11:35 +0100 | [diff] [blame] | 3585 | LY_PATH_PREFIX_FIRST, LY_PATH_PRED_SIMPLE, &expr); |
Michal Vasko | 3e1f655 | 2021-01-14 09:27:55 +0100 | [diff] [blame] | 3586 | LY_CHECK_GOTO(ret, cleanup); |
| 3587 | |
| 3588 | /* compile the path */ |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 3589 | ret = ly_path_compile(LYD_CTX(ctx_node), NULL, ctx_node->schema, NULL, expr, |
Michal Vasko | 0884d21 | 2021-10-14 09:21:46 +0200 | [diff] [blame] | 3590 | output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_SINGLE, 0, LY_VALUE_JSON, NULL, &lypath); |
Michal Vasko | 3e1f655 | 2021-01-14 09:27:55 +0100 | [diff] [blame] | 3591 | LY_CHECK_GOTO(ret, cleanup); |
| 3592 | |
| 3593 | /* evaluate the path */ |
Michal Vasko | 838829d | 2023-10-09 16:06:43 +0200 | [diff] [blame] | 3594 | ret = ly_path_eval_partial(lypath, ctx_node, NULL, 0, NULL, match); |
Michal Vasko | 3e1f655 | 2021-01-14 09:27:55 +0100 | [diff] [blame] | 3595 | |
| 3596 | cleanup: |
| 3597 | lyxp_expr_free(LYD_CTX(ctx_node), expr); |
Michal Vasko | 68b9634 | 2024-09-09 15:24:18 +0200 | [diff] [blame] | 3598 | ly_path_free(lypath); |
Michal Vasko | 3e1f655 | 2021-01-14 09:27:55 +0100 | [diff] [blame] | 3599 | return ret; |
| 3600 | } |
| 3601 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 3602 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | bb22b18 | 2021-06-14 08:14:21 +0200 | [diff] [blame] | 3603 | lyd_find_target(const struct ly_path *path, const struct lyd_node *tree, struct lyd_node **match) |
| 3604 | { |
| 3605 | LY_ERR ret; |
| 3606 | struct lyd_node *m; |
| 3607 | |
| 3608 | LY_CHECK_ARG_RET(NULL, path, LY_EINVAL); |
| 3609 | |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 3610 | ret = ly_path_eval(path, tree, NULL, &m); |
Michal Vasko | bb22b18 | 2021-06-14 08:14:21 +0200 | [diff] [blame] | 3611 | if (ret) { |
| 3612 | if (match) { |
| 3613 | *match = NULL; |
| 3614 | } |
| 3615 | return LY_ENOTFOUND; |
| 3616 | } |
| 3617 | |
| 3618 | if (match) { |
| 3619 | *match = m; |
| 3620 | } |
| 3621 | return LY_SUCCESS; |
| 3622 | } |
Irfan | d3b351a | 2023-09-14 14:52:15 +0200 | [diff] [blame] | 3623 | |
steweg | f9041a2 | 2024-01-18 13:29:12 +0100 | [diff] [blame] | 3624 | LY_ERR |
| 3625 | lyd_get_or_create_leafref_links_record(const struct lyd_node_term *node, struct lyd_leafref_links_rec **record, ly_bool create) |
| 3626 | { |
| 3627 | struct ly_ht *ht; |
| 3628 | uint32_t hash; |
Michal Vasko | b46061d | 2024-01-18 13:58:13 +0100 | [diff] [blame] | 3629 | struct lyd_leafref_links_rec rec = {0}; |
steweg | f9041a2 | 2024-01-18 13:29:12 +0100 | [diff] [blame] | 3630 | |
| 3631 | assert(node); |
| 3632 | assert(record); |
| 3633 | |
Michal Vasko | b46061d | 2024-01-18 13:58:13 +0100 | [diff] [blame] | 3634 | *record = NULL; |
| 3635 | |
steweg | f9041a2 | 2024-01-18 13:29:12 +0100 | [diff] [blame] | 3636 | if (!(ly_ctx_get_options(LYD_CTX(node)) & LY_CTX_LEAFREF_LINKING)) { |
steweg | f9041a2 | 2024-01-18 13:29:12 +0100 | [diff] [blame] | 3637 | return LY_EDENIED; |
| 3638 | } |
| 3639 | |
| 3640 | rec.node = node; |
steweg | f9041a2 | 2024-01-18 13:29:12 +0100 | [diff] [blame] | 3641 | ht = LYD_CTX(node)->leafref_links_ht; |
Michal Vasko | 67bf587 | 2024-01-18 13:58:25 +0100 | [diff] [blame] | 3642 | hash = lyht_hash((const char *)&node, sizeof node); |
| 3643 | |
steweg | f9041a2 | 2024-01-18 13:29:12 +0100 | [diff] [blame] | 3644 | if (lyht_find(ht, &rec, hash, (void **)record) == LY_ENOTFOUND) { |
| 3645 | if (create) { |
| 3646 | LY_CHECK_RET(lyht_insert_no_check(ht, &rec, hash, (void **)record)); |
| 3647 | } else { |
steweg | f9041a2 | 2024-01-18 13:29:12 +0100 | [diff] [blame] | 3648 | return LY_ENOTFOUND; |
| 3649 | } |
| 3650 | } |
| 3651 | |
| 3652 | return LY_SUCCESS; |
| 3653 | } |
| 3654 | |
| 3655 | LIBYANG_API_DEF LY_ERR |
| 3656 | lyd_leafref_get_links(const struct lyd_node_term *node, const struct lyd_leafref_links_rec **record) |
| 3657 | { |
| 3658 | LY_CHECK_ARG_RET(NULL, node, record, LY_EINVAL); |
| 3659 | |
| 3660 | return lyd_get_or_create_leafref_links_record(node, (struct lyd_leafref_links_rec **)record, 0); |
| 3661 | } |
| 3662 | |
| 3663 | LY_ERR |
| 3664 | lyd_link_leafref_node(const struct lyd_node_term *node, const struct lyd_node_term *leafref_node) |
| 3665 | { |
steweg | f9041a2 | 2024-01-18 13:29:12 +0100 | [diff] [blame] | 3666 | const struct lyd_node_term **item = NULL; |
| 3667 | struct lyd_leafref_links_rec *rec; |
steweg | 6738895 | 2024-01-25 12:14:50 +0100 | [diff] [blame] | 3668 | LY_ARRAY_COUNT_TYPE u; |
steweg | f9041a2 | 2024-01-18 13:29:12 +0100 | [diff] [blame] | 3669 | |
| 3670 | assert(node); |
| 3671 | assert(leafref_node); |
| 3672 | |
| 3673 | if (!(ly_ctx_get_options(LYD_CTX(node)) & LY_CTX_LEAFREF_LINKING)) { |
| 3674 | return LY_EDENIED; |
| 3675 | } |
| 3676 | |
steweg | 6738895 | 2024-01-25 12:14:50 +0100 | [diff] [blame] | 3677 | /* add leafref node into the list of target node */ |
steweg | f9041a2 | 2024-01-18 13:29:12 +0100 | [diff] [blame] | 3678 | LY_CHECK_RET(lyd_get_or_create_leafref_links_record(node, &rec, 1)); |
| 3679 | LY_ARRAY_FOR(rec->leafref_nodes, u) { |
| 3680 | if (rec->leafref_nodes[u] == leafref_node) { |
| 3681 | return LY_SUCCESS; |
| 3682 | } |
| 3683 | } |
| 3684 | |
| 3685 | LY_ARRAY_NEW_RET(LYD_CTX(node), rec->leafref_nodes, item, LY_EMEM); |
| 3686 | *item = leafref_node; |
steweg | 6738895 | 2024-01-25 12:14:50 +0100 | [diff] [blame] | 3687 | |
| 3688 | /* add target node into the list of leafref node*/ |
steweg | f9041a2 | 2024-01-18 13:29:12 +0100 | [diff] [blame] | 3689 | LY_CHECK_RET(lyd_get_or_create_leafref_links_record(leafref_node, &rec, 1)); |
steweg | 6738895 | 2024-01-25 12:14:50 +0100 | [diff] [blame] | 3690 | LY_ARRAY_FOR(rec->target_nodes, u) { |
| 3691 | if (rec->target_nodes[u] == node) { |
| 3692 | return LY_SUCCESS; |
| 3693 | } |
| 3694 | } |
| 3695 | |
| 3696 | LY_ARRAY_NEW_RET(LYD_CTX(node), rec->target_nodes, item, LY_EMEM); |
| 3697 | *item = node; |
| 3698 | |
steweg | f9041a2 | 2024-01-18 13:29:12 +0100 | [diff] [blame] | 3699 | return LY_SUCCESS; |
| 3700 | } |
| 3701 | |
| 3702 | LIBYANG_API_DEF LY_ERR |
| 3703 | lyd_leafref_link_node_tree(const struct lyd_node *tree) |
| 3704 | { |
| 3705 | const struct lyd_node *sibling, *elem; |
steweg | 6738895 | 2024-01-25 12:14:50 +0100 | [diff] [blame] | 3706 | struct ly_set *targets = NULL; |
steweg | f9041a2 | 2024-01-18 13:29:12 +0100 | [diff] [blame] | 3707 | char *errmsg; |
| 3708 | struct lyd_node_term *leafref_node; |
| 3709 | struct lysc_node_leaf *leaf_schema; |
| 3710 | struct lysc_type_leafref *lref; |
steweg | 6738895 | 2024-01-25 12:14:50 +0100 | [diff] [blame] | 3711 | LY_ERR ret = LY_SUCCESS; |
| 3712 | uint32_t i; |
steweg | f9041a2 | 2024-01-18 13:29:12 +0100 | [diff] [blame] | 3713 | |
| 3714 | LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL); |
| 3715 | |
| 3716 | if (!(ly_ctx_get_options(LYD_CTX(tree)) & LY_CTX_LEAFREF_LINKING)) { |
| 3717 | return LY_EDENIED; |
| 3718 | } |
| 3719 | |
| 3720 | LY_LIST_FOR(tree, sibling) { |
| 3721 | LYD_TREE_DFS_BEGIN(sibling, elem) { |
steweg | 0e1e509 | 2024-02-12 09:06:04 +0100 | [diff] [blame] | 3722 | if (elem->schema && (elem->schema->nodetype & LYD_NODE_TERM)) { |
steweg | f9041a2 | 2024-01-18 13:29:12 +0100 | [diff] [blame] | 3723 | leafref_node = (struct lyd_node_term *)elem; |
| 3724 | leaf_schema = (struct lysc_node_leaf *)elem->schema; |
Michal Vasko | b46061d | 2024-01-18 13:58:13 +0100 | [diff] [blame] | 3725 | |
steweg | f9041a2 | 2024-01-18 13:29:12 +0100 | [diff] [blame] | 3726 | if (leaf_schema->type->basetype == LY_TYPE_LEAFREF) { |
| 3727 | lref = (struct lysc_type_leafref *)leaf_schema->type; |
steweg | 6738895 | 2024-01-25 12:14:50 +0100 | [diff] [blame] | 3728 | ly_set_free(targets, NULL); |
| 3729 | if (lyplg_type_resolve_leafref(lref, elem, &leafref_node->value, tree, &targets, &errmsg)) { |
Michal Vasko | b46061d | 2024-01-18 13:58:13 +0100 | [diff] [blame] | 3730 | /* leafref target not found */ |
steweg | f9041a2 | 2024-01-18 13:29:12 +0100 | [diff] [blame] | 3731 | free(errmsg); |
Michal Vasko | b46061d | 2024-01-18 13:58:13 +0100 | [diff] [blame] | 3732 | } else { |
| 3733 | /* leafref target found, link it */ |
steweg | 6738895 | 2024-01-25 12:14:50 +0100 | [diff] [blame] | 3734 | for (i = 0; i < targets->count; ++i) { |
| 3735 | if (targets->dnodes[i]->schema->nodetype & LYD_NODE_TERM) { |
| 3736 | ret = lyd_link_leafref_node((struct lyd_node_term *)targets->dnodes[i], leafref_node); |
| 3737 | LY_CHECK_GOTO(ret, cleanup); |
| 3738 | } |
| 3739 | } |
steweg | f9041a2 | 2024-01-18 13:29:12 +0100 | [diff] [blame] | 3740 | } |
| 3741 | } |
| 3742 | } |
| 3743 | LYD_TREE_DFS_END(sibling, elem); |
| 3744 | } |
| 3745 | } |
Michal Vasko | b46061d | 2024-01-18 13:58:13 +0100 | [diff] [blame] | 3746 | |
steweg | 6738895 | 2024-01-25 12:14:50 +0100 | [diff] [blame] | 3747 | cleanup: |
| 3748 | ly_set_free(targets, NULL); |
| 3749 | return ret; |
steweg | f9041a2 | 2024-01-18 13:29:12 +0100 | [diff] [blame] | 3750 | } |
| 3751 | |
| 3752 | LY_ERR |
| 3753 | lyd_unlink_leafref_node(const struct lyd_node_term *node, const struct lyd_node_term *leafref_node) |
| 3754 | { |
| 3755 | LY_ERR ret; |
| 3756 | struct lyd_leafref_links_rec *rec; |
| 3757 | |
| 3758 | assert(node); |
| 3759 | assert(leafref_node); |
| 3760 | |
| 3761 | if (!(ly_ctx_get_options(LYD_CTX(node)) & LY_CTX_LEAFREF_LINKING)) { |
| 3762 | return LY_EDENIED; |
| 3763 | } |
| 3764 | |
steweg | 6738895 | 2024-01-25 12:14:50 +0100 | [diff] [blame] | 3765 | /* remove link from target node to leafref node */ |
steweg | f9041a2 | 2024-01-18 13:29:12 +0100 | [diff] [blame] | 3766 | ret = lyd_get_or_create_leafref_links_record(node, &rec, 0); |
| 3767 | if (ret == LY_SUCCESS) { |
| 3768 | LY_ARRAY_REMOVE_VALUE(rec->leafref_nodes, leafref_node); |
steweg | 6738895 | 2024-01-25 12:14:50 +0100 | [diff] [blame] | 3769 | if ((LY_ARRAY_COUNT(rec->leafref_nodes) == 0) && (LY_ARRAY_COUNT(rec->target_nodes) == 0)) { |
steweg | f9041a2 | 2024-01-18 13:29:12 +0100 | [diff] [blame] | 3770 | lyd_free_leafref_nodes(node); |
| 3771 | } |
| 3772 | } else if (ret != LY_ENOTFOUND) { |
| 3773 | return ret; |
| 3774 | } |
| 3775 | |
steweg | 6738895 | 2024-01-25 12:14:50 +0100 | [diff] [blame] | 3776 | /* remove link from leafref node to target node */ |
steweg | f9041a2 | 2024-01-18 13:29:12 +0100 | [diff] [blame] | 3777 | ret = lyd_get_or_create_leafref_links_record(leafref_node, &rec, 0); |
| 3778 | if (ret == LY_SUCCESS) { |
steweg | 6738895 | 2024-01-25 12:14:50 +0100 | [diff] [blame] | 3779 | LY_ARRAY_REMOVE_VALUE(rec->target_nodes, node); |
| 3780 | if ((LY_ARRAY_COUNT(rec->leafref_nodes) == 0) && (LY_ARRAY_COUNT(rec->target_nodes) == 0)) { |
steweg | f9041a2 | 2024-01-18 13:29:12 +0100 | [diff] [blame] | 3781 | lyd_free_leafref_nodes(leafref_node); |
| 3782 | } |
| 3783 | } else if (ret != LY_ENOTFOUND) { |
| 3784 | return ret; |
| 3785 | } |
| 3786 | |
| 3787 | return LY_SUCCESS; |
| 3788 | } |