Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file tree_data_helpers.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief Parsing and validation helper functions for data trees |
| 5 | * |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 6 | * Copyright (c) 2015 - 2022 CESNET, z.s.p.o. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 14 | |
| 15 | #define _GNU_SOURCE /* asprintf, strdup */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 16 | |
| 17 | #include <assert.h> |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 18 | #include <ctype.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 19 | #include <stdint.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 20 | #include <stdlib.h> |
Radek Krejci | ad97c5f | 2020-06-30 09:19:28 +0200 | [diff] [blame] | 21 | #include <string.h> |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 22 | #include <time.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 23 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 24 | #include "common.h" |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 25 | #include "compat.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 26 | #include "context.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 27 | #include "dict.h" |
Radek Krejci | ad97c5f | 2020-06-30 09:19:28 +0200 | [diff] [blame] | 28 | #include "hash_table.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 29 | #include "log.h" |
Radek Krejci | ad97c5f | 2020-06-30 09:19:28 +0200 | [diff] [blame] | 30 | #include "lyb.h" |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 31 | #include "parser_data.h" |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 32 | #include "plugins_exts.h" |
Michal Vasko | a820c31 | 2021-02-05 16:33:00 +0100 | [diff] [blame] | 33 | #include "printer_data.h" |
Radek Krejci | ad97c5f | 2020-06-30 09:19:28 +0200 | [diff] [blame] | 34 | #include "set.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 35 | #include "tree.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 36 | #include "tree_data.h" |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 37 | #include "tree_data_internal.h" |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 38 | #include "tree_edit.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 39 | #include "tree_schema.h" |
Radek Krejci | 0aa1f70 | 2021-04-01 16:16:19 +0200 | [diff] [blame] | 40 | #include "tree_schema_internal.h" |
Radek Krejci | 4f2e3e5 | 2021-03-30 14:20:28 +0200 | [diff] [blame] | 41 | #include "validation.h" |
Radek Krejci | 7711410 | 2021-03-10 15:21:57 +0100 | [diff] [blame] | 42 | #include "xml.h" |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 43 | #include "xpath.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 44 | |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 45 | /** |
| 46 | * @brief Find an entry in duplicate instance cache for an instance. Create it if it does not exist. |
| 47 | * |
| 48 | * @param[in] first_inst Instance of the cache entry. |
| 49 | * @param[in,out] dup_inst_cache Duplicate instance cache. |
| 50 | * @return Instance cache entry. |
| 51 | */ |
| 52 | static struct lyd_dup_inst * |
| 53 | lyd_dup_inst_get(const struct lyd_node *first_inst, struct lyd_dup_inst **dup_inst_cache) |
| 54 | { |
| 55 | struct lyd_dup_inst *item; |
| 56 | LY_ARRAY_COUNT_TYPE u; |
| 57 | |
| 58 | LY_ARRAY_FOR(*dup_inst_cache, u) { |
| 59 | if ((*dup_inst_cache)[u].inst_set->dnodes[0] == first_inst) { |
| 60 | return &(*dup_inst_cache)[u]; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /* it was not added yet, add it now */ |
| 65 | LY_ARRAY_NEW_RET(LYD_CTX(first_inst), *dup_inst_cache, item, NULL); |
| 66 | |
| 67 | return item; |
| 68 | } |
| 69 | |
| 70 | LY_ERR |
| 71 | lyd_dup_inst_next(struct lyd_node **inst, const struct lyd_node *siblings, struct lyd_dup_inst **dup_inst_cache) |
| 72 | { |
| 73 | struct lyd_dup_inst *dup_inst; |
| 74 | |
| 75 | if (!*inst || !lysc_is_dup_inst_list((*inst)->schema)) { |
| 76 | /* no match or not dup-inst list, inst is unchanged */ |
| 77 | return LY_SUCCESS; |
| 78 | } |
| 79 | |
| 80 | /* there can be more exact same instances and we must make sure we do not match a single node more times */ |
| 81 | dup_inst = lyd_dup_inst_get(*inst, dup_inst_cache); |
| 82 | LY_CHECK_ERR_RET(!dup_inst, LOGMEM(LYD_CTX(siblings)), LY_EMEM); |
| 83 | |
| 84 | if (!dup_inst->used) { |
| 85 | /* we did not cache these instances yet, do so */ |
| 86 | lyd_find_sibling_dup_inst_set(siblings, *inst, &dup_inst->inst_set); |
| 87 | assert(dup_inst->inst_set->count && (dup_inst->inst_set->dnodes[0] == *inst)); |
| 88 | } |
| 89 | |
| 90 | if (dup_inst->used == dup_inst->inst_set->count) { |
| 91 | /* we have used all the instances */ |
| 92 | *inst = NULL; |
| 93 | } else { |
| 94 | assert(dup_inst->used < dup_inst->inst_set->count); |
| 95 | |
| 96 | /* use another instance */ |
| 97 | *inst = dup_inst->inst_set->dnodes[dup_inst->used]; |
| 98 | ++dup_inst->used; |
| 99 | } |
| 100 | |
| 101 | return LY_SUCCESS; |
| 102 | } |
| 103 | |
| 104 | void |
| 105 | lyd_dup_inst_free(struct lyd_dup_inst *dup_inst) |
| 106 | { |
| 107 | LY_ARRAY_COUNT_TYPE u; |
| 108 | |
| 109 | LY_ARRAY_FOR(dup_inst, u) { |
| 110 | ly_set_free(dup_inst[u].inst_set, NULL); |
| 111 | } |
| 112 | LY_ARRAY_FREE(dup_inst); |
| 113 | } |
| 114 | |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 115 | struct lyd_node * |
| 116 | lys_getnext_data(const struct lyd_node *last, const struct lyd_node *sibling, const struct lysc_node **slast, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 117 | const struct lysc_node *parent, const struct lysc_module *module) |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 118 | { |
| 119 | const struct lysc_node *siter = NULL; |
| 120 | struct lyd_node *match = NULL; |
| 121 | |
| 122 | assert(parent || module); |
| 123 | assert(!last || (slast && *slast)); |
| 124 | |
| 125 | if (slast) { |
| 126 | siter = *slast; |
| 127 | } |
| 128 | |
| 129 | if (last && last->next && (last->next->schema == siter)) { |
| 130 | /* return next data instance */ |
| 131 | return last->next; |
| 132 | } |
| 133 | |
| 134 | /* find next schema node data instance */ |
| 135 | while ((siter = lys_getnext(siter, parent, module, 0))) { |
| 136 | if (!lyd_find_sibling_val(sibling, siter, NULL, 0, &match)) { |
| 137 | break; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | if (slast) { |
| 142 | *slast = siter; |
| 143 | } |
| 144 | return match; |
| 145 | } |
| 146 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 147 | struct lyd_node ** |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 148 | lyd_node_child_p(struct lyd_node *node) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 149 | { |
| 150 | assert(node); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 151 | |
| 152 | if (!node->schema) { |
| 153 | return &((struct lyd_node_opaq *)node)->child; |
| 154 | } else { |
| 155 | switch (node->schema->nodetype) { |
| 156 | case LYS_CONTAINER: |
| 157 | case LYS_LIST: |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 158 | case LYS_RPC: |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 159 | case LYS_ACTION: |
| 160 | case LYS_NOTIF: |
| 161 | return &((struct lyd_node_inner *)node)->child; |
| 162 | default: |
| 163 | return NULL; |
| 164 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 168 | LIBYANG_API_DEF LY_ERR |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 169 | lyxp_vars_set(struct lyxp_var **vars, const char *name, const char *value) |
| 170 | { |
| 171 | LY_ERR ret = LY_SUCCESS; |
| 172 | char *var_name = NULL, *var_value = NULL; |
| 173 | struct lyxp_var *item; |
| 174 | |
| 175 | if (!vars || !name || !value) { |
| 176 | return LY_EINVAL; |
| 177 | } |
| 178 | |
| 179 | /* If variable is already defined then change its value. */ |
| 180 | if (*vars && !lyxp_vars_find(*vars, name, 0, &item)) { |
| 181 | var_value = strdup(value); |
| 182 | LY_CHECK_RET(!var_value, LY_EMEM); |
| 183 | |
| 184 | /* Set new value. */ |
| 185 | free(item->value); |
| 186 | item->value = var_value; |
| 187 | } else { |
| 188 | var_name = strdup(name); |
| 189 | var_value = strdup(value); |
| 190 | LY_CHECK_ERR_GOTO(!var_name || !var_value, ret = LY_EMEM, error); |
| 191 | |
| 192 | /* Add new variable. */ |
| 193 | LY_ARRAY_NEW_GOTO(NULL, *vars, item, ret, error); |
| 194 | item->name = var_name; |
| 195 | item->value = var_value; |
| 196 | } |
| 197 | |
| 198 | return LY_SUCCESS; |
| 199 | |
| 200 | error: |
| 201 | free(var_name); |
| 202 | free(var_value); |
| 203 | return ret; |
| 204 | } |
| 205 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 206 | LIBYANG_API_DEF void |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 207 | lyxp_vars_free(struct lyxp_var *vars) |
| 208 | { |
| 209 | LY_ARRAY_COUNT_TYPE u; |
| 210 | |
| 211 | if (!vars) { |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | LY_ARRAY_FOR(vars, u) { |
| 216 | free(vars[u].name); |
| 217 | free(vars[u].value); |
| 218 | } |
| 219 | |
| 220 | LY_ARRAY_FREE(vars); |
| 221 | } |
| 222 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 223 | LIBYANG_API_DEF struct lyd_node * |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 224 | lyd_child_no_keys(const struct lyd_node *node) |
| 225 | { |
| 226 | struct lyd_node **children; |
| 227 | |
| 228 | if (!node) { |
| 229 | return NULL; |
| 230 | } |
| 231 | |
| 232 | if (!node->schema) { |
| 233 | /* opaq node */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 234 | return ((struct lyd_node_opaq *)node)->child; |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 235 | } |
| 236 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 237 | children = lyd_node_child_p((struct lyd_node *)node); |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 238 | if (children) { |
| 239 | struct lyd_node *child = *children; |
| 240 | while (child && child->schema && (child->schema->flags & LYS_KEY)) { |
| 241 | child = child->next; |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 242 | } |
| 243 | return child; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 244 | } else { |
| 245 | return NULL; |
| 246 | } |
| 247 | } |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 248 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 249 | LIBYANG_API_DEF const struct lys_module * |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 250 | lyd_owner_module(const struct lyd_node *node) |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 251 | { |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 252 | const struct lyd_node_opaq *opaq; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 253 | |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 254 | if (!node) { |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 255 | return NULL; |
| 256 | } |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 257 | |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 258 | if (!node->schema) { |
| 259 | opaq = (struct lyd_node_opaq *)node; |
| 260 | switch (opaq->format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 261 | case LY_VALUE_XML: |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 262 | return ly_ctx_get_module_implemented_ns(LYD_CTX(node), opaq->name.module_ns); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 263 | case LY_VALUE_JSON: |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 264 | return ly_ctx_get_module_implemented(LYD_CTX(node), opaq->name.module_name); |
| 265 | default: |
| 266 | return NULL; |
| 267 | } |
| 268 | } |
| 269 | |
Michal Vasko | ef53c81 | 2021-10-13 10:21:03 +0200 | [diff] [blame] | 270 | return lysc_owner_module(node->schema); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 271 | } |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 272 | |
Michal Vasko | 598063b | 2021-07-19 11:39:05 +0200 | [diff] [blame] | 273 | void |
| 274 | lyd_first_module_sibling(struct lyd_node **node, const struct lys_module *mod) |
| 275 | { |
| 276 | int cmp; |
| 277 | struct lyd_node *first; |
Michal Vasko | ec139eb | 2022-05-10 10:08:40 +0200 | [diff] [blame^] | 278 | const struct lys_module *own_mod; |
Michal Vasko | 598063b | 2021-07-19 11:39:05 +0200 | [diff] [blame] | 279 | |
| 280 | assert(node && mod); |
| 281 | |
| 282 | if (!*node) { |
| 283 | return; |
| 284 | } |
| 285 | |
| 286 | first = *node; |
Michal Vasko | ec139eb | 2022-05-10 10:08:40 +0200 | [diff] [blame^] | 287 | own_mod = lyd_owner_module(first); |
| 288 | cmp = own_mod ? strcmp(own_mod->name, mod->name) : 1; |
Michal Vasko | 598063b | 2021-07-19 11:39:05 +0200 | [diff] [blame] | 289 | if (cmp > 0) { |
| 290 | /* there may be some preceding data */ |
| 291 | while (first->prev->next) { |
| 292 | first = first->prev; |
| 293 | if (lyd_owner_module(first) == mod) { |
| 294 | cmp = 0; |
| 295 | break; |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | if (cmp == 0) { |
| 301 | /* there may be some preceding data belonging to this module */ |
| 302 | while (first->prev->next) { |
| 303 | if (lyd_owner_module(first->prev) != mod) { |
| 304 | break; |
| 305 | } |
| 306 | first = first->prev; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | if (cmp < 0) { |
| 311 | /* there may be some following data */ |
| 312 | LY_LIST_FOR(first, first) { |
| 313 | if (lyd_owner_module(first) == mod) { |
| 314 | cmp = 0; |
| 315 | break; |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | if (cmp == 0) { |
| 321 | /* we have found the first module data node */ |
| 322 | *node = first; |
| 323 | } |
| 324 | } |
| 325 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 326 | const struct lys_module * |
Michal Vasko | 26e8001 | 2020-07-08 10:55:46 +0200 | [diff] [blame] | 327 | lyd_mod_next_module(struct lyd_node *tree, const struct lys_module *module, const struct ly_ctx *ctx, uint32_t *i, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 328 | struct lyd_node **first) |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 329 | { |
| 330 | struct lyd_node *iter; |
| 331 | const struct lys_module *mod; |
| 332 | |
| 333 | /* get the next module */ |
Michal Vasko | 26e8001 | 2020-07-08 10:55:46 +0200 | [diff] [blame] | 334 | if (module) { |
| 335 | if (*i) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 336 | mod = NULL; |
Michal Vasko | 26e8001 | 2020-07-08 10:55:46 +0200 | [diff] [blame] | 337 | } else { |
| 338 | mod = module; |
| 339 | ++(*i); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 340 | } |
| 341 | } else { |
| 342 | do { |
| 343 | mod = ly_ctx_get_module_iter(ctx, i); |
| 344 | } while (mod && !mod->implemented); |
| 345 | } |
| 346 | |
| 347 | /* find its data */ |
| 348 | *first = NULL; |
| 349 | if (mod) { |
| 350 | LY_LIST_FOR(tree, iter) { |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 351 | if (lyd_owner_module(iter) == mod) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 352 | *first = iter; |
| 353 | break; |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | return mod; |
| 359 | } |
| 360 | |
| 361 | const struct lys_module * |
| 362 | lyd_data_next_module(struct lyd_node **next, struct lyd_node **first) |
| 363 | { |
| 364 | const struct lys_module *mod; |
| 365 | |
| 366 | if (!*next) { |
| 367 | /* all data traversed */ |
| 368 | *first = NULL; |
| 369 | return NULL; |
| 370 | } |
| 371 | |
| 372 | *first = *next; |
| 373 | |
| 374 | /* prepare next */ |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 375 | mod = lyd_owner_module(*next); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 376 | LY_LIST_FOR(*next, *next) { |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 377 | if (lyd_owner_module(*next) != mod) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 378 | break; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | return mod; |
| 383 | } |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 384 | |
| 385 | LY_ERR |
| 386 | lyd_parse_check_keys(struct lyd_node *node) |
| 387 | { |
| 388 | const struct lysc_node *skey = NULL; |
| 389 | const struct lyd_node *key; |
| 390 | |
| 391 | assert(node->schema->nodetype == LYS_LIST); |
| 392 | |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 393 | key = lyd_child(node); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 394 | while ((skey = lys_getnext(skey, node->schema, NULL, 0)) && (skey->flags & LYS_KEY)) { |
| 395 | if (!key || (key->schema != skey)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 396 | LOGVAL(LYD_CTX(node), LY_VCODE_NOKEY, skey->name); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 397 | return LY_EVALID; |
| 398 | } |
| 399 | |
| 400 | key = key->next; |
| 401 | } |
| 402 | |
| 403 | return LY_SUCCESS; |
| 404 | } |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 405 | |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 406 | LY_ERR |
| 407 | lyd_parse_set_data_flags(struct lyd_node *node, struct lyd_meta **meta, struct lyd_ctx *lydctx, |
| 408 | struct lysc_ext_instance *ext) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 409 | { |
| 410 | struct lyd_meta *meta2, *prev_meta = NULL; |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 411 | struct lyd_ctx_ext_val *ext_val; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 412 | |
Michal Vasko | a5705e5 | 2020-12-09 18:15:14 +0100 | [diff] [blame] | 413 | if (lysc_has_when(node->schema)) { |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 414 | if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 415 | /* remember we need to evaluate this node's when */ |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 416 | LY_CHECK_RET(ly_set_add(&lydctx->node_when, node, 1, NULL)); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 420 | LY_LIST_FOR(*meta, meta2) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 421 | if (!strcmp(meta2->name, "default") && !strcmp(meta2->annotation->module->name, "ietf-netconf-with-defaults") && |
| 422 | meta2->value.boolean) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 423 | /* node is default according to the metadata */ |
| 424 | node->flags |= LYD_DEFAULT; |
| 425 | |
| 426 | /* delete the metadata */ |
| 427 | if (prev_meta) { |
| 428 | prev_meta->next = meta2->next; |
| 429 | } else { |
| 430 | *meta = (*meta)->next; |
| 431 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 432 | lyd_free_meta_single(meta2); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 433 | break; |
| 434 | } |
| 435 | |
| 436 | prev_meta = meta2; |
| 437 | } |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 438 | |
| 439 | if (ext) { |
| 440 | /* parsed for an extension */ |
| 441 | node->flags |= LYD_EXT; |
| 442 | |
| 443 | if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) { |
| 444 | /* rememeber for validation */ |
| 445 | ext_val = malloc(sizeof *ext_val); |
| 446 | LY_CHECK_ERR_RET(!ext_val, LOGMEM(LYD_CTX(node)), LY_EMEM); |
| 447 | ext_val->ext = ext; |
| 448 | ext_val->sibling = node; |
| 449 | LY_CHECK_RET(ly_set_add(&lydctx->ext_val, ext_val, 1, NULL)); |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | return LY_SUCCESS; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 454 | } |
| 455 | |
Michal Vasko | bfff6ac | 2022-02-23 16:22:53 +0100 | [diff] [blame] | 456 | /** |
| 457 | * @brief Check list node parsed into an opaque node for the reason. |
| 458 | * |
| 459 | * @param[in] node Opaque node. |
| 460 | * @param[in] snode Schema node of @p opaq. |
| 461 | * @return LY_SUCCESS if the node is valid; |
| 462 | * @return LY_ERR on error. |
| 463 | */ |
| 464 | static LY_ERR |
| 465 | lyd_parse_opaq_list_error(const struct lyd_node *node, const struct lysc_node *snode) |
| 466 | { |
| 467 | LY_ERR ret = LY_SUCCESS; |
| 468 | struct ly_set key_set = {0}; |
| 469 | const struct lysc_node *key = NULL; |
| 470 | const struct lyd_node *child; |
| 471 | const struct lyd_node_opaq *opaq_k; |
| 472 | uint32_t i; |
| 473 | |
| 474 | assert(!node->schema); |
| 475 | |
| 476 | /* get all keys into a set */ |
| 477 | while ((key = lys_getnext(key, snode, NULL, 0)) && (snode->flags & LYS_KEY)) { |
| 478 | LY_CHECK_GOTO(ret = ly_set_add(&key_set, (void *)snode, 1, NULL), cleanup); |
| 479 | } |
| 480 | |
| 481 | LY_LIST_FOR(lyd_child(node), child) { |
| 482 | if (child->schema) { |
| 483 | LOGERR(LYD_CTX(node), LY_EINVAL, "Unexpected node %s \"%s\".", lys_nodetype2str(child->schema->nodetype), |
| 484 | LYD_NAME(child)); |
| 485 | ret = LY_EINVAL; |
| 486 | goto cleanup; |
| 487 | } |
| 488 | |
| 489 | opaq_k = (struct lyd_node_opaq *)child; |
| 490 | |
| 491 | /* find the key schema node */ |
| 492 | for (i = 0; i < key_set.count; ++i) { |
| 493 | key = key_set.snodes[i]; |
| 494 | if (!strcmp(key->name, opaq_k->name.name)) { |
| 495 | break; |
| 496 | } |
| 497 | } |
| 498 | if (i == key_set.count) { |
| 499 | /* some other node, skip */ |
| 500 | continue; |
| 501 | } |
| 502 | |
| 503 | /* key found */ |
| 504 | ly_set_rm_index(&key_set, i, NULL); |
| 505 | |
| 506 | /* check value */ |
| 507 | ret = lys_value_validate(LYD_CTX(node), key, opaq_k->value, strlen(opaq_k->value), opaq_k->format, |
| 508 | opaq_k->val_prefix_data); |
| 509 | LY_CHECK_GOTO(ret, cleanup); |
| 510 | } |
| 511 | |
| 512 | if (key_set.count) { |
| 513 | /* missing keys */ |
| 514 | LOGVAL(LYD_CTX(node), LY_VCODE_NOKEY, key_set.snodes[0]->name); |
| 515 | ret = LY_EVALID; |
| 516 | goto cleanup; |
| 517 | } |
| 518 | |
| 519 | cleanup: |
| 520 | ly_set_erase(&key_set, NULL); |
| 521 | return ret; |
| 522 | } |
| 523 | |
| 524 | LIBYANG_API_DEF LY_ERR |
| 525 | lyd_parse_opaq_error(const struct lyd_node *node) |
| 526 | { |
| 527 | const struct ly_ctx *ctx; |
| 528 | const struct lyd_node_opaq *opaq; |
| 529 | const struct lyd_node *parent; |
| 530 | const struct lys_module *mod; |
| 531 | const struct lysc_node *snode; |
| 532 | |
| 533 | LY_CHECK_ARG_RET(LYD_CTX(node), node, !node->schema, !lyd_parent(node) || lyd_parent(node)->schema, LY_EINVAL); |
| 534 | |
| 535 | ctx = LYD_CTX(node); |
| 536 | opaq = (struct lyd_node_opaq *)node; |
| 537 | parent = lyd_parent(node); |
| 538 | |
| 539 | /* is always filled by parsers */ |
| 540 | LY_CHECK_ARG_RET(ctx, opaq->name.module_ns, LY_EINVAL); |
| 541 | |
| 542 | /* module */ |
| 543 | switch (opaq->format) { |
| 544 | case LY_VALUE_XML: |
| 545 | if (!parent || strcmp(opaq->name.module_ns, parent->schema->module->ns)) { |
| 546 | mod = ly_ctx_get_module_implemented_ns(ctx, opaq->name.module_ns); |
| 547 | if (!mod) { |
| 548 | LOGVAL(ctx, LYVE_REFERENCE, "No (implemented) module with namespace \"%s\" in the context.", |
| 549 | opaq->name.module_ns); |
| 550 | return LY_EVALID; |
| 551 | } |
| 552 | } else { |
| 553 | /* inherit */ |
| 554 | mod = parent->schema->module; |
| 555 | } |
| 556 | break; |
| 557 | case LY_VALUE_JSON: |
| 558 | case LY_VALUE_LYB: |
| 559 | if (!parent || strcmp(opaq->name.module_name, parent->schema->module->name)) { |
| 560 | mod = ly_ctx_get_module_implemented(ctx, opaq->name.module_name); |
| 561 | if (!mod) { |
| 562 | LOGVAL(ctx, LYVE_REFERENCE, "No (implemented) module named \"%s\" in the context.", opaq->name.module_name); |
| 563 | return LY_EVALID; |
| 564 | } |
| 565 | } else { |
| 566 | /* inherit */ |
| 567 | mod = parent->schema->module; |
| 568 | } |
| 569 | break; |
| 570 | default: |
| 571 | LOGERR(ctx, LY_EINVAL, "Unsupported value format."); |
| 572 | return LY_EINVAL; |
| 573 | } |
| 574 | |
| 575 | /* schema */ |
| 576 | snode = lys_find_child(parent ? parent->schema : NULL, mod, opaq->name.name, 0, 0, 0); |
Michal Vasko | ac6f4be | 2022-05-02 10:16:50 +0200 | [diff] [blame] | 577 | if (!snode && parent && parent->schema && (parent->schema->nodetype & (LYS_RPC | LYS_ACTION))) { |
| 578 | /* maybe output node */ |
Michal Vasko | 89afc6e | 2022-05-02 10:24:26 +0200 | [diff] [blame] | 579 | snode = lys_find_child(parent->schema, mod, opaq->name.name, 0, 0, LYS_GETNEXT_OUTPUT); |
Michal Vasko | ac6f4be | 2022-05-02 10:16:50 +0200 | [diff] [blame] | 580 | } |
Michal Vasko | bfff6ac | 2022-02-23 16:22:53 +0100 | [diff] [blame] | 581 | if (!snode) { |
| 582 | if (parent) { |
| 583 | LOGVAL(ctx, LYVE_REFERENCE, "Node \"%s\" not found as a child of \"%s\" node.", opaq->name.name, |
| 584 | LYD_NAME(parent)); |
| 585 | } else { |
| 586 | LOGVAL(ctx, LYVE_REFERENCE, "Node \"%s\" not found in the \"%s\" module.", opaq->name.name, mod->name); |
| 587 | } |
| 588 | return LY_EVALID; |
| 589 | } |
| 590 | |
| 591 | if (snode->nodetype & LYD_NODE_TERM) { |
| 592 | /* leaf / leaf-list */ |
| 593 | LY_CHECK_RET(lys_value_validate(ctx, snode, opaq->value, strlen(opaq->value), opaq->format, opaq->val_prefix_data)); |
| 594 | } else if (snode->nodetype == LYS_LIST) { |
| 595 | /* list */ |
| 596 | LY_CHECK_RET(lyd_parse_opaq_list_error(node, snode)); |
| 597 | } else if (snode->nodetype & LYD_NODE_INNER) { |
| 598 | /* inner node */ |
| 599 | if (opaq->value) { |
| 600 | LOGVAL(ctx, LYVE_DATA, "Invalid value \"%s\" for %s \"%s\".", opaq->value, |
| 601 | lys_nodetype2str(snode->nodetype), snode->name); |
| 602 | return LY_EVALID; |
| 603 | } |
| 604 | } else { |
| 605 | LOGERR(ctx, LY_EINVAL, "Unexpected opaque schema node %s \"%s\".", lys_nodetype2str(snode->nodetype), snode->name); |
| 606 | return LY_EINVAL; |
| 607 | } |
| 608 | |
| 609 | LOGERR(ctx, LY_EINVAL, "Unexpected valid opaque node %s \"%s\".", lys_nodetype2str(snode->nodetype), snode->name); |
| 610 | return LY_EINVAL; |
| 611 | } |
| 612 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 613 | LIBYANG_API_DEF const char * |
Christian Hopps | 46bd21b | 2021-04-27 09:43:58 -0400 | [diff] [blame] | 614 | lyd_value_get_canonical(const struct ly_ctx *ctx, const struct lyd_value *value) |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 615 | { |
Michal Vasko | ab40e7e | 2021-04-28 17:04:24 +0200 | [diff] [blame] | 616 | LY_CHECK_ARG_RET(ctx, ctx, value, NULL); |
| 617 | |
Michal Vasko | 3387602 | 2021-04-27 16:42:24 +0200 | [diff] [blame] | 618 | return value->_canonical ? value->_canonical : |
| 619 | (const char *)value->realtype->plugin->print(ctx, value, LY_VALUE_CANON, NULL, NULL, NULL); |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 620 | } |
| 621 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 622 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | a820c31 | 2021-02-05 16:33:00 +0100 | [diff] [blame] | 623 | lyd_any_value_str(const struct lyd_node *any, char **value_str) |
| 624 | { |
| 625 | const struct lyd_node_any *a; |
| 626 | struct lyd_node *tree = NULL; |
| 627 | const char *str = NULL; |
| 628 | ly_bool dynamic = 0; |
| 629 | LY_ERR ret = LY_SUCCESS; |
| 630 | |
| 631 | LY_CHECK_ARG_RET(NULL, any, value_str, LY_EINVAL); |
Radek Krejci | 71877df | 2021-04-06 17:24:06 +0200 | [diff] [blame] | 632 | LY_CHECK_ARG_RET(NULL, any->schema, any->schema->nodetype & LYS_ANYDATA, LY_EINVAL); |
Michal Vasko | a820c31 | 2021-02-05 16:33:00 +0100 | [diff] [blame] | 633 | |
| 634 | a = (struct lyd_node_any *)any; |
| 635 | *value_str = NULL; |
| 636 | |
| 637 | if (!a->value.str) { |
| 638 | /* there is no value in the union */ |
| 639 | return LY_SUCCESS; |
| 640 | } |
| 641 | |
| 642 | switch (a->value_type) { |
| 643 | case LYD_ANYDATA_LYB: |
| 644 | /* parse into a data tree */ |
| 645 | ret = lyd_parse_data_mem(LYD_CTX(any), a->value.mem, LYD_LYB, LYD_PARSE_ONLY, 0, &tree); |
| 646 | LY_CHECK_GOTO(ret, cleanup); |
| 647 | dynamic = 1; |
| 648 | break; |
| 649 | case LYD_ANYDATA_DATATREE: |
| 650 | tree = a->value.tree; |
| 651 | break; |
| 652 | case LYD_ANYDATA_STRING: |
| 653 | case LYD_ANYDATA_XML: |
| 654 | case LYD_ANYDATA_JSON: |
| 655 | /* simply use the string */ |
| 656 | str = a->value.str; |
| 657 | break; |
| 658 | } |
| 659 | |
| 660 | if (tree) { |
| 661 | /* print into a string */ |
| 662 | ret = lyd_print_mem(value_str, tree, LYD_XML, LYD_PRINT_WITHSIBLINGS); |
| 663 | LY_CHECK_GOTO(ret, cleanup); |
| 664 | } else { |
| 665 | assert(str); |
| 666 | *value_str = strdup(str); |
| 667 | LY_CHECK_ERR_GOTO(!*value_str, LOGMEM(LYD_CTX(any)), cleanup); |
| 668 | } |
| 669 | |
| 670 | /* success */ |
| 671 | |
| 672 | cleanup: |
| 673 | if (dynamic) { |
| 674 | lyd_free_all(tree); |
| 675 | } |
| 676 | return ret; |
| 677 | } |
| 678 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 679 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 680 | lyd_any_copy_value(struct lyd_node *trg, const union lyd_any_value *value, LYD_ANYDATA_VALUETYPE value_type) |
| 681 | { |
| 682 | struct lyd_node_any *t; |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 683 | |
Michal Vasko | a820c31 | 2021-02-05 16:33:00 +0100 | [diff] [blame] | 684 | LY_CHECK_ARG_RET(NULL, trg, LY_EINVAL); |
Radek Krejci | 71877df | 2021-04-06 17:24:06 +0200 | [diff] [blame] | 685 | LY_CHECK_ARG_RET(NULL, trg->schema, trg->schema->nodetype & LYS_ANYDATA, LY_EINVAL); |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 686 | |
| 687 | t = (struct lyd_node_any *)trg; |
| 688 | |
| 689 | /* free trg */ |
| 690 | switch (t->value_type) { |
| 691 | case LYD_ANYDATA_DATATREE: |
| 692 | lyd_free_all(t->value.tree); |
| 693 | break; |
| 694 | case LYD_ANYDATA_STRING: |
| 695 | case LYD_ANYDATA_XML: |
| 696 | case LYD_ANYDATA_JSON: |
Michal Vasko | e180ed0 | 2021-02-05 16:31:20 +0100 | [diff] [blame] | 697 | lydict_remove(LYD_CTX(trg), t->value.str); |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 698 | break; |
| 699 | case LYD_ANYDATA_LYB: |
| 700 | free(t->value.mem); |
| 701 | break; |
| 702 | } |
| 703 | t->value.str = NULL; |
| 704 | |
| 705 | if (!value) { |
| 706 | /* only free value in this case */ |
| 707 | return LY_SUCCESS; |
| 708 | } |
| 709 | |
| 710 | /* copy src */ |
| 711 | t->value_type = value_type; |
| 712 | switch (value_type) { |
| 713 | case LYD_ANYDATA_DATATREE: |
| 714 | if (value->tree) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 715 | LY_CHECK_RET(lyd_dup_siblings(value->tree, NULL, LYD_DUP_RECURSIVE, &t->value.tree)); |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 716 | } |
| 717 | break; |
| 718 | case LYD_ANYDATA_STRING: |
| 719 | case LYD_ANYDATA_XML: |
| 720 | case LYD_ANYDATA_JSON: |
| 721 | if (value->str) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 722 | LY_CHECK_RET(lydict_insert(LYD_CTX(trg), value->str, 0, &t->value.str)); |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 723 | } |
| 724 | break; |
| 725 | case LYD_ANYDATA_LYB: |
| 726 | if (value->mem) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 727 | int len = lyd_lyb_data_length(value->mem); |
Radek Krejci | 82fa8d4 | 2020-07-11 22:00:59 +0200 | [diff] [blame] | 728 | LY_CHECK_RET(len == -1, LY_EINVAL); |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 729 | t->value.mem = malloc(len); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 730 | LY_CHECK_ERR_RET(!t->value.mem, LOGMEM(LYD_CTX(trg)), LY_EMEM); |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 731 | memcpy(t->value.mem, value->mem, len); |
| 732 | } |
| 733 | break; |
| 734 | } |
| 735 | |
| 736 | return LY_SUCCESS; |
| 737 | } |
| 738 | |
Michal Vasko | 106f086 | 2021-11-02 11:49:27 +0100 | [diff] [blame] | 739 | const struct lysc_node * |
| 740 | lyd_node_schema(const struct lyd_node *node) |
| 741 | { |
| 742 | const struct lysc_node *schema = NULL; |
| 743 | const struct lyd_node *prev_iter = NULL, *iter; |
| 744 | const struct lys_module *mod; |
| 745 | |
| 746 | if (!node) { |
| 747 | return NULL; |
| 748 | } else if (node->schema) { |
| 749 | return node->schema; |
| 750 | } |
| 751 | |
| 752 | /* get schema node of an opaque node */ |
| 753 | do { |
| 754 | /* get next data node */ |
| 755 | for (iter = node; lyd_parent(iter) != prev_iter; iter = lyd_parent(iter)) {} |
| 756 | |
| 757 | /* get equivalent schema node */ |
| 758 | if (iter->schema) { |
| 759 | schema = iter->schema; |
| 760 | } else { |
| 761 | /* get module */ |
| 762 | mod = lyd_owner_module(iter); |
Michal Vasko | a41826a | 2021-11-02 12:13:03 +0100 | [diff] [blame] | 763 | if (!mod && !schema) { |
| 764 | /* top-level opaque node has unknown module */ |
| 765 | break; |
| 766 | } |
Michal Vasko | 106f086 | 2021-11-02 11:49:27 +0100 | [diff] [blame] | 767 | |
| 768 | /* get schema node */ |
| 769 | schema = lys_find_child(schema, mod ? mod : schema->module, LYD_NAME(iter), 0, 0, 0); |
| 770 | } |
Michal Vasko | d2f404f | 2021-11-04 15:37:11 +0100 | [diff] [blame] | 771 | |
| 772 | /* remember to move to the descendant */ |
| 773 | prev_iter = iter; |
Michal Vasko | 106f086 | 2021-11-02 11:49:27 +0100 | [diff] [blame] | 774 | } while (schema && (iter != node)); |
| 775 | |
| 776 | return schema; |
| 777 | } |
| 778 | |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 779 | void |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 780 | lyd_del_move_root(struct lyd_node **root, const struct lyd_node *to_del, const struct lys_module *mod) |
| 781 | { |
| 782 | if (*root && (lyd_owner_module(*root) != mod)) { |
| 783 | /* there are no data of mod so this is simply the first top-level sibling */ |
| 784 | mod = NULL; |
| 785 | } |
| 786 | |
| 787 | if ((*root != to_del) || (*root)->parent) { |
| 788 | return; |
| 789 | } |
| 790 | |
Michal Vasko | 598063b | 2021-07-19 11:39:05 +0200 | [diff] [blame] | 791 | if (mod && (*root)->prev->next && (!(*root)->next || (lyd_owner_module(to_del) != lyd_owner_module((*root)->next)))) { |
| 792 | /* there are no more nodes from mod, simply get the first top-level sibling */ |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 793 | *root = lyd_first_sibling(*root); |
Michal Vasko | 598063b | 2021-07-19 11:39:05 +0200 | [diff] [blame] | 794 | } else { |
| 795 | *root = (*root)->next; |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 796 | } |
| 797 | } |
| 798 | |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 799 | LY_ERR |
| 800 | ly_nested_ext_schema(const struct lyd_node *parent, const struct lysc_node *sparent, const char *prefix, |
| 801 | size_t prefix_len, LY_VALUE_FORMAT format, void *prefix_data, const char *name, size_t name_len, |
| 802 | const struct lysc_node **snode, struct lysc_ext_instance **ext) |
| 803 | { |
| 804 | LY_ERR r; |
| 805 | LY_ARRAY_COUNT_TYPE u; |
| 806 | struct lysc_ext_instance *nested_exts = NULL; |
| 807 | lyplg_ext_data_snode_clb ext_snode_cb; |
| 808 | |
| 809 | /* check if there are any nested extension instances */ |
| 810 | if (parent && parent->schema) { |
| 811 | nested_exts = parent->schema->exts; |
| 812 | } else if (sparent) { |
| 813 | nested_exts = sparent->exts; |
| 814 | } |
| 815 | LY_ARRAY_FOR(nested_exts, u) { |
Michal Vasko | 305c6cb | 2022-04-27 10:33:04 +0200 | [diff] [blame] | 816 | if (!nested_exts[u].def->plugin) { |
| 817 | /* no plugin */ |
| 818 | continue; |
| 819 | } |
| 820 | |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 821 | ext_snode_cb = nested_exts[u].def->plugin->snode; |
| 822 | if (!ext_snode_cb) { |
| 823 | /* not an extension with nested data */ |
| 824 | continue; |
| 825 | } |
| 826 | |
| 827 | /* try to get the schema node */ |
| 828 | r = ext_snode_cb(&nested_exts[u], parent, sparent, prefix, prefix_len, format, prefix_data, name, name_len, snode); |
| 829 | if (!r) { |
| 830 | /* data successfully created, remember the ext instance */ |
| 831 | *ext = &nested_exts[u]; |
| 832 | return LY_SUCCESS; |
| 833 | } else if (r != LY_ENOT) { |
| 834 | /* fatal error */ |
| 835 | return r; |
| 836 | } |
| 837 | /* data was not from this module, continue */ |
| 838 | } |
| 839 | |
| 840 | /* no extensions or none matched */ |
| 841 | return LY_ENOT; |
| 842 | } |
| 843 | |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 844 | void |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 845 | ly_free_prefix_data(LY_VALUE_FORMAT format, void *prefix_data) |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 846 | { |
| 847 | struct ly_set *ns_list; |
| 848 | struct lysc_prefix *prefixes; |
| 849 | uint32_t i; |
| 850 | LY_ARRAY_COUNT_TYPE u; |
| 851 | |
| 852 | if (!prefix_data) { |
| 853 | return; |
| 854 | } |
| 855 | |
| 856 | switch (format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 857 | case LY_VALUE_XML: |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 858 | case LY_VALUE_STR_NS: |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 859 | ns_list = prefix_data; |
| 860 | for (i = 0; i < ns_list->count; ++i) { |
| 861 | free(((struct lyxml_ns *)ns_list->objs[i])->prefix); |
| 862 | free(((struct lyxml_ns *)ns_list->objs[i])->uri); |
| 863 | } |
| 864 | ly_set_free(ns_list, free); |
| 865 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 866 | case LY_VALUE_SCHEMA_RESOLVED: |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 867 | prefixes = prefix_data; |
| 868 | LY_ARRAY_FOR(prefixes, u) { |
| 869 | free(prefixes[u].prefix); |
| 870 | } |
| 871 | LY_ARRAY_FREE(prefixes); |
| 872 | break; |
Radek Krejci | 224d4b4 | 2021-04-23 13:54:59 +0200 | [diff] [blame] | 873 | case LY_VALUE_CANON: |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 874 | case LY_VALUE_SCHEMA: |
| 875 | case LY_VALUE_JSON: |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 876 | case LY_VALUE_LYB: |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 877 | break; |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | LY_ERR |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 882 | ly_dup_prefix_data(const struct ly_ctx *ctx, LY_VALUE_FORMAT format, const void *prefix_data, |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 883 | void **prefix_data_p) |
| 884 | { |
| 885 | LY_ERR ret = LY_SUCCESS; |
| 886 | struct lyxml_ns *ns; |
| 887 | struct lysc_prefix *prefixes = NULL, *orig_pref; |
| 888 | struct ly_set *ns_list, *orig_ns; |
| 889 | uint32_t i; |
| 890 | LY_ARRAY_COUNT_TYPE u; |
| 891 | |
| 892 | assert(!*prefix_data_p); |
| 893 | |
| 894 | switch (format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 895 | case LY_VALUE_SCHEMA: |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 896 | *prefix_data_p = (void *)prefix_data; |
| 897 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 898 | case LY_VALUE_SCHEMA_RESOLVED: |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 899 | /* copy all the value prefixes */ |
| 900 | orig_pref = (struct lysc_prefix *)prefix_data; |
| 901 | LY_ARRAY_CREATE_GOTO(ctx, prefixes, LY_ARRAY_COUNT(orig_pref), ret, cleanup); |
| 902 | *prefix_data_p = prefixes; |
| 903 | |
| 904 | LY_ARRAY_FOR(orig_pref, u) { |
| 905 | if (orig_pref[u].prefix) { |
| 906 | prefixes[u].prefix = strdup(orig_pref[u].prefix); |
| 907 | LY_CHECK_ERR_GOTO(!prefixes[u].prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 908 | } |
| 909 | prefixes[u].mod = orig_pref[u].mod; |
| 910 | LY_ARRAY_INCREMENT(prefixes); |
| 911 | } |
| 912 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 913 | case LY_VALUE_XML: |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 914 | case LY_VALUE_STR_NS: |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 915 | /* copy all the namespaces */ |
| 916 | LY_CHECK_GOTO(ret = ly_set_new(&ns_list), cleanup); |
| 917 | *prefix_data_p = ns_list; |
| 918 | |
| 919 | orig_ns = (struct ly_set *)prefix_data; |
| 920 | for (i = 0; i < orig_ns->count; ++i) { |
| 921 | ns = calloc(1, sizeof *ns); |
| 922 | LY_CHECK_ERR_GOTO(!ns, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 923 | LY_CHECK_GOTO(ret = ly_set_add(ns_list, ns, 1, NULL), cleanup); |
| 924 | |
| 925 | if (((struct lyxml_ns *)orig_ns->objs[i])->prefix) { |
| 926 | ns->prefix = strdup(((struct lyxml_ns *)orig_ns->objs[i])->prefix); |
| 927 | LY_CHECK_ERR_GOTO(!ns->prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 928 | } |
| 929 | ns->uri = strdup(((struct lyxml_ns *)orig_ns->objs[i])->uri); |
| 930 | LY_CHECK_ERR_GOTO(!ns->uri, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 931 | } |
| 932 | break; |
Radek Krejci | 224d4b4 | 2021-04-23 13:54:59 +0200 | [diff] [blame] | 933 | case LY_VALUE_CANON: |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 934 | case LY_VALUE_JSON: |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 935 | case LY_VALUE_LYB: |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 936 | assert(!prefix_data); |
| 937 | *prefix_data_p = NULL; |
| 938 | break; |
| 939 | } |
| 940 | |
| 941 | cleanup: |
| 942 | if (ret) { |
| 943 | ly_free_prefix_data(format, *prefix_data_p); |
| 944 | *prefix_data_p = NULL; |
| 945 | } |
| 946 | return ret; |
| 947 | } |
| 948 | |
| 949 | LY_ERR |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 950 | ly_store_prefix_data(const struct ly_ctx *ctx, const void *value, size_t value_len, LY_VALUE_FORMAT format, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 951 | const void *prefix_data, LY_VALUE_FORMAT *format_p, void **prefix_data_p) |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 952 | { |
| 953 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 954 | const struct lys_module *mod; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 955 | const struct lyxml_ns *ns; |
| 956 | struct lyxml_ns *new_ns; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 957 | struct ly_set *ns_list; |
| 958 | struct lysc_prefix *prefixes = NULL, *val_pref; |
aPiecek | 83436bc | 2021-03-30 12:20:45 +0200 | [diff] [blame] | 959 | const char *value_iter, *value_next, *value_end; |
| 960 | uint32_t substr_len; |
| 961 | ly_bool is_prefix; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 962 | |
| 963 | switch (format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 964 | case LY_VALUE_SCHEMA: |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 965 | /* copy all referenced modules as prefix - module pairs */ |
| 966 | if (!*prefix_data_p) { |
| 967 | /* new prefix data */ |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 968 | LY_ARRAY_CREATE_GOTO(ctx, prefixes, 0, ret, cleanup); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 969 | *format_p = LY_VALUE_SCHEMA_RESOLVED; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 970 | *prefix_data_p = prefixes; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 971 | } else { |
| 972 | /* reuse prefix data */ |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 973 | assert(*format_p == LY_VALUE_SCHEMA_RESOLVED); |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 974 | prefixes = *prefix_data_p; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 975 | } |
| 976 | |
Michal Vasko | c0f9c4c | 2022-05-06 12:12:17 +0200 | [diff] [blame] | 977 | /* add current module for unprefixed values */ |
| 978 | LY_ARRAY_NEW_GOTO(ctx, prefixes, val_pref, ret, cleanup); |
| 979 | *prefix_data_p = prefixes; |
| 980 | |
| 981 | val_pref->prefix = NULL; |
| 982 | val_pref->mod = ((const struct lysp_module *)prefix_data)->mod; |
| 983 | |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 984 | /* add all used prefixes */ |
Michal Vasko | 59e90fc | 2021-09-22 12:17:08 +0200 | [diff] [blame] | 985 | value_end = (char *)value + value_len; |
aPiecek | 83436bc | 2021-03-30 12:20:45 +0200 | [diff] [blame] | 986 | for (value_iter = value; value_iter; value_iter = value_next) { |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 987 | LY_CHECK_GOTO(ret = ly_value_prefix_next(value_iter, value_end, &substr_len, &is_prefix, &value_next), cleanup); |
aPiecek | 83436bc | 2021-03-30 12:20:45 +0200 | [diff] [blame] | 988 | if (is_prefix) { |
| 989 | /* we have a possible prefix. Do we already have the prefix? */ |
| 990 | mod = ly_resolve_prefix(ctx, value_iter, substr_len, *format_p, *prefix_data_p); |
| 991 | if (!mod) { |
| 992 | mod = ly_resolve_prefix(ctx, value_iter, substr_len, format, prefix_data); |
| 993 | if (mod) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 994 | assert(*format_p == LY_VALUE_SCHEMA_RESOLVED); |
aPiecek | 83436bc | 2021-03-30 12:20:45 +0200 | [diff] [blame] | 995 | /* store a new prefix - module pair */ |
| 996 | LY_ARRAY_NEW_GOTO(ctx, prefixes, val_pref, ret, cleanup); |
| 997 | *prefix_data_p = prefixes; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 998 | |
aPiecek | 83436bc | 2021-03-30 12:20:45 +0200 | [diff] [blame] | 999 | val_pref->prefix = strndup(value_iter, substr_len); |
| 1000 | LY_CHECK_ERR_GOTO(!val_pref->prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 1001 | val_pref->mod = mod; |
| 1002 | } /* else it is not even defined */ |
| 1003 | } /* else the prefix is already present */ |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1004 | } |
| 1005 | } |
| 1006 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1007 | case LY_VALUE_XML: |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1008 | case LY_VALUE_STR_NS: |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1009 | /* copy all referenced namespaces as prefix - namespace pairs */ |
| 1010 | if (!*prefix_data_p) { |
| 1011 | /* new prefix data */ |
| 1012 | LY_CHECK_GOTO(ret = ly_set_new(&ns_list), cleanup); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1013 | *format_p = format; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1014 | *prefix_data_p = ns_list; |
| 1015 | } else { |
| 1016 | /* reuse prefix data */ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1017 | assert(*format_p == format); |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1018 | ns_list = *prefix_data_p; |
| 1019 | } |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 1020 | |
Michal Vasko | 294e7f0 | 2022-02-28 13:59:00 +0100 | [diff] [blame] | 1021 | /* store default namespace */ |
| 1022 | ns = lyxml_ns_get(prefix_data, NULL, 0); |
| 1023 | if (ns) { |
| 1024 | new_ns = calloc(1, sizeof *new_ns); |
| 1025 | LY_CHECK_ERR_GOTO(!new_ns, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 1026 | LY_CHECK_GOTO(ret = ly_set_add(ns_list, new_ns, 1, NULL), cleanup); |
| 1027 | |
| 1028 | new_ns->prefix = NULL; |
| 1029 | new_ns->uri = strdup(ns->uri); |
| 1030 | LY_CHECK_ERR_GOTO(!new_ns->uri, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 1031 | } |
| 1032 | |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1033 | /* add all used prefixes */ |
Michal Vasko | 59e90fc | 2021-09-22 12:17:08 +0200 | [diff] [blame] | 1034 | value_end = (char *)value + value_len; |
aPiecek | 83436bc | 2021-03-30 12:20:45 +0200 | [diff] [blame] | 1035 | for (value_iter = value; value_iter; value_iter = value_next) { |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 1036 | LY_CHECK_GOTO(ret = ly_value_prefix_next(value_iter, value_end, &substr_len, &is_prefix, &value_next), cleanup); |
aPiecek | 83436bc | 2021-03-30 12:20:45 +0200 | [diff] [blame] | 1037 | if (is_prefix) { |
| 1038 | /* we have a possible prefix. Do we already have the prefix? */ |
| 1039 | ns = lyxml_ns_get(ns_list, value_iter, substr_len); |
| 1040 | if (!ns) { |
| 1041 | ns = lyxml_ns_get(prefix_data, value_iter, substr_len); |
| 1042 | if (ns) { |
| 1043 | /* store a new prefix - namespace pair */ |
| 1044 | new_ns = calloc(1, sizeof *new_ns); |
| 1045 | LY_CHECK_ERR_GOTO(!new_ns, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 1046 | LY_CHECK_GOTO(ret = ly_set_add(ns_list, new_ns, 1, NULL), cleanup); |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1047 | |
aPiecek | 83436bc | 2021-03-30 12:20:45 +0200 | [diff] [blame] | 1048 | new_ns->prefix = strndup(value_iter, substr_len); |
| 1049 | LY_CHECK_ERR_GOTO(!new_ns->prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 1050 | new_ns->uri = strdup(ns->uri); |
| 1051 | LY_CHECK_ERR_GOTO(!new_ns->uri, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 1052 | } /* else it is not even defined */ |
| 1053 | } /* else the prefix is already present */ |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 1054 | } |
| 1055 | } |
| 1056 | break; |
Radek Krejci | 224d4b4 | 2021-04-23 13:54:59 +0200 | [diff] [blame] | 1057 | case LY_VALUE_CANON: |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1058 | case LY_VALUE_SCHEMA_RESOLVED: |
| 1059 | case LY_VALUE_JSON: |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 1060 | case LY_VALUE_LYB: |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1061 | if (!*prefix_data_p) { |
| 1062 | /* new prefix data - simply copy all the prefix data */ |
| 1063 | *format_p = format; |
| 1064 | LY_CHECK_GOTO(ret = ly_dup_prefix_data(ctx, format, prefix_data, prefix_data_p), cleanup); |
| 1065 | } /* else reuse prefix data - the prefix data are always the same, nothing to do */ |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 1066 | break; |
| 1067 | } |
| 1068 | |
| 1069 | cleanup: |
| 1070 | if (ret) { |
| 1071 | ly_free_prefix_data(*format_p, *prefix_data_p); |
| 1072 | *prefix_data_p = NULL; |
| 1073 | } |
| 1074 | return ret; |
| 1075 | } |
Michal Vasko | 7ed1fcb | 2020-12-03 14:15:22 +0100 | [diff] [blame] | 1076 | |
| 1077 | const char * |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1078 | ly_format2str(LY_VALUE_FORMAT format) |
Michal Vasko | 7ed1fcb | 2020-12-03 14:15:22 +0100 | [diff] [blame] | 1079 | { |
| 1080 | switch (format) { |
Radek Krejci | 224d4b4 | 2021-04-23 13:54:59 +0200 | [diff] [blame] | 1081 | case LY_VALUE_CANON: |
| 1082 | return "canonical"; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1083 | case LY_VALUE_SCHEMA: |
Michal Vasko | 7ed1fcb | 2020-12-03 14:15:22 +0100 | [diff] [blame] | 1084 | return "schema imports"; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1085 | case LY_VALUE_SCHEMA_RESOLVED: |
Michal Vasko | 7ed1fcb | 2020-12-03 14:15:22 +0100 | [diff] [blame] | 1086 | return "schema stored mapping"; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1087 | case LY_VALUE_XML: |
Michal Vasko | 7ed1fcb | 2020-12-03 14:15:22 +0100 | [diff] [blame] | 1088 | return "XML prefixes"; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1089 | case LY_VALUE_JSON: |
Michal Vasko | 7ed1fcb | 2020-12-03 14:15:22 +0100 | [diff] [blame] | 1090 | return "JSON module names"; |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 1091 | case LY_VALUE_LYB: |
| 1092 | return "LYB prefixes"; |
Michal Vasko | 7ed1fcb | 2020-12-03 14:15:22 +0100 | [diff] [blame] | 1093 | default: |
| 1094 | break; |
| 1095 | } |
| 1096 | |
| 1097 | return NULL; |
| 1098 | } |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 1099 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1100 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 1101 | ly_time_str2time(const char *value, time_t *time, char **fractions_s) |
| 1102 | { |
| 1103 | struct tm tm = {0}; |
| 1104 | uint32_t i, frac_len; |
| 1105 | const char *frac; |
| 1106 | int64_t shift, shift_m; |
| 1107 | time_t t; |
| 1108 | |
| 1109 | LY_CHECK_ARG_RET(NULL, value, time, LY_EINVAL); |
| 1110 | |
| 1111 | tm.tm_year = atoi(&value[0]) - 1900; |
| 1112 | tm.tm_mon = atoi(&value[5]) - 1; |
| 1113 | tm.tm_mday = atoi(&value[8]); |
| 1114 | tm.tm_hour = atoi(&value[11]); |
| 1115 | tm.tm_min = atoi(&value[14]); |
| 1116 | tm.tm_sec = atoi(&value[17]); |
| 1117 | |
| 1118 | t = timegm(&tm); |
| 1119 | i = 19; |
| 1120 | |
| 1121 | /* fractions of a second */ |
| 1122 | if (value[i] == '.') { |
| 1123 | ++i; |
| 1124 | frac = &value[i]; |
| 1125 | for (frac_len = 0; isdigit(frac[frac_len]); ++frac_len) {} |
| 1126 | |
| 1127 | i += frac_len; |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 1128 | } else { |
| 1129 | frac = NULL; |
| 1130 | } |
| 1131 | |
| 1132 | /* apply offset */ |
| 1133 | if ((value[i] == 'Z') || (value[i] == 'z')) { |
| 1134 | /* zero shift */ |
| 1135 | shift = 0; |
| 1136 | } else { |
| 1137 | shift = strtol(&value[i], NULL, 10); |
| 1138 | shift = shift * 60 * 60; /* convert from hours to seconds */ |
| 1139 | shift_m = strtol(&value[i + 4], NULL, 10) * 60; /* includes conversion from minutes to seconds */ |
| 1140 | /* correct sign */ |
| 1141 | if (shift < 0) { |
| 1142 | shift_m *= -1; |
| 1143 | } |
| 1144 | /* connect hours and minutes of the shift */ |
| 1145 | shift = shift + shift_m; |
| 1146 | } |
| 1147 | |
| 1148 | /* we have to shift to the opposite way to correct the time */ |
| 1149 | t -= shift; |
| 1150 | |
| 1151 | *time = t; |
| 1152 | if (fractions_s) { |
| 1153 | if (frac) { |
| 1154 | *fractions_s = strndup(frac, frac_len); |
| 1155 | LY_CHECK_RET(!*fractions_s, LY_EMEM); |
| 1156 | } else { |
| 1157 | *fractions_s = NULL; |
| 1158 | } |
| 1159 | } |
| 1160 | return LY_SUCCESS; |
| 1161 | } |
| 1162 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1163 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 1164 | ly_time_time2str(time_t time, const char *fractions_s, char **str) |
| 1165 | { |
| 1166 | struct tm tm; |
Michal Vasko | 143ffa8 | 2021-05-20 11:11:39 +0200 | [diff] [blame] | 1167 | char zoneshift[8]; |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 1168 | int32_t zonediff_h, zonediff_m; |
| 1169 | |
| 1170 | LY_CHECK_ARG_RET(NULL, str, LY_EINVAL); |
| 1171 | |
| 1172 | /* initialize the local timezone */ |
| 1173 | tzset(); |
| 1174 | |
Jan Kundrát | b17efe9 | 2022-02-14 18:32:18 +0100 | [diff] [blame] | 1175 | #ifdef HAVE_TM_GMTOFF |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 1176 | /* convert */ |
| 1177 | if (!localtime_r(&time, &tm)) { |
| 1178 | return LY_ESYS; |
| 1179 | } |
| 1180 | |
| 1181 | /* get timezone offset */ |
| 1182 | if (tm.tm_gmtoff == 0) { |
| 1183 | /* time is Zulu (UTC) */ |
| 1184 | zonediff_h = 0; |
| 1185 | zonediff_m = 0; |
| 1186 | } else { |
| 1187 | /* timezone offset */ |
| 1188 | zonediff_h = tm.tm_gmtoff / 60 / 60; |
| 1189 | zonediff_m = tm.tm_gmtoff / 60 % 60; |
| 1190 | } |
| 1191 | sprintf(zoneshift, "%+03d:%02d", zonediff_h, zonediff_m); |
Jan Kundrát | e182a27 | 2021-12-09 23:25:15 +0100 | [diff] [blame] | 1192 | #else |
Jan Kundrát | b17efe9 | 2022-02-14 18:32:18 +0100 | [diff] [blame] | 1193 | /* convert */ |
| 1194 | if (!gmtime_r(&time, &tm)) { |
| 1195 | return LY_ESYS; |
| 1196 | } |
| 1197 | |
Jan Kundrát | e182a27 | 2021-12-09 23:25:15 +0100 | [diff] [blame] | 1198 | (void)zonediff_h; |
| 1199 | (void)zonediff_m; |
| 1200 | sprintf(zoneshift, "-00:00"); |
| 1201 | #endif |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 1202 | |
| 1203 | /* print */ |
| 1204 | if (asprintf(str, "%04d-%02d-%02dT%02d:%02d:%02d%s%s%s", |
| 1205 | tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, |
| 1206 | fractions_s ? "." : "", fractions_s ? fractions_s : "", zoneshift) == -1) { |
| 1207 | return LY_EMEM; |
| 1208 | } |
| 1209 | |
| 1210 | return LY_SUCCESS; |
| 1211 | } |
| 1212 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1213 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 1214 | ly_time_str2ts(const char *value, struct timespec *ts) |
| 1215 | { |
| 1216 | LY_ERR rc; |
Michal Vasko | 7297506 | 2021-08-25 08:13:04 +0200 | [diff] [blame] | 1217 | char *fractions_s, frac_buf[10]; |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 1218 | int frac_len; |
| 1219 | |
| 1220 | LY_CHECK_ARG_RET(NULL, value, ts, LY_EINVAL); |
| 1221 | |
| 1222 | rc = ly_time_str2time(value, &ts->tv_sec, &fractions_s); |
| 1223 | LY_CHECK_RET(rc); |
| 1224 | |
| 1225 | /* convert fractions of a second to nanoseconds */ |
| 1226 | if (fractions_s) { |
Michal Vasko | 7297506 | 2021-08-25 08:13:04 +0200 | [diff] [blame] | 1227 | /* init frac_buf with zeroes */ |
| 1228 | memset(frac_buf, '0', 9); |
| 1229 | frac_buf[9] = '\0'; |
| 1230 | |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 1231 | frac_len = strlen(fractions_s); |
| 1232 | memcpy(frac_buf, fractions_s, frac_len > 9 ? 9 : frac_len); |
| 1233 | ts->tv_nsec = atol(frac_buf); |
| 1234 | free(fractions_s); |
| 1235 | } else { |
| 1236 | ts->tv_nsec = 0; |
| 1237 | } |
| 1238 | |
| 1239 | return LY_SUCCESS; |
| 1240 | } |
| 1241 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1242 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 1243 | ly_time_ts2str(const struct timespec *ts, char **str) |
| 1244 | { |
| 1245 | char frac_buf[10]; |
| 1246 | |
Jan Kundrát | bd15700 | 2021-08-30 14:02:22 +0200 | [diff] [blame] | 1247 | LY_CHECK_ARG_RET(NULL, ts, str, ((ts->tv_nsec <= 999999999) && (ts->tv_nsec >= 0)), LY_EINVAL); |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 1248 | |
| 1249 | /* convert nanoseconds to fractions of a second */ |
| 1250 | if (ts->tv_nsec) { |
| 1251 | sprintf(frac_buf, "%09ld", ts->tv_nsec); |
| 1252 | } |
| 1253 | |
| 1254 | return ly_time_time2str(ts->tv_sec, ts->tv_nsec ? frac_buf : NULL, str); |
| 1255 | } |