blob: dd085de3ef3986d9346644faafc13759cf3811bd [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:
76 if (!next) {
Radek Krejcia9026eb2018-12-12 16:04:47 +010077 /* possibly go back to parent */
78 if (last->parent != parent) {
79 last = last->parent;
80 next = last->next;
81 goto repeat;
82 }
Radek Krejcia3045382018-11-22 14:30:31 +010083 return next;
84 }
85 switch (next->nodetype) {
86 case LYS_ACTION:
87 case LYS_NOTIF:
88 case LYS_LEAF:
89 case LYS_ANYXML:
90 case LYS_ANYDATA:
91 case LYS_LIST:
92 case LYS_LEAFLIST:
Radek Krejcia9026eb2018-12-12 16:04:47 +010093 case LYS_CASE:
Radek Krejcia3045382018-11-22 14:30:31 +010094 break;
95 case LYS_CONTAINER:
96 if (!(((struct lysc_node_container *)next)->flags & LYS_PRESENCE) && (options & LYS_GETNEXT_INTONPCONT)) {
97 if (((struct lysc_node_container *)next)->child) {
98 /* go into */
99 next = ((struct lysc_node_container *)next)->child;
100 } else {
101 next = next->next;
102 }
103 goto repeat;
104 }
105 break;
106 case LYS_CHOICE:
107 if (options & LYS_GETNEXT_WITHCHOICE) {
108 return next;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100109 } else if ((options & LYS_GETNEXT_NOCHOICE) || !((struct lysc_node_choice *)next)->cases) {
110 next = next->next;
111 } else {
Radek Krejcia3045382018-11-22 14:30:31 +0100112 /* go into */
Radek Krejcia9026eb2018-12-12 16:04:47 +0100113 if (options & LYS_GETNEXT_WITHCASE) {
114 next = (struct lysc_node*)&((struct lysc_node_choice *)next)->cases;
115 } else {
116 next = ((struct lysc_node_choice *)next)->cases->child;
117 }
Radek Krejcia3045382018-11-22 14:30:31 +0100118 }
119 goto repeat;
120 default:
121 /* we should not be here */
122 LOGINT(last->module->compiled->ctx);
123 return NULL;
124 }
125
126 if (!(options & LYS_GETNEXT_NOSTATECHECK)) {
127 /* check if the node is disabled by if-feature */
128 if (lys_is_disabled(next, 0)) {
129 next = next->next;
130 goto repeat;
131 }
132 }
133 return next;
134}
135
136API const struct lysc_node *
137lys_child(const struct lysc_node *parent, const struct lys_module *module,
138 const char *name, size_t name_len, uint16_t nodetype, int options)
139{
140 const struct lysc_node *node = NULL;
141
142 LY_CHECK_ARG_RET(NULL, module, name, NULL);
143 if (!nodetype) {
144 nodetype = 0xffff;
145 }
146
147 while ((node = lys_getnext(node, parent, module->compiled, options))) {
148 if (!(node->nodetype & nodetype)) {
149 continue;
150 }
151 if (node->module != module) {
152 continue;
153 }
154
155 if (name_len) {
156 if (!strncmp(node->name, name, name_len) && !node->name[name_len]) {
157 return node;
158 }
159 } else {
160 if (!strcmp(node->name, name)) {
161 return node;
162 }
163 }
164 }
165 return NULL;
166}
167
Radek Krejci19a96102018-11-15 13:38:09 +0100168API int
169lysc_feature_value(const struct lysc_feature *feature)
Radek Krejci6f7feb62018-10-12 15:23:02 +0200170{
Radek Krejci19a96102018-11-15 13:38:09 +0100171 LY_CHECK_ARG_RET(NULL, feature, -1);
172 return feature->flags & LYS_FENABLED ? 1 : 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200173}
174
175static uint8_t
176iff_getop(uint8_t *list, int pos)
177{
178 uint8_t *item;
179 uint8_t mask = 3, result;
180
181 assert(pos >= 0);
182
183 item = &list[pos / 4];
184 result = (*item) & (mask << 2 * (pos % 4));
185 return result >> 2 * (pos % 4);
186}
187
Radek Krejci151a5b72018-10-19 14:21:44 +0200188static int
189lysc_iffeature_value_(const struct lysc_iffeature *iff, int *index_e, int *index_f)
190{
191 uint8_t op;
192 int a, b;
193
194 op = iff_getop(iff->expr, *index_e);
195 (*index_e)++;
196
197 switch (op) {
198 case LYS_IFF_F:
199 /* resolve feature */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200200 return lysc_feature_value(iff->features[(*index_f)++]);
Radek Krejci151a5b72018-10-19 14:21:44 +0200201 case LYS_IFF_NOT:
202 /* invert result */
203 return lysc_iffeature_value_(iff, index_e, index_f) ? 0 : 1;
204 case LYS_IFF_AND:
205 case LYS_IFF_OR:
206 a = lysc_iffeature_value_(iff, index_e, index_f);
207 b = lysc_iffeature_value_(iff, index_e, index_f);
208 if (op == LYS_IFF_AND) {
209 return a && b;
210 } else { /* LYS_IFF_OR */
211 return a || b;
212 }
213 }
214
215 return 0;
216}
217
218API int
219lysc_iffeature_value(const struct lysc_iffeature *iff)
220{
221 int index_e = 0, index_f = 0;
222
223 LY_CHECK_ARG_RET(NULL, iff, -1);
224
225 if (iff->expr) {
226 return lysc_iffeature_value_(iff, &index_e, &index_f);
227 }
228 return 0;
229}
230
Radek Krejci151a5b72018-10-19 14:21:44 +0200231/**
232 * @brief Enable/Disable the specified feature in the module.
233 *
234 * If the feature is already set to the desired value, LY_SUCCESS is returned.
235 * By changing the feature, also all the feature which depends on it via their
236 * if-feature statements are again evaluated (disabled if a if-feature statemen
237 * evaluates to false).
238 *
239 * @param[in] mod Compiled module where to set (search for) the feature.
240 * @param[in] name Name of the feature to set. Asterisk ('*') can be used to
241 * set all the features in the module.
242 * @param[in] value Desired value of the feature: 1 (enable) or 0 (disable).
243 * @return LY_ERR value.
244 */
245static LY_ERR
246lys_feature_change(const struct lysc_module *mod, const char *name, int value)
247{
248 int all = 0;
Radek Krejcica3db002018-11-01 10:31:01 +0100249 unsigned int u, changed_count, disabled_count;
Radek Krejci151a5b72018-10-19 14:21:44 +0200250 struct lysc_feature *f, **df;
251 struct lysc_iffeature *iff;
252 struct ly_set *changed;
253
254 if (!mod->features) {
255 LOGERR(mod->ctx, LY_EINVAL, "Unable to switch feature since the module \"%s\" has no features.", mod->name);
256 return LY_EINVAL;
257 }
258
259 if (!strcmp(name, "*")) {
260 /* enable all */
261 all = 1;
262 }
263 changed = ly_set_new();
Radek Krejcica3db002018-11-01 10:31:01 +0100264 changed_count = 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200265
Radek Krejcica3db002018-11-01 10:31:01 +0100266run:
267 for (disabled_count = u = 0; u < LY_ARRAY_SIZE(mod->features); ++u) {
Radek Krejci2c4e7172018-10-19 15:56:26 +0200268 f = &mod->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200269 if (all || !strcmp(f->name, name)) {
270 if ((value && (f->flags & LYS_FENABLED)) || (!value && !(f->flags & LYS_FENABLED))) {
271 if (all) {
272 /* skip already set features */
273 continue;
274 } else {
275 /* feature already set correctly */
276 ly_set_free(changed, NULL);
277 return LY_SUCCESS;
278 }
279 }
280
281 if (value) { /* enable */
282 /* check referenced features if they are enabled */
283 LY_ARRAY_FOR(f->iffeatures, struct lysc_iffeature, iff) {
284 if (!lysc_iffeature_value(iff)) {
285 if (all) {
Radek Krejcica3db002018-11-01 10:31:01 +0100286 ++disabled_count;
Radek Krejci151a5b72018-10-19 14:21:44 +0200287 goto next;
288 } else {
289 LOGERR(mod->ctx, LY_EDENIED,
290 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
291 f->name);
292 ly_set_free(changed, NULL);
293 return LY_EDENIED;
294 }
295 }
296 }
297 /* enable the feature */
298 f->flags |= LYS_FENABLED;
299 } else { /* disable */
300 /* disable the feature */
301 f->flags &= ~LYS_FENABLED;
302 }
303
304 /* remember the changed feature */
305 ly_set_add(changed, f, LY_SET_OPT_USEASLIST);
306
307 if (!all) {
308 /* stop in case changing a single feature */
309 break;
310 }
311 }
312next:
313 ;
314 }
315
316 if (!all && !changed->count) {
317 LOGERR(mod->ctx, LY_EINVAL, "Feature \"%s\" not found in module \"%s\".", name, mod->name);
318 ly_set_free(changed, NULL);
319 return LY_EINVAL;
320 }
321
Radek Krejcica3db002018-11-01 10:31:01 +0100322 if (value && all && disabled_count) {
323 if (changed_count == changed->count) {
324 /* no change in last run -> not able to enable all ... */
325 /* ... print errors */
326 for (u = 0; disabled_count && u < LY_ARRAY_SIZE(mod->features); ++u) {
327 if (!(mod->features[u].flags & LYS_FENABLED)) {
328 LOGERR(mod->ctx, LY_EDENIED,
329 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
330 mod->features[u].name);
331 --disabled_count;
332 }
333 }
334 /* ... restore the original state */
335 for (u = 0; u < changed->count; ++u) {
336 f = changed->objs[u];
337 /* re-disable the feature */
338 f->flags &= ~LYS_FENABLED;
339 }
340
341 ly_set_free(changed, NULL);
342 return LY_EDENIED;
343 } else {
344 /* we did some change in last run, try it again */
345 changed_count = changed->count;
346 goto run;
347 }
348 }
349
Radek Krejci151a5b72018-10-19 14:21:44 +0200350 /* reflect change(s) in the dependent features */
351 for (u = 0; u < changed->count; ++u) {
352 /* If a dependent feature is enabled, it can be now changed by the change (to false) of the value of
353 * its if-feature statements. The reverse logic, automatically enable feature when its feature is enabled
354 * is not done - by default, features are disabled and must be explicitely enabled. */
355 f = changed->objs[u];
356 LY_ARRAY_FOR(f->depfeatures, struct lysc_feature*, df) {
357 if (!((*df)->flags & LYS_FENABLED)) {
358 /* not enabled, nothing to do */
359 continue;
360 }
361 /* check the feature's if-features which could change by the previous change of our feature */
362 LY_ARRAY_FOR((*df)->iffeatures, struct lysc_iffeature, iff) {
363 if (!lysc_iffeature_value(iff)) {
364 /* the feature must be disabled now */
365 (*df)->flags &= ~LYS_FENABLED;
366 /* add the feature into the list of changed features */
367 ly_set_add(changed, *df, LY_SET_OPT_USEASLIST);
368 break;
369 }
370 }
371 }
372 }
373
374 ly_set_free(changed, NULL);
375 return LY_SUCCESS;
376}
377
378API LY_ERR
379lys_feature_enable(struct lys_module *module, const char *feature)
380{
381 LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, LY_EINVAL);
382
383 return lys_feature_change(module->compiled, feature, 1);
384}
385
386API LY_ERR
387lys_feature_disable(struct lys_module *module, const char *feature)
388{
389 LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, LY_EINVAL);
390
391 return lys_feature_change(module->compiled, feature, 0);
392}
393
394API int
395lys_feature_value(const struct lys_module *module, const char *feature)
396{
397 struct lysc_feature *f;
398 struct lysc_module *mod;
399 unsigned int u;
400
401 LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, -1);
402 mod = module->compiled;
403
404 /* search for the specified feature */
405 for (u = 0; u < LY_ARRAY_SIZE(mod->features); ++u) {
Radek Krejci2c4e7172018-10-19 15:56:26 +0200406 f = &mod->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200407 if (!strcmp(f->name, feature)) {
408 if (f->flags & LYS_FENABLED) {
409 return 1;
410 } else {
411 return 0;
412 }
413 }
414 }
415
416 /* feature definition not found */
417 return -1;
418}
419
Radek Krejcia3045382018-11-22 14:30:31 +0100420API const struct lysc_iffeature *
421lys_is_disabled(const struct lysc_node *node, int recursive)
422{
423 unsigned int u;
Radek Krejcia3045382018-11-22 14:30:31 +0100424
425 LY_CHECK_ARG_RET(NULL, node, NULL);
426
427 while(node) {
428 if (node->nodetype & LYS_CHOICE) {
429 return NULL;
430 }
431
Radek Krejci056d0a82018-12-06 16:57:25 +0100432 if (node->iffeatures) {
Radek Krejcia3045382018-11-22 14:30:31 +0100433 /* check local if-features */
Radek Krejci056d0a82018-12-06 16:57:25 +0100434 LY_ARRAY_FOR(node->iffeatures, u) {
435 if (!lysc_iffeature_value(&node->iffeatures[u])) {
436 return &node->iffeatures[u];
Radek Krejcia3045382018-11-22 14:30:31 +0100437 }
438 }
439 }
440
441 if (!recursive) {
442 return NULL;
443 }
444
445 /* go through parents */
446 node = node->parent;
447 }
448 return NULL;
449}
450
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200451static void
Radek Krejci086c7132018-10-26 15:29:04 +0200452lys_latest_switch(struct lys_module *old, struct lysp_module *new)
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200453{
Radek Krejci086c7132018-10-26 15:29:04 +0200454 if (old->parsed) {
455 new->latest_revision = old->parsed->latest_revision;
456 old->parsed->latest_revision = 0;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200457 }
Radek Krejci086c7132018-10-26 15:29:04 +0200458 if (old->compiled) {
459 new->latest_revision = old->parsed->latest_revision;
460 old->compiled->latest_revision = 0;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200461 }
462}
463
Radek Krejcid33273d2018-10-25 14:55:52 +0200464struct lys_module *
Radek Krejci3b1f9292018-11-08 10:58:35 +0100465lys_parse_mem_(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, int implement, struct ly_parser_ctx *main_ctx,
Radek Krejci9ed7a192018-10-31 16:23:51 +0100466 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, void *data), void *check_data)
Radek Krejci86d106e2018-10-18 09:53:19 +0200467{
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100468 struct lys_module *mod = NULL, *latest, *mod_dup;
Radek Krejcid33273d2018-10-25 14:55:52 +0200469 struct lysp_module *latest_p;
Radek Krejci086c7132018-10-26 15:29:04 +0200470 struct lysp_import *imp;
471 struct lysp_include *inc;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100472 LY_ERR ret = LY_EINVAL;
Radek Krejci086c7132018-10-26 15:29:04 +0200473 unsigned int u, i;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100474 struct ly_parser_ctx context = {0};
Radek Krejci86d106e2018-10-18 09:53:19 +0200475
476 LY_CHECK_ARG_RET(ctx, ctx, data, NULL);
477
Radek Krejcibbe09a92018-11-08 09:36:54 +0100478 context.ctx = ctx;
479 context.line = 1;
480
Radek Krejci3b1f9292018-11-08 10:58:35 +0100481 if (main_ctx) {
482 /* map the typedefs and groupings list from main context to the submodule's context */
483 memcpy(&context.tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
484 memcpy(&context.grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
485 }
486
Radek Krejci86d106e2018-10-18 09:53:19 +0200487 mod = calloc(1, sizeof *mod);
488 LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), NULL);
489
490 switch (format) {
491 case LYS_IN_YIN:
492 /* TODO not yet supported
493 mod = yin_read_module(ctx, data, revision, implement);
494 */
495 break;
496 case LYS_IN_YANG:
Radek Krejcibbe09a92018-11-08 09:36:54 +0100497 ret = yang_parse(&context, data, &mod->parsed);
Radek Krejci86d106e2018-10-18 09:53:19 +0200498 break;
499 default:
500 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
501 break;
502 }
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100503 LY_CHECK_ERR_RET(ret, free(mod), NULL);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200504
505 /* make sure that the newest revision is at position 0 */
506 lysp_sort_revisions(mod->parsed->revs);
Radek Krejci86d106e2018-10-18 09:53:19 +0200507
508 if (implement) {
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200509 /* mark the loaded module implemented */
Radek Krejcib7db73a2018-10-24 14:18:40 +0200510 if (ly_ctx_get_module_implemented(ctx, mod->parsed->name)) {
511 LOGERR(ctx, LY_EDENIED, "Module \"%s\" is already implemented in the context.", mod->parsed->name);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100512 goto error;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200513 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200514 mod->parsed->implemented = 1;
515 }
516
Radek Krejci9ed7a192018-10-31 16:23:51 +0100517 if (custom_check) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100518 LY_CHECK_GOTO(custom_check(ctx, mod->parsed, check_data), error);
Radek Krejci86d106e2018-10-18 09:53:19 +0200519 }
520
Radek Krejcid33273d2018-10-25 14:55:52 +0200521 if (mod->parsed->submodule) { /* submodule */
Radek Krejci3b1f9292018-11-08 10:58:35 +0100522 if (!main_ctx) {
523 LOGERR(ctx, LY_EDENIED, "Input data contains submodule \"%s\" which cannot be parsed directly without its main module.",
524 mod->parsed->name);
525 goto error;
526 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200527 /* decide the latest revision */
528 latest_p = ly_ctx_get_submodule(ctx, mod->parsed->belongsto, mod->parsed->name, NULL);
529 if (latest_p) {
530 if (mod->parsed->revs) {
531 if (!latest_p->revs) {
532 /* latest has no revision, so mod is anyway newer */
Radek Krejci9ed7a192018-10-31 16:23:51 +0100533 mod->parsed->latest_revision = latest_p->latest_revision;
Radek Krejcid33273d2018-10-25 14:55:52 +0200534 latest_p->latest_revision = 0;
535 } else {
536 if (strcmp(mod->parsed->revs[0].date, latest_p->revs[0].date) > 0) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100537 mod->parsed->latest_revision = latest_p->latest_revision;
Radek Krejcid33273d2018-10-25 14:55:52 +0200538 latest_p->latest_revision = 0;
539 }
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200540 }
541 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200542 } else {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100543 mod->parsed->latest_revision = 1;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200544 }
Radek Krejci3b1f9292018-11-08 10:58:35 +0100545 /* remap possibly changed and reallocated typedefs and groupings list back to the main context */
546 memcpy(&main_ctx->tpdfs_nodes, &context.tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
547 memcpy(&main_ctx->grps_nodes, &context.grps_nodes, sizeof main_ctx->grps_nodes);
Radek Krejcid33273d2018-10-25 14:55:52 +0200548 } else { /* module */
549 /* check for duplicity in the context */
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100550 mod_dup = (struct lys_module*)ly_ctx_get_module(ctx, mod->parsed->name, mod->parsed->revs ? mod->parsed->revs[0].date : NULL);
551 if (mod_dup) {
552 if (mod_dup->parsed) {
553 /* error */
554 if (mod->parsed->revs) {
555 LOGERR(ctx, LY_EEXIST, "Module \"%s\" of revision \"%s\" is already present in the context.",
556 mod->parsed->name, mod->parsed->revs[0].date);
557 } else {
558 LOGERR(ctx, LY_EEXIST, "Module \"%s\" with no revision is already present in the context.",
559 mod->parsed->name);
560 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100561 goto error;
Radek Krejcid33273d2018-10-25 14:55:52 +0200562 } else {
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100563 /* add the parsed data to the currently compiled-only module in the context */
564 mod_dup->parsed = mod->parsed;
565 free(mod);
566 mod = mod_dup;
567 goto finish_parsing;
Radek Krejcid33273d2018-10-25 14:55:52 +0200568 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200569 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200570
571#if 0
572 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
573 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
574 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
575 * the anotation definitions available in the internal schema structure. There is another hack in schema
576 * printers to do not print this internally added annotation. */
577 if (mod && ly_strequal(mod->name, "ietf-netconf", 0)) {
578 if (lyp_add_ietf_netconf_annotations(mod)) {
579 lys_free(mod, NULL, 1, 1);
580 return NULL;
581 }
582 }
583#endif
584
Radek Krejcid33273d2018-10-25 14:55:52 +0200585 /* decide the latest revision */
586 latest = (struct lys_module*)ly_ctx_get_module_latest(ctx, mod->parsed->name);
587 if (latest) {
588 if (mod->parsed->revs) {
Radek Krejcif8f882a2018-10-31 14:51:15 +0100589 if ((latest->parsed && !latest->parsed->revs) || (!latest->parsed && !latest->compiled->revision)) {
Radek Krejcid33273d2018-10-25 14:55:52 +0200590 /* latest has no revision, so mod is anyway newer */
Radek Krejci086c7132018-10-26 15:29:04 +0200591 lys_latest_switch(latest, mod->parsed);
Radek Krejcid33273d2018-10-25 14:55:52 +0200592 } else {
Radek Krejcif8f882a2018-10-31 14:51:15 +0100593 if (strcmp(mod->parsed->revs[0].date, latest->parsed ? latest->parsed->revs[0].date : latest->compiled->revision) > 0) {
Radek Krejci086c7132018-10-26 15:29:04 +0200594 lys_latest_switch(latest, mod->parsed);
Radek Krejcid33273d2018-10-25 14:55:52 +0200595 }
596 }
597 }
598 } else {
599 mod->parsed->latest_revision = 1;
600 }
601
602 /* add into context */
603 ly_set_add(&ctx->list, mod, LY_SET_OPT_USEASLIST);
604
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100605finish_parsing:
Radek Krejcibbe09a92018-11-08 09:36:54 +0100606 /* resolve imports */
Radek Krejci086c7132018-10-26 15:29:04 +0200607 mod->parsed->parsing = 1;
608 LY_ARRAY_FOR(mod->parsed->imports, u) {
609 imp = &mod->parsed->imports[u];
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100610 if (!imp->module && lysp_load_module(ctx, imp->name, imp->rev[0] ? imp->rev : NULL, 0, 0, &imp->module)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100611 goto error_ctx;
Radek Krejci086c7132018-10-26 15:29:04 +0200612 }
613 /* check for importing the same module twice */
614 for (i = 0; i < u; ++i) {
615 if (imp->module == mod->parsed->imports[i].module) {
616 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 +0100617 goto error_ctx;
Radek Krejci086c7132018-10-26 15:29:04 +0200618 }
619 }
620 }
621 LY_ARRAY_FOR(mod->parsed->includes, u) {
622 inc = &mod->parsed->includes[u];
Radek Krejci3b1f9292018-11-08 10:58:35 +0100623 if (!inc->submodule && lysp_load_submodule(&context, mod->parsed, inc)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100624 goto error_ctx;
Radek Krejci086c7132018-10-26 15:29:04 +0200625 }
626 }
627 mod->parsed->parsing = 0;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100628
629 /* check name collisions - typedefs and groupings */
630 LY_CHECK_GOTO(lysp_check_typedefs(&context), error_ctx);
Radek Krejcid33273d2018-10-25 14:55:52 +0200631 }
632
Radek Krejci86d106e2018-10-18 09:53:19 +0200633 return mod;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100634
635error_ctx:
636 ly_set_rm(&ctx->list, mod, NULL);
637error:
638 lys_module_free(mod, NULL);
639 ly_set_erase(&context.tpdfs_nodes, NULL);
640 return NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +0200641}
642
Radek Krejcid14e9692018-11-01 11:00:37 +0100643API struct lys_module *
Radek Krejci86d106e2018-10-18 09:53:19 +0200644lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format)
645{
Radek Krejci3b1f9292018-11-08 10:58:35 +0100646 return lys_parse_mem_(ctx, data, format, 1, NULL, NULL, NULL);
Radek Krejci86d106e2018-10-18 09:53:19 +0200647}
648
649static void
650lys_parse_set_filename(struct ly_ctx *ctx, const char **filename, int fd)
651{
Radek Krejci65639b92018-11-27 10:51:37 +0100652 char path[PATH_MAX];
Radek Krejci86d106e2018-10-18 09:53:19 +0200653
654#ifdef __APPLE__
655 if (fcntl(fd, F_GETPATH, path) != -1) {
656 *filename = lydict_insert(ctx, path, 0);
657 }
658#else
Radek Krejci65639b92018-11-27 10:51:37 +0100659 int len;
660 char proc_path[32];
661
Radek Krejci86d106e2018-10-18 09:53:19 +0200662 /* get URI if there is /proc */
663 sprintf(proc_path, "/proc/self/fd/%d", fd);
664 if ((len = readlink(proc_path, path, PATH_MAX - 1)) > 0) {
665 *filename = lydict_insert(ctx, path, len);
666 }
667#endif
668}
669
Radek Krejcid33273d2018-10-25 14:55:52 +0200670struct lys_module *
Radek Krejci3b1f9292018-11-08 10:58:35 +0100671lys_parse_fd_(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, int implement, struct ly_parser_ctx *main_ctx,
Radek Krejci9ed7a192018-10-31 16:23:51 +0100672 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, void *data), void *check_data)
Radek Krejci86d106e2018-10-18 09:53:19 +0200673{
Radek Krejcid33273d2018-10-25 14:55:52 +0200674 struct lys_module *mod;
Radek Krejci86d106e2018-10-18 09:53:19 +0200675 size_t length;
676 char *addr;
677
678 LY_CHECK_ARG_RET(ctx, ctx, NULL);
679 if (fd < 0) {
680 LOGARG(ctx, fd);
681 return NULL;
682 }
683
684 LY_CHECK_RET(ly_mmap(ctx, fd, &length, (void **)&addr), NULL);
685 if (!addr) {
686 LOGERR(ctx, LY_EINVAL, "Empty schema file.");
687 return NULL;
688 }
689
Radek Krejci3b1f9292018-11-08 10:58:35 +0100690 mod = lys_parse_mem_(ctx, addr, format, implement, main_ctx, custom_check, check_data);
Radek Krejci86d106e2018-10-18 09:53:19 +0200691 ly_munmap(addr, length);
692
693 if (mod && !mod->parsed->filepath) {
694 lys_parse_set_filename(ctx, &mod->parsed->filepath, fd);
695 }
696
697 return mod;
698}
699
Radek Krejcid14e9692018-11-01 11:00:37 +0100700API struct lys_module *
Radek Krejci86d106e2018-10-18 09:53:19 +0200701lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format)
702{
Radek Krejci3b1f9292018-11-08 10:58:35 +0100703 return lys_parse_fd_(ctx, fd, format, 1, NULL, NULL, NULL);
Radek Krejci86d106e2018-10-18 09:53:19 +0200704}
705
Radek Krejcid33273d2018-10-25 14:55:52 +0200706struct lys_module *
Radek Krejci3b1f9292018-11-08 10:58:35 +0100707lys_parse_path_(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, int implement, struct ly_parser_ctx *main_ctx,
Radek Krejci9ed7a192018-10-31 16:23:51 +0100708 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, void *data), void *check_data)
Radek Krejci86d106e2018-10-18 09:53:19 +0200709{
710 int fd;
Radek Krejcid33273d2018-10-25 14:55:52 +0200711 struct lys_module *mod;
Radek Krejci86d106e2018-10-18 09:53:19 +0200712 const char *rev, *dot, *filename;
713 size_t len;
714
715 LY_CHECK_ARG_RET(ctx, ctx, path, NULL);
716
717 fd = open(path, O_RDONLY);
718 LY_CHECK_ERR_RET(fd == -1, LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)), NULL);
719
Radek Krejci3b1f9292018-11-08 10:58:35 +0100720 mod = lys_parse_fd_(ctx, fd, format, implement, main_ctx, custom_check, check_data);
Radek Krejci86d106e2018-10-18 09:53:19 +0200721 close(fd);
722 LY_CHECK_RET(!mod, NULL);
723
724 /* check that name and revision match filename */
725 filename = strrchr(path, '/');
726 if (!filename) {
727 filename = path;
728 } else {
729 filename++;
730 }
731 rev = strchr(filename, '@');
732 dot = strrchr(filename, '.');
733
734 /* name */
735 len = strlen(mod->parsed->name);
736 if (strncmp(filename, mod->parsed->name, len) ||
737 ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) {
738 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->parsed->name);
739 }
740 if (rev) {
741 len = dot - ++rev;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200742 if (!mod->parsed->revs || len != 10 || strncmp(mod->parsed->revs[0].date, rev, len)) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200743 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Radek Krejcib7db73a2018-10-24 14:18:40 +0200744 mod->parsed->revs ? mod->parsed->revs[0].date : "none");
Radek Krejci86d106e2018-10-18 09:53:19 +0200745 }
746 }
747
748 if (!mod->parsed->filepath) {
749 /* store URI */
750 char rpath[PATH_MAX];
751 if (realpath(path, rpath) != NULL) {
752 mod->parsed->filepath = lydict_insert(ctx, rpath, 0);
753 } else {
754 mod->parsed->filepath = lydict_insert(ctx, path, 0);
755 }
756 }
757
758 return mod;
759}
760
Radek Krejcid14e9692018-11-01 11:00:37 +0100761API struct lys_module *
Radek Krejcid33273d2018-10-25 14:55:52 +0200762lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format)
763{
Radek Krejci3b1f9292018-11-08 10:58:35 +0100764 return lys_parse_path_(ctx, path, format, 1, NULL, NULL, NULL);
Radek Krejcid33273d2018-10-25 14:55:52 +0200765}
766
767API LY_ERR
768lys_search_localfile(const char * const *searchpaths, int cwd, const char *name, const char *revision,
769 char **localfile, LYS_INFORMAT *format)
770{
771 size_t len, flen, match_len = 0, dir_len;
772 int i, implicit_cwd = 0, ret = EXIT_FAILURE;
773 char *wd, *wn = NULL;
774 DIR *dir = NULL;
775 struct dirent *file;
776 char *match_name = NULL;
777 LYS_INFORMAT format_aux, match_format = 0;
778 struct ly_set *dirs;
779 struct stat st;
780
781 LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL);
782
783 /* start to fill the dir fifo with the context's search path (if set)
784 * and the current working directory */
785 dirs = ly_set_new();
786 if (!dirs) {
787 LOGMEM(NULL);
788 return EXIT_FAILURE;
789 }
790
791 len = strlen(name);
792 if (cwd) {
793 wd = get_current_dir_name();
794 if (!wd) {
795 LOGMEM(NULL);
796 goto cleanup;
797 } else {
798 /* add implicit current working directory (./) to be searched,
799 * this directory is not searched recursively */
800 if (ly_set_add(dirs, wd, 0) == -1) {
801 goto cleanup;
802 }
803 implicit_cwd = 1;
804 }
805 }
806 if (searchpaths) {
807 for (i = 0; searchpaths[i]; i++) {
808 /* check for duplicities with the implicit current working directory */
809 if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) {
810 implicit_cwd = 0;
811 continue;
812 }
813 wd = strdup(searchpaths[i]);
814 if (!wd) {
815 LOGMEM(NULL);
816 goto cleanup;
817 } else if (ly_set_add(dirs, wd, 0) == -1) {
818 goto cleanup;
819 }
820 }
821 }
822 wd = NULL;
823
824 /* start searching */
825 while (dirs->count) {
826 free(wd);
827 free(wn); wn = NULL;
828
829 dirs->count--;
830 wd = (char *)dirs->objs[dirs->count];
831 dirs->objs[dirs->count] = NULL;
832 LOGVRB("Searching for \"%s\" in %s.", name, wd);
833
834 if (dir) {
835 closedir(dir);
836 }
837 dir = opendir(wd);
838 dir_len = strlen(wd);
839 if (!dir) {
840 LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno));
841 } else {
842 while ((file = readdir(dir))) {
843 if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) {
844 /* skip . and .. */
845 continue;
846 }
847 free(wn);
848 if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) {
849 LOGMEM(NULL);
850 goto cleanup;
851 }
852 if (stat(wn, &st) == -1) {
853 LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)",
854 file->d_name, wd, strerror(errno));
855 continue;
856 }
857 if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) {
858 /* we have another subdirectory in searchpath to explore,
859 * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */
860 if (ly_set_add(dirs, wn, 0) == -1) {
861 goto cleanup;
862 }
863 /* continue with the next item in current directory */
864 wn = NULL;
865 continue;
866 } else if (!S_ISREG(st.st_mode)) {
867 /* not a regular file (note that we see the target of symlinks instead of symlinks */
868 continue;
869 }
870
871 /* here we know that the item is a file which can contain a module */
872 if (strncmp(name, file->d_name, len) ||
873 (file->d_name[len] != '.' && file->d_name[len] != '@')) {
874 /* different filename than the module we search for */
875 continue;
876 }
877
878 /* get type according to filename suffix */
879 flen = strlen(file->d_name);
880 if (!strcmp(&file->d_name[flen - 4], ".yin")) {
881 format_aux = LYS_IN_YIN;
882 } else if (!strcmp(&file->d_name[flen - 5], ".yang")) {
883 format_aux = LYS_IN_YANG;
884 } else {
885 /* not supportde suffix/file format */
886 continue;
887 }
888
889 if (revision) {
890 /* we look for the specific revision, try to get it from the filename */
891 if (file->d_name[len] == '@') {
892 /* check revision from the filename */
893 if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) {
894 /* another revision */
895 continue;
896 } else {
897 /* exact revision */
898 free(match_name);
899 match_name = wn;
900 wn = NULL;
901 match_len = dir_len + 1 + len;
902 match_format = format_aux;
903 goto success;
904 }
905 } else {
906 /* continue trying to find exact revision match, use this only if not found */
907 free(match_name);
908 match_name = wn;
909 wn = NULL;
910 match_len = dir_len + 1 +len;
911 match_format = format_aux;
912 continue;
913 }
914 } else {
915 /* remember the revision and try to find the newest one */
916 if (match_name) {
917 if (file->d_name[len] != '@' ||
918 lysp_check_date(NULL, &file->d_name[len + 1], flen - (format_aux == LYS_IN_YANG ? 5 : 4) - len - 1, NULL)) {
919 continue;
920 } else if (match_name[match_len] == '@' &&
921 (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) {
922 continue;
923 }
924 free(match_name);
925 }
926
927 match_name = wn;
928 wn = NULL;
929 match_len = dir_len + 1 + len;
930 match_format = format_aux;
931 continue;
932 }
933 }
934 }
935 }
936
937success:
938 (*localfile) = match_name;
939 match_name = NULL;
940 if (format) {
941 (*format) = match_format;
942 }
943 ret = EXIT_SUCCESS;
944
945cleanup:
946 free(wn);
947 free(wd);
948 if (dir) {
949 closedir(dir);
950 }
951 free(match_name);
952 ly_set_free(dirs, free);
953
954 return ret;
955}
956