blob: cabccb2060e6ce8907fb3c18620e788875fa9c34 [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 Krejcid33273d2018-10-25 14:55:52 +020017#include <dirent.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020018#include <errno.h>
Radek Krejci7802bae2018-11-26 15:34:10 +010019#include <limits.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020020#include <fcntl.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020021#include <stdio.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020022#include <sys/stat.h>
23#include <sys/types.h>
24#include <unistd.h>
Radek Krejci3f5e3db2018-10-11 15:57:47 +020025
26#include "libyang.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020027#include "context.h"
Radek Krejci70853c52018-10-15 14:46:16 +020028#include "tree_schema_internal.h"
Radek Krejci4f28eda2018-11-12 11:46:16 +010029#include "xpath.h"
Radek Krejci3f5e3db2018-10-11 15:57:47 +020030
Radek Krejcia3045382018-11-22 14:30:31 +010031API const struct lysc_node *
32lys_getnext(const struct lysc_node *last, const struct lysc_node *parent, const struct lysc_module *module, int options)
33{
Radek Krejci6eeb58f2019-02-22 16:29:37 +010034 const struct lysc_node *next = NULL;
Radek Krejcia3045382018-11-22 14:30:31 +010035 struct lysc_node **snode;
Radek Krejci6eeb58f2019-02-22 16:29:37 +010036 int action_flag = 0, notif_flag = 0;
37 const struct lysc_action *actions;
38 const struct lysc_notif *notifs;
39 unsigned int u;
Radek Krejcia3045382018-11-22 14:30:31 +010040
41 LY_CHECK_ARG_RET(NULL, parent || module, NULL);
42
43 if (!last) {
44 /* first call */
45
46 /* get know where to start */
47 if (parent) {
48 /* schema subtree */
Radek Krejci056d0a82018-12-06 16:57:25 +010049 if (parent->nodetype == LYS_CHOICE && (options & LYS_GETNEXT_WITHCASE)) {
50 if (!((struct lysc_node_choice*)parent)->cases) {
51 return NULL;
52 }
53 next = last = (const struct lysc_node*)&((struct lysc_node_choice*)parent)->cases[0];
54 } else {
Radek Krejci6eeb58f2019-02-22 16:29:37 +010055 snode = lysc_node_children_p(parent, (options & LYS_GETNEXT_OUTPUT) ? LYS_CONFIG_R : LYS_CONFIG_W);
Radek Krejci05b774b2019-02-25 13:26:18 +010056 /* do not return anything if the node does not have any children */
Radek Krejci056d0a82018-12-06 16:57:25 +010057 if (!snode || !(*snode)) {
58 return NULL;
59 }
60 next = last = *snode;
Radek Krejcia3045382018-11-22 14:30:31 +010061 }
Radek Krejcia3045382018-11-22 14:30:31 +010062 } else {
63 /* top level data */
64 next = last = module->data;
65 }
66 if (!next) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +010067 /* try to get action or notification */
68 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +010069 }
Radek Krejci05b774b2019-02-25 13:26:18 +010070 /* test if the next can be returned */
71 goto check;
72
Radek Krejci6eeb58f2019-02-22 16:29:37 +010073 } else if (last->nodetype == LYS_ACTION) {
Radek Krejci05b774b2019-02-25 13:26:18 +010074 action_flag = 1;
Radek Krejci6eeb58f2019-02-22 16:29:37 +010075 if (last->parent) {
76 actions = lysc_node_actions(last->parent);
77 } else {
78 actions = module->rpcs;
79 }
80 LY_ARRAY_FOR(actions, u) {
81 if (&actions[u] == (struct lysc_action*)last) {
82 break;
83 }
84 }
85 if (u + 1 < LY_ARRAY_SIZE(actions)) {
86 next = (struct lysc_node*)(&actions[u + 1]);
87 }
88 goto repeat;
89 } else if (last->nodetype == LYS_NOTIF) {
Radek Krejci05b774b2019-02-25 13:26:18 +010090 action_flag = notif_flag = 1;
Radek Krejci6eeb58f2019-02-22 16:29:37 +010091 if (last->parent) {
92 notifs = lysc_node_notifs(last->parent);
93 } else {
94 notifs = module->notifs;
95 }
96 LY_ARRAY_FOR(notifs, u) {
97 if (&notifs[u] == (struct lysc_notif*)last) {
98 break;
99 }
100 }
101 if (u + 1 < LY_ARRAY_SIZE(notifs)) {
102 next = (struct lysc_node*)(&notifs[u + 1]);
103 }
104 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100105 }
106
107 next = last->next;
108repeat:
Radek Krejci01342af2019-01-03 15:18:08 +0100109 if (next && parent && parent->nodetype == LYS_CASE && next->parent != parent) {
110 /* inside case (as an explicit parent, not when diving into it from choice),
111 * limit the list of children only to the specific case */
112 next = NULL;
113 }
Radek Krejcia3045382018-11-22 14:30:31 +0100114 if (!next) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100115 /* possibly go back to parent */
Radek Krejci05b774b2019-02-25 13:26:18 +0100116 if (last && last->parent != parent) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100117 last = last->parent;
118 next = last->next;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100119 } else if (!action_flag) {
120 action_flag = 1;
121 next = parent ? (struct lysc_node*)lysc_node_actions(parent) : (struct lysc_node*)module->rpcs;
122 } else if (!notif_flag) {
123 notif_flag = 1;
124 next = parent ? (struct lysc_node*)lysc_node_notifs(parent) : (struct lysc_node*)module->notifs;
125 } else {
126 return NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100127 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100128 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100129 }
Radek Krejci05b774b2019-02-25 13:26:18 +0100130check:
Radek Krejcia3045382018-11-22 14:30:31 +0100131 switch (next->nodetype) {
132 case LYS_ACTION:
133 case LYS_NOTIF:
134 case LYS_LEAF:
135 case LYS_ANYXML:
136 case LYS_ANYDATA:
137 case LYS_LIST:
138 case LYS_LEAFLIST:
Radek Krejcia9026eb2018-12-12 16:04:47 +0100139 case LYS_CASE:
Radek Krejcia3045382018-11-22 14:30:31 +0100140 break;
141 case LYS_CONTAINER:
142 if (!(((struct lysc_node_container *)next)->flags & LYS_PRESENCE) && (options & LYS_GETNEXT_INTONPCONT)) {
143 if (((struct lysc_node_container *)next)->child) {
144 /* go into */
145 next = ((struct lysc_node_container *)next)->child;
146 } else {
147 next = next->next;
148 }
149 goto repeat;
150 }
151 break;
152 case LYS_CHOICE:
153 if (options & LYS_GETNEXT_WITHCHOICE) {
154 return next;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100155 } else if ((options & LYS_GETNEXT_NOCHOICE) || !((struct lysc_node_choice *)next)->cases) {
156 next = next->next;
157 } else {
Radek Krejcia3045382018-11-22 14:30:31 +0100158 /* go into */
Radek Krejcia9026eb2018-12-12 16:04:47 +0100159 if (options & LYS_GETNEXT_WITHCASE) {
Radek Krejci05b774b2019-02-25 13:26:18 +0100160 next = (struct lysc_node*)((struct lysc_node_choice *)next)->cases;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100161 } else {
162 next = ((struct lysc_node_choice *)next)->cases->child;
163 }
Radek Krejcia3045382018-11-22 14:30:31 +0100164 }
165 goto repeat;
166 default:
167 /* we should not be here */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100168 LOGINT(last->module->ctx);
Radek Krejcia3045382018-11-22 14:30:31 +0100169 return NULL;
170 }
171
172 if (!(options & LYS_GETNEXT_NOSTATECHECK)) {
173 /* check if the node is disabled by if-feature */
174 if (lys_is_disabled(next, 0)) {
175 next = next->next;
176 goto repeat;
177 }
178 }
179 return next;
180}
181
182API const struct lysc_node *
183lys_child(const struct lysc_node *parent, const struct lys_module *module,
184 const char *name, size_t name_len, uint16_t nodetype, int options)
185{
186 const struct lysc_node *node = NULL;
187
188 LY_CHECK_ARG_RET(NULL, module, name, NULL);
189 if (!nodetype) {
190 nodetype = 0xffff;
191 }
192
193 while ((node = lys_getnext(node, parent, module->compiled, options))) {
194 if (!(node->nodetype & nodetype)) {
195 continue;
196 }
197 if (node->module != module) {
198 continue;
199 }
200
201 if (name_len) {
202 if (!strncmp(node->name, name, name_len) && !node->name[name_len]) {
203 return node;
204 }
205 } else {
206 if (!strcmp(node->name, name)) {
207 return node;
208 }
209 }
210 }
211 return NULL;
212}
213
Radek Krejci19a96102018-11-15 13:38:09 +0100214API int
215lysc_feature_value(const struct lysc_feature *feature)
Radek Krejci6f7feb62018-10-12 15:23:02 +0200216{
Radek Krejci19a96102018-11-15 13:38:09 +0100217 LY_CHECK_ARG_RET(NULL, feature, -1);
218 return feature->flags & LYS_FENABLED ? 1 : 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200219}
220
221static uint8_t
222iff_getop(uint8_t *list, int pos)
223{
224 uint8_t *item;
225 uint8_t mask = 3, result;
226
227 assert(pos >= 0);
228
229 item = &list[pos / 4];
230 result = (*item) & (mask << 2 * (pos % 4));
231 return result >> 2 * (pos % 4);
232}
233
Radek Krejci151a5b72018-10-19 14:21:44 +0200234static int
235lysc_iffeature_value_(const struct lysc_iffeature *iff, int *index_e, int *index_f)
236{
237 uint8_t op;
238 int a, b;
239
240 op = iff_getop(iff->expr, *index_e);
241 (*index_e)++;
242
243 switch (op) {
244 case LYS_IFF_F:
245 /* resolve feature */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200246 return lysc_feature_value(iff->features[(*index_f)++]);
Radek Krejci151a5b72018-10-19 14:21:44 +0200247 case LYS_IFF_NOT:
248 /* invert result */
249 return lysc_iffeature_value_(iff, index_e, index_f) ? 0 : 1;
250 case LYS_IFF_AND:
251 case LYS_IFF_OR:
252 a = lysc_iffeature_value_(iff, index_e, index_f);
253 b = lysc_iffeature_value_(iff, index_e, index_f);
254 if (op == LYS_IFF_AND) {
255 return a && b;
256 } else { /* LYS_IFF_OR */
257 return a || b;
258 }
259 }
260
261 return 0;
262}
263
264API int
265lysc_iffeature_value(const struct lysc_iffeature *iff)
266{
267 int index_e = 0, index_f = 0;
268
269 LY_CHECK_ARG_RET(NULL, iff, -1);
270
271 if (iff->expr) {
272 return lysc_iffeature_value_(iff, &index_e, &index_f);
273 }
274 return 0;
275}
276
Radek Krejci151a5b72018-10-19 14:21:44 +0200277/**
278 * @brief Enable/Disable the specified feature in the module.
279 *
280 * If the feature is already set to the desired value, LY_SUCCESS is returned.
281 * By changing the feature, also all the feature which depends on it via their
282 * if-feature statements are again evaluated (disabled if a if-feature statemen
283 * evaluates to false).
284 *
Radek Krejci0af46292019-01-11 16:02:31 +0100285 * @param[in] mod Module where to set (search for) the feature.
Radek Krejci151a5b72018-10-19 14:21:44 +0200286 * @param[in] name Name of the feature to set. Asterisk ('*') can be used to
287 * set all the features in the module.
288 * @param[in] value Desired value of the feature: 1 (enable) or 0 (disable).
289 * @return LY_ERR value.
290 */
291static LY_ERR
Radek Krejci0af46292019-01-11 16:02:31 +0100292lys_feature_change(const struct lys_module *mod, const char *name, int value)
Radek Krejci151a5b72018-10-19 14:21:44 +0200293{
294 int all = 0;
Radek Krejcica3db002018-11-01 10:31:01 +0100295 unsigned int u, changed_count, disabled_count;
Radek Krejci151a5b72018-10-19 14:21:44 +0200296 struct lysc_feature *f, **df;
297 struct lysc_iffeature *iff;
298 struct ly_set *changed;
Radek Krejci0af46292019-01-11 16:02:31 +0100299 struct ly_ctx *ctx = mod->ctx; /* shortcut */
Radek Krejci151a5b72018-10-19 14:21:44 +0200300
Radek Krejci0af46292019-01-11 16:02:31 +0100301 if (!mod->compiled) {
302 LOGERR(ctx, LY_EINVAL, "Module \"%s\" is not implemented so all its features are permanently disabled without a chance to change it.",
303 mod->name);
304 return LY_EINVAL;
305 }
306 if (!mod->compiled->features) {
307 LOGERR(ctx, LY_EINVAL, "Unable to switch feature since the module \"%s\" has no features.", mod->name);
Radek Krejci151a5b72018-10-19 14:21:44 +0200308 return LY_EINVAL;
309 }
310
311 if (!strcmp(name, "*")) {
312 /* enable all */
313 all = 1;
314 }
315 changed = ly_set_new();
Radek Krejcica3db002018-11-01 10:31:01 +0100316 changed_count = 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200317
Radek Krejcica3db002018-11-01 10:31:01 +0100318run:
Radek Krejci0af46292019-01-11 16:02:31 +0100319 for (disabled_count = u = 0; u < LY_ARRAY_SIZE(mod->compiled->features); ++u) {
320 f = &mod->compiled->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200321 if (all || !strcmp(f->name, name)) {
322 if ((value && (f->flags & LYS_FENABLED)) || (!value && !(f->flags & LYS_FENABLED))) {
323 if (all) {
324 /* skip already set features */
325 continue;
326 } else {
327 /* feature already set correctly */
328 ly_set_free(changed, NULL);
329 return LY_SUCCESS;
330 }
331 }
332
333 if (value) { /* enable */
334 /* check referenced features if they are enabled */
335 LY_ARRAY_FOR(f->iffeatures, struct lysc_iffeature, iff) {
336 if (!lysc_iffeature_value(iff)) {
337 if (all) {
Radek Krejcica3db002018-11-01 10:31:01 +0100338 ++disabled_count;
Radek Krejci151a5b72018-10-19 14:21:44 +0200339 goto next;
340 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100341 LOGERR(ctx, LY_EDENIED,
Radek Krejci151a5b72018-10-19 14:21:44 +0200342 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
343 f->name);
344 ly_set_free(changed, NULL);
345 return LY_EDENIED;
346 }
347 }
348 }
349 /* enable the feature */
350 f->flags |= LYS_FENABLED;
351 } else { /* disable */
352 /* disable the feature */
353 f->flags &= ~LYS_FENABLED;
354 }
355
356 /* remember the changed feature */
357 ly_set_add(changed, f, LY_SET_OPT_USEASLIST);
358
359 if (!all) {
360 /* stop in case changing a single feature */
361 break;
362 }
363 }
364next:
365 ;
366 }
367
368 if (!all && !changed->count) {
Radek Krejci0af46292019-01-11 16:02:31 +0100369 LOGERR(ctx, LY_EINVAL, "Feature \"%s\" not found in module \"%s\".", name, mod->name);
Radek Krejci151a5b72018-10-19 14:21:44 +0200370 ly_set_free(changed, NULL);
371 return LY_EINVAL;
372 }
373
Radek Krejcica3db002018-11-01 10:31:01 +0100374 if (value && all && disabled_count) {
375 if (changed_count == changed->count) {
376 /* no change in last run -> not able to enable all ... */
377 /* ... print errors */
Radek Krejci0af46292019-01-11 16:02:31 +0100378 for (u = 0; disabled_count && u < LY_ARRAY_SIZE(mod->compiled->features); ++u) {
379 if (!(mod->compiled->features[u].flags & LYS_FENABLED)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100380 LOGERR(ctx, LY_EDENIED,
Radek Krejcica3db002018-11-01 10:31:01 +0100381 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
Radek Krejci0af46292019-01-11 16:02:31 +0100382 mod->compiled->features[u].name);
Radek Krejcica3db002018-11-01 10:31:01 +0100383 --disabled_count;
384 }
385 }
386 /* ... restore the original state */
387 for (u = 0; u < changed->count; ++u) {
388 f = changed->objs[u];
389 /* re-disable the feature */
390 f->flags &= ~LYS_FENABLED;
391 }
392
393 ly_set_free(changed, NULL);
394 return LY_EDENIED;
395 } else {
396 /* we did some change in last run, try it again */
397 changed_count = changed->count;
398 goto run;
399 }
400 }
401
Radek Krejci151a5b72018-10-19 14:21:44 +0200402 /* reflect change(s) in the dependent features */
403 for (u = 0; u < changed->count; ++u) {
404 /* If a dependent feature is enabled, it can be now changed by the change (to false) of the value of
405 * its if-feature statements. The reverse logic, automatically enable feature when its feature is enabled
406 * is not done - by default, features are disabled and must be explicitely enabled. */
407 f = changed->objs[u];
408 LY_ARRAY_FOR(f->depfeatures, struct lysc_feature*, df) {
409 if (!((*df)->flags & LYS_FENABLED)) {
410 /* not enabled, nothing to do */
411 continue;
412 }
413 /* check the feature's if-features which could change by the previous change of our feature */
414 LY_ARRAY_FOR((*df)->iffeatures, struct lysc_iffeature, iff) {
415 if (!lysc_iffeature_value(iff)) {
416 /* the feature must be disabled now */
417 (*df)->flags &= ~LYS_FENABLED;
418 /* add the feature into the list of changed features */
419 ly_set_add(changed, *df, LY_SET_OPT_USEASLIST);
420 break;
421 }
422 }
423 }
424 }
425
426 ly_set_free(changed, NULL);
427 return LY_SUCCESS;
428}
429
430API LY_ERR
431lys_feature_enable(struct lys_module *module, const char *feature)
432{
Radek Krejci0af46292019-01-11 16:02:31 +0100433 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200434
Radek Krejci0af46292019-01-11 16:02:31 +0100435 return lys_feature_change(module, feature, 1);
Radek Krejci151a5b72018-10-19 14:21:44 +0200436}
437
438API LY_ERR
439lys_feature_disable(struct lys_module *module, const char *feature)
440{
Radek Krejci0af46292019-01-11 16:02:31 +0100441 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200442
Radek Krejci0af46292019-01-11 16:02:31 +0100443 return lys_feature_change(module, feature, 0);
Radek Krejci151a5b72018-10-19 14:21:44 +0200444}
445
446API int
447lys_feature_value(const struct lys_module *module, const char *feature)
448{
449 struct lysc_feature *f;
450 struct lysc_module *mod;
451 unsigned int u;
452
453 LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, -1);
454 mod = module->compiled;
455
456 /* search for the specified feature */
457 for (u = 0; u < LY_ARRAY_SIZE(mod->features); ++u) {
Radek Krejci2c4e7172018-10-19 15:56:26 +0200458 f = &mod->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200459 if (!strcmp(f->name, feature)) {
460 if (f->flags & LYS_FENABLED) {
461 return 1;
462 } else {
463 return 0;
464 }
465 }
466 }
467
468 /* feature definition not found */
469 return -1;
470}
471
Radek Krejcia3045382018-11-22 14:30:31 +0100472API const struct lysc_iffeature *
473lys_is_disabled(const struct lysc_node *node, int recursive)
474{
475 unsigned int u;
Radek Krejcia3045382018-11-22 14:30:31 +0100476
477 LY_CHECK_ARG_RET(NULL, node, NULL);
478
479 while(node) {
480 if (node->nodetype & LYS_CHOICE) {
481 return NULL;
482 }
483
Radek Krejci056d0a82018-12-06 16:57:25 +0100484 if (node->iffeatures) {
Radek Krejcia3045382018-11-22 14:30:31 +0100485 /* check local if-features */
Radek Krejci056d0a82018-12-06 16:57:25 +0100486 LY_ARRAY_FOR(node->iffeatures, u) {
487 if (!lysc_iffeature_value(&node->iffeatures[u])) {
488 return &node->iffeatures[u];
Radek Krejcia3045382018-11-22 14:30:31 +0100489 }
490 }
491 }
492
493 if (!recursive) {
494 return NULL;
495 }
496
497 /* go through parents */
498 node = node->parent;
499 }
500 return NULL;
501}
502
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100503struct lysp_submodule *
504lys_parse_mem_submodule(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, struct ly_parser_ctx *main_ctx,
505 LY_ERR (*custom_check)(struct ly_ctx*, struct lysp_module*, struct lysp_submodule*, void*), void *check_data)
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200506{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100507 LY_ERR ret = LY_EINVAL;
508 struct lysp_submodule *submod = NULL, *latest_sp;
509 struct ly_parser_ctx context = {0};
510
511 LY_CHECK_ARG_RET(ctx, ctx, data, NULL);
512
513 context.ctx = ctx;
514 context.line = 1;
515
516 /* map the typedefs and groupings list from main context to the submodule's context */
517 memcpy(&context.tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
518 memcpy(&context.grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
519
520 switch (format) {
521 case LYS_IN_YIN:
522 /* TODO not yet supported
523 mod = yin_read_module();
524 */
525 break;
526 case LYS_IN_YANG:
527 ret = yang_parse_submodule(&context, data, &submod);
528 break;
529 default:
530 LOGERR(context.ctx, LY_EINVAL, "Invalid schema input format.");
531 break;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200532 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100533 LY_CHECK_RET(ret, NULL);
534
535 /* make sure that the newest revision is at position 0 */
536 lysp_sort_revisions(submod->revs);
537
538 if (custom_check) {
539 LY_CHECK_GOTO(custom_check(context.ctx, NULL, submod, check_data), error);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200540 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100541
542 /* decide the latest revision */
543 latest_sp = ly_ctx_get_submodule(context.ctx, submod->belongsto, submod->name, NULL);
544 if (latest_sp) {
545 if (submod->revs) {
546 if (!latest_sp->revs) {
547 /* latest has no revision, so mod is anyway newer */
548 submod->latest_revision = latest_sp->latest_revision;
549 latest_sp->latest_revision = 0;
550 } else {
551 if (strcmp(submod->revs[0].date, latest_sp->revs[0].date) > 0) {
552 submod->latest_revision = latest_sp->latest_revision;
553 latest_sp->latest_revision = 0;
554 }
555 }
556 }
557 } else {
558 submod->latest_revision = 1;
559 }
560
561 /* remap possibly changed and reallocated typedefs and groupings list back to the main context */
562 memcpy(&main_ctx->tpdfs_nodes, &context.tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
563 memcpy(&main_ctx->grps_nodes, &context.grps_nodes, sizeof main_ctx->grps_nodes);
564
565 return submod;
566error:
567 lysp_submodule_free(ctx, submod);
568 return NULL;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200569}
570
Radek Krejcid33273d2018-10-25 14:55:52 +0200571struct lys_module *
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100572lys_parse_mem_module(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, int implement,
573 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
574 void *check_data)
Radek Krejci86d106e2018-10-18 09:53:19 +0200575{
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100576 struct lys_module *mod = NULL, *latest, *mod_dup;
Radek Krejci086c7132018-10-26 15:29:04 +0200577 struct lysp_import *imp;
578 struct lysp_include *inc;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100579 LY_ERR ret = LY_EINVAL;
Radek Krejci086c7132018-10-26 15:29:04 +0200580 unsigned int u, i;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100581 struct ly_parser_ctx context = {0};
Radek Krejci86d106e2018-10-18 09:53:19 +0200582
583 LY_CHECK_ARG_RET(ctx, ctx, data, NULL);
584
Radek Krejcibbe09a92018-11-08 09:36:54 +0100585 context.ctx = ctx;
586 context.line = 1;
587
Radek Krejci86d106e2018-10-18 09:53:19 +0200588 mod = calloc(1, sizeof *mod);
589 LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100590 mod->ctx = ctx;
Radek Krejci86d106e2018-10-18 09:53:19 +0200591
592 switch (format) {
593 case LYS_IN_YIN:
594 /* TODO not yet supported
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100595 mod = yin_read_module();
Radek Krejci86d106e2018-10-18 09:53:19 +0200596 */
597 break;
598 case LYS_IN_YANG:
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100599 ret = yang_parse_module(&context, data, mod);
Radek Krejci86d106e2018-10-18 09:53:19 +0200600 break;
601 default:
602 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
603 break;
604 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100605 LY_CHECK_ERR_RET(ret, lys_module_free(mod, NULL), NULL);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200606
607 /* make sure that the newest revision is at position 0 */
608 lysp_sort_revisions(mod->parsed->revs);
Radek Krejci0af46292019-01-11 16:02:31 +0100609 if (mod->parsed->revs) {
610 mod->revision = lydict_insert(ctx, mod->parsed->revs[0].date, 0);
611 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200612
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100613 if (custom_check) {
614 LY_CHECK_GOTO(custom_check(ctx, mod->parsed, NULL, check_data), error);
615 }
616
Radek Krejci86d106e2018-10-18 09:53:19 +0200617 if (implement) {
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200618 /* mark the loaded module implemented */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100619 if (ly_ctx_get_module_implemented(ctx, mod->name)) {
620 LOGERR(ctx, LY_EDENIED, "Module \"%s\" is already implemented in the context.", mod->name);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100621 goto error;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200622 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100623 mod->implemented = 1;
Radek Krejci86d106e2018-10-18 09:53:19 +0200624 }
625
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100626 /* check for duplicity in the context */
Radek Krejci0af46292019-01-11 16:02:31 +0100627 mod_dup = (struct lys_module*)ly_ctx_get_module(ctx, mod->name, mod->revision);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100628 if (mod_dup) {
629 if (mod_dup->parsed) {
630 /* error */
Radek Krejcid33273d2018-10-25 14:55:52 +0200631 if (mod->parsed->revs) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100632 LOGERR(ctx, LY_EEXIST, "Module \"%s\" of revision \"%s\" is already present in the context.",
633 mod->name, mod->parsed->revs[0].date);
Radek Krejcid33273d2018-10-25 14:55:52 +0200634 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100635 LOGERR(ctx, LY_EEXIST, "Module \"%s\" with no revision is already present in the context.",
636 mod->name);
Radek Krejcid33273d2018-10-25 14:55:52 +0200637 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100638 goto error;
639 } else {
640 /* add the parsed data to the currently compiled-only module in the context */
641 mod_dup->parsed = mod->parsed;
642 mod_dup->parsed->mod = mod_dup;
643 mod->parsed = NULL;
644 lys_module_free(mod, NULL);
645 mod = mod_dup;
646 goto finish_parsing;
Radek Krejcid33273d2018-10-25 14:55:52 +0200647 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100648 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200649
650#if 0
651 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
652 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
653 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
654 * the anotation definitions available in the internal schema structure. There is another hack in schema
655 * printers to do not print this internally added annotation. */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100656 if (ly_strequal(mod->name, "ietf-netconf", 0)) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200657 if (lyp_add_ietf_netconf_annotations(mod)) {
658 lys_free(mod, NULL, 1, 1);
659 return NULL;
660 }
661 }
662#endif
663
Radek Krejci0af46292019-01-11 16:02:31 +0100664 if (!mod->implemented) {
665 /* pre-compile features of the module */
666 LY_CHECK_GOTO(lys_feature_precompile(ctx, mod->parsed->features, &mod->off_features), error);
667 }
668
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100669 /* decide the latest revision */
670 latest = (struct lys_module*)ly_ctx_get_module_latest(ctx, mod->name);
671 if (latest) {
Radek Krejci0af46292019-01-11 16:02:31 +0100672 if (mod->revision) {
673 if (!latest->revision) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100674 /* latest has no revision, so mod is anyway newer */
675 mod->latest_revision = latest->latest_revision;
676 latest->latest_revision = 0;
Radek Krejci0af46292019-01-11 16:02:31 +0100677 } else if (strcmp(mod->revision, latest->revision) > 0) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100678 mod->latest_revision = latest->latest_revision;
679 latest->latest_revision = 0;
Radek Krejcid33273d2018-10-25 14:55:52 +0200680 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200681 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100682 } else {
683 mod->latest_revision = 1;
684 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200685
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100686 /* add into context */
687 ly_set_add(&ctx->list, mod, LY_SET_OPT_USEASLIST);
Radek Krejcid33273d2018-10-25 14:55:52 +0200688
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100689finish_parsing:
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100690 /* resolve imports */
691 mod->parsed->parsing = 1;
692 LY_ARRAY_FOR(mod->parsed->imports, u) {
693 imp = &mod->parsed->imports[u];
694 if (!imp->module && lysp_load_module(ctx, imp->name, imp->rev[0] ? imp->rev : NULL, 0, 0, &imp->module)) {
695 goto error_ctx;
Radek Krejci086c7132018-10-26 15:29:04 +0200696 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100697 /* check for importing the same module twice */
698 for (i = 0; i < u; ++i) {
699 if (imp->module == mod->parsed->imports[i].module) {
700 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 +0100701 goto error_ctx;
Radek Krejci086c7132018-10-26 15:29:04 +0200702 }
703 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200704 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100705 LY_ARRAY_FOR(mod->parsed->includes, u) {
706 inc = &mod->parsed->includes[u];
707 if (!inc->submodule && lysp_load_submodule(&context, mod->parsed, inc)) {
708 goto error_ctx;
709 }
Radek Krejci0af46292019-01-11 16:02:31 +0100710 if (!mod->implemented) {
711 /* pre-compile features of the module */
712 LY_CHECK_GOTO(lys_feature_precompile(ctx, inc->submodule->features, &mod->off_features), error);
713 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100714 }
715 mod->parsed->parsing = 0;
716
717 /* check name collisions - typedefs and groupings */
718 LY_CHECK_GOTO(lysp_check_typedefs(&context, mod->parsed), error_ctx);
Radek Krejcid33273d2018-10-25 14:55:52 +0200719
Radek Krejci86d106e2018-10-18 09:53:19 +0200720 return mod;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100721
722error_ctx:
723 ly_set_rm(&ctx->list, mod, NULL);
724error:
725 lys_module_free(mod, NULL);
726 ly_set_erase(&context.tpdfs_nodes, NULL);
727 return NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +0200728}
729
Radek Krejcid14e9692018-11-01 11:00:37 +0100730API struct lys_module *
Radek Krejci86d106e2018-10-18 09:53:19 +0200731lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format)
732{
Radek Krejci096235c2019-01-11 11:12:19 +0100733 struct lys_module *mod;
734
735 mod = lys_parse_mem_module(ctx, data, format, 1, NULL, NULL);
736 LY_CHECK_RET(!mod, NULL);
737
738 if (lys_compile(mod, 0)) {
739 ly_set_rm(&ctx->list, mod, NULL);
740 lys_module_free(mod, NULL);
741 return NULL;
742 }
743 return mod;
Radek Krejci86d106e2018-10-18 09:53:19 +0200744}
745
746static void
747lys_parse_set_filename(struct ly_ctx *ctx, const char **filename, int fd)
748{
Radek Krejci65639b92018-11-27 10:51:37 +0100749 char path[PATH_MAX];
Radek Krejci86d106e2018-10-18 09:53:19 +0200750
751#ifdef __APPLE__
752 if (fcntl(fd, F_GETPATH, path) != -1) {
753 *filename = lydict_insert(ctx, path, 0);
754 }
755#else
Radek Krejci65639b92018-11-27 10:51:37 +0100756 int len;
757 char proc_path[32];
758
Radek Krejci86d106e2018-10-18 09:53:19 +0200759 /* get URI if there is /proc */
760 sprintf(proc_path, "/proc/self/fd/%d", fd);
761 if ((len = readlink(proc_path, path, PATH_MAX - 1)) > 0) {
762 *filename = lydict_insert(ctx, path, len);
763 }
764#endif
765}
766
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100767void *
Radek Krejci3b1f9292018-11-08 10:58:35 +0100768lys_parse_fd_(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, int implement, struct ly_parser_ctx *main_ctx,
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100769 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
770 void *check_data)
Radek Krejci86d106e2018-10-18 09:53:19 +0200771{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100772 void *result;
773 struct lys_module *mod = NULL;
774 struct lysp_submodule *submod = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +0200775 size_t length;
776 char *addr;
777
778 LY_CHECK_ARG_RET(ctx, ctx, NULL);
779 if (fd < 0) {
780 LOGARG(ctx, fd);
781 return NULL;
782 }
783
784 LY_CHECK_RET(ly_mmap(ctx, fd, &length, (void **)&addr), NULL);
785 if (!addr) {
786 LOGERR(ctx, LY_EINVAL, "Empty schema file.");
787 return NULL;
788 }
789
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100790 if (main_ctx) {
791 result = submod = lys_parse_mem_submodule(ctx, addr, format, main_ctx, custom_check, check_data);
792 } else {
793 result = mod = lys_parse_mem_module(ctx, addr, format, implement, custom_check, check_data);
Radek Krejci096235c2019-01-11 11:12:19 +0100794 if (mod && implement && lys_compile(mod, 0)) {
795 ly_set_rm(&ctx->list, mod, NULL);
796 lys_module_free(mod, NULL);
797 result = mod = NULL;
798 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100799 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200800 ly_munmap(addr, length);
801
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100802 if (mod && !mod->filepath) {
803 lys_parse_set_filename(ctx, &mod->filepath, fd);
804 } else if (submod && !submod->filepath) {
805 lys_parse_set_filename(ctx, &submod->filepath, fd);
Radek Krejci86d106e2018-10-18 09:53:19 +0200806 }
807
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100808 return result;
809}
810
811struct lys_module *
812lys_parse_fd_module(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, int implement,
813 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
814 void *check_data)
815{
816 return (struct lys_module*)lys_parse_fd_(ctx, fd, format, implement, NULL, custom_check, check_data);
817}
818
819struct lysp_submodule *
820lys_parse_fd_submodule(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, struct ly_parser_ctx *main_ctx,
821 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
822 void *check_data)
823{
824 assert(main_ctx);
825 return (struct lysp_submodule*)lys_parse_fd_(ctx, fd, format, 0, main_ctx, custom_check, check_data);
Radek Krejci86d106e2018-10-18 09:53:19 +0200826}
827
Radek Krejcid14e9692018-11-01 11:00:37 +0100828API struct lys_module *
Radek Krejci86d106e2018-10-18 09:53:19 +0200829lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format)
830{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100831 return lys_parse_fd_module(ctx, fd, format, 1, NULL, NULL);
Radek Krejci86d106e2018-10-18 09:53:19 +0200832}
833
Radek Krejcid33273d2018-10-25 14:55:52 +0200834struct lys_module *
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100835lys_parse_path_(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, int implement,
836 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 +0200837{
838 int fd;
Radek Krejcid33273d2018-10-25 14:55:52 +0200839 struct lys_module *mod;
Radek Krejci86d106e2018-10-18 09:53:19 +0200840 const char *rev, *dot, *filename;
841 size_t len;
842
843 LY_CHECK_ARG_RET(ctx, ctx, path, NULL);
844
845 fd = open(path, O_RDONLY);
846 LY_CHECK_ERR_RET(fd == -1, LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)), NULL);
847
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100848 mod = lys_parse_fd_module(ctx, fd, format, implement, custom_check, check_data);
Radek Krejci86d106e2018-10-18 09:53:19 +0200849 close(fd);
850 LY_CHECK_RET(!mod, NULL);
851
852 /* check that name and revision match filename */
853 filename = strrchr(path, '/');
854 if (!filename) {
855 filename = path;
856 } else {
857 filename++;
858 }
859 rev = strchr(filename, '@');
860 dot = strrchr(filename, '.');
861
862 /* name */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100863 len = strlen(mod->name);
864 if (strncmp(filename, mod->name, len) ||
Radek Krejci86d106e2018-10-18 09:53:19 +0200865 ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100866 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name);
Radek Krejci86d106e2018-10-18 09:53:19 +0200867 }
868 if (rev) {
869 len = dot - ++rev;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200870 if (!mod->parsed->revs || len != 10 || strncmp(mod->parsed->revs[0].date, rev, len)) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200871 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Radek Krejcib7db73a2018-10-24 14:18:40 +0200872 mod->parsed->revs ? mod->parsed->revs[0].date : "none");
Radek Krejci86d106e2018-10-18 09:53:19 +0200873 }
874 }
875
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100876 if (!mod->filepath) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200877 /* store URI */
878 char rpath[PATH_MAX];
879 if (realpath(path, rpath) != NULL) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100880 mod->filepath = lydict_insert(ctx, rpath, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +0200881 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100882 mod->filepath = lydict_insert(ctx, path, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +0200883 }
884 }
885
886 return mod;
887}
888
Radek Krejcid14e9692018-11-01 11:00:37 +0100889API struct lys_module *
Radek Krejcid33273d2018-10-25 14:55:52 +0200890lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format)
891{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100892 return lys_parse_path_(ctx, path, format, 1, NULL, NULL);
Radek Krejcid33273d2018-10-25 14:55:52 +0200893}
894
895API LY_ERR
896lys_search_localfile(const char * const *searchpaths, int cwd, const char *name, const char *revision,
897 char **localfile, LYS_INFORMAT *format)
898{
899 size_t len, flen, match_len = 0, dir_len;
900 int i, implicit_cwd = 0, ret = EXIT_FAILURE;
901 char *wd, *wn = NULL;
902 DIR *dir = NULL;
903 struct dirent *file;
904 char *match_name = NULL;
905 LYS_INFORMAT format_aux, match_format = 0;
906 struct ly_set *dirs;
907 struct stat st;
908
909 LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL);
910
911 /* start to fill the dir fifo with the context's search path (if set)
912 * and the current working directory */
913 dirs = ly_set_new();
914 if (!dirs) {
915 LOGMEM(NULL);
916 return EXIT_FAILURE;
917 }
918
919 len = strlen(name);
920 if (cwd) {
921 wd = get_current_dir_name();
922 if (!wd) {
923 LOGMEM(NULL);
924 goto cleanup;
925 } else {
926 /* add implicit current working directory (./) to be searched,
927 * this directory is not searched recursively */
928 if (ly_set_add(dirs, wd, 0) == -1) {
929 goto cleanup;
930 }
931 implicit_cwd = 1;
932 }
933 }
934 if (searchpaths) {
935 for (i = 0; searchpaths[i]; i++) {
936 /* check for duplicities with the implicit current working directory */
937 if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) {
938 implicit_cwd = 0;
939 continue;
940 }
941 wd = strdup(searchpaths[i]);
942 if (!wd) {
943 LOGMEM(NULL);
944 goto cleanup;
945 } else if (ly_set_add(dirs, wd, 0) == -1) {
946 goto cleanup;
947 }
948 }
949 }
950 wd = NULL;
951
952 /* start searching */
953 while (dirs->count) {
954 free(wd);
955 free(wn); wn = NULL;
956
957 dirs->count--;
958 wd = (char *)dirs->objs[dirs->count];
959 dirs->objs[dirs->count] = NULL;
960 LOGVRB("Searching for \"%s\" in %s.", name, wd);
961
962 if (dir) {
963 closedir(dir);
964 }
965 dir = opendir(wd);
966 dir_len = strlen(wd);
967 if (!dir) {
968 LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno));
969 } else {
970 while ((file = readdir(dir))) {
971 if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) {
972 /* skip . and .. */
973 continue;
974 }
975 free(wn);
976 if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) {
977 LOGMEM(NULL);
978 goto cleanup;
979 }
980 if (stat(wn, &st) == -1) {
981 LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)",
982 file->d_name, wd, strerror(errno));
983 continue;
984 }
985 if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) {
986 /* we have another subdirectory in searchpath to explore,
987 * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */
988 if (ly_set_add(dirs, wn, 0) == -1) {
989 goto cleanup;
990 }
991 /* continue with the next item in current directory */
992 wn = NULL;
993 continue;
994 } else if (!S_ISREG(st.st_mode)) {
995 /* not a regular file (note that we see the target of symlinks instead of symlinks */
996 continue;
997 }
998
999 /* here we know that the item is a file which can contain a module */
1000 if (strncmp(name, file->d_name, len) ||
1001 (file->d_name[len] != '.' && file->d_name[len] != '@')) {
1002 /* different filename than the module we search for */
1003 continue;
1004 }
1005
1006 /* get type according to filename suffix */
1007 flen = strlen(file->d_name);
1008 if (!strcmp(&file->d_name[flen - 4], ".yin")) {
1009 format_aux = LYS_IN_YIN;
1010 } else if (!strcmp(&file->d_name[flen - 5], ".yang")) {
1011 format_aux = LYS_IN_YANG;
1012 } else {
1013 /* not supportde suffix/file format */
1014 continue;
1015 }
1016
1017 if (revision) {
1018 /* we look for the specific revision, try to get it from the filename */
1019 if (file->d_name[len] == '@') {
1020 /* check revision from the filename */
1021 if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) {
1022 /* another revision */
1023 continue;
1024 } else {
1025 /* exact revision */
1026 free(match_name);
1027 match_name = wn;
1028 wn = NULL;
1029 match_len = dir_len + 1 + len;
1030 match_format = format_aux;
1031 goto success;
1032 }
1033 } else {
1034 /* continue trying to find exact revision match, use this only if not found */
1035 free(match_name);
1036 match_name = wn;
1037 wn = NULL;
1038 match_len = dir_len + 1 +len;
1039 match_format = format_aux;
1040 continue;
1041 }
1042 } else {
1043 /* remember the revision and try to find the newest one */
1044 if (match_name) {
1045 if (file->d_name[len] != '@' ||
1046 lysp_check_date(NULL, &file->d_name[len + 1], flen - (format_aux == LYS_IN_YANG ? 5 : 4) - len - 1, NULL)) {
1047 continue;
1048 } else if (match_name[match_len] == '@' &&
1049 (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) {
1050 continue;
1051 }
1052 free(match_name);
1053 }
1054
1055 match_name = wn;
1056 wn = NULL;
1057 match_len = dir_len + 1 + len;
1058 match_format = format_aux;
1059 continue;
1060 }
1061 }
1062 }
1063 }
1064
1065success:
1066 (*localfile) = match_name;
1067 match_name = NULL;
1068 if (format) {
1069 (*format) = match_format;
1070 }
1071 ret = EXIT_SUCCESS;
1072
1073cleanup:
1074 free(wn);
1075 free(wd);
1076 if (dir) {
1077 closedir(dir);
1078 }
1079 free(match_name);
1080 ly_set_free(dirs, free);
1081
1082 return ret;
1083}
1084