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