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