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