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