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