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" |
| 31 | |
Radek Krejci | 576b23f | 2019-07-12 14:06:32 +0200 | [diff] [blame] | 32 | API void |
| 33 | lyd_trees_free(const struct lyd_node **trees, int free_data) |
| 34 | { |
| 35 | if (!trees) { |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | if (free_data) { |
| 40 | unsigned int u; |
| 41 | LY_ARRAY_FOR(trees, u) { |
| 42 | lyd_free_all((struct lyd_node *)trees[u]); |
| 43 | } |
| 44 | } |
| 45 | LY_ARRAY_FREE(trees); |
| 46 | } |
| 47 | |
| 48 | static const struct lyd_node * |
| 49 | lyd_trees_getstart(const struct lyd_node *tree) |
| 50 | { |
| 51 | if (!tree) { |
| 52 | return NULL; |
| 53 | } |
| 54 | while (tree->prev->next) { |
| 55 | tree = tree->prev; |
| 56 | } |
| 57 | return tree; |
| 58 | } |
| 59 | |
| 60 | API const struct lyd_node ** |
| 61 | lyd_trees_new(size_t count, const struct lyd_node *tree, ...) |
| 62 | { |
| 63 | LY_ERR ret; |
| 64 | const struct lyd_node **trees = NULL; |
| 65 | va_list ap; |
| 66 | |
| 67 | LY_CHECK_ARG_RET(NULL, tree, count > 0, NULL); |
| 68 | |
| 69 | va_start(ap, tree); |
| 70 | |
| 71 | LY_ARRAY_CREATE_GOTO(tree->schema->module->ctx, trees, count, ret, error); |
| 72 | /* first, mandatory, tree to insert */ |
| 73 | trees[0] = lyd_trees_getstart(tree); |
| 74 | LY_ARRAY_INCREMENT(trees); |
| 75 | |
| 76 | /* variable arguments */ |
| 77 | for (unsigned int u = 1; u < count; ++u) { |
| 78 | trees[u] = lyd_trees_getstart(va_arg(ap, const struct lyd_node *)); |
| 79 | LY_ARRAY_INCREMENT(trees); |
| 80 | } |
| 81 | |
| 82 | va_end(ap); |
| 83 | return trees; |
| 84 | |
| 85 | error: |
| 86 | (void)ret; /* unused */ |
| 87 | lyd_trees_free(trees, 1); |
| 88 | va_end(ap); |
| 89 | return NULL; |
| 90 | } |
| 91 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 92 | static int |
Radek Krejci | 357398c | 2019-05-27 14:15:01 +0200 | [diff] [blame] | 93 | cmp_str(const char *refstr, const char *str, size_t str_len) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 94 | { |
| 95 | |
Radek Krejci | 357398c | 2019-05-27 14:15:01 +0200 | [diff] [blame] | 96 | if (str_len) { |
| 97 | int r = strncmp(refstr, str, str_len); |
| 98 | if (!r && !refstr[str_len]) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 99 | return 0; |
| 100 | } else { |
| 101 | return 1; |
| 102 | } |
| 103 | } else { |
| 104 | return strcmp(refstr, str); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | API const struct lyd_node * |
| 109 | lyd_search(const struct lyd_node *first, const struct lys_module *module, |
| 110 | const char *name, size_t name_len, uint16_t nodetype, const char *value, size_t value_len) |
| 111 | { |
| 112 | const struct lyd_node *node = NULL; |
| 113 | const struct lysc_node *snode; |
| 114 | |
| 115 | LY_CHECK_ARG_RET(NULL, module, name, NULL); |
| 116 | if (!nodetype) { |
| 117 | nodetype = 0xffff; |
| 118 | } |
| 119 | |
| 120 | LY_LIST_FOR(first, node) { |
| 121 | snode = node->schema; |
| 122 | if (!(snode->nodetype & nodetype)) { |
| 123 | continue; |
| 124 | } |
| 125 | if (snode->module != module) { |
| 126 | continue; |
| 127 | } |
| 128 | |
| 129 | if (cmp_str(snode->name, name, name_len)) { |
| 130 | continue; |
| 131 | } |
| 132 | |
| 133 | if (value) { |
| 134 | if (snode->nodetype == LYS_LIST) { |
| 135 | /* TODO handle value as keys of the list instance */ |
| 136 | } else if (snode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 137 | if (cmp_str(((struct lyd_node_term*)node)->value.canonized, value, value_len)) { |
| 138 | continue; |
| 139 | } |
| 140 | } else { |
| 141 | continue; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /* all criteria passed */ |
| 146 | return node; |
| 147 | } |
| 148 | return NULL; |
| 149 | } |
| 150 | |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 151 | LY_ERR |
Radek Krejci | 3c9758d | 2019-07-11 16:49:10 +0200 | [diff] [blame] | 152 | 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] | 153 | 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] | 154 | { |
| 155 | LY_ERR ret = LY_SUCCESS, rc; |
| 156 | struct ly_err_item *err = NULL; |
| 157 | struct ly_ctx *ctx; |
| 158 | struct lysc_type *type; |
Radek Krejci | 3c9758d | 2019-07-11 16:49:10 +0200 | [diff] [blame] | 159 | int options = LY_TYPE_OPTS_STORE | (second ? LY_TYPE_OPTS_SECOND_CALL : 0) | |
| 160 | (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] | 161 | assert(node); |
| 162 | |
| 163 | ctx = node->schema->module->ctx; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 164 | |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 165 | type = ((struct lysc_node_leaf*)node->schema)->type; |
Radek Krejci | 62903c3 | 2019-07-15 14:42:05 +0200 | [diff] [blame] | 166 | if (!second) { |
| 167 | node->value.realtype = type; |
| 168 | } |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 169 | rc = type->plugin->store(ctx, type, value, value_len, options, get_prefix, parser, format, |
| 170 | trees ? (void*)node : (void*)node->schema, trees, |
| 171 | &node->value, NULL, &err); |
| 172 | if (rc == LY_EINCOMPLETE) { |
| 173 | ret = rc; |
| 174 | /* continue with storing, just remember what to return if storing is ok */ |
| 175 | } else if (rc) { |
| 176 | ret = rc; |
| 177 | if (err) { |
| 178 | ly_err_print(err); |
| 179 | LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg); |
| 180 | ly_err_free(err); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 181 | } |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 182 | goto error; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | error: |
| 186 | return ret; |
| 187 | } |
| 188 | |
| 189 | API LY_ERR |
| 190 | lys_value_validate(struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len, |
| 191 | ly_clb_resolve_prefix get_prefix, void *get_prefix_data, LYD_FORMAT format) |
| 192 | { |
| 193 | LY_ERR rc = LY_SUCCESS; |
| 194 | struct ly_err_item *err = NULL; |
| 195 | struct lysc_type *type; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 196 | |
| 197 | LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL); |
| 198 | |
| 199 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 200 | LOGARG(ctx, node); |
| 201 | return LY_EINVAL; |
| 202 | } |
| 203 | |
| 204 | type = ((struct lysc_node_leaf*)node)->type; |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 205 | /* just validate, no storing of enything */ |
| 206 | rc = type->plugin->store(ctx ? ctx : node->module->ctx, type, value, value_len, LY_TYPE_OPTS_INCOMPLETE_DATA, |
| 207 | get_prefix, get_prefix_data, format, node, NULL, NULL, NULL, &err); |
| 208 | if (rc == LY_EINCOMPLETE) { |
| 209 | /* actually success since we do not provide the context tree and call validation with |
| 210 | * LY_TYPE_OPTS_INCOMPLETE_DATA */ |
| 211 | rc = LY_SUCCESS; |
| 212 | } else if (rc && err) { |
| 213 | if (ctx) { |
| 214 | /* log only in case the ctx was provided as input parameter */ |
| 215 | ly_err_print(err); |
| 216 | LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 217 | } |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 218 | ly_err_free(err); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | return rc; |
| 222 | } |
| 223 | |
| 224 | API LY_ERR |
| 225 | 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] | 226 | 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] | 227 | { |
| 228 | LY_ERR rc; |
| 229 | struct ly_err_item *err = NULL; |
| 230 | struct lysc_type *type; |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 231 | int options = (trees ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA); |
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 | type = ((struct lysc_node_leaf*)node->schema)->type; |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 236 | rc = type->plugin->store(ctx ? ctx : node->schema->module->ctx, type, value, value_len, options, |
| 237 | get_prefix, get_prefix_data, format, trees ? (void*)node : (void*)node->schema, trees, |
| 238 | NULL, NULL, &err); |
| 239 | if (rc == LY_EINCOMPLETE) { |
| 240 | return rc; |
| 241 | } else if (rc) { |
| 242 | if (err) { |
| 243 | if (ctx) { |
| 244 | ly_err_print(err); |
| 245 | LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 246 | } |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 247 | ly_err_free(err); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 248 | } |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 249 | return rc; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | return LY_SUCCESS; |
| 253 | } |
| 254 | |
| 255 | API LY_ERR |
| 256 | 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] | 257 | 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] | 258 | { |
| 259 | LY_ERR ret = LY_SUCCESS, rc; |
| 260 | struct ly_err_item *err = NULL; |
| 261 | struct ly_ctx *ctx; |
| 262 | struct lysc_type *type; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 263 | struct lyd_value data = {0}; |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 264 | 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] | 265 | |
| 266 | LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, value, LY_EINVAL); |
| 267 | |
| 268 | ctx = node->schema->module->ctx; |
| 269 | type = ((struct lysc_node_leaf*)node->schema)->type; |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 270 | rc = type->plugin->store(ctx, type, value, value_len, options, get_prefix, get_prefix_data, format, (struct lyd_node*)node, |
| 271 | trees, &data, NULL, &err); |
| 272 | if (rc == LY_EINCOMPLETE) { |
| 273 | ret = rc; |
| 274 | /* continue with comparing, just remember what to return if storing is ok */ |
| 275 | } else if (rc) { |
| 276 | /* value to compare is invalid */ |
| 277 | ret = LY_EINVAL; |
| 278 | if (err) { |
| 279 | ly_err_free(err); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 280 | } |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 281 | goto cleanup; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /* compare data */ |
Radek Krejci | 5af0484 | 2019-07-12 11:32:07 +0200 | [diff] [blame] | 285 | if (type->plugin->compare(&node->value, &data)) { |
| 286 | /* do not assign it directly from the compare callback to keep possible LY_EINCOMPLETE from validation */ |
| 287 | ret = LY_EVALID; |
| 288 | } |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 289 | |
| 290 | cleanup: |
Radek Krejci | 62903c3 | 2019-07-15 14:42:05 +0200 | [diff] [blame] | 291 | type->plugin->free(ctx, &data); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 292 | |
| 293 | return ret; |
| 294 | } |
| 295 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 296 | static struct lyd_node * |
| 297 | lyd_parse_mem_(struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int options, va_list ap) |
| 298 | { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 299 | struct lyd_node *result = NULL; |
| 300 | const struct lyd_node *rpc_act = NULL, *data_tree = NULL, *iter; |
Radek Krejci | e92210c | 2019-05-17 15:53:35 +0200 | [diff] [blame] | 301 | #if 0 |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 302 | const char *yang_data_name = NULL; |
Radek Krejci | e92210c | 2019-05-17 15:53:35 +0200 | [diff] [blame] | 303 | #endif |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 304 | |
| 305 | if (lyd_parse_check_options(ctx, options, __func__)) { |
| 306 | return NULL; |
| 307 | } |
| 308 | |
| 309 | if (options & LYD_OPT_RPCREPLY) { |
| 310 | rpc_act = va_arg(ap, const struct lyd_node *); |
| 311 | if (!rpc_act || rpc_act->parent || !(rpc_act->schema->nodetype & (LYS_ACTION | LYS_LIST | LYS_CONTAINER))) { |
| 312 | LOGERR(ctx, LY_EINVAL, "Data parser invalid variable parameter (const struct lyd_node *rpc_act)."); |
| 313 | return NULL; |
| 314 | } |
| 315 | } |
| 316 | if (options & (LYD_OPT_RPC | LYD_OPT_NOTIF | LYD_OPT_RPCREPLY)) { |
| 317 | data_tree = va_arg(ap, const struct lyd_node *); |
| 318 | if (data_tree) { |
| 319 | if (options & LYD_OPT_NOEXTDEPS) { |
| 320 | LOGERR(ctx, LY_EINVAL, "%s: invalid parameter (variable arg const struct lyd_node *data_tree and LYD_OPT_NOEXTDEPS set).", |
| 321 | __func__); |
| 322 | return NULL; |
| 323 | } |
| 324 | |
| 325 | LY_LIST_FOR(data_tree, iter) { |
| 326 | if (iter->parent) { |
| 327 | /* a sibling is not top-level */ |
| 328 | LOGERR(ctx, LY_EINVAL, "%s: invalid variable parameter (const struct lyd_node *data_tree).", __func__); |
| 329 | return NULL; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | /* move it to the beginning */ |
| 334 | for (; data_tree->prev->next; data_tree = data_tree->prev); |
| 335 | |
| 336 | /* LYD_OPT_NOSIBLINGS cannot be set in this case */ |
| 337 | if (options & LYD_OPT_NOSIBLINGS) { |
| 338 | LOGERR(ctx, LY_EINVAL, "%s: invalid parameter (variable arg const struct lyd_node *data_tree with LYD_OPT_NOSIBLINGS).", __func__); |
| 339 | return NULL; |
| 340 | } |
| 341 | } |
| 342 | } |
Radek Krejci | e92210c | 2019-05-17 15:53:35 +0200 | [diff] [blame] | 343 | #if 0 |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 344 | if (options & LYD_OPT_DATA_TEMPLATE) { |
| 345 | yang_data_name = va_arg(ap, const char *); |
| 346 | } |
Radek Krejci | e92210c | 2019-05-17 15:53:35 +0200 | [diff] [blame] | 347 | #endif |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 348 | |
| 349 | if (!format) { |
| 350 | /* TODO try to detect format from the content */ |
| 351 | } |
| 352 | |
| 353 | switch (format) { |
| 354 | case LYD_XML: |
Radek Krejci | e92210c | 2019-05-17 15:53:35 +0200 | [diff] [blame] | 355 | lyd_parse_xml(ctx, data, options, &result); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 356 | break; |
| 357 | #if 0 |
| 358 | case LYD_JSON: |
Radek Krejci | e92210c | 2019-05-17 15:53:35 +0200 | [diff] [blame] | 359 | lyd_parse_json(ctx, data, options, rpc_act, data_tree, yang_data_name); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 360 | break; |
| 361 | case LYD_LYB: |
Radek Krejci | e92210c | 2019-05-17 15:53:35 +0200 | [diff] [blame] | 362 | lyd_parse_lyb(ctx, data, options, data_tree, yang_data_name, NULL); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 363 | break; |
| 364 | #endif |
| 365 | case LYD_UNKNOWN: |
| 366 | LOGINT(ctx); |
| 367 | break; |
| 368 | } |
| 369 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 370 | return result; |
| 371 | } |
| 372 | |
| 373 | API struct lyd_node * |
| 374 | lyd_parse_mem(struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int options, ...) |
| 375 | { |
| 376 | va_list ap; |
| 377 | struct lyd_node *result; |
| 378 | |
| 379 | va_start(ap, options); |
| 380 | result = lyd_parse_mem_(ctx, data, format, options, ap); |
| 381 | va_end(ap); |
| 382 | |
| 383 | return result; |
| 384 | } |
| 385 | |
| 386 | static struct lyd_node * |
| 387 | lyd_parse_fd_(struct ly_ctx *ctx, int fd, LYD_FORMAT format, int options, va_list ap) |
| 388 | { |
| 389 | struct lyd_node *result; |
| 390 | size_t length; |
| 391 | char *addr; |
| 392 | |
| 393 | LY_CHECK_ARG_RET(ctx, ctx, NULL); |
| 394 | if (fd < 0) { |
| 395 | LOGARG(ctx, fd); |
| 396 | return NULL; |
| 397 | } |
| 398 | |
| 399 | LY_CHECK_RET(ly_mmap(ctx, fd, &length, (void **)&addr), NULL); |
| 400 | result = lyd_parse_mem_(ctx, addr ? addr : "", format, options, ap); |
Radek Krejci | df3da79 | 2019-05-17 10:32:24 +0200 | [diff] [blame] | 401 | if (addr) { |
| 402 | ly_munmap(addr, length); |
| 403 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 404 | |
| 405 | return result; |
| 406 | } |
| 407 | |
| 408 | API struct lyd_node * |
| 409 | lyd_parse_fd(struct ly_ctx *ctx, int fd, LYD_FORMAT format, int options, ...) |
| 410 | { |
| 411 | struct lyd_node *ret; |
| 412 | va_list ap; |
| 413 | |
| 414 | va_start(ap, options); |
| 415 | ret = lyd_parse_fd_(ctx, fd, format, options, ap); |
| 416 | va_end(ap); |
| 417 | |
| 418 | return ret; |
| 419 | } |
| 420 | |
| 421 | API struct lyd_node * |
| 422 | lyd_parse_path(struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int options, ...) |
| 423 | { |
| 424 | int fd; |
| 425 | struct lyd_node *result; |
| 426 | size_t len; |
| 427 | va_list ap; |
| 428 | |
| 429 | LY_CHECK_ARG_RET(ctx, ctx, path, NULL); |
| 430 | |
| 431 | fd = open(path, O_RDONLY); |
| 432 | LY_CHECK_ERR_RET(fd == -1, LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)), NULL); |
| 433 | |
| 434 | if (!format) { |
| 435 | /* unknown format - try to detect it from filename's suffix */ |
| 436 | len = strlen(path); |
| 437 | |
| 438 | /* ignore trailing whitespaces */ |
| 439 | for (; len > 0 && isspace(path[len - 1]); len--); |
| 440 | |
| 441 | if (len >= 5 && !strncmp(&path[len - 4], ".xml", 4)) { |
| 442 | format = LYD_XML; |
| 443 | #if 0 |
| 444 | } else if (len >= 6 && !strncmp(&path[len - 5], ".json", 5)) { |
| 445 | format = LYD_JSON; |
| 446 | } else if (len >= 5 && !strncmp(&path[len - 4], ".lyb", 4)) { |
| 447 | format = LYD_LYB; |
| 448 | #endif |
| 449 | } /* else still unknown, try later to detect it from the content */ |
| 450 | } |
| 451 | |
| 452 | va_start(ap, options); |
| 453 | result = lyd_parse_fd_(ctx, fd, format, options, ap); |
| 454 | |
| 455 | va_end(ap); |
| 456 | close(fd); |
| 457 | |
| 458 | return result; |
| 459 | } |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 460 | |
| 461 | API const struct lyd_node_term * |
Radek Krejci | 576b23f | 2019-07-12 14:06:32 +0200 | [diff] [blame] | 462 | lyd_target(struct lyd_value_path *path, const struct lyd_node **trees) |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 463 | { |
| 464 | unsigned int u, v, x; |
| 465 | const struct lyd_node *node = NULL, *parent = NULL, *start_search; |
| 466 | uint64_t pos = 1; |
| 467 | |
| 468 | LY_CHECK_ARG_RET(NULL, path, trees, NULL); |
| 469 | |
| 470 | LY_ARRAY_FOR(path, u) { |
| 471 | if (parent) { |
| 472 | start_search = lyd_node_children(parent); |
| 473 | search_inner: |
| 474 | node = lyd_search(start_search, path[u].node->module, path[u].node->name, strlen(path[u].node->name), path[u].node->nodetype, NULL, 0); |
| 475 | } else { |
| 476 | LY_ARRAY_FOR(trees, v) { |
| 477 | start_search = trees[v]; |
| 478 | search_toplevel: |
| 479 | /* WARNING! to use search_toplevel label correctly, variable v must be preserved and not changed! */ |
| 480 | node = lyd_search(start_search, path[u].node->module, path[u].node->name, strlen(path[u].node->name), path[u].node->nodetype, NULL, 0); |
| 481 | if (node) { |
| 482 | break; |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | if (!node) { |
| 487 | return NULL; |
| 488 | } |
| 489 | |
| 490 | /* check predicate if any */ |
| 491 | LY_ARRAY_FOR(path[u].predicates, x) { |
| 492 | if (path[u].predicates[x].type == 0) { |
| 493 | /* position predicate */ |
| 494 | if (pos != path[u].predicates[x].position) { |
| 495 | pos++; |
| 496 | goto search_repeat; |
| 497 | } |
| 498 | /* done, no more predicates are allowed here */ |
| 499 | break; |
| 500 | } else if (path[u].predicates[x].type == 1) { |
| 501 | /* key-predicate */ |
| 502 | struct lysc_type *type = ((struct lysc_node_leaf*)path[u].predicates[x].key)->type; |
| 503 | const struct lyd_node *key = lyd_search(lyd_node_children(node), path[u].predicates[x].key->module, |
| 504 | path[u].predicates[x].key->name, strlen(path[u].predicates[x].key->name), |
| 505 | LYS_LEAF, NULL, 0); |
| 506 | if (!key) { |
| 507 | /* probably error and we shouldn't be here due to previous checks when creating path */ |
| 508 | goto search_repeat; |
| 509 | } |
| 510 | if (type->plugin->compare(&((struct lyd_node_term*)key)->value, path[u].predicates[x].value)) { |
| 511 | goto search_repeat; |
| 512 | } |
| 513 | } else if (path[u].predicates[x].type == 2) { |
| 514 | /* leaf-list-predicate */ |
| 515 | struct lysc_type *type = ((struct lysc_node_leaf*)path[u].node)->type; |
| 516 | if (type->plugin->compare(&((struct lyd_node_term*)node)->value, path[u].predicates[x].value)) { |
| 517 | goto search_repeat; |
| 518 | } |
| 519 | } else { |
| 520 | LOGINT(NULL); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | parent = node; |
| 525 | } |
| 526 | |
| 527 | return (const struct lyd_node_term*)node; |
| 528 | |
| 529 | search_repeat: |
| 530 | start_search = node->next; |
| 531 | if (parent) { |
| 532 | goto search_inner; |
| 533 | } else { |
| 534 | goto search_toplevel; |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | |