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