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