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