Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1 | /** |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 2 | * @file in.c |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 4 | * @brief libyang input functions. |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 5 | * |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 6 | * Copyright (c) 2015 - 2020 CESNET, z.s.p.o. |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +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 | */ |
| 14 | |
| 15 | #define _GNU_SOURCE |
Radek Krejci | f8dc59a | 2020-11-25 13:47:44 +0100 | [diff] [blame] | 16 | #define _POSIX_C_SOURCE 200809L /* strdup, strndup */ |
| 17 | |
| 18 | #ifdef __APPLE__ |
| 19 | #define _DARWIN_C_SOURCE /* F_GETPATH */ |
| 20 | #endif |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 21 | |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 22 | #include "in.h" |
| 23 | #include "in_internal.h" |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 24 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 25 | #include <errno.h> |
| 26 | #include <fcntl.h> |
| 27 | #include <limits.h> |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 28 | #include <stdint.h> |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 29 | #include <stdio.h> |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 30 | #include <stdlib.h> |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 31 | #include <string.h> |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 32 | #include <unistd.h> |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 33 | |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 34 | #include "common.h" |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 35 | #include "compat.h" |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 36 | #include "dict.h" |
| 37 | #include "log.h" |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 38 | #include "parser_data.h" |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 39 | #include "parser_internal.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 40 | #include "set.h" |
| 41 | #include "tree_data.h" |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 42 | #include "tree_data_internal.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 43 | #include "tree_schema.h" |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 44 | #include "tree_schema_internal.h" |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 45 | |
| 46 | API LY_IN_TYPE |
| 47 | ly_in_type(const struct ly_in *in) |
| 48 | { |
| 49 | LY_CHECK_ARG_RET(NULL, in, LY_IN_ERROR); |
| 50 | return in->type; |
| 51 | } |
| 52 | |
| 53 | API LY_ERR |
| 54 | ly_in_new_fd(int fd, struct ly_in **in) |
| 55 | { |
| 56 | size_t length; |
| 57 | char *addr; |
| 58 | |
| 59 | LY_CHECK_ARG_RET(NULL, fd >= 0, in, LY_EINVAL); |
| 60 | |
| 61 | LY_CHECK_RET(ly_mmap(NULL, fd, &length, (void **)&addr)); |
| 62 | if (!addr) { |
| 63 | LOGERR(NULL, LY_EINVAL, "Empty input file."); |
| 64 | return LY_EINVAL; |
| 65 | } |
| 66 | |
| 67 | *in = calloc(1, sizeof **in); |
| 68 | LY_CHECK_ERR_RET(!*in, LOGMEM(NULL); ly_munmap(addr, length), LY_EMEM); |
| 69 | |
| 70 | (*in)->type = LY_IN_FD; |
| 71 | (*in)->method.fd = fd; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 72 | (*in)->current = (*in)->start = (*in)->func_start = addr; |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 73 | (*in)->line = 1; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 74 | (*in)->length = length; |
| 75 | |
| 76 | return LY_SUCCESS; |
| 77 | } |
| 78 | |
| 79 | API int |
| 80 | ly_in_fd(struct ly_in *in, int fd) |
| 81 | { |
| 82 | int prev_fd; |
| 83 | size_t length; |
| 84 | const char *addr; |
| 85 | |
| 86 | LY_CHECK_ARG_RET(NULL, in, in->type == LY_IN_FD, -1); |
| 87 | |
| 88 | prev_fd = in->method.fd; |
| 89 | |
| 90 | if (fd != -1) { |
| 91 | LY_CHECK_RET(ly_mmap(NULL, fd, &length, (void **)&addr), -1); |
| 92 | if (!addr) { |
| 93 | LOGERR(NULL, LY_EINVAL, "Empty input file."); |
| 94 | return -1; |
| 95 | } |
| 96 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 97 | ly_munmap((char *)in->start, in->length); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 98 | |
| 99 | in->method.fd = fd; |
| 100 | in->current = in->start = addr; |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 101 | in->line = 1; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 102 | in->length = length; |
| 103 | } |
| 104 | |
| 105 | return prev_fd; |
| 106 | } |
| 107 | |
| 108 | API LY_ERR |
| 109 | ly_in_new_file(FILE *f, struct ly_in **in) |
| 110 | { |
| 111 | LY_CHECK_ARG_RET(NULL, f, in, LY_EINVAL); |
| 112 | |
| 113 | LY_CHECK_RET(ly_in_new_fd(fileno(f), in)); |
| 114 | |
| 115 | /* convert the LY_IN_FD input handler into the LY_IN_FILE */ |
| 116 | (*in)->type = LY_IN_FILE; |
| 117 | (*in)->method.f = f; |
| 118 | |
| 119 | return LY_SUCCESS; |
| 120 | } |
| 121 | |
| 122 | API FILE * |
| 123 | ly_in_file(struct ly_in *in, FILE *f) |
| 124 | { |
| 125 | FILE *prev_f; |
| 126 | |
| 127 | LY_CHECK_ARG_RET(NULL, in, in->type == LY_IN_FILE, NULL); |
| 128 | |
| 129 | prev_f = in->method.f; |
| 130 | |
| 131 | if (f) { |
| 132 | /* convert LY_IN_FILE handler into LY_IN_FD to be able to update it via ly_in_fd() */ |
| 133 | in->type = LY_IN_FD; |
| 134 | in->method.fd = fileno(prev_f); |
| 135 | if (ly_in_fd(in, fileno(f)) == -1) { |
| 136 | in->type = LY_IN_FILE; |
| 137 | in->method.f = prev_f; |
| 138 | return NULL; |
| 139 | } |
| 140 | |
| 141 | /* if success, convert the result back */ |
| 142 | in->type = LY_IN_FILE; |
| 143 | in->method.f = f; |
| 144 | } |
| 145 | |
| 146 | return prev_f; |
| 147 | } |
| 148 | |
| 149 | API LY_ERR |
| 150 | ly_in_new_memory(const char *str, struct ly_in **in) |
| 151 | { |
| 152 | LY_CHECK_ARG_RET(NULL, str, in, LY_EINVAL); |
| 153 | |
| 154 | *in = calloc(1, sizeof **in); |
| 155 | LY_CHECK_ERR_RET(!*in, LOGMEM(NULL), LY_EMEM); |
| 156 | |
| 157 | (*in)->type = LY_IN_MEMORY; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 158 | (*in)->start = (*in)->current = (*in)->func_start = str; |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 159 | (*in)->line = 1; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 160 | |
| 161 | return LY_SUCCESS; |
| 162 | } |
| 163 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 164 | API const char * |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 165 | ly_in_memory(struct ly_in *in, const char *str) |
| 166 | { |
| 167 | const char *data; |
| 168 | |
| 169 | LY_CHECK_ARG_RET(NULL, in, in->type == LY_IN_MEMORY, NULL); |
| 170 | |
| 171 | data = in->current; |
| 172 | |
| 173 | if (str) { |
| 174 | in->start = in->current = str; |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 175 | in->line = 1; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | return data; |
| 179 | } |
| 180 | |
| 181 | API LY_ERR |
| 182 | ly_in_reset(struct ly_in *in) |
| 183 | { |
| 184 | LY_CHECK_ARG_RET(NULL, in, LY_EINVAL); |
| 185 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 186 | in->current = in->func_start = in->start; |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 187 | in->line = 1; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 188 | return LY_SUCCESS; |
| 189 | } |
| 190 | |
| 191 | API LY_ERR |
| 192 | ly_in_new_filepath(const char *filepath, size_t len, struct ly_in **in) |
| 193 | { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 194 | LY_ERR ret; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 195 | char *fp; |
| 196 | int fd; |
| 197 | |
| 198 | LY_CHECK_ARG_RET(NULL, filepath, in, LY_EINVAL); |
| 199 | |
| 200 | if (len) { |
| 201 | fp = strndup(filepath, len); |
| 202 | } else { |
| 203 | fp = strdup(filepath); |
| 204 | } |
| 205 | |
| 206 | fd = open(fp, O_RDONLY); |
Michal Vasko | f2eb8af | 2020-07-14 12:22:40 +0200 | [diff] [blame] | 207 | LY_CHECK_ERR_RET(fd == -1, LOGERR(NULL, LY_ESYS, "Failed to open file \"%s\" (%s).", fp, strerror(errno)); free(fp), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 208 | LY_ESYS); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 209 | |
| 210 | LY_CHECK_ERR_RET(ret = ly_in_new_fd(fd, in), free(fp), ret); |
| 211 | |
| 212 | /* convert the LY_IN_FD input handler into the LY_IN_FILE */ |
| 213 | (*in)->type = LY_IN_FILEPATH; |
| 214 | (*in)->method.fpath.fd = fd; |
| 215 | (*in)->method.fpath.filepath = fp; |
| 216 | |
| 217 | return LY_SUCCESS; |
| 218 | } |
| 219 | |
| 220 | API const char * |
| 221 | ly_in_filepath(struct ly_in *in, const char *filepath, size_t len) |
| 222 | { |
| 223 | int fd, prev_fd; |
| 224 | char *fp = NULL; |
| 225 | |
| 226 | LY_CHECK_ARG_RET(NULL, in, in->type == LY_IN_FILEPATH, filepath ? NULL : ((void *)-1)); |
| 227 | |
| 228 | if (!filepath) { |
| 229 | return in->method.fpath.filepath; |
| 230 | } |
| 231 | |
| 232 | if (len) { |
| 233 | fp = strndup(filepath, len); |
| 234 | } else { |
| 235 | fp = strdup(filepath); |
| 236 | } |
| 237 | |
| 238 | /* replace filepath */ |
| 239 | fd = open(fp, O_RDONLY); |
| 240 | LY_CHECK_ERR_RET(!fd, LOGERR(NULL, LY_ESYS, "Failed to open file \"%s\" (%s).", fp, strerror(errno)); free(fp), NULL); |
| 241 | |
| 242 | /* convert LY_IN_FILEPATH handler into LY_IN_FD to be able to update it via ly_in_fd() */ |
| 243 | in->type = LY_IN_FD; |
| 244 | prev_fd = ly_in_fd(in, fd); |
| 245 | LY_CHECK_ERR_RET(prev_fd == -1, in->type = LY_IN_FILEPATH; free(fp), NULL); |
| 246 | |
| 247 | /* and convert the result back */ |
| 248 | in->type = LY_IN_FILEPATH; |
| 249 | close(prev_fd); |
| 250 | free(in->method.fpath.filepath); |
| 251 | in->method.fpath.fd = fd; |
| 252 | in->method.fpath.filepath = fp; |
| 253 | |
| 254 | return NULL; |
| 255 | } |
| 256 | |
| 257 | void |
| 258 | lys_parser_fill_filepath(struct ly_ctx *ctx, struct ly_in *in, const char **filepath) |
| 259 | { |
| 260 | char path[PATH_MAX]; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 261 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 262 | #ifndef __APPLE__ |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 263 | char proc_path[32]; |
| 264 | int len; |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 265 | #endif |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 266 | |
| 267 | LY_CHECK_ARG_RET(NULL, ctx, in, filepath, ); |
| 268 | if (*filepath) { |
| 269 | /* filepath already set */ |
| 270 | return; |
| 271 | } |
| 272 | |
| 273 | switch (in->type) { |
| 274 | case LY_IN_FILEPATH: |
| 275 | if (realpath(in->method.fpath.filepath, path) != NULL) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 276 | lydict_insert(ctx, path, 0, filepath); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 277 | } else { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 278 | lydict_insert(ctx, in->method.fpath.filepath, 0, filepath); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | break; |
| 282 | case LY_IN_FD: |
| 283 | #ifdef __APPLE__ |
| 284 | if (fcntl(in->method.fd, F_GETPATH, path) != -1) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 285 | lydict_insert(ctx, path, 0, filepath); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 286 | } |
| 287 | #else |
| 288 | /* get URI if there is /proc */ |
| 289 | sprintf(proc_path, "/proc/self/fd/%d", in->method.fd); |
| 290 | if ((len = readlink(proc_path, path, PATH_MAX - 1)) > 0) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 291 | lydict_insert(ctx, path, len, filepath); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 292 | } |
| 293 | #endif |
| 294 | break; |
| 295 | case LY_IN_MEMORY: |
| 296 | case LY_IN_FILE: |
| 297 | /* nothing to do */ |
| 298 | break; |
| 299 | default: |
| 300 | LOGINT(ctx); |
| 301 | break; |
| 302 | } |
| 303 | |
| 304 | } |
| 305 | |
| 306 | API void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 307 | ly_in_free(struct ly_in *in, ly_bool destroy) |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 308 | { |
| 309 | if (!in) { |
| 310 | return; |
| 311 | } else if (in->type == LY_IN_ERROR) { |
| 312 | LOGINT(NULL); |
| 313 | return; |
| 314 | } |
| 315 | |
| 316 | if (destroy) { |
| 317 | if (in->type == LY_IN_MEMORY) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 318 | free((char *)in->start); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 319 | } else { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 320 | ly_munmap((char *)in->start, in->length); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 321 | |
| 322 | if (in->type == LY_IN_FILE) { |
| 323 | fclose(in->method.f); |
| 324 | } else { |
| 325 | close(in->method.fd); |
| 326 | |
| 327 | if (in->type == LY_IN_FILEPATH) { |
| 328 | free(in->method.fpath.filepath); |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | } else if (in->type != LY_IN_MEMORY) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 333 | ly_munmap((char *)in->start, in->length); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 334 | |
| 335 | if (in->type == LY_IN_FILEPATH) { |
| 336 | close(in->method.fpath.fd); |
| 337 | free(in->method.fpath.filepath); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | free(in); |
| 342 | } |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 343 | |
| 344 | LY_ERR |
| 345 | ly_in_read(struct ly_in *in, void *buf, size_t count) |
| 346 | { |
| 347 | if (in->length && (in->length - (in->current - in->start) < count)) { |
| 348 | /* EOF */ |
| 349 | return LY_EDENIED; |
| 350 | } |
| 351 | |
| 352 | memcpy(buf, in->current, count); |
| 353 | in->current += count; |
| 354 | return LY_SUCCESS; |
| 355 | } |
| 356 | |
| 357 | API size_t |
| 358 | ly_in_parsed(const struct ly_in *in) |
| 359 | { |
| 360 | return in->current - in->func_start; |
| 361 | } |
| 362 | |
| 363 | LY_ERR |
| 364 | ly_in_skip(struct ly_in *in, size_t count) |
| 365 | { |
| 366 | if (in->length && (in->length - (in->current - in->start) < count)) { |
| 367 | /* EOF */ |
| 368 | return LY_EDENIED; |
| 369 | } |
| 370 | |
| 371 | in->current += count; |
| 372 | return LY_SUCCESS; |
| 373 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 374 | |
| 375 | void |
| 376 | lyd_ctx_free(struct lyd_ctx *lydctx) |
| 377 | { |
Michal Vasko | 3271138 | 2020-12-03 14:14:31 +0100 | [diff] [blame] | 378 | ly_set_erase(&lydctx->node_types, NULL); |
| 379 | ly_set_erase(&lydctx->meta_types, NULL); |
| 380 | ly_set_erase(&lydctx->node_when, NULL); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | LY_ERR |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 384 | lyd_parser_find_operation(const struct lyd_node *parent, uint32_t int_opts, struct lyd_node **op) |
| 385 | { |
| 386 | const struct lyd_node *iter; |
| 387 | |
| 388 | *op = NULL; |
| 389 | |
| 390 | if (!parent) { |
| 391 | /* no parent, nothing to look for */ |
| 392 | return LY_SUCCESS; |
| 393 | } |
| 394 | |
| 395 | /* we need to find the operation node if it already exists */ |
| 396 | for (iter = parent; iter; iter = lyd_parent(iter)) { |
| 397 | if (iter->schema && (iter->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))) { |
| 398 | break; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | if (!iter) { |
| 403 | /* no operation found */ |
| 404 | return LY_SUCCESS; |
| 405 | } |
| 406 | |
| 407 | if (!(int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY))) { |
| 408 | LOGERR(LYD_CTX(parent), LY_EINVAL, "Invalid parent %s \"%s\" node when not parsing any operation.", |
| 409 | lys_nodetype2str(iter->schema->nodetype), iter->schema->name); |
| 410 | return LY_EINVAL; |
| 411 | } else if ((iter->schema->nodetype == LYS_RPC) && !(int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_REPLY))) { |
| 412 | LOGERR(LYD_CTX(parent), LY_EINVAL, "Invalid parent RPC \"%s\" node when not parsing RPC nor rpc-reply.", |
| 413 | iter->schema->name); |
| 414 | return LY_EINVAL; |
| 415 | } else if ((iter->schema->nodetype == LYS_ACTION) && !(int_opts & (LYD_INTOPT_ACTION | LYD_INTOPT_REPLY))) { |
| 416 | LOGERR(LYD_CTX(parent), LY_EINVAL, "Invalid parent action \"%s\" node when not parsing action nor rpc-reply.", |
| 417 | iter->schema->name); |
| 418 | return LY_EINVAL; |
| 419 | } else if ((iter->schema->nodetype == LYS_NOTIF) && !(int_opts & LYD_INTOPT_NOTIF)) { |
| 420 | LOGERR(LYD_CTX(parent), LY_EINVAL, "Invalid parent notification \"%s\" node when not parsing a notification.", |
| 421 | iter->schema->name); |
| 422 | return LY_EINVAL; |
| 423 | } |
| 424 | |
| 425 | *op = (struct lyd_node *)iter; |
| 426 | return LY_SUCCESS; |
| 427 | } |
| 428 | |
| 429 | LY_ERR |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 430 | lyd_parser_check_schema(struct lyd_ctx *lydctx, const struct lysc_node *snode) |
| 431 | { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 432 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 433 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 434 | LOG_LOCSET(snode, NULL, NULL, NULL); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 435 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 436 | if ((lydctx->parse_opts & LYD_PARSE_NO_STATE) && (snode->flags & LYS_CONFIG_R)) { |
Michal Vasko | 224e777 | 2021-02-18 14:22:33 +0100 | [diff] [blame] | 437 | LOGVAL(lydctx->data_ctx->ctx, LY_VCODE_UNEXPNODE, "state", snode->name); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 438 | rc = LY_EVALID; |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 439 | goto cleanup; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 440 | } |
| 441 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 442 | if (snode->nodetype == LYS_RPC) { |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 443 | if (lydctx->int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_REPLY)) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 444 | if (lydctx->op_node) { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 445 | goto error_node_dup; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 446 | } |
| 447 | } else { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 448 | goto error_node_inval; |
| 449 | } |
| 450 | } else if (snode->nodetype == LYS_ACTION) { |
| 451 | if (lydctx->int_opts & (LYD_INTOPT_ACTION | LYD_INTOPT_REPLY)) { |
| 452 | if (lydctx->op_node) { |
| 453 | goto error_node_dup; |
| 454 | } |
| 455 | } else { |
| 456 | goto error_node_inval; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 457 | } |
| 458 | } else if (snode->nodetype == LYS_NOTIF) { |
| 459 | if (lydctx->int_opts & LYD_INTOPT_NOTIF) { |
| 460 | if (lydctx->op_node) { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 461 | goto error_node_dup; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 462 | } |
| 463 | } else { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 464 | goto error_node_inval; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 468 | /* success */ |
| 469 | goto cleanup; |
| 470 | |
| 471 | error_node_dup: |
| 472 | LOGVAL(lydctx->data_ctx->ctx, LYVE_DATA, "Unexpected %s element \"%s\", %s \"%s\" already parsed.", |
| 473 | lys_nodetype2str(snode->nodetype), snode->name, lys_nodetype2str(lydctx->op_node->schema->nodetype), |
| 474 | lydctx->op_node->schema->name); |
| 475 | rc = LY_EVALID; |
| 476 | goto cleanup; |
| 477 | |
| 478 | error_node_inval: |
| 479 | LOGVAL(lydctx->data_ctx->ctx, LYVE_DATA, "Unexpected %s element \"%s\".", lys_nodetype2str(snode->nodetype), |
| 480 | snode->name); |
| 481 | rc = LY_EVALID; |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 482 | |
| 483 | cleanup: |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 484 | LOG_LOCBACK(1, 0, 0, 0); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 485 | return rc; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | LY_ERR |
| 489 | lyd_parser_create_term(struct lyd_ctx *lydctx, const struct lysc_node *schema, const char *value, size_t value_len, |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 490 | ly_bool *dynamic, LY_PREFIX_FORMAT format, void *prefix_data, uint32_t hints, struct lyd_node **node) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 491 | { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 492 | ly_bool incomplete; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 493 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 494 | LY_CHECK_RET(lyd_create_term(schema, value, value_len, dynamic, format, prefix_data, hints, &incomplete, node)); |
| 495 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 496 | if (incomplete && !(lydctx->parse_opts & LYD_PARSE_ONLY)) { |
Michal Vasko | 3271138 | 2020-12-03 14:14:31 +0100 | [diff] [blame] | 497 | LY_CHECK_RET(ly_set_add(&lydctx->node_types, *node, 1, NULL)); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 498 | } |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 499 | return LY_SUCCESS; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | LY_ERR |
| 503 | lyd_parser_create_meta(struct lyd_ctx *lydctx, struct lyd_node *parent, struct lyd_meta **meta, const struct lys_module *mod, |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 504 | const char *name, size_t name_len, const char *value, size_t value_len, ly_bool *dynamic, LY_PREFIX_FORMAT format, |
| 505 | void *prefix_data, uint32_t hints) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 506 | { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 507 | ly_bool incomplete; |
Michal Vasko | b68571a | 2020-11-06 17:18:41 +0100 | [diff] [blame] | 508 | struct lyd_meta *first = NULL; |
| 509 | |
| 510 | if (meta && *meta) { |
| 511 | /* remember the first metadata */ |
| 512 | first = *meta; |
| 513 | } |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 514 | |
| 515 | LY_CHECK_RET(lyd_create_meta(parent, meta, mod, name, name_len, value, value_len, dynamic, format, prefix_data, |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 516 | hints, 0, &incomplete)); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 517 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 518 | if (incomplete && !(lydctx->parse_opts & LYD_PARSE_ONLY)) { |
Michal Vasko | 3271138 | 2020-12-03 14:14:31 +0100 | [diff] [blame] | 519 | LY_CHECK_RET(ly_set_add(&lydctx->meta_types, *meta, 1, NULL)); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 520 | } |
Michal Vasko | b68571a | 2020-11-06 17:18:41 +0100 | [diff] [blame] | 521 | |
| 522 | if (first) { |
| 523 | /* always return the first metadata */ |
| 524 | *meta = first; |
| 525 | } |
| 526 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 527 | return LY_SUCCESS; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 528 | } |