blob: 4a55eb64986f9c216bb50cd26d43322471d3a03e [file] [log] [blame]
Radek Krejci3f5e3db2018-10-11 15:57:47 +02001/**
2 * @file tree_schema.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema tree implementation
5 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Radek Krejcib7db73a2018-10-24 14:18:40 +020014
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#define _GNU_SOURCE
Radek Krejcif8dc59a2020-11-25 13:47:44 +010016#define _POSIX_C_SOURCE 200809L /* strdup */
Radek Krejci535ea9f2020-05-29 16:01:05 +020017
Radek Krejcica376bd2020-06-11 16:04:06 +020018#include "tree_schema.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020019
Radek Krejcie7b95092019-05-15 11:03:07 +020020#include <assert.h>
Radek Krejci545b4872020-11-15 10:15:12 +010021#include <ctype.h>
Radek Krejcid33273d2018-10-25 14:55:52 +020022#include <dirent.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020023#include <errno.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020024#include <stdint.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020025#include <stdio.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include <stdlib.h>
27#include <string.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020028#include <sys/stat.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020029#include <unistd.h>
Radek Krejci3f5e3db2018-10-11 15:57:47 +020030
Radek Krejcica376bd2020-06-11 16:04:06 +020031#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020032#include "compat.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020033#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020034#include "dict.h"
Radek Krejci47fab892020-11-05 17:02:41 +010035#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020036#include "in_internal.h"
Radek Krejci47fab892020-11-05 17:02:41 +010037#include "log.h"
Radek Krejcif0e1ba52020-05-22 15:14:35 +020038#include "parser_internal.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020039#include "parser_schema.h"
Michal Vasko40308e72020-10-20 16:38:40 +020040#include "path.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020041#include "schema_compile.h"
42#include "schema_compile_amend.h"
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010043#include "schema_features.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020044#include "set.h"
45#include "tree.h"
Radek Krejci47fab892020-11-05 17:02:41 +010046#include "tree_data.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020047#include "tree_schema_internal.h"
48#include "xpath.h"
Radek Krejci3f5e3db2018-10-11 15:57:47 +020049
Michal Vaskof1ab44f2020-10-22 08:58:32 +020050API LY_ERR
51lysc_tree_dfs_full(const struct lysc_node *root, lysc_dfs_clb dfs_clb, void *data)
52{
Michal Vasko1d972ca2020-11-03 17:16:56 +010053 struct lysc_node *elem, *elem2;
54 const struct lysc_action *acts;
55 const struct lysc_notif *notifs;
Michal Vaskof1ab44f2020-10-22 08:58:32 +020056 LY_ARRAY_COUNT_TYPE u;
57
58 LY_CHECK_ARG_RET(NULL, root, dfs_clb, LY_EINVAL);
59
60 LYSC_TREE_DFS_BEGIN(root, elem) {
61 /* schema node */
62 LY_CHECK_RET(dfs_clb(elem, data, &LYSC_TREE_DFS_continue));
63
Michal Vasko1d972ca2020-11-03 17:16:56 +010064 acts = lysc_node_actions(elem);
65 LY_ARRAY_FOR(acts, u) {
66 LYSC_TREE_DFS_BEGIN(&acts[u], elem2) {
Michal Vaskof1ab44f2020-10-22 08:58:32 +020067 /* action subtree */
68 LY_CHECK_RET(dfs_clb(elem2, data, &LYSC_TREE_DFS_continue));
69
Michal Vasko1d972ca2020-11-03 17:16:56 +010070 LYSC_TREE_DFS_END(&acts[u], elem2);
Michal Vaskof1ab44f2020-10-22 08:58:32 +020071 }
72 }
73
Michal Vasko1d972ca2020-11-03 17:16:56 +010074 notifs = lysc_node_notifs(elem);
75 LY_ARRAY_FOR(notifs, u) {
76 LYSC_TREE_DFS_BEGIN(&notifs[u], elem2) {
Michal Vaskof1ab44f2020-10-22 08:58:32 +020077 /* notification subtree */
78 LY_CHECK_RET(dfs_clb(elem2, data, &LYSC_TREE_DFS_continue));
79
Michal Vasko1d972ca2020-11-03 17:16:56 +010080 LYSC_TREE_DFS_END(&notifs[u], elem2);
Michal Vaskof1ab44f2020-10-22 08:58:32 +020081 }
82 }
83
84 LYSC_TREE_DFS_END(root, elem);
85 }
86
87 return LY_SUCCESS;
88}
89
90API LY_ERR
91lysc_module_dfs_full(const struct lys_module *mod, lysc_dfs_clb dfs_clb, void *data)
92{
93 LY_ARRAY_COUNT_TYPE u;
Michal Vasko2336cf52020-11-03 17:18:15 +010094 const struct lysc_node *root;
Michal Vaskof1ab44f2020-10-22 08:58:32 +020095
96 LY_CHECK_ARG_RET(NULL, mod, mod->compiled, dfs_clb, LY_EINVAL);
97
98 /* schema nodes */
Michal Vasko2336cf52020-11-03 17:18:15 +010099 LY_LIST_FOR(mod->compiled->data, root) {
100 LY_CHECK_RET(lysc_tree_dfs_full(root, dfs_clb, data));
101 }
Michal Vaskof1ab44f2020-10-22 08:58:32 +0200102
103 /* RPCs */
104 LY_ARRAY_FOR(mod->compiled->rpcs, u) {
105 LY_CHECK_RET(lysc_tree_dfs_full((struct lysc_node *)&mod->compiled->rpcs[u], dfs_clb, data));
106 }
107
108 /* notifications */
109 LY_ARRAY_FOR(mod->compiled->notifs, u) {
110 LY_CHECK_RET(lysc_tree_dfs_full((struct lysc_node *)&mod->compiled->notifs[u], dfs_clb, data));
111 }
112
113 return LY_SUCCESS;
114}
115
Radek Krejcib93bd412020-11-02 13:23:11 +0100116static void
117lys_getnext_into_case(const struct lysc_node_case *first_case, const struct lysc_node **last, const struct lysc_node **next)
118{
Radek Krejcic5b54a02020-11-05 17:13:18 +0100119 for ( ; first_case; first_case = (const struct lysc_node_case *)first_case->next) {
Radek Krejcib93bd412020-11-02 13:23:11 +0100120 if (first_case->child) {
121 /* there is something to return */
122 (*next) = first_case->child;
123 return;
124 }
125 }
126
127 /* no children in choice's cases, so go to the choice's sibling instead of into it */
128 (*last) = (*next);
129 (*next) = (*next)->next;
130}
131
Radek Krejcia3045382018-11-22 14:30:31 +0100132API const struct lysc_node *
Radek Krejci1deb5be2020-08-26 16:43:36 +0200133lys_getnext(const struct lysc_node *last, const struct lysc_node *parent, const struct lysc_module *module, uint32_t options)
Radek Krejcia3045382018-11-22 14:30:31 +0100134{
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100135 const struct lysc_node *next = NULL;
Radek Krejcia3045382018-11-22 14:30:31 +0100136 struct lysc_node **snode;
Radek Krejci857189e2020-09-01 13:26:36 +0200137 ly_bool action_flag = 0, notif_flag = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100138 const struct lysc_action *actions;
139 const struct lysc_notif *notifs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200140 LY_ARRAY_COUNT_TYPE u;
Radek Krejcia3045382018-11-22 14:30:31 +0100141
142 LY_CHECK_ARG_RET(NULL, parent || module, NULL);
143
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200144next:
Radek Krejcia3045382018-11-22 14:30:31 +0100145 if (!last) {
146 /* first call */
147
148 /* get know where to start */
149 if (parent) {
150 /* schema subtree */
Michal Vasko69730152020-10-09 16:30:07 +0200151 if ((parent->nodetype == LYS_CHOICE) && (options & LYS_GETNEXT_WITHCASE)) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200152 if (((struct lysc_node_choice *)parent)->cases) {
Radek Krejcib93bd412020-11-02 13:23:11 +0100153 next = last = (const struct lysc_node *)((struct lysc_node_choice *)parent)->cases;
Radek Krejci056d0a82018-12-06 16:57:25 +0100154 }
Radek Krejci056d0a82018-12-06 16:57:25 +0100155 } else {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100156 snode = lysc_node_children_p(parent, (options & LYS_GETNEXT_OUTPUT) ? LYS_CONFIG_R : LYS_CONFIG_W);
Radek Krejci05b774b2019-02-25 13:26:18 +0100157 /* do not return anything if the node does not have any children */
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200158 if (snode && *snode) {
159 next = last = *snode;
Radek Krejci056d0a82018-12-06 16:57:25 +0100160 }
Radek Krejcia3045382018-11-22 14:30:31 +0100161 }
Radek Krejcia3045382018-11-22 14:30:31 +0100162 } else {
163 /* top level data */
164 next = last = module->data;
165 }
166 if (!next) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100167 /* try to get action or notification */
168 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100169 }
Radek Krejci05b774b2019-02-25 13:26:18 +0100170 /* test if the next can be returned */
171 goto check;
172
Michal Vasko1bf09392020-03-27 12:38:10 +0100173 } else if (last->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +0100174 action_flag = 1;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100175 if (last->parent) {
176 actions = lysc_node_actions(last->parent);
177 } else {
178 actions = module->rpcs;
179 }
180 LY_ARRAY_FOR(actions, u) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200181 if (&actions[u] == (struct lysc_action *)last) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100182 break;
183 }
184 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200185 if (u + 1 < LY_ARRAY_COUNT(actions)) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200186 next = (struct lysc_node *)(&actions[u + 1]);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100187 }
188 goto repeat;
189 } else if (last->nodetype == LYS_NOTIF) {
Radek Krejci05b774b2019-02-25 13:26:18 +0100190 action_flag = notif_flag = 1;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100191 if (last->parent) {
192 notifs = lysc_node_notifs(last->parent);
193 } else {
194 notifs = module->notifs;
195 }
196 LY_ARRAY_FOR(notifs, u) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200197 if (&notifs[u] == (struct lysc_notif *)last) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100198 break;
199 }
200 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200201 if (u + 1 < LY_ARRAY_COUNT(notifs)) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200202 next = (struct lysc_node *)(&notifs[u + 1]);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100203 }
204 goto repeat;
Michal Vasko20424b42020-08-31 12:29:38 +0200205 } else {
206 next = last->next;
Radek Krejcia3045382018-11-22 14:30:31 +0100207 }
208
Radek Krejcia3045382018-11-22 14:30:31 +0100209repeat:
210 if (!next) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100211 /* possibly go back to parent */
Michal Vasko69730152020-10-09 16:30:07 +0200212 if (last && (last->parent != parent)) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100213 last = last->parent;
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200214 goto next;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100215 } else if (!action_flag) {
216 action_flag = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +0200217 next = parent ? (struct lysc_node *)lysc_node_actions(parent) : (struct lysc_node *)module->rpcs;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100218 } else if (!notif_flag) {
219 notif_flag = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +0200220 next = parent ? (struct lysc_node *)lysc_node_notifs(parent) : (struct lysc_node *)module->notifs;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100221 } else {
222 return NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100223 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100224 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100225 }
Radek Krejci05b774b2019-02-25 13:26:18 +0100226check:
Radek Krejcia3045382018-11-22 14:30:31 +0100227 switch (next->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +0100228 case LYS_RPC:
Radek Krejcia3045382018-11-22 14:30:31 +0100229 case LYS_ACTION:
230 case LYS_NOTIF:
231 case LYS_LEAF:
232 case LYS_ANYXML:
233 case LYS_ANYDATA:
234 case LYS_LIST:
235 case LYS_LEAFLIST:
236 break;
Michal Vasko20424b42020-08-31 12:29:38 +0200237 case LYS_CASE:
238 if (options & LYS_GETNEXT_WITHCASE) {
239 break;
240 } else {
241 /* go into */
Radek Krejcib93bd412020-11-02 13:23:11 +0100242 lys_getnext_into_case((const struct lysc_node_case *)next, &last, &next);
Michal Vasko20424b42020-08-31 12:29:38 +0200243 }
244 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100245 case LYS_CONTAINER:
246 if (!(((struct lysc_node_container *)next)->flags & LYS_PRESENCE) && (options & LYS_GETNEXT_INTONPCONT)) {
247 if (((struct lysc_node_container *)next)->child) {
248 /* go into */
249 next = ((struct lysc_node_container *)next)->child;
250 } else {
Radek Krejcib93bd412020-11-02 13:23:11 +0100251 last = next;
Radek Krejcia3045382018-11-22 14:30:31 +0100252 next = next->next;
253 }
254 goto repeat;
255 }
256 break;
257 case LYS_CHOICE:
258 if (options & LYS_GETNEXT_WITHCHOICE) {
Michal Vasko20424b42020-08-31 12:29:38 +0200259 break;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100260 } else if ((options & LYS_GETNEXT_NOCHOICE) || !((struct lysc_node_choice *)next)->cases) {
261 next = next->next;
262 } else {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100263 if (options & LYS_GETNEXT_WITHCASE) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200264 next = (struct lysc_node *)((struct lysc_node_choice *)next)->cases;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100265 } else {
Radek Krejcib93bd412020-11-02 13:23:11 +0100266 /* go into */
267 lys_getnext_into_case(((struct lysc_node_choice *)next)->cases, &last, &next);
Radek Krejcia9026eb2018-12-12 16:04:47 +0100268 }
Radek Krejcia3045382018-11-22 14:30:31 +0100269 }
270 goto repeat;
271 default:
272 /* we should not be here */
Radek Krejcib07b5c92019-04-08 10:56:37 +0200273 LOGINT(module ? module->mod->ctx : parent->module->ctx);
Radek Krejcia3045382018-11-22 14:30:31 +0100274 return NULL;
275 }
276
Radek Krejcia3045382018-11-22 14:30:31 +0100277 return next;
278}
279
280API const struct lysc_node *
Michal Vaskoe444f752020-02-10 12:20:06 +0100281lys_find_child(const struct lysc_node *parent, const struct lys_module *module, const char *name, size_t name_len,
Radek Krejci1deb5be2020-08-26 16:43:36 +0200282 uint16_t nodetype, uint32_t options)
Radek Krejcia3045382018-11-22 14:30:31 +0100283{
284 const struct lysc_node *node = NULL;
285
286 LY_CHECK_ARG_RET(NULL, module, name, NULL);
287 if (!nodetype) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100288 nodetype = LYS_NODETYPE_MASK;
Radek Krejcia3045382018-11-22 14:30:31 +0100289 }
290
291 while ((node = lys_getnext(node, parent, module->compiled, options))) {
292 if (!(node->nodetype & nodetype)) {
293 continue;
294 }
295 if (node->module != module) {
296 continue;
297 }
298
299 if (name_len) {
Radek Krejci7f9b6512019-09-18 13:11:09 +0200300 if (!ly_strncmp(node->name, name, name_len)) {
Radek Krejcia3045382018-11-22 14:30:31 +0100301 return node;
302 }
303 } else {
304 if (!strcmp(node->name, name)) {
305 return node;
306 }
307 }
308 }
309 return NULL;
310}
311
Michal Vasko519fd602020-05-26 12:17:39 +0200312API LY_ERR
Michal Vasko26512682021-01-11 11:35:40 +0100313lys_find_xpath_atoms(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *xpath, uint32_t options,
314 struct ly_set **set)
Michal Vasko519fd602020-05-26 12:17:39 +0200315{
316 LY_ERR ret = LY_SUCCESS;
317 struct lyxp_set xp_set;
Radek Krejcif03a9e22020-09-18 20:09:31 +0200318 struct lyxp_expr *exp = NULL;
Michal Vasko519fd602020-05-26 12:17:39 +0200319 uint32_t i;
320
Michal Vasko26512682021-01-11 11:35:40 +0100321 LY_CHECK_ARG_RET(NULL, ctx || ctx_node, xpath, set, LY_EINVAL);
Michal Vasko519fd602020-05-26 12:17:39 +0200322 if (!(options & LYXP_SCNODE_ALL)) {
323 options = LYXP_SCNODE;
324 }
Michal Vasko26512682021-01-11 11:35:40 +0100325 if (!ctx) {
326 ctx = ctx_node->module->ctx;
327 }
Michal Vasko519fd602020-05-26 12:17:39 +0200328
329 memset(&xp_set, 0, sizeof xp_set);
330
331 /* compile expression */
Michal Vasko26512682021-01-11 11:35:40 +0100332 ret = lyxp_expr_parse(ctx, xpath, 0, 1, &exp);
Radek Krejcif03a9e22020-09-18 20:09:31 +0200333 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200334
335 /* atomize expression */
Michal Vasko400e9672021-01-11 13:39:17 +0100336 ret = lyxp_atomize(ctx, exp, NULL, LY_PREF_JSON, NULL, ctx_node, &xp_set, options);
Michal Vasko519fd602020-05-26 12:17:39 +0200337 LY_CHECK_GOTO(ret, cleanup);
338
339 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200340 ret = ly_set_new(set);
341 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200342
343 /* transform into ly_set */
344 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
Michal Vasko26512682021-01-11 11:35:40 +0100345 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200346 (*set)->size = xp_set.used;
347
348 for (i = 0; i < xp_set.used; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +0200349 if (xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200350 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200351 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200352 }
353 }
354
355cleanup:
356 lyxp_set_free_content(&xp_set);
Michal Vasko26512682021-01-11 11:35:40 +0100357 lyxp_expr_free(ctx, exp);
Michal Vasko519fd602020-05-26 12:17:39 +0200358 return ret;
359}
360
Michal Vasko072de482020-08-05 13:27:21 +0200361API LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +0200362lys_find_expr_atoms(const struct lysc_node *ctx_node, const struct lys_module *cur_mod, const struct lyxp_expr *expr,
363 const struct lysc_prefix *prefixes, uint32_t options, struct ly_set **set)
364{
365 LY_ERR ret = LY_SUCCESS;
366 struct lyxp_set xp_set = {0};
367 uint32_t i;
368
369 LY_CHECK_ARG_RET(NULL, cur_mod, expr, prefixes, set, LY_EINVAL);
370 if (!(options & LYXP_SCNODE_ALL)) {
371 options = LYXP_SCNODE;
372 }
373
374 /* atomize expression */
Michal Vasko400e9672021-01-11 13:39:17 +0100375 ret = lyxp_atomize(cur_mod->ctx, expr, cur_mod, LY_PREF_SCHEMA_RESOLVED, (void *)prefixes, ctx_node, &xp_set, options);
Michal Vasko40308e72020-10-20 16:38:40 +0200376 LY_CHECK_GOTO(ret, cleanup);
377
378 /* allocate return set */
379 ret = ly_set_new(set);
380 LY_CHECK_GOTO(ret, cleanup);
381
382 /* transform into ly_set */
383 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
384 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(cur_mod->ctx); ret = LY_EMEM, cleanup);
385 (*set)->size = xp_set.used;
386
387 for (i = 0; i < xp_set.used; ++i) {
Michal Vaskod97959c2020-12-10 12:18:28 +0100388 if ((xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (xp_set.val.scnodes[i].in_ctx >= LYXP_SET_SCNODE_ATOM)) {
389 assert((xp_set.val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM) ||
390 (xp_set.val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX));
Michal Vasko40308e72020-10-20 16:38:40 +0200391 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
392 LY_CHECK_GOTO(ret, cleanup);
393 }
394 }
395
396cleanup:
397 lyxp_set_free_content(&xp_set);
398 if (ret) {
399 ly_set_free(*set, NULL);
400 *set = NULL;
401 }
402 return ret;
403}
404
405API LY_ERR
Michal Vasko26512682021-01-11 11:35:40 +0100406lys_find_xpath(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *xpath, uint32_t options,
407 struct ly_set **set)
Michal Vasko072de482020-08-05 13:27:21 +0200408{
409 LY_ERR ret = LY_SUCCESS;
Michal Vasko40308e72020-10-20 16:38:40 +0200410 struct lyxp_set xp_set = {0};
Radek Krejcif03a9e22020-09-18 20:09:31 +0200411 struct lyxp_expr *exp = NULL;
Michal Vasko072de482020-08-05 13:27:21 +0200412 uint32_t i;
413
Michal Vasko26512682021-01-11 11:35:40 +0100414 LY_CHECK_ARG_RET(NULL, ctx || ctx_node, xpath, set, LY_EINVAL);
Michal Vasko072de482020-08-05 13:27:21 +0200415 if (!(options & LYXP_SCNODE_ALL)) {
416 options = LYXP_SCNODE;
417 }
Michal Vasko26512682021-01-11 11:35:40 +0100418 if (!ctx) {
419 ctx = ctx_node->module->ctx;
420 }
Michal Vasko072de482020-08-05 13:27:21 +0200421
Michal Vasko072de482020-08-05 13:27:21 +0200422 /* compile expression */
Michal Vasko26512682021-01-11 11:35:40 +0100423 ret = lyxp_expr_parse(ctx, xpath, 0, 1, &exp);
Radek Krejcif03a9e22020-09-18 20:09:31 +0200424 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200425
426 /* atomize expression */
Michal Vasko400e9672021-01-11 13:39:17 +0100427 ret = lyxp_atomize(ctx, exp, NULL, LY_PREF_JSON, NULL, ctx_node, &xp_set, options);
Michal Vasko072de482020-08-05 13:27:21 +0200428 LY_CHECK_GOTO(ret, cleanup);
429
430 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200431 ret = ly_set_new(set);
432 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200433
434 /* transform into ly_set */
435 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
Michal Vasko26512682021-01-11 11:35:40 +0100436 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200437 (*set)->size = xp_set.used;
438
439 for (i = 0; i < xp_set.used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100440 if ((xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (xp_set.val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX)) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200441 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200442 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200443 }
444 }
445
446cleanup:
447 lyxp_set_free_content(&xp_set);
Michal Vasko26512682021-01-11 11:35:40 +0100448 lyxp_expr_free(ctx, exp);
Michal Vaskoae159662020-10-21 11:57:24 +0200449 if (ret) {
Michal Vasko40308e72020-10-20 16:38:40 +0200450 ly_set_free(*set, NULL);
451 *set = NULL;
452 }
Michal Vasko072de482020-08-05 13:27:21 +0200453 return ret;
454}
455
Radek Krejcibc5644c2020-10-27 14:53:17 +0100456API LY_ERR
457lys_find_lypath_atoms(const struct ly_path *path, struct ly_set **set)
458{
459 LY_ERR ret = LY_SUCCESS;
460 LY_ARRAY_COUNT_TYPE u, v;
461
462 LY_CHECK_ARG_RET(NULL, path, set, LY_EINVAL);
463
464 /* allocate return set */
465 LY_CHECK_RET(ly_set_new(set));
466
467 LY_ARRAY_FOR(path, u) {
468 /* add nodes from the path */
469 LY_CHECK_GOTO(ret = ly_set_add(*set, (void *)path[u].node, 0, NULL), cleanup);
470 if (path[u].pred_type == LY_PATH_PREDTYPE_LIST) {
471 LY_ARRAY_FOR(path[u].predicates, v) {
472 /* add all the keys in a predicate */
473 LY_CHECK_GOTO(ret = ly_set_add(*set, (void *)path[u].predicates[v].key, 0, NULL), cleanup);
474 }
475 }
476 }
477
478cleanup:
479 if (ret) {
480 ly_set_free(*set, NULL);
481 *set = NULL;
482 }
483 return ret;
484}
485
486API LY_ERR
487lys_find_path_atoms(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *path, ly_bool output,
488 struct ly_set **set)
489{
490 LY_ERR ret = LY_SUCCESS;
491 uint8_t oper;
492 struct lyxp_expr *expr = NULL;
493 struct ly_path *p = NULL;
494
495 LY_CHECK_ARG_RET(ctx, ctx || ctx_node, path, set, LY_EINVAL);
496
497 if (!ctx) {
498 ctx = ctx_node->module->ctx;
499 }
500
501 /* parse */
502 ret = lyxp_expr_parse(ctx, path, strlen(path), 0, &expr);
503 LY_CHECK_GOTO(ret, cleanup);
504
505 /* compile */
506 oper = output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
507 ret = ly_path_compile(ctx, NULL, ctx_node, expr, LY_PATH_LREF_FALSE, oper, LY_PATH_TARGET_MANY,
Michal Vasko405cc9e2020-12-01 12:01:27 +0100508 LY_PREF_JSON, NULL, NULL, &p);
Radek Krejcibc5644c2020-10-27 14:53:17 +0100509 LY_CHECK_GOTO(ret, cleanup);
510
511 /* resolve */
512 ret = lys_find_lypath_atoms(p, set);
513
514cleanup:
515 ly_path_free(ctx, p);
516 lyxp_expr_free(ctx, expr);
517 return ret;
518}
519
520API const struct lysc_node *
521lys_find_path(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *path, ly_bool output)
522{
523 const struct lysc_node *snode = NULL;
524 struct lyxp_expr *exp = NULL;
525 struct ly_path *p = NULL;
526 LY_ERR ret;
527 uint8_t oper;
528
529 LY_CHECK_ARG_RET(ctx, ctx || ctx_node, NULL);
530
531 if (!ctx) {
532 ctx = ctx_node->module->ctx;
533 }
534
535 /* parse */
536 ret = lyxp_expr_parse(ctx, path, strlen(path), 0, &exp);
537 LY_CHECK_GOTO(ret, cleanup);
538
539 /* compile */
540 oper = output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
541 ret = ly_path_compile(ctx, NULL, ctx_node, exp, LY_PATH_LREF_FALSE, oper, LY_PATH_TARGET_MANY,
Michal Vasko405cc9e2020-12-01 12:01:27 +0100542 LY_PREF_JSON, NULL, NULL, &p);
Radek Krejcibc5644c2020-10-27 14:53:17 +0100543 LY_CHECK_GOTO(ret, cleanup);
544
545 /* get last node */
546 snode = p[LY_ARRAY_COUNT(p) - 1].node;
547
548cleanup:
549 ly_path_free(ctx, p);
550 lyxp_expr_free(ctx, exp);
551 return snode;
552}
553
Michal Vasko14654712020-02-06 08:35:21 +0100554char *
555lysc_path_until(const struct lysc_node *node, const struct lysc_node *parent, LYSC_PATH_TYPE pathtype, char *buffer,
Radek Krejci0f969882020-08-21 16:56:47 +0200556 size_t buflen)
Radek Krejci327de162019-06-14 12:52:07 +0200557{
Michal Vasko03ff5a72019-09-11 13:49:33 +0200558 const struct lysc_node *iter;
Radek Krejci327de162019-06-14 12:52:07 +0200559 char *path = NULL;
560 int len = 0;
561
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200562 LY_CHECK_ARG_RET(NULL, node, NULL);
563 if (buffer) {
564 LY_CHECK_ARG_RET(node->module->ctx, buflen > 1, NULL);
Michal Vasko770d3fc2021-01-26 09:14:35 +0100565 buffer[0] = '\0';
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200566 }
567
Radek Krejci327de162019-06-14 12:52:07 +0200568 switch (pathtype) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200569 case LYSC_PATH_LOG:
Michal Vasko65de0402020-08-03 16:34:19 +0200570 case LYSC_PATH_DATA:
Michal Vasko90932a92020-02-12 14:33:03 +0100571 for (iter = node; iter && (iter != parent) && (len >= 0); iter = iter->parent) {
Michal Vasko11deea12020-08-05 13:54:50 +0200572 char *s, *id;
Michal Vasko14654712020-02-06 08:35:21 +0100573 const char *slash;
Radek Krejci327de162019-06-14 12:52:07 +0200574
Michal Vasko65de0402020-08-03 16:34:19 +0200575 if ((pathtype == LYSC_PATH_DATA) && (iter->nodetype & (LYS_CHOICE | LYS_CASE))) {
576 /* schema-only node */
577 continue;
578 }
579
Michal Vasko11deea12020-08-05 13:54:50 +0200580 s = buffer ? strdup(buffer) : path;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200581 id = strdup(iter->name);
Michal Vasko14654712020-02-06 08:35:21 +0100582 if (parent && (iter->parent == parent)) {
583 slash = "";
584 } else {
585 slash = "/";
586 }
Michal Vasko69730152020-10-09 16:30:07 +0200587 if (!iter->parent || (iter->parent->module != iter->module)) {
Radek Krejci327de162019-06-14 12:52:07 +0200588 /* print prefix */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200589 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100590 len = snprintf(buffer, buflen, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200591 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100592 len = asprintf(&path, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200593 }
Radek Krejci327de162019-06-14 12:52:07 +0200594 } else {
595 /* prefix is the same as in parent */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200596 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100597 len = snprintf(buffer, buflen, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200598 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100599 len = asprintf(&path, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200600 }
Radek Krejci327de162019-06-14 12:52:07 +0200601 }
602 free(s);
603 free(id);
Radek Krejci1c0c3442019-07-23 16:08:47 +0200604
Michal Vasko69730152020-10-09 16:30:07 +0200605 if (buffer && (buflen <= (size_t)len)) {
Radek Krejci1c0c3442019-07-23 16:08:47 +0200606 /* not enough space in buffer */
607 break;
608 }
Radek Krejci327de162019-06-14 12:52:07 +0200609 }
610
611 if (len < 0) {
612 free(path);
613 path = NULL;
614 } else if (len == 0) {
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200615 if (buffer) {
616 strcpy(buffer, "/");
617 } else {
618 path = strdup("/");
619 }
Radek Krejci327de162019-06-14 12:52:07 +0200620 }
621 break;
622 }
623
Radek Krejci1c0c3442019-07-23 16:08:47 +0200624 if (buffer) {
625 return buffer;
626 } else {
627 return path;
628 }
Radek Krejci327de162019-06-14 12:52:07 +0200629}
630
Michal Vasko14654712020-02-06 08:35:21 +0100631API char *
632lysc_path(const struct lysc_node *node, LYSC_PATH_TYPE pathtype, char *buffer, size_t buflen)
633{
634 return lysc_path_until(node, NULL, pathtype, buffer, buflen);
635}
636
Michal Vasko28d78432020-05-26 13:10:53 +0200637API LY_ERR
Radek Krejciaf9cd802020-10-06 21:59:47 +0200638lysc_set_private(const struct lysc_node *node, void *priv, void **prev_priv_p)
Radek Krejci19cf8052020-08-18 15:02:38 +0200639{
Radek Krejciaf9cd802020-10-06 21:59:47 +0200640 struct lysc_action *act;
641 struct lysc_notif *notif;
642
Radek Krejci19cf8052020-08-18 15:02:38 +0200643 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
644
Radek Krejciaf9cd802020-10-06 21:59:47 +0200645 switch (node->nodetype) {
646 case LYS_CONTAINER:
647 case LYS_CHOICE:
648 case LYS_CASE:
649 case LYS_LEAF:
650 case LYS_LEAFLIST:
651 case LYS_LIST:
652 case LYS_ANYXML:
653 case LYS_ANYDATA:
654 if (prev_priv_p) {
655 *prev_priv_p = node->priv;
656 }
657 ((struct lysc_node *)node)->priv = priv;
658 break;
659 case LYS_RPC:
660 case LYS_ACTION:
661 act = (struct lysc_action *)node;
662 if (prev_priv_p) {
663 *prev_priv_p = act->priv;
664 }
665 act->priv = priv;
666 break;
667 case LYS_NOTIF:
668 notif = (struct lysc_notif *)node;
669 if (prev_priv_p) {
670 *prev_priv_p = notif->priv;
671 }
672 notif->priv = priv;
673 break;
674 default:
675 return LY_EINVAL;
Radek Krejci19cf8052020-08-18 15:02:38 +0200676 }
Radek Krejci19cf8052020-08-18 15:02:38 +0200677
678 return LY_SUCCESS;
679}
680
Michal Vasko405cc9e2020-12-01 12:01:27 +0100681LY_ERR
682lys_set_implemented_r(struct lys_module *mod, const char **features, struct lys_glob_unres *unres)
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200683{
684 struct lys_module *m;
685
Michal Vasko405cc9e2020-12-01 12:01:27 +0100686 assert(!mod->implemented);
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200687
688 /* we have module from the current context */
689 m = ly_ctx_get_module_implemented(mod->ctx, mod->name);
690 if (m) {
Michal Vasko89b5c072020-10-06 13:52:44 +0200691 assert(m != mod);
692
693 /* check collision with other implemented revision */
694 LOGERR(mod->ctx, LY_EDENIED, "Module \"%s%s%s\" is present in the context in other implemented revision (%s).",
695 mod->name, mod->revision ? "@" : "", mod->revision ? mod->revision : "", m->revision ? m->revision : "none");
696 return LY_EDENIED;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200697 }
698
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100699 /* enable features */
700 LY_CHECK_RET(lys_enable_features(mod->parsed, features));
701
Michal Vasko89b5c072020-10-06 13:52:44 +0200702 /* add the module into newly implemented module set */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100703 LY_CHECK_RET(ly_set_add(&unres->implementing, mod, 1, NULL));
Michal Vasko89b5c072020-10-06 13:52:44 +0200704
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200705 /* mark the module implemented, check for collision was already done */
Michal Vasko89b5c072020-10-06 13:52:44 +0200706 mod->implemented = 1;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200707
708 /* compile the schema */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100709 return lys_compile(mod, 0, unres);
710}
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200711
Michal Vasko405cc9e2020-12-01 12:01:27 +0100712API LY_ERR
713lys_set_implemented(struct lys_module *mod, const char **features)
714{
715 LY_ERR ret = LY_SUCCESS, r;
716 struct lys_glob_unres unres = {0};
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200717
Michal Vasko405cc9e2020-12-01 12:01:27 +0100718 LY_CHECK_ARG_RET(NULL, mod, LY_EINVAL);
Michal Vasko916aefb2020-11-02 15:43:16 +0100719
Michal Vasko405cc9e2020-12-01 12:01:27 +0100720 if (mod->implemented) {
721 /* mod is already implemented, set the features */
722 r = lys_set_features(mod->parsed, features);
723 if (r == LY_EEXIST) {
724 /* no changes */
725 return LY_SUCCESS;
726 } else if (r) {
727 /* error */
728 return r;
Michal Vasko89b5c072020-10-06 13:52:44 +0200729 }
730
Michal Vasko405cc9e2020-12-01 12:01:27 +0100731 /* full recompilation */
732 return lys_recompile(mod->ctx, 1);
Michal Vasko89b5c072020-10-06 13:52:44 +0200733 }
Michal Vasko08c8b272020-11-24 18:11:30 +0100734
Michal Vasko405cc9e2020-12-01 12:01:27 +0100735 /* implement this module and any other required modules, recursively */
736 ret = lys_set_implemented_r(mod, features, &unres);
Michal Vasko405cc9e2020-12-01 12:01:27 +0100737
738 /* the first module being implemented is finished, resolve global unres, consolidate the set */
739 if (!ret) {
740 ret = lys_compile_unres_glob(mod->ctx, &unres);
741 }
742 if (ret) {
743 /* failure, full compile revert */
744 lys_compile_unres_glob_revert(mod->ctx, &unres);
745 }
746
747 lys_compile_unres_glob_erase(mod->ctx, &unres);
Michal Vasko89b5c072020-10-06 13:52:44 +0200748 return ret;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200749}
750
Michal Vasko7c8439f2020-08-05 13:25:19 +0200751static LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100752lys_resolve_import_include(struct lys_parser_ctx *pctx, struct lysp_module *pmod)
Michal Vasko7c8439f2020-08-05 13:25:19 +0200753{
754 struct lysp_import *imp;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200755 LY_ARRAY_COUNT_TYPE u, v;
756
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100757 pmod->parsing = 1;
758 LY_ARRAY_FOR(pmod->imports, u) {
759 imp = &pmod->imports[u];
Michal Vasko7c8439f2020-08-05 13:25:19 +0200760 if (!imp->module) {
Michal Vasko405cc9e2020-12-01 12:01:27 +0100761 LY_CHECK_RET(lysp_load_module(PARSER_CTX(pctx), imp->name, imp->rev[0] ? imp->rev : NULL, 0, NULL,
762 pctx->unres, &imp->module));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200763 }
764 /* check for importing the same module twice */
765 for (v = 0; v < u; ++v) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100766 if (imp->module == pmod->imports[v].module) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200767 LOGWRN(PARSER_CTX(pctx), "Single revision of the module \"%s\" imported twice.", imp->name);
768 }
769 }
770 }
Radek Krejci771928a2021-01-19 13:42:36 +0100771 LY_CHECK_RET(lysp_load_submodules(pctx, pmod));
772
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100773 pmod->parsing = 0;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200774
775 return LY_SUCCESS;
776}
777
Michal Vasko3a41dff2020-07-15 14:30:28 +0200778LY_ERR
Michal Vasko7a0b0762020-09-02 16:37:01 +0200779lys_parse_submodule(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, struct lys_parser_ctx *main_ctx,
Michal Vasko22df3f02020-08-24 13:29:22 +0200780 LY_ERR (*custom_check)(const struct ly_ctx *, struct lysp_module *, struct lysp_submodule *, void *),
Radek Krejci0f969882020-08-21 16:56:47 +0200781 void *check_data, struct lysp_submodule **submodule)
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200782{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200783 LY_ERR ret;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100784 struct lysp_submodule *submod = NULL, *latest_sp;
Michal Vaskob36053d2020-03-26 15:49:30 +0100785 struct lys_yang_parser_ctx *yangctx = NULL;
786 struct lys_yin_parser_ctx *yinctx = NULL;
787 struct lys_parser_ctx *pctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100788
Michal Vasko3a41dff2020-07-15 14:30:28 +0200789 LY_CHECK_ARG_RET(ctx, ctx, in, LY_EINVAL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100790
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100791 switch (format) {
792 case LYS_IN_YIN:
Michal Vasko63f3d842020-07-08 10:10:14 +0200793 ret = yin_parse_submodule(&yinctx, ctx, main_ctx, in, &submod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100794 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100795 break;
796 case LYS_IN_YANG:
Michal Vasko63f3d842020-07-08 10:10:14 +0200797 ret = yang_parse_submodule(&yangctx, ctx, main_ctx, in, &submod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100798 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100799 break;
800 default:
David Sedlák4f2f5ba2019-08-15 13:18:48 +0200801 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Radek Krejci82fa8d42020-07-11 22:00:59 +0200802 ret = LY_EINVAL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100803 break;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200804 }
Radek Krejcif6923e82020-07-02 16:36:53 +0200805 LY_CHECK_GOTO(ret, error);
Radek Krejcif027df72020-09-15 13:00:28 +0200806 assert(submod);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100807
808 /* make sure that the newest revision is at position 0 */
809 lysp_sort_revisions(submod->revs);
810
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100811 /* decide the latest revision */
Michal Vaskoc3781c32020-10-06 14:04:08 +0200812 latest_sp = ly_ctx_get_submodule(NULL, submod->mod, submod->name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100813 if (latest_sp) {
814 if (submod->revs) {
815 if (!latest_sp->revs) {
816 /* latest has no revision, so mod is anyway newer */
817 submod->latest_revision = latest_sp->latest_revision;
Radek Krejcib3289d62019-09-18 12:21:39 +0200818 /* the latest_sp is zeroed later when the new module is being inserted into the context */
819 } else if (strcmp(submod->revs[0].date, latest_sp->revs[0].date) > 0) {
820 submod->latest_revision = latest_sp->latest_revision;
821 /* the latest_sp is zeroed later when the new module is being inserted into the context */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100822 } else {
Radek Krejcib3289d62019-09-18 12:21:39 +0200823 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100824 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200825 } else {
826 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100827 }
828 } else {
829 submod->latest_revision = 1;
830 }
831
Radek Krejcib3289d62019-09-18 12:21:39 +0200832 if (custom_check) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200833 LY_CHECK_GOTO(ret = custom_check(ctx, NULL, submod, check_data), error);
Radek Krejcib3289d62019-09-18 12:21:39 +0200834 }
835
836 if (latest_sp) {
837 latest_sp->latest_revision = 0;
838 }
839
Michal Vasko7a0b0762020-09-02 16:37:01 +0200840 lys_parser_fill_filepath(ctx, in, &submod->filepath);
841
Michal Vasko7c8439f2020-08-05 13:25:19 +0200842 /* resolve imports and includes */
843 LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, (struct lysp_module *)submod), error);
844
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100845 /* remap possibly changed and reallocated typedefs and groupings list back to the main context */
Michal Vaskob36053d2020-03-26 15:49:30 +0100846 memcpy(&main_ctx->tpdfs_nodes, &pctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
847 memcpy(&main_ctx->grps_nodes, &pctx->grps_nodes, sizeof main_ctx->grps_nodes);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100848
David Sedlák1b623122019-08-05 15:27:49 +0200849 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100850 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200851 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100852 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200853 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200854 *submodule = submod;
855 return LY_SUCCESS;
David Sedlák1b623122019-08-05 15:27:49 +0200856
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100857error:
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200858 lysp_module_free((struct lysp_module *)submod);
David Sedlák1b623122019-08-05 15:27:49 +0200859 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100860 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200861 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100862 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200863 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200864 return ret;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200865}
866
Michal Vasko45b521c2020-11-04 17:14:39 +0100867/**
868 * @brief Add ietf-netconf metadata to the parsed module. Operation, filter, and select are added.
869 *
870 * @param[in] mod Parsed module to add to.
871 * @return LY_SUCCESS on success.
872 * @return LY_ERR on error.
873 */
874static LY_ERR
875lys_parsed_add_internal_ietf_netconf(struct lysp_module *mod)
876{
877 struct lysp_ext_instance *ext_p;
878 struct lysp_stmt *stmt;
879 struct lysp_import *imp;
880
881 /*
882 * 1) edit-config's operation
883 */
884 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
885 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
886 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
887 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "operation", 0, &ext_p->argument));
888 ext_p->flags = LYS_INTERNAL;
889 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
890 ext_p->insubstmt_index = 0;
891
892 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
893 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
894 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
895 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enumeration", 0, &stmt->arg));
896 stmt->kw = LY_STMT_TYPE;
897
898 stmt->child = calloc(1, sizeof *stmt->child);
899 stmt = stmt->child;
900 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
901 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
902 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "merge", 0, &stmt->arg));
903 stmt->kw = LY_STMT_ENUM;
904
905 stmt->next = calloc(1, sizeof *stmt->child);
906 stmt = stmt->next;
907 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
908 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
909 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "replace", 0, &stmt->arg));
910 stmt->kw = LY_STMT_ENUM;
911
912 stmt->next = calloc(1, sizeof *stmt->child);
913 stmt = stmt->next;
914 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
915 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
916 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "create", 0, &stmt->arg));
917 stmt->kw = LY_STMT_ENUM;
918
919 stmt->next = calloc(1, sizeof *stmt->child);
920 stmt = stmt->next;
921 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
922 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
923 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "delete", 0, &stmt->arg));
924 stmt->kw = LY_STMT_ENUM;
925
926 stmt->next = calloc(1, sizeof *stmt->child);
927 stmt = stmt->next;
928 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
929 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
930 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "remove", 0, &stmt->arg));
931 stmt->kw = LY_STMT_ENUM;
932
933 /*
934 * 2) filter's type
935 */
936 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
937 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
938 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
939 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &ext_p->argument));
940 ext_p->flags = LYS_INTERNAL;
941 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
942 ext_p->insubstmt_index = 0;
943
944 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
945 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
946 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
947 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enumeration", 0, &stmt->arg));
948 stmt->kw = LY_STMT_TYPE;
949
950 stmt->child = calloc(1, sizeof *stmt->child);
951 stmt = stmt->child;
952 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
953 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
954 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "subtree", 0, &stmt->arg));
955 stmt->kw = LY_STMT_ENUM;
956
957 stmt->next = calloc(1, sizeof *stmt->child);
958 stmt = stmt->next;
959 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
960 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
961 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "xpath", 0, &stmt->arg));
962 stmt->kw = LY_STMT_ENUM;
963
964 /* if-feature for enum allowed only for YANG 1.1 modules */
965 if (mod->version >= LYS_VERSION_1_1) {
966 stmt->child = calloc(1, sizeof *stmt->child);
967 stmt = stmt->child;
968 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
969 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "if-feature", 0, &stmt->stmt));
970 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "xpath", 0, &stmt->arg));
971 stmt->kw = LY_STMT_IF_FEATURE;
972 }
973
974 /*
975 * 3) filter's select
976 */
977 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
978 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
979 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
980 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "select", 0, &ext_p->argument));
981 ext_p->flags = LYS_INTERNAL;
982 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
983 ext_p->insubstmt_index = 0;
984
985 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
986 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
987 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
988 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "yang_:xpath1.0", 0, &stmt->arg));
989 stmt->kw = LY_STMT_TYPE;
990
991 /* create new imports for the used prefixes */
992 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM);
993
994 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-metadata", 0, &imp->name));
995 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_", 0, &imp->prefix));
996 imp->flags = LYS_INTERNAL;
997
998 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM);
999
1000 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-types", 0, &imp->name));
1001 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "yang_", 0, &imp->prefix));
1002 imp->flags = LYS_INTERNAL;
1003
1004 return LY_SUCCESS;
1005}
1006
1007/**
1008 * @brief Add ietf-netconf-with-defaults "default" metadata to the parsed module.
1009 *
1010 * @param[in] mod Parsed module to add to.
1011 * @return LY_SUCCESS on success.
1012 * @return LY_ERR on error.
1013 */
1014static LY_ERR
1015lys_parsed_add_internal_ietf_netconf_with_defaults(struct lysp_module *mod)
1016{
1017 struct lysp_ext_instance *ext_p;
1018 struct lysp_stmt *stmt;
1019 struct lysp_import *imp;
1020
1021 /* add new extension instance */
1022 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
1023
1024 /* fill in the extension instance fields */
1025 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
1026 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
1027 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "default", 0, &ext_p->argument));
1028 ext_p->flags = LYS_INTERNAL;
1029 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
1030 ext_p->insubstmt_index = 0;
1031
1032 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
1033 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
1034 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
1035 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "boolean", 0, &stmt->arg));
1036 stmt->kw = LY_STMT_TYPE;
1037
1038 /* create new import for the used prefix */
1039 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM);
1040
1041 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-metadata", 0, &imp->name));
1042 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_", 0, &imp->prefix));
1043 imp->flags = LYS_INTERNAL;
1044
1045 return LY_SUCCESS;
1046}
1047
Michal Vasko3a41dff2020-07-15 14:30:28 +02001048LY_ERR
Michal Vasko34e334d2021-01-25 16:12:31 +01001049lys_create_module(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, ly_bool need_implemented,
Radek Krejci1deb5be2020-08-26 16:43:36 +02001050 LY_ERR (*custom_check)(const struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
Michal Vasko405cc9e2020-12-01 12:01:27 +01001051 void *check_data, const char **features, struct lys_glob_unres *unres, struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001052{
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001053 struct lys_module *mod = NULL, *latest, *mod_dup, *mod_impl;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001054 struct lysp_submodule *submod;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001055 LY_ERR ret;
Michal Vasko7c8439f2020-08-05 13:25:19 +02001056 LY_ARRAY_COUNT_TYPE u;
Michal Vaskob36053d2020-03-26 15:49:30 +01001057 struct lys_yang_parser_ctx *yangctx = NULL;
1058 struct lys_yin_parser_ctx *yinctx = NULL;
Radek Krejcif6923e82020-07-02 16:36:53 +02001059 struct lys_parser_ctx *pctx = NULL;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001060 char *filename, *rev, *dot;
1061 size_t len;
Michal Vasko34e334d2021-01-25 16:12:31 +01001062 ly_bool implement;
Radek Krejci86d106e2018-10-18 09:53:19 +02001063
Michal Vasko34e334d2021-01-25 16:12:31 +01001064 assert(ctx && in && (!features || need_implemented) && unres);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001065
Michal Vasko7a0b0762020-09-02 16:37:01 +02001066 if (module) {
1067 *module = NULL;
1068 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001069
Michal Vasko34e334d2021-01-25 16:12:31 +01001070 if (ctx->flags & LY_CTX_ALL_IMPLEMENTED) {
1071 implement = 1;
1072 } else {
1073 implement = need_implemented;
1074 }
1075
Radek Krejci86d106e2018-10-18 09:53:19 +02001076 mod = calloc(1, sizeof *mod);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001077 LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), LY_EMEM);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001078 mod->ctx = ctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001079
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001080 /* parse */
Radek Krejci86d106e2018-10-18 09:53:19 +02001081 switch (format) {
1082 case LYS_IN_YIN:
Michal Vasko405cc9e2020-12-01 12:01:27 +01001083 ret = yin_parse_module(&yinctx, in, mod, unres);
Michal Vaskob36053d2020-03-26 15:49:30 +01001084 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001085 break;
1086 case LYS_IN_YANG:
Michal Vasko405cc9e2020-12-01 12:01:27 +01001087 ret = yang_parse_module(&yangctx, in, mod, unres);
Michal Vaskob36053d2020-03-26 15:49:30 +01001088 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001089 break;
1090 default:
1091 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Michal Vasko3a41dff2020-07-15 14:30:28 +02001092 ret = LY_EINVAL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001093 break;
1094 }
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001095 LY_CHECK_GOTO(ret, free_mod_cleanup);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +02001096
1097 /* make sure that the newest revision is at position 0 */
1098 lysp_sort_revisions(mod->parsed->revs);
Radek Krejci0af46292019-01-11 16:02:31 +01001099 if (mod->parsed->revs) {
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001100 LY_CHECK_GOTO(ret = lydict_insert(ctx, mod->parsed->revs[0].date, 0, &mod->revision), free_mod_cleanup);
Radek Krejci0af46292019-01-11 16:02:31 +01001101 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001102
Radek Krejcib3289d62019-09-18 12:21:39 +02001103 /* decide the latest revision */
Michal Vasko22df3f02020-08-24 13:29:22 +02001104 latest = (struct lys_module *)ly_ctx_get_module_latest(ctx, mod->name);
Radek Krejcib3289d62019-09-18 12:21:39 +02001105 if (latest) {
1106 if (mod->revision) {
1107 if (!latest->revision) {
1108 /* latest has no revision, so mod is anyway newer */
1109 mod->latest_revision = latest->latest_revision;
1110 /* the latest is zeroed later when the new module is being inserted into the context */
1111 } else if (strcmp(mod->revision, latest->revision) > 0) {
1112 mod->latest_revision = latest->latest_revision;
1113 /* the latest is zeroed later when the new module is being inserted into the context */
1114 } else {
1115 latest = NULL;
1116 }
1117 } else {
1118 latest = NULL;
1119 }
1120 } else {
1121 mod->latest_revision = 1;
1122 }
1123
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001124 if (custom_check) {
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001125 LY_CHECK_GOTO(ret = custom_check(ctx, mod->parsed, NULL, check_data), free_mod_cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001126 }
1127
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001128 /* check whether it is not already in the context in the same revision */
Michal Vasko22df3f02020-08-24 13:29:22 +02001129 mod_dup = (struct lys_module *)ly_ctx_get_module(ctx, mod->name, mod->revision);
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001130 if (implement) {
1131 mod_impl = ly_ctx_get_module_implemented(ctx, mod->name);
1132 if (mod_impl && (mod_impl != mod_dup)) {
1133 LOGERR(ctx, LY_EDENIED, "Module \"%s@%s\" is already implemented in the context.", mod_impl->name,
1134 mod_impl->revision ? mod_impl->revision : "<none>");
1135 ret = LY_EDENIED;
1136 goto free_mod_cleanup;
Radek Krejcid33273d2018-10-25 14:55:52 +02001137 }
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001138 }
1139 if (mod_dup) {
1140 if (implement) {
1141 if (!mod_dup->implemented) {
1142 /* just implement it */
1143 LY_CHECK_GOTO(ret = lys_set_implemented_r(mod_dup, features, unres), free_mod_cleanup);
1144 goto free_mod_cleanup;
1145 }
1146
1147 /* nothing to do */
1148 LOGVRB("Module \"%s@%s\" is already implemented in the context.", mod_dup->name,
1149 mod_dup->revision ? mod_dup->revision : "<none>");
1150 goto free_mod_cleanup;
1151 }
1152
1153 /* nothing to do */
1154 LOGVRB("Module \"%s@%s\" is already present in the context.", mod_dup->name,
1155 mod_dup->revision ? mod_dup->revision : "<none>");
1156 goto free_mod_cleanup;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001157 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001158
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001159 switch (in->type) {
1160 case LY_IN_FILEPATH:
1161 /* check that name and revision match filename */
1162 filename = strrchr(in->method.fpath.filepath, '/');
1163 if (!filename) {
1164 filename = in->method.fpath.filepath;
1165 } else {
1166 filename++;
1167 }
1168 rev = strchr(filename, '@');
1169 dot = strrchr(filename, '.');
1170
1171 /* name */
1172 len = strlen(mod->name);
1173 if (strncmp(filename, mod->name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +02001174 ((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001175 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name);
1176 }
1177 if (rev) {
1178 len = dot - ++rev;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001179 if (!mod->parsed->revs || (len != LY_REV_SIZE - 1) || strncmp(mod->parsed->revs[0].date, rev, len)) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001180 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Michal Vasko69730152020-10-09 16:30:07 +02001181 mod->parsed->revs ? mod->parsed->revs[0].date : "none");
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001182 }
1183 }
1184
1185 break;
1186 case LY_IN_FD:
1187 case LY_IN_FILE:
1188 case LY_IN_MEMORY:
1189 /* nothing special to do */
1190 break;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001191 case LY_IN_ERROR:
1192 LOGINT(ctx);
1193 ret = LY_EINT;
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001194 goto free_mod_cleanup;
Radek Krejci096235c2019-01-11 11:12:19 +01001195 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001196 lys_parser_fill_filepath(ctx, in, &mod->filepath);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001197
Michal Vasko7a0b0762020-09-02 16:37:01 +02001198 if (latest) {
1199 latest->latest_revision = 0;
1200 }
1201
Michal Vasko45b521c2020-11-04 17:14:39 +01001202 /* add internal data in case specific modules were parsed */
1203 if (!strcmp(mod->name, "ietf-netconf")) {
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001204 LY_CHECK_GOTO(ret = lys_parsed_add_internal_ietf_netconf(mod->parsed), free_mod_cleanup);
Michal Vasko45b521c2020-11-04 17:14:39 +01001205 } else if (!strcmp(mod->name, "ietf-netconf-with-defaults")) {
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001206 LY_CHECK_GOTO(ret = lys_parsed_add_internal_ietf_netconf_with_defaults(mod->parsed), free_mod_cleanup);
Michal Vasko45b521c2020-11-04 17:14:39 +01001207 }
1208
Michal Vasko405cc9e2020-12-01 12:01:27 +01001209 /* add the module into newly created module set, will also be freed from there on any error */
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001210 LY_CHECK_GOTO(ret = ly_set_add(&unres->creating, mod, 1, NULL), free_mod_cleanup);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001211
Michal Vasko7a0b0762020-09-02 16:37:01 +02001212 /* add into context */
Radek Krejci3d92e442020-10-12 12:48:13 +02001213 ret = ly_set_add(&ctx->list, mod, 1, NULL);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001214 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001215 ctx->module_set_id++;
1216
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001217 /* resolve includes and all imports */
Michal Vasko405cc9e2020-12-01 12:01:27 +01001218 LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, mod->parsed), cleanup);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001219
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001220 /* check name collisions */
Michal Vasko405cc9e2020-12-01 12:01:27 +01001221 LY_CHECK_GOTO(ret = lysp_check_dup_typedefs(pctx, mod->parsed), cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001222 /* TODO groupings */
Michal Vasko405cc9e2020-12-01 12:01:27 +01001223 LY_CHECK_GOTO(ret = lysp_check_dup_features(pctx, mod->parsed), cleanup);
1224 LY_CHECK_GOTO(ret = lysp_check_dup_identities(pctx, mod->parsed), cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001225
1226 /* compile features */
Michal Vasko405cc9e2020-12-01 12:01:27 +01001227 LY_CHECK_GOTO(ret = lys_compile_feature_iffeatures(mod->parsed), cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001228
Michal Vasko89b5c072020-10-06 13:52:44 +02001229 if (!implement) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001230 /* pre-compile identities of the module */
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001231 LY_CHECK_GOTO(ret = lys_identity_precompile(NULL, ctx, mod->parsed, mod->parsed->identities, &mod->identities), cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001232
1233 /* pre-compile identities of any submodules */
Michal Vasko7a0b0762020-09-02 16:37:01 +02001234 LY_ARRAY_FOR(mod->parsed->includes, u) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001235 submod = mod->parsed->includes[u].submodule;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001236 ret = lys_identity_precompile(NULL, ctx, (struct lysp_module *)submod, submod->identities, &mod->identities);
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001237 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001238 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001239 } else {
Michal Vasko89b5c072020-10-06 13:52:44 +02001240 /* implement (compile) */
Michal Vasko405cc9e2020-12-01 12:01:27 +01001241 LY_CHECK_GOTO(ret = lys_set_implemented_r(mod, features, unres), cleanup);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001242 }
1243
Michal Vasko405cc9e2020-12-01 12:01:27 +01001244 /* success */
1245 goto cleanup;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001246
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001247free_mod_cleanup:
Michal Vasko7a0b0762020-09-02 16:37:01 +02001248 lys_module_free(mod, NULL);
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001249 mod = NULL;
1250
Michal Vasko405cc9e2020-12-01 12:01:27 +01001251cleanup:
Michal Vasko7a0b0762020-09-02 16:37:01 +02001252 if (pctx) {
1253 ly_set_erase(&pctx->tpdfs_nodes, NULL);
1254 }
1255 if (format == LYS_IN_YANG) {
1256 yang_parser_ctx_free(yangctx);
1257 } else {
1258 yin_parser_ctx_free(yinctx);
1259 }
1260
Michal Vasko405cc9e2020-12-01 12:01:27 +01001261 if (!ret && module) {
1262 *module = mod;
1263 }
Michal Vasko7a0b0762020-09-02 16:37:01 +02001264 return ret;
1265}
1266
Radek Krejci545b4872020-11-15 10:15:12 +01001267static LYS_INFORMAT
1268lys_parse_get_format(const struct ly_in *in, LYS_INFORMAT format)
1269{
1270 if (!format && (in->type == LY_IN_FILEPATH)) {
1271 /* unknown format - try to detect it from filename's suffix */
1272 const char *path = in->method.fpath.filepath;
1273 size_t len = strlen(path);
1274
1275 /* ignore trailing whitespaces */
1276 for ( ; len > 0 && isspace(path[len - 1]); len--) {}
1277
Radek Krejcif13b87b2020-12-01 22:02:17 +01001278 if ((len >= LY_YANG_SUFFIX_LEN + 1) &&
1279 !strncmp(&path[len - LY_YANG_SUFFIX_LEN], LY_YANG_SUFFIX, LY_YANG_SUFFIX_LEN)) {
Radek Krejci545b4872020-11-15 10:15:12 +01001280 format = LYS_IN_YANG;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001281 } else if ((len >= LY_YIN_SUFFIX_LEN + 1) &&
1282 !strncmp(&path[len - LY_YIN_SUFFIX_LEN], LY_YIN_SUFFIX, LY_YIN_SUFFIX_LEN)) {
Radek Krejci545b4872020-11-15 10:15:12 +01001283 format = LYS_IN_YIN;
1284 } /* else still unknown */
1285 }
1286
1287 return format;
1288}
1289
Michal Vasko7a0b0762020-09-02 16:37:01 +02001290API LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001291lys_parse(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, const char **features, const struct lys_module **module)
Michal Vasko7a0b0762020-09-02 16:37:01 +02001292{
Michal Vasko405cc9e2020-12-01 12:01:27 +01001293 LY_ERR ret;
1294 struct lys_glob_unres unres = {0};
1295
Michal Vasko7a0b0762020-09-02 16:37:01 +02001296 if (module) {
1297 *module = NULL;
1298 }
Radek Krejci545b4872020-11-15 10:15:12 +01001299 LY_CHECK_ARG_RET(NULL, ctx, in, LY_EINVAL);
1300
1301 format = lys_parse_get_format(in, format);
1302 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001303
1304 /* remember input position */
1305 in->func_start = in->current;
1306
Michal Vasko405cc9e2020-12-01 12:01:27 +01001307 ret = lys_create_module(ctx, in, format, 1, NULL, NULL, features, &unres, (struct lys_module **)module);
1308 LY_CHECK_GOTO(ret, cleanup);
1309
1310 /* resolve global unres */
1311 ret = lys_compile_unres_glob(ctx, &unres);
1312 LY_CHECK_GOTO(ret, cleanup);
1313
1314cleanup:
1315 if (ret) {
1316 lys_compile_unres_glob_revert(ctx, &unres);
1317 }
1318 lys_compile_unres_glob_erase(ctx, &unres);
1319 if (ret && module) {
1320 *module = NULL;
1321 }
1322 return ret;
Radek Krejci86d106e2018-10-18 09:53:19 +02001323}
1324
Michal Vasko3a41dff2020-07-15 14:30:28 +02001325API LY_ERR
1326lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001327{
Radek Krejci0f969882020-08-21 16:56:47 +02001328 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001329 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001330
Michal Vasko3a41dff2020-07-15 14:30:28 +02001331 LY_CHECK_ARG_RET(ctx, data, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci65639b92018-11-27 10:51:37 +01001332
Michal Vasko3a41dff2020-07-15 14:30:28 +02001333 LY_CHECK_ERR_RET(ret = ly_in_new_memory(data, &in), LOGERR(ctx, ret, "Unable to create input handler."), ret);
Radek Krejci86d106e2018-10-18 09:53:19 +02001334
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001335 ret = lys_parse(ctx, in, format, NULL, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001336 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001337
Michal Vasko3a41dff2020-07-15 14:30:28 +02001338 return ret;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001339}
1340
Michal Vasko3a41dff2020-07-15 14:30:28 +02001341API LY_ERR
1342lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001343{
Radek Krejci0f969882020-08-21 16:56:47 +02001344 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001345 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001346
Michal Vasko3a41dff2020-07-15 14:30:28 +02001347 LY_CHECK_ARG_RET(ctx, fd > -1, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci86d106e2018-10-18 09:53:19 +02001348
Michal Vasko3a41dff2020-07-15 14:30:28 +02001349 LY_CHECK_ERR_RET(ret = ly_in_new_fd(fd, &in), LOGERR(ctx, ret, "Unable to create input handler."), ret);
Radek Krejci86d106e2018-10-18 09:53:19 +02001350
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001351 ret = lys_parse(ctx, in, format, NULL, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001352 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001353
Michal Vasko3a41dff2020-07-15 14:30:28 +02001354 return ret;
Radek Krejci86d106e2018-10-18 09:53:19 +02001355}
1356
Michal Vasko3a41dff2020-07-15 14:30:28 +02001357API LY_ERR
1358lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejcid33273d2018-10-25 14:55:52 +02001359{
Radek Krejci0f969882020-08-21 16:56:47 +02001360 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001361 struct ly_in *in = NULL;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001362
Michal Vasko3a41dff2020-07-15 14:30:28 +02001363 LY_CHECK_ARG_RET(ctx, path, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001364
Michal Vasko3a41dff2020-07-15 14:30:28 +02001365 LY_CHECK_ERR_RET(ret = ly_in_new_filepath(path, 0, &in),
Michal Vasko69730152020-10-09 16:30:07 +02001366 LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", path), ret);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001367
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001368 ret = lys_parse(ctx, in, format, NULL, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001369 ly_in_free(in, 0);
1370
Michal Vasko3a41dff2020-07-15 14:30:28 +02001371 return ret;
Radek Krejcid33273d2018-10-25 14:55:52 +02001372}
1373
1374API LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001375lys_search_localfile(const char * const *searchpaths, ly_bool cwd, const char *name, const char *revision,
Radek Krejci0f969882020-08-21 16:56:47 +02001376 char **localfile, LYS_INFORMAT *format)
Radek Krejcid33273d2018-10-25 14:55:52 +02001377{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001378 LY_ERR ret = LY_EMEM;
Radek Krejcid33273d2018-10-25 14:55:52 +02001379 size_t len, flen, match_len = 0, dir_len;
Radek Krejci857189e2020-09-01 13:26:36 +02001380 ly_bool implicit_cwd = 0;
Radek Krejcid33273d2018-10-25 14:55:52 +02001381 char *wd, *wn = NULL;
1382 DIR *dir = NULL;
1383 struct dirent *file;
1384 char *match_name = NULL;
1385 LYS_INFORMAT format_aux, match_format = 0;
1386 struct ly_set *dirs;
1387 struct stat st;
1388
1389 LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL);
1390
1391 /* start to fill the dir fifo with the context's search path (if set)
1392 * and the current working directory */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001393 LY_CHECK_RET(ly_set_new(&dirs));
Radek Krejcid33273d2018-10-25 14:55:52 +02001394
1395 len = strlen(name);
1396 if (cwd) {
1397 wd = get_current_dir_name();
1398 if (!wd) {
1399 LOGMEM(NULL);
1400 goto cleanup;
1401 } else {
1402 /* add implicit current working directory (./) to be searched,
1403 * this directory is not searched recursively */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001404 ret = ly_set_add(dirs, wd, 0, NULL);
1405 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid33273d2018-10-25 14:55:52 +02001406 implicit_cwd = 1;
1407 }
1408 }
1409 if (searchpaths) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02001410 for (uint64_t i = 0; searchpaths[i]; i++) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001411 /* check for duplicities with the implicit current working directory */
1412 if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) {
1413 implicit_cwd = 0;
1414 continue;
1415 }
1416 wd = strdup(searchpaths[i]);
1417 if (!wd) {
1418 LOGMEM(NULL);
1419 goto cleanup;
Radek Krejciba03a5a2020-08-27 14:40:41 +02001420 } else {
1421 ret = ly_set_add(dirs, wd, 0, NULL);
1422 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid33273d2018-10-25 14:55:52 +02001423 }
1424 }
1425 }
1426 wd = NULL;
1427
1428 /* start searching */
1429 while (dirs->count) {
1430 free(wd);
1431 free(wn); wn = NULL;
1432
1433 dirs->count--;
1434 wd = (char *)dirs->objs[dirs->count];
1435 dirs->objs[dirs->count] = NULL;
Radek Krejcieeee95c2021-01-19 10:57:22 +01001436 LOGVRB("Searching for \"%s\" in \"%s\".", name, wd);
Radek Krejcid33273d2018-10-25 14:55:52 +02001437
1438 if (dir) {
1439 closedir(dir);
1440 }
1441 dir = opendir(wd);
1442 dir_len = strlen(wd);
1443 if (!dir) {
1444 LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno));
1445 } else {
1446 while ((file = readdir(dir))) {
1447 if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) {
1448 /* skip . and .. */
1449 continue;
1450 }
1451 free(wn);
1452 if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) {
1453 LOGMEM(NULL);
1454 goto cleanup;
1455 }
1456 if (stat(wn, &st) == -1) {
1457 LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)",
Michal Vasko69730152020-10-09 16:30:07 +02001458 file->d_name, wd, strerror(errno));
Radek Krejcid33273d2018-10-25 14:55:52 +02001459 continue;
1460 }
1461 if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) {
1462 /* we have another subdirectory in searchpath to explore,
1463 * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001464 ret = ly_set_add(dirs, wn, 0, NULL);
1465 LY_CHECK_GOTO(ret, cleanup);
1466
Radek Krejcid33273d2018-10-25 14:55:52 +02001467 /* continue with the next item in current directory */
1468 wn = NULL;
1469 continue;
1470 } else if (!S_ISREG(st.st_mode)) {
1471 /* not a regular file (note that we see the target of symlinks instead of symlinks */
1472 continue;
1473 }
1474
1475 /* here we know that the item is a file which can contain a module */
1476 if (strncmp(name, file->d_name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +02001477 ((file->d_name[len] != '.') && (file->d_name[len] != '@'))) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001478 /* different filename than the module we search for */
1479 continue;
1480 }
1481
1482 /* get type according to filename suffix */
1483 flen = strlen(file->d_name);
Radek Krejcif13b87b2020-12-01 22:02:17 +01001484 if ((flen >= LY_YANG_SUFFIX_LEN + 1) &&
1485 !strcmp(&file->d_name[flen - LY_YANG_SUFFIX_LEN], LY_YANG_SUFFIX)) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001486 format_aux = LYS_IN_YANG;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001487 } else if ((flen >= LY_YIN_SUFFIX_LEN + 1) &&
1488 !strcmp(&file->d_name[flen - LY_YIN_SUFFIX_LEN], LY_YIN_SUFFIX)) {
Radek Krejci01a937f2020-11-15 10:14:12 +01001489 format_aux = LYS_IN_YIN;
Radek Krejcid33273d2018-10-25 14:55:52 +02001490 } else {
1491 /* not supportde suffix/file format */
1492 continue;
1493 }
1494
1495 if (revision) {
1496 /* we look for the specific revision, try to get it from the filename */
1497 if (file->d_name[len] == '@') {
1498 /* check revision from the filename */
1499 if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) {
1500 /* another revision */
1501 continue;
1502 } else {
1503 /* exact revision */
1504 free(match_name);
1505 match_name = wn;
1506 wn = NULL;
1507 match_len = dir_len + 1 + len;
1508 match_format = format_aux;
1509 goto success;
1510 }
1511 } else {
1512 /* continue trying to find exact revision match, use this only if not found */
1513 free(match_name);
1514 match_name = wn;
1515 wn = NULL;
Michal Vasko44f3d2c2020-08-24 09:49:38 +02001516 match_len = dir_len + 1 + len;
Radek Krejcid33273d2018-10-25 14:55:52 +02001517 match_format = format_aux;
1518 continue;
1519 }
1520 } else {
1521 /* remember the revision and try to find the newest one */
1522 if (match_name) {
Michal Vasko69730152020-10-09 16:30:07 +02001523 if ((file->d_name[len] != '@') ||
Radek Krejcif13b87b2020-12-01 22:02:17 +01001524 lysp_check_date(NULL, &file->d_name[len + 1],
1525 flen - ((format_aux == LYS_IN_YANG) ? LY_YANG_SUFFIX_LEN : LY_YIN_SUFFIX_LEN) - len - 1, NULL)) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001526 continue;
Michal Vasko69730152020-10-09 16:30:07 +02001527 } else if ((match_name[match_len] == '@') &&
Radek Krejcid33273d2018-10-25 14:55:52 +02001528 (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) {
1529 continue;
1530 }
1531 free(match_name);
1532 }
1533
1534 match_name = wn;
1535 wn = NULL;
1536 match_len = dir_len + 1 + len;
1537 match_format = format_aux;
1538 continue;
1539 }
1540 }
1541 }
1542 }
1543
1544success:
1545 (*localfile) = match_name;
1546 match_name = NULL;
1547 if (format) {
1548 (*format) = match_format;
1549 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02001550 ret = LY_SUCCESS;
Radek Krejcid33273d2018-10-25 14:55:52 +02001551
1552cleanup:
1553 free(wn);
1554 free(wd);
1555 if (dir) {
1556 closedir(dir);
1557 }
1558 free(match_name);
1559 ly_set_free(dirs, free);
1560
1561 return ret;
1562}