blob: 3fc8b086ada91649e15f6bb12dce18dd6eb96280 [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 */
179 if (lys_is_disabled(next, 0)) {
180 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 Krejci1c0c3442019-07-23 16:08:47 +0200219
220
Radek Krejci327de162019-06-14 12:52:07 +0200221API char *
Radek Krejci1c0c3442019-07-23 16:08:47 +0200222lysc_path(struct lysc_node *node, LY_PATH_TYPE pathtype, char *buffer, size_t buflen)
Radek Krejci327de162019-06-14 12:52:07 +0200223{
224 struct lysc_node *iter;
225 char *path = NULL;
226 int len = 0;
227
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200228 LY_CHECK_ARG_RET(NULL, node, NULL);
229 if (buffer) {
230 LY_CHECK_ARG_RET(node->module->ctx, buflen > 1, NULL);
231 }
232
Radek Krejci327de162019-06-14 12:52:07 +0200233 switch (pathtype) {
234 case LY_PATH_LOG:
235 for (iter = node; iter && len >= 0; iter = iter->parent) {
Radek Krejci1c0c3442019-07-23 16:08:47 +0200236 char *s = buffer ? strdup(buffer) : path;
Radek Krejci327de162019-06-14 12:52:07 +0200237 char *id;
238
239 switch (iter->nodetype) {
240 case LYS_USES:
241 asprintf(&id, "{uses='%s'}", iter->name);
242 break;
243 case LYS_GROUPING:
244 asprintf(&id, "{grouping='%s'}", iter->name);
245 break;
246 case LYS_AUGMENT:
247 asprintf(&id, "{augment='%s'}", iter->name);
248 break;
249 default:
250 id = strdup(iter->name);
251 break;
252 }
253
254 if (!iter->parent || iter->parent->module != iter->module) {
255 /* print prefix */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200256 if (buffer) {
257 len = snprintf(buffer, buflen, "/%s:%s%s", iter->module->name, id, s ? s : "");
258 } else {
259 len = asprintf(&path, "/%s:%s%s", iter->module->name, id, s ? s : "");
260 }
Radek Krejci327de162019-06-14 12:52:07 +0200261 } else {
262 /* prefix is the same as in parent */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200263 if (buffer) {
264 len = snprintf(buffer, buflen, "/%s%s", id, s ? s : "");
265 } else {
266 len = asprintf(&path, "/%s%s", id, s ? s : "");
267 }
Radek Krejci327de162019-06-14 12:52:07 +0200268 }
269 free(s);
270 free(id);
Radek Krejci1c0c3442019-07-23 16:08:47 +0200271
272 if (buffer && buflen <= (size_t)len) {
273 /* not enough space in buffer */
274 break;
275 }
Radek Krejci327de162019-06-14 12:52:07 +0200276 }
277
278 if (len < 0) {
279 free(path);
280 path = NULL;
281 } else if (len == 0) {
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200282 if (buffer) {
283 strcpy(buffer, "/");
284 } else {
285 path = strdup("/");
286 }
Radek Krejci327de162019-06-14 12:52:07 +0200287 }
288 break;
289 }
290
Radek Krejci1c0c3442019-07-23 16:08:47 +0200291 if (buffer) {
292 return buffer;
293 } else {
294 return path;
295 }
Radek Krejci327de162019-06-14 12:52:07 +0200296}
297
Radek Krejci19a96102018-11-15 13:38:09 +0100298API int
299lysc_feature_value(const struct lysc_feature *feature)
Radek Krejci6f7feb62018-10-12 15:23:02 +0200300{
Radek Krejci19a96102018-11-15 13:38:09 +0100301 LY_CHECK_ARG_RET(NULL, feature, -1);
302 return feature->flags & LYS_FENABLED ? 1 : 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200303}
304
Radek Krejci693262f2019-04-29 15:23:20 +0200305uint8_t
306lysc_iff_getop(uint8_t *list, int pos)
Radek Krejci151a5b72018-10-19 14:21:44 +0200307{
308 uint8_t *item;
309 uint8_t mask = 3, result;
310
311 assert(pos >= 0);
312
313 item = &list[pos / 4];
314 result = (*item) & (mask << 2 * (pos % 4));
315 return result >> 2 * (pos % 4);
316}
317
Radek Krejci151a5b72018-10-19 14:21:44 +0200318static int
319lysc_iffeature_value_(const struct lysc_iffeature *iff, int *index_e, int *index_f)
320{
321 uint8_t op;
322 int a, b;
323
Radek Krejci693262f2019-04-29 15:23:20 +0200324 op = lysc_iff_getop(iff->expr, *index_e);
Radek Krejci151a5b72018-10-19 14:21:44 +0200325 (*index_e)++;
326
327 switch (op) {
328 case LYS_IFF_F:
329 /* resolve feature */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200330 return lysc_feature_value(iff->features[(*index_f)++]);
Radek Krejci151a5b72018-10-19 14:21:44 +0200331 case LYS_IFF_NOT:
332 /* invert result */
333 return lysc_iffeature_value_(iff, index_e, index_f) ? 0 : 1;
334 case LYS_IFF_AND:
335 case LYS_IFF_OR:
336 a = lysc_iffeature_value_(iff, index_e, index_f);
337 b = lysc_iffeature_value_(iff, index_e, index_f);
338 if (op == LYS_IFF_AND) {
339 return a && b;
340 } else { /* LYS_IFF_OR */
341 return a || b;
342 }
343 }
344
345 return 0;
346}
347
348API int
349lysc_iffeature_value(const struct lysc_iffeature *iff)
350{
351 int index_e = 0, index_f = 0;
352
353 LY_CHECK_ARG_RET(NULL, iff, -1);
354
355 if (iff->expr) {
356 return lysc_iffeature_value_(iff, &index_e, &index_f);
357 }
358 return 0;
359}
360
Radek Krejci151a5b72018-10-19 14:21:44 +0200361/**
362 * @brief Enable/Disable the specified feature in the module.
363 *
364 * If the feature is already set to the desired value, LY_SUCCESS is returned.
365 * By changing the feature, also all the feature which depends on it via their
366 * if-feature statements are again evaluated (disabled if a if-feature statemen
367 * evaluates to false).
368 *
Radek Krejci0af46292019-01-11 16:02:31 +0100369 * @param[in] mod Module where to set (search for) the feature.
Radek Krejci151a5b72018-10-19 14:21:44 +0200370 * @param[in] name Name of the feature to set. Asterisk ('*') can be used to
371 * set all the features in the module.
372 * @param[in] value Desired value of the feature: 1 (enable) or 0 (disable).
373 * @return LY_ERR value.
374 */
375static LY_ERR
Radek Krejci0af46292019-01-11 16:02:31 +0100376lys_feature_change(const struct lys_module *mod, const char *name, int value)
Radek Krejci151a5b72018-10-19 14:21:44 +0200377{
378 int all = 0;
Radek Krejcica3db002018-11-01 10:31:01 +0100379 unsigned int u, changed_count, disabled_count;
Radek Krejci151a5b72018-10-19 14:21:44 +0200380 struct lysc_feature *f, **df;
381 struct lysc_iffeature *iff;
382 struct ly_set *changed;
Radek Krejci0af46292019-01-11 16:02:31 +0100383 struct ly_ctx *ctx = mod->ctx; /* shortcut */
Radek Krejci151a5b72018-10-19 14:21:44 +0200384
Radek Krejci6e67c402019-05-02 09:55:39 +0200385 if (!strcmp(name, "*")) {
386 /* enable all */
387 all = 1;
388 }
389
Radek Krejci0af46292019-01-11 16:02:31 +0100390 if (!mod->compiled) {
391 LOGERR(ctx, LY_EINVAL, "Module \"%s\" is not implemented so all its features are permanently disabled without a chance to change it.",
392 mod->name);
393 return LY_EINVAL;
394 }
395 if (!mod->compiled->features) {
Radek Krejci6e67c402019-05-02 09:55:39 +0200396 if (all) {
397 /* no feature to enable */
398 return LY_SUCCESS;
399 }
Radek Krejci0af46292019-01-11 16:02:31 +0100400 LOGERR(ctx, LY_EINVAL, "Unable to switch feature since the module \"%s\" has no features.", mod->name);
Radek Krejci151a5b72018-10-19 14:21:44 +0200401 return LY_EINVAL;
402 }
403
Radek Krejci151a5b72018-10-19 14:21:44 +0200404 changed = ly_set_new();
Radek Krejcica3db002018-11-01 10:31:01 +0100405 changed_count = 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200406
Radek Krejcica3db002018-11-01 10:31:01 +0100407run:
Radek Krejci0af46292019-01-11 16:02:31 +0100408 for (disabled_count = u = 0; u < LY_ARRAY_SIZE(mod->compiled->features); ++u) {
409 f = &mod->compiled->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200410 if (all || !strcmp(f->name, name)) {
411 if ((value && (f->flags & LYS_FENABLED)) || (!value && !(f->flags & LYS_FENABLED))) {
412 if (all) {
413 /* skip already set features */
414 continue;
415 } else {
416 /* feature already set correctly */
417 ly_set_free(changed, NULL);
418 return LY_SUCCESS;
419 }
420 }
421
422 if (value) { /* enable */
423 /* check referenced features if they are enabled */
424 LY_ARRAY_FOR(f->iffeatures, struct lysc_iffeature, iff) {
425 if (!lysc_iffeature_value(iff)) {
426 if (all) {
Radek Krejcica3db002018-11-01 10:31:01 +0100427 ++disabled_count;
Radek Krejci151a5b72018-10-19 14:21:44 +0200428 goto next;
429 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100430 LOGERR(ctx, LY_EDENIED,
Radek Krejci151a5b72018-10-19 14:21:44 +0200431 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
432 f->name);
433 ly_set_free(changed, NULL);
434 return LY_EDENIED;
435 }
436 }
437 }
438 /* enable the feature */
439 f->flags |= LYS_FENABLED;
440 } else { /* disable */
441 /* disable the feature */
442 f->flags &= ~LYS_FENABLED;
443 }
444
445 /* remember the changed feature */
446 ly_set_add(changed, f, LY_SET_OPT_USEASLIST);
447
448 if (!all) {
449 /* stop in case changing a single feature */
450 break;
451 }
452 }
453next:
454 ;
455 }
456
457 if (!all && !changed->count) {
Radek Krejci0af46292019-01-11 16:02:31 +0100458 LOGERR(ctx, LY_EINVAL, "Feature \"%s\" not found in module \"%s\".", name, mod->name);
Radek Krejci151a5b72018-10-19 14:21:44 +0200459 ly_set_free(changed, NULL);
460 return LY_EINVAL;
461 }
462
Radek Krejcica3db002018-11-01 10:31:01 +0100463 if (value && all && disabled_count) {
464 if (changed_count == changed->count) {
465 /* no change in last run -> not able to enable all ... */
466 /* ... print errors */
Radek Krejci0af46292019-01-11 16:02:31 +0100467 for (u = 0; disabled_count && u < LY_ARRAY_SIZE(mod->compiled->features); ++u) {
468 if (!(mod->compiled->features[u].flags & LYS_FENABLED)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100469 LOGERR(ctx, LY_EDENIED,
Radek Krejcica3db002018-11-01 10:31:01 +0100470 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
Radek Krejci0af46292019-01-11 16:02:31 +0100471 mod->compiled->features[u].name);
Radek Krejcica3db002018-11-01 10:31:01 +0100472 --disabled_count;
473 }
474 }
475 /* ... restore the original state */
476 for (u = 0; u < changed->count; ++u) {
477 f = changed->objs[u];
478 /* re-disable the feature */
479 f->flags &= ~LYS_FENABLED;
480 }
481
482 ly_set_free(changed, NULL);
483 return LY_EDENIED;
484 } else {
485 /* we did some change in last run, try it again */
486 changed_count = changed->count;
487 goto run;
488 }
489 }
490
Radek Krejci151a5b72018-10-19 14:21:44 +0200491 /* reflect change(s) in the dependent features */
492 for (u = 0; u < changed->count; ++u) {
493 /* If a dependent feature is enabled, it can be now changed by the change (to false) of the value of
494 * its if-feature statements. The reverse logic, automatically enable feature when its feature is enabled
495 * is not done - by default, features are disabled and must be explicitely enabled. */
496 f = changed->objs[u];
497 LY_ARRAY_FOR(f->depfeatures, struct lysc_feature*, df) {
498 if (!((*df)->flags & LYS_FENABLED)) {
499 /* not enabled, nothing to do */
500 continue;
501 }
502 /* check the feature's if-features which could change by the previous change of our feature */
503 LY_ARRAY_FOR((*df)->iffeatures, struct lysc_iffeature, iff) {
504 if (!lysc_iffeature_value(iff)) {
505 /* the feature must be disabled now */
506 (*df)->flags &= ~LYS_FENABLED;
507 /* add the feature into the list of changed features */
508 ly_set_add(changed, *df, LY_SET_OPT_USEASLIST);
509 break;
510 }
511 }
512 }
513 }
514
515 ly_set_free(changed, NULL);
516 return LY_SUCCESS;
517}
518
519API LY_ERR
Radek Krejcied5acc52019-04-25 15:57:04 +0200520lys_feature_enable(const struct lys_module *module, const char *feature)
Radek Krejci151a5b72018-10-19 14:21:44 +0200521{
Radek Krejci0af46292019-01-11 16:02:31 +0100522 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200523
Radek Krejcied5acc52019-04-25 15:57:04 +0200524 return lys_feature_change((struct lys_module*)module, feature, 1);
Radek Krejci151a5b72018-10-19 14:21:44 +0200525}
526
527API LY_ERR
Radek Krejcied5acc52019-04-25 15:57:04 +0200528lys_feature_disable(const struct lys_module *module, const char *feature)
Radek Krejci151a5b72018-10-19 14:21:44 +0200529{
Radek Krejci0af46292019-01-11 16:02:31 +0100530 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200531
Radek Krejcied5acc52019-04-25 15:57:04 +0200532 return lys_feature_change((struct lys_module*)module, feature, 0);
Radek Krejci151a5b72018-10-19 14:21:44 +0200533}
534
535API int
536lys_feature_value(const struct lys_module *module, const char *feature)
537{
538 struct lysc_feature *f;
539 struct lysc_module *mod;
540 unsigned int u;
541
542 LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, -1);
543 mod = module->compiled;
544
545 /* search for the specified feature */
546 for (u = 0; u < LY_ARRAY_SIZE(mod->features); ++u) {
Radek Krejci2c4e7172018-10-19 15:56:26 +0200547 f = &mod->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200548 if (!strcmp(f->name, feature)) {
549 if (f->flags & LYS_FENABLED) {
550 return 1;
551 } else {
552 return 0;
553 }
554 }
555 }
556
557 /* feature definition not found */
558 return -1;
559}
560
Radek Krejcia3045382018-11-22 14:30:31 +0100561API const struct lysc_iffeature *
562lys_is_disabled(const struct lysc_node *node, int recursive)
563{
564 unsigned int u;
Radek Krejcia3045382018-11-22 14:30:31 +0100565
566 LY_CHECK_ARG_RET(NULL, node, NULL);
567
568 while(node) {
569 if (node->nodetype & LYS_CHOICE) {
570 return NULL;
571 }
572
Radek Krejci056d0a82018-12-06 16:57:25 +0100573 if (node->iffeatures) {
Radek Krejcia3045382018-11-22 14:30:31 +0100574 /* check local if-features */
Radek Krejci056d0a82018-12-06 16:57:25 +0100575 LY_ARRAY_FOR(node->iffeatures, u) {
576 if (!lysc_iffeature_value(&node->iffeatures[u])) {
577 return &node->iffeatures[u];
Radek Krejcia3045382018-11-22 14:30:31 +0100578 }
579 }
580 }
581
582 if (!recursive) {
583 return NULL;
584 }
585
586 /* go through parents */
587 node = node->parent;
588 }
589 return NULL;
590}
591
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100592struct lysp_submodule *
Radek Krejcie7b95092019-05-15 11:03:07 +0200593lys_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 +0100594 LY_ERR (*custom_check)(struct ly_ctx*, struct lysp_module*, struct lysp_submodule*, void*), void *check_data)
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200595{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100596 LY_ERR ret = LY_EINVAL;
597 struct lysp_submodule *submod = NULL, *latest_sp;
David Sedlák1b623122019-08-05 15:27:49 +0200598 struct lys_parser_ctx *context = NULL;
599 struct yin_parser_ctx *yin_context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100600
601 LY_CHECK_ARG_RET(ctx, ctx, data, NULL);
602
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100603 switch (format) {
604 case LYS_IN_YIN:
David Sedlák1b623122019-08-05 15:27:49 +0200605 ret = yin_parse_submodule(&yin_context, ctx, main_ctx, data, &submod);
606 context = (struct lys_parser_ctx *)yin_context;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100607 break;
608 case LYS_IN_YANG:
David Sedlák1b623122019-08-05 15:27:49 +0200609 ret = yang_parse_submodule(&context, ctx, main_ctx, data, &submod);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100610 break;
611 default:
David Sedlák4f2f5ba2019-08-15 13:18:48 +0200612 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100613 break;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200614 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100615 LY_CHECK_RET(ret, NULL);
616
617 /* make sure that the newest revision is at position 0 */
618 lysp_sort_revisions(submod->revs);
619
620 if (custom_check) {
David Sedlák1b623122019-08-05 15:27:49 +0200621 LY_CHECK_GOTO(custom_check((*context).ctx, NULL, submod, check_data), error);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200622 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100623
624 /* decide the latest revision */
David Sedlák1b623122019-08-05 15:27:49 +0200625 latest_sp = ly_ctx_get_submodule((*context).ctx, submod->belongsto, submod->name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100626 if (latest_sp) {
627 if (submod->revs) {
628 if (!latest_sp->revs) {
629 /* latest has no revision, so mod is anyway newer */
630 submod->latest_revision = latest_sp->latest_revision;
631 latest_sp->latest_revision = 0;
632 } else {
633 if (strcmp(submod->revs[0].date, latest_sp->revs[0].date) > 0) {
634 submod->latest_revision = latest_sp->latest_revision;
635 latest_sp->latest_revision = 0;
636 }
637 }
638 }
639 } else {
640 submod->latest_revision = 1;
641 }
642
643 /* remap possibly changed and reallocated typedefs and groupings list back to the main context */
David Sedlák1b623122019-08-05 15:27:49 +0200644 memcpy(&main_ctx->tpdfs_nodes, &(*context).tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
645 memcpy(&main_ctx->grps_nodes, &(*context).grps_nodes, sizeof main_ctx->grps_nodes);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100646
David Sedlák1b623122019-08-05 15:27:49 +0200647 if (format == LYS_IN_YANG) {
648 lys_parser_ctx_free(context);
649 } else {
650 yin_parser_ctx_free(yin_context);
651 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100652 return submod;
David Sedlák1b623122019-08-05 15:27:49 +0200653
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100654error:
655 lysp_submodule_free(ctx, submod);
David Sedlák1b623122019-08-05 15:27:49 +0200656 if (format == LYS_IN_YANG) {
657 lys_parser_ctx_free(context);
658 } else {
659 yin_parser_ctx_free(yin_context);
660 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100661 return NULL;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200662}
663
Radek Krejcid33273d2018-10-25 14:55:52 +0200664struct lys_module *
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100665lys_parse_mem_module(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, int implement,
666 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
667 void *check_data)
Radek Krejci86d106e2018-10-18 09:53:19 +0200668{
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100669 struct lys_module *mod = NULL, *latest, *mod_dup;
Radek Krejci086c7132018-10-26 15:29:04 +0200670 struct lysp_import *imp;
671 struct lysp_include *inc;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100672 LY_ERR ret = LY_EINVAL;
Radek Krejci086c7132018-10-26 15:29:04 +0200673 unsigned int u, i;
David Sedlák1b623122019-08-05 15:27:49 +0200674 struct lys_parser_ctx *context = NULL;
675 struct yin_parser_ctx *yin_context = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +0200676
677 LY_CHECK_ARG_RET(ctx, ctx, data, NULL);
678
679 mod = calloc(1, sizeof *mod);
680 LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100681 mod->ctx = ctx;
Radek Krejci86d106e2018-10-18 09:53:19 +0200682
683 switch (format) {
684 case LYS_IN_YIN:
David Sedlák1b623122019-08-05 15:27:49 +0200685 ret = yin_parse_module(&yin_context, data, mod);
686 context = (struct lys_parser_ctx *)yin_context;
Radek Krejci86d106e2018-10-18 09:53:19 +0200687 break;
688 case LYS_IN_YANG:
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100689 ret = yang_parse_module(&context, data, mod);
Radek Krejci86d106e2018-10-18 09:53:19 +0200690 break;
691 default:
692 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
693 break;
694 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100695 LY_CHECK_ERR_RET(ret, lys_module_free(mod, NULL), NULL);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200696
697 /* make sure that the newest revision is at position 0 */
698 lysp_sort_revisions(mod->parsed->revs);
Radek Krejci0af46292019-01-11 16:02:31 +0100699 if (mod->parsed->revs) {
700 mod->revision = lydict_insert(ctx, mod->parsed->revs[0].date, 0);
701 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200702
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100703 if (custom_check) {
704 LY_CHECK_GOTO(custom_check(ctx, mod->parsed, NULL, check_data), error);
705 }
706
Radek Krejci86d106e2018-10-18 09:53:19 +0200707 if (implement) {
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200708 /* mark the loaded module implemented */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100709 if (ly_ctx_get_module_implemented(ctx, mod->name)) {
710 LOGERR(ctx, LY_EDENIED, "Module \"%s\" is already implemented in the context.", mod->name);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100711 goto error;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200712 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100713 mod->implemented = 1;
Radek Krejci86d106e2018-10-18 09:53:19 +0200714 }
715
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100716 /* check for duplicity in the context */
Radek Krejci0af46292019-01-11 16:02:31 +0100717 mod_dup = (struct lys_module*)ly_ctx_get_module(ctx, mod->name, mod->revision);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100718 if (mod_dup) {
719 if (mod_dup->parsed) {
720 /* error */
Radek Krejcid33273d2018-10-25 14:55:52 +0200721 if (mod->parsed->revs) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100722 LOGERR(ctx, LY_EEXIST, "Module \"%s\" of revision \"%s\" is already present in the context.",
723 mod->name, mod->parsed->revs[0].date);
Radek Krejcid33273d2018-10-25 14:55:52 +0200724 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100725 LOGERR(ctx, LY_EEXIST, "Module \"%s\" with no revision is already present in the context.",
726 mod->name);
Radek Krejcid33273d2018-10-25 14:55:52 +0200727 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100728 goto error;
729 } else {
730 /* add the parsed data to the currently compiled-only module in the context */
731 mod_dup->parsed = mod->parsed;
732 mod_dup->parsed->mod = mod_dup;
733 mod->parsed = NULL;
734 lys_module_free(mod, NULL);
735 mod = mod_dup;
736 goto finish_parsing;
Radek Krejcid33273d2018-10-25 14:55:52 +0200737 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100738 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200739
740#if 0
741 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
742 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
743 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
744 * the anotation definitions available in the internal schema structure. There is another hack in schema
745 * printers to do not print this internally added annotation. */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100746 if (ly_strequal(mod->name, "ietf-netconf", 0)) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200747 if (lyp_add_ietf_netconf_annotations(mod)) {
748 lys_free(mod, NULL, 1, 1);
749 return NULL;
750 }
751 }
752#endif
753
Radek Krejci0af46292019-01-11 16:02:31 +0100754 if (!mod->implemented) {
Radek Krejci0935f412019-08-20 16:15:18 +0200755 /* pre-compile features and extension definitions of the module */
Radek Krejci327de162019-06-14 12:52:07 +0200756 LY_CHECK_GOTO(lys_feature_precompile(NULL, ctx, mod, mod->parsed->features, &mod->off_features), error);
Radek Krejci0935f412019-08-20 16:15:18 +0200757 LY_CHECK_GOTO(lys_extension_precompile(NULL, ctx, mod, mod->parsed->extensions, &mod->off_extensions), error);
Radek Krejci0af46292019-01-11 16:02:31 +0100758 }
759
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100760 /* decide the latest revision */
761 latest = (struct lys_module*)ly_ctx_get_module_latest(ctx, mod->name);
762 if (latest) {
Radek Krejci0af46292019-01-11 16:02:31 +0100763 if (mod->revision) {
764 if (!latest->revision) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100765 /* latest has no revision, so mod is anyway newer */
766 mod->latest_revision = latest->latest_revision;
767 latest->latest_revision = 0;
Radek Krejci0af46292019-01-11 16:02:31 +0100768 } else if (strcmp(mod->revision, latest->revision) > 0) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100769 mod->latest_revision = latest->latest_revision;
770 latest->latest_revision = 0;
Radek Krejcid33273d2018-10-25 14:55:52 +0200771 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200772 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100773 } else {
774 mod->latest_revision = 1;
775 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200776
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100777 /* add into context */
778 ly_set_add(&ctx->list, mod, LY_SET_OPT_USEASLIST);
Radek Krejcid33273d2018-10-25 14:55:52 +0200779
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100780finish_parsing:
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100781 /* resolve imports */
782 mod->parsed->parsing = 1;
783 LY_ARRAY_FOR(mod->parsed->imports, u) {
784 imp = &mod->parsed->imports[u];
785 if (!imp->module && lysp_load_module(ctx, imp->name, imp->rev[0] ? imp->rev : NULL, 0, 0, &imp->module)) {
786 goto error_ctx;
Radek Krejci086c7132018-10-26 15:29:04 +0200787 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100788 /* check for importing the same module twice */
789 for (i = 0; i < u; ++i) {
790 if (imp->module == mod->parsed->imports[i].module) {
791 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 +0100792 goto error_ctx;
Radek Krejci086c7132018-10-26 15:29:04 +0200793 }
794 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200795 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100796 LY_ARRAY_FOR(mod->parsed->includes, u) {
797 inc = &mod->parsed->includes[u];
David Sedlák1b623122019-08-05 15:27:49 +0200798 if (!inc->submodule && lysp_load_submodule(context, mod->parsed, inc)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100799 goto error_ctx;
800 }
Radek Krejci0af46292019-01-11 16:02:31 +0100801 if (!mod->implemented) {
Radek Krejci0935f412019-08-20 16:15:18 +0200802 /* pre-compile features and extension definitions of the module */
Radek Krejci327de162019-06-14 12:52:07 +0200803 LY_CHECK_GOTO(lys_feature_precompile(NULL, ctx, mod, inc->submodule->features, &mod->off_features), error);
Radek Krejci0935f412019-08-20 16:15:18 +0200804 LY_CHECK_GOTO(lys_extension_precompile(NULL, ctx, mod, mod->parsed->extensions, &mod->off_extensions), error);
Radek Krejci0af46292019-01-11 16:02:31 +0100805 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100806 }
807 mod->parsed->parsing = 0;
808
Radek Krejci7fc68292019-06-12 13:51:09 +0200809 /* check name collisions - typedefs and TODO groupings */
David Sedlák1b623122019-08-05 15:27:49 +0200810 LY_CHECK_GOTO(lysp_check_typedefs(context, mod->parsed), error_ctx);
Radek Krejcid33273d2018-10-25 14:55:52 +0200811
David Sedlák1b623122019-08-05 15:27:49 +0200812 if (format == LYS_IN_YANG) {
813 lys_parser_ctx_free(context);
814 } else {
815 yin_parser_ctx_free(yin_context);
816 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200817 return mod;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100818
819error_ctx:
820 ly_set_rm(&ctx->list, mod, NULL);
821error:
822 lys_module_free(mod, NULL);
David Sedlák1b623122019-08-05 15:27:49 +0200823 ly_set_erase(&context->tpdfs_nodes, NULL);
824 if (format == LYS_IN_YANG) {
825 lys_parser_ctx_free(context);
826 } else {
827 yin_parser_ctx_free(yin_context);
828 }
829
Radek Krejcibbe09a92018-11-08 09:36:54 +0100830 return NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +0200831}
832
Radek Krejcid14e9692018-11-01 11:00:37 +0100833API struct lys_module *
Radek Krejci86d106e2018-10-18 09:53:19 +0200834lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format)
835{
Radek Krejci096235c2019-01-11 11:12:19 +0100836 struct lys_module *mod;
837
838 mod = lys_parse_mem_module(ctx, data, format, 1, NULL, NULL);
839 LY_CHECK_RET(!mod, NULL);
840
841 if (lys_compile(mod, 0)) {
842 ly_set_rm(&ctx->list, mod, NULL);
843 lys_module_free(mod, NULL);
844 return NULL;
845 }
846 return mod;
Radek Krejci86d106e2018-10-18 09:53:19 +0200847}
848
849static void
850lys_parse_set_filename(struct ly_ctx *ctx, const char **filename, int fd)
851{
Radek Krejci65639b92018-11-27 10:51:37 +0100852 char path[PATH_MAX];
Radek Krejci86d106e2018-10-18 09:53:19 +0200853
854#ifdef __APPLE__
855 if (fcntl(fd, F_GETPATH, path) != -1) {
856 *filename = lydict_insert(ctx, path, 0);
857 }
858#else
Radek Krejci65639b92018-11-27 10:51:37 +0100859 int len;
860 char proc_path[32];
861
Radek Krejci86d106e2018-10-18 09:53:19 +0200862 /* get URI if there is /proc */
863 sprintf(proc_path, "/proc/self/fd/%d", fd);
864 if ((len = readlink(proc_path, path, PATH_MAX - 1)) > 0) {
865 *filename = lydict_insert(ctx, path, len);
866 }
867#endif
868}
869
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100870void *
Radek Krejcie7b95092019-05-15 11:03:07 +0200871lys_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 +0100872 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
873 void *check_data)
Radek Krejci86d106e2018-10-18 09:53:19 +0200874{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100875 void *result;
876 struct lys_module *mod = NULL;
877 struct lysp_submodule *submod = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +0200878 size_t length;
879 char *addr;
880
881 LY_CHECK_ARG_RET(ctx, ctx, NULL);
882 if (fd < 0) {
883 LOGARG(ctx, fd);
884 return NULL;
885 }
886
887 LY_CHECK_RET(ly_mmap(ctx, fd, &length, (void **)&addr), NULL);
888 if (!addr) {
889 LOGERR(ctx, LY_EINVAL, "Empty schema file.");
890 return NULL;
891 }
892
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100893 if (main_ctx) {
894 result = submod = lys_parse_mem_submodule(ctx, addr, format, main_ctx, custom_check, check_data);
895 } else {
896 result = mod = lys_parse_mem_module(ctx, addr, format, implement, custom_check, check_data);
Radek Krejci096235c2019-01-11 11:12:19 +0100897 if (mod && implement && lys_compile(mod, 0)) {
898 ly_set_rm(&ctx->list, mod, NULL);
899 lys_module_free(mod, NULL);
900 result = mod = NULL;
901 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100902 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200903 ly_munmap(addr, length);
904
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100905 if (mod && !mod->filepath) {
906 lys_parse_set_filename(ctx, &mod->filepath, fd);
907 } else if (submod && !submod->filepath) {
908 lys_parse_set_filename(ctx, &submod->filepath, fd);
Radek Krejci86d106e2018-10-18 09:53:19 +0200909 }
910
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100911 return result;
912}
913
914struct lys_module *
915lys_parse_fd_module(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, int implement,
916 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
917 void *check_data)
918{
919 return (struct lys_module*)lys_parse_fd_(ctx, fd, format, implement, NULL, custom_check, check_data);
920}
921
922struct lysp_submodule *
Radek Krejcie7b95092019-05-15 11:03:07 +0200923lys_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 +0100924 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
925 void *check_data)
926{
927 assert(main_ctx);
928 return (struct lysp_submodule*)lys_parse_fd_(ctx, fd, format, 0, main_ctx, custom_check, check_data);
Radek Krejci86d106e2018-10-18 09:53:19 +0200929}
930
Radek Krejcid14e9692018-11-01 11:00:37 +0100931API struct lys_module *
Radek Krejci86d106e2018-10-18 09:53:19 +0200932lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format)
933{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100934 return lys_parse_fd_module(ctx, fd, format, 1, NULL, NULL);
Radek Krejci86d106e2018-10-18 09:53:19 +0200935}
936
Radek Krejcid33273d2018-10-25 14:55:52 +0200937struct lys_module *
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100938lys_parse_path_(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, int implement,
939 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 +0200940{
941 int fd;
Radek Krejcid33273d2018-10-25 14:55:52 +0200942 struct lys_module *mod;
Radek Krejci86d106e2018-10-18 09:53:19 +0200943 const char *rev, *dot, *filename;
944 size_t len;
945
946 LY_CHECK_ARG_RET(ctx, ctx, path, NULL);
947
948 fd = open(path, O_RDONLY);
949 LY_CHECK_ERR_RET(fd == -1, LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)), NULL);
950
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100951 mod = lys_parse_fd_module(ctx, fd, format, implement, custom_check, check_data);
Radek Krejci86d106e2018-10-18 09:53:19 +0200952 close(fd);
953 LY_CHECK_RET(!mod, NULL);
954
955 /* check that name and revision match filename */
956 filename = strrchr(path, '/');
957 if (!filename) {
958 filename = path;
959 } else {
960 filename++;
961 }
962 rev = strchr(filename, '@');
963 dot = strrchr(filename, '.');
964
965 /* name */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100966 len = strlen(mod->name);
967 if (strncmp(filename, mod->name, len) ||
Radek Krejci86d106e2018-10-18 09:53:19 +0200968 ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100969 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name);
Radek Krejci86d106e2018-10-18 09:53:19 +0200970 }
971 if (rev) {
972 len = dot - ++rev;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200973 if (!mod->parsed->revs || len != 10 || strncmp(mod->parsed->revs[0].date, rev, len)) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200974 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Radek Krejcib7db73a2018-10-24 14:18:40 +0200975 mod->parsed->revs ? mod->parsed->revs[0].date : "none");
Radek Krejci86d106e2018-10-18 09:53:19 +0200976 }
977 }
978
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100979 if (!mod->filepath) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200980 /* store URI */
981 char rpath[PATH_MAX];
982 if (realpath(path, rpath) != NULL) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100983 mod->filepath = lydict_insert(ctx, rpath, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +0200984 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100985 mod->filepath = lydict_insert(ctx, path, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +0200986 }
987 }
988
989 return mod;
990}
991
Radek Krejcid14e9692018-11-01 11:00:37 +0100992API struct lys_module *
Radek Krejcid33273d2018-10-25 14:55:52 +0200993lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format)
994{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100995 return lys_parse_path_(ctx, path, format, 1, NULL, NULL);
Radek Krejcid33273d2018-10-25 14:55:52 +0200996}
997
998API LY_ERR
999lys_search_localfile(const char * const *searchpaths, int cwd, const char *name, const char *revision,
1000 char **localfile, LYS_INFORMAT *format)
1001{
1002 size_t len, flen, match_len = 0, dir_len;
1003 int i, implicit_cwd = 0, ret = EXIT_FAILURE;
1004 char *wd, *wn = NULL;
1005 DIR *dir = NULL;
1006 struct dirent *file;
1007 char *match_name = NULL;
1008 LYS_INFORMAT format_aux, match_format = 0;
1009 struct ly_set *dirs;
1010 struct stat st;
1011
1012 LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL);
1013
1014 /* start to fill the dir fifo with the context's search path (if set)
1015 * and the current working directory */
1016 dirs = ly_set_new();
1017 if (!dirs) {
1018 LOGMEM(NULL);
1019 return EXIT_FAILURE;
1020 }
1021
1022 len = strlen(name);
1023 if (cwd) {
1024 wd = get_current_dir_name();
1025 if (!wd) {
1026 LOGMEM(NULL);
1027 goto cleanup;
1028 } else {
1029 /* add implicit current working directory (./) to be searched,
1030 * this directory is not searched recursively */
1031 if (ly_set_add(dirs, wd, 0) == -1) {
1032 goto cleanup;
1033 }
1034 implicit_cwd = 1;
1035 }
1036 }
1037 if (searchpaths) {
1038 for (i = 0; searchpaths[i]; i++) {
1039 /* check for duplicities with the implicit current working directory */
1040 if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) {
1041 implicit_cwd = 0;
1042 continue;
1043 }
1044 wd = strdup(searchpaths[i]);
1045 if (!wd) {
1046 LOGMEM(NULL);
1047 goto cleanup;
1048 } else if (ly_set_add(dirs, wd, 0) == -1) {
1049 goto cleanup;
1050 }
1051 }
1052 }
1053 wd = NULL;
1054
1055 /* start searching */
1056 while (dirs->count) {
1057 free(wd);
1058 free(wn); wn = NULL;
1059
1060 dirs->count--;
1061 wd = (char *)dirs->objs[dirs->count];
1062 dirs->objs[dirs->count] = NULL;
1063 LOGVRB("Searching for \"%s\" in %s.", name, wd);
1064
1065 if (dir) {
1066 closedir(dir);
1067 }
1068 dir = opendir(wd);
1069 dir_len = strlen(wd);
1070 if (!dir) {
1071 LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno));
1072 } else {
1073 while ((file = readdir(dir))) {
1074 if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) {
1075 /* skip . and .. */
1076 continue;
1077 }
1078 free(wn);
1079 if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) {
1080 LOGMEM(NULL);
1081 goto cleanup;
1082 }
1083 if (stat(wn, &st) == -1) {
1084 LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)",
1085 file->d_name, wd, strerror(errno));
1086 continue;
1087 }
1088 if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) {
1089 /* we have another subdirectory in searchpath to explore,
1090 * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */
1091 if (ly_set_add(dirs, wn, 0) == -1) {
1092 goto cleanup;
1093 }
1094 /* continue with the next item in current directory */
1095 wn = NULL;
1096 continue;
1097 } else if (!S_ISREG(st.st_mode)) {
1098 /* not a regular file (note that we see the target of symlinks instead of symlinks */
1099 continue;
1100 }
1101
1102 /* here we know that the item is a file which can contain a module */
1103 if (strncmp(name, file->d_name, len) ||
1104 (file->d_name[len] != '.' && file->d_name[len] != '@')) {
1105 /* different filename than the module we search for */
1106 continue;
1107 }
1108
1109 /* get type according to filename suffix */
1110 flen = strlen(file->d_name);
Radek Krejcied5acc52019-04-25 15:57:04 +02001111 if (!strcmp(&file->d_name[flen - 5], ".yang")) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001112 format_aux = LYS_IN_YANG;
Radek Krejcied5acc52019-04-25 15:57:04 +02001113 /* TODO YIN parser
1114 } else if (!strcmp(&file->d_name[flen - 4], ".yin")) {
1115 format_aux = LYS_IN_YIN;
1116 */
Radek Krejcid33273d2018-10-25 14:55:52 +02001117 } else {
1118 /* not supportde suffix/file format */
1119 continue;
1120 }
1121
1122 if (revision) {
1123 /* we look for the specific revision, try to get it from the filename */
1124 if (file->d_name[len] == '@') {
1125 /* check revision from the filename */
1126 if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) {
1127 /* another revision */
1128 continue;
1129 } else {
1130 /* exact revision */
1131 free(match_name);
1132 match_name = wn;
1133 wn = NULL;
1134 match_len = dir_len + 1 + len;
1135 match_format = format_aux;
1136 goto success;
1137 }
1138 } else {
1139 /* continue trying to find exact revision match, use this only if not found */
1140 free(match_name);
1141 match_name = wn;
1142 wn = NULL;
1143 match_len = dir_len + 1 +len;
1144 match_format = format_aux;
1145 continue;
1146 }
1147 } else {
1148 /* remember the revision and try to find the newest one */
1149 if (match_name) {
1150 if (file->d_name[len] != '@' ||
1151 lysp_check_date(NULL, &file->d_name[len + 1], flen - (format_aux == LYS_IN_YANG ? 5 : 4) - len - 1, NULL)) {
1152 continue;
1153 } else if (match_name[match_len] == '@' &&
1154 (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) {
1155 continue;
1156 }
1157 free(match_name);
1158 }
1159
1160 match_name = wn;
1161 wn = NULL;
1162 match_len = dir_len + 1 + len;
1163 match_format = format_aux;
1164 continue;
1165 }
1166 }
1167 }
1168 }
1169
1170success:
1171 (*localfile) = match_name;
1172 match_name = NULL;
1173 if (format) {
1174 (*format) = match_format;
1175 }
1176 ret = EXIT_SUCCESS;
1177
1178cleanup:
1179 free(wn);
1180 free(wd);
1181 if (dir) {
1182 closedir(dir);
1183 }
1184 free(match_name);
1185 ly_set_free(dirs, free);
1186
1187 return ret;
1188}
1189