blob: 7ba7be76101d7c5f5884efe6396b187d836bf381 [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{
34 const struct lysc_node *next;
35 struct lysc_node **snode;
36
37 LY_CHECK_ARG_RET(NULL, parent || module, NULL);
38
39 if (!last) {
40 /* first call */
41
42 /* get know where to start */
43 if (parent) {
44 /* schema subtree */
Radek Krejci056d0a82018-12-06 16:57:25 +010045 if (parent->nodetype == LYS_CHOICE && (options & LYS_GETNEXT_WITHCASE)) {
46 if (!((struct lysc_node_choice*)parent)->cases) {
47 return NULL;
48 }
49 next = last = (const struct lysc_node*)&((struct lysc_node_choice*)parent)->cases[0];
50 } else {
51 snode = lysc_node_children_p(parent);
52 /* do not return anything if the augment does not have any children */
53 if (!snode || !(*snode)) {
54 return NULL;
55 }
56 next = last = *snode;
Radek Krejcia3045382018-11-22 14:30:31 +010057 }
Radek Krejcia3045382018-11-22 14:30:31 +010058 } else {
59 /* top level data */
60 next = last = module->data;
61 }
62 if (!next) {
63 return next;
64 } else if (!(options & LYS_GETNEXT_NOSTATECHECK)) {
65 if (!lys_is_disabled(next, 0)) {
66 return next;
67 }
68 /* continue to find next available node */
69 } else {
70 return next;
71 }
72 }
73
74 next = last->next;
75repeat:
Radek Krejci01342af2019-01-03 15:18:08 +010076 if (next && parent && parent->nodetype == LYS_CASE && next->parent != parent) {
77 /* inside case (as an explicit parent, not when diving into it from choice),
78 * limit the list of children only to the specific case */
79 next = NULL;
80 }
Radek Krejcia3045382018-11-22 14:30:31 +010081 if (!next) {
Radek Krejcia9026eb2018-12-12 16:04:47 +010082 /* possibly go back to parent */
83 if (last->parent != parent) {
84 last = last->parent;
85 next = last->next;
86 goto repeat;
87 }
Radek Krejcia3045382018-11-22 14:30:31 +010088 return next;
89 }
90 switch (next->nodetype) {
91 case LYS_ACTION:
92 case LYS_NOTIF:
93 case LYS_LEAF:
94 case LYS_ANYXML:
95 case LYS_ANYDATA:
96 case LYS_LIST:
97 case LYS_LEAFLIST:
Radek Krejcia9026eb2018-12-12 16:04:47 +010098 case LYS_CASE:
Radek Krejcia3045382018-11-22 14:30:31 +010099 break;
100 case LYS_CONTAINER:
101 if (!(((struct lysc_node_container *)next)->flags & LYS_PRESENCE) && (options & LYS_GETNEXT_INTONPCONT)) {
102 if (((struct lysc_node_container *)next)->child) {
103 /* go into */
104 next = ((struct lysc_node_container *)next)->child;
105 } else {
106 next = next->next;
107 }
108 goto repeat;
109 }
110 break;
111 case LYS_CHOICE:
112 if (options & LYS_GETNEXT_WITHCHOICE) {
113 return next;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100114 } else if ((options & LYS_GETNEXT_NOCHOICE) || !((struct lysc_node_choice *)next)->cases) {
115 next = next->next;
116 } else {
Radek Krejcia3045382018-11-22 14:30:31 +0100117 /* go into */
Radek Krejcia9026eb2018-12-12 16:04:47 +0100118 if (options & LYS_GETNEXT_WITHCASE) {
119 next = (struct lysc_node*)&((struct lysc_node_choice *)next)->cases;
120 } else {
121 next = ((struct lysc_node_choice *)next)->cases->child;
122 }
Radek Krejcia3045382018-11-22 14:30:31 +0100123 }
124 goto repeat;
125 default:
126 /* we should not be here */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100127 LOGINT(last->module->ctx);
Radek Krejcia3045382018-11-22 14:30:31 +0100128 return NULL;
129 }
130
131 if (!(options & LYS_GETNEXT_NOSTATECHECK)) {
132 /* check if the node is disabled by if-feature */
133 if (lys_is_disabled(next, 0)) {
134 next = next->next;
135 goto repeat;
136 }
137 }
138 return next;
139}
140
141API const struct lysc_node *
142lys_child(const struct lysc_node *parent, const struct lys_module *module,
143 const char *name, size_t name_len, uint16_t nodetype, int options)
144{
145 const struct lysc_node *node = NULL;
146
147 LY_CHECK_ARG_RET(NULL, module, name, NULL);
148 if (!nodetype) {
149 nodetype = 0xffff;
150 }
151
152 while ((node = lys_getnext(node, parent, module->compiled, options))) {
153 if (!(node->nodetype & nodetype)) {
154 continue;
155 }
156 if (node->module != module) {
157 continue;
158 }
159
160 if (name_len) {
161 if (!strncmp(node->name, name, name_len) && !node->name[name_len]) {
162 return node;
163 }
164 } else {
165 if (!strcmp(node->name, name)) {
166 return node;
167 }
168 }
169 }
170 return NULL;
171}
172
Radek Krejci19a96102018-11-15 13:38:09 +0100173API int
174lysc_feature_value(const struct lysc_feature *feature)
Radek Krejci6f7feb62018-10-12 15:23:02 +0200175{
Radek Krejci19a96102018-11-15 13:38:09 +0100176 LY_CHECK_ARG_RET(NULL, feature, -1);
177 return feature->flags & LYS_FENABLED ? 1 : 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200178}
179
180static uint8_t
181iff_getop(uint8_t *list, int pos)
182{
183 uint8_t *item;
184 uint8_t mask = 3, result;
185
186 assert(pos >= 0);
187
188 item = &list[pos / 4];
189 result = (*item) & (mask << 2 * (pos % 4));
190 return result >> 2 * (pos % 4);
191}
192
Radek Krejci151a5b72018-10-19 14:21:44 +0200193static int
194lysc_iffeature_value_(const struct lysc_iffeature *iff, int *index_e, int *index_f)
195{
196 uint8_t op;
197 int a, b;
198
199 op = iff_getop(iff->expr, *index_e);
200 (*index_e)++;
201
202 switch (op) {
203 case LYS_IFF_F:
204 /* resolve feature */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200205 return lysc_feature_value(iff->features[(*index_f)++]);
Radek Krejci151a5b72018-10-19 14:21:44 +0200206 case LYS_IFF_NOT:
207 /* invert result */
208 return lysc_iffeature_value_(iff, index_e, index_f) ? 0 : 1;
209 case LYS_IFF_AND:
210 case LYS_IFF_OR:
211 a = lysc_iffeature_value_(iff, index_e, index_f);
212 b = lysc_iffeature_value_(iff, index_e, index_f);
213 if (op == LYS_IFF_AND) {
214 return a && b;
215 } else { /* LYS_IFF_OR */
216 return a || b;
217 }
218 }
219
220 return 0;
221}
222
223API int
224lysc_iffeature_value(const struct lysc_iffeature *iff)
225{
226 int index_e = 0, index_f = 0;
227
228 LY_CHECK_ARG_RET(NULL, iff, -1);
229
230 if (iff->expr) {
231 return lysc_iffeature_value_(iff, &index_e, &index_f);
232 }
233 return 0;
234}
235
Radek Krejci151a5b72018-10-19 14:21:44 +0200236/**
237 * @brief Enable/Disable the specified feature in the module.
238 *
239 * If the feature is already set to the desired value, LY_SUCCESS is returned.
240 * By changing the feature, also all the feature which depends on it via their
241 * if-feature statements are again evaluated (disabled if a if-feature statemen
242 * evaluates to false).
243 *
244 * @param[in] mod Compiled module where to set (search for) the feature.
245 * @param[in] name Name of the feature to set. Asterisk ('*') can be used to
246 * set all the features in the module.
247 * @param[in] value Desired value of the feature: 1 (enable) or 0 (disable).
248 * @return LY_ERR value.
249 */
250static LY_ERR
251lys_feature_change(const struct lysc_module *mod, const char *name, int value)
252{
253 int all = 0;
Radek Krejcica3db002018-11-01 10:31:01 +0100254 unsigned int u, changed_count, disabled_count;
Radek Krejci151a5b72018-10-19 14:21:44 +0200255 struct lysc_feature *f, **df;
256 struct lysc_iffeature *iff;
257 struct ly_set *changed;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100258 struct ly_ctx *ctx = mod->mod->ctx; /* shortcut */
Radek Krejci151a5b72018-10-19 14:21:44 +0200259
260 if (!mod->features) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100261 LOGERR(ctx, LY_EINVAL, "Unable to switch feature since the module \"%s\" has no features.", mod->mod->name);
Radek Krejci151a5b72018-10-19 14:21:44 +0200262 return LY_EINVAL;
263 }
264
265 if (!strcmp(name, "*")) {
266 /* enable all */
267 all = 1;
268 }
269 changed = ly_set_new();
Radek Krejcica3db002018-11-01 10:31:01 +0100270 changed_count = 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200271
Radek Krejcica3db002018-11-01 10:31:01 +0100272run:
273 for (disabled_count = u = 0; u < LY_ARRAY_SIZE(mod->features); ++u) {
Radek Krejci2c4e7172018-10-19 15:56:26 +0200274 f = &mod->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200275 if (all || !strcmp(f->name, name)) {
276 if ((value && (f->flags & LYS_FENABLED)) || (!value && !(f->flags & LYS_FENABLED))) {
277 if (all) {
278 /* skip already set features */
279 continue;
280 } else {
281 /* feature already set correctly */
282 ly_set_free(changed, NULL);
283 return LY_SUCCESS;
284 }
285 }
286
287 if (value) { /* enable */
288 /* check referenced features if they are enabled */
289 LY_ARRAY_FOR(f->iffeatures, struct lysc_iffeature, iff) {
290 if (!lysc_iffeature_value(iff)) {
291 if (all) {
Radek Krejcica3db002018-11-01 10:31:01 +0100292 ++disabled_count;
Radek Krejci151a5b72018-10-19 14:21:44 +0200293 goto next;
294 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100295 LOGERR(ctx, LY_EDENIED,
Radek Krejci151a5b72018-10-19 14:21:44 +0200296 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
297 f->name);
298 ly_set_free(changed, NULL);
299 return LY_EDENIED;
300 }
301 }
302 }
303 /* enable the feature */
304 f->flags |= LYS_FENABLED;
305 } else { /* disable */
306 /* disable the feature */
307 f->flags &= ~LYS_FENABLED;
308 }
309
310 /* remember the changed feature */
311 ly_set_add(changed, f, LY_SET_OPT_USEASLIST);
312
313 if (!all) {
314 /* stop in case changing a single feature */
315 break;
316 }
317 }
318next:
319 ;
320 }
321
322 if (!all && !changed->count) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100323 LOGERR(ctx, LY_EINVAL, "Feature \"%s\" not found in module \"%s\".", name, mod->mod->name);
Radek Krejci151a5b72018-10-19 14:21:44 +0200324 ly_set_free(changed, NULL);
325 return LY_EINVAL;
326 }
327
Radek Krejcica3db002018-11-01 10:31:01 +0100328 if (value && all && disabled_count) {
329 if (changed_count == changed->count) {
330 /* no change in last run -> not able to enable all ... */
331 /* ... print errors */
332 for (u = 0; disabled_count && u < LY_ARRAY_SIZE(mod->features); ++u) {
333 if (!(mod->features[u].flags & LYS_FENABLED)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100334 LOGERR(ctx, LY_EDENIED,
Radek Krejcica3db002018-11-01 10:31:01 +0100335 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
336 mod->features[u].name);
337 --disabled_count;
338 }
339 }
340 /* ... restore the original state */
341 for (u = 0; u < changed->count; ++u) {
342 f = changed->objs[u];
343 /* re-disable the feature */
344 f->flags &= ~LYS_FENABLED;
345 }
346
347 ly_set_free(changed, NULL);
348 return LY_EDENIED;
349 } else {
350 /* we did some change in last run, try it again */
351 changed_count = changed->count;
352 goto run;
353 }
354 }
355
Radek Krejci151a5b72018-10-19 14:21:44 +0200356 /* reflect change(s) in the dependent features */
357 for (u = 0; u < changed->count; ++u) {
358 /* If a dependent feature is enabled, it can be now changed by the change (to false) of the value of
359 * its if-feature statements. The reverse logic, automatically enable feature when its feature is enabled
360 * is not done - by default, features are disabled and must be explicitely enabled. */
361 f = changed->objs[u];
362 LY_ARRAY_FOR(f->depfeatures, struct lysc_feature*, df) {
363 if (!((*df)->flags & LYS_FENABLED)) {
364 /* not enabled, nothing to do */
365 continue;
366 }
367 /* check the feature's if-features which could change by the previous change of our feature */
368 LY_ARRAY_FOR((*df)->iffeatures, struct lysc_iffeature, iff) {
369 if (!lysc_iffeature_value(iff)) {
370 /* the feature must be disabled now */
371 (*df)->flags &= ~LYS_FENABLED;
372 /* add the feature into the list of changed features */
373 ly_set_add(changed, *df, LY_SET_OPT_USEASLIST);
374 break;
375 }
376 }
377 }
378 }
379
380 ly_set_free(changed, NULL);
381 return LY_SUCCESS;
382}
383
384API LY_ERR
385lys_feature_enable(struct lys_module *module, const char *feature)
386{
387 LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, LY_EINVAL);
388
389 return lys_feature_change(module->compiled, feature, 1);
390}
391
392API LY_ERR
393lys_feature_disable(struct lys_module *module, const char *feature)
394{
395 LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, LY_EINVAL);
396
397 return lys_feature_change(module->compiled, feature, 0);
398}
399
400API int
401lys_feature_value(const struct lys_module *module, const char *feature)
402{
403 struct lysc_feature *f;
404 struct lysc_module *mod;
405 unsigned int u;
406
407 LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, -1);
408 mod = module->compiled;
409
410 /* search for the specified feature */
411 for (u = 0; u < LY_ARRAY_SIZE(mod->features); ++u) {
Radek Krejci2c4e7172018-10-19 15:56:26 +0200412 f = &mod->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200413 if (!strcmp(f->name, feature)) {
414 if (f->flags & LYS_FENABLED) {
415 return 1;
416 } else {
417 return 0;
418 }
419 }
420 }
421
422 /* feature definition not found */
423 return -1;
424}
425
Radek Krejcia3045382018-11-22 14:30:31 +0100426API const struct lysc_iffeature *
427lys_is_disabled(const struct lysc_node *node, int recursive)
428{
429 unsigned int u;
Radek Krejcia3045382018-11-22 14:30:31 +0100430
431 LY_CHECK_ARG_RET(NULL, node, NULL);
432
433 while(node) {
434 if (node->nodetype & LYS_CHOICE) {
435 return NULL;
436 }
437
Radek Krejci056d0a82018-12-06 16:57:25 +0100438 if (node->iffeatures) {
Radek Krejcia3045382018-11-22 14:30:31 +0100439 /* check local if-features */
Radek Krejci056d0a82018-12-06 16:57:25 +0100440 LY_ARRAY_FOR(node->iffeatures, u) {
441 if (!lysc_iffeature_value(&node->iffeatures[u])) {
442 return &node->iffeatures[u];
Radek Krejcia3045382018-11-22 14:30:31 +0100443 }
444 }
445 }
446
447 if (!recursive) {
448 return NULL;
449 }
450
451 /* go through parents */
452 node = node->parent;
453 }
454 return NULL;
455}
456
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100457struct lysp_submodule *
458lys_parse_mem_submodule(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, struct ly_parser_ctx *main_ctx,
459 LY_ERR (*custom_check)(struct ly_ctx*, struct lysp_module*, struct lysp_submodule*, void*), void *check_data)
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200460{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100461 LY_ERR ret = LY_EINVAL;
462 struct lysp_submodule *submod = NULL, *latest_sp;
463 struct ly_parser_ctx context = {0};
464
465 LY_CHECK_ARG_RET(ctx, ctx, data, NULL);
466
467 context.ctx = ctx;
468 context.line = 1;
469
470 /* map the typedefs and groupings list from main context to the submodule's context */
471 memcpy(&context.tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
472 memcpy(&context.grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
473
474 switch (format) {
475 case LYS_IN_YIN:
476 /* TODO not yet supported
477 mod = yin_read_module();
478 */
479 break;
480 case LYS_IN_YANG:
481 ret = yang_parse_submodule(&context, data, &submod);
482 break;
483 default:
484 LOGERR(context.ctx, LY_EINVAL, "Invalid schema input format.");
485 break;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200486 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100487 LY_CHECK_RET(ret, NULL);
488
489 /* make sure that the newest revision is at position 0 */
490 lysp_sort_revisions(submod->revs);
491
492 if (custom_check) {
493 LY_CHECK_GOTO(custom_check(context.ctx, NULL, submod, check_data), error);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200494 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100495
496 /* decide the latest revision */
497 latest_sp = ly_ctx_get_submodule(context.ctx, submod->belongsto, submod->name, NULL);
498 if (latest_sp) {
499 if (submod->revs) {
500 if (!latest_sp->revs) {
501 /* latest has no revision, so mod is anyway newer */
502 submod->latest_revision = latest_sp->latest_revision;
503 latest_sp->latest_revision = 0;
504 } else {
505 if (strcmp(submod->revs[0].date, latest_sp->revs[0].date) > 0) {
506 submod->latest_revision = latest_sp->latest_revision;
507 latest_sp->latest_revision = 0;
508 }
509 }
510 }
511 } else {
512 submod->latest_revision = 1;
513 }
514
515 /* remap possibly changed and reallocated typedefs and groupings list back to the main context */
516 memcpy(&main_ctx->tpdfs_nodes, &context.tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
517 memcpy(&main_ctx->grps_nodes, &context.grps_nodes, sizeof main_ctx->grps_nodes);
518
519 return submod;
520error:
521 lysp_submodule_free(ctx, submod);
522 return NULL;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200523}
524
Radek Krejcid33273d2018-10-25 14:55:52 +0200525struct lys_module *
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100526lys_parse_mem_module(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, int implement,
527 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
528 void *check_data)
Radek Krejci86d106e2018-10-18 09:53:19 +0200529{
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100530 struct lys_module *mod = NULL, *latest, *mod_dup;
Radek Krejci086c7132018-10-26 15:29:04 +0200531 struct lysp_import *imp;
532 struct lysp_include *inc;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100533 LY_ERR ret = LY_EINVAL;
Radek Krejci086c7132018-10-26 15:29:04 +0200534 unsigned int u, i;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100535 struct ly_parser_ctx context = {0};
Radek Krejci86d106e2018-10-18 09:53:19 +0200536
537 LY_CHECK_ARG_RET(ctx, ctx, data, NULL);
538
Radek Krejcibbe09a92018-11-08 09:36:54 +0100539 context.ctx = ctx;
540 context.line = 1;
541
Radek Krejci86d106e2018-10-18 09:53:19 +0200542 mod = calloc(1, sizeof *mod);
543 LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100544 mod->ctx = ctx;
Radek Krejci86d106e2018-10-18 09:53:19 +0200545
546 switch (format) {
547 case LYS_IN_YIN:
548 /* TODO not yet supported
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100549 mod = yin_read_module();
Radek Krejci86d106e2018-10-18 09:53:19 +0200550 */
551 break;
552 case LYS_IN_YANG:
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100553 ret = yang_parse_module(&context, data, mod);
Radek Krejci86d106e2018-10-18 09:53:19 +0200554 break;
555 default:
556 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
557 break;
558 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100559 LY_CHECK_ERR_RET(ret, lys_module_free(mod, NULL), NULL);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200560
561 /* make sure that the newest revision is at position 0 */
562 lysp_sort_revisions(mod->parsed->revs);
Radek Krejci86d106e2018-10-18 09:53:19 +0200563
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100564 if (custom_check) {
565 LY_CHECK_GOTO(custom_check(ctx, mod->parsed, NULL, check_data), error);
566 }
567
Radek Krejci86d106e2018-10-18 09:53:19 +0200568 if (implement) {
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200569 /* mark the loaded module implemented */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100570 if (ly_ctx_get_module_implemented(ctx, mod->name)) {
571 LOGERR(ctx, LY_EDENIED, "Module \"%s\" is already implemented in the context.", mod->name);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100572 goto error;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200573 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100574 mod->implemented = 1;
Radek Krejci86d106e2018-10-18 09:53:19 +0200575 }
576
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100577 /* check for duplicity in the context */
578 mod_dup = (struct lys_module*)ly_ctx_get_module(ctx, mod->name, mod->parsed->revs ? mod->parsed->revs[0].date : NULL);
579 if (mod_dup) {
580 if (mod_dup->parsed) {
581 /* error */
Radek Krejcid33273d2018-10-25 14:55:52 +0200582 if (mod->parsed->revs) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100583 LOGERR(ctx, LY_EEXIST, "Module \"%s\" of revision \"%s\" is already present in the context.",
584 mod->name, mod->parsed->revs[0].date);
Radek Krejcid33273d2018-10-25 14:55:52 +0200585 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100586 LOGERR(ctx, LY_EEXIST, "Module \"%s\" with no revision is already present in the context.",
587 mod->name);
Radek Krejcid33273d2018-10-25 14:55:52 +0200588 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100589 goto error;
590 } else {
591 /* add the parsed data to the currently compiled-only module in the context */
592 mod_dup->parsed = mod->parsed;
593 mod_dup->parsed->mod = mod_dup;
594 mod->parsed = NULL;
595 lys_module_free(mod, NULL);
596 mod = mod_dup;
597 goto finish_parsing;
Radek Krejcid33273d2018-10-25 14:55:52 +0200598 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100599 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200600
601#if 0
602 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
603 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
604 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
605 * the anotation definitions available in the internal schema structure. There is another hack in schema
606 * printers to do not print this internally added annotation. */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100607 if (ly_strequal(mod->name, "ietf-netconf", 0)) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200608 if (lyp_add_ietf_netconf_annotations(mod)) {
609 lys_free(mod, NULL, 1, 1);
610 return NULL;
611 }
612 }
613#endif
614
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100615 /* decide the latest revision */
616 latest = (struct lys_module*)ly_ctx_get_module_latest(ctx, mod->name);
617 if (latest) {
618 if (mod->parsed->revs) {
619 if ((latest->parsed && !latest->parsed->revs) || (!latest->parsed && !latest->compiled->revision)) {
620 /* latest has no revision, so mod is anyway newer */
621 mod->latest_revision = latest->latest_revision;
622 latest->latest_revision = 0;
623 } else if (strcmp(mod->parsed->revs[0].date, latest->parsed ? latest->parsed->revs[0].date : latest->compiled->revision) > 0) {
624 mod->latest_revision = latest->latest_revision;
625 latest->latest_revision = 0;
Radek Krejcid33273d2018-10-25 14:55:52 +0200626 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200627 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100628 } else {
629 mod->latest_revision = 1;
630 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200631
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100632 /* add into context */
633 ly_set_add(&ctx->list, mod, LY_SET_OPT_USEASLIST);
Radek Krejcid33273d2018-10-25 14:55:52 +0200634
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100635finish_parsing:
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100636 /* resolve imports */
637 mod->parsed->parsing = 1;
638 LY_ARRAY_FOR(mod->parsed->imports, u) {
639 imp = &mod->parsed->imports[u];
640 if (!imp->module && lysp_load_module(ctx, imp->name, imp->rev[0] ? imp->rev : NULL, 0, 0, &imp->module)) {
641 goto error_ctx;
Radek Krejci086c7132018-10-26 15:29:04 +0200642 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100643 /* check for importing the same module twice */
644 for (i = 0; i < u; ++i) {
645 if (imp->module == mod->parsed->imports[i].module) {
646 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 +0100647 goto error_ctx;
Radek Krejci086c7132018-10-26 15:29:04 +0200648 }
649 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200650 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100651 LY_ARRAY_FOR(mod->parsed->includes, u) {
652 inc = &mod->parsed->includes[u];
653 if (!inc->submodule && lysp_load_submodule(&context, mod->parsed, inc)) {
654 goto error_ctx;
655 }
656 }
657 mod->parsed->parsing = 0;
658
659 /* check name collisions - typedefs and groupings */
660 LY_CHECK_GOTO(lysp_check_typedefs(&context, mod->parsed), error_ctx);
Radek Krejcid33273d2018-10-25 14:55:52 +0200661
Radek Krejci86d106e2018-10-18 09:53:19 +0200662 return mod;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100663
664error_ctx:
665 ly_set_rm(&ctx->list, mod, NULL);
666error:
667 lys_module_free(mod, NULL);
668 ly_set_erase(&context.tpdfs_nodes, NULL);
669 return NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +0200670}
671
Radek Krejcid14e9692018-11-01 11:00:37 +0100672API struct lys_module *
Radek Krejci86d106e2018-10-18 09:53:19 +0200673lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format)
674{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100675 return lys_parse_mem_module(ctx, data, format, 1, NULL, NULL);
Radek Krejci86d106e2018-10-18 09:53:19 +0200676}
677
678static void
679lys_parse_set_filename(struct ly_ctx *ctx, const char **filename, int fd)
680{
Radek Krejci65639b92018-11-27 10:51:37 +0100681 char path[PATH_MAX];
Radek Krejci86d106e2018-10-18 09:53:19 +0200682
683#ifdef __APPLE__
684 if (fcntl(fd, F_GETPATH, path) != -1) {
685 *filename = lydict_insert(ctx, path, 0);
686 }
687#else
Radek Krejci65639b92018-11-27 10:51:37 +0100688 int len;
689 char proc_path[32];
690
Radek Krejci86d106e2018-10-18 09:53:19 +0200691 /* get URI if there is /proc */
692 sprintf(proc_path, "/proc/self/fd/%d", fd);
693 if ((len = readlink(proc_path, path, PATH_MAX - 1)) > 0) {
694 *filename = lydict_insert(ctx, path, len);
695 }
696#endif
697}
698
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100699void *
Radek Krejci3b1f9292018-11-08 10:58:35 +0100700lys_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 +0100701 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
702 void *check_data)
Radek Krejci86d106e2018-10-18 09:53:19 +0200703{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100704 void *result;
705 struct lys_module *mod = NULL;
706 struct lysp_submodule *submod = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +0200707 size_t length;
708 char *addr;
709
710 LY_CHECK_ARG_RET(ctx, ctx, NULL);
711 if (fd < 0) {
712 LOGARG(ctx, fd);
713 return NULL;
714 }
715
716 LY_CHECK_RET(ly_mmap(ctx, fd, &length, (void **)&addr), NULL);
717 if (!addr) {
718 LOGERR(ctx, LY_EINVAL, "Empty schema file.");
719 return NULL;
720 }
721
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100722 if (main_ctx) {
723 result = submod = lys_parse_mem_submodule(ctx, addr, format, main_ctx, custom_check, check_data);
724 } else {
725 result = mod = lys_parse_mem_module(ctx, addr, format, implement, custom_check, check_data);
726 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200727 ly_munmap(addr, length);
728
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100729 if (mod && !mod->filepath) {
730 lys_parse_set_filename(ctx, &mod->filepath, fd);
731 } else if (submod && !submod->filepath) {
732 lys_parse_set_filename(ctx, &submod->filepath, fd);
Radek Krejci86d106e2018-10-18 09:53:19 +0200733 }
734
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100735 return result;
736}
737
738struct lys_module *
739lys_parse_fd_module(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, int implement,
740 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
741 void *check_data)
742{
743 return (struct lys_module*)lys_parse_fd_(ctx, fd, format, implement, NULL, custom_check, check_data);
744}
745
746struct lysp_submodule *
747lys_parse_fd_submodule(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, struct ly_parser_ctx *main_ctx,
748 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
749 void *check_data)
750{
751 assert(main_ctx);
752 return (struct lysp_submodule*)lys_parse_fd_(ctx, fd, format, 0, main_ctx, custom_check, check_data);
Radek Krejci86d106e2018-10-18 09:53:19 +0200753}
754
Radek Krejcid14e9692018-11-01 11:00:37 +0100755API struct lys_module *
Radek Krejci86d106e2018-10-18 09:53:19 +0200756lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format)
757{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100758 return lys_parse_fd_module(ctx, fd, format, 1, NULL, NULL);
Radek Krejci86d106e2018-10-18 09:53:19 +0200759}
760
Radek Krejcid33273d2018-10-25 14:55:52 +0200761struct lys_module *
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100762lys_parse_path_(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, int implement,
763 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 +0200764{
765 int fd;
Radek Krejcid33273d2018-10-25 14:55:52 +0200766 struct lys_module *mod;
Radek Krejci86d106e2018-10-18 09:53:19 +0200767 const char *rev, *dot, *filename;
768 size_t len;
769
770 LY_CHECK_ARG_RET(ctx, ctx, path, NULL);
771
772 fd = open(path, O_RDONLY);
773 LY_CHECK_ERR_RET(fd == -1, LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)), NULL);
774
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100775 mod = lys_parse_fd_module(ctx, fd, format, implement, custom_check, check_data);
Radek Krejci86d106e2018-10-18 09:53:19 +0200776 close(fd);
777 LY_CHECK_RET(!mod, NULL);
778
779 /* check that name and revision match filename */
780 filename = strrchr(path, '/');
781 if (!filename) {
782 filename = path;
783 } else {
784 filename++;
785 }
786 rev = strchr(filename, '@');
787 dot = strrchr(filename, '.');
788
789 /* name */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100790 len = strlen(mod->name);
791 if (strncmp(filename, mod->name, len) ||
Radek Krejci86d106e2018-10-18 09:53:19 +0200792 ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100793 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name);
Radek Krejci86d106e2018-10-18 09:53:19 +0200794 }
795 if (rev) {
796 len = dot - ++rev;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200797 if (!mod->parsed->revs || len != 10 || strncmp(mod->parsed->revs[0].date, rev, len)) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200798 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Radek Krejcib7db73a2018-10-24 14:18:40 +0200799 mod->parsed->revs ? mod->parsed->revs[0].date : "none");
Radek Krejci86d106e2018-10-18 09:53:19 +0200800 }
801 }
802
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100803 if (!mod->filepath) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200804 /* store URI */
805 char rpath[PATH_MAX];
806 if (realpath(path, rpath) != NULL) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100807 mod->filepath = lydict_insert(ctx, rpath, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +0200808 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100809 mod->filepath = lydict_insert(ctx, path, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +0200810 }
811 }
812
813 return mod;
814}
815
Radek Krejcid14e9692018-11-01 11:00:37 +0100816API struct lys_module *
Radek Krejcid33273d2018-10-25 14:55:52 +0200817lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format)
818{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100819 return lys_parse_path_(ctx, path, format, 1, NULL, NULL);
Radek Krejcid33273d2018-10-25 14:55:52 +0200820}
821
822API LY_ERR
823lys_search_localfile(const char * const *searchpaths, int cwd, const char *name, const char *revision,
824 char **localfile, LYS_INFORMAT *format)
825{
826 size_t len, flen, match_len = 0, dir_len;
827 int i, implicit_cwd = 0, ret = EXIT_FAILURE;
828 char *wd, *wn = NULL;
829 DIR *dir = NULL;
830 struct dirent *file;
831 char *match_name = NULL;
832 LYS_INFORMAT format_aux, match_format = 0;
833 struct ly_set *dirs;
834 struct stat st;
835
836 LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL);
837
838 /* start to fill the dir fifo with the context's search path (if set)
839 * and the current working directory */
840 dirs = ly_set_new();
841 if (!dirs) {
842 LOGMEM(NULL);
843 return EXIT_FAILURE;
844 }
845
846 len = strlen(name);
847 if (cwd) {
848 wd = get_current_dir_name();
849 if (!wd) {
850 LOGMEM(NULL);
851 goto cleanup;
852 } else {
853 /* add implicit current working directory (./) to be searched,
854 * this directory is not searched recursively */
855 if (ly_set_add(dirs, wd, 0) == -1) {
856 goto cleanup;
857 }
858 implicit_cwd = 1;
859 }
860 }
861 if (searchpaths) {
862 for (i = 0; searchpaths[i]; i++) {
863 /* check for duplicities with the implicit current working directory */
864 if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) {
865 implicit_cwd = 0;
866 continue;
867 }
868 wd = strdup(searchpaths[i]);
869 if (!wd) {
870 LOGMEM(NULL);
871 goto cleanup;
872 } else if (ly_set_add(dirs, wd, 0) == -1) {
873 goto cleanup;
874 }
875 }
876 }
877 wd = NULL;
878
879 /* start searching */
880 while (dirs->count) {
881 free(wd);
882 free(wn); wn = NULL;
883
884 dirs->count--;
885 wd = (char *)dirs->objs[dirs->count];
886 dirs->objs[dirs->count] = NULL;
887 LOGVRB("Searching for \"%s\" in %s.", name, wd);
888
889 if (dir) {
890 closedir(dir);
891 }
892 dir = opendir(wd);
893 dir_len = strlen(wd);
894 if (!dir) {
895 LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno));
896 } else {
897 while ((file = readdir(dir))) {
898 if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) {
899 /* skip . and .. */
900 continue;
901 }
902 free(wn);
903 if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) {
904 LOGMEM(NULL);
905 goto cleanup;
906 }
907 if (stat(wn, &st) == -1) {
908 LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)",
909 file->d_name, wd, strerror(errno));
910 continue;
911 }
912 if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) {
913 /* we have another subdirectory in searchpath to explore,
914 * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */
915 if (ly_set_add(dirs, wn, 0) == -1) {
916 goto cleanup;
917 }
918 /* continue with the next item in current directory */
919 wn = NULL;
920 continue;
921 } else if (!S_ISREG(st.st_mode)) {
922 /* not a regular file (note that we see the target of symlinks instead of symlinks */
923 continue;
924 }
925
926 /* here we know that the item is a file which can contain a module */
927 if (strncmp(name, file->d_name, len) ||
928 (file->d_name[len] != '.' && file->d_name[len] != '@')) {
929 /* different filename than the module we search for */
930 continue;
931 }
932
933 /* get type according to filename suffix */
934 flen = strlen(file->d_name);
935 if (!strcmp(&file->d_name[flen - 4], ".yin")) {
936 format_aux = LYS_IN_YIN;
937 } else if (!strcmp(&file->d_name[flen - 5], ".yang")) {
938 format_aux = LYS_IN_YANG;
939 } else {
940 /* not supportde suffix/file format */
941 continue;
942 }
943
944 if (revision) {
945 /* we look for the specific revision, try to get it from the filename */
946 if (file->d_name[len] == '@') {
947 /* check revision from the filename */
948 if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) {
949 /* another revision */
950 continue;
951 } else {
952 /* exact revision */
953 free(match_name);
954 match_name = wn;
955 wn = NULL;
956 match_len = dir_len + 1 + len;
957 match_format = format_aux;
958 goto success;
959 }
960 } else {
961 /* continue trying to find exact revision match, use this only if not found */
962 free(match_name);
963 match_name = wn;
964 wn = NULL;
965 match_len = dir_len + 1 +len;
966 match_format = format_aux;
967 continue;
968 }
969 } else {
970 /* remember the revision and try to find the newest one */
971 if (match_name) {
972 if (file->d_name[len] != '@' ||
973 lysp_check_date(NULL, &file->d_name[len + 1], flen - (format_aux == LYS_IN_YANG ? 5 : 4) - len - 1, NULL)) {
974 continue;
975 } else if (match_name[match_len] == '@' &&
976 (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) {
977 continue;
978 }
979 free(match_name);
980 }
981
982 match_name = wn;
983 wn = NULL;
984 match_len = dir_len + 1 + len;
985 match_format = format_aux;
986 continue;
987 }
988 }
989 }
990 }
991
992success:
993 (*localfile) = match_name;
994 match_name = NULL;
995 if (format) {
996 (*format) = match_format;
997 }
998 ret = EXIT_SUCCESS;
999
1000cleanup:
1001 free(wn);
1002 free(wd);
1003 if (dir) {
1004 closedir(dir);
1005 }
1006 free(match_name);
1007 ly_set_free(dirs, free);
1008
1009 return ret;
1010}
1011