blob: c0345816e374189728b1d37aca1a6d710e6555ae [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 *
188lys_child(const struct lysc_node *parent, const struct lys_module *module,
189 const char *name, size_t name_len, uint16_t nodetype, int options)
190{
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) {
207 if (!strncmp(node->name, name, name_len) && !node->name[name_len]) {
208 return node;
209 }
210 } else {
211 if (!strcmp(node->name, name)) {
212 return node;
213 }
214 }
215 }
216 return NULL;
217}
218
Radek Krejci327de162019-06-14 12:52:07 +0200219API char *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200220lysc_path(const struct lysc_node *node, LYSC_PATH_TYPE pathtype, char *buffer, size_t buflen)
Radek Krejci327de162019-06-14 12:52:07 +0200221{
Michal Vasko03ff5a72019-09-11 13:49:33 +0200222 const struct lysc_node *iter;
Radek Krejci327de162019-06-14 12:52:07 +0200223 char *path = NULL;
224 int len = 0;
225
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200226 LY_CHECK_ARG_RET(NULL, node, NULL);
227 if (buffer) {
228 LY_CHECK_ARG_RET(node->module->ctx, buflen > 1, NULL);
229 }
230
Radek Krejci327de162019-06-14 12:52:07 +0200231 switch (pathtype) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200232 case LYSC_PATH_LOG:
Radek Krejci327de162019-06-14 12:52:07 +0200233 for (iter = node; iter && len >= 0; iter = iter->parent) {
Radek Krejci1c0c3442019-07-23 16:08:47 +0200234 char *s = buffer ? strdup(buffer) : path;
Radek Krejci327de162019-06-14 12:52:07 +0200235 char *id;
236
Michal Vasko03ff5a72019-09-11 13:49:33 +0200237 id = strdup(iter->name);
Radek Krejci327de162019-06-14 12:52:07 +0200238 if (!iter->parent || iter->parent->module != iter->module) {
239 /* print prefix */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200240 if (buffer) {
241 len = snprintf(buffer, buflen, "/%s:%s%s", iter->module->name, id, s ? s : "");
242 } else {
243 len = asprintf(&path, "/%s:%s%s", iter->module->name, id, s ? s : "");
244 }
Radek Krejci327de162019-06-14 12:52:07 +0200245 } else {
246 /* prefix is the same as in parent */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200247 if (buffer) {
248 len = snprintf(buffer, buflen, "/%s%s", id, s ? s : "");
249 } else {
250 len = asprintf(&path, "/%s%s", id, s ? s : "");
251 }
Radek Krejci327de162019-06-14 12:52:07 +0200252 }
253 free(s);
254 free(id);
Radek Krejci1c0c3442019-07-23 16:08:47 +0200255
256 if (buffer && buflen <= (size_t)len) {
257 /* not enough space in buffer */
258 break;
259 }
Radek Krejci327de162019-06-14 12:52:07 +0200260 }
261
262 if (len < 0) {
263 free(path);
264 path = NULL;
265 } else if (len == 0) {
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200266 if (buffer) {
267 strcpy(buffer, "/");
268 } else {
269 path = strdup("/");
270 }
Radek Krejci327de162019-06-14 12:52:07 +0200271 }
272 break;
273 }
274
Radek Krejci1c0c3442019-07-23 16:08:47 +0200275 if (buffer) {
276 return buffer;
277 } else {
278 return path;
279 }
Radek Krejci327de162019-06-14 12:52:07 +0200280}
281
Radek Krejci19a96102018-11-15 13:38:09 +0100282API int
283lysc_feature_value(const struct lysc_feature *feature)
Radek Krejci6f7feb62018-10-12 15:23:02 +0200284{
Radek Krejci19a96102018-11-15 13:38:09 +0100285 LY_CHECK_ARG_RET(NULL, feature, -1);
286 return feature->flags & LYS_FENABLED ? 1 : 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200287}
288
Radek Krejci693262f2019-04-29 15:23:20 +0200289uint8_t
290lysc_iff_getop(uint8_t *list, int pos)
Radek Krejci151a5b72018-10-19 14:21:44 +0200291{
292 uint8_t *item;
293 uint8_t mask = 3, result;
294
295 assert(pos >= 0);
296
297 item = &list[pos / 4];
298 result = (*item) & (mask << 2 * (pos % 4));
299 return result >> 2 * (pos % 4);
300}
301
Radek Krejci151a5b72018-10-19 14:21:44 +0200302static int
303lysc_iffeature_value_(const struct lysc_iffeature *iff, int *index_e, int *index_f)
304{
305 uint8_t op;
306 int a, b;
307
Radek Krejci693262f2019-04-29 15:23:20 +0200308 op = lysc_iff_getop(iff->expr, *index_e);
Radek Krejci151a5b72018-10-19 14:21:44 +0200309 (*index_e)++;
310
311 switch (op) {
312 case LYS_IFF_F:
313 /* resolve feature */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200314 return lysc_feature_value(iff->features[(*index_f)++]);
Radek Krejci151a5b72018-10-19 14:21:44 +0200315 case LYS_IFF_NOT:
316 /* invert result */
317 return lysc_iffeature_value_(iff, index_e, index_f) ? 0 : 1;
318 case LYS_IFF_AND:
319 case LYS_IFF_OR:
320 a = lysc_iffeature_value_(iff, index_e, index_f);
321 b = lysc_iffeature_value_(iff, index_e, index_f);
322 if (op == LYS_IFF_AND) {
323 return a && b;
324 } else { /* LYS_IFF_OR */
325 return a || b;
326 }
327 }
328
329 return 0;
330}
331
332API int
333lysc_iffeature_value(const struct lysc_iffeature *iff)
334{
335 int index_e = 0, index_f = 0;
336
337 LY_CHECK_ARG_RET(NULL, iff, -1);
338
339 if (iff->expr) {
340 return lysc_iffeature_value_(iff, &index_e, &index_f);
341 }
342 return 0;
343}
344
Radek Krejci151a5b72018-10-19 14:21:44 +0200345/**
346 * @brief Enable/Disable the specified feature in the module.
347 *
348 * If the feature is already set to the desired value, LY_SUCCESS is returned.
349 * By changing the feature, also all the feature which depends on it via their
350 * if-feature statements are again evaluated (disabled if a if-feature statemen
351 * evaluates to false).
352 *
Radek Krejci0af46292019-01-11 16:02:31 +0100353 * @param[in] mod Module where to set (search for) the feature.
Radek Krejci151a5b72018-10-19 14:21:44 +0200354 * @param[in] name Name of the feature to set. Asterisk ('*') can be used to
355 * set all the features in the module.
356 * @param[in] value Desired value of the feature: 1 (enable) or 0 (disable).
357 * @return LY_ERR value.
358 */
359static LY_ERR
Radek Krejci0af46292019-01-11 16:02:31 +0100360lys_feature_change(const struct lys_module *mod, const char *name, int value)
Radek Krejci151a5b72018-10-19 14:21:44 +0200361{
362 int all = 0;
Radek Krejcica3db002018-11-01 10:31:01 +0100363 unsigned int u, changed_count, disabled_count;
Radek Krejci151a5b72018-10-19 14:21:44 +0200364 struct lysc_feature *f, **df;
365 struct lysc_iffeature *iff;
366 struct ly_set *changed;
Radek Krejci0af46292019-01-11 16:02:31 +0100367 struct ly_ctx *ctx = mod->ctx; /* shortcut */
Radek Krejci151a5b72018-10-19 14:21:44 +0200368
Radek Krejci6e67c402019-05-02 09:55:39 +0200369 if (!strcmp(name, "*")) {
370 /* enable all */
371 all = 1;
372 }
373
Radek Krejci0af46292019-01-11 16:02:31 +0100374 if (!mod->compiled) {
375 LOGERR(ctx, LY_EINVAL, "Module \"%s\" is not implemented so all its features are permanently disabled without a chance to change it.",
376 mod->name);
377 return LY_EINVAL;
378 }
379 if (!mod->compiled->features) {
Radek Krejci6e67c402019-05-02 09:55:39 +0200380 if (all) {
381 /* no feature to enable */
382 return LY_SUCCESS;
383 }
Radek Krejci0af46292019-01-11 16:02:31 +0100384 LOGERR(ctx, LY_EINVAL, "Unable to switch feature since the module \"%s\" has no features.", mod->name);
Radek Krejci151a5b72018-10-19 14:21:44 +0200385 return LY_EINVAL;
386 }
387
Radek Krejci151a5b72018-10-19 14:21:44 +0200388 changed = ly_set_new();
Radek Krejcica3db002018-11-01 10:31:01 +0100389 changed_count = 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200390
Radek Krejcica3db002018-11-01 10:31:01 +0100391run:
Radek Krejci0af46292019-01-11 16:02:31 +0100392 for (disabled_count = u = 0; u < LY_ARRAY_SIZE(mod->compiled->features); ++u) {
393 f = &mod->compiled->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200394 if (all || !strcmp(f->name, name)) {
395 if ((value && (f->flags & LYS_FENABLED)) || (!value && !(f->flags & LYS_FENABLED))) {
396 if (all) {
397 /* skip already set features */
398 continue;
399 } else {
400 /* feature already set correctly */
401 ly_set_free(changed, NULL);
402 return LY_SUCCESS;
403 }
404 }
405
406 if (value) { /* enable */
407 /* check referenced features if they are enabled */
408 LY_ARRAY_FOR(f->iffeatures, struct lysc_iffeature, iff) {
409 if (!lysc_iffeature_value(iff)) {
410 if (all) {
Radek Krejcica3db002018-11-01 10:31:01 +0100411 ++disabled_count;
Radek Krejci151a5b72018-10-19 14:21:44 +0200412 goto next;
413 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100414 LOGERR(ctx, LY_EDENIED,
Radek Krejci151a5b72018-10-19 14:21:44 +0200415 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
416 f->name);
417 ly_set_free(changed, NULL);
418 return LY_EDENIED;
419 }
420 }
421 }
422 /* enable the feature */
423 f->flags |= LYS_FENABLED;
424 } else { /* disable */
425 /* disable the feature */
426 f->flags &= ~LYS_FENABLED;
427 }
428
429 /* remember the changed feature */
430 ly_set_add(changed, f, LY_SET_OPT_USEASLIST);
431
432 if (!all) {
433 /* stop in case changing a single feature */
434 break;
435 }
436 }
437next:
438 ;
439 }
440
441 if (!all && !changed->count) {
Radek Krejci0af46292019-01-11 16:02:31 +0100442 LOGERR(ctx, LY_EINVAL, "Feature \"%s\" not found in module \"%s\".", name, mod->name);
Radek Krejci151a5b72018-10-19 14:21:44 +0200443 ly_set_free(changed, NULL);
444 return LY_EINVAL;
445 }
446
Radek Krejcica3db002018-11-01 10:31:01 +0100447 if (value && all && disabled_count) {
448 if (changed_count == changed->count) {
449 /* no change in last run -> not able to enable all ... */
450 /* ... print errors */
Radek Krejci0af46292019-01-11 16:02:31 +0100451 for (u = 0; disabled_count && u < LY_ARRAY_SIZE(mod->compiled->features); ++u) {
452 if (!(mod->compiled->features[u].flags & LYS_FENABLED)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100453 LOGERR(ctx, LY_EDENIED,
Radek Krejcica3db002018-11-01 10:31:01 +0100454 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
Radek Krejci0af46292019-01-11 16:02:31 +0100455 mod->compiled->features[u].name);
Radek Krejcica3db002018-11-01 10:31:01 +0100456 --disabled_count;
457 }
458 }
459 /* ... restore the original state */
460 for (u = 0; u < changed->count; ++u) {
461 f = changed->objs[u];
462 /* re-disable the feature */
463 f->flags &= ~LYS_FENABLED;
464 }
465
466 ly_set_free(changed, NULL);
467 return LY_EDENIED;
468 } else {
469 /* we did some change in last run, try it again */
470 changed_count = changed->count;
471 goto run;
472 }
473 }
474
Radek Krejci151a5b72018-10-19 14:21:44 +0200475 /* reflect change(s) in the dependent features */
476 for (u = 0; u < changed->count; ++u) {
477 /* If a dependent feature is enabled, it can be now changed by the change (to false) of the value of
478 * its if-feature statements. The reverse logic, automatically enable feature when its feature is enabled
479 * is not done - by default, features are disabled and must be explicitely enabled. */
480 f = changed->objs[u];
481 LY_ARRAY_FOR(f->depfeatures, struct lysc_feature*, df) {
482 if (!((*df)->flags & LYS_FENABLED)) {
483 /* not enabled, nothing to do */
484 continue;
485 }
486 /* check the feature's if-features which could change by the previous change of our feature */
487 LY_ARRAY_FOR((*df)->iffeatures, struct lysc_iffeature, iff) {
488 if (!lysc_iffeature_value(iff)) {
489 /* the feature must be disabled now */
490 (*df)->flags &= ~LYS_FENABLED;
491 /* add the feature into the list of changed features */
492 ly_set_add(changed, *df, LY_SET_OPT_USEASLIST);
493 break;
494 }
495 }
496 }
497 }
498
499 ly_set_free(changed, NULL);
500 return LY_SUCCESS;
501}
502
503API LY_ERR
Radek Krejcied5acc52019-04-25 15:57:04 +0200504lys_feature_enable(const struct lys_module *module, const char *feature)
Radek Krejci151a5b72018-10-19 14:21:44 +0200505{
Radek Krejci0af46292019-01-11 16:02:31 +0100506 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200507
Radek Krejcied5acc52019-04-25 15:57:04 +0200508 return lys_feature_change((struct lys_module*)module, feature, 1);
Radek Krejci151a5b72018-10-19 14:21:44 +0200509}
510
511API LY_ERR
Radek Krejcied5acc52019-04-25 15:57:04 +0200512lys_feature_disable(const struct lys_module *module, const char *feature)
Radek Krejci151a5b72018-10-19 14:21:44 +0200513{
Radek Krejci0af46292019-01-11 16:02:31 +0100514 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200515
Radek Krejcied5acc52019-04-25 15:57:04 +0200516 return lys_feature_change((struct lys_module*)module, feature, 0);
Radek Krejci151a5b72018-10-19 14:21:44 +0200517}
518
519API int
520lys_feature_value(const struct lys_module *module, const char *feature)
521{
522 struct lysc_feature *f;
523 struct lysc_module *mod;
524 unsigned int u;
525
526 LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, -1);
527 mod = module->compiled;
528
529 /* search for the specified feature */
530 for (u = 0; u < LY_ARRAY_SIZE(mod->features); ++u) {
Radek Krejci2c4e7172018-10-19 15:56:26 +0200531 f = &mod->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200532 if (!strcmp(f->name, feature)) {
533 if (f->flags & LYS_FENABLED) {
534 return 1;
535 } else {
536 return 0;
537 }
538 }
539 }
540
541 /* feature definition not found */
542 return -1;
543}
544
Radek Krejcia3045382018-11-22 14:30:31 +0100545API const struct lysc_iffeature *
Radek Krejcifab954b2019-09-11 11:25:14 +0200546lysc_node_is_disabled(const struct lysc_node *node, int recursive)
Radek Krejcia3045382018-11-22 14:30:31 +0100547{
548 unsigned int u;
Radek Krejcia3045382018-11-22 14:30:31 +0100549
550 LY_CHECK_ARG_RET(NULL, node, NULL);
551
552 while(node) {
553 if (node->nodetype & LYS_CHOICE) {
554 return NULL;
555 }
556
Radek Krejci056d0a82018-12-06 16:57:25 +0100557 if (node->iffeatures) {
Radek Krejcia3045382018-11-22 14:30:31 +0100558 /* check local if-features */
Radek Krejci056d0a82018-12-06 16:57:25 +0100559 LY_ARRAY_FOR(node->iffeatures, u) {
560 if (!lysc_iffeature_value(&node->iffeatures[u])) {
561 return &node->iffeatures[u];
Radek Krejcia3045382018-11-22 14:30:31 +0100562 }
563 }
564 }
565
566 if (!recursive) {
567 return NULL;
568 }
569
570 /* go through parents */
571 node = node->parent;
572 }
573 return NULL;
574}
575
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200576LY_ERR
577lys_set_implemented_internal(struct lys_module *mod, uint8_t value)
578{
579 struct lys_module *m;
580
581 LY_CHECK_ARG_RET(NULL, mod, LY_EINVAL);
582
583 if (mod->implemented) {
584 return LY_SUCCESS;
585 }
586
587 /* we have module from the current context */
588 m = ly_ctx_get_module_implemented(mod->ctx, mod->name);
589 if (m) {
590 if (m != mod) {
591 /* check collision with other implemented revision */
592 LOGERR(mod->ctx, LY_EDENIED, "Module \"%s\" is present in the context in other implemented revision (%s).",
593 mod->name, mod->revision ? mod->revision : "module without revision");
594 return LY_EDENIED;
595 } else {
596 /* mod is already implemented */
597 return LY_SUCCESS;
598 }
599 }
600
601 /* mark the module implemented, check for collision was already done */
602 mod->implemented = value;
603
604 /* compile the schema */
605 LY_CHECK_RET(lys_compile(mod, LYSC_OPT_INTERNAL));
606
607 return LY_SUCCESS;
608}
609
610API LY_ERR
611lys_set_implemented(struct lys_module *mod)
612{
613 return lys_set_implemented_internal(mod, 1);
614}
615
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100616struct lysp_submodule *
Radek Krejcie7b95092019-05-15 11:03:07 +0200617lys_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 +0100618 LY_ERR (*custom_check)(struct ly_ctx*, struct lysp_module*, struct lysp_submodule*, void*), void *check_data)
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200619{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100620 LY_ERR ret = LY_EINVAL;
621 struct lysp_submodule *submod = NULL, *latest_sp;
David Sedlák1b623122019-08-05 15:27:49 +0200622 struct lys_parser_ctx *context = NULL;
623 struct yin_parser_ctx *yin_context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100624
625 LY_CHECK_ARG_RET(ctx, ctx, data, NULL);
626
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100627 switch (format) {
628 case LYS_IN_YIN:
David Sedlák1b623122019-08-05 15:27:49 +0200629 ret = yin_parse_submodule(&yin_context, ctx, main_ctx, data, &submod);
630 context = (struct lys_parser_ctx *)yin_context;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100631 break;
632 case LYS_IN_YANG:
David Sedlák1b623122019-08-05 15:27:49 +0200633 ret = yang_parse_submodule(&context, ctx, main_ctx, data, &submod);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100634 break;
635 default:
David Sedlák4f2f5ba2019-08-15 13:18:48 +0200636 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100637 break;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200638 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100639 LY_CHECK_RET(ret, NULL);
640
641 /* make sure that the newest revision is at position 0 */
642 lysp_sort_revisions(submod->revs);
643
644 if (custom_check) {
David Sedlák1b623122019-08-05 15:27:49 +0200645 LY_CHECK_GOTO(custom_check((*context).ctx, NULL, submod, check_data), error);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200646 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100647
648 /* decide the latest revision */
David Sedlák1b623122019-08-05 15:27:49 +0200649 latest_sp = ly_ctx_get_submodule((*context).ctx, submod->belongsto, submod->name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100650 if (latest_sp) {
651 if (submod->revs) {
652 if (!latest_sp->revs) {
653 /* latest has no revision, so mod is anyway newer */
654 submod->latest_revision = latest_sp->latest_revision;
655 latest_sp->latest_revision = 0;
656 } else {
657 if (strcmp(submod->revs[0].date, latest_sp->revs[0].date) > 0) {
658 submod->latest_revision = latest_sp->latest_revision;
659 latest_sp->latest_revision = 0;
660 }
661 }
662 }
663 } else {
664 submod->latest_revision = 1;
665 }
666
667 /* remap possibly changed and reallocated typedefs and groupings list back to the main context */
David Sedlák1b623122019-08-05 15:27:49 +0200668 memcpy(&main_ctx->tpdfs_nodes, &(*context).tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
669 memcpy(&main_ctx->grps_nodes, &(*context).grps_nodes, sizeof main_ctx->grps_nodes);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100670
David Sedlák1b623122019-08-05 15:27:49 +0200671 if (format == LYS_IN_YANG) {
672 lys_parser_ctx_free(context);
673 } else {
674 yin_parser_ctx_free(yin_context);
675 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100676 return submod;
David Sedlák1b623122019-08-05 15:27:49 +0200677
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100678error:
679 lysp_submodule_free(ctx, submod);
David Sedlák1b623122019-08-05 15:27:49 +0200680 if (format == LYS_IN_YANG) {
681 lys_parser_ctx_free(context);
682 } else {
683 yin_parser_ctx_free(yin_context);
684 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100685 return NULL;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200686}
687
Radek Krejcid33273d2018-10-25 14:55:52 +0200688struct lys_module *
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100689lys_parse_mem_module(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, int implement,
690 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
691 void *check_data)
Radek Krejci86d106e2018-10-18 09:53:19 +0200692{
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100693 struct lys_module *mod = NULL, *latest, *mod_dup;
Radek Krejci086c7132018-10-26 15:29:04 +0200694 struct lysp_import *imp;
695 struct lysp_include *inc;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100696 LY_ERR ret = LY_EINVAL;
Radek Krejci086c7132018-10-26 15:29:04 +0200697 unsigned int u, i;
David Sedlák1b623122019-08-05 15:27:49 +0200698 struct lys_parser_ctx *context = NULL;
699 struct yin_parser_ctx *yin_context = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +0200700
701 LY_CHECK_ARG_RET(ctx, ctx, data, NULL);
702
703 mod = calloc(1, sizeof *mod);
704 LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100705 mod->ctx = ctx;
Radek Krejci86d106e2018-10-18 09:53:19 +0200706
707 switch (format) {
708 case LYS_IN_YIN:
David Sedlák1b623122019-08-05 15:27:49 +0200709 ret = yin_parse_module(&yin_context, data, mod);
710 context = (struct lys_parser_ctx *)yin_context;
Radek Krejci86d106e2018-10-18 09:53:19 +0200711 break;
712 case LYS_IN_YANG:
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100713 ret = yang_parse_module(&context, data, mod);
Radek Krejci86d106e2018-10-18 09:53:19 +0200714 break;
715 default:
716 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
717 break;
718 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100719 LY_CHECK_ERR_RET(ret, lys_module_free(mod, NULL), NULL);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200720
721 /* make sure that the newest revision is at position 0 */
722 lysp_sort_revisions(mod->parsed->revs);
Radek Krejci0af46292019-01-11 16:02:31 +0100723 if (mod->parsed->revs) {
724 mod->revision = lydict_insert(ctx, mod->parsed->revs[0].date, 0);
725 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200726
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100727 if (custom_check) {
728 LY_CHECK_GOTO(custom_check(ctx, mod->parsed, NULL, check_data), error);
729 }
730
Radek Krejci86d106e2018-10-18 09:53:19 +0200731 if (implement) {
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200732 /* mark the loaded module implemented */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100733 if (ly_ctx_get_module_implemented(ctx, mod->name)) {
734 LOGERR(ctx, LY_EDENIED, "Module \"%s\" is already implemented in the context.", mod->name);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100735 goto error;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200736 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100737 mod->implemented = 1;
Radek Krejci86d106e2018-10-18 09:53:19 +0200738 }
739
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100740 /* check for duplicity in the context */
Radek Krejci0af46292019-01-11 16:02:31 +0100741 mod_dup = (struct lys_module*)ly_ctx_get_module(ctx, mod->name, mod->revision);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100742 if (mod_dup) {
743 if (mod_dup->parsed) {
744 /* error */
Radek Krejcid33273d2018-10-25 14:55:52 +0200745 if (mod->parsed->revs) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100746 LOGERR(ctx, LY_EEXIST, "Module \"%s\" of revision \"%s\" is already present in the context.",
747 mod->name, mod->parsed->revs[0].date);
Radek Krejcid33273d2018-10-25 14:55:52 +0200748 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100749 LOGERR(ctx, LY_EEXIST, "Module \"%s\" with no revision is already present in the context.",
750 mod->name);
Radek Krejcid33273d2018-10-25 14:55:52 +0200751 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100752 goto error;
753 } else {
754 /* add the parsed data to the currently compiled-only module in the context */
755 mod_dup->parsed = mod->parsed;
756 mod_dup->parsed->mod = mod_dup;
757 mod->parsed = NULL;
758 lys_module_free(mod, NULL);
759 mod = mod_dup;
760 goto finish_parsing;
Radek Krejcid33273d2018-10-25 14:55:52 +0200761 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100762 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200763
764#if 0
765 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
766 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
767 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
768 * the anotation definitions available in the internal schema structure. There is another hack in schema
769 * printers to do not print this internally added annotation. */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100770 if (ly_strequal(mod->name, "ietf-netconf", 0)) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200771 if (lyp_add_ietf_netconf_annotations(mod)) {
772 lys_free(mod, NULL, 1, 1);
773 return NULL;
774 }
775 }
776#endif
777
Radek Krejci0af46292019-01-11 16:02:31 +0100778 if (!mod->implemented) {
Radek Krejci0935f412019-08-20 16:15:18 +0200779 /* pre-compile features and extension definitions of the module */
Radek Krejci327de162019-06-14 12:52:07 +0200780 LY_CHECK_GOTO(lys_feature_precompile(NULL, ctx, mod, mod->parsed->features, &mod->off_features), error);
Radek Krejci0935f412019-08-20 16:15:18 +0200781 LY_CHECK_GOTO(lys_extension_precompile(NULL, ctx, mod, mod->parsed->extensions, &mod->off_extensions), error);
Radek Krejci0af46292019-01-11 16:02:31 +0100782 }
783
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100784 /* decide the latest revision */
785 latest = (struct lys_module*)ly_ctx_get_module_latest(ctx, mod->name);
786 if (latest) {
Radek Krejci0af46292019-01-11 16:02:31 +0100787 if (mod->revision) {
788 if (!latest->revision) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100789 /* latest has no revision, so mod is anyway newer */
790 mod->latest_revision = latest->latest_revision;
791 latest->latest_revision = 0;
Radek Krejci0af46292019-01-11 16:02:31 +0100792 } else if (strcmp(mod->revision, latest->revision) > 0) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100793 mod->latest_revision = latest->latest_revision;
794 latest->latest_revision = 0;
Radek Krejcid33273d2018-10-25 14:55:52 +0200795 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200796 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100797 } else {
798 mod->latest_revision = 1;
799 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200800
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100801 /* add into context */
802 ly_set_add(&ctx->list, mod, LY_SET_OPT_USEASLIST);
Radek Krejcid33273d2018-10-25 14:55:52 +0200803
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100804finish_parsing:
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100805 /* resolve imports */
806 mod->parsed->parsing = 1;
807 LY_ARRAY_FOR(mod->parsed->imports, u) {
808 imp = &mod->parsed->imports[u];
809 if (!imp->module && lysp_load_module(ctx, imp->name, imp->rev[0] ? imp->rev : NULL, 0, 0, &imp->module)) {
810 goto error_ctx;
Radek Krejci086c7132018-10-26 15:29:04 +0200811 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100812 /* check for importing the same module twice */
813 for (i = 0; i < u; ++i) {
814 if (imp->module == mod->parsed->imports[i].module) {
815 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 +0100816 goto error_ctx;
Radek Krejci086c7132018-10-26 15:29:04 +0200817 }
818 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200819 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100820 LY_ARRAY_FOR(mod->parsed->includes, u) {
821 inc = &mod->parsed->includes[u];
David Sedlák1b623122019-08-05 15:27:49 +0200822 if (!inc->submodule && lysp_load_submodule(context, mod->parsed, inc)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100823 goto error_ctx;
824 }
Radek Krejci0af46292019-01-11 16:02:31 +0100825 if (!mod->implemented) {
Radek Krejci0935f412019-08-20 16:15:18 +0200826 /* pre-compile features and extension definitions of the module */
Radek Krejci327de162019-06-14 12:52:07 +0200827 LY_CHECK_GOTO(lys_feature_precompile(NULL, ctx, mod, inc->submodule->features, &mod->off_features), error);
Radek Krejci0935f412019-08-20 16:15:18 +0200828 LY_CHECK_GOTO(lys_extension_precompile(NULL, ctx, mod, mod->parsed->extensions, &mod->off_extensions), error);
Radek Krejci0af46292019-01-11 16:02:31 +0100829 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100830 }
831 mod->parsed->parsing = 0;
832
Radek Krejci7fc68292019-06-12 13:51:09 +0200833 /* check name collisions - typedefs and TODO groupings */
David Sedlák1b623122019-08-05 15:27:49 +0200834 LY_CHECK_GOTO(lysp_check_typedefs(context, mod->parsed), error_ctx);
Radek Krejcid33273d2018-10-25 14:55:52 +0200835
David Sedlák1b623122019-08-05 15:27:49 +0200836 if (format == LYS_IN_YANG) {
837 lys_parser_ctx_free(context);
838 } else {
839 yin_parser_ctx_free(yin_context);
840 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200841 return mod;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100842
843error_ctx:
844 ly_set_rm(&ctx->list, mod, NULL);
845error:
846 lys_module_free(mod, NULL);
David Sedlák1b623122019-08-05 15:27:49 +0200847 ly_set_erase(&context->tpdfs_nodes, NULL);
848 if (format == LYS_IN_YANG) {
849 lys_parser_ctx_free(context);
850 } else {
851 yin_parser_ctx_free(yin_context);
852 }
853
Radek Krejcibbe09a92018-11-08 09:36:54 +0100854 return NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +0200855}
856
Radek Krejcid14e9692018-11-01 11:00:37 +0100857API struct lys_module *
Radek Krejci86d106e2018-10-18 09:53:19 +0200858lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format)
859{
Radek Krejci096235c2019-01-11 11:12:19 +0100860 struct lys_module *mod;
861
862 mod = lys_parse_mem_module(ctx, data, format, 1, NULL, NULL);
863 LY_CHECK_RET(!mod, NULL);
864
865 if (lys_compile(mod, 0)) {
866 ly_set_rm(&ctx->list, mod, NULL);
867 lys_module_free(mod, NULL);
868 return NULL;
869 }
870 return mod;
Radek Krejci86d106e2018-10-18 09:53:19 +0200871}
872
873static void
874lys_parse_set_filename(struct ly_ctx *ctx, const char **filename, int fd)
875{
Radek Krejci65639b92018-11-27 10:51:37 +0100876 char path[PATH_MAX];
Radek Krejci86d106e2018-10-18 09:53:19 +0200877
878#ifdef __APPLE__
879 if (fcntl(fd, F_GETPATH, path) != -1) {
880 *filename = lydict_insert(ctx, path, 0);
881 }
882#else
Radek Krejci65639b92018-11-27 10:51:37 +0100883 int len;
884 char proc_path[32];
885
Radek Krejci86d106e2018-10-18 09:53:19 +0200886 /* get URI if there is /proc */
887 sprintf(proc_path, "/proc/self/fd/%d", fd);
888 if ((len = readlink(proc_path, path, PATH_MAX - 1)) > 0) {
889 *filename = lydict_insert(ctx, path, len);
890 }
891#endif
892}
893
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100894void *
Radek Krejcie7b95092019-05-15 11:03:07 +0200895lys_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 +0100896 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
897 void *check_data)
Radek Krejci86d106e2018-10-18 09:53:19 +0200898{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100899 void *result;
900 struct lys_module *mod = NULL;
901 struct lysp_submodule *submod = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +0200902 size_t length;
903 char *addr;
904
905 LY_CHECK_ARG_RET(ctx, ctx, NULL);
906 if (fd < 0) {
907 LOGARG(ctx, fd);
908 return NULL;
909 }
910
911 LY_CHECK_RET(ly_mmap(ctx, fd, &length, (void **)&addr), NULL);
912 if (!addr) {
913 LOGERR(ctx, LY_EINVAL, "Empty schema file.");
914 return NULL;
915 }
916
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100917 if (main_ctx) {
918 result = submod = lys_parse_mem_submodule(ctx, addr, format, main_ctx, custom_check, check_data);
919 } else {
920 result = mod = lys_parse_mem_module(ctx, addr, format, implement, custom_check, check_data);
Radek Krejci096235c2019-01-11 11:12:19 +0100921 if (mod && implement && lys_compile(mod, 0)) {
922 ly_set_rm(&ctx->list, mod, NULL);
923 lys_module_free(mod, NULL);
924 result = mod = NULL;
925 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100926 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200927 ly_munmap(addr, length);
928
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100929 if (mod && !mod->filepath) {
930 lys_parse_set_filename(ctx, &mod->filepath, fd);
931 } else if (submod && !submod->filepath) {
932 lys_parse_set_filename(ctx, &submod->filepath, fd);
Radek Krejci86d106e2018-10-18 09:53:19 +0200933 }
934
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100935 return result;
936}
937
938struct lys_module *
939lys_parse_fd_module(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, int implement,
940 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
941 void *check_data)
942{
943 return (struct lys_module*)lys_parse_fd_(ctx, fd, format, implement, NULL, custom_check, check_data);
944}
945
946struct lysp_submodule *
Radek Krejcie7b95092019-05-15 11:03:07 +0200947lys_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 +0100948 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
949 void *check_data)
950{
951 assert(main_ctx);
952 return (struct lysp_submodule*)lys_parse_fd_(ctx, fd, format, 0, main_ctx, custom_check, check_data);
Radek Krejci86d106e2018-10-18 09:53:19 +0200953}
954
Radek Krejcid14e9692018-11-01 11:00:37 +0100955API struct lys_module *
Radek Krejci86d106e2018-10-18 09:53:19 +0200956lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format)
957{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100958 return lys_parse_fd_module(ctx, fd, format, 1, NULL, NULL);
Radek Krejci86d106e2018-10-18 09:53:19 +0200959}
960
Radek Krejcid33273d2018-10-25 14:55:52 +0200961struct lys_module *
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100962lys_parse_path_(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, int implement,
963 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 +0200964{
965 int fd;
Radek Krejcid33273d2018-10-25 14:55:52 +0200966 struct lys_module *mod;
Radek Krejci86d106e2018-10-18 09:53:19 +0200967 const char *rev, *dot, *filename;
968 size_t len;
969
970 LY_CHECK_ARG_RET(ctx, ctx, path, NULL);
971
972 fd = open(path, O_RDONLY);
973 LY_CHECK_ERR_RET(fd == -1, LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)), NULL);
974
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100975 mod = lys_parse_fd_module(ctx, fd, format, implement, custom_check, check_data);
Radek Krejci86d106e2018-10-18 09:53:19 +0200976 close(fd);
977 LY_CHECK_RET(!mod, NULL);
978
979 /* check that name and revision match filename */
980 filename = strrchr(path, '/');
981 if (!filename) {
982 filename = path;
983 } else {
984 filename++;
985 }
986 rev = strchr(filename, '@');
987 dot = strrchr(filename, '.');
988
989 /* name */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100990 len = strlen(mod->name);
991 if (strncmp(filename, mod->name, len) ||
Radek Krejci86d106e2018-10-18 09:53:19 +0200992 ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100993 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name);
Radek Krejci86d106e2018-10-18 09:53:19 +0200994 }
995 if (rev) {
996 len = dot - ++rev;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200997 if (!mod->parsed->revs || len != 10 || strncmp(mod->parsed->revs[0].date, rev, len)) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200998 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Radek Krejcib7db73a2018-10-24 14:18:40 +0200999 mod->parsed->revs ? mod->parsed->revs[0].date : "none");
Radek Krejci86d106e2018-10-18 09:53:19 +02001000 }
1001 }
1002
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001003 if (!mod->filepath) {
Radek Krejci86d106e2018-10-18 09:53:19 +02001004 /* store URI */
1005 char rpath[PATH_MAX];
1006 if (realpath(path, rpath) != NULL) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001007 mod->filepath = lydict_insert(ctx, rpath, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001008 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001009 mod->filepath = lydict_insert(ctx, path, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001010 }
1011 }
1012
1013 return mod;
1014}
1015
Radek Krejcid14e9692018-11-01 11:00:37 +01001016API struct lys_module *
Radek Krejcid33273d2018-10-25 14:55:52 +02001017lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format)
1018{
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001019 return lys_parse_path_(ctx, path, format, 1, NULL, NULL);
Radek Krejcid33273d2018-10-25 14:55:52 +02001020}
1021
1022API LY_ERR
1023lys_search_localfile(const char * const *searchpaths, int cwd, const char *name, const char *revision,
1024 char **localfile, LYS_INFORMAT *format)
1025{
1026 size_t len, flen, match_len = 0, dir_len;
1027 int i, implicit_cwd = 0, ret = EXIT_FAILURE;
1028 char *wd, *wn = NULL;
1029 DIR *dir = NULL;
1030 struct dirent *file;
1031 char *match_name = NULL;
1032 LYS_INFORMAT format_aux, match_format = 0;
1033 struct ly_set *dirs;
1034 struct stat st;
1035
1036 LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL);
1037
1038 /* start to fill the dir fifo with the context's search path (if set)
1039 * and the current working directory */
1040 dirs = ly_set_new();
1041 if (!dirs) {
1042 LOGMEM(NULL);
1043 return EXIT_FAILURE;
1044 }
1045
1046 len = strlen(name);
1047 if (cwd) {
1048 wd = get_current_dir_name();
1049 if (!wd) {
1050 LOGMEM(NULL);
1051 goto cleanup;
1052 } else {
1053 /* add implicit current working directory (./) to be searched,
1054 * this directory is not searched recursively */
1055 if (ly_set_add(dirs, wd, 0) == -1) {
1056 goto cleanup;
1057 }
1058 implicit_cwd = 1;
1059 }
1060 }
1061 if (searchpaths) {
1062 for (i = 0; searchpaths[i]; i++) {
1063 /* check for duplicities with the implicit current working directory */
1064 if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) {
1065 implicit_cwd = 0;
1066 continue;
1067 }
1068 wd = strdup(searchpaths[i]);
1069 if (!wd) {
1070 LOGMEM(NULL);
1071 goto cleanup;
1072 } else if (ly_set_add(dirs, wd, 0) == -1) {
1073 goto cleanup;
1074 }
1075 }
1076 }
1077 wd = NULL;
1078
1079 /* start searching */
1080 while (dirs->count) {
1081 free(wd);
1082 free(wn); wn = NULL;
1083
1084 dirs->count--;
1085 wd = (char *)dirs->objs[dirs->count];
1086 dirs->objs[dirs->count] = NULL;
1087 LOGVRB("Searching for \"%s\" in %s.", name, wd);
1088
1089 if (dir) {
1090 closedir(dir);
1091 }
1092 dir = opendir(wd);
1093 dir_len = strlen(wd);
1094 if (!dir) {
1095 LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno));
1096 } else {
1097 while ((file = readdir(dir))) {
1098 if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) {
1099 /* skip . and .. */
1100 continue;
1101 }
1102 free(wn);
1103 if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) {
1104 LOGMEM(NULL);
1105 goto cleanup;
1106 }
1107 if (stat(wn, &st) == -1) {
1108 LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)",
1109 file->d_name, wd, strerror(errno));
1110 continue;
1111 }
1112 if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) {
1113 /* we have another subdirectory in searchpath to explore,
1114 * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */
1115 if (ly_set_add(dirs, wn, 0) == -1) {
1116 goto cleanup;
1117 }
1118 /* continue with the next item in current directory */
1119 wn = NULL;
1120 continue;
1121 } else if (!S_ISREG(st.st_mode)) {
1122 /* not a regular file (note that we see the target of symlinks instead of symlinks */
1123 continue;
1124 }
1125
1126 /* here we know that the item is a file which can contain a module */
1127 if (strncmp(name, file->d_name, len) ||
1128 (file->d_name[len] != '.' && file->d_name[len] != '@')) {
1129 /* different filename than the module we search for */
1130 continue;
1131 }
1132
1133 /* get type according to filename suffix */
1134 flen = strlen(file->d_name);
Radek Krejcied5acc52019-04-25 15:57:04 +02001135 if (!strcmp(&file->d_name[flen - 5], ".yang")) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001136 format_aux = LYS_IN_YANG;
Radek Krejcied5acc52019-04-25 15:57:04 +02001137 /* TODO YIN parser
1138 } else if (!strcmp(&file->d_name[flen - 4], ".yin")) {
1139 format_aux = LYS_IN_YIN;
1140 */
Radek Krejcid33273d2018-10-25 14:55:52 +02001141 } else {
1142 /* not supportde suffix/file format */
1143 continue;
1144 }
1145
1146 if (revision) {
1147 /* we look for the specific revision, try to get it from the filename */
1148 if (file->d_name[len] == '@') {
1149 /* check revision from the filename */
1150 if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) {
1151 /* another revision */
1152 continue;
1153 } else {
1154 /* exact revision */
1155 free(match_name);
1156 match_name = wn;
1157 wn = NULL;
1158 match_len = dir_len + 1 + len;
1159 match_format = format_aux;
1160 goto success;
1161 }
1162 } else {
1163 /* continue trying to find exact revision match, use this only if not found */
1164 free(match_name);
1165 match_name = wn;
1166 wn = NULL;
1167 match_len = dir_len + 1 +len;
1168 match_format = format_aux;
1169 continue;
1170 }
1171 } else {
1172 /* remember the revision and try to find the newest one */
1173 if (match_name) {
1174 if (file->d_name[len] != '@' ||
1175 lysp_check_date(NULL, &file->d_name[len + 1], flen - (format_aux == LYS_IN_YANG ? 5 : 4) - len - 1, NULL)) {
1176 continue;
1177 } else if (match_name[match_len] == '@' &&
1178 (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) {
1179 continue;
1180 }
1181 free(match_name);
1182 }
1183
1184 match_name = wn;
1185 wn = NULL;
1186 match_len = dir_len + 1 + len;
1187 match_format = format_aux;
1188 continue;
1189 }
1190 }
1191 }
1192 }
1193
1194success:
1195 (*localfile) = match_name;
1196 match_name = NULL;
1197 if (format) {
1198 (*format) = match_format;
1199 }
1200 ret = EXIT_SUCCESS;
1201
1202cleanup:
1203 free(wn);
1204 free(wd);
1205 if (dir) {
1206 closedir(dir);
1207 }
1208 free(match_name);
1209 ly_set_free(dirs, free);
1210
1211 return ret;
1212}
1213