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