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