blob: cacadf724a173527744ae6514fa675e4a08245d6 [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 Krejci19a96102018-11-15 13:38:09 +010033API int
34lysc_feature_value(const struct lysc_feature *feature)
Radek Krejci6f7feb62018-10-12 15:23:02 +020035{
Radek Krejci19a96102018-11-15 13:38:09 +010036 LY_CHECK_ARG_RET(NULL, feature, -1);
37 return feature->flags & LYS_FENABLED ? 1 : 0;
Radek Krejci151a5b72018-10-19 14:21:44 +020038}
39
40static uint8_t
41iff_getop(uint8_t *list, int pos)
42{
43 uint8_t *item;
44 uint8_t mask = 3, result;
45
46 assert(pos >= 0);
47
48 item = &list[pos / 4];
49 result = (*item) & (mask << 2 * (pos % 4));
50 return result >> 2 * (pos % 4);
51}
52
Radek Krejci151a5b72018-10-19 14:21:44 +020053static int
54lysc_iffeature_value_(const struct lysc_iffeature *iff, int *index_e, int *index_f)
55{
56 uint8_t op;
57 int a, b;
58
59 op = iff_getop(iff->expr, *index_e);
60 (*index_e)++;
61
62 switch (op) {
63 case LYS_IFF_F:
64 /* resolve feature */
Radek Krejci2c4e7172018-10-19 15:56:26 +020065 return lysc_feature_value(iff->features[(*index_f)++]);
Radek Krejci151a5b72018-10-19 14:21:44 +020066 case LYS_IFF_NOT:
67 /* invert result */
68 return lysc_iffeature_value_(iff, index_e, index_f) ? 0 : 1;
69 case LYS_IFF_AND:
70 case LYS_IFF_OR:
71 a = lysc_iffeature_value_(iff, index_e, index_f);
72 b = lysc_iffeature_value_(iff, index_e, index_f);
73 if (op == LYS_IFF_AND) {
74 return a && b;
75 } else { /* LYS_IFF_OR */
76 return a || b;
77 }
78 }
79
80 return 0;
81}
82
83API int
84lysc_iffeature_value(const struct lysc_iffeature *iff)
85{
86 int index_e = 0, index_f = 0;
87
88 LY_CHECK_ARG_RET(NULL, iff, -1);
89
90 if (iff->expr) {
91 return lysc_iffeature_value_(iff, &index_e, &index_f);
92 }
93 return 0;
94}
95
Radek Krejci151a5b72018-10-19 14:21:44 +020096/**
97 * @brief Enable/Disable the specified feature in the module.
98 *
99 * If the feature is already set to the desired value, LY_SUCCESS is returned.
100 * By changing the feature, also all the feature which depends on it via their
101 * if-feature statements are again evaluated (disabled if a if-feature statemen
102 * evaluates to false).
103 *
104 * @param[in] mod Compiled module where to set (search for) the feature.
105 * @param[in] name Name of the feature to set. Asterisk ('*') can be used to
106 * set all the features in the module.
107 * @param[in] value Desired value of the feature: 1 (enable) or 0 (disable).
108 * @return LY_ERR value.
109 */
110static LY_ERR
111lys_feature_change(const struct lysc_module *mod, const char *name, int value)
112{
113 int all = 0;
Radek Krejcica3db002018-11-01 10:31:01 +0100114 unsigned int u, changed_count, disabled_count;
Radek Krejci151a5b72018-10-19 14:21:44 +0200115 struct lysc_feature *f, **df;
116 struct lysc_iffeature *iff;
117 struct ly_set *changed;
118
119 if (!mod->features) {
120 LOGERR(mod->ctx, LY_EINVAL, "Unable to switch feature since the module \"%s\" has no features.", mod->name);
121 return LY_EINVAL;
122 }
123
124 if (!strcmp(name, "*")) {
125 /* enable all */
126 all = 1;
127 }
128 changed = ly_set_new();
Radek Krejcica3db002018-11-01 10:31:01 +0100129 changed_count = 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200130
Radek Krejcica3db002018-11-01 10:31:01 +0100131run:
132 for (disabled_count = u = 0; u < LY_ARRAY_SIZE(mod->features); ++u) {
Radek Krejci2c4e7172018-10-19 15:56:26 +0200133 f = &mod->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200134 if (all || !strcmp(f->name, name)) {
135 if ((value && (f->flags & LYS_FENABLED)) || (!value && !(f->flags & LYS_FENABLED))) {
136 if (all) {
137 /* skip already set features */
138 continue;
139 } else {
140 /* feature already set correctly */
141 ly_set_free(changed, NULL);
142 return LY_SUCCESS;
143 }
144 }
145
146 if (value) { /* enable */
147 /* check referenced features if they are enabled */
148 LY_ARRAY_FOR(f->iffeatures, struct lysc_iffeature, iff) {
149 if (!lysc_iffeature_value(iff)) {
150 if (all) {
Radek Krejcica3db002018-11-01 10:31:01 +0100151 ++disabled_count;
Radek Krejci151a5b72018-10-19 14:21:44 +0200152 goto next;
153 } else {
154 LOGERR(mod->ctx, LY_EDENIED,
155 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
156 f->name);
157 ly_set_free(changed, NULL);
158 return LY_EDENIED;
159 }
160 }
161 }
162 /* enable the feature */
163 f->flags |= LYS_FENABLED;
164 } else { /* disable */
165 /* disable the feature */
166 f->flags &= ~LYS_FENABLED;
167 }
168
169 /* remember the changed feature */
170 ly_set_add(changed, f, LY_SET_OPT_USEASLIST);
171
172 if (!all) {
173 /* stop in case changing a single feature */
174 break;
175 }
176 }
177next:
178 ;
179 }
180
181 if (!all && !changed->count) {
182 LOGERR(mod->ctx, LY_EINVAL, "Feature \"%s\" not found in module \"%s\".", name, mod->name);
183 ly_set_free(changed, NULL);
184 return LY_EINVAL;
185 }
186
Radek Krejcica3db002018-11-01 10:31:01 +0100187 if (value && all && disabled_count) {
188 if (changed_count == changed->count) {
189 /* no change in last run -> not able to enable all ... */
190 /* ... print errors */
191 for (u = 0; disabled_count && u < LY_ARRAY_SIZE(mod->features); ++u) {
192 if (!(mod->features[u].flags & LYS_FENABLED)) {
193 LOGERR(mod->ctx, LY_EDENIED,
194 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
195 mod->features[u].name);
196 --disabled_count;
197 }
198 }
199 /* ... restore the original state */
200 for (u = 0; u < changed->count; ++u) {
201 f = changed->objs[u];
202 /* re-disable the feature */
203 f->flags &= ~LYS_FENABLED;
204 }
205
206 ly_set_free(changed, NULL);
207 return LY_EDENIED;
208 } else {
209 /* we did some change in last run, try it again */
210 changed_count = changed->count;
211 goto run;
212 }
213 }
214
Radek Krejci151a5b72018-10-19 14:21:44 +0200215 /* reflect change(s) in the dependent features */
216 for (u = 0; u < changed->count; ++u) {
217 /* If a dependent feature is enabled, it can be now changed by the change (to false) of the value of
218 * its if-feature statements. The reverse logic, automatically enable feature when its feature is enabled
219 * is not done - by default, features are disabled and must be explicitely enabled. */
220 f = changed->objs[u];
221 LY_ARRAY_FOR(f->depfeatures, struct lysc_feature*, df) {
222 if (!((*df)->flags & LYS_FENABLED)) {
223 /* not enabled, nothing to do */
224 continue;
225 }
226 /* check the feature's if-features which could change by the previous change of our feature */
227 LY_ARRAY_FOR((*df)->iffeatures, struct lysc_iffeature, iff) {
228 if (!lysc_iffeature_value(iff)) {
229 /* the feature must be disabled now */
230 (*df)->flags &= ~LYS_FENABLED;
231 /* add the feature into the list of changed features */
232 ly_set_add(changed, *df, LY_SET_OPT_USEASLIST);
233 break;
234 }
235 }
236 }
237 }
238
239 ly_set_free(changed, NULL);
240 return LY_SUCCESS;
241}
242
243API LY_ERR
244lys_feature_enable(struct lys_module *module, const char *feature)
245{
246 LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, LY_EINVAL);
247
248 return lys_feature_change(module->compiled, feature, 1);
249}
250
251API LY_ERR
252lys_feature_disable(struct lys_module *module, const char *feature)
253{
254 LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, LY_EINVAL);
255
256 return lys_feature_change(module->compiled, feature, 0);
257}
258
259API int
260lys_feature_value(const struct lys_module *module, const char *feature)
261{
262 struct lysc_feature *f;
263 struct lysc_module *mod;
264 unsigned int u;
265
266 LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, -1);
267 mod = module->compiled;
268
269 /* search for the specified feature */
270 for (u = 0; u < LY_ARRAY_SIZE(mod->features); ++u) {
Radek Krejci2c4e7172018-10-19 15:56:26 +0200271 f = &mod->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200272 if (!strcmp(f->name, feature)) {
273 if (f->flags & LYS_FENABLED) {
274 return 1;
275 } else {
276 return 0;
277 }
278 }
279 }
280
281 /* feature definition not found */
282 return -1;
283}
284
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200285static void
Radek Krejci086c7132018-10-26 15:29:04 +0200286lys_latest_switch(struct lys_module *old, struct lysp_module *new)
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200287{
Radek Krejci086c7132018-10-26 15:29:04 +0200288 if (old->parsed) {
289 new->latest_revision = old->parsed->latest_revision;
290 old->parsed->latest_revision = 0;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200291 }
Radek Krejci086c7132018-10-26 15:29:04 +0200292 if (old->compiled) {
293 new->latest_revision = old->parsed->latest_revision;
294 old->compiled->latest_revision = 0;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200295 }
296}
297
Radek Krejcid33273d2018-10-25 14:55:52 +0200298struct lys_module *
Radek Krejci3b1f9292018-11-08 10:58:35 +0100299lys_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 +0100300 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, void *data), void *check_data)
Radek Krejci86d106e2018-10-18 09:53:19 +0200301{
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100302 struct lys_module *mod = NULL, *latest, *mod_dup;
Radek Krejcid33273d2018-10-25 14:55:52 +0200303 struct lysp_module *latest_p;
Radek Krejci086c7132018-10-26 15:29:04 +0200304 struct lysp_import *imp;
305 struct lysp_include *inc;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100306 LY_ERR ret = LY_EINVAL;
Radek Krejci086c7132018-10-26 15:29:04 +0200307 unsigned int u, i;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100308 struct ly_parser_ctx context = {0};
Radek Krejci86d106e2018-10-18 09:53:19 +0200309
310 LY_CHECK_ARG_RET(ctx, ctx, data, NULL);
311
Radek Krejcibbe09a92018-11-08 09:36:54 +0100312 context.ctx = ctx;
313 context.line = 1;
314
Radek Krejci3b1f9292018-11-08 10:58:35 +0100315 if (main_ctx) {
316 /* map the typedefs and groupings list from main context to the submodule's context */
317 memcpy(&context.tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
318 memcpy(&context.grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
319 }
320
Radek Krejci86d106e2018-10-18 09:53:19 +0200321 mod = calloc(1, sizeof *mod);
322 LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), NULL);
323
324 switch (format) {
325 case LYS_IN_YIN:
326 /* TODO not yet supported
327 mod = yin_read_module(ctx, data, revision, implement);
328 */
329 break;
330 case LYS_IN_YANG:
Radek Krejcibbe09a92018-11-08 09:36:54 +0100331 ret = yang_parse(&context, data, &mod->parsed);
Radek Krejci86d106e2018-10-18 09:53:19 +0200332 break;
333 default:
334 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
335 break;
336 }
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100337 LY_CHECK_ERR_RET(ret, free(mod), NULL);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200338
339 /* make sure that the newest revision is at position 0 */
340 lysp_sort_revisions(mod->parsed->revs);
Radek Krejci86d106e2018-10-18 09:53:19 +0200341
342 if (implement) {
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200343 /* mark the loaded module implemented */
Radek Krejcib7db73a2018-10-24 14:18:40 +0200344 if (ly_ctx_get_module_implemented(ctx, mod->parsed->name)) {
345 LOGERR(ctx, LY_EDENIED, "Module \"%s\" is already implemented in the context.", mod->parsed->name);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100346 goto error;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200347 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200348 mod->parsed->implemented = 1;
349 }
350
Radek Krejci9ed7a192018-10-31 16:23:51 +0100351 if (custom_check) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100352 LY_CHECK_GOTO(custom_check(ctx, mod->parsed, check_data), error);
Radek Krejci86d106e2018-10-18 09:53:19 +0200353 }
354
Radek Krejcid33273d2018-10-25 14:55:52 +0200355 if (mod->parsed->submodule) { /* submodule */
Radek Krejci3b1f9292018-11-08 10:58:35 +0100356 if (!main_ctx) {
357 LOGERR(ctx, LY_EDENIED, "Input data contains submodule \"%s\" which cannot be parsed directly without its main module.",
358 mod->parsed->name);
359 goto error;
360 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200361 /* decide the latest revision */
362 latest_p = ly_ctx_get_submodule(ctx, mod->parsed->belongsto, mod->parsed->name, NULL);
363 if (latest_p) {
364 if (mod->parsed->revs) {
365 if (!latest_p->revs) {
366 /* latest has no revision, so mod is anyway newer */
Radek Krejci9ed7a192018-10-31 16:23:51 +0100367 mod->parsed->latest_revision = latest_p->latest_revision;
Radek Krejcid33273d2018-10-25 14:55:52 +0200368 latest_p->latest_revision = 0;
369 } else {
370 if (strcmp(mod->parsed->revs[0].date, latest_p->revs[0].date) > 0) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100371 mod->parsed->latest_revision = latest_p->latest_revision;
Radek Krejcid33273d2018-10-25 14:55:52 +0200372 latest_p->latest_revision = 0;
373 }
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200374 }
375 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200376 } else {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100377 mod->parsed->latest_revision = 1;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200378 }
Radek Krejci3b1f9292018-11-08 10:58:35 +0100379 /* remap possibly changed and reallocated typedefs and groupings list back to the main context */
380 memcpy(&main_ctx->tpdfs_nodes, &context.tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
381 memcpy(&main_ctx->grps_nodes, &context.grps_nodes, sizeof main_ctx->grps_nodes);
Radek Krejcid33273d2018-10-25 14:55:52 +0200382 } else { /* module */
383 /* check for duplicity in the context */
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100384 mod_dup = (struct lys_module*)ly_ctx_get_module(ctx, mod->parsed->name, mod->parsed->revs ? mod->parsed->revs[0].date : NULL);
385 if (mod_dup) {
386 if (mod_dup->parsed) {
387 /* error */
388 if (mod->parsed->revs) {
389 LOGERR(ctx, LY_EEXIST, "Module \"%s\" of revision \"%s\" is already present in the context.",
390 mod->parsed->name, mod->parsed->revs[0].date);
391 } else {
392 LOGERR(ctx, LY_EEXIST, "Module \"%s\" with no revision is already present in the context.",
393 mod->parsed->name);
394 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100395 goto error;
Radek Krejcid33273d2018-10-25 14:55:52 +0200396 } else {
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100397 /* add the parsed data to the currently compiled-only module in the context */
398 mod_dup->parsed = mod->parsed;
399 free(mod);
400 mod = mod_dup;
401 goto finish_parsing;
Radek Krejcid33273d2018-10-25 14:55:52 +0200402 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200403 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200404
405#if 0
406 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
407 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
408 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
409 * the anotation definitions available in the internal schema structure. There is another hack in schema
410 * printers to do not print this internally added annotation. */
411 if (mod && ly_strequal(mod->name, "ietf-netconf", 0)) {
412 if (lyp_add_ietf_netconf_annotations(mod)) {
413 lys_free(mod, NULL, 1, 1);
414 return NULL;
415 }
416 }
417#endif
418
Radek Krejcid33273d2018-10-25 14:55:52 +0200419 /* decide the latest revision */
420 latest = (struct lys_module*)ly_ctx_get_module_latest(ctx, mod->parsed->name);
421 if (latest) {
422 if (mod->parsed->revs) {
Radek Krejcif8f882a2018-10-31 14:51:15 +0100423 if ((latest->parsed && !latest->parsed->revs) || (!latest->parsed && !latest->compiled->revision)) {
Radek Krejcid33273d2018-10-25 14:55:52 +0200424 /* latest has no revision, so mod is anyway newer */
Radek Krejci086c7132018-10-26 15:29:04 +0200425 lys_latest_switch(latest, mod->parsed);
Radek Krejcid33273d2018-10-25 14:55:52 +0200426 } else {
Radek Krejcif8f882a2018-10-31 14:51:15 +0100427 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 +0200428 lys_latest_switch(latest, mod->parsed);
Radek Krejcid33273d2018-10-25 14:55:52 +0200429 }
430 }
431 }
432 } else {
433 mod->parsed->latest_revision = 1;
434 }
435
436 /* add into context */
437 ly_set_add(&ctx->list, mod, LY_SET_OPT_USEASLIST);
438
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100439finish_parsing:
Radek Krejcibbe09a92018-11-08 09:36:54 +0100440 /* resolve imports */
Radek Krejci086c7132018-10-26 15:29:04 +0200441 mod->parsed->parsing = 1;
442 LY_ARRAY_FOR(mod->parsed->imports, u) {
443 imp = &mod->parsed->imports[u];
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100444 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 +0100445 goto error_ctx;
Radek Krejci086c7132018-10-26 15:29:04 +0200446 }
447 /* check for importing the same module twice */
448 for (i = 0; i < u; ++i) {
449 if (imp->module == mod->parsed->imports[i].module) {
450 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 +0100451 goto error_ctx;
Radek Krejci086c7132018-10-26 15:29:04 +0200452 }
453 }
454 }
455 LY_ARRAY_FOR(mod->parsed->includes, u) {
456 inc = &mod->parsed->includes[u];
Radek Krejci3b1f9292018-11-08 10:58:35 +0100457 if (!inc->submodule && lysp_load_submodule(&context, mod->parsed, inc)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100458 goto error_ctx;
Radek Krejci086c7132018-10-26 15:29:04 +0200459 }
460 }
461 mod->parsed->parsing = 0;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100462
463 /* check name collisions - typedefs and groupings */
464 LY_CHECK_GOTO(lysp_check_typedefs(&context), error_ctx);
Radek Krejcid33273d2018-10-25 14:55:52 +0200465 }
466
Radek Krejci86d106e2018-10-18 09:53:19 +0200467 return mod;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100468
469error_ctx:
470 ly_set_rm(&ctx->list, mod, NULL);
471error:
472 lys_module_free(mod, NULL);
473 ly_set_erase(&context.tpdfs_nodes, NULL);
474 return NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +0200475}
476
Radek Krejcid14e9692018-11-01 11:00:37 +0100477API struct lys_module *
Radek Krejci86d106e2018-10-18 09:53:19 +0200478lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format)
479{
Radek Krejci3b1f9292018-11-08 10:58:35 +0100480 return lys_parse_mem_(ctx, data, format, 1, NULL, NULL, NULL);
Radek Krejci86d106e2018-10-18 09:53:19 +0200481}
482
483static void
484lys_parse_set_filename(struct ly_ctx *ctx, const char **filename, int fd)
485{
486#ifdef __APPLE__
487 char path[MAXPATHLEN];
488#else
489 int len;
490 char path[PATH_MAX], proc_path[32];
491#endif
492
493#ifdef __APPLE__
494 if (fcntl(fd, F_GETPATH, path) != -1) {
495 *filename = lydict_insert(ctx, path, 0);
496 }
497#else
498 /* get URI if there is /proc */
499 sprintf(proc_path, "/proc/self/fd/%d", fd);
500 if ((len = readlink(proc_path, path, PATH_MAX - 1)) > 0) {
501 *filename = lydict_insert(ctx, path, len);
502 }
503#endif
504}
505
Radek Krejcid33273d2018-10-25 14:55:52 +0200506struct lys_module *
Radek Krejci3b1f9292018-11-08 10:58:35 +0100507lys_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 +0100508 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, void *data), void *check_data)
Radek Krejci86d106e2018-10-18 09:53:19 +0200509{
Radek Krejcid33273d2018-10-25 14:55:52 +0200510 struct lys_module *mod;
Radek Krejci86d106e2018-10-18 09:53:19 +0200511 size_t length;
512 char *addr;
513
514 LY_CHECK_ARG_RET(ctx, ctx, NULL);
515 if (fd < 0) {
516 LOGARG(ctx, fd);
517 return NULL;
518 }
519
520 LY_CHECK_RET(ly_mmap(ctx, fd, &length, (void **)&addr), NULL);
521 if (!addr) {
522 LOGERR(ctx, LY_EINVAL, "Empty schema file.");
523 return NULL;
524 }
525
Radek Krejci3b1f9292018-11-08 10:58:35 +0100526 mod = lys_parse_mem_(ctx, addr, format, implement, main_ctx, custom_check, check_data);
Radek Krejci86d106e2018-10-18 09:53:19 +0200527 ly_munmap(addr, length);
528
529 if (mod && !mod->parsed->filepath) {
530 lys_parse_set_filename(ctx, &mod->parsed->filepath, fd);
531 }
532
533 return mod;
534}
535
Radek Krejcid14e9692018-11-01 11:00:37 +0100536API struct lys_module *
Radek Krejci86d106e2018-10-18 09:53:19 +0200537lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format)
538{
Radek Krejci3b1f9292018-11-08 10:58:35 +0100539 return lys_parse_fd_(ctx, fd, format, 1, NULL, NULL, NULL);
Radek Krejci86d106e2018-10-18 09:53:19 +0200540}
541
Radek Krejcid33273d2018-10-25 14:55:52 +0200542struct lys_module *
Radek Krejci3b1f9292018-11-08 10:58:35 +0100543lys_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 +0100544 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, void *data), void *check_data)
Radek Krejci86d106e2018-10-18 09:53:19 +0200545{
546 int fd;
Radek Krejcid33273d2018-10-25 14:55:52 +0200547 struct lys_module *mod;
Radek Krejci86d106e2018-10-18 09:53:19 +0200548 const char *rev, *dot, *filename;
549 size_t len;
550
551 LY_CHECK_ARG_RET(ctx, ctx, path, NULL);
552
553 fd = open(path, O_RDONLY);
554 LY_CHECK_ERR_RET(fd == -1, LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)), NULL);
555
Radek Krejci3b1f9292018-11-08 10:58:35 +0100556 mod = lys_parse_fd_(ctx, fd, format, implement, main_ctx, custom_check, check_data);
Radek Krejci86d106e2018-10-18 09:53:19 +0200557 close(fd);
558 LY_CHECK_RET(!mod, NULL);
559
560 /* check that name and revision match filename */
561 filename = strrchr(path, '/');
562 if (!filename) {
563 filename = path;
564 } else {
565 filename++;
566 }
567 rev = strchr(filename, '@');
568 dot = strrchr(filename, '.');
569
570 /* name */
571 len = strlen(mod->parsed->name);
572 if (strncmp(filename, mod->parsed->name, len) ||
573 ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) {
574 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->parsed->name);
575 }
576 if (rev) {
577 len = dot - ++rev;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200578 if (!mod->parsed->revs || len != 10 || strncmp(mod->parsed->revs[0].date, rev, len)) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200579 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Radek Krejcib7db73a2018-10-24 14:18:40 +0200580 mod->parsed->revs ? mod->parsed->revs[0].date : "none");
Radek Krejci86d106e2018-10-18 09:53:19 +0200581 }
582 }
583
584 if (!mod->parsed->filepath) {
585 /* store URI */
586 char rpath[PATH_MAX];
587 if (realpath(path, rpath) != NULL) {
588 mod->parsed->filepath = lydict_insert(ctx, rpath, 0);
589 } else {
590 mod->parsed->filepath = lydict_insert(ctx, path, 0);
591 }
592 }
593
594 return mod;
595}
596
Radek Krejcid14e9692018-11-01 11:00:37 +0100597API struct lys_module *
Radek Krejcid33273d2018-10-25 14:55:52 +0200598lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format)
599{
Radek Krejci3b1f9292018-11-08 10:58:35 +0100600 return lys_parse_path_(ctx, path, format, 1, NULL, NULL, NULL);
Radek Krejcid33273d2018-10-25 14:55:52 +0200601}
602
603API LY_ERR
604lys_search_localfile(const char * const *searchpaths, int cwd, const char *name, const char *revision,
605 char **localfile, LYS_INFORMAT *format)
606{
607 size_t len, flen, match_len = 0, dir_len;
608 int i, implicit_cwd = 0, ret = EXIT_FAILURE;
609 char *wd, *wn = NULL;
610 DIR *dir = NULL;
611 struct dirent *file;
612 char *match_name = NULL;
613 LYS_INFORMAT format_aux, match_format = 0;
614 struct ly_set *dirs;
615 struct stat st;
616
617 LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL);
618
619 /* start to fill the dir fifo with the context's search path (if set)
620 * and the current working directory */
621 dirs = ly_set_new();
622 if (!dirs) {
623 LOGMEM(NULL);
624 return EXIT_FAILURE;
625 }
626
627 len = strlen(name);
628 if (cwd) {
629 wd = get_current_dir_name();
630 if (!wd) {
631 LOGMEM(NULL);
632 goto cleanup;
633 } else {
634 /* add implicit current working directory (./) to be searched,
635 * this directory is not searched recursively */
636 if (ly_set_add(dirs, wd, 0) == -1) {
637 goto cleanup;
638 }
639 implicit_cwd = 1;
640 }
641 }
642 if (searchpaths) {
643 for (i = 0; searchpaths[i]; i++) {
644 /* check for duplicities with the implicit current working directory */
645 if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) {
646 implicit_cwd = 0;
647 continue;
648 }
649 wd = strdup(searchpaths[i]);
650 if (!wd) {
651 LOGMEM(NULL);
652 goto cleanup;
653 } else if (ly_set_add(dirs, wd, 0) == -1) {
654 goto cleanup;
655 }
656 }
657 }
658 wd = NULL;
659
660 /* start searching */
661 while (dirs->count) {
662 free(wd);
663 free(wn); wn = NULL;
664
665 dirs->count--;
666 wd = (char *)dirs->objs[dirs->count];
667 dirs->objs[dirs->count] = NULL;
668 LOGVRB("Searching for \"%s\" in %s.", name, wd);
669
670 if (dir) {
671 closedir(dir);
672 }
673 dir = opendir(wd);
674 dir_len = strlen(wd);
675 if (!dir) {
676 LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno));
677 } else {
678 while ((file = readdir(dir))) {
679 if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) {
680 /* skip . and .. */
681 continue;
682 }
683 free(wn);
684 if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) {
685 LOGMEM(NULL);
686 goto cleanup;
687 }
688 if (stat(wn, &st) == -1) {
689 LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)",
690 file->d_name, wd, strerror(errno));
691 continue;
692 }
693 if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) {
694 /* we have another subdirectory in searchpath to explore,
695 * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */
696 if (ly_set_add(dirs, wn, 0) == -1) {
697 goto cleanup;
698 }
699 /* continue with the next item in current directory */
700 wn = NULL;
701 continue;
702 } else if (!S_ISREG(st.st_mode)) {
703 /* not a regular file (note that we see the target of symlinks instead of symlinks */
704 continue;
705 }
706
707 /* here we know that the item is a file which can contain a module */
708 if (strncmp(name, file->d_name, len) ||
709 (file->d_name[len] != '.' && file->d_name[len] != '@')) {
710 /* different filename than the module we search for */
711 continue;
712 }
713
714 /* get type according to filename suffix */
715 flen = strlen(file->d_name);
716 if (!strcmp(&file->d_name[flen - 4], ".yin")) {
717 format_aux = LYS_IN_YIN;
718 } else if (!strcmp(&file->d_name[flen - 5], ".yang")) {
719 format_aux = LYS_IN_YANG;
720 } else {
721 /* not supportde suffix/file format */
722 continue;
723 }
724
725 if (revision) {
726 /* we look for the specific revision, try to get it from the filename */
727 if (file->d_name[len] == '@') {
728 /* check revision from the filename */
729 if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) {
730 /* another revision */
731 continue;
732 } else {
733 /* exact revision */
734 free(match_name);
735 match_name = wn;
736 wn = NULL;
737 match_len = dir_len + 1 + len;
738 match_format = format_aux;
739 goto success;
740 }
741 } else {
742 /* continue trying to find exact revision match, use this only if not found */
743 free(match_name);
744 match_name = wn;
745 wn = NULL;
746 match_len = dir_len + 1 +len;
747 match_format = format_aux;
748 continue;
749 }
750 } else {
751 /* remember the revision and try to find the newest one */
752 if (match_name) {
753 if (file->d_name[len] != '@' ||
754 lysp_check_date(NULL, &file->d_name[len + 1], flen - (format_aux == LYS_IN_YANG ? 5 : 4) - len - 1, NULL)) {
755 continue;
756 } else if (match_name[match_len] == '@' &&
757 (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) {
758 continue;
759 }
760 free(match_name);
761 }
762
763 match_name = wn;
764 wn = NULL;
765 match_len = dir_len + 1 + len;
766 match_format = format_aux;
767 continue;
768 }
769 }
770 }
771 }
772
773success:
774 (*localfile) = match_name;
775 match_name = NULL;
776 if (format) {
777 (*format) = match_format;
778 }
779 ret = EXIT_SUCCESS;
780
781cleanup:
782 free(wn);
783 free(wd);
784 if (dir) {
785 closedir(dir);
786 }
787 free(match_name);
788 ly_set_free(dirs, free);
789
790 return ret;
791}
792