blob: 537d3b6e0337381e0d7a88ba5e59590670aac9f9 [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 */
45 snode = lysc_node_children(parent);
46 /* do not return anything if the augment does not have any children */
47 if (!snode || !(*snode)) {
48 return NULL;
49 }
50 next = last = *snode;
51 } else {
52 /* top level data */
53 next = last = module->data;
54 }
55 if (!next) {
56 return next;
57 } else if (!(options & LYS_GETNEXT_NOSTATECHECK)) {
58 if (!lys_is_disabled(next, 0)) {
59 return next;
60 }
61 /* continue to find next available node */
62 } else {
63 return next;
64 }
65 }
66
67 next = last->next;
68repeat:
69 if (!next) {
70 return next;
71 }
72 switch (next->nodetype) {
73 case LYS_ACTION:
74 case LYS_NOTIF:
75 case LYS_LEAF:
76 case LYS_ANYXML:
77 case LYS_ANYDATA:
78 case LYS_LIST:
79 case LYS_LEAFLIST:
80 break;
81 case LYS_CONTAINER:
82 if (!(((struct lysc_node_container *)next)->flags & LYS_PRESENCE) && (options & LYS_GETNEXT_INTONPCONT)) {
83 if (((struct lysc_node_container *)next)->child) {
84 /* go into */
85 next = ((struct lysc_node_container *)next)->child;
86 } else {
87 next = next->next;
88 }
89 goto repeat;
90 }
91 break;
92 case LYS_CHOICE:
93 if (options & LYS_GETNEXT_WITHCHOICE) {
94 return next;
95 } else if (((struct lysc_node_choice *)next)->cases) {
96 /* go into */
97 next = ((struct lysc_node_choice *)next)->cases[0].child;
98 } else {
99 next = next->next;
100 }
101 goto repeat;
102 default:
103 /* we should not be here */
104 LOGINT(last->module->compiled->ctx);
105 return NULL;
106 }
107
108 if (!(options & LYS_GETNEXT_NOSTATECHECK)) {
109 /* check if the node is disabled by if-feature */
110 if (lys_is_disabled(next, 0)) {
111 next = next->next;
112 goto repeat;
113 }
114 }
115 return next;
116}
117
118API const struct lysc_node *
119lys_child(const struct lysc_node *parent, const struct lys_module *module,
120 const char *name, size_t name_len, uint16_t nodetype, int options)
121{
122 const struct lysc_node *node = NULL;
123
124 LY_CHECK_ARG_RET(NULL, module, name, NULL);
125 if (!nodetype) {
126 nodetype = 0xffff;
127 }
128
129 while ((node = lys_getnext(node, parent, module->compiled, options))) {
130 if (!(node->nodetype & nodetype)) {
131 continue;
132 }
133 if (node->module != module) {
134 continue;
135 }
136
137 if (name_len) {
138 if (!strncmp(node->name, name, name_len) && !node->name[name_len]) {
139 return node;
140 }
141 } else {
142 if (!strcmp(node->name, name)) {
143 return node;
144 }
145 }
146 }
147 return NULL;
148}
149
Radek Krejci19a96102018-11-15 13:38:09 +0100150API int
151lysc_feature_value(const struct lysc_feature *feature)
Radek Krejci6f7feb62018-10-12 15:23:02 +0200152{
Radek Krejci19a96102018-11-15 13:38:09 +0100153 LY_CHECK_ARG_RET(NULL, feature, -1);
154 return feature->flags & LYS_FENABLED ? 1 : 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200155}
156
157static uint8_t
158iff_getop(uint8_t *list, int pos)
159{
160 uint8_t *item;
161 uint8_t mask = 3, result;
162
163 assert(pos >= 0);
164
165 item = &list[pos / 4];
166 result = (*item) & (mask << 2 * (pos % 4));
167 return result >> 2 * (pos % 4);
168}
169
Radek Krejci151a5b72018-10-19 14:21:44 +0200170static int
171lysc_iffeature_value_(const struct lysc_iffeature *iff, int *index_e, int *index_f)
172{
173 uint8_t op;
174 int a, b;
175
176 op = iff_getop(iff->expr, *index_e);
177 (*index_e)++;
178
179 switch (op) {
180 case LYS_IFF_F:
181 /* resolve feature */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200182 return lysc_feature_value(iff->features[(*index_f)++]);
Radek Krejci151a5b72018-10-19 14:21:44 +0200183 case LYS_IFF_NOT:
184 /* invert result */
185 return lysc_iffeature_value_(iff, index_e, index_f) ? 0 : 1;
186 case LYS_IFF_AND:
187 case LYS_IFF_OR:
188 a = lysc_iffeature_value_(iff, index_e, index_f);
189 b = lysc_iffeature_value_(iff, index_e, index_f);
190 if (op == LYS_IFF_AND) {
191 return a && b;
192 } else { /* LYS_IFF_OR */
193 return a || b;
194 }
195 }
196
197 return 0;
198}
199
200API int
201lysc_iffeature_value(const struct lysc_iffeature *iff)
202{
203 int index_e = 0, index_f = 0;
204
205 LY_CHECK_ARG_RET(NULL, iff, -1);
206
207 if (iff->expr) {
208 return lysc_iffeature_value_(iff, &index_e, &index_f);
209 }
210 return 0;
211}
212
Radek Krejci151a5b72018-10-19 14:21:44 +0200213/**
214 * @brief Enable/Disable the specified feature in the module.
215 *
216 * If the feature is already set to the desired value, LY_SUCCESS is returned.
217 * By changing the feature, also all the feature which depends on it via their
218 * if-feature statements are again evaluated (disabled if a if-feature statemen
219 * evaluates to false).
220 *
221 * @param[in] mod Compiled module where to set (search for) the feature.
222 * @param[in] name Name of the feature to set. Asterisk ('*') can be used to
223 * set all the features in the module.
224 * @param[in] value Desired value of the feature: 1 (enable) or 0 (disable).
225 * @return LY_ERR value.
226 */
227static LY_ERR
228lys_feature_change(const struct lysc_module *mod, const char *name, int value)
229{
230 int all = 0;
Radek Krejcica3db002018-11-01 10:31:01 +0100231 unsigned int u, changed_count, disabled_count;
Radek Krejci151a5b72018-10-19 14:21:44 +0200232 struct lysc_feature *f, **df;
233 struct lysc_iffeature *iff;
234 struct ly_set *changed;
235
236 if (!mod->features) {
237 LOGERR(mod->ctx, LY_EINVAL, "Unable to switch feature since the module \"%s\" has no features.", mod->name);
238 return LY_EINVAL;
239 }
240
241 if (!strcmp(name, "*")) {
242 /* enable all */
243 all = 1;
244 }
245 changed = ly_set_new();
Radek Krejcica3db002018-11-01 10:31:01 +0100246 changed_count = 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200247
Radek Krejcica3db002018-11-01 10:31:01 +0100248run:
249 for (disabled_count = u = 0; u < LY_ARRAY_SIZE(mod->features); ++u) {
Radek Krejci2c4e7172018-10-19 15:56:26 +0200250 f = &mod->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200251 if (all || !strcmp(f->name, name)) {
252 if ((value && (f->flags & LYS_FENABLED)) || (!value && !(f->flags & LYS_FENABLED))) {
253 if (all) {
254 /* skip already set features */
255 continue;
256 } else {
257 /* feature already set correctly */
258 ly_set_free(changed, NULL);
259 return LY_SUCCESS;
260 }
261 }
262
263 if (value) { /* enable */
264 /* check referenced features if they are enabled */
265 LY_ARRAY_FOR(f->iffeatures, struct lysc_iffeature, iff) {
266 if (!lysc_iffeature_value(iff)) {
267 if (all) {
Radek Krejcica3db002018-11-01 10:31:01 +0100268 ++disabled_count;
Radek Krejci151a5b72018-10-19 14:21:44 +0200269 goto next;
270 } else {
271 LOGERR(mod->ctx, LY_EDENIED,
272 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
273 f->name);
274 ly_set_free(changed, NULL);
275 return LY_EDENIED;
276 }
277 }
278 }
279 /* enable the feature */
280 f->flags |= LYS_FENABLED;
281 } else { /* disable */
282 /* disable the feature */
283 f->flags &= ~LYS_FENABLED;
284 }
285
286 /* remember the changed feature */
287 ly_set_add(changed, f, LY_SET_OPT_USEASLIST);
288
289 if (!all) {
290 /* stop in case changing a single feature */
291 break;
292 }
293 }
294next:
295 ;
296 }
297
298 if (!all && !changed->count) {
299 LOGERR(mod->ctx, LY_EINVAL, "Feature \"%s\" not found in module \"%s\".", name, mod->name);
300 ly_set_free(changed, NULL);
301 return LY_EINVAL;
302 }
303
Radek Krejcica3db002018-11-01 10:31:01 +0100304 if (value && all && disabled_count) {
305 if (changed_count == changed->count) {
306 /* no change in last run -> not able to enable all ... */
307 /* ... print errors */
308 for (u = 0; disabled_count && u < LY_ARRAY_SIZE(mod->features); ++u) {
309 if (!(mod->features[u].flags & LYS_FENABLED)) {
310 LOGERR(mod->ctx, LY_EDENIED,
311 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
312 mod->features[u].name);
313 --disabled_count;
314 }
315 }
316 /* ... restore the original state */
317 for (u = 0; u < changed->count; ++u) {
318 f = changed->objs[u];
319 /* re-disable the feature */
320 f->flags &= ~LYS_FENABLED;
321 }
322
323 ly_set_free(changed, NULL);
324 return LY_EDENIED;
325 } else {
326 /* we did some change in last run, try it again */
327 changed_count = changed->count;
328 goto run;
329 }
330 }
331
Radek Krejci151a5b72018-10-19 14:21:44 +0200332 /* reflect change(s) in the dependent features */
333 for (u = 0; u < changed->count; ++u) {
334 /* If a dependent feature is enabled, it can be now changed by the change (to false) of the value of
335 * its if-feature statements. The reverse logic, automatically enable feature when its feature is enabled
336 * is not done - by default, features are disabled and must be explicitely enabled. */
337 f = changed->objs[u];
338 LY_ARRAY_FOR(f->depfeatures, struct lysc_feature*, df) {
339 if (!((*df)->flags & LYS_FENABLED)) {
340 /* not enabled, nothing to do */
341 continue;
342 }
343 /* check the feature's if-features which could change by the previous change of our feature */
344 LY_ARRAY_FOR((*df)->iffeatures, struct lysc_iffeature, iff) {
345 if (!lysc_iffeature_value(iff)) {
346 /* the feature must be disabled now */
347 (*df)->flags &= ~LYS_FENABLED;
348 /* add the feature into the list of changed features */
349 ly_set_add(changed, *df, LY_SET_OPT_USEASLIST);
350 break;
351 }
352 }
353 }
354 }
355
356 ly_set_free(changed, NULL);
357 return LY_SUCCESS;
358}
359
360API LY_ERR
361lys_feature_enable(struct lys_module *module, const char *feature)
362{
363 LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, LY_EINVAL);
364
365 return lys_feature_change(module->compiled, feature, 1);
366}
367
368API LY_ERR
369lys_feature_disable(struct lys_module *module, const char *feature)
370{
371 LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, LY_EINVAL);
372
373 return lys_feature_change(module->compiled, feature, 0);
374}
375
376API int
377lys_feature_value(const struct lys_module *module, const char *feature)
378{
379 struct lysc_feature *f;
380 struct lysc_module *mod;
381 unsigned int u;
382
383 LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, -1);
384 mod = module->compiled;
385
386 /* search for the specified feature */
387 for (u = 0; u < LY_ARRAY_SIZE(mod->features); ++u) {
Radek Krejci2c4e7172018-10-19 15:56:26 +0200388 f = &mod->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200389 if (!strcmp(f->name, feature)) {
390 if (f->flags & LYS_FENABLED) {
391 return 1;
392 } else {
393 return 0;
394 }
395 }
396 }
397
398 /* feature definition not found */
399 return -1;
400}
401
Radek Krejcia3045382018-11-22 14:30:31 +0100402API const struct lysc_iffeature *
403lys_is_disabled(const struct lysc_node *node, int recursive)
404{
405 unsigned int u;
406 struct lysc_iffeature **iff;
407
408 LY_CHECK_ARG_RET(NULL, node, NULL);
409
410 while(node) {
411 if (node->nodetype & LYS_CHOICE) {
412 return NULL;
413 }
414
415 iff = lysc_node_iff(node);
416 if (iff && *iff) {
417 /* check local if-features */
418 LY_ARRAY_FOR(*iff, u) {
419 if (!lysc_iffeature_value(&(*iff)[u])) {
420 return &(*iff)[u];
421 }
422 }
423 }
424
425 if (!recursive) {
426 return NULL;
427 }
428
429 /* go through parents */
430 node = node->parent;
431 }
432 return NULL;
433}
434
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200435static void
Radek Krejci086c7132018-10-26 15:29:04 +0200436lys_latest_switch(struct lys_module *old, struct lysp_module *new)
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200437{
Radek Krejci086c7132018-10-26 15:29:04 +0200438 if (old->parsed) {
439 new->latest_revision = old->parsed->latest_revision;
440 old->parsed->latest_revision = 0;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200441 }
Radek Krejci086c7132018-10-26 15:29:04 +0200442 if (old->compiled) {
443 new->latest_revision = old->parsed->latest_revision;
444 old->compiled->latest_revision = 0;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200445 }
446}
447
Radek Krejcid33273d2018-10-25 14:55:52 +0200448struct lys_module *
Radek Krejci3b1f9292018-11-08 10:58:35 +0100449lys_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 +0100450 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, void *data), void *check_data)
Radek Krejci86d106e2018-10-18 09:53:19 +0200451{
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100452 struct lys_module *mod = NULL, *latest, *mod_dup;
Radek Krejcid33273d2018-10-25 14:55:52 +0200453 struct lysp_module *latest_p;
Radek Krejci086c7132018-10-26 15:29:04 +0200454 struct lysp_import *imp;
455 struct lysp_include *inc;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100456 LY_ERR ret = LY_EINVAL;
Radek Krejci086c7132018-10-26 15:29:04 +0200457 unsigned int u, i;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100458 struct ly_parser_ctx context = {0};
Radek Krejci86d106e2018-10-18 09:53:19 +0200459
460 LY_CHECK_ARG_RET(ctx, ctx, data, NULL);
461
Radek Krejcibbe09a92018-11-08 09:36:54 +0100462 context.ctx = ctx;
463 context.line = 1;
464
Radek Krejci3b1f9292018-11-08 10:58:35 +0100465 if (main_ctx) {
466 /* map the typedefs and groupings list from main context to the submodule's context */
467 memcpy(&context.tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
468 memcpy(&context.grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
469 }
470
Radek Krejci86d106e2018-10-18 09:53:19 +0200471 mod = calloc(1, sizeof *mod);
472 LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), NULL);
473
474 switch (format) {
475 case LYS_IN_YIN:
476 /* TODO not yet supported
477 mod = yin_read_module(ctx, data, revision, implement);
478 */
479 break;
480 case LYS_IN_YANG:
Radek Krejcibbe09a92018-11-08 09:36:54 +0100481 ret = yang_parse(&context, data, &mod->parsed);
Radek Krejci86d106e2018-10-18 09:53:19 +0200482 break;
483 default:
484 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
485 break;
486 }
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100487 LY_CHECK_ERR_RET(ret, free(mod), NULL);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200488
489 /* make sure that the newest revision is at position 0 */
490 lysp_sort_revisions(mod->parsed->revs);
Radek Krejci86d106e2018-10-18 09:53:19 +0200491
492 if (implement) {
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200493 /* mark the loaded module implemented */
Radek Krejcib7db73a2018-10-24 14:18:40 +0200494 if (ly_ctx_get_module_implemented(ctx, mod->parsed->name)) {
495 LOGERR(ctx, LY_EDENIED, "Module \"%s\" is already implemented in the context.", mod->parsed->name);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100496 goto error;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200497 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200498 mod->parsed->implemented = 1;
499 }
500
Radek Krejci9ed7a192018-10-31 16:23:51 +0100501 if (custom_check) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100502 LY_CHECK_GOTO(custom_check(ctx, mod->parsed, check_data), error);
Radek Krejci86d106e2018-10-18 09:53:19 +0200503 }
504
Radek Krejcid33273d2018-10-25 14:55:52 +0200505 if (mod->parsed->submodule) { /* submodule */
Radek Krejci3b1f9292018-11-08 10:58:35 +0100506 if (!main_ctx) {
507 LOGERR(ctx, LY_EDENIED, "Input data contains submodule \"%s\" which cannot be parsed directly without its main module.",
508 mod->parsed->name);
509 goto error;
510 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200511 /* decide the latest revision */
512 latest_p = ly_ctx_get_submodule(ctx, mod->parsed->belongsto, mod->parsed->name, NULL);
513 if (latest_p) {
514 if (mod->parsed->revs) {
515 if (!latest_p->revs) {
516 /* latest has no revision, so mod is anyway newer */
Radek Krejci9ed7a192018-10-31 16:23:51 +0100517 mod->parsed->latest_revision = latest_p->latest_revision;
Radek Krejcid33273d2018-10-25 14:55:52 +0200518 latest_p->latest_revision = 0;
519 } else {
520 if (strcmp(mod->parsed->revs[0].date, latest_p->revs[0].date) > 0) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100521 mod->parsed->latest_revision = latest_p->latest_revision;
Radek Krejcid33273d2018-10-25 14:55:52 +0200522 latest_p->latest_revision = 0;
523 }
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200524 }
525 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200526 } else {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100527 mod->parsed->latest_revision = 1;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200528 }
Radek Krejci3b1f9292018-11-08 10:58:35 +0100529 /* remap possibly changed and reallocated typedefs and groupings list back to the main context */
530 memcpy(&main_ctx->tpdfs_nodes, &context.tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
531 memcpy(&main_ctx->grps_nodes, &context.grps_nodes, sizeof main_ctx->grps_nodes);
Radek Krejcid33273d2018-10-25 14:55:52 +0200532 } else { /* module */
533 /* check for duplicity in the context */
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100534 mod_dup = (struct lys_module*)ly_ctx_get_module(ctx, mod->parsed->name, mod->parsed->revs ? mod->parsed->revs[0].date : NULL);
535 if (mod_dup) {
536 if (mod_dup->parsed) {
537 /* error */
538 if (mod->parsed->revs) {
539 LOGERR(ctx, LY_EEXIST, "Module \"%s\" of revision \"%s\" is already present in the context.",
540 mod->parsed->name, mod->parsed->revs[0].date);
541 } else {
542 LOGERR(ctx, LY_EEXIST, "Module \"%s\" with no revision is already present in the context.",
543 mod->parsed->name);
544 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100545 goto error;
Radek Krejcid33273d2018-10-25 14:55:52 +0200546 } else {
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100547 /* add the parsed data to the currently compiled-only module in the context */
548 mod_dup->parsed = mod->parsed;
549 free(mod);
550 mod = mod_dup;
551 goto finish_parsing;
Radek Krejcid33273d2018-10-25 14:55:52 +0200552 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200553 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200554
555#if 0
556 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
557 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
558 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
559 * the anotation definitions available in the internal schema structure. There is another hack in schema
560 * printers to do not print this internally added annotation. */
561 if (mod && ly_strequal(mod->name, "ietf-netconf", 0)) {
562 if (lyp_add_ietf_netconf_annotations(mod)) {
563 lys_free(mod, NULL, 1, 1);
564 return NULL;
565 }
566 }
567#endif
568
Radek Krejcid33273d2018-10-25 14:55:52 +0200569 /* decide the latest revision */
570 latest = (struct lys_module*)ly_ctx_get_module_latest(ctx, mod->parsed->name);
571 if (latest) {
572 if (mod->parsed->revs) {
Radek Krejcif8f882a2018-10-31 14:51:15 +0100573 if ((latest->parsed && !latest->parsed->revs) || (!latest->parsed && !latest->compiled->revision)) {
Radek Krejcid33273d2018-10-25 14:55:52 +0200574 /* latest has no revision, so mod is anyway newer */
Radek Krejci086c7132018-10-26 15:29:04 +0200575 lys_latest_switch(latest, mod->parsed);
Radek Krejcid33273d2018-10-25 14:55:52 +0200576 } else {
Radek Krejcif8f882a2018-10-31 14:51:15 +0100577 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 +0200578 lys_latest_switch(latest, mod->parsed);
Radek Krejcid33273d2018-10-25 14:55:52 +0200579 }
580 }
581 }
582 } else {
583 mod->parsed->latest_revision = 1;
584 }
585
586 /* add into context */
587 ly_set_add(&ctx->list, mod, LY_SET_OPT_USEASLIST);
588
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100589finish_parsing:
Radek Krejcibbe09a92018-11-08 09:36:54 +0100590 /* resolve imports */
Radek Krejci086c7132018-10-26 15:29:04 +0200591 mod->parsed->parsing = 1;
592 LY_ARRAY_FOR(mod->parsed->imports, u) {
593 imp = &mod->parsed->imports[u];
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100594 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 +0100595 goto error_ctx;
Radek Krejci086c7132018-10-26 15:29:04 +0200596 }
597 /* check for importing the same module twice */
598 for (i = 0; i < u; ++i) {
599 if (imp->module == mod->parsed->imports[i].module) {
600 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 +0100601 goto error_ctx;
Radek Krejci086c7132018-10-26 15:29:04 +0200602 }
603 }
604 }
605 LY_ARRAY_FOR(mod->parsed->includes, u) {
606 inc = &mod->parsed->includes[u];
Radek Krejci3b1f9292018-11-08 10:58:35 +0100607 if (!inc->submodule && lysp_load_submodule(&context, mod->parsed, inc)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100608 goto error_ctx;
Radek Krejci086c7132018-10-26 15:29:04 +0200609 }
610 }
611 mod->parsed->parsing = 0;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100612
613 /* check name collisions - typedefs and groupings */
614 LY_CHECK_GOTO(lysp_check_typedefs(&context), error_ctx);
Radek Krejcid33273d2018-10-25 14:55:52 +0200615 }
616
Radek Krejci86d106e2018-10-18 09:53:19 +0200617 return mod;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100618
619error_ctx:
620 ly_set_rm(&ctx->list, mod, NULL);
621error:
622 lys_module_free(mod, NULL);
623 ly_set_erase(&context.tpdfs_nodes, NULL);
624 return NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +0200625}
626
Radek Krejcid14e9692018-11-01 11:00:37 +0100627API struct lys_module *
Radek Krejci86d106e2018-10-18 09:53:19 +0200628lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format)
629{
Radek Krejci3b1f9292018-11-08 10:58:35 +0100630 return lys_parse_mem_(ctx, data, format, 1, NULL, NULL, NULL);
Radek Krejci86d106e2018-10-18 09:53:19 +0200631}
632
633static void
634lys_parse_set_filename(struct ly_ctx *ctx, const char **filename, int fd)
635{
Radek Krejci65639b92018-11-27 10:51:37 +0100636 char path[PATH_MAX];
Radek Krejci86d106e2018-10-18 09:53:19 +0200637
638#ifdef __APPLE__
639 if (fcntl(fd, F_GETPATH, path) != -1) {
640 *filename = lydict_insert(ctx, path, 0);
641 }
642#else
Radek Krejci65639b92018-11-27 10:51:37 +0100643 int len;
644 char proc_path[32];
645
Radek Krejci86d106e2018-10-18 09:53:19 +0200646 /* get URI if there is /proc */
647 sprintf(proc_path, "/proc/self/fd/%d", fd);
648 if ((len = readlink(proc_path, path, PATH_MAX - 1)) > 0) {
649 *filename = lydict_insert(ctx, path, len);
650 }
651#endif
652}
653
Radek Krejcid33273d2018-10-25 14:55:52 +0200654struct lys_module *
Radek Krejci3b1f9292018-11-08 10:58:35 +0100655lys_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 +0100656 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, void *data), void *check_data)
Radek Krejci86d106e2018-10-18 09:53:19 +0200657{
Radek Krejcid33273d2018-10-25 14:55:52 +0200658 struct lys_module *mod;
Radek Krejci86d106e2018-10-18 09:53:19 +0200659 size_t length;
660 char *addr;
661
662 LY_CHECK_ARG_RET(ctx, ctx, NULL);
663 if (fd < 0) {
664 LOGARG(ctx, fd);
665 return NULL;
666 }
667
668 LY_CHECK_RET(ly_mmap(ctx, fd, &length, (void **)&addr), NULL);
669 if (!addr) {
670 LOGERR(ctx, LY_EINVAL, "Empty schema file.");
671 return NULL;
672 }
673
Radek Krejci3b1f9292018-11-08 10:58:35 +0100674 mod = lys_parse_mem_(ctx, addr, format, implement, main_ctx, custom_check, check_data);
Radek Krejci86d106e2018-10-18 09:53:19 +0200675 ly_munmap(addr, length);
676
677 if (mod && !mod->parsed->filepath) {
678 lys_parse_set_filename(ctx, &mod->parsed->filepath, fd);
679 }
680
681 return mod;
682}
683
Radek Krejcid14e9692018-11-01 11:00:37 +0100684API struct lys_module *
Radek Krejci86d106e2018-10-18 09:53:19 +0200685lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format)
686{
Radek Krejci3b1f9292018-11-08 10:58:35 +0100687 return lys_parse_fd_(ctx, fd, format, 1, NULL, NULL, NULL);
Radek Krejci86d106e2018-10-18 09:53:19 +0200688}
689
Radek Krejcid33273d2018-10-25 14:55:52 +0200690struct lys_module *
Radek Krejci3b1f9292018-11-08 10:58:35 +0100691lys_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 +0100692 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, void *data), void *check_data)
Radek Krejci86d106e2018-10-18 09:53:19 +0200693{
694 int fd;
Radek Krejcid33273d2018-10-25 14:55:52 +0200695 struct lys_module *mod;
Radek Krejci86d106e2018-10-18 09:53:19 +0200696 const char *rev, *dot, *filename;
697 size_t len;
698
699 LY_CHECK_ARG_RET(ctx, ctx, path, NULL);
700
701 fd = open(path, O_RDONLY);
702 LY_CHECK_ERR_RET(fd == -1, LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)), NULL);
703
Radek Krejci3b1f9292018-11-08 10:58:35 +0100704 mod = lys_parse_fd_(ctx, fd, format, implement, main_ctx, custom_check, check_data);
Radek Krejci86d106e2018-10-18 09:53:19 +0200705 close(fd);
706 LY_CHECK_RET(!mod, NULL);
707
708 /* check that name and revision match filename */
709 filename = strrchr(path, '/');
710 if (!filename) {
711 filename = path;
712 } else {
713 filename++;
714 }
715 rev = strchr(filename, '@');
716 dot = strrchr(filename, '.');
717
718 /* name */
719 len = strlen(mod->parsed->name);
720 if (strncmp(filename, mod->parsed->name, len) ||
721 ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) {
722 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->parsed->name);
723 }
724 if (rev) {
725 len = dot - ++rev;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200726 if (!mod->parsed->revs || len != 10 || strncmp(mod->parsed->revs[0].date, rev, len)) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200727 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Radek Krejcib7db73a2018-10-24 14:18:40 +0200728 mod->parsed->revs ? mod->parsed->revs[0].date : "none");
Radek Krejci86d106e2018-10-18 09:53:19 +0200729 }
730 }
731
732 if (!mod->parsed->filepath) {
733 /* store URI */
734 char rpath[PATH_MAX];
735 if (realpath(path, rpath) != NULL) {
736 mod->parsed->filepath = lydict_insert(ctx, rpath, 0);
737 } else {
738 mod->parsed->filepath = lydict_insert(ctx, path, 0);
739 }
740 }
741
742 return mod;
743}
744
Radek Krejcid14e9692018-11-01 11:00:37 +0100745API struct lys_module *
Radek Krejcid33273d2018-10-25 14:55:52 +0200746lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format)
747{
Radek Krejci3b1f9292018-11-08 10:58:35 +0100748 return lys_parse_path_(ctx, path, format, 1, NULL, NULL, NULL);
Radek Krejcid33273d2018-10-25 14:55:52 +0200749}
750
751API LY_ERR
752lys_search_localfile(const char * const *searchpaths, int cwd, const char *name, const char *revision,
753 char **localfile, LYS_INFORMAT *format)
754{
755 size_t len, flen, match_len = 0, dir_len;
756 int i, implicit_cwd = 0, ret = EXIT_FAILURE;
757 char *wd, *wn = NULL;
758 DIR *dir = NULL;
759 struct dirent *file;
760 char *match_name = NULL;
761 LYS_INFORMAT format_aux, match_format = 0;
762 struct ly_set *dirs;
763 struct stat st;
764
765 LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL);
766
767 /* start to fill the dir fifo with the context's search path (if set)
768 * and the current working directory */
769 dirs = ly_set_new();
770 if (!dirs) {
771 LOGMEM(NULL);
772 return EXIT_FAILURE;
773 }
774
775 len = strlen(name);
776 if (cwd) {
777 wd = get_current_dir_name();
778 if (!wd) {
779 LOGMEM(NULL);
780 goto cleanup;
781 } else {
782 /* add implicit current working directory (./) to be searched,
783 * this directory is not searched recursively */
784 if (ly_set_add(dirs, wd, 0) == -1) {
785 goto cleanup;
786 }
787 implicit_cwd = 1;
788 }
789 }
790 if (searchpaths) {
791 for (i = 0; searchpaths[i]; i++) {
792 /* check for duplicities with the implicit current working directory */
793 if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) {
794 implicit_cwd = 0;
795 continue;
796 }
797 wd = strdup(searchpaths[i]);
798 if (!wd) {
799 LOGMEM(NULL);
800 goto cleanup;
801 } else if (ly_set_add(dirs, wd, 0) == -1) {
802 goto cleanup;
803 }
804 }
805 }
806 wd = NULL;
807
808 /* start searching */
809 while (dirs->count) {
810 free(wd);
811 free(wn); wn = NULL;
812
813 dirs->count--;
814 wd = (char *)dirs->objs[dirs->count];
815 dirs->objs[dirs->count] = NULL;
816 LOGVRB("Searching for \"%s\" in %s.", name, wd);
817
818 if (dir) {
819 closedir(dir);
820 }
821 dir = opendir(wd);
822 dir_len = strlen(wd);
823 if (!dir) {
824 LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno));
825 } else {
826 while ((file = readdir(dir))) {
827 if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) {
828 /* skip . and .. */
829 continue;
830 }
831 free(wn);
832 if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) {
833 LOGMEM(NULL);
834 goto cleanup;
835 }
836 if (stat(wn, &st) == -1) {
837 LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)",
838 file->d_name, wd, strerror(errno));
839 continue;
840 }
841 if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) {
842 /* we have another subdirectory in searchpath to explore,
843 * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */
844 if (ly_set_add(dirs, wn, 0) == -1) {
845 goto cleanup;
846 }
847 /* continue with the next item in current directory */
848 wn = NULL;
849 continue;
850 } else if (!S_ISREG(st.st_mode)) {
851 /* not a regular file (note that we see the target of symlinks instead of symlinks */
852 continue;
853 }
854
855 /* here we know that the item is a file which can contain a module */
856 if (strncmp(name, file->d_name, len) ||
857 (file->d_name[len] != '.' && file->d_name[len] != '@')) {
858 /* different filename than the module we search for */
859 continue;
860 }
861
862 /* get type according to filename suffix */
863 flen = strlen(file->d_name);
864 if (!strcmp(&file->d_name[flen - 4], ".yin")) {
865 format_aux = LYS_IN_YIN;
866 } else if (!strcmp(&file->d_name[flen - 5], ".yang")) {
867 format_aux = LYS_IN_YANG;
868 } else {
869 /* not supportde suffix/file format */
870 continue;
871 }
872
873 if (revision) {
874 /* we look for the specific revision, try to get it from the filename */
875 if (file->d_name[len] == '@') {
876 /* check revision from the filename */
877 if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) {
878 /* another revision */
879 continue;
880 } else {
881 /* exact revision */
882 free(match_name);
883 match_name = wn;
884 wn = NULL;
885 match_len = dir_len + 1 + len;
886 match_format = format_aux;
887 goto success;
888 }
889 } else {
890 /* continue trying to find exact revision match, use this only if not found */
891 free(match_name);
892 match_name = wn;
893 wn = NULL;
894 match_len = dir_len + 1 +len;
895 match_format = format_aux;
896 continue;
897 }
898 } else {
899 /* remember the revision and try to find the newest one */
900 if (match_name) {
901 if (file->d_name[len] != '@' ||
902 lysp_check_date(NULL, &file->d_name[len + 1], flen - (format_aux == LYS_IN_YANG ? 5 : 4) - len - 1, NULL)) {
903 continue;
904 } else if (match_name[match_len] == '@' &&
905 (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) {
906 continue;
907 }
908 free(match_name);
909 }
910
911 match_name = wn;
912 wn = NULL;
913 match_len = dir_len + 1 + len;
914 match_format = format_aux;
915 continue;
916 }
917 }
918 }
919 }
920
921success:
922 (*localfile) = match_name;
923 match_name = NULL;
924 if (format) {
925 (*format) = match_format;
926 }
927 ret = EXIT_SUCCESS;
928
929cleanup:
930 free(wn);
931 free(wd);
932 if (dir) {
933 closedir(dir);
934 }
935 free(match_name);
936 ly_set_free(dirs, free);
937
938 return ret;
939}
940