blob: 9ababa35f9e5fa66137f661b304efe901baf86e5 [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 Krejci151a5b72018-10-19 14:21:44 +020017#include <ctype.h>
Radek Krejcid33273d2018-10-25 14:55:52 +020018#include <dirent.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020019#include <errno.h>
20#include <fcntl.h>
21#include <linux/limits.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26#include <unistd.h>
Radek Krejci3f5e3db2018-10-11 15:57:47 +020027
28#include "libyang.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020029#include "context.h"
Radek Krejci70853c52018-10-15 14:46:16 +020030#include "tree_schema_internal.h"
Radek Krejci4f28eda2018-11-12 11:46:16 +010031#include "xpath.h"
Radek Krejci3f5e3db2018-10-11 15:57:47 +020032
Radek Krejcia3045382018-11-22 14:30:31 +010033API const struct lysc_node *
34lys_getnext(const struct lysc_node *last, const struct lysc_node *parent, const struct lysc_module *module, int options)
35{
36 const struct lysc_node *next;
37 struct lysc_node **snode;
38
39 LY_CHECK_ARG_RET(NULL, parent || module, NULL);
40
41 if (!last) {
42 /* first call */
43
44 /* get know where to start */
45 if (parent) {
46 /* schema subtree */
47 snode = lysc_node_children(parent);
48 /* do not return anything if the augment does not have any children */
49 if (!snode || !(*snode)) {
50 return NULL;
51 }
52 next = last = *snode;
53 } else {
54 /* top level data */
55 next = last = module->data;
56 }
57 if (!next) {
58 return next;
59 } else if (!(options & LYS_GETNEXT_NOSTATECHECK)) {
60 if (!lys_is_disabled(next, 0)) {
61 return next;
62 }
63 /* continue to find next available node */
64 } else {
65 return next;
66 }
67 }
68
69 next = last->next;
70repeat:
71 if (!next) {
72 return next;
73 }
74 switch (next->nodetype) {
75 case LYS_ACTION:
76 case LYS_NOTIF:
77 case LYS_LEAF:
78 case LYS_ANYXML:
79 case LYS_ANYDATA:
80 case LYS_LIST:
81 case LYS_LEAFLIST:
82 break;
83 case LYS_CONTAINER:
84 if (!(((struct lysc_node_container *)next)->flags & LYS_PRESENCE) && (options & LYS_GETNEXT_INTONPCONT)) {
85 if (((struct lysc_node_container *)next)->child) {
86 /* go into */
87 next = ((struct lysc_node_container *)next)->child;
88 } else {
89 next = next->next;
90 }
91 goto repeat;
92 }
93 break;
94 case LYS_CHOICE:
95 if (options & LYS_GETNEXT_WITHCHOICE) {
96 return next;
97 } else if (((struct lysc_node_choice *)next)->cases) {
98 /* go into */
99 next = ((struct lysc_node_choice *)next)->cases[0].child;
100 } else {
101 next = next->next;
102 }
103 goto repeat;
104 default:
105 /* we should not be here */
106 LOGINT(last->module->compiled->ctx);
107 return NULL;
108 }
109
110 if (!(options & LYS_GETNEXT_NOSTATECHECK)) {
111 /* check if the node is disabled by if-feature */
112 if (lys_is_disabled(next, 0)) {
113 next = next->next;
114 goto repeat;
115 }
116 }
117 return next;
118}
119
120API const struct lysc_node *
121lys_child(const struct lysc_node *parent, const struct lys_module *module,
122 const char *name, size_t name_len, uint16_t nodetype, int options)
123{
124 const struct lysc_node *node = NULL;
125
126 LY_CHECK_ARG_RET(NULL, module, name, NULL);
127 if (!nodetype) {
128 nodetype = 0xffff;
129 }
130
131 while ((node = lys_getnext(node, parent, module->compiled, options))) {
132 if (!(node->nodetype & nodetype)) {
133 continue;
134 }
135 if (node->module != module) {
136 continue;
137 }
138
139 if (name_len) {
140 if (!strncmp(node->name, name, name_len) && !node->name[name_len]) {
141 return node;
142 }
143 } else {
144 if (!strcmp(node->name, name)) {
145 return node;
146 }
147 }
148 }
149 return NULL;
150}
151
Radek Krejci19a96102018-11-15 13:38:09 +0100152API int
153lysc_feature_value(const struct lysc_feature *feature)
Radek Krejci6f7feb62018-10-12 15:23:02 +0200154{
Radek Krejci19a96102018-11-15 13:38:09 +0100155 LY_CHECK_ARG_RET(NULL, feature, -1);
156 return feature->flags & LYS_FENABLED ? 1 : 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200157}
158
159static uint8_t
160iff_getop(uint8_t *list, int pos)
161{
162 uint8_t *item;
163 uint8_t mask = 3, result;
164
165 assert(pos >= 0);
166
167 item = &list[pos / 4];
168 result = (*item) & (mask << 2 * (pos % 4));
169 return result >> 2 * (pos % 4);
170}
171
Radek Krejci151a5b72018-10-19 14:21:44 +0200172static int
173lysc_iffeature_value_(const struct lysc_iffeature *iff, int *index_e, int *index_f)
174{
175 uint8_t op;
176 int a, b;
177
178 op = iff_getop(iff->expr, *index_e);
179 (*index_e)++;
180
181 switch (op) {
182 case LYS_IFF_F:
183 /* resolve feature */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200184 return lysc_feature_value(iff->features[(*index_f)++]);
Radek Krejci151a5b72018-10-19 14:21:44 +0200185 case LYS_IFF_NOT:
186 /* invert result */
187 return lysc_iffeature_value_(iff, index_e, index_f) ? 0 : 1;
188 case LYS_IFF_AND:
189 case LYS_IFF_OR:
190 a = lysc_iffeature_value_(iff, index_e, index_f);
191 b = lysc_iffeature_value_(iff, index_e, index_f);
192 if (op == LYS_IFF_AND) {
193 return a && b;
194 } else { /* LYS_IFF_OR */
195 return a || b;
196 }
197 }
198
199 return 0;
200}
201
202API int
203lysc_iffeature_value(const struct lysc_iffeature *iff)
204{
205 int index_e = 0, index_f = 0;
206
207 LY_CHECK_ARG_RET(NULL, iff, -1);
208
209 if (iff->expr) {
210 return lysc_iffeature_value_(iff, &index_e, &index_f);
211 }
212 return 0;
213}
214
Radek Krejci151a5b72018-10-19 14:21:44 +0200215/**
216 * @brief Enable/Disable the specified feature in the module.
217 *
218 * If the feature is already set to the desired value, LY_SUCCESS is returned.
219 * By changing the feature, also all the feature which depends on it via their
220 * if-feature statements are again evaluated (disabled if a if-feature statemen
221 * evaluates to false).
222 *
223 * @param[in] mod Compiled module where to set (search for) the feature.
224 * @param[in] name Name of the feature to set. Asterisk ('*') can be used to
225 * set all the features in the module.
226 * @param[in] value Desired value of the feature: 1 (enable) or 0 (disable).
227 * @return LY_ERR value.
228 */
229static LY_ERR
230lys_feature_change(const struct lysc_module *mod, const char *name, int value)
231{
232 int all = 0;
Radek Krejcica3db002018-11-01 10:31:01 +0100233 unsigned int u, changed_count, disabled_count;
Radek Krejci151a5b72018-10-19 14:21:44 +0200234 struct lysc_feature *f, **df;
235 struct lysc_iffeature *iff;
236 struct ly_set *changed;
237
238 if (!mod->features) {
239 LOGERR(mod->ctx, LY_EINVAL, "Unable to switch feature since the module \"%s\" has no features.", mod->name);
240 return LY_EINVAL;
241 }
242
243 if (!strcmp(name, "*")) {
244 /* enable all */
245 all = 1;
246 }
247 changed = ly_set_new();
Radek Krejcica3db002018-11-01 10:31:01 +0100248 changed_count = 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200249
Radek Krejcica3db002018-11-01 10:31:01 +0100250run:
251 for (disabled_count = u = 0; u < LY_ARRAY_SIZE(mod->features); ++u) {
Radek Krejci2c4e7172018-10-19 15:56:26 +0200252 f = &mod->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200253 if (all || !strcmp(f->name, name)) {
254 if ((value && (f->flags & LYS_FENABLED)) || (!value && !(f->flags & LYS_FENABLED))) {
255 if (all) {
256 /* skip already set features */
257 continue;
258 } else {
259 /* feature already set correctly */
260 ly_set_free(changed, NULL);
261 return LY_SUCCESS;
262 }
263 }
264
265 if (value) { /* enable */
266 /* check referenced features if they are enabled */
267 LY_ARRAY_FOR(f->iffeatures, struct lysc_iffeature, iff) {
268 if (!lysc_iffeature_value(iff)) {
269 if (all) {
Radek Krejcica3db002018-11-01 10:31:01 +0100270 ++disabled_count;
Radek Krejci151a5b72018-10-19 14:21:44 +0200271 goto next;
272 } else {
273 LOGERR(mod->ctx, LY_EDENIED,
274 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
275 f->name);
276 ly_set_free(changed, NULL);
277 return LY_EDENIED;
278 }
279 }
280 }
281 /* enable the feature */
282 f->flags |= LYS_FENABLED;
283 } else { /* disable */
284 /* disable the feature */
285 f->flags &= ~LYS_FENABLED;
286 }
287
288 /* remember the changed feature */
289 ly_set_add(changed, f, LY_SET_OPT_USEASLIST);
290
291 if (!all) {
292 /* stop in case changing a single feature */
293 break;
294 }
295 }
296next:
297 ;
298 }
299
300 if (!all && !changed->count) {
301 LOGERR(mod->ctx, LY_EINVAL, "Feature \"%s\" not found in module \"%s\".", name, mod->name);
302 ly_set_free(changed, NULL);
303 return LY_EINVAL;
304 }
305
Radek Krejcica3db002018-11-01 10:31:01 +0100306 if (value && all && disabled_count) {
307 if (changed_count == changed->count) {
308 /* no change in last run -> not able to enable all ... */
309 /* ... print errors */
310 for (u = 0; disabled_count && u < LY_ARRAY_SIZE(mod->features); ++u) {
311 if (!(mod->features[u].flags & LYS_FENABLED)) {
312 LOGERR(mod->ctx, LY_EDENIED,
313 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
314 mod->features[u].name);
315 --disabled_count;
316 }
317 }
318 /* ... restore the original state */
319 for (u = 0; u < changed->count; ++u) {
320 f = changed->objs[u];
321 /* re-disable the feature */
322 f->flags &= ~LYS_FENABLED;
323 }
324
325 ly_set_free(changed, NULL);
326 return LY_EDENIED;
327 } else {
328 /* we did some change in last run, try it again */
329 changed_count = changed->count;
330 goto run;
331 }
332 }
333
Radek Krejci151a5b72018-10-19 14:21:44 +0200334 /* reflect change(s) in the dependent features */
335 for (u = 0; u < changed->count; ++u) {
336 /* If a dependent feature is enabled, it can be now changed by the change (to false) of the value of
337 * its if-feature statements. The reverse logic, automatically enable feature when its feature is enabled
338 * is not done - by default, features are disabled and must be explicitely enabled. */
339 f = changed->objs[u];
340 LY_ARRAY_FOR(f->depfeatures, struct lysc_feature*, df) {
341 if (!((*df)->flags & LYS_FENABLED)) {
342 /* not enabled, nothing to do */
343 continue;
344 }
345 /* check the feature's if-features which could change by the previous change of our feature */
346 LY_ARRAY_FOR((*df)->iffeatures, struct lysc_iffeature, iff) {
347 if (!lysc_iffeature_value(iff)) {
348 /* the feature must be disabled now */
349 (*df)->flags &= ~LYS_FENABLED;
350 /* add the feature into the list of changed features */
351 ly_set_add(changed, *df, LY_SET_OPT_USEASLIST);
352 break;
353 }
354 }
355 }
356 }
357
358 ly_set_free(changed, NULL);
359 return LY_SUCCESS;
360}
361
362API LY_ERR
363lys_feature_enable(struct lys_module *module, const char *feature)
364{
365 LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, LY_EINVAL);
366
367 return lys_feature_change(module->compiled, feature, 1);
368}
369
370API LY_ERR
371lys_feature_disable(struct lys_module *module, const char *feature)
372{
373 LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, LY_EINVAL);
374
375 return lys_feature_change(module->compiled, feature, 0);
376}
377
378API int
379lys_feature_value(const struct lys_module *module, const char *feature)
380{
381 struct lysc_feature *f;
382 struct lysc_module *mod;
383 unsigned int u;
384
385 LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, -1);
386 mod = module->compiled;
387
388 /* search for the specified feature */
389 for (u = 0; u < LY_ARRAY_SIZE(mod->features); ++u) {
Radek Krejci2c4e7172018-10-19 15:56:26 +0200390 f = &mod->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200391 if (!strcmp(f->name, feature)) {
392 if (f->flags & LYS_FENABLED) {
393 return 1;
394 } else {
395 return 0;
396 }
397 }
398 }
399
400 /* feature definition not found */
401 return -1;
402}
403
Radek Krejcia3045382018-11-22 14:30:31 +0100404API const struct lysc_iffeature *
405lys_is_disabled(const struct lysc_node *node, int recursive)
406{
407 unsigned int u;
408 struct lysc_iffeature **iff;
409
410 LY_CHECK_ARG_RET(NULL, node, NULL);
411
412 while(node) {
413 if (node->nodetype & LYS_CHOICE) {
414 return NULL;
415 }
416
417 iff = lysc_node_iff(node);
418 if (iff && *iff) {
419 /* check local if-features */
420 LY_ARRAY_FOR(*iff, u) {
421 if (!lysc_iffeature_value(&(*iff)[u])) {
422 return &(*iff)[u];
423 }
424 }
425 }
426
427 if (!recursive) {
428 return NULL;
429 }
430
431 /* go through parents */
432 node = node->parent;
433 }
434 return NULL;
435}
436
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200437static void
Radek Krejci086c7132018-10-26 15:29:04 +0200438lys_latest_switch(struct lys_module *old, struct lysp_module *new)
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200439{
Radek Krejci086c7132018-10-26 15:29:04 +0200440 if (old->parsed) {
441 new->latest_revision = old->parsed->latest_revision;
442 old->parsed->latest_revision = 0;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200443 }
Radek Krejci086c7132018-10-26 15:29:04 +0200444 if (old->compiled) {
445 new->latest_revision = old->parsed->latest_revision;
446 old->compiled->latest_revision = 0;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200447 }
448}
449
Radek Krejcid33273d2018-10-25 14:55:52 +0200450struct lys_module *
Radek Krejci3b1f9292018-11-08 10:58:35 +0100451lys_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 +0100452 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, void *data), void *check_data)
Radek Krejci86d106e2018-10-18 09:53:19 +0200453{
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100454 struct lys_module *mod = NULL, *latest, *mod_dup;
Radek Krejcid33273d2018-10-25 14:55:52 +0200455 struct lysp_module *latest_p;
Radek Krejci086c7132018-10-26 15:29:04 +0200456 struct lysp_import *imp;
457 struct lysp_include *inc;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100458 LY_ERR ret = LY_EINVAL;
Radek Krejci086c7132018-10-26 15:29:04 +0200459 unsigned int u, i;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100460 struct ly_parser_ctx context = {0};
Radek Krejci86d106e2018-10-18 09:53:19 +0200461
462 LY_CHECK_ARG_RET(ctx, ctx, data, NULL);
463
Radek Krejcibbe09a92018-11-08 09:36:54 +0100464 context.ctx = ctx;
465 context.line = 1;
466
Radek Krejci3b1f9292018-11-08 10:58:35 +0100467 if (main_ctx) {
468 /* map the typedefs and groupings list from main context to the submodule's context */
469 memcpy(&context.tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
470 memcpy(&context.grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
471 }
472
Radek Krejci86d106e2018-10-18 09:53:19 +0200473 mod = calloc(1, sizeof *mod);
474 LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), NULL);
475
476 switch (format) {
477 case LYS_IN_YIN:
478 /* TODO not yet supported
479 mod = yin_read_module(ctx, data, revision, implement);
480 */
481 break;
482 case LYS_IN_YANG:
Radek Krejcibbe09a92018-11-08 09:36:54 +0100483 ret = yang_parse(&context, data, &mod->parsed);
Radek Krejci86d106e2018-10-18 09:53:19 +0200484 break;
485 default:
486 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
487 break;
488 }
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100489 LY_CHECK_ERR_RET(ret, free(mod), NULL);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200490
491 /* make sure that the newest revision is at position 0 */
492 lysp_sort_revisions(mod->parsed->revs);
Radek Krejci86d106e2018-10-18 09:53:19 +0200493
494 if (implement) {
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200495 /* mark the loaded module implemented */
Radek Krejcib7db73a2018-10-24 14:18:40 +0200496 if (ly_ctx_get_module_implemented(ctx, mod->parsed->name)) {
497 LOGERR(ctx, LY_EDENIED, "Module \"%s\" is already implemented in the context.", mod->parsed->name);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100498 goto error;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200499 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200500 mod->parsed->implemented = 1;
501 }
502
Radek Krejci9ed7a192018-10-31 16:23:51 +0100503 if (custom_check) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100504 LY_CHECK_GOTO(custom_check(ctx, mod->parsed, check_data), error);
Radek Krejci86d106e2018-10-18 09:53:19 +0200505 }
506
Radek Krejcid33273d2018-10-25 14:55:52 +0200507 if (mod->parsed->submodule) { /* submodule */
Radek Krejci3b1f9292018-11-08 10:58:35 +0100508 if (!main_ctx) {
509 LOGERR(ctx, LY_EDENIED, "Input data contains submodule \"%s\" which cannot be parsed directly without its main module.",
510 mod->parsed->name);
511 goto error;
512 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200513 /* decide the latest revision */
514 latest_p = ly_ctx_get_submodule(ctx, mod->parsed->belongsto, mod->parsed->name, NULL);
515 if (latest_p) {
516 if (mod->parsed->revs) {
517 if (!latest_p->revs) {
518 /* latest has no revision, so mod is anyway newer */
Radek Krejci9ed7a192018-10-31 16:23:51 +0100519 mod->parsed->latest_revision = latest_p->latest_revision;
Radek Krejcid33273d2018-10-25 14:55:52 +0200520 latest_p->latest_revision = 0;
521 } else {
522 if (strcmp(mod->parsed->revs[0].date, latest_p->revs[0].date) > 0) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100523 mod->parsed->latest_revision = latest_p->latest_revision;
Radek Krejcid33273d2018-10-25 14:55:52 +0200524 latest_p->latest_revision = 0;
525 }
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200526 }
527 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200528 } else {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100529 mod->parsed->latest_revision = 1;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200530 }
Radek Krejci3b1f9292018-11-08 10:58:35 +0100531 /* remap possibly changed and reallocated typedefs and groupings list back to the main context */
532 memcpy(&main_ctx->tpdfs_nodes, &context.tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
533 memcpy(&main_ctx->grps_nodes, &context.grps_nodes, sizeof main_ctx->grps_nodes);
Radek Krejcid33273d2018-10-25 14:55:52 +0200534 } else { /* module */
535 /* check for duplicity in the context */
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100536 mod_dup = (struct lys_module*)ly_ctx_get_module(ctx, mod->parsed->name, mod->parsed->revs ? mod->parsed->revs[0].date : NULL);
537 if (mod_dup) {
538 if (mod_dup->parsed) {
539 /* error */
540 if (mod->parsed->revs) {
541 LOGERR(ctx, LY_EEXIST, "Module \"%s\" of revision \"%s\" is already present in the context.",
542 mod->parsed->name, mod->parsed->revs[0].date);
543 } else {
544 LOGERR(ctx, LY_EEXIST, "Module \"%s\" with no revision is already present in the context.",
545 mod->parsed->name);
546 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100547 goto error;
Radek Krejcid33273d2018-10-25 14:55:52 +0200548 } else {
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100549 /* add the parsed data to the currently compiled-only module in the context */
550 mod_dup->parsed = mod->parsed;
551 free(mod);
552 mod = mod_dup;
553 goto finish_parsing;
Radek Krejcid33273d2018-10-25 14:55:52 +0200554 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200555 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200556
557#if 0
558 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
559 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
560 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
561 * the anotation definitions available in the internal schema structure. There is another hack in schema
562 * printers to do not print this internally added annotation. */
563 if (mod && ly_strequal(mod->name, "ietf-netconf", 0)) {
564 if (lyp_add_ietf_netconf_annotations(mod)) {
565 lys_free(mod, NULL, 1, 1);
566 return NULL;
567 }
568 }
569#endif
570
Radek Krejcid33273d2018-10-25 14:55:52 +0200571 /* decide the latest revision */
572 latest = (struct lys_module*)ly_ctx_get_module_latest(ctx, mod->parsed->name);
573 if (latest) {
574 if (mod->parsed->revs) {
Radek Krejcif8f882a2018-10-31 14:51:15 +0100575 if ((latest->parsed && !latest->parsed->revs) || (!latest->parsed && !latest->compiled->revision)) {
Radek Krejcid33273d2018-10-25 14:55:52 +0200576 /* latest has no revision, so mod is anyway newer */
Radek Krejci086c7132018-10-26 15:29:04 +0200577 lys_latest_switch(latest, mod->parsed);
Radek Krejcid33273d2018-10-25 14:55:52 +0200578 } else {
Radek Krejcif8f882a2018-10-31 14:51:15 +0100579 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 +0200580 lys_latest_switch(latest, mod->parsed);
Radek Krejcid33273d2018-10-25 14:55:52 +0200581 }
582 }
583 }
584 } else {
585 mod->parsed->latest_revision = 1;
586 }
587
588 /* add into context */
589 ly_set_add(&ctx->list, mod, LY_SET_OPT_USEASLIST);
590
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100591finish_parsing:
Radek Krejcibbe09a92018-11-08 09:36:54 +0100592 /* resolve imports */
Radek Krejci086c7132018-10-26 15:29:04 +0200593 mod->parsed->parsing = 1;
594 LY_ARRAY_FOR(mod->parsed->imports, u) {
595 imp = &mod->parsed->imports[u];
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100596 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 +0100597 goto error_ctx;
Radek Krejci086c7132018-10-26 15:29:04 +0200598 }
599 /* check for importing the same module twice */
600 for (i = 0; i < u; ++i) {
601 if (imp->module == mod->parsed->imports[i].module) {
602 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 +0100603 goto error_ctx;
Radek Krejci086c7132018-10-26 15:29:04 +0200604 }
605 }
606 }
607 LY_ARRAY_FOR(mod->parsed->includes, u) {
608 inc = &mod->parsed->includes[u];
Radek Krejci3b1f9292018-11-08 10:58:35 +0100609 if (!inc->submodule && lysp_load_submodule(&context, mod->parsed, inc)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100610 goto error_ctx;
Radek Krejci086c7132018-10-26 15:29:04 +0200611 }
612 }
613 mod->parsed->parsing = 0;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100614
615 /* check name collisions - typedefs and groupings */
616 LY_CHECK_GOTO(lysp_check_typedefs(&context), error_ctx);
Radek Krejcid33273d2018-10-25 14:55:52 +0200617 }
618
Radek Krejci86d106e2018-10-18 09:53:19 +0200619 return mod;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100620
621error_ctx:
622 ly_set_rm(&ctx->list, mod, NULL);
623error:
624 lys_module_free(mod, NULL);
625 ly_set_erase(&context.tpdfs_nodes, NULL);
626 return NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +0200627}
628
Radek Krejcid14e9692018-11-01 11:00:37 +0100629API struct lys_module *
Radek Krejci86d106e2018-10-18 09:53:19 +0200630lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format)
631{
Radek Krejci3b1f9292018-11-08 10:58:35 +0100632 return lys_parse_mem_(ctx, data, format, 1, NULL, NULL, NULL);
Radek Krejci86d106e2018-10-18 09:53:19 +0200633}
634
635static void
636lys_parse_set_filename(struct ly_ctx *ctx, const char **filename, int fd)
637{
638#ifdef __APPLE__
639 char path[MAXPATHLEN];
640#else
641 int len;
642 char path[PATH_MAX], proc_path[32];
643#endif
644
645#ifdef __APPLE__
646 if (fcntl(fd, F_GETPATH, path) != -1) {
647 *filename = lydict_insert(ctx, path, 0);
648 }
649#else
650 /* get URI if there is /proc */
651 sprintf(proc_path, "/proc/self/fd/%d", fd);
652 if ((len = readlink(proc_path, path, PATH_MAX - 1)) > 0) {
653 *filename = lydict_insert(ctx, path, len);
654 }
655#endif
656}
657
Radek Krejcid33273d2018-10-25 14:55:52 +0200658struct lys_module *
Radek Krejci3b1f9292018-11-08 10:58:35 +0100659lys_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 +0100660 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, void *data), void *check_data)
Radek Krejci86d106e2018-10-18 09:53:19 +0200661{
Radek Krejcid33273d2018-10-25 14:55:52 +0200662 struct lys_module *mod;
Radek Krejci86d106e2018-10-18 09:53:19 +0200663 size_t length;
664 char *addr;
665
666 LY_CHECK_ARG_RET(ctx, ctx, NULL);
667 if (fd < 0) {
668 LOGARG(ctx, fd);
669 return NULL;
670 }
671
672 LY_CHECK_RET(ly_mmap(ctx, fd, &length, (void **)&addr), NULL);
673 if (!addr) {
674 LOGERR(ctx, LY_EINVAL, "Empty schema file.");
675 return NULL;
676 }
677
Radek Krejci3b1f9292018-11-08 10:58:35 +0100678 mod = lys_parse_mem_(ctx, addr, format, implement, main_ctx, custom_check, check_data);
Radek Krejci86d106e2018-10-18 09:53:19 +0200679 ly_munmap(addr, length);
680
681 if (mod && !mod->parsed->filepath) {
682 lys_parse_set_filename(ctx, &mod->parsed->filepath, fd);
683 }
684
685 return mod;
686}
687
Radek Krejcid14e9692018-11-01 11:00:37 +0100688API struct lys_module *
Radek Krejci86d106e2018-10-18 09:53:19 +0200689lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format)
690{
Radek Krejci3b1f9292018-11-08 10:58:35 +0100691 return lys_parse_fd_(ctx, fd, format, 1, NULL, NULL, NULL);
Radek Krejci86d106e2018-10-18 09:53:19 +0200692}
693
Radek Krejcid33273d2018-10-25 14:55:52 +0200694struct lys_module *
Radek Krejci3b1f9292018-11-08 10:58:35 +0100695lys_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 +0100696 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, void *data), void *check_data)
Radek Krejci86d106e2018-10-18 09:53:19 +0200697{
698 int fd;
Radek Krejcid33273d2018-10-25 14:55:52 +0200699 struct lys_module *mod;
Radek Krejci86d106e2018-10-18 09:53:19 +0200700 const char *rev, *dot, *filename;
701 size_t len;
702
703 LY_CHECK_ARG_RET(ctx, ctx, path, NULL);
704
705 fd = open(path, O_RDONLY);
706 LY_CHECK_ERR_RET(fd == -1, LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)), NULL);
707
Radek Krejci3b1f9292018-11-08 10:58:35 +0100708 mod = lys_parse_fd_(ctx, fd, format, implement, main_ctx, custom_check, check_data);
Radek Krejci86d106e2018-10-18 09:53:19 +0200709 close(fd);
710 LY_CHECK_RET(!mod, NULL);
711
712 /* check that name and revision match filename */
713 filename = strrchr(path, '/');
714 if (!filename) {
715 filename = path;
716 } else {
717 filename++;
718 }
719 rev = strchr(filename, '@');
720 dot = strrchr(filename, '.');
721
722 /* name */
723 len = strlen(mod->parsed->name);
724 if (strncmp(filename, mod->parsed->name, len) ||
725 ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) {
726 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->parsed->name);
727 }
728 if (rev) {
729 len = dot - ++rev;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200730 if (!mod->parsed->revs || len != 10 || strncmp(mod->parsed->revs[0].date, rev, len)) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200731 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Radek Krejcib7db73a2018-10-24 14:18:40 +0200732 mod->parsed->revs ? mod->parsed->revs[0].date : "none");
Radek Krejci86d106e2018-10-18 09:53:19 +0200733 }
734 }
735
736 if (!mod->parsed->filepath) {
737 /* store URI */
738 char rpath[PATH_MAX];
739 if (realpath(path, rpath) != NULL) {
740 mod->parsed->filepath = lydict_insert(ctx, rpath, 0);
741 } else {
742 mod->parsed->filepath = lydict_insert(ctx, path, 0);
743 }
744 }
745
746 return mod;
747}
748
Radek Krejcid14e9692018-11-01 11:00:37 +0100749API struct lys_module *
Radek Krejcid33273d2018-10-25 14:55:52 +0200750lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format)
751{
Radek Krejci3b1f9292018-11-08 10:58:35 +0100752 return lys_parse_path_(ctx, path, format, 1, NULL, NULL, NULL);
Radek Krejcid33273d2018-10-25 14:55:52 +0200753}
754
755API LY_ERR
756lys_search_localfile(const char * const *searchpaths, int cwd, const char *name, const char *revision,
757 char **localfile, LYS_INFORMAT *format)
758{
759 size_t len, flen, match_len = 0, dir_len;
760 int i, implicit_cwd = 0, ret = EXIT_FAILURE;
761 char *wd, *wn = NULL;
762 DIR *dir = NULL;
763 struct dirent *file;
764 char *match_name = NULL;
765 LYS_INFORMAT format_aux, match_format = 0;
766 struct ly_set *dirs;
767 struct stat st;
768
769 LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL);
770
771 /* start to fill the dir fifo with the context's search path (if set)
772 * and the current working directory */
773 dirs = ly_set_new();
774 if (!dirs) {
775 LOGMEM(NULL);
776 return EXIT_FAILURE;
777 }
778
779 len = strlen(name);
780 if (cwd) {
781 wd = get_current_dir_name();
782 if (!wd) {
783 LOGMEM(NULL);
784 goto cleanup;
785 } else {
786 /* add implicit current working directory (./) to be searched,
787 * this directory is not searched recursively */
788 if (ly_set_add(dirs, wd, 0) == -1) {
789 goto cleanup;
790 }
791 implicit_cwd = 1;
792 }
793 }
794 if (searchpaths) {
795 for (i = 0; searchpaths[i]; i++) {
796 /* check for duplicities with the implicit current working directory */
797 if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) {
798 implicit_cwd = 0;
799 continue;
800 }
801 wd = strdup(searchpaths[i]);
802 if (!wd) {
803 LOGMEM(NULL);
804 goto cleanup;
805 } else if (ly_set_add(dirs, wd, 0) == -1) {
806 goto cleanup;
807 }
808 }
809 }
810 wd = NULL;
811
812 /* start searching */
813 while (dirs->count) {
814 free(wd);
815 free(wn); wn = NULL;
816
817 dirs->count--;
818 wd = (char *)dirs->objs[dirs->count];
819 dirs->objs[dirs->count] = NULL;
820 LOGVRB("Searching for \"%s\" in %s.", name, wd);
821
822 if (dir) {
823 closedir(dir);
824 }
825 dir = opendir(wd);
826 dir_len = strlen(wd);
827 if (!dir) {
828 LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno));
829 } else {
830 while ((file = readdir(dir))) {
831 if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) {
832 /* skip . and .. */
833 continue;
834 }
835 free(wn);
836 if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) {
837 LOGMEM(NULL);
838 goto cleanup;
839 }
840 if (stat(wn, &st) == -1) {
841 LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)",
842 file->d_name, wd, strerror(errno));
843 continue;
844 }
845 if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) {
846 /* we have another subdirectory in searchpath to explore,
847 * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */
848 if (ly_set_add(dirs, wn, 0) == -1) {
849 goto cleanup;
850 }
851 /* continue with the next item in current directory */
852 wn = NULL;
853 continue;
854 } else if (!S_ISREG(st.st_mode)) {
855 /* not a regular file (note that we see the target of symlinks instead of symlinks */
856 continue;
857 }
858
859 /* here we know that the item is a file which can contain a module */
860 if (strncmp(name, file->d_name, len) ||
861 (file->d_name[len] != '.' && file->d_name[len] != '@')) {
862 /* different filename than the module we search for */
863 continue;
864 }
865
866 /* get type according to filename suffix */
867 flen = strlen(file->d_name);
868 if (!strcmp(&file->d_name[flen - 4], ".yin")) {
869 format_aux = LYS_IN_YIN;
870 } else if (!strcmp(&file->d_name[flen - 5], ".yang")) {
871 format_aux = LYS_IN_YANG;
872 } else {
873 /* not supportde suffix/file format */
874 continue;
875 }
876
877 if (revision) {
878 /* we look for the specific revision, try to get it from the filename */
879 if (file->d_name[len] == '@') {
880 /* check revision from the filename */
881 if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) {
882 /* another revision */
883 continue;
884 } else {
885 /* exact revision */
886 free(match_name);
887 match_name = wn;
888 wn = NULL;
889 match_len = dir_len + 1 + len;
890 match_format = format_aux;
891 goto success;
892 }
893 } else {
894 /* continue trying to find exact revision match, use this only if not found */
895 free(match_name);
896 match_name = wn;
897 wn = NULL;
898 match_len = dir_len + 1 +len;
899 match_format = format_aux;
900 continue;
901 }
902 } else {
903 /* remember the revision and try to find the newest one */
904 if (match_name) {
905 if (file->d_name[len] != '@' ||
906 lysp_check_date(NULL, &file->d_name[len + 1], flen - (format_aux == LYS_IN_YANG ? 5 : 4) - len - 1, NULL)) {
907 continue;
908 } else if (match_name[match_len] == '@' &&
909 (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) {
910 continue;
911 }
912 free(match_name);
913 }
914
915 match_name = wn;
916 wn = NULL;
917 match_len = dir_len + 1 + len;
918 match_format = format_aux;
919 continue;
920 }
921 }
922 }
923 }
924
925success:
926 (*localfile) = match_name;
927 match_name = NULL;
928 if (format) {
929 (*format) = match_format;
930 }
931 ret = EXIT_SUCCESS;
932
933cleanup:
934 free(wn);
935 free(wd);
936 if (dir) {
937 closedir(dir);
938 }
939 free(match_name);
940 ly_set_free(dirs, free);
941
942 return ret;
943}
944