blob: 176664a4779efded227712bd19d32ea5022591b6 [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
15#include "common.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020016
Radek Krejcie7b95092019-05-15 11:03:07 +020017#include <assert.h>
Radek Krejcid33273d2018-10-25 14:55:52 +020018#include <dirent.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020019#include <errno.h>
20#include <fcntl.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020021#include <limits.h>
22#include <stdint.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020023#include <stdio.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020024#include <stdlib.h>
25#include <string.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020026#include <sys/stat.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020027#include <unistd.h>
Radek Krejci3f5e3db2018-10-11 15:57:47 +020028
Radek Krejci86d106e2018-10-18 09:53:19 +020029#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020030#include "dict.h"
31#include "log.h"
32#include "set.h"
33#include "tree.h"
34#include "tree_schema.h"
Radek Krejci70853c52018-10-15 14:46:16 +020035#include "tree_schema_internal.h"
Radek Krejci3f5e3db2018-10-11 15:57:47 +020036
Radek Krejcia3045382018-11-22 14:30:31 +010037API const struct lysc_node *
38lys_getnext(const struct lysc_node *last, const struct lysc_node *parent, const struct lysc_module *module, int options)
39{
Radek Krejci6eeb58f2019-02-22 16:29:37 +010040 const struct lysc_node *next = NULL;
Radek Krejcia3045382018-11-22 14:30:31 +010041 struct lysc_node **snode;
Radek Krejci6eeb58f2019-02-22 16:29:37 +010042 int action_flag = 0, notif_flag = 0;
43 const struct lysc_action *actions;
44 const struct lysc_notif *notifs;
45 unsigned int u;
Radek Krejcia3045382018-11-22 14:30:31 +010046
47 LY_CHECK_ARG_RET(NULL, parent || module, NULL);
48
Radek Krejcid5a2b9d2019-04-12 10:39:30 +020049next:
Radek Krejcia3045382018-11-22 14:30:31 +010050 if (!last) {
51 /* first call */
52
53 /* get know where to start */
54 if (parent) {
55 /* schema subtree */
Radek Krejci056d0a82018-12-06 16:57:25 +010056 if (parent->nodetype == LYS_CHOICE && (options & LYS_GETNEXT_WITHCASE)) {
Radek Krejcid5a2b9d2019-04-12 10:39:30 +020057 if (((struct lysc_node_choice*)parent)->cases) {
58 next = last = (const struct lysc_node*)&((struct lysc_node_choice*)parent)->cases[0];
Radek Krejci056d0a82018-12-06 16:57:25 +010059 }
Radek Krejci056d0a82018-12-06 16:57:25 +010060 } else {
Radek Krejci6eeb58f2019-02-22 16:29:37 +010061 snode = lysc_node_children_p(parent, (options & LYS_GETNEXT_OUTPUT) ? LYS_CONFIG_R : LYS_CONFIG_W);
Radek Krejci05b774b2019-02-25 13:26:18 +010062 /* do not return anything if the node does not have any children */
Radek Krejcid5a2b9d2019-04-12 10:39:30 +020063 if (snode && *snode) {
64 next = last = *snode;
Radek Krejci056d0a82018-12-06 16:57:25 +010065 }
Radek Krejcia3045382018-11-22 14:30:31 +010066 }
Radek Krejcia3045382018-11-22 14:30:31 +010067 } else {
68 /* top level data */
69 next = last = module->data;
70 }
71 if (!next) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +010072 /* try to get action or notification */
73 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +010074 }
Radek Krejci05b774b2019-02-25 13:26:18 +010075 /* test if the next can be returned */
76 goto check;
77
Michal Vasko1bf09392020-03-27 12:38:10 +010078 } else if (last->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +010079 action_flag = 1;
Radek Krejci6eeb58f2019-02-22 16:29:37 +010080 if (last->parent) {
81 actions = lysc_node_actions(last->parent);
82 } else {
83 actions = module->rpcs;
84 }
85 LY_ARRAY_FOR(actions, u) {
86 if (&actions[u] == (struct lysc_action*)last) {
87 break;
88 }
89 }
90 if (u + 1 < LY_ARRAY_SIZE(actions)) {
91 next = (struct lysc_node*)(&actions[u + 1]);
92 }
93 goto repeat;
94 } else if (last->nodetype == LYS_NOTIF) {
Radek Krejci05b774b2019-02-25 13:26:18 +010095 action_flag = notif_flag = 1;
Radek Krejci6eeb58f2019-02-22 16:29:37 +010096 if (last->parent) {
97 notifs = lysc_node_notifs(last->parent);
98 } else {
99 notifs = module->notifs;
100 }
101 LY_ARRAY_FOR(notifs, u) {
102 if (&notifs[u] == (struct lysc_notif*)last) {
103 break;
104 }
105 }
106 if (u + 1 < LY_ARRAY_SIZE(notifs)) {
107 next = (struct lysc_node*)(&notifs[u + 1]);
108 }
109 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100110 }
111
112 next = last->next;
113repeat:
Radek Krejci01342af2019-01-03 15:18:08 +0100114 if (next && parent && parent->nodetype == LYS_CASE && next->parent != parent) {
115 /* inside case (as an explicit parent, not when diving into it from choice),
116 * limit the list of children only to the specific case */
117 next = NULL;
118 }
Radek Krejcia3045382018-11-22 14:30:31 +0100119 if (!next) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100120 /* possibly go back to parent */
Radek Krejci05b774b2019-02-25 13:26:18 +0100121 if (last && last->parent != parent) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100122 last = last->parent;
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200123 goto next;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100124 } else if (!action_flag) {
125 action_flag = 1;
126 next = parent ? (struct lysc_node*)lysc_node_actions(parent) : (struct lysc_node*)module->rpcs;
127 } else if (!notif_flag) {
128 notif_flag = 1;
129 next = parent ? (struct lysc_node*)lysc_node_notifs(parent) : (struct lysc_node*)module->notifs;
130 } else {
131 return NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100132 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100133 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100134 }
Radek Krejci05b774b2019-02-25 13:26:18 +0100135check:
Radek Krejcia3045382018-11-22 14:30:31 +0100136 switch (next->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +0100137 case LYS_RPC:
Radek Krejcia3045382018-11-22 14:30:31 +0100138 case LYS_ACTION:
139 case LYS_NOTIF:
140 case LYS_LEAF:
141 case LYS_ANYXML:
142 case LYS_ANYDATA:
143 case LYS_LIST:
144 case LYS_LEAFLIST:
Radek Krejcia9026eb2018-12-12 16:04:47 +0100145 case LYS_CASE:
Radek Krejcia3045382018-11-22 14:30:31 +0100146 break;
147 case LYS_CONTAINER:
148 if (!(((struct lysc_node_container *)next)->flags & LYS_PRESENCE) && (options & LYS_GETNEXT_INTONPCONT)) {
149 if (((struct lysc_node_container *)next)->child) {
150 /* go into */
151 next = ((struct lysc_node_container *)next)->child;
152 } else {
153 next = next->next;
154 }
155 goto repeat;
156 }
157 break;
158 case LYS_CHOICE:
159 if (options & LYS_GETNEXT_WITHCHOICE) {
160 return next;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100161 } else if ((options & LYS_GETNEXT_NOCHOICE) || !((struct lysc_node_choice *)next)->cases) {
162 next = next->next;
163 } else {
Radek Krejcia3045382018-11-22 14:30:31 +0100164 /* go into */
Radek Krejcia9026eb2018-12-12 16:04:47 +0100165 if (options & LYS_GETNEXT_WITHCASE) {
Radek Krejci05b774b2019-02-25 13:26:18 +0100166 next = (struct lysc_node*)((struct lysc_node_choice *)next)->cases;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100167 } else {
168 next = ((struct lysc_node_choice *)next)->cases->child;
169 }
Radek Krejcia3045382018-11-22 14:30:31 +0100170 }
171 goto repeat;
172 default:
173 /* we should not be here */
Radek Krejcib07b5c92019-04-08 10:56:37 +0200174 LOGINT(module ? module->mod->ctx : parent->module->ctx);
Radek Krejcia3045382018-11-22 14:30:31 +0100175 return NULL;
176 }
177
178 if (!(options & LYS_GETNEXT_NOSTATECHECK)) {
179 /* check if the node is disabled by if-feature */
Radek Krejcifab954b2019-09-11 11:25:14 +0200180 if (lysc_node_is_disabled(next, 0)) {
Radek Krejcia3045382018-11-22 14:30:31 +0100181 next = next->next;
182 goto repeat;
183 }
184 }
185 return next;
186}
187
188API const struct lysc_node *
Michal Vaskoe444f752020-02-10 12:20:06 +0100189lys_find_child(const struct lysc_node *parent, const struct lys_module *module, const char *name, size_t name_len,
190 uint16_t nodetype, int options)
Radek Krejcia3045382018-11-22 14:30:31 +0100191{
192 const struct lysc_node *node = NULL;
193
194 LY_CHECK_ARG_RET(NULL, module, name, NULL);
195 if (!nodetype) {
196 nodetype = 0xffff;
197 }
198
199 while ((node = lys_getnext(node, parent, module->compiled, options))) {
200 if (!(node->nodetype & nodetype)) {
201 continue;
202 }
203 if (node->module != module) {
204 continue;
205 }
206
207 if (name_len) {
Radek Krejci7f9b6512019-09-18 13:11:09 +0200208 if (!ly_strncmp(node->name, name, name_len)) {
Radek Krejcia3045382018-11-22 14:30:31 +0100209 return node;
210 }
211 } else {
212 if (!strcmp(node->name, name)) {
213 return node;
214 }
215 }
216 }
217 return NULL;
218}
219
Michal Vasko14654712020-02-06 08:35:21 +0100220char *
221lysc_path_until(const struct lysc_node *node, const struct lysc_node *parent, LYSC_PATH_TYPE pathtype, char *buffer,
222 size_t buflen)
Radek Krejci327de162019-06-14 12:52:07 +0200223{
Michal Vasko03ff5a72019-09-11 13:49:33 +0200224 const struct lysc_node *iter;
Radek Krejci327de162019-06-14 12:52:07 +0200225 char *path = NULL;
226 int len = 0;
227
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200228 LY_CHECK_ARG_RET(NULL, node, NULL);
229 if (buffer) {
230 LY_CHECK_ARG_RET(node->module->ctx, buflen > 1, NULL);
231 }
232
Radek Krejci327de162019-06-14 12:52:07 +0200233 switch (pathtype) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200234 case LYSC_PATH_LOG:
Michal Vasko90932a92020-02-12 14:33:03 +0100235 for (iter = node; iter && (iter != parent) && (len >= 0); iter = iter->parent) {
Radek Krejci1c0c3442019-07-23 16:08:47 +0200236 char *s = buffer ? strdup(buffer) : path;
Radek Krejci327de162019-06-14 12:52:07 +0200237 char *id;
Michal Vasko14654712020-02-06 08:35:21 +0100238 const char *slash;
Radek Krejci327de162019-06-14 12:52:07 +0200239
Michal Vasko03ff5a72019-09-11 13:49:33 +0200240 id = strdup(iter->name);
Michal Vasko14654712020-02-06 08:35:21 +0100241 if (parent && (iter->parent == parent)) {
242 slash = "";
243 } else {
244 slash = "/";
245 }
Radek Krejci327de162019-06-14 12:52:07 +0200246 if (!iter->parent || iter->parent->module != iter->module) {
247 /* print prefix */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200248 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100249 len = snprintf(buffer, buflen, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200250 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100251 len = asprintf(&path, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200252 }
Radek Krejci327de162019-06-14 12:52:07 +0200253 } else {
254 /* prefix is the same as in parent */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200255 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100256 len = snprintf(buffer, buflen, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200257 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100258 len = asprintf(&path, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200259 }
Radek Krejci327de162019-06-14 12:52:07 +0200260 }
261 free(s);
262 free(id);
Radek Krejci1c0c3442019-07-23 16:08:47 +0200263
264 if (buffer && buflen <= (size_t)len) {
265 /* not enough space in buffer */
266 break;
267 }
Radek Krejci327de162019-06-14 12:52:07 +0200268 }
269
270 if (len < 0) {
271 free(path);
272 path = NULL;
273 } else if (len == 0) {
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200274 if (buffer) {
275 strcpy(buffer, "/");
276 } else {
277 path = strdup("/");
278 }
Radek Krejci327de162019-06-14 12:52:07 +0200279 }
280 break;
281 }
282
Radek Krejci1c0c3442019-07-23 16:08:47 +0200283 if (buffer) {
284 return buffer;
285 } else {
286 return path;
287 }
Radek Krejci327de162019-06-14 12:52:07 +0200288}
289
Michal Vasko14654712020-02-06 08:35:21 +0100290API char *
291lysc_path(const struct lysc_node *node, LYSC_PATH_TYPE pathtype, char *buffer, size_t buflen)
292{
293 return lysc_path_until(node, NULL, pathtype, buffer, buflen);
294}
295
Radek Krejci19a96102018-11-15 13:38:09 +0100296API int
297lysc_feature_value(const struct lysc_feature *feature)
Radek Krejci6f7feb62018-10-12 15:23:02 +0200298{
Radek Krejci19a96102018-11-15 13:38:09 +0100299 LY_CHECK_ARG_RET(NULL, feature, -1);
300 return feature->flags & LYS_FENABLED ? 1 : 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200301}
302
Radek Krejci693262f2019-04-29 15:23:20 +0200303uint8_t
304lysc_iff_getop(uint8_t *list, int pos)
Radek Krejci151a5b72018-10-19 14:21:44 +0200305{
306 uint8_t *item;
307 uint8_t mask = 3, result;
308
309 assert(pos >= 0);
310
311 item = &list[pos / 4];
312 result = (*item) & (mask << 2 * (pos % 4));
313 return result >> 2 * (pos % 4);
314}
315
Radek Krejci151a5b72018-10-19 14:21:44 +0200316static int
317lysc_iffeature_value_(const struct lysc_iffeature *iff, int *index_e, int *index_f)
318{
319 uint8_t op;
320 int a, b;
321
Radek Krejci693262f2019-04-29 15:23:20 +0200322 op = lysc_iff_getop(iff->expr, *index_e);
Radek Krejci151a5b72018-10-19 14:21:44 +0200323 (*index_e)++;
324
325 switch (op) {
326 case LYS_IFF_F:
327 /* resolve feature */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200328 return lysc_feature_value(iff->features[(*index_f)++]);
Radek Krejci151a5b72018-10-19 14:21:44 +0200329 case LYS_IFF_NOT:
330 /* invert result */
331 return lysc_iffeature_value_(iff, index_e, index_f) ? 0 : 1;
332 case LYS_IFF_AND:
333 case LYS_IFF_OR:
334 a = lysc_iffeature_value_(iff, index_e, index_f);
335 b = lysc_iffeature_value_(iff, index_e, index_f);
336 if (op == LYS_IFF_AND) {
337 return a && b;
338 } else { /* LYS_IFF_OR */
339 return a || b;
340 }
341 }
342
343 return 0;
344}
345
346API int
347lysc_iffeature_value(const struct lysc_iffeature *iff)
348{
349 int index_e = 0, index_f = 0;
350
351 LY_CHECK_ARG_RET(NULL, iff, -1);
352
353 if (iff->expr) {
354 return lysc_iffeature_value_(iff, &index_e, &index_f);
355 }
356 return 0;
357}
358
Radek Krejci151a5b72018-10-19 14:21:44 +0200359/**
360 * @brief Enable/Disable the specified feature in the module.
361 *
362 * If the feature is already set to the desired value, LY_SUCCESS is returned.
363 * By changing the feature, also all the feature which depends on it via their
364 * if-feature statements are again evaluated (disabled if a if-feature statemen
365 * evaluates to false).
366 *
Radek Krejci0af46292019-01-11 16:02:31 +0100367 * @param[in] mod Module where to set (search for) the feature.
Radek Krejci151a5b72018-10-19 14:21:44 +0200368 * @param[in] name Name of the feature to set. Asterisk ('*') can be used to
369 * set all the features in the module.
370 * @param[in] value Desired value of the feature: 1 (enable) or 0 (disable).
371 * @return LY_ERR value.
372 */
373static LY_ERR
Radek Krejci0af46292019-01-11 16:02:31 +0100374lys_feature_change(const struct lys_module *mod, const char *name, int value)
Radek Krejci151a5b72018-10-19 14:21:44 +0200375{
376 int all = 0;
Radek Krejcica3db002018-11-01 10:31:01 +0100377 unsigned int u, changed_count, disabled_count;
Radek Krejci151a5b72018-10-19 14:21:44 +0200378 struct lysc_feature *f, **df;
379 struct lysc_iffeature *iff;
380 struct ly_set *changed;
Radek Krejci0af46292019-01-11 16:02:31 +0100381 struct ly_ctx *ctx = mod->ctx; /* shortcut */
Radek Krejci151a5b72018-10-19 14:21:44 +0200382
Radek Krejci6e67c402019-05-02 09:55:39 +0200383 if (!strcmp(name, "*")) {
384 /* enable all */
385 all = 1;
386 }
387
Radek Krejci0af46292019-01-11 16:02:31 +0100388 if (!mod->compiled) {
389 LOGERR(ctx, LY_EINVAL, "Module \"%s\" is not implemented so all its features are permanently disabled without a chance to change it.",
390 mod->name);
391 return LY_EINVAL;
392 }
393 if (!mod->compiled->features) {
Radek Krejci6e67c402019-05-02 09:55:39 +0200394 if (all) {
395 /* no feature to enable */
396 return LY_SUCCESS;
397 }
Radek Krejci0af46292019-01-11 16:02:31 +0100398 LOGERR(ctx, LY_EINVAL, "Unable to switch feature since the module \"%s\" has no features.", mod->name);
Radek Krejci151a5b72018-10-19 14:21:44 +0200399 return LY_EINVAL;
400 }
401
Radek Krejci151a5b72018-10-19 14:21:44 +0200402 changed = ly_set_new();
Radek Krejcica3db002018-11-01 10:31:01 +0100403 changed_count = 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200404
Radek Krejcica3db002018-11-01 10:31:01 +0100405run:
Radek Krejci0af46292019-01-11 16:02:31 +0100406 for (disabled_count = u = 0; u < LY_ARRAY_SIZE(mod->compiled->features); ++u) {
407 f = &mod->compiled->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200408 if (all || !strcmp(f->name, name)) {
409 if ((value && (f->flags & LYS_FENABLED)) || (!value && !(f->flags & LYS_FENABLED))) {
410 if (all) {
411 /* skip already set features */
412 continue;
413 } else {
414 /* feature already set correctly */
415 ly_set_free(changed, NULL);
416 return LY_SUCCESS;
417 }
418 }
419
420 if (value) { /* enable */
421 /* check referenced features if they are enabled */
422 LY_ARRAY_FOR(f->iffeatures, struct lysc_iffeature, iff) {
423 if (!lysc_iffeature_value(iff)) {
424 if (all) {
Radek Krejcica3db002018-11-01 10:31:01 +0100425 ++disabled_count;
Radek Krejci151a5b72018-10-19 14:21:44 +0200426 goto next;
427 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100428 LOGERR(ctx, LY_EDENIED,
Radek Krejci151a5b72018-10-19 14:21:44 +0200429 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
430 f->name);
431 ly_set_free(changed, NULL);
432 return LY_EDENIED;
433 }
434 }
435 }
436 /* enable the feature */
437 f->flags |= LYS_FENABLED;
438 } else { /* disable */
439 /* disable the feature */
440 f->flags &= ~LYS_FENABLED;
441 }
442
443 /* remember the changed feature */
444 ly_set_add(changed, f, LY_SET_OPT_USEASLIST);
445
446 if (!all) {
447 /* stop in case changing a single feature */
448 break;
449 }
450 }
451next:
452 ;
453 }
454
455 if (!all && !changed->count) {
Radek Krejci0af46292019-01-11 16:02:31 +0100456 LOGERR(ctx, LY_EINVAL, "Feature \"%s\" not found in module \"%s\".", name, mod->name);
Radek Krejci151a5b72018-10-19 14:21:44 +0200457 ly_set_free(changed, NULL);
458 return LY_EINVAL;
459 }
460
Radek Krejcica3db002018-11-01 10:31:01 +0100461 if (value && all && disabled_count) {
462 if (changed_count == changed->count) {
463 /* no change in last run -> not able to enable all ... */
464 /* ... print errors */
Radek Krejci0af46292019-01-11 16:02:31 +0100465 for (u = 0; disabled_count && u < LY_ARRAY_SIZE(mod->compiled->features); ++u) {
466 if (!(mod->compiled->features[u].flags & LYS_FENABLED)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100467 LOGERR(ctx, LY_EDENIED,
Radek Krejcica3db002018-11-01 10:31:01 +0100468 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
Radek Krejci0af46292019-01-11 16:02:31 +0100469 mod->compiled->features[u].name);
Radek Krejcica3db002018-11-01 10:31:01 +0100470 --disabled_count;
471 }
472 }
473 /* ... restore the original state */
474 for (u = 0; u < changed->count; ++u) {
475 f = changed->objs[u];
476 /* re-disable the feature */
477 f->flags &= ~LYS_FENABLED;
478 }
479
480 ly_set_free(changed, NULL);
481 return LY_EDENIED;
482 } else {
483 /* we did some change in last run, try it again */
484 changed_count = changed->count;
485 goto run;
486 }
487 }
488
Radek Krejci151a5b72018-10-19 14:21:44 +0200489 /* reflect change(s) in the dependent features */
490 for (u = 0; u < changed->count; ++u) {
491 /* If a dependent feature is enabled, it can be now changed by the change (to false) of the value of
492 * its if-feature statements. The reverse logic, automatically enable feature when its feature is enabled
493 * is not done - by default, features are disabled and must be explicitely enabled. */
494 f = changed->objs[u];
495 LY_ARRAY_FOR(f->depfeatures, struct lysc_feature*, df) {
496 if (!((*df)->flags & LYS_FENABLED)) {
497 /* not enabled, nothing to do */
498 continue;
499 }
500 /* check the feature's if-features which could change by the previous change of our feature */
501 LY_ARRAY_FOR((*df)->iffeatures, struct lysc_iffeature, iff) {
502 if (!lysc_iffeature_value(iff)) {
503 /* the feature must be disabled now */
504 (*df)->flags &= ~LYS_FENABLED;
505 /* add the feature into the list of changed features */
506 ly_set_add(changed, *df, LY_SET_OPT_USEASLIST);
507 break;
508 }
509 }
510 }
511 }
512
513 ly_set_free(changed, NULL);
514 return LY_SUCCESS;
515}
516
517API LY_ERR
Radek Krejcied5acc52019-04-25 15:57:04 +0200518lys_feature_enable(const struct lys_module *module, const char *feature)
Radek Krejci151a5b72018-10-19 14:21:44 +0200519{
Radek Krejci0af46292019-01-11 16:02:31 +0100520 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200521
Radek Krejcied5acc52019-04-25 15:57:04 +0200522 return lys_feature_change((struct lys_module*)module, feature, 1);
Radek Krejci151a5b72018-10-19 14:21:44 +0200523}
524
525API LY_ERR
Radek Krejcied5acc52019-04-25 15:57:04 +0200526lys_feature_disable(const struct lys_module *module, const char *feature)
Radek Krejci151a5b72018-10-19 14:21:44 +0200527{
Radek Krejci0af46292019-01-11 16:02:31 +0100528 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200529
Radek Krejcied5acc52019-04-25 15:57:04 +0200530 return lys_feature_change((struct lys_module*)module, feature, 0);
Radek Krejci151a5b72018-10-19 14:21:44 +0200531}
532
533API int
534lys_feature_value(const struct lys_module *module, const char *feature)
535{
536 struct lysc_feature *f;
537 struct lysc_module *mod;
538 unsigned int u;
539
540 LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, -1);
541 mod = module->compiled;
542
543 /* search for the specified feature */
544 for (u = 0; u < LY_ARRAY_SIZE(mod->features); ++u) {
Radek Krejci2c4e7172018-10-19 15:56:26 +0200545 f = &mod->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200546 if (!strcmp(f->name, feature)) {
547 if (f->flags & LYS_FENABLED) {
548 return 1;
549 } else {
550 return 0;
551 }
552 }
553 }
554
555 /* feature definition not found */
556 return -1;
557}
558
Michal Vaskoc193ce92020-03-06 11:04:48 +0100559API const struct lysc_node *
Radek Krejcifab954b2019-09-11 11:25:14 +0200560lysc_node_is_disabled(const struct lysc_node *node, int recursive)
Radek Krejcia3045382018-11-22 14:30:31 +0100561{
562 unsigned int u;
Radek Krejcia3045382018-11-22 14:30:31 +0100563
564 LY_CHECK_ARG_RET(NULL, node, NULL);
565
Michal Vaskoc193ce92020-03-06 11:04:48 +0100566 do {
Radek Krejci056d0a82018-12-06 16:57:25 +0100567 if (node->iffeatures) {
Radek Krejcia3045382018-11-22 14:30:31 +0100568 /* check local if-features */
Radek Krejci056d0a82018-12-06 16:57:25 +0100569 LY_ARRAY_FOR(node->iffeatures, u) {
570 if (!lysc_iffeature_value(&node->iffeatures[u])) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100571 return node;
Radek Krejcia3045382018-11-22 14:30:31 +0100572 }
573 }
574 }
575
576 if (!recursive) {
577 return NULL;
578 }
579
Michal Vaskoc193ce92020-03-06 11:04:48 +0100580 /* go through schema-only parents */
Radek Krejcia3045382018-11-22 14:30:31 +0100581 node = node->parent;
Michal Vaskoc193ce92020-03-06 11:04:48 +0100582 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
583
Radek Krejcia3045382018-11-22 14:30:31 +0100584 return NULL;
585}
586
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200587LY_ERR
588lys_set_implemented_internal(struct lys_module *mod, uint8_t value)
589{
590 struct lys_module *m;
591
592 LY_CHECK_ARG_RET(NULL, mod, LY_EINVAL);
593
594 if (mod->implemented) {
595 return LY_SUCCESS;
596 }
597
598 /* we have module from the current context */
599 m = ly_ctx_get_module_implemented(mod->ctx, mod->name);
600 if (m) {
601 if (m != mod) {
602 /* check collision with other implemented revision */
603 LOGERR(mod->ctx, LY_EDENIED, "Module \"%s\" is present in the context in other implemented revision (%s).",
604 mod->name, mod->revision ? mod->revision : "module without revision");
605 return LY_EDENIED;
606 } else {
607 /* mod is already implemented */
608 return LY_SUCCESS;
609 }
610 }
611
612 /* mark the module implemented, check for collision was already done */
613 mod->implemented = value;
614
615 /* compile the schema */
616 LY_CHECK_RET(lys_compile(mod, LYSC_OPT_INTERNAL));
617
618 return LY_SUCCESS;
619}
620
621API LY_ERR
622lys_set_implemented(struct lys_module *mod)
623{
624 return lys_set_implemented_internal(mod, 1);
625}
626
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100627struct lysp_submodule *
Radek Krejcie7b95092019-05-15 11:03:07 +0200628lys_parse_mem_submodule(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, struct lys_parser_ctx *main_ctx,
Michal Vaskob36053d2020-03-26 15:49:30 +0100629 LY_ERR (*custom_check)(const struct ly_ctx*, struct lysp_module*, struct lysp_submodule*, void*), void *check_data)
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200630{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100631 LY_ERR ret = LY_EINVAL;
632 struct lysp_submodule *submod = NULL, *latest_sp;
Michal Vaskob36053d2020-03-26 15:49:30 +0100633 struct lys_yang_parser_ctx *yangctx = NULL;
634 struct lys_yin_parser_ctx *yinctx = NULL;
635 struct lys_parser_ctx *pctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100636
637 LY_CHECK_ARG_RET(ctx, ctx, data, NULL);
638
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100639 switch (format) {
640 case LYS_IN_YIN:
Michal Vaskob36053d2020-03-26 15:49:30 +0100641 ret = yin_parse_submodule(&yinctx, ctx, main_ctx, data, &submod);
642 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100643 break;
644 case LYS_IN_YANG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100645 ret = yang_parse_submodule(&yangctx, ctx, main_ctx, data, &submod);
646 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100647 break;
648 default:
David Sedlák4f2f5ba2019-08-15 13:18:48 +0200649 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100650 break;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200651 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100652 LY_CHECK_RET(ret, NULL);
653
654 /* make sure that the newest revision is at position 0 */
655 lysp_sort_revisions(submod->revs);
656
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100657 /* decide the latest revision */
Michal Vaskob36053d2020-03-26 15:49:30 +0100658 latest_sp = ly_ctx_get_submodule(PARSER_CTX(pctx), submod->belongsto, submod->name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100659 if (latest_sp) {
660 if (submod->revs) {
661 if (!latest_sp->revs) {
662 /* latest has no revision, so mod is anyway newer */
663 submod->latest_revision = latest_sp->latest_revision;
Radek Krejcib3289d62019-09-18 12:21:39 +0200664 /* the latest_sp is zeroed later when the new module is being inserted into the context */
665 } else if (strcmp(submod->revs[0].date, latest_sp->revs[0].date) > 0) {
666 submod->latest_revision = latest_sp->latest_revision;
667 /* the latest_sp is zeroed later when the new module is being inserted into the context */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100668 } else {
Radek Krejcib3289d62019-09-18 12:21:39 +0200669 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100670 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200671 } else {
672 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100673 }
674 } else {
675 submod->latest_revision = 1;
676 }
677
Radek Krejcib3289d62019-09-18 12:21:39 +0200678 if (custom_check) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100679 LY_CHECK_GOTO(custom_check(PARSER_CTX(pctx), NULL, submod, check_data), error);
Radek Krejcib3289d62019-09-18 12:21:39 +0200680 }
681
682 if (latest_sp) {
683 latest_sp->latest_revision = 0;
684 }
685
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100686 /* remap possibly changed and reallocated typedefs and groupings list back to the main context */
Michal Vaskob36053d2020-03-26 15:49:30 +0100687 memcpy(&main_ctx->tpdfs_nodes, &pctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
688 memcpy(&main_ctx->grps_nodes, &pctx->grps_nodes, sizeof main_ctx->grps_nodes);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100689
David Sedlák1b623122019-08-05 15:27:49 +0200690 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100691 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200692 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100693 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200694 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100695 return submod;
David Sedlák1b623122019-08-05 15:27:49 +0200696
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100697error:
698 lysp_submodule_free(ctx, submod);
David Sedlák1b623122019-08-05 15:27:49 +0200699 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100700 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200701 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100702 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200703 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100704 return NULL;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200705}
706
Radek Krejcid33273d2018-10-25 14:55:52 +0200707struct lys_module *
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100708lys_parse_mem_module(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, int implement,
Michal Vaskob36053d2020-03-26 15:49:30 +0100709 LY_ERR (*custom_check)(const struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100710 void *check_data)
Radek Krejci86d106e2018-10-18 09:53:19 +0200711{
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100712 struct lys_module *mod = NULL, *latest, *mod_dup;
Radek Krejci086c7132018-10-26 15:29:04 +0200713 struct lysp_import *imp;
714 struct lysp_include *inc;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100715 LY_ERR ret = LY_EINVAL;
Radek Krejci086c7132018-10-26 15:29:04 +0200716 unsigned int u, i;
Michal Vaskob36053d2020-03-26 15:49:30 +0100717 struct lys_yang_parser_ctx *yangctx = NULL;
718 struct lys_yin_parser_ctx *yinctx = NULL;
719 struct lys_parser_ctx *pctx;
Radek Krejci86d106e2018-10-18 09:53:19 +0200720
721 LY_CHECK_ARG_RET(ctx, ctx, data, NULL);
722
723 mod = calloc(1, sizeof *mod);
724 LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100725 mod->ctx = ctx;
Radek Krejci86d106e2018-10-18 09:53:19 +0200726
727 switch (format) {
728 case LYS_IN_YIN:
Michal Vaskob36053d2020-03-26 15:49:30 +0100729 ret = yin_parse_module(&yinctx, data, mod);
730 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci86d106e2018-10-18 09:53:19 +0200731 break;
732 case LYS_IN_YANG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100733 ret = yang_parse_module(&yangctx, data, mod);
734 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci86d106e2018-10-18 09:53:19 +0200735 break;
736 default:
737 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
738 break;
739 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100740 LY_CHECK_ERR_RET(ret, lys_module_free(mod, NULL), NULL);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200741
742 /* make sure that the newest revision is at position 0 */
743 lysp_sort_revisions(mod->parsed->revs);
Radek Krejci0af46292019-01-11 16:02:31 +0100744 if (mod->parsed->revs) {
745 mod->revision = lydict_insert(ctx, mod->parsed->revs[0].date, 0);
746 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200747
Radek Krejcib3289d62019-09-18 12:21:39 +0200748 /* decide the latest revision */
749 latest = (struct lys_module*)ly_ctx_get_module_latest(ctx, mod->name);
750 if (latest) {
751 if (mod->revision) {
752 if (!latest->revision) {
753 /* latest has no revision, so mod is anyway newer */
754 mod->latest_revision = latest->latest_revision;
755 /* the latest is zeroed later when the new module is being inserted into the context */
756 } else if (strcmp(mod->revision, latest->revision) > 0) {
757 mod->latest_revision = latest->latest_revision;
758 /* the latest is zeroed later when the new module is being inserted into the context */
759 } else {
760 latest = NULL;
761 }
762 } else {
763 latest = NULL;
764 }
765 } else {
766 mod->latest_revision = 1;
767 }
768
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100769 if (custom_check) {
770 LY_CHECK_GOTO(custom_check(ctx, mod->parsed, NULL, check_data), error);
771 }
772
Radek Krejci86d106e2018-10-18 09:53:19 +0200773 if (implement) {
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200774 /* mark the loaded module implemented */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100775 if (ly_ctx_get_module_implemented(ctx, mod->name)) {
776 LOGERR(ctx, LY_EDENIED, "Module \"%s\" is already implemented in the context.", mod->name);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100777 goto error;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200778 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100779 mod->implemented = 1;
Radek Krejci86d106e2018-10-18 09:53:19 +0200780 }
781
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100782 /* check for duplicity in the context */
Radek Krejci0af46292019-01-11 16:02:31 +0100783 mod_dup = (struct lys_module*)ly_ctx_get_module(ctx, mod->name, mod->revision);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100784 if (mod_dup) {
785 if (mod_dup->parsed) {
786 /* error */
Radek Krejcid33273d2018-10-25 14:55:52 +0200787 if (mod->parsed->revs) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100788 LOGERR(ctx, LY_EEXIST, "Module \"%s\" of revision \"%s\" is already present in the context.",
789 mod->name, mod->parsed->revs[0].date);
Radek Krejcid33273d2018-10-25 14:55:52 +0200790 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100791 LOGERR(ctx, LY_EEXIST, "Module \"%s\" with no revision is already present in the context.",
792 mod->name);
Radek Krejcid33273d2018-10-25 14:55:52 +0200793 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100794 goto error;
795 } else {
796 /* add the parsed data to the currently compiled-only module in the context */
797 mod_dup->parsed = mod->parsed;
798 mod_dup->parsed->mod = mod_dup;
799 mod->parsed = NULL;
800 lys_module_free(mod, NULL);
801 mod = mod_dup;
802 goto finish_parsing;
Radek Krejcid33273d2018-10-25 14:55:52 +0200803 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100804 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200805
Radek Krejci0af46292019-01-11 16:02:31 +0100806 if (!mod->implemented) {
Michal Vasko5fe75f12020-03-02 13:52:37 +0100807 /* pre-compile features of the module */
Radek Krejci327de162019-06-14 12:52:07 +0200808 LY_CHECK_GOTO(lys_feature_precompile(NULL, ctx, mod, mod->parsed->features, &mod->off_features), error);
Radek Krejci0af46292019-01-11 16:02:31 +0100809 }
810
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100811 if (latest) {
Radek Krejcib3289d62019-09-18 12:21:39 +0200812 latest->latest_revision = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100813 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200814
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100815 /* add into context */
816 ly_set_add(&ctx->list, mod, LY_SET_OPT_USEASLIST);
Radek Krejcid33273d2018-10-25 14:55:52 +0200817
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100818finish_parsing:
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100819 /* resolve imports */
820 mod->parsed->parsing = 1;
821 LY_ARRAY_FOR(mod->parsed->imports, u) {
822 imp = &mod->parsed->imports[u];
823 if (!imp->module && lysp_load_module(ctx, imp->name, imp->rev[0] ? imp->rev : NULL, 0, 0, &imp->module)) {
824 goto error_ctx;
Radek Krejci086c7132018-10-26 15:29:04 +0200825 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100826 /* check for importing the same module twice */
827 for (i = 0; i < u; ++i) {
828 if (imp->module == mod->parsed->imports[i].module) {
829 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Single revision of the module \"%s\" referred twice.", imp->name);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100830 goto error_ctx;
Radek Krejci086c7132018-10-26 15:29:04 +0200831 }
832 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200833 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100834 LY_ARRAY_FOR(mod->parsed->includes, u) {
835 inc = &mod->parsed->includes[u];
Michal Vaskob36053d2020-03-26 15:49:30 +0100836 if (!inc->submodule && lysp_load_submodule(pctx, mod->parsed, inc)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100837 goto error_ctx;
838 }
Radek Krejci0af46292019-01-11 16:02:31 +0100839 if (!mod->implemented) {
Michal Vasko5fe75f12020-03-02 13:52:37 +0100840 /* pre-compile features of the submodule */
Radek Krejci327de162019-06-14 12:52:07 +0200841 LY_CHECK_GOTO(lys_feature_precompile(NULL, ctx, mod, inc->submodule->features, &mod->off_features), error);
Radek Krejci0af46292019-01-11 16:02:31 +0100842 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100843 }
844 mod->parsed->parsing = 0;
845
Radek Krejci7fc68292019-06-12 13:51:09 +0200846 /* check name collisions - typedefs and TODO groupings */
Michal Vaskob36053d2020-03-26 15:49:30 +0100847 LY_CHECK_GOTO(lysp_check_typedefs(pctx, mod->parsed), error_ctx);
Radek Krejcid33273d2018-10-25 14:55:52 +0200848
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 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200854 return mod;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100855
856error_ctx:
857 ly_set_rm(&ctx->list, mod, NULL);
858error:
859 lys_module_free(mod, NULL);
Michal Vaskob36053d2020-03-26 15:49:30 +0100860 ly_set_erase(&pctx->tpdfs_nodes, NULL);
David Sedlák1b623122019-08-05 15:27:49 +0200861 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100862 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200863 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100864 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200865 }
866
Radek Krejcibbe09a92018-11-08 09:36:54 +0100867 return NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +0200868}
869
Radek Krejcid14e9692018-11-01 11:00:37 +0100870API struct lys_module *
Radek Krejci86d106e2018-10-18 09:53:19 +0200871lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format)
872{
Radek Krejci096235c2019-01-11 11:12:19 +0100873 struct lys_module *mod;
874
875 mod = lys_parse_mem_module(ctx, data, format, 1, NULL, NULL);
876 LY_CHECK_RET(!mod, NULL);
877
878 if (lys_compile(mod, 0)) {
879 ly_set_rm(&ctx->list, mod, NULL);
880 lys_module_free(mod, NULL);
881 return NULL;
882 }
883 return mod;
Radek Krejci86d106e2018-10-18 09:53:19 +0200884}
885
886static void
887lys_parse_set_filename(struct ly_ctx *ctx, const char **filename, int fd)
888{
Radek Krejci65639b92018-11-27 10:51:37 +0100889 char path[PATH_MAX];
Radek Krejci86d106e2018-10-18 09:53:19 +0200890
891#ifdef __APPLE__
892 if (fcntl(fd, F_GETPATH, path) != -1) {
893 *filename = lydict_insert(ctx, path, 0);
894 }
895#else
Radek Krejci65639b92018-11-27 10:51:37 +0100896 int len;
897 char proc_path[32];
898
Radek Krejci86d106e2018-10-18 09:53:19 +0200899 /* get URI if there is /proc */
900 sprintf(proc_path, "/proc/self/fd/%d", fd);
901 if ((len = readlink(proc_path, path, PATH_MAX - 1)) > 0) {
902 *filename = lydict_insert(ctx, path, len);
903 }
904#endif
905}
906
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100907void *
Radek Krejcie7b95092019-05-15 11:03:07 +0200908lys_parse_fd_(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, int implement, struct lys_parser_ctx *main_ctx,
Michal Vaskob36053d2020-03-26 15:49:30 +0100909 lys_custom_check custom_check, void *check_data)
Radek Krejci86d106e2018-10-18 09:53:19 +0200910{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100911 void *result;
912 struct lys_module *mod = NULL;
913 struct lysp_submodule *submod = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +0200914 size_t length;
915 char *addr;
916
917 LY_CHECK_ARG_RET(ctx, ctx, NULL);
918 if (fd < 0) {
919 LOGARG(ctx, fd);
920 return NULL;
921 }
922
923 LY_CHECK_RET(ly_mmap(ctx, fd, &length, (void **)&addr), NULL);
924 if (!addr) {
925 LOGERR(ctx, LY_EINVAL, "Empty schema file.");
926 return NULL;
927 }
928
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100929 if (main_ctx) {
930 result = submod = lys_parse_mem_submodule(ctx, addr, format, main_ctx, custom_check, check_data);
931 } else {
932 result = mod = lys_parse_mem_module(ctx, addr, format, implement, custom_check, check_data);
Radek Krejci096235c2019-01-11 11:12:19 +0100933 if (mod && implement && lys_compile(mod, 0)) {
934 ly_set_rm(&ctx->list, mod, NULL);
935 lys_module_free(mod, NULL);
936 result = mod = NULL;
937 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100938 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200939 ly_munmap(addr, length);
940
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100941 if (mod && !mod->filepath) {
942 lys_parse_set_filename(ctx, &mod->filepath, fd);
943 } else if (submod && !submod->filepath) {
944 lys_parse_set_filename(ctx, &submod->filepath, fd);
Radek Krejci86d106e2018-10-18 09:53:19 +0200945 }
946
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100947 return result;
948}
949
950struct lys_module *
Michal Vaskob36053d2020-03-26 15:49:30 +0100951lys_parse_fd_module(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, int implement, lys_custom_check custom_check,
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100952 void *check_data)
953{
954 return (struct lys_module*)lys_parse_fd_(ctx, fd, format, implement, NULL, custom_check, check_data);
955}
956
957struct lysp_submodule *
Radek Krejcie7b95092019-05-15 11:03:07 +0200958lys_parse_fd_submodule(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, struct lys_parser_ctx *main_ctx,
Michal Vaskob36053d2020-03-26 15:49:30 +0100959 lys_custom_check custom_check, void *check_data)
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100960{
961 assert(main_ctx);
962 return (struct lysp_submodule*)lys_parse_fd_(ctx, fd, format, 0, main_ctx, custom_check, check_data);
Radek Krejci86d106e2018-10-18 09:53:19 +0200963}
964
Radek Krejcid14e9692018-11-01 11:00:37 +0100965API struct lys_module *
Radek Krejci86d106e2018-10-18 09:53:19 +0200966lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format)
967{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100968 return lys_parse_fd_module(ctx, fd, format, 1, NULL, NULL);
Radek Krejci86d106e2018-10-18 09:53:19 +0200969}
970
Radek Krejcid33273d2018-10-25 14:55:52 +0200971struct lys_module *
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100972lys_parse_path_(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, int implement,
Michal Vaskob36053d2020-03-26 15:49:30 +0100973 lys_custom_check custom_check, void *check_data)
Radek Krejci86d106e2018-10-18 09:53:19 +0200974{
975 int fd;
Radek Krejcid33273d2018-10-25 14:55:52 +0200976 struct lys_module *mod;
Radek Krejci86d106e2018-10-18 09:53:19 +0200977 const char *rev, *dot, *filename;
978 size_t len;
979
980 LY_CHECK_ARG_RET(ctx, ctx, path, NULL);
981
982 fd = open(path, O_RDONLY);
983 LY_CHECK_ERR_RET(fd == -1, LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)), NULL);
984
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100985 mod = lys_parse_fd_module(ctx, fd, format, implement, custom_check, check_data);
Radek Krejci86d106e2018-10-18 09:53:19 +0200986 close(fd);
987 LY_CHECK_RET(!mod, NULL);
988
989 /* check that name and revision match filename */
990 filename = strrchr(path, '/');
991 if (!filename) {
992 filename = path;
993 } else {
994 filename++;
995 }
996 rev = strchr(filename, '@');
997 dot = strrchr(filename, '.');
998
999 /* name */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001000 len = strlen(mod->name);
1001 if (strncmp(filename, mod->name, len) ||
Radek Krejci86d106e2018-10-18 09:53:19 +02001002 ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001003 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name);
Radek Krejci86d106e2018-10-18 09:53:19 +02001004 }
1005 if (rev) {
1006 len = dot - ++rev;
Radek Krejcib7db73a2018-10-24 14:18:40 +02001007 if (!mod->parsed->revs || len != 10 || strncmp(mod->parsed->revs[0].date, rev, len)) {
Radek Krejci86d106e2018-10-18 09:53:19 +02001008 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Radek Krejcib7db73a2018-10-24 14:18:40 +02001009 mod->parsed->revs ? mod->parsed->revs[0].date : "none");
Radek Krejci86d106e2018-10-18 09:53:19 +02001010 }
1011 }
1012
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001013 if (!mod->filepath) {
Radek Krejci86d106e2018-10-18 09:53:19 +02001014 /* store URI */
1015 char rpath[PATH_MAX];
1016 if (realpath(path, rpath) != NULL) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001017 mod->filepath = lydict_insert(ctx, rpath, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001018 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001019 mod->filepath = lydict_insert(ctx, path, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001020 }
1021 }
1022
1023 return mod;
1024}
1025
Radek Krejcid14e9692018-11-01 11:00:37 +01001026API struct lys_module *
Radek Krejcid33273d2018-10-25 14:55:52 +02001027lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format)
1028{
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001029 return lys_parse_path_(ctx, path, format, 1, NULL, NULL);
Radek Krejcid33273d2018-10-25 14:55:52 +02001030}
1031
1032API LY_ERR
1033lys_search_localfile(const char * const *searchpaths, int cwd, const char *name, const char *revision,
1034 char **localfile, LYS_INFORMAT *format)
1035{
1036 size_t len, flen, match_len = 0, dir_len;
1037 int i, implicit_cwd = 0, ret = EXIT_FAILURE;
1038 char *wd, *wn = NULL;
1039 DIR *dir = NULL;
1040 struct dirent *file;
1041 char *match_name = NULL;
1042 LYS_INFORMAT format_aux, match_format = 0;
1043 struct ly_set *dirs;
1044 struct stat st;
1045
1046 LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL);
1047
1048 /* start to fill the dir fifo with the context's search path (if set)
1049 * and the current working directory */
1050 dirs = ly_set_new();
1051 if (!dirs) {
1052 LOGMEM(NULL);
1053 return EXIT_FAILURE;
1054 }
1055
1056 len = strlen(name);
1057 if (cwd) {
1058 wd = get_current_dir_name();
1059 if (!wd) {
1060 LOGMEM(NULL);
1061 goto cleanup;
1062 } else {
1063 /* add implicit current working directory (./) to be searched,
1064 * this directory is not searched recursively */
1065 if (ly_set_add(dirs, wd, 0) == -1) {
1066 goto cleanup;
1067 }
1068 implicit_cwd = 1;
1069 }
1070 }
1071 if (searchpaths) {
1072 for (i = 0; searchpaths[i]; i++) {
1073 /* check for duplicities with the implicit current working directory */
1074 if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) {
1075 implicit_cwd = 0;
1076 continue;
1077 }
1078 wd = strdup(searchpaths[i]);
1079 if (!wd) {
1080 LOGMEM(NULL);
1081 goto cleanup;
1082 } else if (ly_set_add(dirs, wd, 0) == -1) {
1083 goto cleanup;
1084 }
1085 }
1086 }
1087 wd = NULL;
1088
1089 /* start searching */
1090 while (dirs->count) {
1091 free(wd);
1092 free(wn); wn = NULL;
1093
1094 dirs->count--;
1095 wd = (char *)dirs->objs[dirs->count];
1096 dirs->objs[dirs->count] = NULL;
1097 LOGVRB("Searching for \"%s\" in %s.", name, wd);
1098
1099 if (dir) {
1100 closedir(dir);
1101 }
1102 dir = opendir(wd);
1103 dir_len = strlen(wd);
1104 if (!dir) {
1105 LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno));
1106 } else {
1107 while ((file = readdir(dir))) {
1108 if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) {
1109 /* skip . and .. */
1110 continue;
1111 }
1112 free(wn);
1113 if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) {
1114 LOGMEM(NULL);
1115 goto cleanup;
1116 }
1117 if (stat(wn, &st) == -1) {
1118 LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)",
1119 file->d_name, wd, strerror(errno));
1120 continue;
1121 }
1122 if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) {
1123 /* we have another subdirectory in searchpath to explore,
1124 * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */
1125 if (ly_set_add(dirs, wn, 0) == -1) {
1126 goto cleanup;
1127 }
1128 /* continue with the next item in current directory */
1129 wn = NULL;
1130 continue;
1131 } else if (!S_ISREG(st.st_mode)) {
1132 /* not a regular file (note that we see the target of symlinks instead of symlinks */
1133 continue;
1134 }
1135
1136 /* here we know that the item is a file which can contain a module */
1137 if (strncmp(name, file->d_name, len) ||
1138 (file->d_name[len] != '.' && file->d_name[len] != '@')) {
1139 /* different filename than the module we search for */
1140 continue;
1141 }
1142
1143 /* get type according to filename suffix */
1144 flen = strlen(file->d_name);
Radek Krejcied5acc52019-04-25 15:57:04 +02001145 if (!strcmp(&file->d_name[flen - 5], ".yang")) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001146 format_aux = LYS_IN_YANG;
Radek Krejcied5acc52019-04-25 15:57:04 +02001147 /* TODO YIN parser
1148 } else if (!strcmp(&file->d_name[flen - 4], ".yin")) {
1149 format_aux = LYS_IN_YIN;
1150 */
Radek Krejcid33273d2018-10-25 14:55:52 +02001151 } else {
1152 /* not supportde suffix/file format */
1153 continue;
1154 }
1155
1156 if (revision) {
1157 /* we look for the specific revision, try to get it from the filename */
1158 if (file->d_name[len] == '@') {
1159 /* check revision from the filename */
1160 if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) {
1161 /* another revision */
1162 continue;
1163 } else {
1164 /* exact revision */
1165 free(match_name);
1166 match_name = wn;
1167 wn = NULL;
1168 match_len = dir_len + 1 + len;
1169 match_format = format_aux;
1170 goto success;
1171 }
1172 } else {
1173 /* continue trying to find exact revision match, use this only if not found */
1174 free(match_name);
1175 match_name = wn;
1176 wn = NULL;
1177 match_len = dir_len + 1 +len;
1178 match_format = format_aux;
1179 continue;
1180 }
1181 } else {
1182 /* remember the revision and try to find the newest one */
1183 if (match_name) {
1184 if (file->d_name[len] != '@' ||
1185 lysp_check_date(NULL, &file->d_name[len + 1], flen - (format_aux == LYS_IN_YANG ? 5 : 4) - len - 1, NULL)) {
1186 continue;
1187 } else if (match_name[match_len] == '@' &&
1188 (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) {
1189 continue;
1190 }
1191 free(match_name);
1192 }
1193
1194 match_name = wn;
1195 wn = NULL;
1196 match_len = dir_len + 1 + len;
1197 match_format = format_aux;
1198 continue;
1199 }
1200 }
1201 }
1202 }
1203
1204success:
1205 (*localfile) = match_name;
1206 match_name = NULL;
1207 if (format) {
1208 (*format) = match_format;
1209 }
1210 ret = EXIT_SUCCESS;
1211
1212cleanup:
1213 free(wn);
1214 free(wd);
1215 if (dir) {
1216 closedir(dir);
1217 }
1218 free(match_name);
1219 ly_set_free(dirs, free);
1220
1221 return ret;
1222}
1223