blob: 80633a3779e4240ddd4d912a110a3f76a7962923 [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
Radek Krejci6eeb58f2019-02-22 16:29:37 +010078 } else if (last->nodetype == 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) {
137 case LYS_ACTION:
138 case LYS_NOTIF:
139 case LYS_LEAF:
140 case LYS_ANYXML:
141 case LYS_ANYDATA:
142 case LYS_LIST:
143 case LYS_LEAFLIST:
Radek Krejcia9026eb2018-12-12 16:04:47 +0100144 case LYS_CASE:
Radek Krejcia3045382018-11-22 14:30:31 +0100145 break;
146 case LYS_CONTAINER:
147 if (!(((struct lysc_node_container *)next)->flags & LYS_PRESENCE) && (options & LYS_GETNEXT_INTONPCONT)) {
148 if (((struct lysc_node_container *)next)->child) {
149 /* go into */
150 next = ((struct lysc_node_container *)next)->child;
151 } else {
152 next = next->next;
153 }
154 goto repeat;
155 }
156 break;
157 case LYS_CHOICE:
158 if (options & LYS_GETNEXT_WITHCHOICE) {
159 return next;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100160 } else if ((options & LYS_GETNEXT_NOCHOICE) || !((struct lysc_node_choice *)next)->cases) {
161 next = next->next;
162 } else {
Radek Krejcia3045382018-11-22 14:30:31 +0100163 /* go into */
Radek Krejcia9026eb2018-12-12 16:04:47 +0100164 if (options & LYS_GETNEXT_WITHCASE) {
Radek Krejci05b774b2019-02-25 13:26:18 +0100165 next = (struct lysc_node*)((struct lysc_node_choice *)next)->cases;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100166 } else {
167 next = ((struct lysc_node_choice *)next)->cases->child;
168 }
Radek Krejcia3045382018-11-22 14:30:31 +0100169 }
170 goto repeat;
171 default:
172 /* we should not be here */
Radek Krejcib07b5c92019-04-08 10:56:37 +0200173 LOGINT(module ? module->mod->ctx : parent->module->ctx);
Radek Krejcia3045382018-11-22 14:30:31 +0100174 return NULL;
175 }
176
177 if (!(options & LYS_GETNEXT_NOSTATECHECK)) {
178 /* check if the node is disabled by if-feature */
Radek Krejcifab954b2019-09-11 11:25:14 +0200179 if (lysc_node_is_disabled(next, 0)) {
Radek Krejcia3045382018-11-22 14:30:31 +0100180 next = next->next;
181 goto repeat;
182 }
183 }
184 return next;
185}
186
187API const struct lysc_node *
Michal Vaskoe444f752020-02-10 12:20:06 +0100188lys_find_child(const struct lysc_node *parent, const struct lys_module *module, const char *name, size_t name_len,
189 uint16_t nodetype, int options)
Radek Krejcia3045382018-11-22 14:30:31 +0100190{
191 const struct lysc_node *node = NULL;
192
193 LY_CHECK_ARG_RET(NULL, module, name, NULL);
194 if (!nodetype) {
195 nodetype = 0xffff;
196 }
197
198 while ((node = lys_getnext(node, parent, module->compiled, options))) {
199 if (!(node->nodetype & nodetype)) {
200 continue;
201 }
202 if (node->module != module) {
203 continue;
204 }
205
206 if (name_len) {
Radek Krejci7f9b6512019-09-18 13:11:09 +0200207 if (!ly_strncmp(node->name, name, name_len)) {
Radek Krejcia3045382018-11-22 14:30:31 +0100208 return node;
209 }
210 } else {
211 if (!strcmp(node->name, name)) {
212 return node;
213 }
214 }
215 }
216 return NULL;
217}
218
Michal Vasko14654712020-02-06 08:35:21 +0100219char *
220lysc_path_until(const struct lysc_node *node, const struct lysc_node *parent, LYSC_PATH_TYPE pathtype, char *buffer,
221 size_t buflen)
Radek Krejci327de162019-06-14 12:52:07 +0200222{
Michal Vasko03ff5a72019-09-11 13:49:33 +0200223 const struct lysc_node *iter;
Radek Krejci327de162019-06-14 12:52:07 +0200224 char *path = NULL;
225 int len = 0;
226
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200227 LY_CHECK_ARG_RET(NULL, node, NULL);
228 if (buffer) {
229 LY_CHECK_ARG_RET(node->module->ctx, buflen > 1, NULL);
230 }
231
Radek Krejci327de162019-06-14 12:52:07 +0200232 switch (pathtype) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200233 case LYSC_PATH_LOG:
Michal Vasko90932a92020-02-12 14:33:03 +0100234 for (iter = node; iter && (iter != parent) && (len >= 0); iter = iter->parent) {
Radek Krejci1c0c3442019-07-23 16:08:47 +0200235 char *s = buffer ? strdup(buffer) : path;
Radek Krejci327de162019-06-14 12:52:07 +0200236 char *id;
Michal Vasko14654712020-02-06 08:35:21 +0100237 const char *slash;
Radek Krejci327de162019-06-14 12:52:07 +0200238
Michal Vasko03ff5a72019-09-11 13:49:33 +0200239 id = strdup(iter->name);
Michal Vasko14654712020-02-06 08:35:21 +0100240 if (parent && (iter->parent == parent)) {
241 slash = "";
242 } else {
243 slash = "/";
244 }
Radek Krejci327de162019-06-14 12:52:07 +0200245 if (!iter->parent || iter->parent->module != iter->module) {
246 /* print prefix */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200247 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100248 len = snprintf(buffer, buflen, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200249 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100250 len = asprintf(&path, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200251 }
Radek Krejci327de162019-06-14 12:52:07 +0200252 } else {
253 /* prefix is the same as in parent */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200254 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100255 len = snprintf(buffer, buflen, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200256 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100257 len = asprintf(&path, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200258 }
Radek Krejci327de162019-06-14 12:52:07 +0200259 }
260 free(s);
261 free(id);
Radek Krejci1c0c3442019-07-23 16:08:47 +0200262
263 if (buffer && buflen <= (size_t)len) {
264 /* not enough space in buffer */
265 break;
266 }
Radek Krejci327de162019-06-14 12:52:07 +0200267 }
268
269 if (len < 0) {
270 free(path);
271 path = NULL;
272 } else if (len == 0) {
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200273 if (buffer) {
274 strcpy(buffer, "/");
275 } else {
276 path = strdup("/");
277 }
Radek Krejci327de162019-06-14 12:52:07 +0200278 }
279 break;
280 }
281
Radek Krejci1c0c3442019-07-23 16:08:47 +0200282 if (buffer) {
283 return buffer;
284 } else {
285 return path;
286 }
Radek Krejci327de162019-06-14 12:52:07 +0200287}
288
Michal Vasko14654712020-02-06 08:35:21 +0100289API char *
290lysc_path(const struct lysc_node *node, LYSC_PATH_TYPE pathtype, char *buffer, size_t buflen)
291{
292 return lysc_path_until(node, NULL, pathtype, buffer, buflen);
293}
294
Radek Krejci19a96102018-11-15 13:38:09 +0100295API int
296lysc_feature_value(const struct lysc_feature *feature)
Radek Krejci6f7feb62018-10-12 15:23:02 +0200297{
Radek Krejci19a96102018-11-15 13:38:09 +0100298 LY_CHECK_ARG_RET(NULL, feature, -1);
299 return feature->flags & LYS_FENABLED ? 1 : 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200300}
301
Radek Krejci693262f2019-04-29 15:23:20 +0200302uint8_t
303lysc_iff_getop(uint8_t *list, int pos)
Radek Krejci151a5b72018-10-19 14:21:44 +0200304{
305 uint8_t *item;
306 uint8_t mask = 3, result;
307
308 assert(pos >= 0);
309
310 item = &list[pos / 4];
311 result = (*item) & (mask << 2 * (pos % 4));
312 return result >> 2 * (pos % 4);
313}
314
Radek Krejci151a5b72018-10-19 14:21:44 +0200315static int
316lysc_iffeature_value_(const struct lysc_iffeature *iff, int *index_e, int *index_f)
317{
318 uint8_t op;
319 int a, b;
320
Radek Krejci693262f2019-04-29 15:23:20 +0200321 op = lysc_iff_getop(iff->expr, *index_e);
Radek Krejci151a5b72018-10-19 14:21:44 +0200322 (*index_e)++;
323
324 switch (op) {
325 case LYS_IFF_F:
326 /* resolve feature */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200327 return lysc_feature_value(iff->features[(*index_f)++]);
Radek Krejci151a5b72018-10-19 14:21:44 +0200328 case LYS_IFF_NOT:
329 /* invert result */
330 return lysc_iffeature_value_(iff, index_e, index_f) ? 0 : 1;
331 case LYS_IFF_AND:
332 case LYS_IFF_OR:
333 a = lysc_iffeature_value_(iff, index_e, index_f);
334 b = lysc_iffeature_value_(iff, index_e, index_f);
335 if (op == LYS_IFF_AND) {
336 return a && b;
337 } else { /* LYS_IFF_OR */
338 return a || b;
339 }
340 }
341
342 return 0;
343}
344
345API int
346lysc_iffeature_value(const struct lysc_iffeature *iff)
347{
348 int index_e = 0, index_f = 0;
349
350 LY_CHECK_ARG_RET(NULL, iff, -1);
351
352 if (iff->expr) {
353 return lysc_iffeature_value_(iff, &index_e, &index_f);
354 }
355 return 0;
356}
357
Radek Krejci151a5b72018-10-19 14:21:44 +0200358/**
359 * @brief Enable/Disable the specified feature in the module.
360 *
361 * If the feature is already set to the desired value, LY_SUCCESS is returned.
362 * By changing the feature, also all the feature which depends on it via their
363 * if-feature statements are again evaluated (disabled if a if-feature statemen
364 * evaluates to false).
365 *
Radek Krejci0af46292019-01-11 16:02:31 +0100366 * @param[in] mod Module where to set (search for) the feature.
Radek Krejci151a5b72018-10-19 14:21:44 +0200367 * @param[in] name Name of the feature to set. Asterisk ('*') can be used to
368 * set all the features in the module.
369 * @param[in] value Desired value of the feature: 1 (enable) or 0 (disable).
370 * @return LY_ERR value.
371 */
372static LY_ERR
Radek Krejci0af46292019-01-11 16:02:31 +0100373lys_feature_change(const struct lys_module *mod, const char *name, int value)
Radek Krejci151a5b72018-10-19 14:21:44 +0200374{
375 int all = 0;
Radek Krejcica3db002018-11-01 10:31:01 +0100376 unsigned int u, changed_count, disabled_count;
Radek Krejci151a5b72018-10-19 14:21:44 +0200377 struct lysc_feature *f, **df;
378 struct lysc_iffeature *iff;
379 struct ly_set *changed;
Radek Krejci0af46292019-01-11 16:02:31 +0100380 struct ly_ctx *ctx = mod->ctx; /* shortcut */
Radek Krejci151a5b72018-10-19 14:21:44 +0200381
Radek Krejci6e67c402019-05-02 09:55:39 +0200382 if (!strcmp(name, "*")) {
383 /* enable all */
384 all = 1;
385 }
386
Radek Krejci0af46292019-01-11 16:02:31 +0100387 if (!mod->compiled) {
388 LOGERR(ctx, LY_EINVAL, "Module \"%s\" is not implemented so all its features are permanently disabled without a chance to change it.",
389 mod->name);
390 return LY_EINVAL;
391 }
392 if (!mod->compiled->features) {
Radek Krejci6e67c402019-05-02 09:55:39 +0200393 if (all) {
394 /* no feature to enable */
395 return LY_SUCCESS;
396 }
Radek Krejci0af46292019-01-11 16:02:31 +0100397 LOGERR(ctx, LY_EINVAL, "Unable to switch feature since the module \"%s\" has no features.", mod->name);
Radek Krejci151a5b72018-10-19 14:21:44 +0200398 return LY_EINVAL;
399 }
400
Radek Krejci151a5b72018-10-19 14:21:44 +0200401 changed = ly_set_new();
Radek Krejcica3db002018-11-01 10:31:01 +0100402 changed_count = 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200403
Radek Krejcica3db002018-11-01 10:31:01 +0100404run:
Radek Krejci0af46292019-01-11 16:02:31 +0100405 for (disabled_count = u = 0; u < LY_ARRAY_SIZE(mod->compiled->features); ++u) {
406 f = &mod->compiled->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200407 if (all || !strcmp(f->name, name)) {
408 if ((value && (f->flags & LYS_FENABLED)) || (!value && !(f->flags & LYS_FENABLED))) {
409 if (all) {
410 /* skip already set features */
411 continue;
412 } else {
413 /* feature already set correctly */
414 ly_set_free(changed, NULL);
415 return LY_SUCCESS;
416 }
417 }
418
419 if (value) { /* enable */
420 /* check referenced features if they are enabled */
421 LY_ARRAY_FOR(f->iffeatures, struct lysc_iffeature, iff) {
422 if (!lysc_iffeature_value(iff)) {
423 if (all) {
Radek Krejcica3db002018-11-01 10:31:01 +0100424 ++disabled_count;
Radek Krejci151a5b72018-10-19 14:21:44 +0200425 goto next;
426 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100427 LOGERR(ctx, LY_EDENIED,
Radek Krejci151a5b72018-10-19 14:21:44 +0200428 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
429 f->name);
430 ly_set_free(changed, NULL);
431 return LY_EDENIED;
432 }
433 }
434 }
435 /* enable the feature */
436 f->flags |= LYS_FENABLED;
437 } else { /* disable */
438 /* disable the feature */
439 f->flags &= ~LYS_FENABLED;
440 }
441
442 /* remember the changed feature */
443 ly_set_add(changed, f, LY_SET_OPT_USEASLIST);
444
445 if (!all) {
446 /* stop in case changing a single feature */
447 break;
448 }
449 }
450next:
451 ;
452 }
453
454 if (!all && !changed->count) {
Radek Krejci0af46292019-01-11 16:02:31 +0100455 LOGERR(ctx, LY_EINVAL, "Feature \"%s\" not found in module \"%s\".", name, mod->name);
Radek Krejci151a5b72018-10-19 14:21:44 +0200456 ly_set_free(changed, NULL);
457 return LY_EINVAL;
458 }
459
Radek Krejcica3db002018-11-01 10:31:01 +0100460 if (value && all && disabled_count) {
461 if (changed_count == changed->count) {
462 /* no change in last run -> not able to enable all ... */
463 /* ... print errors */
Radek Krejci0af46292019-01-11 16:02:31 +0100464 for (u = 0; disabled_count && u < LY_ARRAY_SIZE(mod->compiled->features); ++u) {
465 if (!(mod->compiled->features[u].flags & LYS_FENABLED)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100466 LOGERR(ctx, LY_EDENIED,
Radek Krejcica3db002018-11-01 10:31:01 +0100467 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
Radek Krejci0af46292019-01-11 16:02:31 +0100468 mod->compiled->features[u].name);
Radek Krejcica3db002018-11-01 10:31:01 +0100469 --disabled_count;
470 }
471 }
472 /* ... restore the original state */
473 for (u = 0; u < changed->count; ++u) {
474 f = changed->objs[u];
475 /* re-disable the feature */
476 f->flags &= ~LYS_FENABLED;
477 }
478
479 ly_set_free(changed, NULL);
480 return LY_EDENIED;
481 } else {
482 /* we did some change in last run, try it again */
483 changed_count = changed->count;
484 goto run;
485 }
486 }
487
Radek Krejci151a5b72018-10-19 14:21:44 +0200488 /* reflect change(s) in the dependent features */
489 for (u = 0; u < changed->count; ++u) {
490 /* If a dependent feature is enabled, it can be now changed by the change (to false) of the value of
491 * its if-feature statements. The reverse logic, automatically enable feature when its feature is enabled
492 * is not done - by default, features are disabled and must be explicitely enabled. */
493 f = changed->objs[u];
494 LY_ARRAY_FOR(f->depfeatures, struct lysc_feature*, df) {
495 if (!((*df)->flags & LYS_FENABLED)) {
496 /* not enabled, nothing to do */
497 continue;
498 }
499 /* check the feature's if-features which could change by the previous change of our feature */
500 LY_ARRAY_FOR((*df)->iffeatures, struct lysc_iffeature, iff) {
501 if (!lysc_iffeature_value(iff)) {
502 /* the feature must be disabled now */
503 (*df)->flags &= ~LYS_FENABLED;
504 /* add the feature into the list of changed features */
505 ly_set_add(changed, *df, LY_SET_OPT_USEASLIST);
506 break;
507 }
508 }
509 }
510 }
511
512 ly_set_free(changed, NULL);
513 return LY_SUCCESS;
514}
515
516API LY_ERR
Radek Krejcied5acc52019-04-25 15:57:04 +0200517lys_feature_enable(const struct lys_module *module, const char *feature)
Radek Krejci151a5b72018-10-19 14:21:44 +0200518{
Radek Krejci0af46292019-01-11 16:02:31 +0100519 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200520
Radek Krejcied5acc52019-04-25 15:57:04 +0200521 return lys_feature_change((struct lys_module*)module, feature, 1);
Radek Krejci151a5b72018-10-19 14:21:44 +0200522}
523
524API LY_ERR
Radek Krejcied5acc52019-04-25 15:57:04 +0200525lys_feature_disable(const struct lys_module *module, const char *feature)
Radek Krejci151a5b72018-10-19 14:21:44 +0200526{
Radek Krejci0af46292019-01-11 16:02:31 +0100527 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200528
Radek Krejcied5acc52019-04-25 15:57:04 +0200529 return lys_feature_change((struct lys_module*)module, feature, 0);
Radek Krejci151a5b72018-10-19 14:21:44 +0200530}
531
532API int
533lys_feature_value(const struct lys_module *module, const char *feature)
534{
535 struct lysc_feature *f;
536 struct lysc_module *mod;
537 unsigned int u;
538
539 LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, -1);
540 mod = module->compiled;
541
542 /* search for the specified feature */
543 for (u = 0; u < LY_ARRAY_SIZE(mod->features); ++u) {
Radek Krejci2c4e7172018-10-19 15:56:26 +0200544 f = &mod->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200545 if (!strcmp(f->name, feature)) {
546 if (f->flags & LYS_FENABLED) {
547 return 1;
548 } else {
549 return 0;
550 }
551 }
552 }
553
554 /* feature definition not found */
555 return -1;
556}
557
Radek Krejcia3045382018-11-22 14:30:31 +0100558API const struct lysc_iffeature *
Radek Krejcifab954b2019-09-11 11:25:14 +0200559lysc_node_is_disabled(const struct lysc_node *node, int recursive)
Radek Krejcia3045382018-11-22 14:30:31 +0100560{
561 unsigned int u;
Radek Krejcia3045382018-11-22 14:30:31 +0100562
563 LY_CHECK_ARG_RET(NULL, node, NULL);
564
565 while(node) {
566 if (node->nodetype & LYS_CHOICE) {
567 return NULL;
568 }
569
Radek Krejci056d0a82018-12-06 16:57:25 +0100570 if (node->iffeatures) {
Radek Krejcia3045382018-11-22 14:30:31 +0100571 /* check local if-features */
Radek Krejci056d0a82018-12-06 16:57:25 +0100572 LY_ARRAY_FOR(node->iffeatures, u) {
573 if (!lysc_iffeature_value(&node->iffeatures[u])) {
574 return &node->iffeatures[u];
Radek Krejcia3045382018-11-22 14:30:31 +0100575 }
576 }
577 }
578
579 if (!recursive) {
580 return NULL;
581 }
582
583 /* go through parents */
584 node = node->parent;
585 }
586 return NULL;
587}
588
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200589LY_ERR
590lys_set_implemented_internal(struct lys_module *mod, uint8_t value)
591{
592 struct lys_module *m;
593
594 LY_CHECK_ARG_RET(NULL, mod, LY_EINVAL);
595
596 if (mod->implemented) {
597 return LY_SUCCESS;
598 }
599
600 /* we have module from the current context */
601 m = ly_ctx_get_module_implemented(mod->ctx, mod->name);
602 if (m) {
603 if (m != mod) {
604 /* check collision with other implemented revision */
605 LOGERR(mod->ctx, LY_EDENIED, "Module \"%s\" is present in the context in other implemented revision (%s).",
606 mod->name, mod->revision ? mod->revision : "module without revision");
607 return LY_EDENIED;
608 } else {
609 /* mod is already implemented */
610 return LY_SUCCESS;
611 }
612 }
613
614 /* mark the module implemented, check for collision was already done */
615 mod->implemented = value;
616
617 /* compile the schema */
618 LY_CHECK_RET(lys_compile(mod, LYSC_OPT_INTERNAL));
619
620 return LY_SUCCESS;
621}
622
623API LY_ERR
624lys_set_implemented(struct lys_module *mod)
625{
626 return lys_set_implemented_internal(mod, 1);
627}
628
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100629struct lysp_submodule *
Radek Krejcie7b95092019-05-15 11:03:07 +0200630lys_parse_mem_submodule(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, struct lys_parser_ctx *main_ctx,
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100631 LY_ERR (*custom_check)(struct ly_ctx*, struct lysp_module*, struct lysp_submodule*, void*), void *check_data)
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200632{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100633 LY_ERR ret = LY_EINVAL;
634 struct lysp_submodule *submod = NULL, *latest_sp;
David Sedlák1b623122019-08-05 15:27:49 +0200635 struct lys_parser_ctx *context = NULL;
636 struct yin_parser_ctx *yin_context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100637
638 LY_CHECK_ARG_RET(ctx, ctx, data, NULL);
639
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100640 switch (format) {
641 case LYS_IN_YIN:
David Sedlák1b623122019-08-05 15:27:49 +0200642 ret = yin_parse_submodule(&yin_context, ctx, main_ctx, data, &submod);
643 context = (struct lys_parser_ctx *)yin_context;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100644 break;
645 case LYS_IN_YANG:
David Sedlák1b623122019-08-05 15:27:49 +0200646 ret = yang_parse_submodule(&context, ctx, main_ctx, data, &submod);
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 */
David Sedlák1b623122019-08-05 15:27:49 +0200658 latest_sp = ly_ctx_get_submodule((*context).ctx, 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) {
679 LY_CHECK_GOTO(custom_check((*context).ctx, NULL, submod, check_data), error);
680 }
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 */
David Sedlák1b623122019-08-05 15:27:49 +0200687 memcpy(&main_ctx->tpdfs_nodes, &(*context).tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
688 memcpy(&main_ctx->grps_nodes, &(*context).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) {
691 lys_parser_ctx_free(context);
692 } else {
693 yin_parser_ctx_free(yin_context);
694 }
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) {
700 lys_parser_ctx_free(context);
701 } else {
702 yin_parser_ctx_free(yin_context);
703 }
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,
709 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
710 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;
David Sedlák1b623122019-08-05 15:27:49 +0200717 struct lys_parser_ctx *context = NULL;
718 struct yin_parser_ctx *yin_context = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +0200719
720 LY_CHECK_ARG_RET(ctx, ctx, data, NULL);
721
722 mod = calloc(1, sizeof *mod);
723 LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100724 mod->ctx = ctx;
Radek Krejci86d106e2018-10-18 09:53:19 +0200725
726 switch (format) {
727 case LYS_IN_YIN:
David Sedlák1b623122019-08-05 15:27:49 +0200728 ret = yin_parse_module(&yin_context, data, mod);
729 context = (struct lys_parser_ctx *)yin_context;
Radek Krejci86d106e2018-10-18 09:53:19 +0200730 break;
731 case LYS_IN_YANG:
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100732 ret = yang_parse_module(&context, data, mod);
Radek Krejci86d106e2018-10-18 09:53:19 +0200733 break;
734 default:
735 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
736 break;
737 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100738 LY_CHECK_ERR_RET(ret, lys_module_free(mod, NULL), NULL);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200739
740 /* make sure that the newest revision is at position 0 */
741 lysp_sort_revisions(mod->parsed->revs);
Radek Krejci0af46292019-01-11 16:02:31 +0100742 if (mod->parsed->revs) {
743 mod->revision = lydict_insert(ctx, mod->parsed->revs[0].date, 0);
744 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200745
Radek Krejcib3289d62019-09-18 12:21:39 +0200746 /* decide the latest revision */
747 latest = (struct lys_module*)ly_ctx_get_module_latest(ctx, mod->name);
748 if (latest) {
749 if (mod->revision) {
750 if (!latest->revision) {
751 /* latest has no revision, so mod is anyway newer */
752 mod->latest_revision = latest->latest_revision;
753 /* the latest is zeroed later when the new module is being inserted into the context */
754 } else if (strcmp(mod->revision, latest->revision) > 0) {
755 mod->latest_revision = latest->latest_revision;
756 /* the latest is zeroed later when the new module is being inserted into the context */
757 } else {
758 latest = NULL;
759 }
760 } else {
761 latest = NULL;
762 }
763 } else {
764 mod->latest_revision = 1;
765 }
766
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100767 if (custom_check) {
768 LY_CHECK_GOTO(custom_check(ctx, mod->parsed, NULL, check_data), error);
769 }
770
Radek Krejci86d106e2018-10-18 09:53:19 +0200771 if (implement) {
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200772 /* mark the loaded module implemented */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100773 if (ly_ctx_get_module_implemented(ctx, mod->name)) {
774 LOGERR(ctx, LY_EDENIED, "Module \"%s\" is already implemented in the context.", mod->name);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100775 goto error;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200776 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100777 mod->implemented = 1;
Radek Krejci86d106e2018-10-18 09:53:19 +0200778 }
779
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100780 /* check for duplicity in the context */
Radek Krejci0af46292019-01-11 16:02:31 +0100781 mod_dup = (struct lys_module*)ly_ctx_get_module(ctx, mod->name, mod->revision);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100782 if (mod_dup) {
783 if (mod_dup->parsed) {
784 /* error */
Radek Krejcid33273d2018-10-25 14:55:52 +0200785 if (mod->parsed->revs) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100786 LOGERR(ctx, LY_EEXIST, "Module \"%s\" of revision \"%s\" is already present in the context.",
787 mod->name, mod->parsed->revs[0].date);
Radek Krejcid33273d2018-10-25 14:55:52 +0200788 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100789 LOGERR(ctx, LY_EEXIST, "Module \"%s\" with no revision is already present in the context.",
790 mod->name);
Radek Krejcid33273d2018-10-25 14:55:52 +0200791 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100792 goto error;
793 } else {
794 /* add the parsed data to the currently compiled-only module in the context */
795 mod_dup->parsed = mod->parsed;
796 mod_dup->parsed->mod = mod_dup;
797 mod->parsed = NULL;
798 lys_module_free(mod, NULL);
799 mod = mod_dup;
800 goto finish_parsing;
Radek Krejcid33273d2018-10-25 14:55:52 +0200801 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100802 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200803
804#if 0
805 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
806 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
807 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
808 * the anotation definitions available in the internal schema structure. There is another hack in schema
809 * printers to do not print this internally added annotation. */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100810 if (ly_strequal(mod->name, "ietf-netconf", 0)) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200811 if (lyp_add_ietf_netconf_annotations(mod)) {
812 lys_free(mod, NULL, 1, 1);
813 return NULL;
814 }
815 }
816#endif
817
Radek Krejci0af46292019-01-11 16:02:31 +0100818 if (!mod->implemented) {
Radek Krejci0935f412019-08-20 16:15:18 +0200819 /* pre-compile features and extension definitions of the module */
Radek Krejci327de162019-06-14 12:52:07 +0200820 LY_CHECK_GOTO(lys_feature_precompile(NULL, ctx, mod, mod->parsed->features, &mod->off_features), error);
Radek Krejci0935f412019-08-20 16:15:18 +0200821 LY_CHECK_GOTO(lys_extension_precompile(NULL, ctx, mod, mod->parsed->extensions, &mod->off_extensions), error);
Radek Krejci0af46292019-01-11 16:02:31 +0100822 }
823
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100824 if (latest) {
Radek Krejcib3289d62019-09-18 12:21:39 +0200825 latest->latest_revision = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100826 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200827
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100828 /* add into context */
829 ly_set_add(&ctx->list, mod, LY_SET_OPT_USEASLIST);
Radek Krejcid33273d2018-10-25 14:55:52 +0200830
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100831finish_parsing:
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100832 /* resolve imports */
833 mod->parsed->parsing = 1;
834 LY_ARRAY_FOR(mod->parsed->imports, u) {
835 imp = &mod->parsed->imports[u];
836 if (!imp->module && lysp_load_module(ctx, imp->name, imp->rev[0] ? imp->rev : NULL, 0, 0, &imp->module)) {
837 goto error_ctx;
Radek Krejci086c7132018-10-26 15:29:04 +0200838 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100839 /* check for importing the same module twice */
840 for (i = 0; i < u; ++i) {
841 if (imp->module == mod->parsed->imports[i].module) {
842 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 +0100843 goto error_ctx;
Radek Krejci086c7132018-10-26 15:29:04 +0200844 }
845 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200846 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100847 LY_ARRAY_FOR(mod->parsed->includes, u) {
848 inc = &mod->parsed->includes[u];
David Sedlák1b623122019-08-05 15:27:49 +0200849 if (!inc->submodule && lysp_load_submodule(context, mod->parsed, inc)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100850 goto error_ctx;
851 }
Radek Krejci0af46292019-01-11 16:02:31 +0100852 if (!mod->implemented) {
Radek Krejci0935f412019-08-20 16:15:18 +0200853 /* pre-compile features and extension definitions of the module */
Radek Krejci327de162019-06-14 12:52:07 +0200854 LY_CHECK_GOTO(lys_feature_precompile(NULL, ctx, mod, inc->submodule->features, &mod->off_features), error);
Radek Krejci0935f412019-08-20 16:15:18 +0200855 LY_CHECK_GOTO(lys_extension_precompile(NULL, ctx, mod, mod->parsed->extensions, &mod->off_extensions), error);
Radek Krejci0af46292019-01-11 16:02:31 +0100856 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100857 }
858 mod->parsed->parsing = 0;
859
Radek Krejci7fc68292019-06-12 13:51:09 +0200860 /* check name collisions - typedefs and TODO groupings */
David Sedlák1b623122019-08-05 15:27:49 +0200861 LY_CHECK_GOTO(lysp_check_typedefs(context, mod->parsed), error_ctx);
Radek Krejcid33273d2018-10-25 14:55:52 +0200862
David Sedlák1b623122019-08-05 15:27:49 +0200863 if (format == LYS_IN_YANG) {
864 lys_parser_ctx_free(context);
865 } else {
866 yin_parser_ctx_free(yin_context);
867 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200868 return mod;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100869
870error_ctx:
871 ly_set_rm(&ctx->list, mod, NULL);
872error:
873 lys_module_free(mod, NULL);
David Sedlák1b623122019-08-05 15:27:49 +0200874 ly_set_erase(&context->tpdfs_nodes, NULL);
875 if (format == LYS_IN_YANG) {
876 lys_parser_ctx_free(context);
877 } else {
878 yin_parser_ctx_free(yin_context);
879 }
880
Radek Krejcibbe09a92018-11-08 09:36:54 +0100881 return NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +0200882}
883
Radek Krejcid14e9692018-11-01 11:00:37 +0100884API struct lys_module *
Radek Krejci86d106e2018-10-18 09:53:19 +0200885lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format)
886{
Radek Krejci096235c2019-01-11 11:12:19 +0100887 struct lys_module *mod;
888
889 mod = lys_parse_mem_module(ctx, data, format, 1, NULL, NULL);
890 LY_CHECK_RET(!mod, NULL);
891
892 if (lys_compile(mod, 0)) {
893 ly_set_rm(&ctx->list, mod, NULL);
894 lys_module_free(mod, NULL);
895 return NULL;
896 }
897 return mod;
Radek Krejci86d106e2018-10-18 09:53:19 +0200898}
899
900static void
901lys_parse_set_filename(struct ly_ctx *ctx, const char **filename, int fd)
902{
Radek Krejci65639b92018-11-27 10:51:37 +0100903 char path[PATH_MAX];
Radek Krejci86d106e2018-10-18 09:53:19 +0200904
905#ifdef __APPLE__
906 if (fcntl(fd, F_GETPATH, path) != -1) {
907 *filename = lydict_insert(ctx, path, 0);
908 }
909#else
Radek Krejci65639b92018-11-27 10:51:37 +0100910 int len;
911 char proc_path[32];
912
Radek Krejci86d106e2018-10-18 09:53:19 +0200913 /* get URI if there is /proc */
914 sprintf(proc_path, "/proc/self/fd/%d", fd);
915 if ((len = readlink(proc_path, path, PATH_MAX - 1)) > 0) {
916 *filename = lydict_insert(ctx, path, len);
917 }
918#endif
919}
920
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100921void *
Radek Krejcie7b95092019-05-15 11:03:07 +0200922lys_parse_fd_(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, int implement, struct lys_parser_ctx *main_ctx,
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100923 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
924 void *check_data)
Radek Krejci86d106e2018-10-18 09:53:19 +0200925{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100926 void *result;
927 struct lys_module *mod = NULL;
928 struct lysp_submodule *submod = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +0200929 size_t length;
930 char *addr;
931
932 LY_CHECK_ARG_RET(ctx, ctx, NULL);
933 if (fd < 0) {
934 LOGARG(ctx, fd);
935 return NULL;
936 }
937
938 LY_CHECK_RET(ly_mmap(ctx, fd, &length, (void **)&addr), NULL);
939 if (!addr) {
940 LOGERR(ctx, LY_EINVAL, "Empty schema file.");
941 return NULL;
942 }
943
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100944 if (main_ctx) {
945 result = submod = lys_parse_mem_submodule(ctx, addr, format, main_ctx, custom_check, check_data);
946 } else {
947 result = mod = lys_parse_mem_module(ctx, addr, format, implement, custom_check, check_data);
Radek Krejci096235c2019-01-11 11:12:19 +0100948 if (mod && implement && lys_compile(mod, 0)) {
949 ly_set_rm(&ctx->list, mod, NULL);
950 lys_module_free(mod, NULL);
951 result = mod = NULL;
952 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100953 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200954 ly_munmap(addr, length);
955
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100956 if (mod && !mod->filepath) {
957 lys_parse_set_filename(ctx, &mod->filepath, fd);
958 } else if (submod && !submod->filepath) {
959 lys_parse_set_filename(ctx, &submod->filepath, fd);
Radek Krejci86d106e2018-10-18 09:53:19 +0200960 }
961
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100962 return result;
963}
964
965struct lys_module *
966lys_parse_fd_module(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, int implement,
967 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
968 void *check_data)
969{
970 return (struct lys_module*)lys_parse_fd_(ctx, fd, format, implement, NULL, custom_check, check_data);
971}
972
973struct lysp_submodule *
Radek Krejcie7b95092019-05-15 11:03:07 +0200974lys_parse_fd_submodule(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, struct lys_parser_ctx *main_ctx,
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100975 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
976 void *check_data)
977{
978 assert(main_ctx);
979 return (struct lysp_submodule*)lys_parse_fd_(ctx, fd, format, 0, main_ctx, custom_check, check_data);
Radek Krejci86d106e2018-10-18 09:53:19 +0200980}
981
Radek Krejcid14e9692018-11-01 11:00:37 +0100982API struct lys_module *
Radek Krejci86d106e2018-10-18 09:53:19 +0200983lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format)
984{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100985 return lys_parse_fd_module(ctx, fd, format, 1, NULL, NULL);
Radek Krejci86d106e2018-10-18 09:53:19 +0200986}
987
Radek Krejcid33273d2018-10-25 14:55:52 +0200988struct lys_module *
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100989lys_parse_path_(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, int implement,
990 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data), void *check_data)
Radek Krejci86d106e2018-10-18 09:53:19 +0200991{
992 int fd;
Radek Krejcid33273d2018-10-25 14:55:52 +0200993 struct lys_module *mod;
Radek Krejci86d106e2018-10-18 09:53:19 +0200994 const char *rev, *dot, *filename;
995 size_t len;
996
997 LY_CHECK_ARG_RET(ctx, ctx, path, NULL);
998
999 fd = open(path, O_RDONLY);
1000 LY_CHECK_ERR_RET(fd == -1, LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)), NULL);
1001
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001002 mod = lys_parse_fd_module(ctx, fd, format, implement, custom_check, check_data);
Radek Krejci86d106e2018-10-18 09:53:19 +02001003 close(fd);
1004 LY_CHECK_RET(!mod, NULL);
1005
1006 /* check that name and revision match filename */
1007 filename = strrchr(path, '/');
1008 if (!filename) {
1009 filename = path;
1010 } else {
1011 filename++;
1012 }
1013 rev = strchr(filename, '@');
1014 dot = strrchr(filename, '.');
1015
1016 /* name */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001017 len = strlen(mod->name);
1018 if (strncmp(filename, mod->name, len) ||
Radek Krejci86d106e2018-10-18 09:53:19 +02001019 ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001020 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name);
Radek Krejci86d106e2018-10-18 09:53:19 +02001021 }
1022 if (rev) {
1023 len = dot - ++rev;
Radek Krejcib7db73a2018-10-24 14:18:40 +02001024 if (!mod->parsed->revs || len != 10 || strncmp(mod->parsed->revs[0].date, rev, len)) {
Radek Krejci86d106e2018-10-18 09:53:19 +02001025 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Radek Krejcib7db73a2018-10-24 14:18:40 +02001026 mod->parsed->revs ? mod->parsed->revs[0].date : "none");
Radek Krejci86d106e2018-10-18 09:53:19 +02001027 }
1028 }
1029
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001030 if (!mod->filepath) {
Radek Krejci86d106e2018-10-18 09:53:19 +02001031 /* store URI */
1032 char rpath[PATH_MAX];
1033 if (realpath(path, rpath) != NULL) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001034 mod->filepath = lydict_insert(ctx, rpath, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001035 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001036 mod->filepath = lydict_insert(ctx, path, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001037 }
1038 }
1039
1040 return mod;
1041}
1042
Radek Krejcid14e9692018-11-01 11:00:37 +01001043API struct lys_module *
Radek Krejcid33273d2018-10-25 14:55:52 +02001044lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format)
1045{
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001046 return lys_parse_path_(ctx, path, format, 1, NULL, NULL);
Radek Krejcid33273d2018-10-25 14:55:52 +02001047}
1048
1049API LY_ERR
1050lys_search_localfile(const char * const *searchpaths, int cwd, const char *name, const char *revision,
1051 char **localfile, LYS_INFORMAT *format)
1052{
1053 size_t len, flen, match_len = 0, dir_len;
1054 int i, implicit_cwd = 0, ret = EXIT_FAILURE;
1055 char *wd, *wn = NULL;
1056 DIR *dir = NULL;
1057 struct dirent *file;
1058 char *match_name = NULL;
1059 LYS_INFORMAT format_aux, match_format = 0;
1060 struct ly_set *dirs;
1061 struct stat st;
1062
1063 LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL);
1064
1065 /* start to fill the dir fifo with the context's search path (if set)
1066 * and the current working directory */
1067 dirs = ly_set_new();
1068 if (!dirs) {
1069 LOGMEM(NULL);
1070 return EXIT_FAILURE;
1071 }
1072
1073 len = strlen(name);
1074 if (cwd) {
1075 wd = get_current_dir_name();
1076 if (!wd) {
1077 LOGMEM(NULL);
1078 goto cleanup;
1079 } else {
1080 /* add implicit current working directory (./) to be searched,
1081 * this directory is not searched recursively */
1082 if (ly_set_add(dirs, wd, 0) == -1) {
1083 goto cleanup;
1084 }
1085 implicit_cwd = 1;
1086 }
1087 }
1088 if (searchpaths) {
1089 for (i = 0; searchpaths[i]; i++) {
1090 /* check for duplicities with the implicit current working directory */
1091 if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) {
1092 implicit_cwd = 0;
1093 continue;
1094 }
1095 wd = strdup(searchpaths[i]);
1096 if (!wd) {
1097 LOGMEM(NULL);
1098 goto cleanup;
1099 } else if (ly_set_add(dirs, wd, 0) == -1) {
1100 goto cleanup;
1101 }
1102 }
1103 }
1104 wd = NULL;
1105
1106 /* start searching */
1107 while (dirs->count) {
1108 free(wd);
1109 free(wn); wn = NULL;
1110
1111 dirs->count--;
1112 wd = (char *)dirs->objs[dirs->count];
1113 dirs->objs[dirs->count] = NULL;
1114 LOGVRB("Searching for \"%s\" in %s.", name, wd);
1115
1116 if (dir) {
1117 closedir(dir);
1118 }
1119 dir = opendir(wd);
1120 dir_len = strlen(wd);
1121 if (!dir) {
1122 LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno));
1123 } else {
1124 while ((file = readdir(dir))) {
1125 if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) {
1126 /* skip . and .. */
1127 continue;
1128 }
1129 free(wn);
1130 if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) {
1131 LOGMEM(NULL);
1132 goto cleanup;
1133 }
1134 if (stat(wn, &st) == -1) {
1135 LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)",
1136 file->d_name, wd, strerror(errno));
1137 continue;
1138 }
1139 if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) {
1140 /* we have another subdirectory in searchpath to explore,
1141 * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */
1142 if (ly_set_add(dirs, wn, 0) == -1) {
1143 goto cleanup;
1144 }
1145 /* continue with the next item in current directory */
1146 wn = NULL;
1147 continue;
1148 } else if (!S_ISREG(st.st_mode)) {
1149 /* not a regular file (note that we see the target of symlinks instead of symlinks */
1150 continue;
1151 }
1152
1153 /* here we know that the item is a file which can contain a module */
1154 if (strncmp(name, file->d_name, len) ||
1155 (file->d_name[len] != '.' && file->d_name[len] != '@')) {
1156 /* different filename than the module we search for */
1157 continue;
1158 }
1159
1160 /* get type according to filename suffix */
1161 flen = strlen(file->d_name);
Radek Krejcied5acc52019-04-25 15:57:04 +02001162 if (!strcmp(&file->d_name[flen - 5], ".yang")) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001163 format_aux = LYS_IN_YANG;
Radek Krejcied5acc52019-04-25 15:57:04 +02001164 /* TODO YIN parser
1165 } else if (!strcmp(&file->d_name[flen - 4], ".yin")) {
1166 format_aux = LYS_IN_YIN;
1167 */
Radek Krejcid33273d2018-10-25 14:55:52 +02001168 } else {
1169 /* not supportde suffix/file format */
1170 continue;
1171 }
1172
1173 if (revision) {
1174 /* we look for the specific revision, try to get it from the filename */
1175 if (file->d_name[len] == '@') {
1176 /* check revision from the filename */
1177 if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) {
1178 /* another revision */
1179 continue;
1180 } else {
1181 /* exact revision */
1182 free(match_name);
1183 match_name = wn;
1184 wn = NULL;
1185 match_len = dir_len + 1 + len;
1186 match_format = format_aux;
1187 goto success;
1188 }
1189 } else {
1190 /* continue trying to find exact revision match, use this only if not found */
1191 free(match_name);
1192 match_name = wn;
1193 wn = NULL;
1194 match_len = dir_len + 1 +len;
1195 match_format = format_aux;
1196 continue;
1197 }
1198 } else {
1199 /* remember the revision and try to find the newest one */
1200 if (match_name) {
1201 if (file->d_name[len] != '@' ||
1202 lysp_check_date(NULL, &file->d_name[len + 1], flen - (format_aux == LYS_IN_YANG ? 5 : 4) - len - 1, NULL)) {
1203 continue;
1204 } else if (match_name[match_len] == '@' &&
1205 (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) {
1206 continue;
1207 }
1208 free(match_name);
1209 }
1210
1211 match_name = wn;
1212 wn = NULL;
1213 match_len = dir_len + 1 + len;
1214 match_format = format_aux;
1215 continue;
1216 }
1217 }
1218 }
1219 }
1220
1221success:
1222 (*localfile) = match_name;
1223 match_name = NULL;
1224 if (format) {
1225 (*format) = match_format;
1226 }
1227 ret = EXIT_SUCCESS;
1228
1229cleanup:
1230 free(wn);
1231 free(wd);
1232 if (dir) {
1233 closedir(dir);
1234 }
1235 free(match_name);
1236 ly_set_free(dirs, free);
1237
1238 return ret;
1239}
1240