blob: 4e5ffb35b1aaa233fe3c42b64dc932b80a25325f [file] [log] [blame]
Radek Krejci3f5e3db2018-10-11 15:57:47 +02001/**
2 * @file tree_schema.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema tree implementation
5 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Radek Krejcib7db73a2018-10-24 14:18:40 +020014
15#include "common.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020016
Radek Krejcie7b95092019-05-15 11:03:07 +020017#include <assert.h>
Radek Krejcid33273d2018-10-25 14:55:52 +020018#include <dirent.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020019#include <errno.h>
20#include <fcntl.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020021#include <limits.h>
22#include <stdint.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020023#include <stdio.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020024#include <stdlib.h>
25#include <string.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020026#include <sys/stat.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020027#include <unistd.h>
Radek Krejci3f5e3db2018-10-11 15:57:47 +020028
Radek Krejci86d106e2018-10-18 09:53:19 +020029#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020030#include "dict.h"
31#include "log.h"
32#include "set.h"
33#include "tree.h"
34#include "tree_schema.h"
Radek Krejci70853c52018-10-15 14:46:16 +020035#include "tree_schema_internal.h"
Radek Krejci3f5e3db2018-10-11 15:57:47 +020036
Radek Krejcia3045382018-11-22 14:30:31 +010037API const struct lysc_node *
38lys_getnext(const struct lysc_node *last, const struct lysc_node *parent, const struct lysc_module *module, int options)
39{
Radek Krejci6eeb58f2019-02-22 16:29:37 +010040 const struct lysc_node *next = NULL;
Radek Krejcia3045382018-11-22 14:30:31 +010041 struct lysc_node **snode;
Radek Krejci6eeb58f2019-02-22 16:29:37 +010042 int action_flag = 0, notif_flag = 0;
43 const struct lysc_action *actions;
44 const struct lysc_notif *notifs;
45 unsigned int u;
Radek Krejcia3045382018-11-22 14:30:31 +010046
47 LY_CHECK_ARG_RET(NULL, parent || module, NULL);
48
Radek Krejcid5a2b9d2019-04-12 10:39:30 +020049next:
Radek Krejcia3045382018-11-22 14:30:31 +010050 if (!last) {
51 /* first call */
52
53 /* get know where to start */
54 if (parent) {
55 /* schema subtree */
Radek Krejci056d0a82018-12-06 16:57:25 +010056 if (parent->nodetype == LYS_CHOICE && (options & LYS_GETNEXT_WITHCASE)) {
Radek Krejcid5a2b9d2019-04-12 10:39:30 +020057 if (((struct lysc_node_choice*)parent)->cases) {
58 next = last = (const struct lysc_node*)&((struct lysc_node_choice*)parent)->cases[0];
Radek Krejci056d0a82018-12-06 16:57:25 +010059 }
Radek Krejci056d0a82018-12-06 16:57:25 +010060 } else {
Radek Krejci6eeb58f2019-02-22 16:29:37 +010061 snode = lysc_node_children_p(parent, (options & LYS_GETNEXT_OUTPUT) ? LYS_CONFIG_R : LYS_CONFIG_W);
Radek Krejci05b774b2019-02-25 13:26:18 +010062 /* do not return anything if the node does not have any children */
Radek Krejcid5a2b9d2019-04-12 10:39:30 +020063 if (snode && *snode) {
64 next = last = *snode;
Radek Krejci056d0a82018-12-06 16:57:25 +010065 }
Radek Krejcia3045382018-11-22 14:30:31 +010066 }
Radek Krejcia3045382018-11-22 14:30:31 +010067 } else {
68 /* top level data */
69 next = last = module->data;
70 }
71 if (!next) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +010072 /* try to get action or notification */
73 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +010074 }
Radek Krejci05b774b2019-02-25 13:26:18 +010075 /* test if the next can be returned */
76 goto check;
77
Radek Krejci6eeb58f2019-02-22 16:29:37 +010078 } else if (last->nodetype == LYS_ACTION) {
Radek Krejci05b774b2019-02-25 13:26:18 +010079 action_flag = 1;
Radek Krejci6eeb58f2019-02-22 16:29:37 +010080 if (last->parent) {
81 actions = lysc_node_actions(last->parent);
82 } else {
83 actions = module->rpcs;
84 }
85 LY_ARRAY_FOR(actions, u) {
86 if (&actions[u] == (struct lysc_action*)last) {
87 break;
88 }
89 }
90 if (u + 1 < LY_ARRAY_SIZE(actions)) {
91 next = (struct lysc_node*)(&actions[u + 1]);
92 }
93 goto repeat;
94 } else if (last->nodetype == LYS_NOTIF) {
Radek Krejci05b774b2019-02-25 13:26:18 +010095 action_flag = notif_flag = 1;
Radek Krejci6eeb58f2019-02-22 16:29:37 +010096 if (last->parent) {
97 notifs = lysc_node_notifs(last->parent);
98 } else {
99 notifs = module->notifs;
100 }
101 LY_ARRAY_FOR(notifs, u) {
102 if (&notifs[u] == (struct lysc_notif*)last) {
103 break;
104 }
105 }
106 if (u + 1 < LY_ARRAY_SIZE(notifs)) {
107 next = (struct lysc_node*)(&notifs[u + 1]);
108 }
109 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100110 }
111
112 next = last->next;
113repeat:
Radek Krejci01342af2019-01-03 15:18:08 +0100114 if (next && parent && parent->nodetype == LYS_CASE && next->parent != parent) {
115 /* inside case (as an explicit parent, not when diving into it from choice),
116 * limit the list of children only to the specific case */
117 next = NULL;
118 }
Radek Krejcia3045382018-11-22 14:30:31 +0100119 if (!next) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100120 /* possibly go back to parent */
Radek Krejci05b774b2019-02-25 13:26:18 +0100121 if (last && last->parent != parent) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100122 last = last->parent;
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200123 goto next;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100124 } else if (!action_flag) {
125 action_flag = 1;
126 next = parent ? (struct lysc_node*)lysc_node_actions(parent) : (struct lysc_node*)module->rpcs;
127 } else if (!notif_flag) {
128 notif_flag = 1;
129 next = parent ? (struct lysc_node*)lysc_node_notifs(parent) : (struct lysc_node*)module->notifs;
130 } else {
131 return NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100132 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100133 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100134 }
Radek Krejci05b774b2019-02-25 13:26:18 +0100135check:
Radek Krejcia3045382018-11-22 14:30:31 +0100136 switch (next->nodetype) {
137 case LYS_ACTION:
138 case LYS_NOTIF:
139 case LYS_LEAF:
140 case LYS_ANYXML:
141 case LYS_ANYDATA:
142 case LYS_LIST:
143 case LYS_LEAFLIST:
Radek Krejcia9026eb2018-12-12 16:04:47 +0100144 case LYS_CASE:
Radek Krejcia3045382018-11-22 14:30:31 +0100145 break;
146 case LYS_CONTAINER:
147 if (!(((struct lysc_node_container *)next)->flags & LYS_PRESENCE) && (options & LYS_GETNEXT_INTONPCONT)) {
148 if (((struct lysc_node_container *)next)->child) {
149 /* go into */
150 next = ((struct lysc_node_container *)next)->child;
151 } else {
152 next = next->next;
153 }
154 goto repeat;
155 }
156 break;
157 case LYS_CHOICE:
158 if (options & LYS_GETNEXT_WITHCHOICE) {
159 return next;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100160 } else if ((options & LYS_GETNEXT_NOCHOICE) || !((struct lysc_node_choice *)next)->cases) {
161 next = next->next;
162 } else {
Radek Krejcia3045382018-11-22 14:30:31 +0100163 /* go into */
Radek Krejcia9026eb2018-12-12 16:04:47 +0100164 if (options & LYS_GETNEXT_WITHCASE) {
Radek Krejci05b774b2019-02-25 13:26:18 +0100165 next = (struct lysc_node*)((struct lysc_node_choice *)next)->cases;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100166 } else {
167 next = ((struct lysc_node_choice *)next)->cases->child;
168 }
Radek Krejcia3045382018-11-22 14:30:31 +0100169 }
170 goto repeat;
171 default:
172 /* we should not be here */
Radek Krejcib07b5c92019-04-08 10:56:37 +0200173 LOGINT(module ? module->mod->ctx : parent->module->ctx);
Radek Krejcia3045382018-11-22 14:30:31 +0100174 return NULL;
175 }
176
177 if (!(options & LYS_GETNEXT_NOSTATECHECK)) {
178 /* check if the node is disabled by if-feature */
179 if (lys_is_disabled(next, 0)) {
180 next = next->next;
181 goto repeat;
182 }
183 }
184 return next;
185}
186
187API const struct lysc_node *
188lys_child(const struct lysc_node *parent, const struct lys_module *module,
189 const char *name, size_t name_len, uint16_t nodetype, int options)
190{
191 const struct lysc_node *node = NULL;
192
193 LY_CHECK_ARG_RET(NULL, module, name, NULL);
194 if (!nodetype) {
195 nodetype = 0xffff;
196 }
197
198 while ((node = lys_getnext(node, parent, module->compiled, options))) {
199 if (!(node->nodetype & nodetype)) {
200 continue;
201 }
202 if (node->module != module) {
203 continue;
204 }
205
206 if (name_len) {
207 if (!strncmp(node->name, name, name_len) && !node->name[name_len]) {
208 return node;
209 }
210 } else {
211 if (!strcmp(node->name, name)) {
212 return node;
213 }
214 }
215 }
216 return NULL;
217}
218
Radek Krejci19a96102018-11-15 13:38:09 +0100219API int
220lysc_feature_value(const struct lysc_feature *feature)
Radek Krejci6f7feb62018-10-12 15:23:02 +0200221{
Radek Krejci19a96102018-11-15 13:38:09 +0100222 LY_CHECK_ARG_RET(NULL, feature, -1);
223 return feature->flags & LYS_FENABLED ? 1 : 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200224}
225
Radek Krejci693262f2019-04-29 15:23:20 +0200226uint8_t
227lysc_iff_getop(uint8_t *list, int pos)
Radek Krejci151a5b72018-10-19 14:21:44 +0200228{
229 uint8_t *item;
230 uint8_t mask = 3, result;
231
232 assert(pos >= 0);
233
234 item = &list[pos / 4];
235 result = (*item) & (mask << 2 * (pos % 4));
236 return result >> 2 * (pos % 4);
237}
238
Radek Krejci151a5b72018-10-19 14:21:44 +0200239static int
240lysc_iffeature_value_(const struct lysc_iffeature *iff, int *index_e, int *index_f)
241{
242 uint8_t op;
243 int a, b;
244
Radek Krejci693262f2019-04-29 15:23:20 +0200245 op = lysc_iff_getop(iff->expr, *index_e);
Radek Krejci151a5b72018-10-19 14:21:44 +0200246 (*index_e)++;
247
248 switch (op) {
249 case LYS_IFF_F:
250 /* resolve feature */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200251 return lysc_feature_value(iff->features[(*index_f)++]);
Radek Krejci151a5b72018-10-19 14:21:44 +0200252 case LYS_IFF_NOT:
253 /* invert result */
254 return lysc_iffeature_value_(iff, index_e, index_f) ? 0 : 1;
255 case LYS_IFF_AND:
256 case LYS_IFF_OR:
257 a = lysc_iffeature_value_(iff, index_e, index_f);
258 b = lysc_iffeature_value_(iff, index_e, index_f);
259 if (op == LYS_IFF_AND) {
260 return a && b;
261 } else { /* LYS_IFF_OR */
262 return a || b;
263 }
264 }
265
266 return 0;
267}
268
269API int
270lysc_iffeature_value(const struct lysc_iffeature *iff)
271{
272 int index_e = 0, index_f = 0;
273
274 LY_CHECK_ARG_RET(NULL, iff, -1);
275
276 if (iff->expr) {
277 return lysc_iffeature_value_(iff, &index_e, &index_f);
278 }
279 return 0;
280}
281
Radek Krejci151a5b72018-10-19 14:21:44 +0200282/**
283 * @brief Enable/Disable the specified feature in the module.
284 *
285 * If the feature is already set to the desired value, LY_SUCCESS is returned.
286 * By changing the feature, also all the feature which depends on it via their
287 * if-feature statements are again evaluated (disabled if a if-feature statemen
288 * evaluates to false).
289 *
Radek Krejci0af46292019-01-11 16:02:31 +0100290 * @param[in] mod Module where to set (search for) the feature.
Radek Krejci151a5b72018-10-19 14:21:44 +0200291 * @param[in] name Name of the feature to set. Asterisk ('*') can be used to
292 * set all the features in the module.
293 * @param[in] value Desired value of the feature: 1 (enable) or 0 (disable).
294 * @return LY_ERR value.
295 */
296static LY_ERR
Radek Krejci0af46292019-01-11 16:02:31 +0100297lys_feature_change(const struct lys_module *mod, const char *name, int value)
Radek Krejci151a5b72018-10-19 14:21:44 +0200298{
299 int all = 0;
Radek Krejcica3db002018-11-01 10:31:01 +0100300 unsigned int u, changed_count, disabled_count;
Radek Krejci151a5b72018-10-19 14:21:44 +0200301 struct lysc_feature *f, **df;
302 struct lysc_iffeature *iff;
303 struct ly_set *changed;
Radek Krejci0af46292019-01-11 16:02:31 +0100304 struct ly_ctx *ctx = mod->ctx; /* shortcut */
Radek Krejci151a5b72018-10-19 14:21:44 +0200305
Radek Krejci6e67c402019-05-02 09:55:39 +0200306 if (!strcmp(name, "*")) {
307 /* enable all */
308 all = 1;
309 }
310
Radek Krejci0af46292019-01-11 16:02:31 +0100311 if (!mod->compiled) {
312 LOGERR(ctx, LY_EINVAL, "Module \"%s\" is not implemented so all its features are permanently disabled without a chance to change it.",
313 mod->name);
314 return LY_EINVAL;
315 }
316 if (!mod->compiled->features) {
Radek Krejci6e67c402019-05-02 09:55:39 +0200317 if (all) {
318 /* no feature to enable */
319 return LY_SUCCESS;
320 }
Radek Krejci0af46292019-01-11 16:02:31 +0100321 LOGERR(ctx, LY_EINVAL, "Unable to switch feature since the module \"%s\" has no features.", mod->name);
Radek Krejci151a5b72018-10-19 14:21:44 +0200322 return LY_EINVAL;
323 }
324
Radek Krejci151a5b72018-10-19 14:21:44 +0200325 changed = ly_set_new();
Radek Krejcica3db002018-11-01 10:31:01 +0100326 changed_count = 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200327
Radek Krejcica3db002018-11-01 10:31:01 +0100328run:
Radek Krejci0af46292019-01-11 16:02:31 +0100329 for (disabled_count = u = 0; u < LY_ARRAY_SIZE(mod->compiled->features); ++u) {
330 f = &mod->compiled->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200331 if (all || !strcmp(f->name, name)) {
332 if ((value && (f->flags & LYS_FENABLED)) || (!value && !(f->flags & LYS_FENABLED))) {
333 if (all) {
334 /* skip already set features */
335 continue;
336 } else {
337 /* feature already set correctly */
338 ly_set_free(changed, NULL);
339 return LY_SUCCESS;
340 }
341 }
342
343 if (value) { /* enable */
344 /* check referenced features if they are enabled */
345 LY_ARRAY_FOR(f->iffeatures, struct lysc_iffeature, iff) {
346 if (!lysc_iffeature_value(iff)) {
347 if (all) {
Radek Krejcica3db002018-11-01 10:31:01 +0100348 ++disabled_count;
Radek Krejci151a5b72018-10-19 14:21:44 +0200349 goto next;
350 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100351 LOGERR(ctx, LY_EDENIED,
Radek Krejci151a5b72018-10-19 14:21:44 +0200352 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
353 f->name);
354 ly_set_free(changed, NULL);
355 return LY_EDENIED;
356 }
357 }
358 }
359 /* enable the feature */
360 f->flags |= LYS_FENABLED;
361 } else { /* disable */
362 /* disable the feature */
363 f->flags &= ~LYS_FENABLED;
364 }
365
366 /* remember the changed feature */
367 ly_set_add(changed, f, LY_SET_OPT_USEASLIST);
368
369 if (!all) {
370 /* stop in case changing a single feature */
371 break;
372 }
373 }
374next:
375 ;
376 }
377
378 if (!all && !changed->count) {
Radek Krejci0af46292019-01-11 16:02:31 +0100379 LOGERR(ctx, LY_EINVAL, "Feature \"%s\" not found in module \"%s\".", name, mod->name);
Radek Krejci151a5b72018-10-19 14:21:44 +0200380 ly_set_free(changed, NULL);
381 return LY_EINVAL;
382 }
383
Radek Krejcica3db002018-11-01 10:31:01 +0100384 if (value && all && disabled_count) {
385 if (changed_count == changed->count) {
386 /* no change in last run -> not able to enable all ... */
387 /* ... print errors */
Radek Krejci0af46292019-01-11 16:02:31 +0100388 for (u = 0; disabled_count && u < LY_ARRAY_SIZE(mod->compiled->features); ++u) {
389 if (!(mod->compiled->features[u].flags & LYS_FENABLED)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100390 LOGERR(ctx, LY_EDENIED,
Radek Krejcica3db002018-11-01 10:31:01 +0100391 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
Radek Krejci0af46292019-01-11 16:02:31 +0100392 mod->compiled->features[u].name);
Radek Krejcica3db002018-11-01 10:31:01 +0100393 --disabled_count;
394 }
395 }
396 /* ... restore the original state */
397 for (u = 0; u < changed->count; ++u) {
398 f = changed->objs[u];
399 /* re-disable the feature */
400 f->flags &= ~LYS_FENABLED;
401 }
402
403 ly_set_free(changed, NULL);
404 return LY_EDENIED;
405 } else {
406 /* we did some change in last run, try it again */
407 changed_count = changed->count;
408 goto run;
409 }
410 }
411
Radek Krejci151a5b72018-10-19 14:21:44 +0200412 /* reflect change(s) in the dependent features */
413 for (u = 0; u < changed->count; ++u) {
414 /* If a dependent feature is enabled, it can be now changed by the change (to false) of the value of
415 * its if-feature statements. The reverse logic, automatically enable feature when its feature is enabled
416 * is not done - by default, features are disabled and must be explicitely enabled. */
417 f = changed->objs[u];
418 LY_ARRAY_FOR(f->depfeatures, struct lysc_feature*, df) {
419 if (!((*df)->flags & LYS_FENABLED)) {
420 /* not enabled, nothing to do */
421 continue;
422 }
423 /* check the feature's if-features which could change by the previous change of our feature */
424 LY_ARRAY_FOR((*df)->iffeatures, struct lysc_iffeature, iff) {
425 if (!lysc_iffeature_value(iff)) {
426 /* the feature must be disabled now */
427 (*df)->flags &= ~LYS_FENABLED;
428 /* add the feature into the list of changed features */
429 ly_set_add(changed, *df, LY_SET_OPT_USEASLIST);
430 break;
431 }
432 }
433 }
434 }
435
436 ly_set_free(changed, NULL);
437 return LY_SUCCESS;
438}
439
440API LY_ERR
Radek Krejcied5acc52019-04-25 15:57:04 +0200441lys_feature_enable(const struct lys_module *module, const char *feature)
Radek Krejci151a5b72018-10-19 14:21:44 +0200442{
Radek Krejci0af46292019-01-11 16:02:31 +0100443 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200444
Radek Krejcied5acc52019-04-25 15:57:04 +0200445 return lys_feature_change((struct lys_module*)module, feature, 1);
Radek Krejci151a5b72018-10-19 14:21:44 +0200446}
447
448API LY_ERR
Radek Krejcied5acc52019-04-25 15:57:04 +0200449lys_feature_disable(const struct lys_module *module, const char *feature)
Radek Krejci151a5b72018-10-19 14:21:44 +0200450{
Radek Krejci0af46292019-01-11 16:02:31 +0100451 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200452
Radek Krejcied5acc52019-04-25 15:57:04 +0200453 return lys_feature_change((struct lys_module*)module, feature, 0);
Radek Krejci151a5b72018-10-19 14:21:44 +0200454}
455
456API int
457lys_feature_value(const struct lys_module *module, const char *feature)
458{
459 struct lysc_feature *f;
460 struct lysc_module *mod;
461 unsigned int u;
462
463 LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, -1);
464 mod = module->compiled;
465
466 /* search for the specified feature */
467 for (u = 0; u < LY_ARRAY_SIZE(mod->features); ++u) {
Radek Krejci2c4e7172018-10-19 15:56:26 +0200468 f = &mod->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200469 if (!strcmp(f->name, feature)) {
470 if (f->flags & LYS_FENABLED) {
471 return 1;
472 } else {
473 return 0;
474 }
475 }
476 }
477
478 /* feature definition not found */
479 return -1;
480}
481
Radek Krejcia3045382018-11-22 14:30:31 +0100482API const struct lysc_iffeature *
483lys_is_disabled(const struct lysc_node *node, int recursive)
484{
485 unsigned int u;
Radek Krejcia3045382018-11-22 14:30:31 +0100486
487 LY_CHECK_ARG_RET(NULL, node, NULL);
488
489 while(node) {
490 if (node->nodetype & LYS_CHOICE) {
491 return NULL;
492 }
493
Radek Krejci056d0a82018-12-06 16:57:25 +0100494 if (node->iffeatures) {
Radek Krejcia3045382018-11-22 14:30:31 +0100495 /* check local if-features */
Radek Krejci056d0a82018-12-06 16:57:25 +0100496 LY_ARRAY_FOR(node->iffeatures, u) {
497 if (!lysc_iffeature_value(&node->iffeatures[u])) {
498 return &node->iffeatures[u];
Radek Krejcia3045382018-11-22 14:30:31 +0100499 }
500 }
501 }
502
503 if (!recursive) {
504 return NULL;
505 }
506
507 /* go through parents */
508 node = node->parent;
509 }
510 return NULL;
511}
512
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100513struct lysp_submodule *
Radek Krejcie7b95092019-05-15 11:03:07 +0200514lys_parse_mem_submodule(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, struct lys_parser_ctx *main_ctx,
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100515 LY_ERR (*custom_check)(struct ly_ctx*, struct lysp_module*, struct lysp_submodule*, void*), void *check_data)
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200516{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100517 LY_ERR ret = LY_EINVAL;
518 struct lysp_submodule *submod = NULL, *latest_sp;
Radek Krejcie7b95092019-05-15 11:03:07 +0200519 struct lys_parser_ctx context = {0};
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100520
521 LY_CHECK_ARG_RET(ctx, ctx, data, NULL);
522
523 context.ctx = ctx;
524 context.line = 1;
525
526 /* map the typedefs and groupings list from main context to the submodule's context */
527 memcpy(&context.tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
528 memcpy(&context.grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
529
530 switch (format) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200531 /* TODO not yet supported
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100532 case LYS_IN_YIN:
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100533 mod = yin_read_module();
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100534 break;
Radek Krejcied5acc52019-04-25 15:57:04 +0200535 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100536 case LYS_IN_YANG:
537 ret = yang_parse_submodule(&context, data, &submod);
538 break;
539 default:
540 LOGERR(context.ctx, LY_EINVAL, "Invalid schema input format.");
541 break;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200542 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100543 LY_CHECK_RET(ret, NULL);
544
545 /* make sure that the newest revision is at position 0 */
546 lysp_sort_revisions(submod->revs);
547
548 if (custom_check) {
549 LY_CHECK_GOTO(custom_check(context.ctx, NULL, submod, check_data), error);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200550 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100551
552 /* decide the latest revision */
553 latest_sp = ly_ctx_get_submodule(context.ctx, submod->belongsto, submod->name, NULL);
554 if (latest_sp) {
555 if (submod->revs) {
556 if (!latest_sp->revs) {
557 /* latest has no revision, so mod is anyway newer */
558 submod->latest_revision = latest_sp->latest_revision;
559 latest_sp->latest_revision = 0;
560 } else {
561 if (strcmp(submod->revs[0].date, latest_sp->revs[0].date) > 0) {
562 submod->latest_revision = latest_sp->latest_revision;
563 latest_sp->latest_revision = 0;
564 }
565 }
566 }
567 } else {
568 submod->latest_revision = 1;
569 }
570
571 /* remap possibly changed and reallocated typedefs and groupings list back to the main context */
572 memcpy(&main_ctx->tpdfs_nodes, &context.tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
573 memcpy(&main_ctx->grps_nodes, &context.grps_nodes, sizeof main_ctx->grps_nodes);
574
575 return submod;
576error:
577 lysp_submodule_free(ctx, submod);
578 return NULL;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200579}
580
Radek Krejcid33273d2018-10-25 14:55:52 +0200581struct lys_module *
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100582lys_parse_mem_module(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, int implement,
583 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
584 void *check_data)
Radek Krejci86d106e2018-10-18 09:53:19 +0200585{
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100586 struct lys_module *mod = NULL, *latest, *mod_dup;
Radek Krejci086c7132018-10-26 15:29:04 +0200587 struct lysp_import *imp;
588 struct lysp_include *inc;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100589 LY_ERR ret = LY_EINVAL;
Radek Krejci086c7132018-10-26 15:29:04 +0200590 unsigned int u, i;
Radek Krejcie7b95092019-05-15 11:03:07 +0200591 struct lys_parser_ctx context = {0};
Radek Krejci86d106e2018-10-18 09:53:19 +0200592
593 LY_CHECK_ARG_RET(ctx, ctx, data, NULL);
594
Radek Krejcibbe09a92018-11-08 09:36:54 +0100595 context.ctx = ctx;
596 context.line = 1;
597
Radek Krejci86d106e2018-10-18 09:53:19 +0200598 mod = calloc(1, sizeof *mod);
599 LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100600 mod->ctx = ctx;
Radek Krejci86d106e2018-10-18 09:53:19 +0200601
602 switch (format) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200603 /* TODO not yet supported
Radek Krejci86d106e2018-10-18 09:53:19 +0200604 case LYS_IN_YIN:
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100605 mod = yin_read_module();
Radek Krejci86d106e2018-10-18 09:53:19 +0200606 break;
Radek Krejcied5acc52019-04-25 15:57:04 +0200607 */
Radek Krejci86d106e2018-10-18 09:53:19 +0200608 case LYS_IN_YANG:
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100609 ret = yang_parse_module(&context, data, mod);
Radek Krejci86d106e2018-10-18 09:53:19 +0200610 break;
611 default:
612 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
613 break;
614 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100615 LY_CHECK_ERR_RET(ret, lys_module_free(mod, NULL), NULL);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200616
617 /* make sure that the newest revision is at position 0 */
618 lysp_sort_revisions(mod->parsed->revs);
Radek Krejci0af46292019-01-11 16:02:31 +0100619 if (mod->parsed->revs) {
620 mod->revision = lydict_insert(ctx, mod->parsed->revs[0].date, 0);
621 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200622
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100623 if (custom_check) {
624 LY_CHECK_GOTO(custom_check(ctx, mod->parsed, NULL, check_data), error);
625 }
626
Radek Krejci86d106e2018-10-18 09:53:19 +0200627 if (implement) {
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200628 /* mark the loaded module implemented */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100629 if (ly_ctx_get_module_implemented(ctx, mod->name)) {
630 LOGERR(ctx, LY_EDENIED, "Module \"%s\" is already implemented in the context.", mod->name);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100631 goto error;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200632 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100633 mod->implemented = 1;
Radek Krejci86d106e2018-10-18 09:53:19 +0200634 }
635
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100636 /* check for duplicity in the context */
Radek Krejci0af46292019-01-11 16:02:31 +0100637 mod_dup = (struct lys_module*)ly_ctx_get_module(ctx, mod->name, mod->revision);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100638 if (mod_dup) {
639 if (mod_dup->parsed) {
640 /* error */
Radek Krejcid33273d2018-10-25 14:55:52 +0200641 if (mod->parsed->revs) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100642 LOGERR(ctx, LY_EEXIST, "Module \"%s\" of revision \"%s\" is already present in the context.",
643 mod->name, mod->parsed->revs[0].date);
Radek Krejcid33273d2018-10-25 14:55:52 +0200644 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100645 LOGERR(ctx, LY_EEXIST, "Module \"%s\" with no revision is already present in the context.",
646 mod->name);
Radek Krejcid33273d2018-10-25 14:55:52 +0200647 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100648 goto error;
649 } else {
650 /* add the parsed data to the currently compiled-only module in the context */
651 mod_dup->parsed = mod->parsed;
652 mod_dup->parsed->mod = mod_dup;
653 mod->parsed = NULL;
654 lys_module_free(mod, NULL);
655 mod = mod_dup;
656 goto finish_parsing;
Radek Krejcid33273d2018-10-25 14:55:52 +0200657 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100658 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200659
660#if 0
661 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
662 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
663 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
664 * the anotation definitions available in the internal schema structure. There is another hack in schema
665 * printers to do not print this internally added annotation. */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100666 if (ly_strequal(mod->name, "ietf-netconf", 0)) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200667 if (lyp_add_ietf_netconf_annotations(mod)) {
668 lys_free(mod, NULL, 1, 1);
669 return NULL;
670 }
671 }
672#endif
673
Radek Krejci0af46292019-01-11 16:02:31 +0100674 if (!mod->implemented) {
675 /* pre-compile features of the module */
Radek Krejci693262f2019-04-29 15:23:20 +0200676 LY_CHECK_GOTO(lys_feature_precompile(ctx, mod, mod->parsed->features, &mod->off_features), error);
Radek Krejci0af46292019-01-11 16:02:31 +0100677 }
678
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100679 /* decide the latest revision */
680 latest = (struct lys_module*)ly_ctx_get_module_latest(ctx, mod->name);
681 if (latest) {
Radek Krejci0af46292019-01-11 16:02:31 +0100682 if (mod->revision) {
683 if (!latest->revision) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100684 /* latest has no revision, so mod is anyway newer */
685 mod->latest_revision = latest->latest_revision;
686 latest->latest_revision = 0;
Radek Krejci0af46292019-01-11 16:02:31 +0100687 } else if (strcmp(mod->revision, latest->revision) > 0) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100688 mod->latest_revision = latest->latest_revision;
689 latest->latest_revision = 0;
Radek Krejcid33273d2018-10-25 14:55:52 +0200690 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200691 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100692 } else {
693 mod->latest_revision = 1;
694 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200695
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100696 /* add into context */
697 ly_set_add(&ctx->list, mod, LY_SET_OPT_USEASLIST);
Radek Krejcid33273d2018-10-25 14:55:52 +0200698
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100699finish_parsing:
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100700 /* resolve imports */
701 mod->parsed->parsing = 1;
702 LY_ARRAY_FOR(mod->parsed->imports, u) {
703 imp = &mod->parsed->imports[u];
704 if (!imp->module && lysp_load_module(ctx, imp->name, imp->rev[0] ? imp->rev : NULL, 0, 0, &imp->module)) {
705 goto error_ctx;
Radek Krejci086c7132018-10-26 15:29:04 +0200706 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100707 /* check for importing the same module twice */
708 for (i = 0; i < u; ++i) {
709 if (imp->module == mod->parsed->imports[i].module) {
710 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 +0100711 goto error_ctx;
Radek Krejci086c7132018-10-26 15:29:04 +0200712 }
713 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200714 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100715 LY_ARRAY_FOR(mod->parsed->includes, u) {
716 inc = &mod->parsed->includes[u];
717 if (!inc->submodule && lysp_load_submodule(&context, mod->parsed, inc)) {
718 goto error_ctx;
719 }
Radek Krejci0af46292019-01-11 16:02:31 +0100720 if (!mod->implemented) {
721 /* pre-compile features of the module */
Radek Krejci693262f2019-04-29 15:23:20 +0200722 LY_CHECK_GOTO(lys_feature_precompile(ctx, mod, inc->submodule->features, &mod->off_features), error);
Radek Krejci0af46292019-01-11 16:02:31 +0100723 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100724 }
725 mod->parsed->parsing = 0;
726
727 /* check name collisions - typedefs and groupings */
728 LY_CHECK_GOTO(lysp_check_typedefs(&context, mod->parsed), error_ctx);
Radek Krejcid33273d2018-10-25 14:55:52 +0200729
Radek Krejci86d106e2018-10-18 09:53:19 +0200730 return mod;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100731
732error_ctx:
733 ly_set_rm(&ctx->list, mod, NULL);
734error:
735 lys_module_free(mod, NULL);
736 ly_set_erase(&context.tpdfs_nodes, NULL);
737 return NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +0200738}
739
Radek Krejcid14e9692018-11-01 11:00:37 +0100740API struct lys_module *
Radek Krejci86d106e2018-10-18 09:53:19 +0200741lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format)
742{
Radek Krejci096235c2019-01-11 11:12:19 +0100743 struct lys_module *mod;
744
745 mod = lys_parse_mem_module(ctx, data, format, 1, NULL, NULL);
746 LY_CHECK_RET(!mod, NULL);
747
748 if (lys_compile(mod, 0)) {
749 ly_set_rm(&ctx->list, mod, NULL);
750 lys_module_free(mod, NULL);
751 return NULL;
752 }
753 return mod;
Radek Krejci86d106e2018-10-18 09:53:19 +0200754}
755
756static void
757lys_parse_set_filename(struct ly_ctx *ctx, const char **filename, int fd)
758{
Radek Krejci65639b92018-11-27 10:51:37 +0100759 char path[PATH_MAX];
Radek Krejci86d106e2018-10-18 09:53:19 +0200760
761#ifdef __APPLE__
762 if (fcntl(fd, F_GETPATH, path) != -1) {
763 *filename = lydict_insert(ctx, path, 0);
764 }
765#else
Radek Krejci65639b92018-11-27 10:51:37 +0100766 int len;
767 char proc_path[32];
768
Radek Krejci86d106e2018-10-18 09:53:19 +0200769 /* get URI if there is /proc */
770 sprintf(proc_path, "/proc/self/fd/%d", fd);
771 if ((len = readlink(proc_path, path, PATH_MAX - 1)) > 0) {
772 *filename = lydict_insert(ctx, path, len);
773 }
774#endif
775}
776
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100777void *
Radek Krejcie7b95092019-05-15 11:03:07 +0200778lys_parse_fd_(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, int implement, struct lys_parser_ctx *main_ctx,
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100779 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
780 void *check_data)
Radek Krejci86d106e2018-10-18 09:53:19 +0200781{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100782 void *result;
783 struct lys_module *mod = NULL;
784 struct lysp_submodule *submod = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +0200785 size_t length;
786 char *addr;
787
788 LY_CHECK_ARG_RET(ctx, ctx, NULL);
789 if (fd < 0) {
790 LOGARG(ctx, fd);
791 return NULL;
792 }
793
794 LY_CHECK_RET(ly_mmap(ctx, fd, &length, (void **)&addr), NULL);
795 if (!addr) {
796 LOGERR(ctx, LY_EINVAL, "Empty schema file.");
797 return NULL;
798 }
799
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100800 if (main_ctx) {
801 result = submod = lys_parse_mem_submodule(ctx, addr, format, main_ctx, custom_check, check_data);
802 } else {
803 result = mod = lys_parse_mem_module(ctx, addr, format, implement, custom_check, check_data);
Radek Krejci096235c2019-01-11 11:12:19 +0100804 if (mod && implement && lys_compile(mod, 0)) {
805 ly_set_rm(&ctx->list, mod, NULL);
806 lys_module_free(mod, NULL);
807 result = mod = NULL;
808 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100809 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200810 ly_munmap(addr, length);
811
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100812 if (mod && !mod->filepath) {
813 lys_parse_set_filename(ctx, &mod->filepath, fd);
814 } else if (submod && !submod->filepath) {
815 lys_parse_set_filename(ctx, &submod->filepath, fd);
Radek Krejci86d106e2018-10-18 09:53:19 +0200816 }
817
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100818 return result;
819}
820
821struct lys_module *
822lys_parse_fd_module(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, int implement,
823 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
824 void *check_data)
825{
826 return (struct lys_module*)lys_parse_fd_(ctx, fd, format, implement, NULL, custom_check, check_data);
827}
828
829struct lysp_submodule *
Radek Krejcie7b95092019-05-15 11:03:07 +0200830lys_parse_fd_submodule(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, struct lys_parser_ctx *main_ctx,
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100831 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
832 void *check_data)
833{
834 assert(main_ctx);
835 return (struct lysp_submodule*)lys_parse_fd_(ctx, fd, format, 0, main_ctx, custom_check, check_data);
Radek Krejci86d106e2018-10-18 09:53:19 +0200836}
837
Radek Krejcid14e9692018-11-01 11:00:37 +0100838API struct lys_module *
Radek Krejci86d106e2018-10-18 09:53:19 +0200839lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format)
840{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100841 return lys_parse_fd_module(ctx, fd, format, 1, NULL, NULL);
Radek Krejci86d106e2018-10-18 09:53:19 +0200842}
843
Radek Krejcid33273d2018-10-25 14:55:52 +0200844struct lys_module *
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100845lys_parse_path_(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, int implement,
846 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 +0200847{
848 int fd;
Radek Krejcid33273d2018-10-25 14:55:52 +0200849 struct lys_module *mod;
Radek Krejci86d106e2018-10-18 09:53:19 +0200850 const char *rev, *dot, *filename;
851 size_t len;
852
853 LY_CHECK_ARG_RET(ctx, ctx, path, NULL);
854
855 fd = open(path, O_RDONLY);
856 LY_CHECK_ERR_RET(fd == -1, LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)), NULL);
857
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100858 mod = lys_parse_fd_module(ctx, fd, format, implement, custom_check, check_data);
Radek Krejci86d106e2018-10-18 09:53:19 +0200859 close(fd);
860 LY_CHECK_RET(!mod, NULL);
861
862 /* check that name and revision match filename */
863 filename = strrchr(path, '/');
864 if (!filename) {
865 filename = path;
866 } else {
867 filename++;
868 }
869 rev = strchr(filename, '@');
870 dot = strrchr(filename, '.');
871
872 /* name */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100873 len = strlen(mod->name);
874 if (strncmp(filename, mod->name, len) ||
Radek Krejci86d106e2018-10-18 09:53:19 +0200875 ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100876 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name);
Radek Krejci86d106e2018-10-18 09:53:19 +0200877 }
878 if (rev) {
879 len = dot - ++rev;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200880 if (!mod->parsed->revs || len != 10 || strncmp(mod->parsed->revs[0].date, rev, len)) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200881 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Radek Krejcib7db73a2018-10-24 14:18:40 +0200882 mod->parsed->revs ? mod->parsed->revs[0].date : "none");
Radek Krejci86d106e2018-10-18 09:53:19 +0200883 }
884 }
885
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100886 if (!mod->filepath) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200887 /* store URI */
888 char rpath[PATH_MAX];
889 if (realpath(path, rpath) != NULL) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100890 mod->filepath = lydict_insert(ctx, rpath, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +0200891 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100892 mod->filepath = lydict_insert(ctx, path, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +0200893 }
894 }
895
896 return mod;
897}
898
Radek Krejcid14e9692018-11-01 11:00:37 +0100899API struct lys_module *
Radek Krejcid33273d2018-10-25 14:55:52 +0200900lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format)
901{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100902 return lys_parse_path_(ctx, path, format, 1, NULL, NULL);
Radek Krejcid33273d2018-10-25 14:55:52 +0200903}
904
905API LY_ERR
906lys_search_localfile(const char * const *searchpaths, int cwd, const char *name, const char *revision,
907 char **localfile, LYS_INFORMAT *format)
908{
909 size_t len, flen, match_len = 0, dir_len;
910 int i, implicit_cwd = 0, ret = EXIT_FAILURE;
911 char *wd, *wn = NULL;
912 DIR *dir = NULL;
913 struct dirent *file;
914 char *match_name = NULL;
915 LYS_INFORMAT format_aux, match_format = 0;
916 struct ly_set *dirs;
917 struct stat st;
918
919 LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL);
920
921 /* start to fill the dir fifo with the context's search path (if set)
922 * and the current working directory */
923 dirs = ly_set_new();
924 if (!dirs) {
925 LOGMEM(NULL);
926 return EXIT_FAILURE;
927 }
928
929 len = strlen(name);
930 if (cwd) {
931 wd = get_current_dir_name();
932 if (!wd) {
933 LOGMEM(NULL);
934 goto cleanup;
935 } else {
936 /* add implicit current working directory (./) to be searched,
937 * this directory is not searched recursively */
938 if (ly_set_add(dirs, wd, 0) == -1) {
939 goto cleanup;
940 }
941 implicit_cwd = 1;
942 }
943 }
944 if (searchpaths) {
945 for (i = 0; searchpaths[i]; i++) {
946 /* check for duplicities with the implicit current working directory */
947 if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) {
948 implicit_cwd = 0;
949 continue;
950 }
951 wd = strdup(searchpaths[i]);
952 if (!wd) {
953 LOGMEM(NULL);
954 goto cleanup;
955 } else if (ly_set_add(dirs, wd, 0) == -1) {
956 goto cleanup;
957 }
958 }
959 }
960 wd = NULL;
961
962 /* start searching */
963 while (dirs->count) {
964 free(wd);
965 free(wn); wn = NULL;
966
967 dirs->count--;
968 wd = (char *)dirs->objs[dirs->count];
969 dirs->objs[dirs->count] = NULL;
970 LOGVRB("Searching for \"%s\" in %s.", name, wd);
971
972 if (dir) {
973 closedir(dir);
974 }
975 dir = opendir(wd);
976 dir_len = strlen(wd);
977 if (!dir) {
978 LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno));
979 } else {
980 while ((file = readdir(dir))) {
981 if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) {
982 /* skip . and .. */
983 continue;
984 }
985 free(wn);
986 if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) {
987 LOGMEM(NULL);
988 goto cleanup;
989 }
990 if (stat(wn, &st) == -1) {
991 LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)",
992 file->d_name, wd, strerror(errno));
993 continue;
994 }
995 if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) {
996 /* we have another subdirectory in searchpath to explore,
997 * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */
998 if (ly_set_add(dirs, wn, 0) == -1) {
999 goto cleanup;
1000 }
1001 /* continue with the next item in current directory */
1002 wn = NULL;
1003 continue;
1004 } else if (!S_ISREG(st.st_mode)) {
1005 /* not a regular file (note that we see the target of symlinks instead of symlinks */
1006 continue;
1007 }
1008
1009 /* here we know that the item is a file which can contain a module */
1010 if (strncmp(name, file->d_name, len) ||
1011 (file->d_name[len] != '.' && file->d_name[len] != '@')) {
1012 /* different filename than the module we search for */
1013 continue;
1014 }
1015
1016 /* get type according to filename suffix */
1017 flen = strlen(file->d_name);
Radek Krejcied5acc52019-04-25 15:57:04 +02001018 if (!strcmp(&file->d_name[flen - 5], ".yang")) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001019 format_aux = LYS_IN_YANG;
Radek Krejcied5acc52019-04-25 15:57:04 +02001020 /* TODO YIN parser
1021 } else if (!strcmp(&file->d_name[flen - 4], ".yin")) {
1022 format_aux = LYS_IN_YIN;
1023 */
Radek Krejcid33273d2018-10-25 14:55:52 +02001024 } else {
1025 /* not supportde suffix/file format */
1026 continue;
1027 }
1028
1029 if (revision) {
1030 /* we look for the specific revision, try to get it from the filename */
1031 if (file->d_name[len] == '@') {
1032 /* check revision from the filename */
1033 if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) {
1034 /* another revision */
1035 continue;
1036 } else {
1037 /* exact revision */
1038 free(match_name);
1039 match_name = wn;
1040 wn = NULL;
1041 match_len = dir_len + 1 + len;
1042 match_format = format_aux;
1043 goto success;
1044 }
1045 } else {
1046 /* continue trying to find exact revision match, use this only if not found */
1047 free(match_name);
1048 match_name = wn;
1049 wn = NULL;
1050 match_len = dir_len + 1 +len;
1051 match_format = format_aux;
1052 continue;
1053 }
1054 } else {
1055 /* remember the revision and try to find the newest one */
1056 if (match_name) {
1057 if (file->d_name[len] != '@' ||
1058 lysp_check_date(NULL, &file->d_name[len + 1], flen - (format_aux == LYS_IN_YANG ? 5 : 4) - len - 1, NULL)) {
1059 continue;
1060 } else if (match_name[match_len] == '@' &&
1061 (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) {
1062 continue;
1063 }
1064 free(match_name);
1065 }
1066
1067 match_name = wn;
1068 wn = NULL;
1069 match_len = dir_len + 1 + len;
1070 match_format = format_aux;
1071 continue;
1072 }
1073 }
1074 }
1075 }
1076
1077success:
1078 (*localfile) = match_name;
1079 match_name = NULL;
1080 if (format) {
1081 (*format) = match_format;
1082 }
1083 ret = EXIT_SUCCESS;
1084
1085cleanup:
1086 free(wn);
1087 free(wd);
1088 if (dir) {
1089 closedir(dir);
1090 }
1091 free(match_name);
1092 ly_set_free(dirs, free);
1093
1094 return ret;
1095}
1096