Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file tree_schema.c |
| 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 | |
| 15 | #include "common.h" |
| 16 | |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 17 | #include <assert.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 18 | #include <ctype.h> |
| 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <stdarg.h> |
| 22 | #include <stdint.h> |
| 23 | #include <string.h> |
| 24 | #include <unistd.h> |
| 25 | |
| 26 | #include "log.h" |
| 27 | #include "tree.h" |
| 28 | #include "tree_data.h" |
| 29 | #include "tree_data_internal.h" |
| 30 | #include "tree_schema.h" |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 31 | #include "plugins_exts_metadata.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 32 | |
Radek Krejci | 576b23f | 2019-07-12 14:06:32 +0200 | [diff] [blame] | 33 | API void |
| 34 | lyd_trees_free(const struct lyd_node **trees, int free_data) |
| 35 | { |
| 36 | if (!trees) { |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | if (free_data) { |
| 41 | unsigned int u; |
| 42 | LY_ARRAY_FOR(trees, u) { |
| 43 | lyd_free_all((struct lyd_node *)trees[u]); |
| 44 | } |
| 45 | } |
| 46 | LY_ARRAY_FREE(trees); |
| 47 | } |
| 48 | |
| 49 | static const struct lyd_node * |
| 50 | lyd_trees_getstart(const struct lyd_node *tree) |
| 51 | { |
| 52 | if (!tree) { |
| 53 | return NULL; |
| 54 | } |
| 55 | while (tree->prev->next) { |
| 56 | tree = tree->prev; |
| 57 | } |
| 58 | return tree; |
| 59 | } |
| 60 | |
| 61 | API const struct lyd_node ** |
| 62 | lyd_trees_new(size_t count, const struct lyd_node *tree, ...) |
| 63 | { |
| 64 | LY_ERR ret; |
| 65 | const struct lyd_node **trees = NULL; |
| 66 | va_list ap; |
| 67 | |
| 68 | LY_CHECK_ARG_RET(NULL, tree, count > 0, NULL); |
| 69 | |
| 70 | va_start(ap, tree); |
| 71 | |
| 72 | LY_ARRAY_CREATE_GOTO(tree->schema->module->ctx, trees, count, ret, error); |
| 73 | /* first, mandatory, tree to insert */ |
| 74 | trees[0] = lyd_trees_getstart(tree); |
| 75 | LY_ARRAY_INCREMENT(trees); |
| 76 | |
| 77 | /* variable arguments */ |
| 78 | for (unsigned int u = 1; u < count; ++u) { |
| 79 | trees[u] = lyd_trees_getstart(va_arg(ap, const struct lyd_node *)); |
| 80 | LY_ARRAY_INCREMENT(trees); |
| 81 | } |
| 82 | |
| 83 | va_end(ap); |
| 84 | return trees; |
| 85 | |
| 86 | error: |
| 87 | (void)ret; /* unused */ |
| 88 | lyd_trees_free(trees, 1); |
| 89 | va_end(ap); |
| 90 | return NULL; |
| 91 | } |
| 92 | |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 93 | API const struct lyd_node ** |
| 94 | lyd_trees_add(const struct lyd_node **trees, const struct lyd_node *tree) |
| 95 | { |
| 96 | const struct lyd_node **t = NULL; |
| 97 | |
| 98 | LY_CHECK_ARG_RET(NULL, tree, trees, trees); |
| 99 | |
| 100 | LY_ARRAY_NEW_RET(tree->schema->module->ctx, trees, t, NULL); |
| 101 | *t = lyd_trees_getstart(tree); |
| 102 | |
| 103 | return trees; |
| 104 | } |
| 105 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 106 | API const struct lyd_node * |
| 107 | lyd_search(const struct lyd_node *first, const struct lys_module *module, |
| 108 | const char *name, size_t name_len, uint16_t nodetype, const char *value, size_t value_len) |
| 109 | { |
| 110 | const struct lyd_node *node = NULL; |
| 111 | const struct lysc_node *snode; |
| 112 | |
| 113 | LY_CHECK_ARG_RET(NULL, module, name, NULL); |
| 114 | if (!nodetype) { |
| 115 | nodetype = 0xffff; |
| 116 | } |
| 117 | |
| 118 | LY_LIST_FOR(first, node) { |
| 119 | snode = node->schema; |
| 120 | if (!(snode->nodetype & nodetype)) { |
| 121 | continue; |
| 122 | } |
| 123 | if (snode->module != module) { |
| 124 | continue; |
| 125 | } |
| 126 | |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 127 | if (ly_strncmp(snode->name, name, name_len)) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 128 | continue; |
| 129 | } |
| 130 | |
| 131 | if (value) { |
| 132 | if (snode->nodetype == LYS_LIST) { |
| 133 | /* TODO handle value as keys of the list instance */ |
| 134 | } else if (snode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 135 | if (ly_strncmp(((struct lyd_node_term*)node)->value.original, value, value_len)) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 136 | continue; |
| 137 | } |
| 138 | } else { |
| 139 | continue; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /* all criteria passed */ |
| 144 | return node; |
| 145 | } |
| 146 | return NULL; |
| 147 | } |
| 148 | |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 149 | LY_ERR |
Radek Krejci | 3c9758d | 2019-07-11 16:49:10 +0200 | [diff] [blame] | 150 | lyd_value_parse(struct lyd_node_term *node, const char *value, size_t value_len, int dynamic, int second, |
Radek Krejci | 576b23f | 2019-07-12 14:06:32 +0200 | [diff] [blame] | 151 | ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format, const struct lyd_node **trees) |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 152 | { |
| 153 | LY_ERR ret = LY_SUCCESS, rc; |
| 154 | struct ly_err_item *err = NULL; |
| 155 | struct ly_ctx *ctx; |
| 156 | struct lysc_type *type; |
Radek Krejci | 3c9758d | 2019-07-11 16:49:10 +0200 | [diff] [blame] | 157 | int options = LY_TYPE_OPTS_STORE | (second ? LY_TYPE_OPTS_SECOND_CALL : 0) | |
| 158 | (dynamic ? LY_TYPE_OPTS_DYNAMIC : 0) | (trees ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 159 | assert(node); |
| 160 | |
| 161 | ctx = node->schema->module->ctx; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 162 | |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 163 | type = ((struct lysc_node_leaf*)node->schema)->type; |
Radek Krejci | 62903c3 | 2019-07-15 14:42:05 +0200 | [diff] [blame] | 164 | if (!second) { |
| 165 | node->value.realtype = type; |
| 166 | } |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 167 | rc = type->plugin->store(ctx, type, value, value_len, options, get_prefix, parser, format, |
| 168 | trees ? (void*)node : (void*)node->schema, trees, |
| 169 | &node->value, NULL, &err); |
| 170 | if (rc == LY_EINCOMPLETE) { |
| 171 | ret = rc; |
| 172 | /* continue with storing, just remember what to return if storing is ok */ |
| 173 | } else if (rc) { |
| 174 | ret = rc; |
| 175 | if (err) { |
| 176 | ly_err_print(err); |
| 177 | LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg); |
| 178 | ly_err_free(err); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 179 | } |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 180 | goto error; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | error: |
| 184 | return ret; |
| 185 | } |
| 186 | |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 187 | LY_ERR |
| 188 | lyd_value_parse_attr(struct lyd_attr *attr, const char *value, size_t value_len, int dynamic, int second, |
| 189 | ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format, const struct lyd_node **trees) |
| 190 | { |
| 191 | LY_ERR ret = LY_SUCCESS, rc; |
| 192 | struct ly_err_item *err = NULL; |
| 193 | struct ly_ctx *ctx; |
| 194 | struct lyext_metadata *ant; |
| 195 | int options = LY_TYPE_OPTS_STORE | (second ? LY_TYPE_OPTS_SECOND_CALL : 0) | |
| 196 | (dynamic ? LY_TYPE_OPTS_DYNAMIC : 0) | (trees ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA); |
| 197 | assert(attr); |
| 198 | |
| 199 | ctx = attr->parent->schema->module->ctx; |
| 200 | ant = attr->annotation->data; |
| 201 | |
| 202 | if (!second) { |
| 203 | attr->value.realtype = ant->type; |
| 204 | } |
| 205 | rc = ant->type->plugin->store(ctx, ant->type, value, value_len, options, get_prefix, parser, format, |
| 206 | trees ? (void*)attr->parent : (void*)attr->parent->schema, trees, |
| 207 | &attr->value, NULL, &err); |
| 208 | if (rc == LY_EINCOMPLETE) { |
| 209 | ret = rc; |
| 210 | /* continue with storing, just remember what to return if storing is ok */ |
| 211 | } else if (rc) { |
| 212 | ret = rc; |
| 213 | if (err) { |
| 214 | ly_err_print(err); |
| 215 | LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg); |
| 216 | ly_err_free(err); |
| 217 | } |
| 218 | goto error; |
| 219 | } |
| 220 | |
| 221 | error: |
| 222 | return ret; |
| 223 | } |
| 224 | |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 225 | API LY_ERR |
| 226 | lys_value_validate(struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len, |
| 227 | ly_clb_resolve_prefix get_prefix, void *get_prefix_data, LYD_FORMAT format) |
| 228 | { |
| 229 | LY_ERR rc = LY_SUCCESS; |
| 230 | struct ly_err_item *err = NULL; |
| 231 | struct lysc_type *type; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 232 | |
| 233 | LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL); |
| 234 | |
| 235 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 236 | LOGARG(ctx, node); |
| 237 | return LY_EINVAL; |
| 238 | } |
| 239 | |
| 240 | type = ((struct lysc_node_leaf*)node)->type; |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 241 | /* just validate, no storing of enything */ |
| 242 | rc = type->plugin->store(ctx ? ctx : node->module->ctx, type, value, value_len, LY_TYPE_OPTS_INCOMPLETE_DATA, |
| 243 | get_prefix, get_prefix_data, format, node, NULL, NULL, NULL, &err); |
| 244 | if (rc == LY_EINCOMPLETE) { |
| 245 | /* actually success since we do not provide the context tree and call validation with |
| 246 | * LY_TYPE_OPTS_INCOMPLETE_DATA */ |
| 247 | rc = LY_SUCCESS; |
| 248 | } else if (rc && err) { |
| 249 | if (ctx) { |
| 250 | /* log only in case the ctx was provided as input parameter */ |
| 251 | ly_err_print(err); |
| 252 | LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 253 | } |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 254 | ly_err_free(err); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | return rc; |
| 258 | } |
| 259 | |
| 260 | API LY_ERR |
| 261 | lyd_value_validate(struct ly_ctx *ctx, const struct lyd_node_term *node, const char *value, size_t value_len, |
Radek Krejci | 576b23f | 2019-07-12 14:06:32 +0200 | [diff] [blame] | 262 | ly_clb_resolve_prefix get_prefix, void *get_prefix_data, LYD_FORMAT format, const struct lyd_node **trees) |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 263 | { |
| 264 | LY_ERR rc; |
| 265 | struct ly_err_item *err = NULL; |
| 266 | struct lysc_type *type; |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 267 | int options = (trees ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 268 | |
| 269 | LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL); |
| 270 | |
| 271 | type = ((struct lysc_node_leaf*)node->schema)->type; |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 272 | rc = type->plugin->store(ctx ? ctx : node->schema->module->ctx, type, value, value_len, options, |
| 273 | get_prefix, get_prefix_data, format, trees ? (void*)node : (void*)node->schema, trees, |
| 274 | NULL, NULL, &err); |
| 275 | if (rc == LY_EINCOMPLETE) { |
| 276 | return rc; |
| 277 | } else if (rc) { |
| 278 | if (err) { |
| 279 | if (ctx) { |
| 280 | ly_err_print(err); |
| 281 | LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 282 | } |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 283 | ly_err_free(err); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 284 | } |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 285 | return rc; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | return LY_SUCCESS; |
| 289 | } |
| 290 | |
| 291 | API LY_ERR |
| 292 | lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len, |
Radek Krejci | 576b23f | 2019-07-12 14:06:32 +0200 | [diff] [blame] | 293 | ly_clb_resolve_prefix get_prefix, void *get_prefix_data, LYD_FORMAT format, const struct lyd_node **trees) |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 294 | { |
| 295 | LY_ERR ret = LY_SUCCESS, rc; |
| 296 | struct ly_err_item *err = NULL; |
| 297 | struct ly_ctx *ctx; |
| 298 | struct lysc_type *type; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 299 | struct lyd_value data = {0}; |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 300 | int options = LY_TYPE_OPTS_STORE | (trees ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 301 | |
| 302 | LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, value, LY_EINVAL); |
| 303 | |
| 304 | ctx = node->schema->module->ctx; |
| 305 | type = ((struct lysc_node_leaf*)node->schema)->type; |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 306 | rc = type->plugin->store(ctx, type, value, value_len, options, get_prefix, get_prefix_data, format, (struct lyd_node*)node, |
| 307 | trees, &data, NULL, &err); |
| 308 | if (rc == LY_EINCOMPLETE) { |
| 309 | ret = rc; |
| 310 | /* continue with comparing, just remember what to return if storing is ok */ |
| 311 | } else if (rc) { |
| 312 | /* value to compare is invalid */ |
| 313 | ret = LY_EINVAL; |
| 314 | if (err) { |
| 315 | ly_err_free(err); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 316 | } |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 317 | goto cleanup; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | /* compare data */ |
Radek Krejci | 5af0484 | 2019-07-12 11:32:07 +0200 | [diff] [blame] | 321 | if (type->plugin->compare(&node->value, &data)) { |
| 322 | /* do not assign it directly from the compare callback to keep possible LY_EINCOMPLETE from validation */ |
| 323 | ret = LY_EVALID; |
| 324 | } |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 325 | |
| 326 | cleanup: |
Radek Krejci | 62903c3 | 2019-07-15 14:42:05 +0200 | [diff] [blame] | 327 | type->plugin->free(ctx, &data); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 328 | |
| 329 | return ret; |
| 330 | } |
| 331 | |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 332 | API const char * |
| 333 | lyd_value2str(const struct lyd_node_term *node, int *dynamic) |
| 334 | { |
| 335 | LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, dynamic, NULL); |
| 336 | |
| 337 | return node->value.realtype->plugin->print(&node->value, LYD_JSON, json_print_get_prefix, NULL, dynamic); |
| 338 | } |
| 339 | |
| 340 | API const char * |
| 341 | lyd_attr2str(const struct lyd_attr *attr, int *dynamic) |
| 342 | { |
| 343 | LY_CHECK_ARG_RET(attr ? attr->parent->schema->module->ctx : NULL, attr, dynamic, NULL); |
| 344 | |
| 345 | return attr->value.realtype->plugin->print(&attr->value, LYD_JSON, json_print_get_prefix, NULL, dynamic); |
| 346 | } |
| 347 | |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 348 | API struct lyd_node * |
| 349 | lyd_parse_mem(struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int options, const struct lyd_node **trees) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 350 | { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 351 | struct lyd_node *result = NULL; |
Radek Krejci | e92210c | 2019-05-17 15:53:35 +0200 | [diff] [blame] | 352 | #if 0 |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 353 | const char *yang_data_name = NULL; |
Radek Krejci | e92210c | 2019-05-17 15:53:35 +0200 | [diff] [blame] | 354 | #endif |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 355 | |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 356 | LY_CHECK_ARG_RET(ctx, ctx, NULL); |
| 357 | |
| 358 | if (lyd_parse_options_check(ctx, options, __func__)) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 359 | return NULL; |
| 360 | } |
| 361 | |
| 362 | if (options & LYD_OPT_RPCREPLY) { |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 363 | /* first item in trees is mandatory - the RPC/action request */ |
| 364 | LY_CHECK_ARG_RET(ctx, trees, LY_ARRAY_SIZE(trees) >= 1, NULL); |
| 365 | if (!trees[0] || trees[0]->parent || !(trees[0]->schema->nodetype & (LYS_ACTION | LYS_LIST | LYS_CONTAINER))) { |
| 366 | LOGERR(ctx, LY_EINVAL, "Data parser invalid argument trees - the first item in the array must be the RPC/action request when parsing %s.", |
| 367 | lyd_parse_options_type2str(options)); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 368 | return NULL; |
| 369 | } |
| 370 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 371 | |
Radek Krejci | e92210c | 2019-05-17 15:53:35 +0200 | [diff] [blame] | 372 | #if 0 |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 373 | if (options & LYD_OPT_DATA_TEMPLATE) { |
| 374 | yang_data_name = va_arg(ap, const char *); |
| 375 | } |
Radek Krejci | e92210c | 2019-05-17 15:53:35 +0200 | [diff] [blame] | 376 | #endif |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 377 | |
| 378 | if (!format) { |
| 379 | /* TODO try to detect format from the content */ |
| 380 | } |
| 381 | |
| 382 | switch (format) { |
| 383 | case LYD_XML: |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 384 | lyd_parse_xml(ctx, data, options, trees, &result); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 385 | break; |
| 386 | #if 0 |
| 387 | case LYD_JSON: |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 388 | lyd_parse_json(ctx, data, options, trees, &result); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 389 | break; |
| 390 | case LYD_LYB: |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 391 | lyd_parse_lyb(ctx, data, options, trees, &result); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 392 | break; |
| 393 | #endif |
| 394 | case LYD_UNKNOWN: |
| 395 | LOGINT(ctx); |
| 396 | break; |
| 397 | } |
| 398 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 399 | return result; |
| 400 | } |
| 401 | |
| 402 | API struct lyd_node * |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 403 | lyd_parse_fd(struct ly_ctx *ctx, int fd, LYD_FORMAT format, int options, const struct lyd_node **trees) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 404 | { |
| 405 | struct lyd_node *result; |
| 406 | size_t length; |
| 407 | char *addr; |
| 408 | |
| 409 | LY_CHECK_ARG_RET(ctx, ctx, NULL); |
| 410 | if (fd < 0) { |
| 411 | LOGARG(ctx, fd); |
| 412 | return NULL; |
| 413 | } |
| 414 | |
| 415 | LY_CHECK_RET(ly_mmap(ctx, fd, &length, (void **)&addr), NULL); |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 416 | result = lyd_parse_mem(ctx, addr ? addr : "", format, options, trees); |
Radek Krejci | df3da79 | 2019-05-17 10:32:24 +0200 | [diff] [blame] | 417 | if (addr) { |
| 418 | ly_munmap(addr, length); |
| 419 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 420 | |
| 421 | return result; |
| 422 | } |
| 423 | |
| 424 | API struct lyd_node * |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 425 | lyd_parse_path(struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int options, const struct lyd_node **trees) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 426 | { |
| 427 | int fd; |
| 428 | struct lyd_node *result; |
| 429 | size_t len; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 430 | |
| 431 | LY_CHECK_ARG_RET(ctx, ctx, path, NULL); |
| 432 | |
| 433 | fd = open(path, O_RDONLY); |
| 434 | LY_CHECK_ERR_RET(fd == -1, LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)), NULL); |
| 435 | |
| 436 | if (!format) { |
| 437 | /* unknown format - try to detect it from filename's suffix */ |
| 438 | len = strlen(path); |
| 439 | |
| 440 | /* ignore trailing whitespaces */ |
| 441 | for (; len > 0 && isspace(path[len - 1]); len--); |
| 442 | |
| 443 | if (len >= 5 && !strncmp(&path[len - 4], ".xml", 4)) { |
| 444 | format = LYD_XML; |
| 445 | #if 0 |
| 446 | } else if (len >= 6 && !strncmp(&path[len - 5], ".json", 5)) { |
| 447 | format = LYD_JSON; |
| 448 | } else if (len >= 5 && !strncmp(&path[len - 4], ".lyb", 4)) { |
| 449 | format = LYD_LYB; |
| 450 | #endif |
| 451 | } /* else still unknown, try later to detect it from the content */ |
| 452 | } |
| 453 | |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 454 | result = lyd_parse_fd(ctx, fd, format, options, trees); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 455 | close(fd); |
| 456 | |
| 457 | return result; |
| 458 | } |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 459 | |
| 460 | API const struct lyd_node_term * |
Radek Krejci | 576b23f | 2019-07-12 14:06:32 +0200 | [diff] [blame] | 461 | lyd_target(struct lyd_value_path *path, const struct lyd_node **trees) |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 462 | { |
| 463 | unsigned int u, v, x; |
| 464 | const struct lyd_node *node = NULL, *parent = NULL, *start_search; |
| 465 | uint64_t pos = 1; |
| 466 | |
| 467 | LY_CHECK_ARG_RET(NULL, path, trees, NULL); |
| 468 | |
| 469 | LY_ARRAY_FOR(path, u) { |
| 470 | if (parent) { |
| 471 | start_search = lyd_node_children(parent); |
| 472 | search_inner: |
| 473 | node = lyd_search(start_search, path[u].node->module, path[u].node->name, strlen(path[u].node->name), path[u].node->nodetype, NULL, 0); |
| 474 | } else { |
| 475 | LY_ARRAY_FOR(trees, v) { |
| 476 | start_search = trees[v]; |
| 477 | search_toplevel: |
| 478 | /* WARNING! to use search_toplevel label correctly, variable v must be preserved and not changed! */ |
| 479 | node = lyd_search(start_search, path[u].node->module, path[u].node->name, strlen(path[u].node->name), path[u].node->nodetype, NULL, 0); |
| 480 | if (node) { |
| 481 | break; |
| 482 | } |
| 483 | } |
| 484 | } |
| 485 | if (!node) { |
| 486 | return NULL; |
| 487 | } |
| 488 | |
| 489 | /* check predicate if any */ |
| 490 | LY_ARRAY_FOR(path[u].predicates, x) { |
| 491 | if (path[u].predicates[x].type == 0) { |
| 492 | /* position predicate */ |
| 493 | if (pos != path[u].predicates[x].position) { |
| 494 | pos++; |
| 495 | goto search_repeat; |
| 496 | } |
| 497 | /* done, no more predicates are allowed here */ |
| 498 | break; |
| 499 | } else if (path[u].predicates[x].type == 1) { |
| 500 | /* key-predicate */ |
| 501 | struct lysc_type *type = ((struct lysc_node_leaf*)path[u].predicates[x].key)->type; |
| 502 | const struct lyd_node *key = lyd_search(lyd_node_children(node), path[u].predicates[x].key->module, |
| 503 | path[u].predicates[x].key->name, strlen(path[u].predicates[x].key->name), |
| 504 | LYS_LEAF, NULL, 0); |
| 505 | if (!key) { |
| 506 | /* probably error and we shouldn't be here due to previous checks when creating path */ |
| 507 | goto search_repeat; |
| 508 | } |
| 509 | if (type->plugin->compare(&((struct lyd_node_term*)key)->value, path[u].predicates[x].value)) { |
| 510 | goto search_repeat; |
| 511 | } |
| 512 | } else if (path[u].predicates[x].type == 2) { |
| 513 | /* leaf-list-predicate */ |
| 514 | struct lysc_type *type = ((struct lysc_node_leaf*)path[u].node)->type; |
| 515 | if (type->plugin->compare(&((struct lyd_node_term*)node)->value, path[u].predicates[x].value)) { |
| 516 | goto search_repeat; |
| 517 | } |
| 518 | } else { |
| 519 | LOGINT(NULL); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | parent = node; |
| 524 | } |
| 525 | |
| 526 | return (const struct lyd_node_term*)node; |
| 527 | |
| 528 | search_repeat: |
| 529 | start_search = node->next; |
| 530 | if (parent) { |
| 531 | goto search_inner; |
| 532 | } else { |
| 533 | goto search_toplevel; |
| 534 | } |
| 535 | } |
| 536 | |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 537 | API LY_ERR |
| 538 | lyd_compare(const struct lyd_node *node1, const struct lyd_node *node2, int options) |
| 539 | { |
| 540 | const struct lyd_node *iter1, *iter2; |
| 541 | struct lyd_node_term *term1, *term2; |
| 542 | struct lyd_node_any *any1, *any2; |
| 543 | struct lysc_type *type; |
| 544 | size_t len1, len2; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 545 | |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 546 | if (!node1 || !node2) { |
| 547 | if (node1 == node2) { |
| 548 | return LY_SUCCESS; |
| 549 | } else { |
| 550 | return LY_ENOT; |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | if (node1->schema->module->ctx != node2->schema->module->ctx || node1->schema != node2 ->schema) { |
| 555 | return LY_ENOT; |
| 556 | } |
| 557 | |
| 558 | if (node1->hash != node2->hash) { |
| 559 | return LY_ENOT; |
| 560 | } |
| 561 | |
| 562 | /* equal hashes do not mean equal nodes, they can be just in collision so the nodes must be checked explicitly */ |
| 563 | |
| 564 | switch (node1->schema->nodetype) { |
| 565 | case LYS_LEAF: |
| 566 | case LYS_LEAFLIST: |
| 567 | if (options & LYD_COMPARE_DEFAULTS) { |
| 568 | if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) { |
| 569 | return LY_ENOT; |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | term1 = (struct lyd_node_term*)node1; |
| 574 | term2 = (struct lyd_node_term*)node2; |
| 575 | type = ((struct lysc_node_leaf*)node1->schema)->type; |
| 576 | |
| 577 | return type->plugin->compare(&term1->value, &term2->value); |
| 578 | case LYS_CONTAINER: |
| 579 | if (options & LYD_COMPARE_DEFAULTS) { |
| 580 | if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) { |
| 581 | return LY_ENOT; |
| 582 | } |
| 583 | } |
| 584 | if (options & LYD_COMPARE_FULL_RECURSION) { |
| 585 | iter1 = ((struct lyd_node_inner*)node1)->child; |
| 586 | iter2 = ((struct lyd_node_inner*)node2)->child; |
| 587 | goto all_children_compare; |
| 588 | } |
| 589 | return LY_SUCCESS; |
| 590 | case LYS_ACTION: |
| 591 | if (options & LYD_COMPARE_FULL_RECURSION) { |
| 592 | /* TODO action/RPC |
| 593 | goto all_children_compare; |
| 594 | */ |
| 595 | } |
| 596 | return LY_SUCCESS; |
| 597 | case LYS_NOTIF: |
| 598 | if (options & LYD_COMPARE_FULL_RECURSION) { |
| 599 | /* TODO Notification |
| 600 | goto all_children_compare; |
| 601 | */ |
| 602 | } |
| 603 | return LY_SUCCESS; |
| 604 | case LYS_LIST: |
| 605 | iter1 = ((struct lyd_node_inner*)node1)->child; |
| 606 | iter2 = ((struct lyd_node_inner*)node2)->child; |
| 607 | |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 608 | if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) { |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 609 | /* lists with keys, their equivalence is based on their keys */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 610 | for (struct lysc_node *key = ((struct lysc_node_list*)node1->schema)->child; |
| 611 | key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY); |
| 612 | key = key->next) { |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 613 | if (lyd_compare(iter1, iter2, options)) { |
| 614 | return LY_ENOT; |
| 615 | } |
| 616 | iter1 = iter1->next; |
| 617 | iter2 = iter2->next; |
| 618 | } |
| 619 | } else { |
| 620 | /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */ |
| 621 | |
| 622 | all_children_compare: |
| 623 | if (!iter1 && !iter2) { |
| 624 | /* no children, nothing to compare */ |
| 625 | return LY_SUCCESS; |
| 626 | } |
| 627 | |
| 628 | for (; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) { |
| 629 | if (lyd_compare(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION)) { |
| 630 | return LY_ENOT; |
| 631 | } |
| 632 | } |
| 633 | if (iter1 || iter2) { |
| 634 | return LY_ENOT; |
| 635 | } |
| 636 | } |
| 637 | return LY_SUCCESS; |
| 638 | case LYS_ANYXML: |
| 639 | case LYS_ANYDATA: |
| 640 | any1 = (struct lyd_node_any*)node1; |
| 641 | any2 = (struct lyd_node_any*)node2; |
| 642 | |
| 643 | if (any1->value_type != any2->value_type) { |
| 644 | return LY_ENOT; |
| 645 | } |
| 646 | switch (any1->value_type) { |
| 647 | case LYD_ANYDATA_DATATREE: |
| 648 | iter1 = any1->value.tree; |
| 649 | iter2 = any2->value.tree; |
| 650 | goto all_children_compare; |
| 651 | case LYD_ANYDATA_STRING: |
| 652 | case LYD_ANYDATA_XML: |
| 653 | case LYD_ANYDATA_JSON: |
| 654 | len1 = strlen(any1->value.str); |
| 655 | len2 = strlen(any2->value.str); |
| 656 | if (len1 != len2 || strcmp(any1->value.str, any2->value.str)) { |
| 657 | return LY_ENOT; |
| 658 | } |
| 659 | return LY_SUCCESS; |
| 660 | #if 0 /* TODO LYB format */ |
| 661 | case LYD_ANYDATA_LYB: |
| 662 | int len1 = lyd_lyb_data_length(any1->value.mem); |
| 663 | int len2 = lyd_lyb_data_length(any2->value.mem); |
| 664 | if (len1 != len2 || memcmp(any1->value.mem, any2->value.mem, len1)) { |
| 665 | return LY_ENOT; |
| 666 | } |
| 667 | return LY_SUCCESS; |
| 668 | #endif |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | LOGINT(node1->schema->module->ctx); |
| 673 | return LY_EINT; |
| 674 | } |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 675 | |
| 676 | /** |
| 677 | * @brief Duplicates just a single node and interconnect it into a @p parent (if present) and after the @p prev |
| 678 | * sibling (if present). |
| 679 | * |
| 680 | * Ignores LYD_DUP_WITH_PARENTS and LYD_DUP_WITH_SIBLINGS which are supposed to be handled by lyd_dup(). |
| 681 | */ |
| 682 | static struct lyd_node * |
| 683 | lyd_dup_recursive(const struct lyd_node *node, struct lyd_node_inner *parent, struct lyd_node *prev, int options) |
| 684 | { |
| 685 | struct ly_ctx *ctx; |
| 686 | struct lyd_node *dup = NULL; |
| 687 | |
| 688 | LY_CHECK_ARG_RET(NULL, node, NULL); |
| 689 | ctx = node->schema->module->ctx; |
| 690 | |
| 691 | switch (node->schema->nodetype) { |
| 692 | case LYS_ACTION: |
| 693 | case LYS_NOTIF: |
| 694 | case LYS_CONTAINER: |
| 695 | case LYS_LIST: |
| 696 | dup = calloc(1, sizeof(struct lyd_node_inner)); |
| 697 | break; |
| 698 | case LYS_LEAF: |
| 699 | case LYS_LEAFLIST: |
| 700 | dup = calloc(1, sizeof(struct lyd_node_term)); |
| 701 | break; |
| 702 | case LYS_ANYDATA: |
| 703 | case LYS_ANYXML: |
| 704 | dup = calloc(1, sizeof(struct lyd_node_any)); |
| 705 | break; |
| 706 | default: |
| 707 | LOGINT(ctx); |
| 708 | goto error; |
| 709 | } |
| 710 | |
| 711 | /* TODO implement LYD_DUP_WITH_WHEN */ |
| 712 | dup->flags = node->flags; |
| 713 | dup->schema = node->schema; |
| 714 | |
| 715 | /* interconnect the node at the end */ |
| 716 | dup->parent = parent; |
| 717 | if (prev) { |
| 718 | dup->prev = prev; |
| 719 | prev->next = dup; |
| 720 | } else { |
| 721 | dup->prev = dup; |
| 722 | if (parent) { |
| 723 | parent->child = dup; |
| 724 | } |
| 725 | } |
| 726 | if (parent) { |
| 727 | parent->child->prev = dup; |
| 728 | } else if (prev) { |
| 729 | struct lyd_node *first; |
| 730 | for (first = prev; first->prev != prev; first = first->prev); |
| 731 | first->prev = dup; |
| 732 | } |
| 733 | |
| 734 | /* TODO duplicate attributes, implement LYD_DUP_NO_ATTR */ |
| 735 | |
| 736 | /* nodetype-specific work */ |
| 737 | if (dup->schema->nodetype & LYD_NODE_TERM) { |
| 738 | struct lyd_node_term *term = (struct lyd_node_term*)dup; |
| 739 | struct lyd_node_term *orig = (struct lyd_node_term*)node; |
| 740 | |
| 741 | term->hash = orig->hash; |
| 742 | term->value.realtype = orig->value.realtype; |
| 743 | LY_CHECK_ERR_GOTO(term->value.realtype->plugin->duplicate(ctx, &orig->value, &term->value), |
| 744 | LOGERR(ctx, LY_EINT, "Value duplication failed."), error); |
| 745 | } else if (dup->schema->nodetype & LYD_NODE_INNER) { |
| 746 | struct lyd_node_inner *inner = (struct lyd_node_inner*)dup; |
| 747 | struct lyd_node_inner *orig = (struct lyd_node_inner*)node; |
| 748 | struct lyd_node *child, *last = NULL; |
| 749 | |
| 750 | if (options & LYD_DUP_RECURSIVE) { |
| 751 | /* duplicate all the children */ |
| 752 | LY_LIST_FOR(orig->child, child) { |
| 753 | last = lyd_dup_recursive(child, inner, last, options); |
| 754 | LY_CHECK_GOTO(!last, error); |
| 755 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 756 | } else if (dup->schema->nodetype == LYS_LIST && !(dup->schema->flags & LYS_KEYLESS)) { |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 757 | /* always duplicate keys of a list */ |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 758 | child = orig->child; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 759 | for (struct lysc_node *key = ((struct lysc_node_list*)dup->schema)->child; |
| 760 | key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY); |
| 761 | key = key->next) { |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 762 | if (!child) { |
| 763 | /* possibly not keys are present in filtered tree */ |
| 764 | break; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 765 | } else if (child->schema != key) { |
| 766 | /* possibly not all keys are present in filtered tree, |
| 767 | * but there can be also some non-key nodes */ |
| 768 | continue; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 769 | } |
| 770 | last = lyd_dup_recursive(child, inner, last, options); |
| 771 | child = child->next; |
| 772 | } |
| 773 | } |
| 774 | lyd_hash(dup); |
| 775 | } else if (dup->schema->nodetype & LYD_NODE_ANY) { |
| 776 | struct lyd_node_any *any = (struct lyd_node_any*)dup; |
| 777 | struct lyd_node_any *orig = (struct lyd_node_any*)node; |
| 778 | |
| 779 | any->hash = orig->hash; |
| 780 | any->value_type = orig->value_type; |
| 781 | switch (any->value_type) { |
| 782 | case LYD_ANYDATA_DATATREE: |
| 783 | if (orig->value.tree) { |
| 784 | any->value.tree = lyd_dup(orig->value.tree, NULL, LYD_DUP_RECURSIVE | LYD_DUP_WITH_SIBLINGS); |
| 785 | LY_CHECK_GOTO(!any->value.tree, error); |
| 786 | } |
| 787 | break; |
| 788 | case LYD_ANYDATA_STRING: |
| 789 | case LYD_ANYDATA_XML: |
| 790 | case LYD_ANYDATA_JSON: |
| 791 | if (orig->value.str) { |
| 792 | any->value.str = lydict_insert(ctx, orig->value.str, strlen(orig->value.str)); |
| 793 | } |
| 794 | break; |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | lyd_insert_hash(dup); |
| 799 | return dup; |
| 800 | |
| 801 | error: |
| 802 | if (!parent && !prev) { |
| 803 | lyd_free_tree(dup); |
| 804 | } |
| 805 | return NULL; |
| 806 | } |
| 807 | |
| 808 | API struct lyd_node * |
| 809 | lyd_dup(const struct lyd_node *node, struct lyd_node_inner *parent, int options) |
| 810 | { |
| 811 | struct ly_ctx *ctx; |
| 812 | const struct lyd_node *orig; /* original node to be duplicated */ |
| 813 | struct lyd_node *first = NULL; /* the first duplicated node, this is returned */ |
| 814 | struct lyd_node *last = NULL; /* the last sibling of the duplicated nodes */ |
| 815 | struct lyd_node *top = NULL; /* the most higher created node */ |
| 816 | struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */ |
| 817 | int keyless_parent_list = 0; |
| 818 | |
| 819 | LY_CHECK_ARG_RET(NULL, node, NULL); |
| 820 | ctx = node->schema->module->ctx; |
| 821 | |
| 822 | if (options & LYD_DUP_WITH_PARENTS) { |
| 823 | struct lyd_node_inner *orig_parent, *iter; |
| 824 | int repeat = 1; |
| 825 | for (top = NULL, orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) { |
| 826 | if (parent && parent->schema == orig_parent->schema) { |
| 827 | /* stop creating parents, connect what we have into the provided parent */ |
| 828 | iter = parent; |
| 829 | repeat = 0; |
| 830 | /* get know if there is a keyless list which we will have to rehash */ |
| 831 | for (struct lyd_node_inner *piter = parent; piter; piter = piter->parent) { |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 832 | if (piter->schema->nodetype == LYS_LIST && (piter->schema->flags & LYS_KEYLESS)) { |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 833 | keyless_parent_list = 1; |
| 834 | break; |
| 835 | } |
| 836 | } |
| 837 | } else { |
| 838 | iter = (struct lyd_node_inner*)lyd_dup_recursive((struct lyd_node*)orig_parent, NULL, NULL, 0); |
| 839 | LY_CHECK_GOTO(!iter, error); |
| 840 | } |
| 841 | if (!local_parent) { |
| 842 | local_parent = iter; |
| 843 | } |
| 844 | if (iter->child) { |
| 845 | /* 1) list - add after keys |
| 846 | * 2) provided parent with some children */ |
| 847 | iter->child->prev->next = top; |
| 848 | if (top) { |
| 849 | top->prev = iter->child->prev; |
| 850 | iter->child->prev = top; |
| 851 | } |
| 852 | } else { |
| 853 | iter->child = top; |
| 854 | if (iter->schema->nodetype == LYS_LIST) { |
| 855 | /* keyless list - we will need to rehash it since we are going to add nodes into it */ |
| 856 | keyless_parent_list = 1; |
| 857 | } |
| 858 | } |
| 859 | if (top) { |
| 860 | top->parent = iter; |
| 861 | } |
| 862 | top = (struct lyd_node*)iter; |
| 863 | } |
| 864 | if (repeat && parent) { |
| 865 | /* given parent and created parents chain actually do not interconnect */ |
| 866 | LOGERR(ctx, LY_EINVAL, "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__); |
| 867 | goto error; |
| 868 | } |
| 869 | } else { |
| 870 | local_parent = parent; |
| 871 | } |
| 872 | |
| 873 | if (local_parent && local_parent->child) { |
| 874 | last = local_parent->child->prev; |
| 875 | } |
| 876 | |
| 877 | LY_LIST_FOR(node, orig) { |
| 878 | last = lyd_dup_recursive(orig, local_parent, last, options); |
| 879 | LY_CHECK_GOTO(!last, error); |
| 880 | if (!first) { |
| 881 | first = last; |
| 882 | } |
| 883 | |
| 884 | if (!(options & LYD_DUP_WITH_SIBLINGS)) { |
| 885 | break; |
| 886 | } |
| 887 | } |
| 888 | if (keyless_parent_list) { |
| 889 | /* rehash */ |
| 890 | for (; local_parent; local_parent = local_parent->parent) { |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 891 | 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] | 892 | lyd_hash((struct lyd_node*)local_parent); |
| 893 | } |
| 894 | } |
| 895 | } |
| 896 | return first; |
| 897 | |
| 898 | error: |
| 899 | if (top) { |
| 900 | lyd_free_tree(top); |
| 901 | } else { |
| 902 | lyd_free_withsiblings(first); |
| 903 | } |
| 904 | return NULL; |
| 905 | } |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 906 | |
| 907 | static LY_ERR |
| 908 | lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, int is_static) |
| 909 | { |
| 910 | if (reqlen > *buflen) { |
| 911 | if (is_static) { |
| 912 | return LY_EINCOMPLETE; |
| 913 | } |
| 914 | |
| 915 | *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer); |
| 916 | if (!*buffer) { |
| 917 | return LY_EMEM; |
| 918 | } |
| 919 | |
| 920 | *buflen = reqlen; |
| 921 | } |
| 922 | |
| 923 | return LY_SUCCESS; |
| 924 | } |
| 925 | |
| 926 | /** |
| 927 | * @brief Append all list key predicates to path. |
| 928 | * |
| 929 | * @param[in] node Node with keys to print. |
| 930 | * @param[in,out] buffer Buffer to print to. |
| 931 | * @param[in,out] buflen Current buffer length. |
| 932 | * @param[in,out] bufused Current number of characters used in @p buffer. |
| 933 | * @param[in] is_static Whether buffer is static or can be reallocated. |
| 934 | * @return LY_ERR |
| 935 | */ |
| 936 | static LY_ERR |
| 937 | lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static) |
| 938 | { |
| 939 | const struct lyd_node *key; |
| 940 | int dynamic = 0; |
| 941 | size_t len; |
| 942 | const char *val; |
| 943 | char quot; |
| 944 | LY_ERR rc; |
| 945 | |
| 946 | for (key = lyd_node_children(node); key && (key->flags & LYS_KEY); key = key->next) { |
| 947 | val = lyd_value2str((struct lyd_node_term *)key, &dynamic); |
| 948 | len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2; |
| 949 | rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static); |
| 950 | if (rc != LY_SUCCESS) { |
| 951 | if (dynamic) { |
| 952 | free((char *)val); |
| 953 | } |
| 954 | return rc; |
| 955 | } |
| 956 | |
| 957 | quot = '\''; |
| 958 | if (strchr(val, '\'')) { |
| 959 | quot = '"'; |
| 960 | } |
| 961 | *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot); |
| 962 | |
| 963 | if (dynamic) { |
| 964 | free((char *)val); |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | return LY_SUCCESS; |
| 969 | } |
| 970 | |
| 971 | /** |
| 972 | * @brief Append leaf-list value predicate to path. |
| 973 | * |
| 974 | * @param[in] node Node to print. |
| 975 | * @param[in,out] buffer Buffer to print to. |
| 976 | * @param[in,out] buflen Current buffer length. |
| 977 | * @param[in,out] bufused Current number of characters used in @p buffer. |
| 978 | * @param[in] is_static Whether buffer is static or can be reallocated. |
| 979 | * @return LY_ERR |
| 980 | */ |
| 981 | static LY_ERR |
| 982 | lyd_path_leaflist_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static) |
| 983 | { |
| 984 | int dynamic = 0; |
| 985 | size_t len; |
| 986 | const char *val; |
| 987 | char quot; |
| 988 | LY_ERR rc; |
| 989 | |
| 990 | val = lyd_value2str((struct lyd_node_term *)node, &dynamic); |
| 991 | len = 4 + strlen(val) + 2; |
| 992 | rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static); |
| 993 | if (rc != LY_SUCCESS) { |
| 994 | goto cleanup; |
| 995 | } |
| 996 | |
| 997 | quot = '\''; |
| 998 | if (strchr(val, '\'')) { |
| 999 | quot = '"'; |
| 1000 | } |
| 1001 | *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot); |
| 1002 | |
| 1003 | cleanup: |
| 1004 | if (dynamic) { |
| 1005 | free((char *)val); |
| 1006 | } |
| 1007 | return rc; |
| 1008 | } |
| 1009 | |
| 1010 | /** |
| 1011 | * @brief Append node position (relative to its other instances) predicate to path. |
| 1012 | * |
| 1013 | * @param[in] node Node to print. |
| 1014 | * @param[in,out] buffer Buffer to print to. |
| 1015 | * @param[in,out] buflen Current buffer length. |
| 1016 | * @param[in,out] bufused Current number of characters used in @p buffer. |
| 1017 | * @param[in] is_static Whether buffer is static or can be reallocated. |
| 1018 | * @return LY_ERR |
| 1019 | */ |
| 1020 | static LY_ERR |
| 1021 | lyd_path_position_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static) |
| 1022 | { |
| 1023 | const struct lyd_node *first, *iter; |
| 1024 | size_t len; |
| 1025 | int pos; |
| 1026 | char *val = NULL; |
| 1027 | LY_ERR rc; |
| 1028 | |
| 1029 | if (node->parent) { |
| 1030 | first = node->parent->child; |
| 1031 | } else { |
| 1032 | for (first = node; node->prev->next; node = node->prev); |
| 1033 | } |
| 1034 | pos = 1; |
| 1035 | for (iter = first; iter != node; iter = iter->next) { |
| 1036 | if (iter->schema == node->schema) { |
| 1037 | ++pos; |
| 1038 | } |
| 1039 | } |
| 1040 | if (asprintf(&val, "%d", pos) == -1) { |
| 1041 | return LY_EMEM; |
| 1042 | } |
| 1043 | |
| 1044 | len = 1 + strlen(val) + 1; |
| 1045 | rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static); |
| 1046 | if (rc != LY_SUCCESS) { |
| 1047 | goto cleanup; |
| 1048 | } |
| 1049 | |
| 1050 | *bufused += sprintf(*buffer + *bufused, "[%s]", val); |
| 1051 | |
| 1052 | cleanup: |
| 1053 | free(val); |
| 1054 | return rc; |
| 1055 | } |
| 1056 | |
| 1057 | API char * |
| 1058 | lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen) |
| 1059 | { |
| 1060 | int is_static = 0, i, j, depth; |
| 1061 | size_t bufused = 1 /* ending zero */, len; |
| 1062 | const struct lyd_node *iter; |
| 1063 | const struct lys_module *mod; |
| 1064 | LY_ERR rc; |
| 1065 | |
| 1066 | LY_CHECK_ARG_RET(NULL, node, NULL); |
| 1067 | if (buffer) { |
| 1068 | LY_CHECK_ARG_RET(node->schema->module->ctx, buflen > 1, NULL); |
| 1069 | is_static = 1; |
| 1070 | } |
| 1071 | |
| 1072 | switch (pathtype) { |
| 1073 | case LYSC_PATH_LOG: |
| 1074 | depth = 0; |
| 1075 | for (iter = node; iter->parent; iter = (const struct lyd_node *)iter->parent) { |
| 1076 | ++depth; |
| 1077 | } |
| 1078 | |
| 1079 | i = depth + 1; |
| 1080 | goto iter_print; |
| 1081 | while (i) { |
| 1082 | /* find the right node */ |
| 1083 | for (iter = node, j = 0; j < i; iter = (const struct lyd_node *)iter->parent, ++j); |
| 1084 | iter_print: |
| 1085 | /* print prefix and name */ |
| 1086 | mod = NULL; |
| 1087 | if (!iter->parent || (iter->schema->module != iter->parent->schema->module)) { |
| 1088 | mod = iter->schema->module; |
| 1089 | } |
| 1090 | |
| 1091 | /* realloc string */ |
| 1092 | len = 1 + (mod ? strlen(mod->name) + 1 : 0) + strlen(iter->schema->name); |
| 1093 | rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static); |
| 1094 | if (rc != LY_SUCCESS) { |
| 1095 | break; |
| 1096 | } |
| 1097 | |
| 1098 | /* print next node */ |
| 1099 | bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", iter->schema->name); |
| 1100 | |
| 1101 | switch (iter->schema->nodetype) { |
| 1102 | case LYS_LIST: |
| 1103 | if (iter->schema->flags & LYS_KEYLESS) { |
| 1104 | /* print its position */ |
| 1105 | rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static); |
| 1106 | } else { |
| 1107 | /* print all list keys in predicates */ |
| 1108 | rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static); |
| 1109 | } |
| 1110 | break; |
| 1111 | case LYS_LEAFLIST: |
| 1112 | if (iter->schema->flags & LYS_CONFIG_W) { |
| 1113 | /* print leaf-list value */ |
| 1114 | rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static); |
| 1115 | } else { |
| 1116 | /* print its position */ |
| 1117 | rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static); |
| 1118 | } |
| 1119 | break; |
| 1120 | default: |
| 1121 | /* nothing to print more */ |
| 1122 | rc = LY_SUCCESS; |
| 1123 | break; |
| 1124 | } |
| 1125 | if (rc != LY_SUCCESS) { |
| 1126 | break; |
| 1127 | } |
| 1128 | |
| 1129 | --i; |
| 1130 | } |
| 1131 | break; |
| 1132 | } |
| 1133 | |
| 1134 | return buffer; |
| 1135 | } |