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