blob: 61ca70e311c799cf44e508dc5eecc8000c83fe97 [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
228 switch (pathtype) {
229 case LY_PATH_LOG:
230 for (iter = node; iter && len >= 0; iter = iter->parent) {
Radek Krejci1c0c3442019-07-23 16:08:47 +0200231 char *s = buffer ? strdup(buffer) : path;
Radek Krejci327de162019-06-14 12:52:07 +0200232 char *id;
233
234 switch (iter->nodetype) {
235 case LYS_USES:
236 asprintf(&id, "{uses='%s'}", iter->name);
237 break;
238 case LYS_GROUPING:
239 asprintf(&id, "{grouping='%s'}", iter->name);
240 break;
241 case LYS_AUGMENT:
242 asprintf(&id, "{augment='%s'}", iter->name);
243 break;
244 default:
245 id = strdup(iter->name);
246 break;
247 }
248
249 if (!iter->parent || iter->parent->module != iter->module) {
250 /* print prefix */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200251 if (buffer) {
252 len = snprintf(buffer, buflen, "/%s:%s%s", iter->module->name, id, s ? s : "");
253 } else {
254 len = asprintf(&path, "/%s:%s%s", iter->module->name, id, s ? s : "");
255 }
Radek Krejci327de162019-06-14 12:52:07 +0200256 } else {
257 /* prefix is the same as in parent */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200258 if (buffer) {
259 len = snprintf(buffer, buflen, "/%s%s", id, s ? s : "");
260 } else {
261 len = asprintf(&path, "/%s%s", id, s ? s : "");
262 }
Radek Krejci327de162019-06-14 12:52:07 +0200263 }
264 free(s);
265 free(id);
Radek Krejci1c0c3442019-07-23 16:08:47 +0200266
267 if (buffer && buflen <= (size_t)len) {
268 /* not enough space in buffer */
269 break;
270 }
Radek Krejci327de162019-06-14 12:52:07 +0200271 }
272
273 if (len < 0) {
274 free(path);
275 path = NULL;
276 } else if (len == 0) {
277 path = strdup("/");
278 len = 1;
279 }
280 break;
281 }
282
Radek Krejci1c0c3442019-07-23 16:08:47 +0200283 if (buffer) {
284 return buffer;
285 } else {
286 return path;
287 }
Radek Krejci327de162019-06-14 12:52:07 +0200288}
289
Radek Krejci19a96102018-11-15 13:38:09 +0100290API int
291lysc_feature_value(const struct lysc_feature *feature)
Radek Krejci6f7feb62018-10-12 15:23:02 +0200292{
Radek Krejci19a96102018-11-15 13:38:09 +0100293 LY_CHECK_ARG_RET(NULL, feature, -1);
294 return feature->flags & LYS_FENABLED ? 1 : 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200295}
296
Radek Krejci693262f2019-04-29 15:23:20 +0200297uint8_t
298lysc_iff_getop(uint8_t *list, int pos)
Radek Krejci151a5b72018-10-19 14:21:44 +0200299{
300 uint8_t *item;
301 uint8_t mask = 3, result;
302
303 assert(pos >= 0);
304
305 item = &list[pos / 4];
306 result = (*item) & (mask << 2 * (pos % 4));
307 return result >> 2 * (pos % 4);
308}
309
Radek Krejci151a5b72018-10-19 14:21:44 +0200310static int
311lysc_iffeature_value_(const struct lysc_iffeature *iff, int *index_e, int *index_f)
312{
313 uint8_t op;
314 int a, b;
315
Radek Krejci693262f2019-04-29 15:23:20 +0200316 op = lysc_iff_getop(iff->expr, *index_e);
Radek Krejci151a5b72018-10-19 14:21:44 +0200317 (*index_e)++;
318
319 switch (op) {
320 case LYS_IFF_F:
321 /* resolve feature */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200322 return lysc_feature_value(iff->features[(*index_f)++]);
Radek Krejci151a5b72018-10-19 14:21:44 +0200323 case LYS_IFF_NOT:
324 /* invert result */
325 return lysc_iffeature_value_(iff, index_e, index_f) ? 0 : 1;
326 case LYS_IFF_AND:
327 case LYS_IFF_OR:
328 a = lysc_iffeature_value_(iff, index_e, index_f);
329 b = lysc_iffeature_value_(iff, index_e, index_f);
330 if (op == LYS_IFF_AND) {
331 return a && b;
332 } else { /* LYS_IFF_OR */
333 return a || b;
334 }
335 }
336
337 return 0;
338}
339
340API int
341lysc_iffeature_value(const struct lysc_iffeature *iff)
342{
343 int index_e = 0, index_f = 0;
344
345 LY_CHECK_ARG_RET(NULL, iff, -1);
346
347 if (iff->expr) {
348 return lysc_iffeature_value_(iff, &index_e, &index_f);
349 }
350 return 0;
351}
352
Radek Krejci151a5b72018-10-19 14:21:44 +0200353/**
354 * @brief Enable/Disable the specified feature in the module.
355 *
356 * If the feature is already set to the desired value, LY_SUCCESS is returned.
357 * By changing the feature, also all the feature which depends on it via their
358 * if-feature statements are again evaluated (disabled if a if-feature statemen
359 * evaluates to false).
360 *
Radek Krejci0af46292019-01-11 16:02:31 +0100361 * @param[in] mod Module where to set (search for) the feature.
Radek Krejci151a5b72018-10-19 14:21:44 +0200362 * @param[in] name Name of the feature to set. Asterisk ('*') can be used to
363 * set all the features in the module.
364 * @param[in] value Desired value of the feature: 1 (enable) or 0 (disable).
365 * @return LY_ERR value.
366 */
367static LY_ERR
Radek Krejci0af46292019-01-11 16:02:31 +0100368lys_feature_change(const struct lys_module *mod, const char *name, int value)
Radek Krejci151a5b72018-10-19 14:21:44 +0200369{
370 int all = 0;
Radek Krejcica3db002018-11-01 10:31:01 +0100371 unsigned int u, changed_count, disabled_count;
Radek Krejci151a5b72018-10-19 14:21:44 +0200372 struct lysc_feature *f, **df;
373 struct lysc_iffeature *iff;
374 struct ly_set *changed;
Radek Krejci0af46292019-01-11 16:02:31 +0100375 struct ly_ctx *ctx = mod->ctx; /* shortcut */
Radek Krejci151a5b72018-10-19 14:21:44 +0200376
Radek Krejci6e67c402019-05-02 09:55:39 +0200377 if (!strcmp(name, "*")) {
378 /* enable all */
379 all = 1;
380 }
381
Radek Krejci0af46292019-01-11 16:02:31 +0100382 if (!mod->compiled) {
383 LOGERR(ctx, LY_EINVAL, "Module \"%s\" is not implemented so all its features are permanently disabled without a chance to change it.",
384 mod->name);
385 return LY_EINVAL;
386 }
387 if (!mod->compiled->features) {
Radek Krejci6e67c402019-05-02 09:55:39 +0200388 if (all) {
389 /* no feature to enable */
390 return LY_SUCCESS;
391 }
Radek Krejci0af46292019-01-11 16:02:31 +0100392 LOGERR(ctx, LY_EINVAL, "Unable to switch feature since the module \"%s\" has no features.", mod->name);
Radek Krejci151a5b72018-10-19 14:21:44 +0200393 return LY_EINVAL;
394 }
395
Radek Krejci151a5b72018-10-19 14:21:44 +0200396 changed = ly_set_new();
Radek Krejcica3db002018-11-01 10:31:01 +0100397 changed_count = 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200398
Radek Krejcica3db002018-11-01 10:31:01 +0100399run:
Radek Krejci0af46292019-01-11 16:02:31 +0100400 for (disabled_count = u = 0; u < LY_ARRAY_SIZE(mod->compiled->features); ++u) {
401 f = &mod->compiled->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200402 if (all || !strcmp(f->name, name)) {
403 if ((value && (f->flags & LYS_FENABLED)) || (!value && !(f->flags & LYS_FENABLED))) {
404 if (all) {
405 /* skip already set features */
406 continue;
407 } else {
408 /* feature already set correctly */
409 ly_set_free(changed, NULL);
410 return LY_SUCCESS;
411 }
412 }
413
414 if (value) { /* enable */
415 /* check referenced features if they are enabled */
416 LY_ARRAY_FOR(f->iffeatures, struct lysc_iffeature, iff) {
417 if (!lysc_iffeature_value(iff)) {
418 if (all) {
Radek Krejcica3db002018-11-01 10:31:01 +0100419 ++disabled_count;
Radek Krejci151a5b72018-10-19 14:21:44 +0200420 goto next;
421 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100422 LOGERR(ctx, LY_EDENIED,
Radek Krejci151a5b72018-10-19 14:21:44 +0200423 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
424 f->name);
425 ly_set_free(changed, NULL);
426 return LY_EDENIED;
427 }
428 }
429 }
430 /* enable the feature */
431 f->flags |= LYS_FENABLED;
432 } else { /* disable */
433 /* disable the feature */
434 f->flags &= ~LYS_FENABLED;
435 }
436
437 /* remember the changed feature */
438 ly_set_add(changed, f, LY_SET_OPT_USEASLIST);
439
440 if (!all) {
441 /* stop in case changing a single feature */
442 break;
443 }
444 }
445next:
446 ;
447 }
448
449 if (!all && !changed->count) {
Radek Krejci0af46292019-01-11 16:02:31 +0100450 LOGERR(ctx, LY_EINVAL, "Feature \"%s\" not found in module \"%s\".", name, mod->name);
Radek Krejci151a5b72018-10-19 14:21:44 +0200451 ly_set_free(changed, NULL);
452 return LY_EINVAL;
453 }
454
Radek Krejcica3db002018-11-01 10:31:01 +0100455 if (value && all && disabled_count) {
456 if (changed_count == changed->count) {
457 /* no change in last run -> not able to enable all ... */
458 /* ... print errors */
Radek Krejci0af46292019-01-11 16:02:31 +0100459 for (u = 0; disabled_count && u < LY_ARRAY_SIZE(mod->compiled->features); ++u) {
460 if (!(mod->compiled->features[u].flags & LYS_FENABLED)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100461 LOGERR(ctx, LY_EDENIED,
Radek Krejcica3db002018-11-01 10:31:01 +0100462 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
Radek Krejci0af46292019-01-11 16:02:31 +0100463 mod->compiled->features[u].name);
Radek Krejcica3db002018-11-01 10:31:01 +0100464 --disabled_count;
465 }
466 }
467 /* ... restore the original state */
468 for (u = 0; u < changed->count; ++u) {
469 f = changed->objs[u];
470 /* re-disable the feature */
471 f->flags &= ~LYS_FENABLED;
472 }
473
474 ly_set_free(changed, NULL);
475 return LY_EDENIED;
476 } else {
477 /* we did some change in last run, try it again */
478 changed_count = changed->count;
479 goto run;
480 }
481 }
482
Radek Krejci151a5b72018-10-19 14:21:44 +0200483 /* reflect change(s) in the dependent features */
484 for (u = 0; u < changed->count; ++u) {
485 /* If a dependent feature is enabled, it can be now changed by the change (to false) of the value of
486 * its if-feature statements. The reverse logic, automatically enable feature when its feature is enabled
487 * is not done - by default, features are disabled and must be explicitely enabled. */
488 f = changed->objs[u];
489 LY_ARRAY_FOR(f->depfeatures, struct lysc_feature*, df) {
490 if (!((*df)->flags & LYS_FENABLED)) {
491 /* not enabled, nothing to do */
492 continue;
493 }
494 /* check the feature's if-features which could change by the previous change of our feature */
495 LY_ARRAY_FOR((*df)->iffeatures, struct lysc_iffeature, iff) {
496 if (!lysc_iffeature_value(iff)) {
497 /* the feature must be disabled now */
498 (*df)->flags &= ~LYS_FENABLED;
499 /* add the feature into the list of changed features */
500 ly_set_add(changed, *df, LY_SET_OPT_USEASLIST);
501 break;
502 }
503 }
504 }
505 }
506
507 ly_set_free(changed, NULL);
508 return LY_SUCCESS;
509}
510
511API LY_ERR
Radek Krejcied5acc52019-04-25 15:57:04 +0200512lys_feature_enable(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, 1);
Radek Krejci151a5b72018-10-19 14:21:44 +0200517}
518
519API LY_ERR
Radek Krejcied5acc52019-04-25 15:57:04 +0200520lys_feature_disable(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, 0);
Radek Krejci151a5b72018-10-19 14:21:44 +0200525}
526
527API int
528lys_feature_value(const struct lys_module *module, const char *feature)
529{
530 struct lysc_feature *f;
531 struct lysc_module *mod;
532 unsigned int u;
533
534 LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, -1);
535 mod = module->compiled;
536
537 /* search for the specified feature */
538 for (u = 0; u < LY_ARRAY_SIZE(mod->features); ++u) {
Radek Krejci2c4e7172018-10-19 15:56:26 +0200539 f = &mod->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200540 if (!strcmp(f->name, feature)) {
541 if (f->flags & LYS_FENABLED) {
542 return 1;
543 } else {
544 return 0;
545 }
546 }
547 }
548
549 /* feature definition not found */
550 return -1;
551}
552
Radek Krejcia3045382018-11-22 14:30:31 +0100553API const struct lysc_iffeature *
554lys_is_disabled(const struct lysc_node *node, int recursive)
555{
556 unsigned int u;
Radek Krejcia3045382018-11-22 14:30:31 +0100557
558 LY_CHECK_ARG_RET(NULL, node, NULL);
559
560 while(node) {
561 if (node->nodetype & LYS_CHOICE) {
562 return NULL;
563 }
564
Radek Krejci056d0a82018-12-06 16:57:25 +0100565 if (node->iffeatures) {
Radek Krejcia3045382018-11-22 14:30:31 +0100566 /* check local if-features */
Radek Krejci056d0a82018-12-06 16:57:25 +0100567 LY_ARRAY_FOR(node->iffeatures, u) {
568 if (!lysc_iffeature_value(&node->iffeatures[u])) {
569 return &node->iffeatures[u];
Radek Krejcia3045382018-11-22 14:30:31 +0100570 }
571 }
572 }
573
574 if (!recursive) {
575 return NULL;
576 }
577
578 /* go through parents */
579 node = node->parent;
580 }
581 return NULL;
582}
583
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100584struct lysp_submodule *
Radek Krejcie7b95092019-05-15 11:03:07 +0200585lys_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 +0100586 LY_ERR (*custom_check)(struct ly_ctx*, struct lysp_module*, struct lysp_submodule*, void*), void *check_data)
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200587{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100588 LY_ERR ret = LY_EINVAL;
589 struct lysp_submodule *submod = NULL, *latest_sp;
Radek Krejcie7b95092019-05-15 11:03:07 +0200590 struct lys_parser_ctx context = {0};
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100591
592 LY_CHECK_ARG_RET(ctx, ctx, data, NULL);
593
594 context.ctx = ctx;
595 context.line = 1;
596
597 /* map the typedefs and groupings list from main context to the submodule's context */
598 memcpy(&context.tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
599 memcpy(&context.grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
600
601 switch (format) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200602 /* TODO not yet supported
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100603 case LYS_IN_YIN:
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100604 mod = yin_read_module();
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100605 break;
Radek Krejcied5acc52019-04-25 15:57:04 +0200606 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100607 case LYS_IN_YANG:
608 ret = yang_parse_submodule(&context, data, &submod);
609 break;
610 default:
611 LOGERR(context.ctx, LY_EINVAL, "Invalid schema input format.");
612 break;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200613 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100614 LY_CHECK_RET(ret, NULL);
615
616 /* make sure that the newest revision is at position 0 */
617 lysp_sort_revisions(submod->revs);
618
619 if (custom_check) {
620 LY_CHECK_GOTO(custom_check(context.ctx, NULL, submod, check_data), error);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200621 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100622
623 /* decide the latest revision */
624 latest_sp = ly_ctx_get_submodule(context.ctx, submod->belongsto, submod->name, NULL);
625 if (latest_sp) {
626 if (submod->revs) {
627 if (!latest_sp->revs) {
628 /* latest has no revision, so mod is anyway newer */
629 submod->latest_revision = latest_sp->latest_revision;
630 latest_sp->latest_revision = 0;
631 } else {
632 if (strcmp(submod->revs[0].date, latest_sp->revs[0].date) > 0) {
633 submod->latest_revision = latest_sp->latest_revision;
634 latest_sp->latest_revision = 0;
635 }
636 }
637 }
638 } else {
639 submod->latest_revision = 1;
640 }
641
642 /* remap possibly changed and reallocated typedefs and groupings list back to the main context */
643 memcpy(&main_ctx->tpdfs_nodes, &context.tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
644 memcpy(&main_ctx->grps_nodes, &context.grps_nodes, sizeof main_ctx->grps_nodes);
645
646 return submod;
647error:
648 lysp_submodule_free(ctx, submod);
649 return NULL;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200650}
651
Radek Krejcid33273d2018-10-25 14:55:52 +0200652struct lys_module *
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100653lys_parse_mem_module(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, int implement,
654 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
655 void *check_data)
Radek Krejci86d106e2018-10-18 09:53:19 +0200656{
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100657 struct lys_module *mod = NULL, *latest, *mod_dup;
Radek Krejci086c7132018-10-26 15:29:04 +0200658 struct lysp_import *imp;
659 struct lysp_include *inc;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100660 LY_ERR ret = LY_EINVAL;
Radek Krejci086c7132018-10-26 15:29:04 +0200661 unsigned int u, i;
Radek Krejcie7b95092019-05-15 11:03:07 +0200662 struct lys_parser_ctx context = {0};
Radek Krejci86d106e2018-10-18 09:53:19 +0200663
664 LY_CHECK_ARG_RET(ctx, ctx, data, NULL);
665
Radek Krejcibbe09a92018-11-08 09:36:54 +0100666 context.ctx = ctx;
667 context.line = 1;
668
Radek Krejci86d106e2018-10-18 09:53:19 +0200669 mod = calloc(1, sizeof *mod);
670 LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100671 mod->ctx = ctx;
Radek Krejci86d106e2018-10-18 09:53:19 +0200672
673 switch (format) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200674 /* TODO not yet supported
Radek Krejci86d106e2018-10-18 09:53:19 +0200675 case LYS_IN_YIN:
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100676 mod = yin_read_module();
Radek Krejci86d106e2018-10-18 09:53:19 +0200677 break;
Radek Krejcied5acc52019-04-25 15:57:04 +0200678 */
Radek Krejci86d106e2018-10-18 09:53:19 +0200679 case LYS_IN_YANG:
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100680 ret = yang_parse_module(&context, data, mod);
Radek Krejci86d106e2018-10-18 09:53:19 +0200681 break;
682 default:
683 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
684 break;
685 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100686 LY_CHECK_ERR_RET(ret, lys_module_free(mod, NULL), NULL);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200687
688 /* make sure that the newest revision is at position 0 */
689 lysp_sort_revisions(mod->parsed->revs);
Radek Krejci0af46292019-01-11 16:02:31 +0100690 if (mod->parsed->revs) {
691 mod->revision = lydict_insert(ctx, mod->parsed->revs[0].date, 0);
692 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200693
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100694 if (custom_check) {
695 LY_CHECK_GOTO(custom_check(ctx, mod->parsed, NULL, check_data), error);
696 }
697
Radek Krejci86d106e2018-10-18 09:53:19 +0200698 if (implement) {
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200699 /* mark the loaded module implemented */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100700 if (ly_ctx_get_module_implemented(ctx, mod->name)) {
701 LOGERR(ctx, LY_EDENIED, "Module \"%s\" is already implemented in the context.", mod->name);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100702 goto error;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200703 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100704 mod->implemented = 1;
Radek Krejci86d106e2018-10-18 09:53:19 +0200705 }
706
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100707 /* check for duplicity in the context */
Radek Krejci0af46292019-01-11 16:02:31 +0100708 mod_dup = (struct lys_module*)ly_ctx_get_module(ctx, mod->name, mod->revision);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100709 if (mod_dup) {
710 if (mod_dup->parsed) {
711 /* error */
Radek Krejcid33273d2018-10-25 14:55:52 +0200712 if (mod->parsed->revs) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100713 LOGERR(ctx, LY_EEXIST, "Module \"%s\" of revision \"%s\" is already present in the context.",
714 mod->name, mod->parsed->revs[0].date);
Radek Krejcid33273d2018-10-25 14:55:52 +0200715 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100716 LOGERR(ctx, LY_EEXIST, "Module \"%s\" with no revision is already present in the context.",
717 mod->name);
Radek Krejcid33273d2018-10-25 14:55:52 +0200718 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100719 goto error;
720 } else {
721 /* add the parsed data to the currently compiled-only module in the context */
722 mod_dup->parsed = mod->parsed;
723 mod_dup->parsed->mod = mod_dup;
724 mod->parsed = NULL;
725 lys_module_free(mod, NULL);
726 mod = mod_dup;
727 goto finish_parsing;
Radek Krejcid33273d2018-10-25 14:55:52 +0200728 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100729 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200730
731#if 0
732 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
733 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
734 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
735 * the anotation definitions available in the internal schema structure. There is another hack in schema
736 * printers to do not print this internally added annotation. */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100737 if (ly_strequal(mod->name, "ietf-netconf", 0)) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200738 if (lyp_add_ietf_netconf_annotations(mod)) {
739 lys_free(mod, NULL, 1, 1);
740 return NULL;
741 }
742 }
743#endif
744
Radek Krejci0af46292019-01-11 16:02:31 +0100745 if (!mod->implemented) {
746 /* pre-compile features of the module */
Radek Krejci327de162019-06-14 12:52:07 +0200747 LY_CHECK_GOTO(lys_feature_precompile(NULL, ctx, mod, mod->parsed->features, &mod->off_features), error);
Radek Krejci0af46292019-01-11 16:02:31 +0100748 }
749
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100750 /* decide the latest revision */
751 latest = (struct lys_module*)ly_ctx_get_module_latest(ctx, mod->name);
752 if (latest) {
Radek Krejci0af46292019-01-11 16:02:31 +0100753 if (mod->revision) {
754 if (!latest->revision) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100755 /* latest has no revision, so mod is anyway newer */
756 mod->latest_revision = latest->latest_revision;
757 latest->latest_revision = 0;
Radek Krejci0af46292019-01-11 16:02:31 +0100758 } else if (strcmp(mod->revision, latest->revision) > 0) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100759 mod->latest_revision = latest->latest_revision;
760 latest->latest_revision = 0;
Radek Krejcid33273d2018-10-25 14:55:52 +0200761 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200762 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100763 } else {
764 mod->latest_revision = 1;
765 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200766
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100767 /* add into context */
768 ly_set_add(&ctx->list, mod, LY_SET_OPT_USEASLIST);
Radek Krejcid33273d2018-10-25 14:55:52 +0200769
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100770finish_parsing:
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100771 /* resolve imports */
772 mod->parsed->parsing = 1;
773 LY_ARRAY_FOR(mod->parsed->imports, u) {
774 imp = &mod->parsed->imports[u];
775 if (!imp->module && lysp_load_module(ctx, imp->name, imp->rev[0] ? imp->rev : NULL, 0, 0, &imp->module)) {
776 goto error_ctx;
Radek Krejci086c7132018-10-26 15:29:04 +0200777 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100778 /* check for importing the same module twice */
779 for (i = 0; i < u; ++i) {
780 if (imp->module == mod->parsed->imports[i].module) {
781 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 +0100782 goto error_ctx;
Radek Krejci086c7132018-10-26 15:29:04 +0200783 }
784 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200785 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100786 LY_ARRAY_FOR(mod->parsed->includes, u) {
787 inc = &mod->parsed->includes[u];
788 if (!inc->submodule && lysp_load_submodule(&context, mod->parsed, inc)) {
789 goto error_ctx;
790 }
Radek Krejci0af46292019-01-11 16:02:31 +0100791 if (!mod->implemented) {
792 /* pre-compile features of the module */
Radek Krejci327de162019-06-14 12:52:07 +0200793 LY_CHECK_GOTO(lys_feature_precompile(NULL, ctx, mod, inc->submodule->features, &mod->off_features), error);
Radek Krejci0af46292019-01-11 16:02:31 +0100794 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100795 }
796 mod->parsed->parsing = 0;
797
Radek Krejci7fc68292019-06-12 13:51:09 +0200798 /* check name collisions - typedefs and TODO groupings */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100799 LY_CHECK_GOTO(lysp_check_typedefs(&context, mod->parsed), error_ctx);
Radek Krejcid33273d2018-10-25 14:55:52 +0200800
Radek Krejci86d106e2018-10-18 09:53:19 +0200801 return mod;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100802
803error_ctx:
804 ly_set_rm(&ctx->list, mod, NULL);
805error:
806 lys_module_free(mod, NULL);
807 ly_set_erase(&context.tpdfs_nodes, NULL);
808 return NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +0200809}
810
Radek Krejcid14e9692018-11-01 11:00:37 +0100811API struct lys_module *
Radek Krejci86d106e2018-10-18 09:53:19 +0200812lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format)
813{
Radek Krejci096235c2019-01-11 11:12:19 +0100814 struct lys_module *mod;
815
816 mod = lys_parse_mem_module(ctx, data, format, 1, NULL, NULL);
817 LY_CHECK_RET(!mod, NULL);
818
819 if (lys_compile(mod, 0)) {
820 ly_set_rm(&ctx->list, mod, NULL);
821 lys_module_free(mod, NULL);
822 return NULL;
823 }
824 return mod;
Radek Krejci86d106e2018-10-18 09:53:19 +0200825}
826
827static void
828lys_parse_set_filename(struct ly_ctx *ctx, const char **filename, int fd)
829{
Radek Krejci65639b92018-11-27 10:51:37 +0100830 char path[PATH_MAX];
Radek Krejci86d106e2018-10-18 09:53:19 +0200831
832#ifdef __APPLE__
833 if (fcntl(fd, F_GETPATH, path) != -1) {
834 *filename = lydict_insert(ctx, path, 0);
835 }
836#else
Radek Krejci65639b92018-11-27 10:51:37 +0100837 int len;
838 char proc_path[32];
839
Radek Krejci86d106e2018-10-18 09:53:19 +0200840 /* get URI if there is /proc */
841 sprintf(proc_path, "/proc/self/fd/%d", fd);
842 if ((len = readlink(proc_path, path, PATH_MAX - 1)) > 0) {
843 *filename = lydict_insert(ctx, path, len);
844 }
845#endif
846}
847
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100848void *
Radek Krejcie7b95092019-05-15 11:03:07 +0200849lys_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 +0100850 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
851 void *check_data)
Radek Krejci86d106e2018-10-18 09:53:19 +0200852{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100853 void *result;
854 struct lys_module *mod = NULL;
855 struct lysp_submodule *submod = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +0200856 size_t length;
857 char *addr;
858
859 LY_CHECK_ARG_RET(ctx, ctx, NULL);
860 if (fd < 0) {
861 LOGARG(ctx, fd);
862 return NULL;
863 }
864
865 LY_CHECK_RET(ly_mmap(ctx, fd, &length, (void **)&addr), NULL);
866 if (!addr) {
867 LOGERR(ctx, LY_EINVAL, "Empty schema file.");
868 return NULL;
869 }
870
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100871 if (main_ctx) {
872 result = submod = lys_parse_mem_submodule(ctx, addr, format, main_ctx, custom_check, check_data);
873 } else {
874 result = mod = lys_parse_mem_module(ctx, addr, format, implement, custom_check, check_data);
Radek Krejci096235c2019-01-11 11:12:19 +0100875 if (mod && implement && lys_compile(mod, 0)) {
876 ly_set_rm(&ctx->list, mod, NULL);
877 lys_module_free(mod, NULL);
878 result = mod = NULL;
879 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100880 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200881 ly_munmap(addr, length);
882
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100883 if (mod && !mod->filepath) {
884 lys_parse_set_filename(ctx, &mod->filepath, fd);
885 } else if (submod && !submod->filepath) {
886 lys_parse_set_filename(ctx, &submod->filepath, fd);
Radek Krejci86d106e2018-10-18 09:53:19 +0200887 }
888
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100889 return result;
890}
891
892struct lys_module *
893lys_parse_fd_module(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, int implement,
894 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
895 void *check_data)
896{
897 return (struct lys_module*)lys_parse_fd_(ctx, fd, format, implement, NULL, custom_check, check_data);
898}
899
900struct lysp_submodule *
Radek Krejcie7b95092019-05-15 11:03:07 +0200901lys_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 +0100902 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
903 void *check_data)
904{
905 assert(main_ctx);
906 return (struct lysp_submodule*)lys_parse_fd_(ctx, fd, format, 0, main_ctx, custom_check, check_data);
Radek Krejci86d106e2018-10-18 09:53:19 +0200907}
908
Radek Krejcid14e9692018-11-01 11:00:37 +0100909API struct lys_module *
Radek Krejci86d106e2018-10-18 09:53:19 +0200910lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format)
911{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100912 return lys_parse_fd_module(ctx, fd, format, 1, NULL, NULL);
Radek Krejci86d106e2018-10-18 09:53:19 +0200913}
914
Radek Krejcid33273d2018-10-25 14:55:52 +0200915struct lys_module *
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100916lys_parse_path_(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, int implement,
917 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 +0200918{
919 int fd;
Radek Krejcid33273d2018-10-25 14:55:52 +0200920 struct lys_module *mod;
Radek Krejci86d106e2018-10-18 09:53:19 +0200921 const char *rev, *dot, *filename;
922 size_t len;
923
924 LY_CHECK_ARG_RET(ctx, ctx, path, NULL);
925
926 fd = open(path, O_RDONLY);
927 LY_CHECK_ERR_RET(fd == -1, LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)), NULL);
928
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100929 mod = lys_parse_fd_module(ctx, fd, format, implement, custom_check, check_data);
Radek Krejci86d106e2018-10-18 09:53:19 +0200930 close(fd);
931 LY_CHECK_RET(!mod, NULL);
932
933 /* check that name and revision match filename */
934 filename = strrchr(path, '/');
935 if (!filename) {
936 filename = path;
937 } else {
938 filename++;
939 }
940 rev = strchr(filename, '@');
941 dot = strrchr(filename, '.');
942
943 /* name */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100944 len = strlen(mod->name);
945 if (strncmp(filename, mod->name, len) ||
Radek Krejci86d106e2018-10-18 09:53:19 +0200946 ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100947 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name);
Radek Krejci86d106e2018-10-18 09:53:19 +0200948 }
949 if (rev) {
950 len = dot - ++rev;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200951 if (!mod->parsed->revs || len != 10 || strncmp(mod->parsed->revs[0].date, rev, len)) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200952 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Radek Krejcib7db73a2018-10-24 14:18:40 +0200953 mod->parsed->revs ? mod->parsed->revs[0].date : "none");
Radek Krejci86d106e2018-10-18 09:53:19 +0200954 }
955 }
956
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100957 if (!mod->filepath) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200958 /* store URI */
959 char rpath[PATH_MAX];
960 if (realpath(path, rpath) != NULL) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100961 mod->filepath = lydict_insert(ctx, rpath, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +0200962 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100963 mod->filepath = lydict_insert(ctx, path, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +0200964 }
965 }
966
967 return mod;
968}
969
Radek Krejcid14e9692018-11-01 11:00:37 +0100970API struct lys_module *
Radek Krejcid33273d2018-10-25 14:55:52 +0200971lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format)
972{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100973 return lys_parse_path_(ctx, path, format, 1, NULL, NULL);
Radek Krejcid33273d2018-10-25 14:55:52 +0200974}
975
976API LY_ERR
977lys_search_localfile(const char * const *searchpaths, int cwd, const char *name, const char *revision,
978 char **localfile, LYS_INFORMAT *format)
979{
980 size_t len, flen, match_len = 0, dir_len;
981 int i, implicit_cwd = 0, ret = EXIT_FAILURE;
982 char *wd, *wn = NULL;
983 DIR *dir = NULL;
984 struct dirent *file;
985 char *match_name = NULL;
986 LYS_INFORMAT format_aux, match_format = 0;
987 struct ly_set *dirs;
988 struct stat st;
989
990 LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL);
991
992 /* start to fill the dir fifo with the context's search path (if set)
993 * and the current working directory */
994 dirs = ly_set_new();
995 if (!dirs) {
996 LOGMEM(NULL);
997 return EXIT_FAILURE;
998 }
999
1000 len = strlen(name);
1001 if (cwd) {
1002 wd = get_current_dir_name();
1003 if (!wd) {
1004 LOGMEM(NULL);
1005 goto cleanup;
1006 } else {
1007 /* add implicit current working directory (./) to be searched,
1008 * this directory is not searched recursively */
1009 if (ly_set_add(dirs, wd, 0) == -1) {
1010 goto cleanup;
1011 }
1012 implicit_cwd = 1;
1013 }
1014 }
1015 if (searchpaths) {
1016 for (i = 0; searchpaths[i]; i++) {
1017 /* check for duplicities with the implicit current working directory */
1018 if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) {
1019 implicit_cwd = 0;
1020 continue;
1021 }
1022 wd = strdup(searchpaths[i]);
1023 if (!wd) {
1024 LOGMEM(NULL);
1025 goto cleanup;
1026 } else if (ly_set_add(dirs, wd, 0) == -1) {
1027 goto cleanup;
1028 }
1029 }
1030 }
1031 wd = NULL;
1032
1033 /* start searching */
1034 while (dirs->count) {
1035 free(wd);
1036 free(wn); wn = NULL;
1037
1038 dirs->count--;
1039 wd = (char *)dirs->objs[dirs->count];
1040 dirs->objs[dirs->count] = NULL;
1041 LOGVRB("Searching for \"%s\" in %s.", name, wd);
1042
1043 if (dir) {
1044 closedir(dir);
1045 }
1046 dir = opendir(wd);
1047 dir_len = strlen(wd);
1048 if (!dir) {
1049 LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno));
1050 } else {
1051 while ((file = readdir(dir))) {
1052 if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) {
1053 /* skip . and .. */
1054 continue;
1055 }
1056 free(wn);
1057 if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) {
1058 LOGMEM(NULL);
1059 goto cleanup;
1060 }
1061 if (stat(wn, &st) == -1) {
1062 LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)",
1063 file->d_name, wd, strerror(errno));
1064 continue;
1065 }
1066 if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) {
1067 /* we have another subdirectory in searchpath to explore,
1068 * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */
1069 if (ly_set_add(dirs, wn, 0) == -1) {
1070 goto cleanup;
1071 }
1072 /* continue with the next item in current directory */
1073 wn = NULL;
1074 continue;
1075 } else if (!S_ISREG(st.st_mode)) {
1076 /* not a regular file (note that we see the target of symlinks instead of symlinks */
1077 continue;
1078 }
1079
1080 /* here we know that the item is a file which can contain a module */
1081 if (strncmp(name, file->d_name, len) ||
1082 (file->d_name[len] != '.' && file->d_name[len] != '@')) {
1083 /* different filename than the module we search for */
1084 continue;
1085 }
1086
1087 /* get type according to filename suffix */
1088 flen = strlen(file->d_name);
Radek Krejcied5acc52019-04-25 15:57:04 +02001089 if (!strcmp(&file->d_name[flen - 5], ".yang")) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001090 format_aux = LYS_IN_YANG;
Radek Krejcied5acc52019-04-25 15:57:04 +02001091 /* TODO YIN parser
1092 } else if (!strcmp(&file->d_name[flen - 4], ".yin")) {
1093 format_aux = LYS_IN_YIN;
1094 */
Radek Krejcid33273d2018-10-25 14:55:52 +02001095 } else {
1096 /* not supportde suffix/file format */
1097 continue;
1098 }
1099
1100 if (revision) {
1101 /* we look for the specific revision, try to get it from the filename */
1102 if (file->d_name[len] == '@') {
1103 /* check revision from the filename */
1104 if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) {
1105 /* another revision */
1106 continue;
1107 } else {
1108 /* exact revision */
1109 free(match_name);
1110 match_name = wn;
1111 wn = NULL;
1112 match_len = dir_len + 1 + len;
1113 match_format = format_aux;
1114 goto success;
1115 }
1116 } else {
1117 /* continue trying to find exact revision match, use this only if not found */
1118 free(match_name);
1119 match_name = wn;
1120 wn = NULL;
1121 match_len = dir_len + 1 +len;
1122 match_format = format_aux;
1123 continue;
1124 }
1125 } else {
1126 /* remember the revision and try to find the newest one */
1127 if (match_name) {
1128 if (file->d_name[len] != '@' ||
1129 lysp_check_date(NULL, &file->d_name[len + 1], flen - (format_aux == LYS_IN_YANG ? 5 : 4) - len - 1, NULL)) {
1130 continue;
1131 } else if (match_name[match_len] == '@' &&
1132 (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) {
1133 continue;
1134 }
1135 free(match_name);
1136 }
1137
1138 match_name = wn;
1139 wn = NULL;
1140 match_len = dir_len + 1 + len;
1141 match_format = format_aux;
1142 continue;
1143 }
1144 }
1145 }
1146 }
1147
1148success:
1149 (*localfile) = match_name;
1150 match_name = NULL;
1151 if (format) {
1152 (*format) = match_format;
1153 }
1154 ret = EXIT_SUCCESS;
1155
1156cleanup:
1157 free(wn);
1158 free(wd);
1159 if (dir) {
1160 closedir(dir);
1161 }
1162 free(match_name);
1163 ly_set_free(dirs, free);
1164
1165 return ret;
1166}
1167