blob: 316f3a30b84c7e63b9459abee58df0f4ece35bb2 [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
Radek Krejcid5a2b9d2019-04-12 10:39:30 +020043next:
Radek Krejcia3045382018-11-22 14:30:31 +010044 if (!last) {
45 /* first call */
46
47 /* get know where to start */
48 if (parent) {
49 /* schema subtree */
Radek Krejci056d0a82018-12-06 16:57:25 +010050 if (parent->nodetype == LYS_CHOICE && (options & LYS_GETNEXT_WITHCASE)) {
Radek Krejcid5a2b9d2019-04-12 10:39:30 +020051 if (((struct lysc_node_choice*)parent)->cases) {
52 next = last = (const struct lysc_node*)&((struct lysc_node_choice*)parent)->cases[0];
Radek Krejci056d0a82018-12-06 16:57:25 +010053 }
Radek Krejci056d0a82018-12-06 16:57:25 +010054 } 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 Krejcid5a2b9d2019-04-12 10:39:30 +020057 if (snode && *snode) {
58 next = last = *snode;
Radek Krejci056d0a82018-12-06 16:57:25 +010059 }
Radek Krejcia3045382018-11-22 14:30:31 +010060 }
Radek Krejcia3045382018-11-22 14:30:31 +010061 } else {
62 /* top level data */
63 next = last = module->data;
64 }
65 if (!next) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +010066 /* try to get action or notification */
67 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +010068 }
Radek Krejci05b774b2019-02-25 13:26:18 +010069 /* test if the next can be returned */
70 goto check;
71
Radek Krejci6eeb58f2019-02-22 16:29:37 +010072 } else if (last->nodetype == LYS_ACTION) {
Radek Krejci05b774b2019-02-25 13:26:18 +010073 action_flag = 1;
Radek Krejci6eeb58f2019-02-22 16:29:37 +010074 if (last->parent) {
75 actions = lysc_node_actions(last->parent);
76 } else {
77 actions = module->rpcs;
78 }
79 LY_ARRAY_FOR(actions, u) {
80 if (&actions[u] == (struct lysc_action*)last) {
81 break;
82 }
83 }
84 if (u + 1 < LY_ARRAY_SIZE(actions)) {
85 next = (struct lysc_node*)(&actions[u + 1]);
86 }
87 goto repeat;
88 } else if (last->nodetype == LYS_NOTIF) {
Radek Krejci05b774b2019-02-25 13:26:18 +010089 action_flag = notif_flag = 1;
Radek Krejci6eeb58f2019-02-22 16:29:37 +010090 if (last->parent) {
91 notifs = lysc_node_notifs(last->parent);
92 } else {
93 notifs = module->notifs;
94 }
95 LY_ARRAY_FOR(notifs, u) {
96 if (&notifs[u] == (struct lysc_notif*)last) {
97 break;
98 }
99 }
100 if (u + 1 < LY_ARRAY_SIZE(notifs)) {
101 next = (struct lysc_node*)(&notifs[u + 1]);
102 }
103 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100104 }
105
106 next = last->next;
107repeat:
Radek Krejci01342af2019-01-03 15:18:08 +0100108 if (next && parent && parent->nodetype == LYS_CASE && next->parent != parent) {
109 /* inside case (as an explicit parent, not when diving into it from choice),
110 * limit the list of children only to the specific case */
111 next = NULL;
112 }
Radek Krejcia3045382018-11-22 14:30:31 +0100113 if (!next) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100114 /* possibly go back to parent */
Radek Krejci05b774b2019-02-25 13:26:18 +0100115 if (last && last->parent != parent) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100116 last = last->parent;
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200117 goto next;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100118 } else if (!action_flag) {
119 action_flag = 1;
120 next = parent ? (struct lysc_node*)lysc_node_actions(parent) : (struct lysc_node*)module->rpcs;
121 } else if (!notif_flag) {
122 notif_flag = 1;
123 next = parent ? (struct lysc_node*)lysc_node_notifs(parent) : (struct lysc_node*)module->notifs;
124 } else {
125 return NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100126 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100127 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100128 }
Radek Krejci05b774b2019-02-25 13:26:18 +0100129check:
Radek Krejcia3045382018-11-22 14:30:31 +0100130 switch (next->nodetype) {
131 case LYS_ACTION:
132 case LYS_NOTIF:
133 case LYS_LEAF:
134 case LYS_ANYXML:
135 case LYS_ANYDATA:
136 case LYS_LIST:
137 case LYS_LEAFLIST:
Radek Krejcia9026eb2018-12-12 16:04:47 +0100138 case LYS_CASE:
Radek Krejcia3045382018-11-22 14:30:31 +0100139 break;
140 case LYS_CONTAINER:
141 if (!(((struct lysc_node_container *)next)->flags & LYS_PRESENCE) && (options & LYS_GETNEXT_INTONPCONT)) {
142 if (((struct lysc_node_container *)next)->child) {
143 /* go into */
144 next = ((struct lysc_node_container *)next)->child;
145 } else {
146 next = next->next;
147 }
148 goto repeat;
149 }
150 break;
151 case LYS_CHOICE:
152 if (options & LYS_GETNEXT_WITHCHOICE) {
153 return next;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100154 } else if ((options & LYS_GETNEXT_NOCHOICE) || !((struct lysc_node_choice *)next)->cases) {
155 next = next->next;
156 } else {
Radek Krejcia3045382018-11-22 14:30:31 +0100157 /* go into */
Radek Krejcia9026eb2018-12-12 16:04:47 +0100158 if (options & LYS_GETNEXT_WITHCASE) {
Radek Krejci05b774b2019-02-25 13:26:18 +0100159 next = (struct lysc_node*)((struct lysc_node_choice *)next)->cases;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100160 } else {
161 next = ((struct lysc_node_choice *)next)->cases->child;
162 }
Radek Krejcia3045382018-11-22 14:30:31 +0100163 }
164 goto repeat;
165 default:
166 /* we should not be here */
Radek Krejcib07b5c92019-04-08 10:56:37 +0200167 LOGINT(module ? module->mod->ctx : parent->module->ctx);
Radek Krejcia3045382018-11-22 14:30:31 +0100168 return NULL;
169 }
170
171 if (!(options & LYS_GETNEXT_NOSTATECHECK)) {
172 /* check if the node is disabled by if-feature */
173 if (lys_is_disabled(next, 0)) {
174 next = next->next;
175 goto repeat;
176 }
177 }
178 return next;
179}
180
181API const struct lysc_node *
182lys_child(const struct lysc_node *parent, const struct lys_module *module,
183 const char *name, size_t name_len, uint16_t nodetype, int options)
184{
185 const struct lysc_node *node = NULL;
186
187 LY_CHECK_ARG_RET(NULL, module, name, NULL);
188 if (!nodetype) {
189 nodetype = 0xffff;
190 }
191
192 while ((node = lys_getnext(node, parent, module->compiled, options))) {
193 if (!(node->nodetype & nodetype)) {
194 continue;
195 }
196 if (node->module != module) {
197 continue;
198 }
199
200 if (name_len) {
201 if (!strncmp(node->name, name, name_len) && !node->name[name_len]) {
202 return node;
203 }
204 } else {
205 if (!strcmp(node->name, name)) {
206 return node;
207 }
208 }
209 }
210 return NULL;
211}
212
Radek Krejci19a96102018-11-15 13:38:09 +0100213API int
214lysc_feature_value(const struct lysc_feature *feature)
Radek Krejci6f7feb62018-10-12 15:23:02 +0200215{
Radek Krejci19a96102018-11-15 13:38:09 +0100216 LY_CHECK_ARG_RET(NULL, feature, -1);
217 return feature->flags & LYS_FENABLED ? 1 : 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200218}
219
Radek Krejci693262f2019-04-29 15:23:20 +0200220uint8_t
221lysc_iff_getop(uint8_t *list, int pos)
Radek Krejci151a5b72018-10-19 14:21:44 +0200222{
223 uint8_t *item;
224 uint8_t mask = 3, result;
225
226 assert(pos >= 0);
227
228 item = &list[pos / 4];
229 result = (*item) & (mask << 2 * (pos % 4));
230 return result >> 2 * (pos % 4);
231}
232
Radek Krejci151a5b72018-10-19 14:21:44 +0200233static int
234lysc_iffeature_value_(const struct lysc_iffeature *iff, int *index_e, int *index_f)
235{
236 uint8_t op;
237 int a, b;
238
Radek Krejci693262f2019-04-29 15:23:20 +0200239 op = lysc_iff_getop(iff->expr, *index_e);
Radek Krejci151a5b72018-10-19 14:21:44 +0200240 (*index_e)++;
241
242 switch (op) {
243 case LYS_IFF_F:
244 /* resolve feature */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200245 return lysc_feature_value(iff->features[(*index_f)++]);
Radek Krejci151a5b72018-10-19 14:21:44 +0200246 case LYS_IFF_NOT:
247 /* invert result */
248 return lysc_iffeature_value_(iff, index_e, index_f) ? 0 : 1;
249 case LYS_IFF_AND:
250 case LYS_IFF_OR:
251 a = lysc_iffeature_value_(iff, index_e, index_f);
252 b = lysc_iffeature_value_(iff, index_e, index_f);
253 if (op == LYS_IFF_AND) {
254 return a && b;
255 } else { /* LYS_IFF_OR */
256 return a || b;
257 }
258 }
259
260 return 0;
261}
262
263API int
264lysc_iffeature_value(const struct lysc_iffeature *iff)
265{
266 int index_e = 0, index_f = 0;
267
268 LY_CHECK_ARG_RET(NULL, iff, -1);
269
270 if (iff->expr) {
271 return lysc_iffeature_value_(iff, &index_e, &index_f);
272 }
273 return 0;
274}
275
Radek Krejci151a5b72018-10-19 14:21:44 +0200276/**
277 * @brief Enable/Disable the specified feature in the module.
278 *
279 * If the feature is already set to the desired value, LY_SUCCESS is returned.
280 * By changing the feature, also all the feature which depends on it via their
281 * if-feature statements are again evaluated (disabled if a if-feature statemen
282 * evaluates to false).
283 *
Radek Krejci0af46292019-01-11 16:02:31 +0100284 * @param[in] mod Module where to set (search for) the feature.
Radek Krejci151a5b72018-10-19 14:21:44 +0200285 * @param[in] name Name of the feature to set. Asterisk ('*') can be used to
286 * set all the features in the module.
287 * @param[in] value Desired value of the feature: 1 (enable) or 0 (disable).
288 * @return LY_ERR value.
289 */
290static LY_ERR
Radek Krejci0af46292019-01-11 16:02:31 +0100291lys_feature_change(const struct lys_module *mod, const char *name, int value)
Radek Krejci151a5b72018-10-19 14:21:44 +0200292{
293 int all = 0;
Radek Krejcica3db002018-11-01 10:31:01 +0100294 unsigned int u, changed_count, disabled_count;
Radek Krejci151a5b72018-10-19 14:21:44 +0200295 struct lysc_feature *f, **df;
296 struct lysc_iffeature *iff;
297 struct ly_set *changed;
Radek Krejci0af46292019-01-11 16:02:31 +0100298 struct ly_ctx *ctx = mod->ctx; /* shortcut */
Radek Krejci151a5b72018-10-19 14:21:44 +0200299
Radek Krejci0af46292019-01-11 16:02:31 +0100300 if (!mod->compiled) {
301 LOGERR(ctx, LY_EINVAL, "Module \"%s\" is not implemented so all its features are permanently disabled without a chance to change it.",
302 mod->name);
303 return LY_EINVAL;
304 }
305 if (!mod->compiled->features) {
306 LOGERR(ctx, LY_EINVAL, "Unable to switch feature since the module \"%s\" has no features.", mod->name);
Radek Krejci151a5b72018-10-19 14:21:44 +0200307 return LY_EINVAL;
308 }
309
310 if (!strcmp(name, "*")) {
311 /* enable all */
312 all = 1;
313 }
314 changed = ly_set_new();
Radek Krejcica3db002018-11-01 10:31:01 +0100315 changed_count = 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200316
Radek Krejcica3db002018-11-01 10:31:01 +0100317run:
Radek Krejci0af46292019-01-11 16:02:31 +0100318 for (disabled_count = u = 0; u < LY_ARRAY_SIZE(mod->compiled->features); ++u) {
319 f = &mod->compiled->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200320 if (all || !strcmp(f->name, name)) {
321 if ((value && (f->flags & LYS_FENABLED)) || (!value && !(f->flags & LYS_FENABLED))) {
322 if (all) {
323 /* skip already set features */
324 continue;
325 } else {
326 /* feature already set correctly */
327 ly_set_free(changed, NULL);
328 return LY_SUCCESS;
329 }
330 }
331
332 if (value) { /* enable */
333 /* check referenced features if they are enabled */
334 LY_ARRAY_FOR(f->iffeatures, struct lysc_iffeature, iff) {
335 if (!lysc_iffeature_value(iff)) {
336 if (all) {
Radek Krejcica3db002018-11-01 10:31:01 +0100337 ++disabled_count;
Radek Krejci151a5b72018-10-19 14:21:44 +0200338 goto next;
339 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100340 LOGERR(ctx, LY_EDENIED,
Radek Krejci151a5b72018-10-19 14:21:44 +0200341 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
342 f->name);
343 ly_set_free(changed, NULL);
344 return LY_EDENIED;
345 }
346 }
347 }
348 /* enable the feature */
349 f->flags |= LYS_FENABLED;
350 } else { /* disable */
351 /* disable the feature */
352 f->flags &= ~LYS_FENABLED;
353 }
354
355 /* remember the changed feature */
356 ly_set_add(changed, f, LY_SET_OPT_USEASLIST);
357
358 if (!all) {
359 /* stop in case changing a single feature */
360 break;
361 }
362 }
363next:
364 ;
365 }
366
367 if (!all && !changed->count) {
Radek Krejci0af46292019-01-11 16:02:31 +0100368 LOGERR(ctx, LY_EINVAL, "Feature \"%s\" not found in module \"%s\".", name, mod->name);
Radek Krejci151a5b72018-10-19 14:21:44 +0200369 ly_set_free(changed, NULL);
370 return LY_EINVAL;
371 }
372
Radek Krejcica3db002018-11-01 10:31:01 +0100373 if (value && all && disabled_count) {
374 if (changed_count == changed->count) {
375 /* no change in last run -> not able to enable all ... */
376 /* ... print errors */
Radek Krejci0af46292019-01-11 16:02:31 +0100377 for (u = 0; disabled_count && u < LY_ARRAY_SIZE(mod->compiled->features); ++u) {
378 if (!(mod->compiled->features[u].flags & LYS_FENABLED)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100379 LOGERR(ctx, LY_EDENIED,
Radek Krejcica3db002018-11-01 10:31:01 +0100380 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
Radek Krejci0af46292019-01-11 16:02:31 +0100381 mod->compiled->features[u].name);
Radek Krejcica3db002018-11-01 10:31:01 +0100382 --disabled_count;
383 }
384 }
385 /* ... restore the original state */
386 for (u = 0; u < changed->count; ++u) {
387 f = changed->objs[u];
388 /* re-disable the feature */
389 f->flags &= ~LYS_FENABLED;
390 }
391
392 ly_set_free(changed, NULL);
393 return LY_EDENIED;
394 } else {
395 /* we did some change in last run, try it again */
396 changed_count = changed->count;
397 goto run;
398 }
399 }
400
Radek Krejci151a5b72018-10-19 14:21:44 +0200401 /* reflect change(s) in the dependent features */
402 for (u = 0; u < changed->count; ++u) {
403 /* If a dependent feature is enabled, it can be now changed by the change (to false) of the value of
404 * its if-feature statements. The reverse logic, automatically enable feature when its feature is enabled
405 * is not done - by default, features are disabled and must be explicitely enabled. */
406 f = changed->objs[u];
407 LY_ARRAY_FOR(f->depfeatures, struct lysc_feature*, df) {
408 if (!((*df)->flags & LYS_FENABLED)) {
409 /* not enabled, nothing to do */
410 continue;
411 }
412 /* check the feature's if-features which could change by the previous change of our feature */
413 LY_ARRAY_FOR((*df)->iffeatures, struct lysc_iffeature, iff) {
414 if (!lysc_iffeature_value(iff)) {
415 /* the feature must be disabled now */
416 (*df)->flags &= ~LYS_FENABLED;
417 /* add the feature into the list of changed features */
418 ly_set_add(changed, *df, LY_SET_OPT_USEASLIST);
419 break;
420 }
421 }
422 }
423 }
424
425 ly_set_free(changed, NULL);
426 return LY_SUCCESS;
427}
428
429API LY_ERR
Radek Krejcied5acc52019-04-25 15:57:04 +0200430lys_feature_enable(const struct lys_module *module, const char *feature)
Radek Krejci151a5b72018-10-19 14:21:44 +0200431{
Radek Krejci0af46292019-01-11 16:02:31 +0100432 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200433
Radek Krejcied5acc52019-04-25 15:57:04 +0200434 return lys_feature_change((struct lys_module*)module, feature, 1);
Radek Krejci151a5b72018-10-19 14:21:44 +0200435}
436
437API LY_ERR
Radek Krejcied5acc52019-04-25 15:57:04 +0200438lys_feature_disable(const struct lys_module *module, const char *feature)
Radek Krejci151a5b72018-10-19 14:21:44 +0200439{
Radek Krejci0af46292019-01-11 16:02:31 +0100440 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200441
Radek Krejcied5acc52019-04-25 15:57:04 +0200442 return lys_feature_change((struct lys_module*)module, feature, 0);
Radek Krejci151a5b72018-10-19 14:21:44 +0200443}
444
445API int
446lys_feature_value(const struct lys_module *module, const char *feature)
447{
448 struct lysc_feature *f;
449 struct lysc_module *mod;
450 unsigned int u;
451
452 LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, -1);
453 mod = module->compiled;
454
455 /* search for the specified feature */
456 for (u = 0; u < LY_ARRAY_SIZE(mod->features); ++u) {
Radek Krejci2c4e7172018-10-19 15:56:26 +0200457 f = &mod->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200458 if (!strcmp(f->name, feature)) {
459 if (f->flags & LYS_FENABLED) {
460 return 1;
461 } else {
462 return 0;
463 }
464 }
465 }
466
467 /* feature definition not found */
468 return -1;
469}
470
Radek Krejcia3045382018-11-22 14:30:31 +0100471API const struct lysc_iffeature *
472lys_is_disabled(const struct lysc_node *node, int recursive)
473{
474 unsigned int u;
Radek Krejcia3045382018-11-22 14:30:31 +0100475
476 LY_CHECK_ARG_RET(NULL, node, NULL);
477
478 while(node) {
479 if (node->nodetype & LYS_CHOICE) {
480 return NULL;
481 }
482
Radek Krejci056d0a82018-12-06 16:57:25 +0100483 if (node->iffeatures) {
Radek Krejcia3045382018-11-22 14:30:31 +0100484 /* check local if-features */
Radek Krejci056d0a82018-12-06 16:57:25 +0100485 LY_ARRAY_FOR(node->iffeatures, u) {
486 if (!lysc_iffeature_value(&node->iffeatures[u])) {
487 return &node->iffeatures[u];
Radek Krejcia3045382018-11-22 14:30:31 +0100488 }
489 }
490 }
491
492 if (!recursive) {
493 return NULL;
494 }
495
496 /* go through parents */
497 node = node->parent;
498 }
499 return NULL;
500}
501
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100502struct lysp_submodule *
503lys_parse_mem_submodule(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, struct ly_parser_ctx *main_ctx,
504 LY_ERR (*custom_check)(struct ly_ctx*, struct lysp_module*, struct lysp_submodule*, void*), void *check_data)
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200505{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100506 LY_ERR ret = LY_EINVAL;
507 struct lysp_submodule *submod = NULL, *latest_sp;
508 struct ly_parser_ctx context = {0};
509
510 LY_CHECK_ARG_RET(ctx, ctx, data, NULL);
511
512 context.ctx = ctx;
513 context.line = 1;
514
515 /* map the typedefs and groupings list from main context to the submodule's context */
516 memcpy(&context.tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
517 memcpy(&context.grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
518
519 switch (format) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200520 /* TODO not yet supported
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100521 case LYS_IN_YIN:
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100522 mod = yin_read_module();
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100523 break;
Radek Krejcied5acc52019-04-25 15:57:04 +0200524 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100525 case LYS_IN_YANG:
526 ret = yang_parse_submodule(&context, data, &submod);
527 break;
528 default:
529 LOGERR(context.ctx, LY_EINVAL, "Invalid schema input format.");
530 break;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200531 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100532 LY_CHECK_RET(ret, NULL);
533
534 /* make sure that the newest revision is at position 0 */
535 lysp_sort_revisions(submod->revs);
536
537 if (custom_check) {
538 LY_CHECK_GOTO(custom_check(context.ctx, NULL, submod, check_data), error);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200539 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100540
541 /* decide the latest revision */
542 latest_sp = ly_ctx_get_submodule(context.ctx, submod->belongsto, submod->name, NULL);
543 if (latest_sp) {
544 if (submod->revs) {
545 if (!latest_sp->revs) {
546 /* latest has no revision, so mod is anyway newer */
547 submod->latest_revision = latest_sp->latest_revision;
548 latest_sp->latest_revision = 0;
549 } else {
550 if (strcmp(submod->revs[0].date, latest_sp->revs[0].date) > 0) {
551 submod->latest_revision = latest_sp->latest_revision;
552 latest_sp->latest_revision = 0;
553 }
554 }
555 }
556 } else {
557 submod->latest_revision = 1;
558 }
559
560 /* remap possibly changed and reallocated typedefs and groupings list back to the main context */
561 memcpy(&main_ctx->tpdfs_nodes, &context.tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
562 memcpy(&main_ctx->grps_nodes, &context.grps_nodes, sizeof main_ctx->grps_nodes);
563
564 return submod;
565error:
566 lysp_submodule_free(ctx, submod);
567 return NULL;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200568}
569
Radek Krejcid33273d2018-10-25 14:55:52 +0200570struct lys_module *
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100571lys_parse_mem_module(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, int implement,
572 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
573 void *check_data)
Radek Krejci86d106e2018-10-18 09:53:19 +0200574{
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100575 struct lys_module *mod = NULL, *latest, *mod_dup;
Radek Krejci086c7132018-10-26 15:29:04 +0200576 struct lysp_import *imp;
577 struct lysp_include *inc;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100578 LY_ERR ret = LY_EINVAL;
Radek Krejci086c7132018-10-26 15:29:04 +0200579 unsigned int u, i;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100580 struct ly_parser_ctx context = {0};
Radek Krejci86d106e2018-10-18 09:53:19 +0200581
582 LY_CHECK_ARG_RET(ctx, ctx, data, NULL);
583
Radek Krejcibbe09a92018-11-08 09:36:54 +0100584 context.ctx = ctx;
585 context.line = 1;
586
Radek Krejci86d106e2018-10-18 09:53:19 +0200587 mod = calloc(1, sizeof *mod);
588 LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100589 mod->ctx = ctx;
Radek Krejci86d106e2018-10-18 09:53:19 +0200590
591 switch (format) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200592 /* TODO not yet supported
Radek Krejci86d106e2018-10-18 09:53:19 +0200593 case LYS_IN_YIN:
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100594 mod = yin_read_module();
Radek Krejci86d106e2018-10-18 09:53:19 +0200595 break;
Radek Krejcied5acc52019-04-25 15:57:04 +0200596 */
Radek Krejci86d106e2018-10-18 09:53:19 +0200597 case LYS_IN_YANG:
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100598 ret = yang_parse_module(&context, data, mod);
Radek Krejci86d106e2018-10-18 09:53:19 +0200599 break;
600 default:
601 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
602 break;
603 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100604 LY_CHECK_ERR_RET(ret, lys_module_free(mod, NULL), NULL);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200605
606 /* make sure that the newest revision is at position 0 */
607 lysp_sort_revisions(mod->parsed->revs);
Radek Krejci0af46292019-01-11 16:02:31 +0100608 if (mod->parsed->revs) {
609 mod->revision = lydict_insert(ctx, mod->parsed->revs[0].date, 0);
610 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200611
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100612 if (custom_check) {
613 LY_CHECK_GOTO(custom_check(ctx, mod->parsed, NULL, check_data), error);
614 }
615
Radek Krejci86d106e2018-10-18 09:53:19 +0200616 if (implement) {
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200617 /* mark the loaded module implemented */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100618 if (ly_ctx_get_module_implemented(ctx, mod->name)) {
619 LOGERR(ctx, LY_EDENIED, "Module \"%s\" is already implemented in the context.", mod->name);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100620 goto error;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200621 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100622 mod->implemented = 1;
Radek Krejci86d106e2018-10-18 09:53:19 +0200623 }
624
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100625 /* check for duplicity in the context */
Radek Krejci0af46292019-01-11 16:02:31 +0100626 mod_dup = (struct lys_module*)ly_ctx_get_module(ctx, mod->name, mod->revision);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100627 if (mod_dup) {
628 if (mod_dup->parsed) {
629 /* error */
Radek Krejcid33273d2018-10-25 14:55:52 +0200630 if (mod->parsed->revs) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100631 LOGERR(ctx, LY_EEXIST, "Module \"%s\" of revision \"%s\" is already present in the context.",
632 mod->name, mod->parsed->revs[0].date);
Radek Krejcid33273d2018-10-25 14:55:52 +0200633 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100634 LOGERR(ctx, LY_EEXIST, "Module \"%s\" with no revision is already present in the context.",
635 mod->name);
Radek Krejcid33273d2018-10-25 14:55:52 +0200636 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100637 goto error;
638 } else {
639 /* add the parsed data to the currently compiled-only module in the context */
640 mod_dup->parsed = mod->parsed;
641 mod_dup->parsed->mod = mod_dup;
642 mod->parsed = NULL;
643 lys_module_free(mod, NULL);
644 mod = mod_dup;
645 goto finish_parsing;
Radek Krejcid33273d2018-10-25 14:55:52 +0200646 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100647 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200648
649#if 0
650 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
651 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
652 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
653 * the anotation definitions available in the internal schema structure. There is another hack in schema
654 * printers to do not print this internally added annotation. */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100655 if (ly_strequal(mod->name, "ietf-netconf", 0)) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200656 if (lyp_add_ietf_netconf_annotations(mod)) {
657 lys_free(mod, NULL, 1, 1);
658 return NULL;
659 }
660 }
661#endif
662
Radek Krejci0af46292019-01-11 16:02:31 +0100663 if (!mod->implemented) {
664 /* pre-compile features of the module */
Radek Krejci693262f2019-04-29 15:23:20 +0200665 LY_CHECK_GOTO(lys_feature_precompile(ctx, mod, mod->parsed->features, &mod->off_features), error);
Radek Krejci0af46292019-01-11 16:02:31 +0100666 }
667
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100668 /* decide the latest revision */
669 latest = (struct lys_module*)ly_ctx_get_module_latest(ctx, mod->name);
670 if (latest) {
Radek Krejci0af46292019-01-11 16:02:31 +0100671 if (mod->revision) {
672 if (!latest->revision) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100673 /* latest has no revision, so mod is anyway newer */
674 mod->latest_revision = latest->latest_revision;
675 latest->latest_revision = 0;
Radek Krejci0af46292019-01-11 16:02:31 +0100676 } else if (strcmp(mod->revision, latest->revision) > 0) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100677 mod->latest_revision = latest->latest_revision;
678 latest->latest_revision = 0;
Radek Krejcid33273d2018-10-25 14:55:52 +0200679 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200680 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100681 } else {
682 mod->latest_revision = 1;
683 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200684
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100685 /* add into context */
686 ly_set_add(&ctx->list, mod, LY_SET_OPT_USEASLIST);
Radek Krejcid33273d2018-10-25 14:55:52 +0200687
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100688finish_parsing:
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100689 /* resolve imports */
690 mod->parsed->parsing = 1;
691 LY_ARRAY_FOR(mod->parsed->imports, u) {
692 imp = &mod->parsed->imports[u];
693 if (!imp->module && lysp_load_module(ctx, imp->name, imp->rev[0] ? imp->rev : NULL, 0, 0, &imp->module)) {
694 goto error_ctx;
Radek Krejci086c7132018-10-26 15:29:04 +0200695 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100696 /* check for importing the same module twice */
697 for (i = 0; i < u; ++i) {
698 if (imp->module == mod->parsed->imports[i].module) {
699 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 +0100700 goto error_ctx;
Radek Krejci086c7132018-10-26 15:29:04 +0200701 }
702 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200703 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100704 LY_ARRAY_FOR(mod->parsed->includes, u) {
705 inc = &mod->parsed->includes[u];
706 if (!inc->submodule && lysp_load_submodule(&context, mod->parsed, inc)) {
707 goto error_ctx;
708 }
Radek Krejci0af46292019-01-11 16:02:31 +0100709 if (!mod->implemented) {
710 /* pre-compile features of the module */
Radek Krejci693262f2019-04-29 15:23:20 +0200711 LY_CHECK_GOTO(lys_feature_precompile(ctx, mod, inc->submodule->features, &mod->off_features), error);
Radek Krejci0af46292019-01-11 16:02:31 +0100712 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100713 }
714 mod->parsed->parsing = 0;
715
716 /* check name collisions - typedefs and groupings */
717 LY_CHECK_GOTO(lysp_check_typedefs(&context, mod->parsed), error_ctx);
Radek Krejcid33273d2018-10-25 14:55:52 +0200718
Radek Krejci86d106e2018-10-18 09:53:19 +0200719 return mod;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100720
721error_ctx:
722 ly_set_rm(&ctx->list, mod, NULL);
723error:
724 lys_module_free(mod, NULL);
725 ly_set_erase(&context.tpdfs_nodes, NULL);
726 return NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +0200727}
728
Radek Krejcid14e9692018-11-01 11:00:37 +0100729API struct lys_module *
Radek Krejci86d106e2018-10-18 09:53:19 +0200730lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format)
731{
Radek Krejci096235c2019-01-11 11:12:19 +0100732 struct lys_module *mod;
733
734 mod = lys_parse_mem_module(ctx, data, format, 1, NULL, NULL);
735 LY_CHECK_RET(!mod, NULL);
736
737 if (lys_compile(mod, 0)) {
738 ly_set_rm(&ctx->list, mod, NULL);
739 lys_module_free(mod, NULL);
740 return NULL;
741 }
742 return mod;
Radek Krejci86d106e2018-10-18 09:53:19 +0200743}
744
745static void
746lys_parse_set_filename(struct ly_ctx *ctx, const char **filename, int fd)
747{
Radek Krejci65639b92018-11-27 10:51:37 +0100748 char path[PATH_MAX];
Radek Krejci86d106e2018-10-18 09:53:19 +0200749
750#ifdef __APPLE__
751 if (fcntl(fd, F_GETPATH, path) != -1) {
752 *filename = lydict_insert(ctx, path, 0);
753 }
754#else
Radek Krejci65639b92018-11-27 10:51:37 +0100755 int len;
756 char proc_path[32];
757
Radek Krejci86d106e2018-10-18 09:53:19 +0200758 /* get URI if there is /proc */
759 sprintf(proc_path, "/proc/self/fd/%d", fd);
760 if ((len = readlink(proc_path, path, PATH_MAX - 1)) > 0) {
761 *filename = lydict_insert(ctx, path, len);
762 }
763#endif
764}
765
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100766void *
Radek Krejci3b1f9292018-11-08 10:58:35 +0100767lys_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 +0100768 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
769 void *check_data)
Radek Krejci86d106e2018-10-18 09:53:19 +0200770{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100771 void *result;
772 struct lys_module *mod = NULL;
773 struct lysp_submodule *submod = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +0200774 size_t length;
775 char *addr;
776
777 LY_CHECK_ARG_RET(ctx, ctx, NULL);
778 if (fd < 0) {
779 LOGARG(ctx, fd);
780 return NULL;
781 }
782
783 LY_CHECK_RET(ly_mmap(ctx, fd, &length, (void **)&addr), NULL);
784 if (!addr) {
785 LOGERR(ctx, LY_EINVAL, "Empty schema file.");
786 return NULL;
787 }
788
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100789 if (main_ctx) {
790 result = submod = lys_parse_mem_submodule(ctx, addr, format, main_ctx, custom_check, check_data);
791 } else {
792 result = mod = lys_parse_mem_module(ctx, addr, format, implement, custom_check, check_data);
Radek Krejci096235c2019-01-11 11:12:19 +0100793 if (mod && implement && lys_compile(mod, 0)) {
794 ly_set_rm(&ctx->list, mod, NULL);
795 lys_module_free(mod, NULL);
796 result = mod = NULL;
797 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100798 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200799 ly_munmap(addr, length);
800
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100801 if (mod && !mod->filepath) {
802 lys_parse_set_filename(ctx, &mod->filepath, fd);
803 } else if (submod && !submod->filepath) {
804 lys_parse_set_filename(ctx, &submod->filepath, fd);
Radek Krejci86d106e2018-10-18 09:53:19 +0200805 }
806
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100807 return result;
808}
809
810struct lys_module *
811lys_parse_fd_module(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, int implement,
812 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
813 void *check_data)
814{
815 return (struct lys_module*)lys_parse_fd_(ctx, fd, format, implement, NULL, custom_check, check_data);
816}
817
818struct lysp_submodule *
819lys_parse_fd_submodule(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, struct ly_parser_ctx *main_ctx,
820 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
821 void *check_data)
822{
823 assert(main_ctx);
824 return (struct lysp_submodule*)lys_parse_fd_(ctx, fd, format, 0, main_ctx, custom_check, check_data);
Radek Krejci86d106e2018-10-18 09:53:19 +0200825}
826
Radek Krejcid14e9692018-11-01 11:00:37 +0100827API struct lys_module *
Radek Krejci86d106e2018-10-18 09:53:19 +0200828lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format)
829{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100830 return lys_parse_fd_module(ctx, fd, format, 1, NULL, NULL);
Radek Krejci86d106e2018-10-18 09:53:19 +0200831}
832
Radek Krejcid33273d2018-10-25 14:55:52 +0200833struct lys_module *
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100834lys_parse_path_(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, int implement,
835 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 +0200836{
837 int fd;
Radek Krejcid33273d2018-10-25 14:55:52 +0200838 struct lys_module *mod;
Radek Krejci86d106e2018-10-18 09:53:19 +0200839 const char *rev, *dot, *filename;
840 size_t len;
841
842 LY_CHECK_ARG_RET(ctx, ctx, path, NULL);
843
844 fd = open(path, O_RDONLY);
845 LY_CHECK_ERR_RET(fd == -1, LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)), NULL);
846
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100847 mod = lys_parse_fd_module(ctx, fd, format, implement, custom_check, check_data);
Radek Krejci86d106e2018-10-18 09:53:19 +0200848 close(fd);
849 LY_CHECK_RET(!mod, NULL);
850
851 /* check that name and revision match filename */
852 filename = strrchr(path, '/');
853 if (!filename) {
854 filename = path;
855 } else {
856 filename++;
857 }
858 rev = strchr(filename, '@');
859 dot = strrchr(filename, '.');
860
861 /* name */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100862 len = strlen(mod->name);
863 if (strncmp(filename, mod->name, len) ||
Radek Krejci86d106e2018-10-18 09:53:19 +0200864 ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100865 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name);
Radek Krejci86d106e2018-10-18 09:53:19 +0200866 }
867 if (rev) {
868 len = dot - ++rev;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200869 if (!mod->parsed->revs || len != 10 || strncmp(mod->parsed->revs[0].date, rev, len)) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200870 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Radek Krejcib7db73a2018-10-24 14:18:40 +0200871 mod->parsed->revs ? mod->parsed->revs[0].date : "none");
Radek Krejci86d106e2018-10-18 09:53:19 +0200872 }
873 }
874
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100875 if (!mod->filepath) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200876 /* store URI */
877 char rpath[PATH_MAX];
878 if (realpath(path, rpath) != NULL) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100879 mod->filepath = lydict_insert(ctx, rpath, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +0200880 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100881 mod->filepath = lydict_insert(ctx, path, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +0200882 }
883 }
884
885 return mod;
886}
887
Radek Krejcid14e9692018-11-01 11:00:37 +0100888API struct lys_module *
Radek Krejcid33273d2018-10-25 14:55:52 +0200889lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format)
890{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100891 return lys_parse_path_(ctx, path, format, 1, NULL, NULL);
Radek Krejcid33273d2018-10-25 14:55:52 +0200892}
893
894API LY_ERR
895lys_search_localfile(const char * const *searchpaths, int cwd, const char *name, const char *revision,
896 char **localfile, LYS_INFORMAT *format)
897{
898 size_t len, flen, match_len = 0, dir_len;
899 int i, implicit_cwd = 0, ret = EXIT_FAILURE;
900 char *wd, *wn = NULL;
901 DIR *dir = NULL;
902 struct dirent *file;
903 char *match_name = NULL;
904 LYS_INFORMAT format_aux, match_format = 0;
905 struct ly_set *dirs;
906 struct stat st;
907
908 LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL);
909
910 /* start to fill the dir fifo with the context's search path (if set)
911 * and the current working directory */
912 dirs = ly_set_new();
913 if (!dirs) {
914 LOGMEM(NULL);
915 return EXIT_FAILURE;
916 }
917
918 len = strlen(name);
919 if (cwd) {
920 wd = get_current_dir_name();
921 if (!wd) {
922 LOGMEM(NULL);
923 goto cleanup;
924 } else {
925 /* add implicit current working directory (./) to be searched,
926 * this directory is not searched recursively */
927 if (ly_set_add(dirs, wd, 0) == -1) {
928 goto cleanup;
929 }
930 implicit_cwd = 1;
931 }
932 }
933 if (searchpaths) {
934 for (i = 0; searchpaths[i]; i++) {
935 /* check for duplicities with the implicit current working directory */
936 if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) {
937 implicit_cwd = 0;
938 continue;
939 }
940 wd = strdup(searchpaths[i]);
941 if (!wd) {
942 LOGMEM(NULL);
943 goto cleanup;
944 } else if (ly_set_add(dirs, wd, 0) == -1) {
945 goto cleanup;
946 }
947 }
948 }
949 wd = NULL;
950
951 /* start searching */
952 while (dirs->count) {
953 free(wd);
954 free(wn); wn = NULL;
955
956 dirs->count--;
957 wd = (char *)dirs->objs[dirs->count];
958 dirs->objs[dirs->count] = NULL;
959 LOGVRB("Searching for \"%s\" in %s.", name, wd);
960
961 if (dir) {
962 closedir(dir);
963 }
964 dir = opendir(wd);
965 dir_len = strlen(wd);
966 if (!dir) {
967 LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno));
968 } else {
969 while ((file = readdir(dir))) {
970 if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) {
971 /* skip . and .. */
972 continue;
973 }
974 free(wn);
975 if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) {
976 LOGMEM(NULL);
977 goto cleanup;
978 }
979 if (stat(wn, &st) == -1) {
980 LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)",
981 file->d_name, wd, strerror(errno));
982 continue;
983 }
984 if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) {
985 /* we have another subdirectory in searchpath to explore,
986 * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */
987 if (ly_set_add(dirs, wn, 0) == -1) {
988 goto cleanup;
989 }
990 /* continue with the next item in current directory */
991 wn = NULL;
992 continue;
993 } else if (!S_ISREG(st.st_mode)) {
994 /* not a regular file (note that we see the target of symlinks instead of symlinks */
995 continue;
996 }
997
998 /* here we know that the item is a file which can contain a module */
999 if (strncmp(name, file->d_name, len) ||
1000 (file->d_name[len] != '.' && file->d_name[len] != '@')) {
1001 /* different filename than the module we search for */
1002 continue;
1003 }
1004
1005 /* get type according to filename suffix */
1006 flen = strlen(file->d_name);
Radek Krejcied5acc52019-04-25 15:57:04 +02001007 if (!strcmp(&file->d_name[flen - 5], ".yang")) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001008 format_aux = LYS_IN_YANG;
Radek Krejcied5acc52019-04-25 15:57:04 +02001009 /* TODO YIN parser
1010 } else if (!strcmp(&file->d_name[flen - 4], ".yin")) {
1011 format_aux = LYS_IN_YIN;
1012 */
Radek Krejcid33273d2018-10-25 14:55:52 +02001013 } else {
1014 /* not supportde suffix/file format */
1015 continue;
1016 }
1017
1018 if (revision) {
1019 /* we look for the specific revision, try to get it from the filename */
1020 if (file->d_name[len] == '@') {
1021 /* check revision from the filename */
1022 if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) {
1023 /* another revision */
1024 continue;
1025 } else {
1026 /* exact revision */
1027 free(match_name);
1028 match_name = wn;
1029 wn = NULL;
1030 match_len = dir_len + 1 + len;
1031 match_format = format_aux;
1032 goto success;
1033 }
1034 } else {
1035 /* continue trying to find exact revision match, use this only if not found */
1036 free(match_name);
1037 match_name = wn;
1038 wn = NULL;
1039 match_len = dir_len + 1 +len;
1040 match_format = format_aux;
1041 continue;
1042 }
1043 } else {
1044 /* remember the revision and try to find the newest one */
1045 if (match_name) {
1046 if (file->d_name[len] != '@' ||
1047 lysp_check_date(NULL, &file->d_name[len + 1], flen - (format_aux == LYS_IN_YANG ? 5 : 4) - len - 1, NULL)) {
1048 continue;
1049 } else if (match_name[match_len] == '@' &&
1050 (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) {
1051 continue;
1052 }
1053 free(match_name);
1054 }
1055
1056 match_name = wn;
1057 wn = NULL;
1058 match_len = dir_len + 1 + len;
1059 match_format = format_aux;
1060 continue;
1061 }
1062 }
1063 }
1064 }
1065
1066success:
1067 (*localfile) = match_name;
1068 match_name = NULL;
1069 if (format) {
1070 (*format) = match_format;
1071 }
1072 ret = EXIT_SUCCESS;
1073
1074cleanup:
1075 free(wn);
1076 free(wd);
1077 if (dir) {
1078 closedir(dir);
1079 }
1080 free(match_name);
1081 ly_set_free(dirs, free);
1082
1083 return ret;
1084}
1085