blob: ca093fce6e59f41bf28aeaf372405d00de4c42f1 [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 Krejci6e67c402019-05-02 09:55:39 +0200300 if (!strcmp(name, "*")) {
301 /* enable all */
302 all = 1;
303 }
304
Radek Krejci0af46292019-01-11 16:02:31 +0100305 if (!mod->compiled) {
306 LOGERR(ctx, LY_EINVAL, "Module \"%s\" is not implemented so all its features are permanently disabled without a chance to change it.",
307 mod->name);
308 return LY_EINVAL;
309 }
310 if (!mod->compiled->features) {
Radek Krejci6e67c402019-05-02 09:55:39 +0200311 if (all) {
312 /* no feature to enable */
313 return LY_SUCCESS;
314 }
Radek Krejci0af46292019-01-11 16:02:31 +0100315 LOGERR(ctx, LY_EINVAL, "Unable to switch feature since the module \"%s\" has no features.", mod->name);
Radek Krejci151a5b72018-10-19 14:21:44 +0200316 return LY_EINVAL;
317 }
318
Radek Krejci151a5b72018-10-19 14:21:44 +0200319 changed = ly_set_new();
Radek Krejcica3db002018-11-01 10:31:01 +0100320 changed_count = 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200321
Radek Krejcica3db002018-11-01 10:31:01 +0100322run:
Radek Krejci0af46292019-01-11 16:02:31 +0100323 for (disabled_count = u = 0; u < LY_ARRAY_SIZE(mod->compiled->features); ++u) {
324 f = &mod->compiled->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200325 if (all || !strcmp(f->name, name)) {
326 if ((value && (f->flags & LYS_FENABLED)) || (!value && !(f->flags & LYS_FENABLED))) {
327 if (all) {
328 /* skip already set features */
329 continue;
330 } else {
331 /* feature already set correctly */
332 ly_set_free(changed, NULL);
333 return LY_SUCCESS;
334 }
335 }
336
337 if (value) { /* enable */
338 /* check referenced features if they are enabled */
339 LY_ARRAY_FOR(f->iffeatures, struct lysc_iffeature, iff) {
340 if (!lysc_iffeature_value(iff)) {
341 if (all) {
Radek Krejcica3db002018-11-01 10:31:01 +0100342 ++disabled_count;
Radek Krejci151a5b72018-10-19 14:21:44 +0200343 goto next;
344 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100345 LOGERR(ctx, LY_EDENIED,
Radek Krejci151a5b72018-10-19 14:21:44 +0200346 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
347 f->name);
348 ly_set_free(changed, NULL);
349 return LY_EDENIED;
350 }
351 }
352 }
353 /* enable the feature */
354 f->flags |= LYS_FENABLED;
355 } else { /* disable */
356 /* disable the feature */
357 f->flags &= ~LYS_FENABLED;
358 }
359
360 /* remember the changed feature */
361 ly_set_add(changed, f, LY_SET_OPT_USEASLIST);
362
363 if (!all) {
364 /* stop in case changing a single feature */
365 break;
366 }
367 }
368next:
369 ;
370 }
371
372 if (!all && !changed->count) {
Radek Krejci0af46292019-01-11 16:02:31 +0100373 LOGERR(ctx, LY_EINVAL, "Feature \"%s\" not found in module \"%s\".", name, mod->name);
Radek Krejci151a5b72018-10-19 14:21:44 +0200374 ly_set_free(changed, NULL);
375 return LY_EINVAL;
376 }
377
Radek Krejcica3db002018-11-01 10:31:01 +0100378 if (value && all && disabled_count) {
379 if (changed_count == changed->count) {
380 /* no change in last run -> not able to enable all ... */
381 /* ... print errors */
Radek Krejci0af46292019-01-11 16:02:31 +0100382 for (u = 0; disabled_count && u < LY_ARRAY_SIZE(mod->compiled->features); ++u) {
383 if (!(mod->compiled->features[u].flags & LYS_FENABLED)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100384 LOGERR(ctx, LY_EDENIED,
Radek Krejcica3db002018-11-01 10:31:01 +0100385 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
Radek Krejci0af46292019-01-11 16:02:31 +0100386 mod->compiled->features[u].name);
Radek Krejcica3db002018-11-01 10:31:01 +0100387 --disabled_count;
388 }
389 }
390 /* ... restore the original state */
391 for (u = 0; u < changed->count; ++u) {
392 f = changed->objs[u];
393 /* re-disable the feature */
394 f->flags &= ~LYS_FENABLED;
395 }
396
397 ly_set_free(changed, NULL);
398 return LY_EDENIED;
399 } else {
400 /* we did some change in last run, try it again */
401 changed_count = changed->count;
402 goto run;
403 }
404 }
405
Radek Krejci151a5b72018-10-19 14:21:44 +0200406 /* reflect change(s) in the dependent features */
407 for (u = 0; u < changed->count; ++u) {
408 /* If a dependent feature is enabled, it can be now changed by the change (to false) of the value of
409 * its if-feature statements. The reverse logic, automatically enable feature when its feature is enabled
410 * is not done - by default, features are disabled and must be explicitely enabled. */
411 f = changed->objs[u];
412 LY_ARRAY_FOR(f->depfeatures, struct lysc_feature*, df) {
413 if (!((*df)->flags & LYS_FENABLED)) {
414 /* not enabled, nothing to do */
415 continue;
416 }
417 /* check the feature's if-features which could change by the previous change of our feature */
418 LY_ARRAY_FOR((*df)->iffeatures, struct lysc_iffeature, iff) {
419 if (!lysc_iffeature_value(iff)) {
420 /* the feature must be disabled now */
421 (*df)->flags &= ~LYS_FENABLED;
422 /* add the feature into the list of changed features */
423 ly_set_add(changed, *df, LY_SET_OPT_USEASLIST);
424 break;
425 }
426 }
427 }
428 }
429
430 ly_set_free(changed, NULL);
431 return LY_SUCCESS;
432}
433
434API LY_ERR
Radek Krejcied5acc52019-04-25 15:57:04 +0200435lys_feature_enable(const struct lys_module *module, const char *feature)
Radek Krejci151a5b72018-10-19 14:21:44 +0200436{
Radek Krejci0af46292019-01-11 16:02:31 +0100437 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200438
Radek Krejcied5acc52019-04-25 15:57:04 +0200439 return lys_feature_change((struct lys_module*)module, feature, 1);
Radek Krejci151a5b72018-10-19 14:21:44 +0200440}
441
442API LY_ERR
Radek Krejcied5acc52019-04-25 15:57:04 +0200443lys_feature_disable(const struct lys_module *module, const char *feature)
Radek Krejci151a5b72018-10-19 14:21:44 +0200444{
Radek Krejci0af46292019-01-11 16:02:31 +0100445 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200446
Radek Krejcied5acc52019-04-25 15:57:04 +0200447 return lys_feature_change((struct lys_module*)module, feature, 0);
Radek Krejci151a5b72018-10-19 14:21:44 +0200448}
449
450API int
451lys_feature_value(const struct lys_module *module, const char *feature)
452{
453 struct lysc_feature *f;
454 struct lysc_module *mod;
455 unsigned int u;
456
457 LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, -1);
458 mod = module->compiled;
459
460 /* search for the specified feature */
461 for (u = 0; u < LY_ARRAY_SIZE(mod->features); ++u) {
Radek Krejci2c4e7172018-10-19 15:56:26 +0200462 f = &mod->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200463 if (!strcmp(f->name, feature)) {
464 if (f->flags & LYS_FENABLED) {
465 return 1;
466 } else {
467 return 0;
468 }
469 }
470 }
471
472 /* feature definition not found */
473 return -1;
474}
475
Radek Krejcia3045382018-11-22 14:30:31 +0100476API const struct lysc_iffeature *
477lys_is_disabled(const struct lysc_node *node, int recursive)
478{
479 unsigned int u;
Radek Krejcia3045382018-11-22 14:30:31 +0100480
481 LY_CHECK_ARG_RET(NULL, node, NULL);
482
483 while(node) {
484 if (node->nodetype & LYS_CHOICE) {
485 return NULL;
486 }
487
Radek Krejci056d0a82018-12-06 16:57:25 +0100488 if (node->iffeatures) {
Radek Krejcia3045382018-11-22 14:30:31 +0100489 /* check local if-features */
Radek Krejci056d0a82018-12-06 16:57:25 +0100490 LY_ARRAY_FOR(node->iffeatures, u) {
491 if (!lysc_iffeature_value(&node->iffeatures[u])) {
492 return &node->iffeatures[u];
Radek Krejcia3045382018-11-22 14:30:31 +0100493 }
494 }
495 }
496
497 if (!recursive) {
498 return NULL;
499 }
500
501 /* go through parents */
502 node = node->parent;
503 }
504 return NULL;
505}
506
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100507struct lysp_submodule *
508lys_parse_mem_submodule(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, struct ly_parser_ctx *main_ctx,
509 LY_ERR (*custom_check)(struct ly_ctx*, struct lysp_module*, struct lysp_submodule*, void*), void *check_data)
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200510{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100511 LY_ERR ret = LY_EINVAL;
512 struct lysp_submodule *submod = NULL, *latest_sp;
513 struct ly_parser_ctx context = {0};
514
515 LY_CHECK_ARG_RET(ctx, ctx, data, NULL);
516
517 context.ctx = ctx;
518 context.line = 1;
519
520 /* map the typedefs and groupings list from main context to the submodule's context */
521 memcpy(&context.tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
522 memcpy(&context.grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
523
524 switch (format) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200525 /* TODO not yet supported
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100526 case LYS_IN_YIN:
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100527 mod = yin_read_module();
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100528 break;
Radek Krejcied5acc52019-04-25 15:57:04 +0200529 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100530 case LYS_IN_YANG:
531 ret = yang_parse_submodule(&context, data, &submod);
532 break;
533 default:
534 LOGERR(context.ctx, LY_EINVAL, "Invalid schema input format.");
535 break;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200536 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100537 LY_CHECK_RET(ret, NULL);
538
539 /* make sure that the newest revision is at position 0 */
540 lysp_sort_revisions(submod->revs);
541
542 if (custom_check) {
543 LY_CHECK_GOTO(custom_check(context.ctx, NULL, submod, check_data), error);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200544 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100545
546 /* decide the latest revision */
547 latest_sp = ly_ctx_get_submodule(context.ctx, submod->belongsto, submod->name, NULL);
548 if (latest_sp) {
549 if (submod->revs) {
550 if (!latest_sp->revs) {
551 /* latest has no revision, so mod is anyway newer */
552 submod->latest_revision = latest_sp->latest_revision;
553 latest_sp->latest_revision = 0;
554 } else {
555 if (strcmp(submod->revs[0].date, latest_sp->revs[0].date) > 0) {
556 submod->latest_revision = latest_sp->latest_revision;
557 latest_sp->latest_revision = 0;
558 }
559 }
560 }
561 } else {
562 submod->latest_revision = 1;
563 }
564
565 /* remap possibly changed and reallocated typedefs and groupings list back to the main context */
566 memcpy(&main_ctx->tpdfs_nodes, &context.tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
567 memcpy(&main_ctx->grps_nodes, &context.grps_nodes, sizeof main_ctx->grps_nodes);
568
569 return submod;
570error:
571 lysp_submodule_free(ctx, submod);
572 return NULL;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200573}
574
Radek Krejcid33273d2018-10-25 14:55:52 +0200575struct lys_module *
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100576lys_parse_mem_module(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, int implement,
577 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
578 void *check_data)
Radek Krejci86d106e2018-10-18 09:53:19 +0200579{
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100580 struct lys_module *mod = NULL, *latest, *mod_dup;
Radek Krejci086c7132018-10-26 15:29:04 +0200581 struct lysp_import *imp;
582 struct lysp_include *inc;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100583 LY_ERR ret = LY_EINVAL;
Radek Krejci086c7132018-10-26 15:29:04 +0200584 unsigned int u, i;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100585 struct ly_parser_ctx context = {0};
Radek Krejci86d106e2018-10-18 09:53:19 +0200586
587 LY_CHECK_ARG_RET(ctx, ctx, data, NULL);
588
Radek Krejcibbe09a92018-11-08 09:36:54 +0100589 context.ctx = ctx;
590 context.line = 1;
591
Radek Krejci86d106e2018-10-18 09:53:19 +0200592 mod = calloc(1, sizeof *mod);
593 LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100594 mod->ctx = ctx;
Radek Krejci86d106e2018-10-18 09:53:19 +0200595
596 switch (format) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200597 /* TODO not yet supported
Radek Krejci86d106e2018-10-18 09:53:19 +0200598 case LYS_IN_YIN:
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100599 mod = yin_read_module();
Radek Krejci86d106e2018-10-18 09:53:19 +0200600 break;
Radek Krejcied5acc52019-04-25 15:57:04 +0200601 */
Radek Krejci86d106e2018-10-18 09:53:19 +0200602 case LYS_IN_YANG:
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100603 ret = yang_parse_module(&context, data, mod);
Radek Krejci86d106e2018-10-18 09:53:19 +0200604 break;
605 default:
606 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
607 break;
608 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100609 LY_CHECK_ERR_RET(ret, lys_module_free(mod, NULL), NULL);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200610
611 /* make sure that the newest revision is at position 0 */
612 lysp_sort_revisions(mod->parsed->revs);
Radek Krejci0af46292019-01-11 16:02:31 +0100613 if (mod->parsed->revs) {
614 mod->revision = lydict_insert(ctx, mod->parsed->revs[0].date, 0);
615 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200616
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100617 if (custom_check) {
618 LY_CHECK_GOTO(custom_check(ctx, mod->parsed, NULL, check_data), error);
619 }
620
Radek Krejci86d106e2018-10-18 09:53:19 +0200621 if (implement) {
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200622 /* mark the loaded module implemented */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100623 if (ly_ctx_get_module_implemented(ctx, mod->name)) {
624 LOGERR(ctx, LY_EDENIED, "Module \"%s\" is already implemented in the context.", mod->name);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100625 goto error;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200626 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100627 mod->implemented = 1;
Radek Krejci86d106e2018-10-18 09:53:19 +0200628 }
629
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100630 /* check for duplicity in the context */
Radek Krejci0af46292019-01-11 16:02:31 +0100631 mod_dup = (struct lys_module*)ly_ctx_get_module(ctx, mod->name, mod->revision);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100632 if (mod_dup) {
633 if (mod_dup->parsed) {
634 /* error */
Radek Krejcid33273d2018-10-25 14:55:52 +0200635 if (mod->parsed->revs) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100636 LOGERR(ctx, LY_EEXIST, "Module \"%s\" of revision \"%s\" is already present in the context.",
637 mod->name, mod->parsed->revs[0].date);
Radek Krejcid33273d2018-10-25 14:55:52 +0200638 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100639 LOGERR(ctx, LY_EEXIST, "Module \"%s\" with no revision is already present in the context.",
640 mod->name);
Radek Krejcid33273d2018-10-25 14:55:52 +0200641 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100642 goto error;
643 } else {
644 /* add the parsed data to the currently compiled-only module in the context */
645 mod_dup->parsed = mod->parsed;
646 mod_dup->parsed->mod = mod_dup;
647 mod->parsed = NULL;
648 lys_module_free(mod, NULL);
649 mod = mod_dup;
650 goto finish_parsing;
Radek Krejcid33273d2018-10-25 14:55:52 +0200651 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100652 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200653
654#if 0
655 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
656 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
657 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
658 * the anotation definitions available in the internal schema structure. There is another hack in schema
659 * printers to do not print this internally added annotation. */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100660 if (ly_strequal(mod->name, "ietf-netconf", 0)) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200661 if (lyp_add_ietf_netconf_annotations(mod)) {
662 lys_free(mod, NULL, 1, 1);
663 return NULL;
664 }
665 }
666#endif
667
Radek Krejci0af46292019-01-11 16:02:31 +0100668 if (!mod->implemented) {
669 /* pre-compile features of the module */
Radek Krejci693262f2019-04-29 15:23:20 +0200670 LY_CHECK_GOTO(lys_feature_precompile(ctx, mod, mod->parsed->features, &mod->off_features), error);
Radek Krejci0af46292019-01-11 16:02:31 +0100671 }
672
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100673 /* decide the latest revision */
674 latest = (struct lys_module*)ly_ctx_get_module_latest(ctx, mod->name);
675 if (latest) {
Radek Krejci0af46292019-01-11 16:02:31 +0100676 if (mod->revision) {
677 if (!latest->revision) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100678 /* latest has no revision, so mod is anyway newer */
679 mod->latest_revision = latest->latest_revision;
680 latest->latest_revision = 0;
Radek Krejci0af46292019-01-11 16:02:31 +0100681 } else if (strcmp(mod->revision, latest->revision) > 0) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100682 mod->latest_revision = latest->latest_revision;
683 latest->latest_revision = 0;
Radek Krejcid33273d2018-10-25 14:55:52 +0200684 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200685 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100686 } else {
687 mod->latest_revision = 1;
688 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200689
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100690 /* add into context */
691 ly_set_add(&ctx->list, mod, LY_SET_OPT_USEASLIST);
Radek Krejcid33273d2018-10-25 14:55:52 +0200692
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100693finish_parsing:
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100694 /* resolve imports */
695 mod->parsed->parsing = 1;
696 LY_ARRAY_FOR(mod->parsed->imports, u) {
697 imp = &mod->parsed->imports[u];
698 if (!imp->module && lysp_load_module(ctx, imp->name, imp->rev[0] ? imp->rev : NULL, 0, 0, &imp->module)) {
699 goto error_ctx;
Radek Krejci086c7132018-10-26 15:29:04 +0200700 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100701 /* check for importing the same module twice */
702 for (i = 0; i < u; ++i) {
703 if (imp->module == mod->parsed->imports[i].module) {
704 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 +0100705 goto error_ctx;
Radek Krejci086c7132018-10-26 15:29:04 +0200706 }
707 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200708 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100709 LY_ARRAY_FOR(mod->parsed->includes, u) {
710 inc = &mod->parsed->includes[u];
711 if (!inc->submodule && lysp_load_submodule(&context, mod->parsed, inc)) {
712 goto error_ctx;
713 }
Radek Krejci0af46292019-01-11 16:02:31 +0100714 if (!mod->implemented) {
715 /* pre-compile features of the module */
Radek Krejci693262f2019-04-29 15:23:20 +0200716 LY_CHECK_GOTO(lys_feature_precompile(ctx, mod, inc->submodule->features, &mod->off_features), error);
Radek Krejci0af46292019-01-11 16:02:31 +0100717 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100718 }
719 mod->parsed->parsing = 0;
720
721 /* check name collisions - typedefs and groupings */
722 LY_CHECK_GOTO(lysp_check_typedefs(&context, mod->parsed), error_ctx);
Radek Krejcid33273d2018-10-25 14:55:52 +0200723
Radek Krejci86d106e2018-10-18 09:53:19 +0200724 return mod;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100725
726error_ctx:
727 ly_set_rm(&ctx->list, mod, NULL);
728error:
729 lys_module_free(mod, NULL);
730 ly_set_erase(&context.tpdfs_nodes, NULL);
731 return NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +0200732}
733
Radek Krejcid14e9692018-11-01 11:00:37 +0100734API struct lys_module *
Radek Krejci86d106e2018-10-18 09:53:19 +0200735lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format)
736{
Radek Krejci096235c2019-01-11 11:12:19 +0100737 struct lys_module *mod;
738
739 mod = lys_parse_mem_module(ctx, data, format, 1, NULL, NULL);
740 LY_CHECK_RET(!mod, NULL);
741
742 if (lys_compile(mod, 0)) {
743 ly_set_rm(&ctx->list, mod, NULL);
744 lys_module_free(mod, NULL);
745 return NULL;
746 }
747 return mod;
Radek Krejci86d106e2018-10-18 09:53:19 +0200748}
749
750static void
751lys_parse_set_filename(struct ly_ctx *ctx, const char **filename, int fd)
752{
Radek Krejci65639b92018-11-27 10:51:37 +0100753 char path[PATH_MAX];
Radek Krejci86d106e2018-10-18 09:53:19 +0200754
755#ifdef __APPLE__
756 if (fcntl(fd, F_GETPATH, path) != -1) {
757 *filename = lydict_insert(ctx, path, 0);
758 }
759#else
Radek Krejci65639b92018-11-27 10:51:37 +0100760 int len;
761 char proc_path[32];
762
Radek Krejci86d106e2018-10-18 09:53:19 +0200763 /* get URI if there is /proc */
764 sprintf(proc_path, "/proc/self/fd/%d", fd);
765 if ((len = readlink(proc_path, path, PATH_MAX - 1)) > 0) {
766 *filename = lydict_insert(ctx, path, len);
767 }
768#endif
769}
770
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100771void *
Radek Krejci3b1f9292018-11-08 10:58:35 +0100772lys_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 +0100773 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
774 void *check_data)
Radek Krejci86d106e2018-10-18 09:53:19 +0200775{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100776 void *result;
777 struct lys_module *mod = NULL;
778 struct lysp_submodule *submod = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +0200779 size_t length;
780 char *addr;
781
782 LY_CHECK_ARG_RET(ctx, ctx, NULL);
783 if (fd < 0) {
784 LOGARG(ctx, fd);
785 return NULL;
786 }
787
788 LY_CHECK_RET(ly_mmap(ctx, fd, &length, (void **)&addr), NULL);
789 if (!addr) {
790 LOGERR(ctx, LY_EINVAL, "Empty schema file.");
791 return NULL;
792 }
793
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100794 if (main_ctx) {
795 result = submod = lys_parse_mem_submodule(ctx, addr, format, main_ctx, custom_check, check_data);
796 } else {
797 result = mod = lys_parse_mem_module(ctx, addr, format, implement, custom_check, check_data);
Radek Krejci096235c2019-01-11 11:12:19 +0100798 if (mod && implement && lys_compile(mod, 0)) {
799 ly_set_rm(&ctx->list, mod, NULL);
800 lys_module_free(mod, NULL);
801 result = mod = NULL;
802 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100803 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200804 ly_munmap(addr, length);
805
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100806 if (mod && !mod->filepath) {
807 lys_parse_set_filename(ctx, &mod->filepath, fd);
808 } else if (submod && !submod->filepath) {
809 lys_parse_set_filename(ctx, &submod->filepath, fd);
Radek Krejci86d106e2018-10-18 09:53:19 +0200810 }
811
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100812 return result;
813}
814
815struct lys_module *
816lys_parse_fd_module(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, int implement,
817 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
818 void *check_data)
819{
820 return (struct lys_module*)lys_parse_fd_(ctx, fd, format, implement, NULL, custom_check, check_data);
821}
822
823struct lysp_submodule *
824lys_parse_fd_submodule(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, struct ly_parser_ctx *main_ctx,
825 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
826 void *check_data)
827{
828 assert(main_ctx);
829 return (struct lysp_submodule*)lys_parse_fd_(ctx, fd, format, 0, main_ctx, custom_check, check_data);
Radek Krejci86d106e2018-10-18 09:53:19 +0200830}
831
Radek Krejcid14e9692018-11-01 11:00:37 +0100832API struct lys_module *
Radek Krejci86d106e2018-10-18 09:53:19 +0200833lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format)
834{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100835 return lys_parse_fd_module(ctx, fd, format, 1, NULL, NULL);
Radek Krejci86d106e2018-10-18 09:53:19 +0200836}
837
Radek Krejcid33273d2018-10-25 14:55:52 +0200838struct lys_module *
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100839lys_parse_path_(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, int implement,
840 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 +0200841{
842 int fd;
Radek Krejcid33273d2018-10-25 14:55:52 +0200843 struct lys_module *mod;
Radek Krejci86d106e2018-10-18 09:53:19 +0200844 const char *rev, *dot, *filename;
845 size_t len;
846
847 LY_CHECK_ARG_RET(ctx, ctx, path, NULL);
848
849 fd = open(path, O_RDONLY);
850 LY_CHECK_ERR_RET(fd == -1, LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)), NULL);
851
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100852 mod = lys_parse_fd_module(ctx, fd, format, implement, custom_check, check_data);
Radek Krejci86d106e2018-10-18 09:53:19 +0200853 close(fd);
854 LY_CHECK_RET(!mod, NULL);
855
856 /* check that name and revision match filename */
857 filename = strrchr(path, '/');
858 if (!filename) {
859 filename = path;
860 } else {
861 filename++;
862 }
863 rev = strchr(filename, '@');
864 dot = strrchr(filename, '.');
865
866 /* name */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100867 len = strlen(mod->name);
868 if (strncmp(filename, mod->name, len) ||
Radek Krejci86d106e2018-10-18 09:53:19 +0200869 ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100870 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name);
Radek Krejci86d106e2018-10-18 09:53:19 +0200871 }
872 if (rev) {
873 len = dot - ++rev;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200874 if (!mod->parsed->revs || len != 10 || strncmp(mod->parsed->revs[0].date, rev, len)) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200875 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Radek Krejcib7db73a2018-10-24 14:18:40 +0200876 mod->parsed->revs ? mod->parsed->revs[0].date : "none");
Radek Krejci86d106e2018-10-18 09:53:19 +0200877 }
878 }
879
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100880 if (!mod->filepath) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200881 /* store URI */
882 char rpath[PATH_MAX];
883 if (realpath(path, rpath) != NULL) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100884 mod->filepath = lydict_insert(ctx, rpath, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +0200885 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100886 mod->filepath = lydict_insert(ctx, path, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +0200887 }
888 }
889
890 return mod;
891}
892
Radek Krejcid14e9692018-11-01 11:00:37 +0100893API struct lys_module *
Radek Krejcid33273d2018-10-25 14:55:52 +0200894lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format)
895{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100896 return lys_parse_path_(ctx, path, format, 1, NULL, NULL);
Radek Krejcid33273d2018-10-25 14:55:52 +0200897}
898
899API LY_ERR
900lys_search_localfile(const char * const *searchpaths, int cwd, const char *name, const char *revision,
901 char **localfile, LYS_INFORMAT *format)
902{
903 size_t len, flen, match_len = 0, dir_len;
904 int i, implicit_cwd = 0, ret = EXIT_FAILURE;
905 char *wd, *wn = NULL;
906 DIR *dir = NULL;
907 struct dirent *file;
908 char *match_name = NULL;
909 LYS_INFORMAT format_aux, match_format = 0;
910 struct ly_set *dirs;
911 struct stat st;
912
913 LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL);
914
915 /* start to fill the dir fifo with the context's search path (if set)
916 * and the current working directory */
917 dirs = ly_set_new();
918 if (!dirs) {
919 LOGMEM(NULL);
920 return EXIT_FAILURE;
921 }
922
923 len = strlen(name);
924 if (cwd) {
925 wd = get_current_dir_name();
926 if (!wd) {
927 LOGMEM(NULL);
928 goto cleanup;
929 } else {
930 /* add implicit current working directory (./) to be searched,
931 * this directory is not searched recursively */
932 if (ly_set_add(dirs, wd, 0) == -1) {
933 goto cleanup;
934 }
935 implicit_cwd = 1;
936 }
937 }
938 if (searchpaths) {
939 for (i = 0; searchpaths[i]; i++) {
940 /* check for duplicities with the implicit current working directory */
941 if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) {
942 implicit_cwd = 0;
943 continue;
944 }
945 wd = strdup(searchpaths[i]);
946 if (!wd) {
947 LOGMEM(NULL);
948 goto cleanup;
949 } else if (ly_set_add(dirs, wd, 0) == -1) {
950 goto cleanup;
951 }
952 }
953 }
954 wd = NULL;
955
956 /* start searching */
957 while (dirs->count) {
958 free(wd);
959 free(wn); wn = NULL;
960
961 dirs->count--;
962 wd = (char *)dirs->objs[dirs->count];
963 dirs->objs[dirs->count] = NULL;
964 LOGVRB("Searching for \"%s\" in %s.", name, wd);
965
966 if (dir) {
967 closedir(dir);
968 }
969 dir = opendir(wd);
970 dir_len = strlen(wd);
971 if (!dir) {
972 LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno));
973 } else {
974 while ((file = readdir(dir))) {
975 if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) {
976 /* skip . and .. */
977 continue;
978 }
979 free(wn);
980 if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) {
981 LOGMEM(NULL);
982 goto cleanup;
983 }
984 if (stat(wn, &st) == -1) {
985 LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)",
986 file->d_name, wd, strerror(errno));
987 continue;
988 }
989 if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) {
990 /* we have another subdirectory in searchpath to explore,
991 * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */
992 if (ly_set_add(dirs, wn, 0) == -1) {
993 goto cleanup;
994 }
995 /* continue with the next item in current directory */
996 wn = NULL;
997 continue;
998 } else if (!S_ISREG(st.st_mode)) {
999 /* not a regular file (note that we see the target of symlinks instead of symlinks */
1000 continue;
1001 }
1002
1003 /* here we know that the item is a file which can contain a module */
1004 if (strncmp(name, file->d_name, len) ||
1005 (file->d_name[len] != '.' && file->d_name[len] != '@')) {
1006 /* different filename than the module we search for */
1007 continue;
1008 }
1009
1010 /* get type according to filename suffix */
1011 flen = strlen(file->d_name);
Radek Krejcied5acc52019-04-25 15:57:04 +02001012 if (!strcmp(&file->d_name[flen - 5], ".yang")) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001013 format_aux = LYS_IN_YANG;
Radek Krejcied5acc52019-04-25 15:57:04 +02001014 /* TODO YIN parser
1015 } else if (!strcmp(&file->d_name[flen - 4], ".yin")) {
1016 format_aux = LYS_IN_YIN;
1017 */
Radek Krejcid33273d2018-10-25 14:55:52 +02001018 } else {
1019 /* not supportde suffix/file format */
1020 continue;
1021 }
1022
1023 if (revision) {
1024 /* we look for the specific revision, try to get it from the filename */
1025 if (file->d_name[len] == '@') {
1026 /* check revision from the filename */
1027 if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) {
1028 /* another revision */
1029 continue;
1030 } else {
1031 /* exact revision */
1032 free(match_name);
1033 match_name = wn;
1034 wn = NULL;
1035 match_len = dir_len + 1 + len;
1036 match_format = format_aux;
1037 goto success;
1038 }
1039 } else {
1040 /* continue trying to find exact revision match, use this only if not found */
1041 free(match_name);
1042 match_name = wn;
1043 wn = NULL;
1044 match_len = dir_len + 1 +len;
1045 match_format = format_aux;
1046 continue;
1047 }
1048 } else {
1049 /* remember the revision and try to find the newest one */
1050 if (match_name) {
1051 if (file->d_name[len] != '@' ||
1052 lysp_check_date(NULL, &file->d_name[len + 1], flen - (format_aux == LYS_IN_YANG ? 5 : 4) - len - 1, NULL)) {
1053 continue;
1054 } else if (match_name[match_len] == '@' &&
1055 (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) {
1056 continue;
1057 }
1058 free(match_name);
1059 }
1060
1061 match_name = wn;
1062 wn = NULL;
1063 match_len = dir_len + 1 + len;
1064 match_format = format_aux;
1065 continue;
1066 }
1067 }
1068 }
1069 }
1070
1071success:
1072 (*localfile) = match_name;
1073 match_name = NULL;
1074 if (format) {
1075 (*format) = match_format;
1076 }
1077 ret = EXIT_SUCCESS;
1078
1079cleanup:
1080 free(wn);
1081 free(wd);
1082 if (dir) {
1083 closedir(dir);
1084 }
1085 free(match_name);
1086 ly_set_free(dirs, free);
1087
1088 return ret;
1089}
1090