blob: 26f4226e6eba2d6f509709a762ad335dec222670 [file] [log] [blame]
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001/**
Michal Vaskoafac7822020-10-20 14:22:26 +02002 * @file in.c
Radek Krejcif0e1ba52020-05-22 15:14:35 +02003 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vaskoafac7822020-10-20 14:22:26 +02004 * @brief libyang input functions.
Radek Krejcif0e1ba52020-05-22 15:14:35 +02005 *
Michal Vaskoafac7822020-10-20 14:22:26 +02006 * Copyright (c) 2015 - 2020 CESNET, z.s.p.o.
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007 *
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 Krejcif8dc59a2020-11-25 13:47:44 +010016#define _POSIX_C_SOURCE 200809L /* strdup, strndup */
17
18#ifdef __APPLE__
19#define _DARWIN_C_SOURCE /* F_GETPATH */
20#endif
Radek Krejcif0e1ba52020-05-22 15:14:35 +020021
Michal Vaskoafac7822020-10-20 14:22:26 +020022#include "in.h"
23#include "in_internal.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020024
Radek Krejcif0e1ba52020-05-22 15:14:35 +020025#include <errno.h>
26#include <fcntl.h>
27#include <limits.h>
Radek Krejci47fab892020-11-05 17:02:41 +010028#include <stdint.h>
Radek Krejcif0e1ba52020-05-22 15:14:35 +020029#include <stdio.h>
Radek Krejcica376bd2020-06-11 16:04:06 +020030#include <stdlib.h>
Radek Krejcif0e1ba52020-05-22 15:14:35 +020031#include <string.h>
Radek Krejcica376bd2020-06-11 16:04:06 +020032#include <unistd.h>
Radek Krejcif0e1ba52020-05-22 15:14:35 +020033
Radek Krejcica376bd2020-06-11 16:04:06 +020034#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020035#include "compat.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020036#include "dict.h"
37#include "log.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020038#include "parser_data.h"
Radek Krejcif0e1ba52020-05-22 15:14:35 +020039#include "parser_internal.h"
Radek Krejci47fab892020-11-05 17:02:41 +010040#include "set.h"
41#include "tree_data.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020042#include "tree_data_internal.h"
Radek Krejci47fab892020-11-05 17:02:41 +010043#include "tree_schema.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020044#include "tree_schema_internal.h"
Radek Krejcif0e1ba52020-05-22 15:14:35 +020045
46API LY_IN_TYPE
47ly_in_type(const struct ly_in *in)
48{
49 LY_CHECK_ARG_RET(NULL, in, LY_IN_ERROR);
50 return in->type;
51}
52
53API LY_ERR
54ly_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 Vasko63f3d842020-07-08 10:10:14 +020072 (*in)->current = (*in)->start = (*in)->func_start = addr;
Radek Krejcif0e1ba52020-05-22 15:14:35 +020073 (*in)->length = length;
74
75 return LY_SUCCESS;
76}
77
78API int
79ly_in_fd(struct ly_in *in, int fd)
80{
81 int prev_fd;
82 size_t length;
83 const char *addr;
84
85 LY_CHECK_ARG_RET(NULL, in, in->type == LY_IN_FD, -1);
86
87 prev_fd = in->method.fd;
88
89 if (fd != -1) {
90 LY_CHECK_RET(ly_mmap(NULL, fd, &length, (void **)&addr), -1);
91 if (!addr) {
92 LOGERR(NULL, LY_EINVAL, "Empty input file.");
93 return -1;
94 }
95
Michal Vasko22df3f02020-08-24 13:29:22 +020096 ly_munmap((char *)in->start, in->length);
Radek Krejcif0e1ba52020-05-22 15:14:35 +020097
98 in->method.fd = fd;
99 in->current = in->start = addr;
100 in->length = length;
101 }
102
103 return prev_fd;
104}
105
106API LY_ERR
107ly_in_new_file(FILE *f, struct ly_in **in)
108{
109 LY_CHECK_ARG_RET(NULL, f, in, LY_EINVAL);
110
111 LY_CHECK_RET(ly_in_new_fd(fileno(f), in));
112
113 /* convert the LY_IN_FD input handler into the LY_IN_FILE */
114 (*in)->type = LY_IN_FILE;
115 (*in)->method.f = f;
116
117 return LY_SUCCESS;
118}
119
120API FILE *
121ly_in_file(struct ly_in *in, FILE *f)
122{
123 FILE *prev_f;
124
125 LY_CHECK_ARG_RET(NULL, in, in->type == LY_IN_FILE, NULL);
126
127 prev_f = in->method.f;
128
129 if (f) {
130 /* convert LY_IN_FILE handler into LY_IN_FD to be able to update it via ly_in_fd() */
131 in->type = LY_IN_FD;
132 in->method.fd = fileno(prev_f);
133 if (ly_in_fd(in, fileno(f)) == -1) {
134 in->type = LY_IN_FILE;
135 in->method.f = prev_f;
136 return NULL;
137 }
138
139 /* if success, convert the result back */
140 in->type = LY_IN_FILE;
141 in->method.f = f;
142 }
143
144 return prev_f;
145}
146
147API LY_ERR
148ly_in_new_memory(const char *str, struct ly_in **in)
149{
150 LY_CHECK_ARG_RET(NULL, str, in, LY_EINVAL);
151
152 *in = calloc(1, sizeof **in);
153 LY_CHECK_ERR_RET(!*in, LOGMEM(NULL), LY_EMEM);
154
155 (*in)->type = LY_IN_MEMORY;
Michal Vasko63f3d842020-07-08 10:10:14 +0200156 (*in)->start = (*in)->current = (*in)->func_start = str;
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200157
158 return LY_SUCCESS;
159}
160
Michal Vasko63f3d842020-07-08 10:10:14 +0200161API const char *
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200162ly_in_memory(struct ly_in *in, const char *str)
163{
164 const char *data;
165
166 LY_CHECK_ARG_RET(NULL, in, in->type == LY_IN_MEMORY, NULL);
167
168 data = in->current;
169
170 if (str) {
171 in->start = in->current = str;
172 }
173
174 return data;
175}
176
177API LY_ERR
178ly_in_reset(struct ly_in *in)
179{
180 LY_CHECK_ARG_RET(NULL, in, LY_EINVAL);
181
Michal Vasko63f3d842020-07-08 10:10:14 +0200182 in->current = in->func_start = in->start;
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200183 return LY_SUCCESS;
184}
185
186API LY_ERR
187ly_in_new_filepath(const char *filepath, size_t len, struct ly_in **in)
188{
Radek Krejci0f969882020-08-21 16:56:47 +0200189 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200190 char *fp;
191 int fd;
192
193 LY_CHECK_ARG_RET(NULL, filepath, in, LY_EINVAL);
194
195 if (len) {
196 fp = strndup(filepath, len);
197 } else {
198 fp = strdup(filepath);
199 }
200
201 fd = open(fp, O_RDONLY);
Michal Vaskof2eb8af2020-07-14 12:22:40 +0200202 LY_CHECK_ERR_RET(fd == -1, LOGERR(NULL, LY_ESYS, "Failed to open file \"%s\" (%s).", fp, strerror(errno)); free(fp),
Michal Vasko69730152020-10-09 16:30:07 +0200203 LY_ESYS);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200204
205 LY_CHECK_ERR_RET(ret = ly_in_new_fd(fd, in), free(fp), ret);
206
207 /* convert the LY_IN_FD input handler into the LY_IN_FILE */
208 (*in)->type = LY_IN_FILEPATH;
209 (*in)->method.fpath.fd = fd;
210 (*in)->method.fpath.filepath = fp;
211
212 return LY_SUCCESS;
213}
214
215API const char *
216ly_in_filepath(struct ly_in *in, const char *filepath, size_t len)
217{
218 int fd, prev_fd;
219 char *fp = NULL;
220
221 LY_CHECK_ARG_RET(NULL, in, in->type == LY_IN_FILEPATH, filepath ? NULL : ((void *)-1));
222
223 if (!filepath) {
224 return in->method.fpath.filepath;
225 }
226
227 if (len) {
228 fp = strndup(filepath, len);
229 } else {
230 fp = strdup(filepath);
231 }
232
233 /* replace filepath */
234 fd = open(fp, O_RDONLY);
235 LY_CHECK_ERR_RET(!fd, LOGERR(NULL, LY_ESYS, "Failed to open file \"%s\" (%s).", fp, strerror(errno)); free(fp), NULL);
236
237 /* convert LY_IN_FILEPATH handler into LY_IN_FD to be able to update it via ly_in_fd() */
238 in->type = LY_IN_FD;
239 prev_fd = ly_in_fd(in, fd);
240 LY_CHECK_ERR_RET(prev_fd == -1, in->type = LY_IN_FILEPATH; free(fp), NULL);
241
242 /* and convert the result back */
243 in->type = LY_IN_FILEPATH;
244 close(prev_fd);
245 free(in->method.fpath.filepath);
246 in->method.fpath.fd = fd;
247 in->method.fpath.filepath = fp;
248
249 return NULL;
250}
251
252void
253lys_parser_fill_filepath(struct ly_ctx *ctx, struct ly_in *in, const char **filepath)
254{
255 char path[PATH_MAX];
Michal Vasko69730152020-10-09 16:30:07 +0200256
Michal Vasko5aa44c02020-06-29 11:47:02 +0200257#ifndef __APPLE__
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200258 char proc_path[32];
259 int len;
Michal Vasko5aa44c02020-06-29 11:47:02 +0200260#endif
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200261
262 LY_CHECK_ARG_RET(NULL, ctx, in, filepath, );
263 if (*filepath) {
264 /* filepath already set */
265 return;
266 }
267
268 switch (in->type) {
269 case LY_IN_FILEPATH:
270 if (realpath(in->method.fpath.filepath, path) != NULL) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200271 lydict_insert(ctx, path, 0, filepath);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200272 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200273 lydict_insert(ctx, in->method.fpath.filepath, 0, filepath);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200274 }
275
276 break;
277 case LY_IN_FD:
278#ifdef __APPLE__
279 if (fcntl(in->method.fd, F_GETPATH, path) != -1) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200280 lydict_insert(ctx, path, 0, filepath);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200281 }
282#else
283 /* get URI if there is /proc */
284 sprintf(proc_path, "/proc/self/fd/%d", in->method.fd);
285 if ((len = readlink(proc_path, path, PATH_MAX - 1)) > 0) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200286 lydict_insert(ctx, path, len, filepath);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200287 }
288#endif
289 break;
290 case LY_IN_MEMORY:
291 case LY_IN_FILE:
292 /* nothing to do */
293 break;
294 default:
295 LOGINT(ctx);
296 break;
297 }
298
299}
300
301API void
Radek Krejci857189e2020-09-01 13:26:36 +0200302ly_in_free(struct ly_in *in, ly_bool destroy)
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200303{
304 if (!in) {
305 return;
306 } else if (in->type == LY_IN_ERROR) {
307 LOGINT(NULL);
308 return;
309 }
310
311 if (destroy) {
312 if (in->type == LY_IN_MEMORY) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200313 free((char *)in->start);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200314 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +0200315 ly_munmap((char *)in->start, in->length);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200316
317 if (in->type == LY_IN_FILE) {
318 fclose(in->method.f);
319 } else {
320 close(in->method.fd);
321
322 if (in->type == LY_IN_FILEPATH) {
323 free(in->method.fpath.filepath);
324 }
325 }
326 }
327 } else if (in->type != LY_IN_MEMORY) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200328 ly_munmap((char *)in->start, in->length);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200329
330 if (in->type == LY_IN_FILEPATH) {
331 close(in->method.fpath.fd);
332 free(in->method.fpath.filepath);
333 }
334 }
335
336 free(in);
337}
Michal Vasko63f3d842020-07-08 10:10:14 +0200338
339LY_ERR
340ly_in_read(struct ly_in *in, void *buf, size_t count)
341{
342 if (in->length && (in->length - (in->current - in->start) < count)) {
343 /* EOF */
344 return LY_EDENIED;
345 }
346
347 memcpy(buf, in->current, count);
348 in->current += count;
349 return LY_SUCCESS;
350}
351
352API size_t
353ly_in_parsed(const struct ly_in *in)
354{
355 return in->current - in->func_start;
356}
357
358LY_ERR
359ly_in_skip(struct ly_in *in, size_t count)
360{
361 if (in->length && (in->length - (in->current - in->start) < count)) {
362 /* EOF */
363 return LY_EDENIED;
364 }
365
366 in->current += count;
367 return LY_SUCCESS;
368}
Radek Krejci1798aae2020-07-14 13:26:06 +0200369
370void
371lyd_ctx_free(struct lyd_ctx *lydctx)
372{
Michal Vasko32711382020-12-03 14:14:31 +0100373 ly_set_erase(&lydctx->node_types, NULL);
374 ly_set_erase(&lydctx->meta_types, NULL);
375 ly_set_erase(&lydctx->node_when, NULL);
Radek Krejci1798aae2020-07-14 13:26:06 +0200376}
377
378LY_ERR
379lyd_parser_check_schema(struct lyd_ctx *lydctx, const struct lysc_node *snode)
380{
381 /* alternatively, we could provide line for the error messages, but it doesn't work for the LYB format */
382
383 if ((lydctx->parse_options & LYD_PARSE_NO_STATE) && (snode->flags & LYS_CONFIG_R)) {
384 LOGVAL(lydctx->data_ctx->ctx, LY_VLOG_LYSC, snode, LY_VCODE_INNODE, "state", snode->name);
385 return LY_EVALID;
386 }
387
388 if (snode->nodetype & (LYS_RPC | LYS_ACTION)) {
Michal Vasko2552ea32020-12-08 15:32:34 +0100389 if (lydctx->int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_REPLY)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200390 if (lydctx->op_node) {
391 LOGVAL(lydctx->data_ctx->ctx, LY_VLOG_LYSC, snode, LYVE_DATA, "Unexpected %s element \"%s\", %s \"%s\" already parsed.",
Michal Vasko69730152020-10-09 16:30:07 +0200392 lys_nodetype2str(snode->nodetype), snode->name,
393 lys_nodetype2str(lydctx->op_node->schema->nodetype), lydctx->op_node->schema->name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200394 return LY_EVALID;
395 }
396 } else {
397 LOGVAL(lydctx->data_ctx->ctx, LY_VLOG_LYSC, snode, LYVE_DATA, "Unexpected %s element \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +0200398 lys_nodetype2str(snode->nodetype), snode->name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200399 return LY_EVALID;
400 }
401 } else if (snode->nodetype == LYS_NOTIF) {
402 if (lydctx->int_opts & LYD_INTOPT_NOTIF) {
403 if (lydctx->op_node) {
404 LOGVAL(lydctx->data_ctx->ctx, LY_VLOG_LYSC, snode, LYVE_DATA, "Unexpected %s element \"%s\", %s \"%s\" already parsed.",
Michal Vasko69730152020-10-09 16:30:07 +0200405 lys_nodetype2str(snode->nodetype), snode->name,
406 lys_nodetype2str(lydctx->op_node->schema->nodetype), lydctx->op_node->schema->name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200407 return LY_EVALID;
408 }
409 } else {
410 LOGVAL(lydctx->data_ctx->ctx, LY_VLOG_LYSC, snode, LYVE_DATA, "Unexpected %s element \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +0200411 lys_nodetype2str(snode->nodetype), snode->name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200412 return LY_EVALID;
413 }
414 }
415
416 return LY_SUCCESS;
417}
418
419LY_ERR
420lyd_parser_create_term(struct lyd_ctx *lydctx, const struct lysc_node *schema, const char *value, size_t value_len,
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200421 ly_bool *dynamic, LY_PREFIX_FORMAT format, void *prefix_data, uint32_t hints, struct lyd_node **node)
Radek Krejci1798aae2020-07-14 13:26:06 +0200422{
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200423 ly_bool incomplete;
Radek Krejci1798aae2020-07-14 13:26:06 +0200424
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200425 LY_CHECK_RET(lyd_create_term(schema, value, value_len, dynamic, format, prefix_data, hints, &incomplete, node));
426
427 if (incomplete && !(lydctx->parse_options & LYD_PARSE_ONLY)) {
Michal Vasko32711382020-12-03 14:14:31 +0100428 LY_CHECK_RET(ly_set_add(&lydctx->node_types, *node, 1, NULL));
Radek Krejci1798aae2020-07-14 13:26:06 +0200429 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200430 return LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +0200431}
432
433LY_ERR
434lyd_parser_create_meta(struct lyd_ctx *lydctx, struct lyd_node *parent, struct lyd_meta **meta, const struct lys_module *mod,
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200435 const char *name, size_t name_len, const char *value, size_t value_len, ly_bool *dynamic, LY_PREFIX_FORMAT format,
436 void *prefix_data, uint32_t hints)
Radek Krejci1798aae2020-07-14 13:26:06 +0200437{
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200438 ly_bool incomplete;
Michal Vaskob68571a2020-11-06 17:18:41 +0100439 struct lyd_meta *first = NULL;
440
441 if (meta && *meta) {
442 /* remember the first metadata */
443 first = *meta;
444 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200445
446 LY_CHECK_RET(lyd_create_meta(parent, meta, mod, name, name_len, value, value_len, dynamic, format, prefix_data,
Michal Vasko871a0252020-11-11 18:35:24 +0100447 hints, 0, &incomplete));
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200448
449 if (incomplete && !(lydctx->parse_options & LYD_PARSE_ONLY)) {
Michal Vasko32711382020-12-03 14:14:31 +0100450 LY_CHECK_RET(ly_set_add(&lydctx->meta_types, *meta, 1, NULL));
Radek Krejci1798aae2020-07-14 13:26:06 +0200451 }
Michal Vaskob68571a2020-11-06 17:18:41 +0100452
453 if (first) {
454 /* always return the first metadata */
455 *meta = first;
456 }
457
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200458 return LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +0200459}