blob: 6fba4d69451ce152c6f447e9ad7dea9a7f59332a [file] [log] [blame]
Radek Krejci3f5e3db2018-10-11 15:57:47 +02001/**
2 * @file tree_schema.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema tree implementation
5 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Radek Krejcib7db73a2018-10-24 14:18:40 +020014
15#include "common.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020016
Radek Krejcid33273d2018-10-25 14:55:52 +020017#include <dirent.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020018#include <errno.h>
Radek Krejci7802bae2018-11-26 15:34:10 +010019#include <limits.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020020#include <fcntl.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020021#include <stdio.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020022#include <sys/stat.h>
23#include <sys/types.h>
24#include <unistd.h>
Radek Krejci3f5e3db2018-10-11 15:57:47 +020025
26#include "libyang.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020027#include "context.h"
Radek Krejci70853c52018-10-15 14:46:16 +020028#include "tree_schema_internal.h"
Radek Krejci4f28eda2018-11-12 11:46:16 +010029#include "xpath.h"
Radek Krejci3f5e3db2018-10-11 15:57:47 +020030
Radek Krejcia3045382018-11-22 14:30:31 +010031API const struct lysc_node *
32lys_getnext(const struct lysc_node *last, const struct lysc_node *parent, const struct lysc_module *module, int options)
33{
34 const struct lysc_node *next;
35 struct lysc_node **snode;
36
37 LY_CHECK_ARG_RET(NULL, parent || module, NULL);
38
39 if (!last) {
40 /* first call */
41
42 /* get know where to start */
43 if (parent) {
44 /* schema subtree */
Radek Krejci056d0a82018-12-06 16:57:25 +010045 if (parent->nodetype == LYS_CHOICE && (options & LYS_GETNEXT_WITHCASE)) {
46 if (!((struct lysc_node_choice*)parent)->cases) {
47 return NULL;
48 }
49 next = last = (const struct lysc_node*)&((struct lysc_node_choice*)parent)->cases[0];
50 } else {
51 snode = lysc_node_children_p(parent);
52 /* do not return anything if the augment does not have any children */
53 if (!snode || !(*snode)) {
54 return NULL;
55 }
56 next = last = *snode;
Radek Krejcia3045382018-11-22 14:30:31 +010057 }
Radek Krejcia3045382018-11-22 14:30:31 +010058 } else {
59 /* top level data */
60 next = last = module->data;
61 }
62 if (!next) {
63 return next;
64 } else if (!(options & LYS_GETNEXT_NOSTATECHECK)) {
65 if (!lys_is_disabled(next, 0)) {
66 return next;
67 }
68 /* continue to find next available node */
69 } else {
70 return next;
71 }
72 }
73
74 next = last->next;
75repeat:
Radek Krejci01342af2019-01-03 15:18:08 +010076 if (next && parent && parent->nodetype == LYS_CASE && next->parent != parent) {
77 /* inside case (as an explicit parent, not when diving into it from choice),
78 * limit the list of children only to the specific case */
79 next = NULL;
80 }
Radek Krejcia3045382018-11-22 14:30:31 +010081 if (!next) {
Radek Krejcia9026eb2018-12-12 16:04:47 +010082 /* possibly go back to parent */
83 if (last->parent != parent) {
84 last = last->parent;
85 next = last->next;
86 goto repeat;
87 }
Radek Krejcia3045382018-11-22 14:30:31 +010088 return next;
89 }
90 switch (next->nodetype) {
91 case LYS_ACTION:
92 case LYS_NOTIF:
93 case LYS_LEAF:
94 case LYS_ANYXML:
95 case LYS_ANYDATA:
96 case LYS_LIST:
97 case LYS_LEAFLIST:
Radek Krejcia9026eb2018-12-12 16:04:47 +010098 case LYS_CASE:
Radek Krejcia3045382018-11-22 14:30:31 +010099 break;
100 case LYS_CONTAINER:
101 if (!(((struct lysc_node_container *)next)->flags & LYS_PRESENCE) && (options & LYS_GETNEXT_INTONPCONT)) {
102 if (((struct lysc_node_container *)next)->child) {
103 /* go into */
104 next = ((struct lysc_node_container *)next)->child;
105 } else {
106 next = next->next;
107 }
108 goto repeat;
109 }
110 break;
111 case LYS_CHOICE:
112 if (options & LYS_GETNEXT_WITHCHOICE) {
113 return next;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100114 } else if ((options & LYS_GETNEXT_NOCHOICE) || !((struct lysc_node_choice *)next)->cases) {
115 next = next->next;
116 } else {
Radek Krejcia3045382018-11-22 14:30:31 +0100117 /* go into */
Radek Krejcia9026eb2018-12-12 16:04:47 +0100118 if (options & LYS_GETNEXT_WITHCASE) {
119 next = (struct lysc_node*)&((struct lysc_node_choice *)next)->cases;
120 } else {
121 next = ((struct lysc_node_choice *)next)->cases->child;
122 }
Radek Krejcia3045382018-11-22 14:30:31 +0100123 }
124 goto repeat;
125 default:
126 /* we should not be here */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100127 LOGINT(last->module->ctx);
Radek Krejcia3045382018-11-22 14:30:31 +0100128 return NULL;
129 }
130
131 if (!(options & LYS_GETNEXT_NOSTATECHECK)) {
132 /* check if the node is disabled by if-feature */
133 if (lys_is_disabled(next, 0)) {
134 next = next->next;
135 goto repeat;
136 }
137 }
138 return next;
139}
140
141API const struct lysc_node *
142lys_child(const struct lysc_node *parent, const struct lys_module *module,
143 const char *name, size_t name_len, uint16_t nodetype, int options)
144{
145 const struct lysc_node *node = NULL;
146
147 LY_CHECK_ARG_RET(NULL, module, name, NULL);
148 if (!nodetype) {
149 nodetype = 0xffff;
150 }
151
152 while ((node = lys_getnext(node, parent, module->compiled, options))) {
153 if (!(node->nodetype & nodetype)) {
154 continue;
155 }
156 if (node->module != module) {
157 continue;
158 }
159
160 if (name_len) {
161 if (!strncmp(node->name, name, name_len) && !node->name[name_len]) {
162 return node;
163 }
164 } else {
165 if (!strcmp(node->name, name)) {
166 return node;
167 }
168 }
169 }
170 return NULL;
171}
172
Radek Krejci19a96102018-11-15 13:38:09 +0100173API int
174lysc_feature_value(const struct lysc_feature *feature)
Radek Krejci6f7feb62018-10-12 15:23:02 +0200175{
Radek Krejci19a96102018-11-15 13:38:09 +0100176 LY_CHECK_ARG_RET(NULL, feature, -1);
177 return feature->flags & LYS_FENABLED ? 1 : 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200178}
179
180static uint8_t
181iff_getop(uint8_t *list, int pos)
182{
183 uint8_t *item;
184 uint8_t mask = 3, result;
185
186 assert(pos >= 0);
187
188 item = &list[pos / 4];
189 result = (*item) & (mask << 2 * (pos % 4));
190 return result >> 2 * (pos % 4);
191}
192
Radek Krejci151a5b72018-10-19 14:21:44 +0200193static int
194lysc_iffeature_value_(const struct lysc_iffeature *iff, int *index_e, int *index_f)
195{
196 uint8_t op;
197 int a, b;
198
199 op = iff_getop(iff->expr, *index_e);
200 (*index_e)++;
201
202 switch (op) {
203 case LYS_IFF_F:
204 /* resolve feature */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200205 return lysc_feature_value(iff->features[(*index_f)++]);
Radek Krejci151a5b72018-10-19 14:21:44 +0200206 case LYS_IFF_NOT:
207 /* invert result */
208 return lysc_iffeature_value_(iff, index_e, index_f) ? 0 : 1;
209 case LYS_IFF_AND:
210 case LYS_IFF_OR:
211 a = lysc_iffeature_value_(iff, index_e, index_f);
212 b = lysc_iffeature_value_(iff, index_e, index_f);
213 if (op == LYS_IFF_AND) {
214 return a && b;
215 } else { /* LYS_IFF_OR */
216 return a || b;
217 }
218 }
219
220 return 0;
221}
222
223API int
224lysc_iffeature_value(const struct lysc_iffeature *iff)
225{
226 int index_e = 0, index_f = 0;
227
228 LY_CHECK_ARG_RET(NULL, iff, -1);
229
230 if (iff->expr) {
231 return lysc_iffeature_value_(iff, &index_e, &index_f);
232 }
233 return 0;
234}
235
Radek Krejci151a5b72018-10-19 14:21:44 +0200236/**
237 * @brief Enable/Disable the specified feature in the module.
238 *
239 * If the feature is already set to the desired value, LY_SUCCESS is returned.
240 * By changing the feature, also all the feature which depends on it via their
241 * if-feature statements are again evaluated (disabled if a if-feature statemen
242 * evaluates to false).
243 *
Radek Krejci0af46292019-01-11 16:02:31 +0100244 * @param[in] mod Module where to set (search for) the feature.
Radek Krejci151a5b72018-10-19 14:21:44 +0200245 * @param[in] name Name of the feature to set. Asterisk ('*') can be used to
246 * set all the features in the module.
247 * @param[in] value Desired value of the feature: 1 (enable) or 0 (disable).
248 * @return LY_ERR value.
249 */
250static LY_ERR
Radek Krejci0af46292019-01-11 16:02:31 +0100251lys_feature_change(const struct lys_module *mod, const char *name, int value)
Radek Krejci151a5b72018-10-19 14:21:44 +0200252{
253 int all = 0;
Radek Krejcica3db002018-11-01 10:31:01 +0100254 unsigned int u, changed_count, disabled_count;
Radek Krejci151a5b72018-10-19 14:21:44 +0200255 struct lysc_feature *f, **df;
256 struct lysc_iffeature *iff;
257 struct ly_set *changed;
Radek Krejci0af46292019-01-11 16:02:31 +0100258 struct ly_ctx *ctx = mod->ctx; /* shortcut */
Radek Krejci151a5b72018-10-19 14:21:44 +0200259
Radek Krejci0af46292019-01-11 16:02:31 +0100260 if (!mod->compiled) {
261 LOGERR(ctx, LY_EINVAL, "Module \"%s\" is not implemented so all its features are permanently disabled without a chance to change it.",
262 mod->name);
263 return LY_EINVAL;
264 }
265 if (!mod->compiled->features) {
266 LOGERR(ctx, LY_EINVAL, "Unable to switch feature since the module \"%s\" has no features.", mod->name);
Radek Krejci151a5b72018-10-19 14:21:44 +0200267 return LY_EINVAL;
268 }
269
270 if (!strcmp(name, "*")) {
271 /* enable all */
272 all = 1;
273 }
274 changed = ly_set_new();
Radek Krejcica3db002018-11-01 10:31:01 +0100275 changed_count = 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200276
Radek Krejcica3db002018-11-01 10:31:01 +0100277run:
Radek Krejci0af46292019-01-11 16:02:31 +0100278 for (disabled_count = u = 0; u < LY_ARRAY_SIZE(mod->compiled->features); ++u) {
279 f = &mod->compiled->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200280 if (all || !strcmp(f->name, name)) {
281 if ((value && (f->flags & LYS_FENABLED)) || (!value && !(f->flags & LYS_FENABLED))) {
282 if (all) {
283 /* skip already set features */
284 continue;
285 } else {
286 /* feature already set correctly */
287 ly_set_free(changed, NULL);
288 return LY_SUCCESS;
289 }
290 }
291
292 if (value) { /* enable */
293 /* check referenced features if they are enabled */
294 LY_ARRAY_FOR(f->iffeatures, struct lysc_iffeature, iff) {
295 if (!lysc_iffeature_value(iff)) {
296 if (all) {
Radek Krejcica3db002018-11-01 10:31:01 +0100297 ++disabled_count;
Radek Krejci151a5b72018-10-19 14:21:44 +0200298 goto next;
299 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100300 LOGERR(ctx, LY_EDENIED,
Radek Krejci151a5b72018-10-19 14:21:44 +0200301 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
302 f->name);
303 ly_set_free(changed, NULL);
304 return LY_EDENIED;
305 }
306 }
307 }
308 /* enable the feature */
309 f->flags |= LYS_FENABLED;
310 } else { /* disable */
311 /* disable the feature */
312 f->flags &= ~LYS_FENABLED;
313 }
314
315 /* remember the changed feature */
316 ly_set_add(changed, f, LY_SET_OPT_USEASLIST);
317
318 if (!all) {
319 /* stop in case changing a single feature */
320 break;
321 }
322 }
323next:
324 ;
325 }
326
327 if (!all && !changed->count) {
Radek Krejci0af46292019-01-11 16:02:31 +0100328 LOGERR(ctx, LY_EINVAL, "Feature \"%s\" not found in module \"%s\".", name, mod->name);
Radek Krejci151a5b72018-10-19 14:21:44 +0200329 ly_set_free(changed, NULL);
330 return LY_EINVAL;
331 }
332
Radek Krejcica3db002018-11-01 10:31:01 +0100333 if (value && all && disabled_count) {
334 if (changed_count == changed->count) {
335 /* no change in last run -> not able to enable all ... */
336 /* ... print errors */
Radek Krejci0af46292019-01-11 16:02:31 +0100337 for (u = 0; disabled_count && u < LY_ARRAY_SIZE(mod->compiled->features); ++u) {
338 if (!(mod->compiled->features[u].flags & LYS_FENABLED)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100339 LOGERR(ctx, LY_EDENIED,
Radek Krejcica3db002018-11-01 10:31:01 +0100340 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
Radek Krejci0af46292019-01-11 16:02:31 +0100341 mod->compiled->features[u].name);
Radek Krejcica3db002018-11-01 10:31:01 +0100342 --disabled_count;
343 }
344 }
345 /* ... restore the original state */
346 for (u = 0; u < changed->count; ++u) {
347 f = changed->objs[u];
348 /* re-disable the feature */
349 f->flags &= ~LYS_FENABLED;
350 }
351
352 ly_set_free(changed, NULL);
353 return LY_EDENIED;
354 } else {
355 /* we did some change in last run, try it again */
356 changed_count = changed->count;
357 goto run;
358 }
359 }
360
Radek Krejci151a5b72018-10-19 14:21:44 +0200361 /* reflect change(s) in the dependent features */
362 for (u = 0; u < changed->count; ++u) {
363 /* If a dependent feature is enabled, it can be now changed by the change (to false) of the value of
364 * its if-feature statements. The reverse logic, automatically enable feature when its feature is enabled
365 * is not done - by default, features are disabled and must be explicitely enabled. */
366 f = changed->objs[u];
367 LY_ARRAY_FOR(f->depfeatures, struct lysc_feature*, df) {
368 if (!((*df)->flags & LYS_FENABLED)) {
369 /* not enabled, nothing to do */
370 continue;
371 }
372 /* check the feature's if-features which could change by the previous change of our feature */
373 LY_ARRAY_FOR((*df)->iffeatures, struct lysc_iffeature, iff) {
374 if (!lysc_iffeature_value(iff)) {
375 /* the feature must be disabled now */
376 (*df)->flags &= ~LYS_FENABLED;
377 /* add the feature into the list of changed features */
378 ly_set_add(changed, *df, LY_SET_OPT_USEASLIST);
379 break;
380 }
381 }
382 }
383 }
384
385 ly_set_free(changed, NULL);
386 return LY_SUCCESS;
387}
388
389API LY_ERR
390lys_feature_enable(struct lys_module *module, const char *feature)
391{
Radek Krejci0af46292019-01-11 16:02:31 +0100392 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200393
Radek Krejci0af46292019-01-11 16:02:31 +0100394 return lys_feature_change(module, feature, 1);
Radek Krejci151a5b72018-10-19 14:21:44 +0200395}
396
397API LY_ERR
398lys_feature_disable(struct lys_module *module, const char *feature)
399{
Radek Krejci0af46292019-01-11 16:02:31 +0100400 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200401
Radek Krejci0af46292019-01-11 16:02:31 +0100402 return lys_feature_change(module, feature, 0);
Radek Krejci151a5b72018-10-19 14:21:44 +0200403}
404
405API int
406lys_feature_value(const struct lys_module *module, const char *feature)
407{
408 struct lysc_feature *f;
409 struct lysc_module *mod;
410 unsigned int u;
411
412 LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, -1);
413 mod = module->compiled;
414
415 /* search for the specified feature */
416 for (u = 0; u < LY_ARRAY_SIZE(mod->features); ++u) {
Radek Krejci2c4e7172018-10-19 15:56:26 +0200417 f = &mod->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200418 if (!strcmp(f->name, feature)) {
419 if (f->flags & LYS_FENABLED) {
420 return 1;
421 } else {
422 return 0;
423 }
424 }
425 }
426
427 /* feature definition not found */
428 return -1;
429}
430
Radek Krejcia3045382018-11-22 14:30:31 +0100431API const struct lysc_iffeature *
432lys_is_disabled(const struct lysc_node *node, int recursive)
433{
434 unsigned int u;
Radek Krejcia3045382018-11-22 14:30:31 +0100435
436 LY_CHECK_ARG_RET(NULL, node, NULL);
437
438 while(node) {
439 if (node->nodetype & LYS_CHOICE) {
440 return NULL;
441 }
442
Radek Krejci056d0a82018-12-06 16:57:25 +0100443 if (node->iffeatures) {
Radek Krejcia3045382018-11-22 14:30:31 +0100444 /* check local if-features */
Radek Krejci056d0a82018-12-06 16:57:25 +0100445 LY_ARRAY_FOR(node->iffeatures, u) {
446 if (!lysc_iffeature_value(&node->iffeatures[u])) {
447 return &node->iffeatures[u];
Radek Krejcia3045382018-11-22 14:30:31 +0100448 }
449 }
450 }
451
452 if (!recursive) {
453 return NULL;
454 }
455
456 /* go through parents */
457 node = node->parent;
458 }
459 return NULL;
460}
461
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100462struct lysp_submodule *
463lys_parse_mem_submodule(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, struct ly_parser_ctx *main_ctx,
464 LY_ERR (*custom_check)(struct ly_ctx*, struct lysp_module*, struct lysp_submodule*, void*), void *check_data)
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200465{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100466 LY_ERR ret = LY_EINVAL;
467 struct lysp_submodule *submod = NULL, *latest_sp;
468 struct ly_parser_ctx context = {0};
469
470 LY_CHECK_ARG_RET(ctx, ctx, data, NULL);
471
472 context.ctx = ctx;
473 context.line = 1;
474
475 /* map the typedefs and groupings list from main context to the submodule's context */
476 memcpy(&context.tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
477 memcpy(&context.grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
478
479 switch (format) {
480 case LYS_IN_YIN:
481 /* TODO not yet supported
482 mod = yin_read_module();
483 */
484 break;
485 case LYS_IN_YANG:
486 ret = yang_parse_submodule(&context, data, &submod);
487 break;
488 default:
489 LOGERR(context.ctx, LY_EINVAL, "Invalid schema input format.");
490 break;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200491 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100492 LY_CHECK_RET(ret, NULL);
493
494 /* make sure that the newest revision is at position 0 */
495 lysp_sort_revisions(submod->revs);
496
497 if (custom_check) {
498 LY_CHECK_GOTO(custom_check(context.ctx, NULL, submod, check_data), error);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200499 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100500
501 /* decide the latest revision */
502 latest_sp = ly_ctx_get_submodule(context.ctx, submod->belongsto, submod->name, NULL);
503 if (latest_sp) {
504 if (submod->revs) {
505 if (!latest_sp->revs) {
506 /* latest has no revision, so mod is anyway newer */
507 submod->latest_revision = latest_sp->latest_revision;
508 latest_sp->latest_revision = 0;
509 } else {
510 if (strcmp(submod->revs[0].date, latest_sp->revs[0].date) > 0) {
511 submod->latest_revision = latest_sp->latest_revision;
512 latest_sp->latest_revision = 0;
513 }
514 }
515 }
516 } else {
517 submod->latest_revision = 1;
518 }
519
520 /* remap possibly changed and reallocated typedefs and groupings list back to the main context */
521 memcpy(&main_ctx->tpdfs_nodes, &context.tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
522 memcpy(&main_ctx->grps_nodes, &context.grps_nodes, sizeof main_ctx->grps_nodes);
523
524 return submod;
525error:
526 lysp_submodule_free(ctx, submod);
527 return NULL;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200528}
529
Radek Krejcid33273d2018-10-25 14:55:52 +0200530struct lys_module *
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100531lys_parse_mem_module(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, int implement,
532 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
533 void *check_data)
Radek Krejci86d106e2018-10-18 09:53:19 +0200534{
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100535 struct lys_module *mod = NULL, *latest, *mod_dup;
Radek Krejci086c7132018-10-26 15:29:04 +0200536 struct lysp_import *imp;
537 struct lysp_include *inc;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100538 LY_ERR ret = LY_EINVAL;
Radek Krejci086c7132018-10-26 15:29:04 +0200539 unsigned int u, i;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100540 struct ly_parser_ctx context = {0};
Radek Krejci86d106e2018-10-18 09:53:19 +0200541
542 LY_CHECK_ARG_RET(ctx, ctx, data, NULL);
543
Radek Krejcibbe09a92018-11-08 09:36:54 +0100544 context.ctx = ctx;
545 context.line = 1;
546
Radek Krejci86d106e2018-10-18 09:53:19 +0200547 mod = calloc(1, sizeof *mod);
548 LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100549 mod->ctx = ctx;
Radek Krejci86d106e2018-10-18 09:53:19 +0200550
551 switch (format) {
552 case LYS_IN_YIN:
553 /* TODO not yet supported
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100554 mod = yin_read_module();
Radek Krejci86d106e2018-10-18 09:53:19 +0200555 */
556 break;
557 case LYS_IN_YANG:
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100558 ret = yang_parse_module(&context, data, mod);
Radek Krejci86d106e2018-10-18 09:53:19 +0200559 break;
560 default:
561 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
562 break;
563 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100564 LY_CHECK_ERR_RET(ret, lys_module_free(mod, NULL), NULL);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200565
566 /* make sure that the newest revision is at position 0 */
567 lysp_sort_revisions(mod->parsed->revs);
Radek Krejci0af46292019-01-11 16:02:31 +0100568 if (mod->parsed->revs) {
569 mod->revision = lydict_insert(ctx, mod->parsed->revs[0].date, 0);
570 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200571
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100572 if (custom_check) {
573 LY_CHECK_GOTO(custom_check(ctx, mod->parsed, NULL, check_data), error);
574 }
575
Radek Krejci86d106e2018-10-18 09:53:19 +0200576 if (implement) {
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200577 /* mark the loaded module implemented */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100578 if (ly_ctx_get_module_implemented(ctx, mod->name)) {
579 LOGERR(ctx, LY_EDENIED, "Module \"%s\" is already implemented in the context.", mod->name);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100580 goto error;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200581 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100582 mod->implemented = 1;
Radek Krejci86d106e2018-10-18 09:53:19 +0200583 }
584
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100585 /* check for duplicity in the context */
Radek Krejci0af46292019-01-11 16:02:31 +0100586 mod_dup = (struct lys_module*)ly_ctx_get_module(ctx, mod->name, mod->revision);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100587 if (mod_dup) {
588 if (mod_dup->parsed) {
589 /* error */
Radek Krejcid33273d2018-10-25 14:55:52 +0200590 if (mod->parsed->revs) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100591 LOGERR(ctx, LY_EEXIST, "Module \"%s\" of revision \"%s\" is already present in the context.",
592 mod->name, mod->parsed->revs[0].date);
Radek Krejcid33273d2018-10-25 14:55:52 +0200593 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100594 LOGERR(ctx, LY_EEXIST, "Module \"%s\" with no revision is already present in the context.",
595 mod->name);
Radek Krejcid33273d2018-10-25 14:55:52 +0200596 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100597 goto error;
598 } else {
599 /* add the parsed data to the currently compiled-only module in the context */
600 mod_dup->parsed = mod->parsed;
601 mod_dup->parsed->mod = mod_dup;
602 mod->parsed = NULL;
603 lys_module_free(mod, NULL);
604 mod = mod_dup;
605 goto finish_parsing;
Radek Krejcid33273d2018-10-25 14:55:52 +0200606 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100607 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200608
609#if 0
610 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
611 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
612 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
613 * the anotation definitions available in the internal schema structure. There is another hack in schema
614 * printers to do not print this internally added annotation. */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100615 if (ly_strequal(mod->name, "ietf-netconf", 0)) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200616 if (lyp_add_ietf_netconf_annotations(mod)) {
617 lys_free(mod, NULL, 1, 1);
618 return NULL;
619 }
620 }
621#endif
622
Radek Krejci0af46292019-01-11 16:02:31 +0100623 if (!mod->implemented) {
624 /* pre-compile features of the module */
625 LY_CHECK_GOTO(lys_feature_precompile(ctx, mod->parsed->features, &mod->off_features), error);
626 }
627
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100628 /* decide the latest revision */
629 latest = (struct lys_module*)ly_ctx_get_module_latest(ctx, mod->name);
630 if (latest) {
Radek Krejci0af46292019-01-11 16:02:31 +0100631 if (mod->revision) {
632 if (!latest->revision) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100633 /* latest has no revision, so mod is anyway newer */
634 mod->latest_revision = latest->latest_revision;
635 latest->latest_revision = 0;
Radek Krejci0af46292019-01-11 16:02:31 +0100636 } else if (strcmp(mod->revision, latest->revision) > 0) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100637 mod->latest_revision = latest->latest_revision;
638 latest->latest_revision = 0;
Radek Krejcid33273d2018-10-25 14:55:52 +0200639 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200640 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100641 } else {
642 mod->latest_revision = 1;
643 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200644
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100645 /* add into context */
646 ly_set_add(&ctx->list, mod, LY_SET_OPT_USEASLIST);
Radek Krejcid33273d2018-10-25 14:55:52 +0200647
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100648finish_parsing:
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100649 /* resolve imports */
650 mod->parsed->parsing = 1;
651 LY_ARRAY_FOR(mod->parsed->imports, u) {
652 imp = &mod->parsed->imports[u];
653 if (!imp->module && lysp_load_module(ctx, imp->name, imp->rev[0] ? imp->rev : NULL, 0, 0, &imp->module)) {
654 goto error_ctx;
Radek Krejci086c7132018-10-26 15:29:04 +0200655 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100656 /* check for importing the same module twice */
657 for (i = 0; i < u; ++i) {
658 if (imp->module == mod->parsed->imports[i].module) {
659 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 +0100660 goto error_ctx;
Radek Krejci086c7132018-10-26 15:29:04 +0200661 }
662 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200663 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100664 LY_ARRAY_FOR(mod->parsed->includes, u) {
665 inc = &mod->parsed->includes[u];
666 if (!inc->submodule && lysp_load_submodule(&context, mod->parsed, inc)) {
667 goto error_ctx;
668 }
Radek Krejci0af46292019-01-11 16:02:31 +0100669 if (!mod->implemented) {
670 /* pre-compile features of the module */
671 LY_CHECK_GOTO(lys_feature_precompile(ctx, inc->submodule->features, &mod->off_features), error);
672 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100673 }
674 mod->parsed->parsing = 0;
675
676 /* check name collisions - typedefs and groupings */
677 LY_CHECK_GOTO(lysp_check_typedefs(&context, mod->parsed), error_ctx);
Radek Krejcid33273d2018-10-25 14:55:52 +0200678
Radek Krejci86d106e2018-10-18 09:53:19 +0200679 return mod;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100680
681error_ctx:
682 ly_set_rm(&ctx->list, mod, NULL);
683error:
684 lys_module_free(mod, NULL);
685 ly_set_erase(&context.tpdfs_nodes, NULL);
686 return NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +0200687}
688
Radek Krejcid14e9692018-11-01 11:00:37 +0100689API struct lys_module *
Radek Krejci86d106e2018-10-18 09:53:19 +0200690lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format)
691{
Radek Krejci096235c2019-01-11 11:12:19 +0100692 struct lys_module *mod;
693
694 mod = lys_parse_mem_module(ctx, data, format, 1, NULL, NULL);
695 LY_CHECK_RET(!mod, NULL);
696
697 if (lys_compile(mod, 0)) {
698 ly_set_rm(&ctx->list, mod, NULL);
699 lys_module_free(mod, NULL);
700 return NULL;
701 }
702 return mod;
Radek Krejci86d106e2018-10-18 09:53:19 +0200703}
704
705static void
706lys_parse_set_filename(struct ly_ctx *ctx, const char **filename, int fd)
707{
Radek Krejci65639b92018-11-27 10:51:37 +0100708 char path[PATH_MAX];
Radek Krejci86d106e2018-10-18 09:53:19 +0200709
710#ifdef __APPLE__
711 if (fcntl(fd, F_GETPATH, path) != -1) {
712 *filename = lydict_insert(ctx, path, 0);
713 }
714#else
Radek Krejci65639b92018-11-27 10:51:37 +0100715 int len;
716 char proc_path[32];
717
Radek Krejci86d106e2018-10-18 09:53:19 +0200718 /* get URI if there is /proc */
719 sprintf(proc_path, "/proc/self/fd/%d", fd);
720 if ((len = readlink(proc_path, path, PATH_MAX - 1)) > 0) {
721 *filename = lydict_insert(ctx, path, len);
722 }
723#endif
724}
725
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100726void *
Radek Krejci3b1f9292018-11-08 10:58:35 +0100727lys_parse_fd_(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, int implement, struct ly_parser_ctx *main_ctx,
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100728 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
729 void *check_data)
Radek Krejci86d106e2018-10-18 09:53:19 +0200730{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100731 void *result;
732 struct lys_module *mod = NULL;
733 struct lysp_submodule *submod = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +0200734 size_t length;
735 char *addr;
736
737 LY_CHECK_ARG_RET(ctx, ctx, NULL);
738 if (fd < 0) {
739 LOGARG(ctx, fd);
740 return NULL;
741 }
742
743 LY_CHECK_RET(ly_mmap(ctx, fd, &length, (void **)&addr), NULL);
744 if (!addr) {
745 LOGERR(ctx, LY_EINVAL, "Empty schema file.");
746 return NULL;
747 }
748
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100749 if (main_ctx) {
750 result = submod = lys_parse_mem_submodule(ctx, addr, format, main_ctx, custom_check, check_data);
751 } else {
752 result = mod = lys_parse_mem_module(ctx, addr, format, implement, custom_check, check_data);
Radek Krejci096235c2019-01-11 11:12:19 +0100753 if (mod && implement && lys_compile(mod, 0)) {
754 ly_set_rm(&ctx->list, mod, NULL);
755 lys_module_free(mod, NULL);
756 result = mod = NULL;
757 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100758 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200759 ly_munmap(addr, length);
760
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100761 if (mod && !mod->filepath) {
762 lys_parse_set_filename(ctx, &mod->filepath, fd);
763 } else if (submod && !submod->filepath) {
764 lys_parse_set_filename(ctx, &submod->filepath, fd);
Radek Krejci86d106e2018-10-18 09:53:19 +0200765 }
766
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100767 return result;
768}
769
770struct lys_module *
771lys_parse_fd_module(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, int implement,
772 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
773 void *check_data)
774{
775 return (struct lys_module*)lys_parse_fd_(ctx, fd, format, implement, NULL, custom_check, check_data);
776}
777
778struct lysp_submodule *
779lys_parse_fd_submodule(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, struct ly_parser_ctx *main_ctx,
780 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
781 void *check_data)
782{
783 assert(main_ctx);
784 return (struct lysp_submodule*)lys_parse_fd_(ctx, fd, format, 0, main_ctx, custom_check, check_data);
Radek Krejci86d106e2018-10-18 09:53:19 +0200785}
786
Radek Krejcid14e9692018-11-01 11:00:37 +0100787API struct lys_module *
Radek Krejci86d106e2018-10-18 09:53:19 +0200788lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format)
789{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100790 return lys_parse_fd_module(ctx, fd, format, 1, NULL, NULL);
Radek Krejci86d106e2018-10-18 09:53:19 +0200791}
792
Radek Krejcid33273d2018-10-25 14:55:52 +0200793struct lys_module *
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100794lys_parse_path_(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, int implement,
795 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data), void *check_data)
Radek Krejci86d106e2018-10-18 09:53:19 +0200796{
797 int fd;
Radek Krejcid33273d2018-10-25 14:55:52 +0200798 struct lys_module *mod;
Radek Krejci86d106e2018-10-18 09:53:19 +0200799 const char *rev, *dot, *filename;
800 size_t len;
801
802 LY_CHECK_ARG_RET(ctx, ctx, path, NULL);
803
804 fd = open(path, O_RDONLY);
805 LY_CHECK_ERR_RET(fd == -1, LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)), NULL);
806
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100807 mod = lys_parse_fd_module(ctx, fd, format, implement, custom_check, check_data);
Radek Krejci86d106e2018-10-18 09:53:19 +0200808 close(fd);
809 LY_CHECK_RET(!mod, NULL);
810
811 /* check that name and revision match filename */
812 filename = strrchr(path, '/');
813 if (!filename) {
814 filename = path;
815 } else {
816 filename++;
817 }
818 rev = strchr(filename, '@');
819 dot = strrchr(filename, '.');
820
821 /* name */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100822 len = strlen(mod->name);
823 if (strncmp(filename, mod->name, len) ||
Radek Krejci86d106e2018-10-18 09:53:19 +0200824 ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100825 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name);
Radek Krejci86d106e2018-10-18 09:53:19 +0200826 }
827 if (rev) {
828 len = dot - ++rev;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200829 if (!mod->parsed->revs || len != 10 || strncmp(mod->parsed->revs[0].date, rev, len)) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200830 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Radek Krejcib7db73a2018-10-24 14:18:40 +0200831 mod->parsed->revs ? mod->parsed->revs[0].date : "none");
Radek Krejci86d106e2018-10-18 09:53:19 +0200832 }
833 }
834
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100835 if (!mod->filepath) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200836 /* store URI */
837 char rpath[PATH_MAX];
838 if (realpath(path, rpath) != NULL) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100839 mod->filepath = lydict_insert(ctx, rpath, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +0200840 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100841 mod->filepath = lydict_insert(ctx, path, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +0200842 }
843 }
844
845 return mod;
846}
847
Radek Krejcid14e9692018-11-01 11:00:37 +0100848API struct lys_module *
Radek Krejcid33273d2018-10-25 14:55:52 +0200849lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format)
850{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100851 return lys_parse_path_(ctx, path, format, 1, NULL, NULL);
Radek Krejcid33273d2018-10-25 14:55:52 +0200852}
853
854API LY_ERR
855lys_search_localfile(const char * const *searchpaths, int cwd, const char *name, const char *revision,
856 char **localfile, LYS_INFORMAT *format)
857{
858 size_t len, flen, match_len = 0, dir_len;
859 int i, implicit_cwd = 0, ret = EXIT_FAILURE;
860 char *wd, *wn = NULL;
861 DIR *dir = NULL;
862 struct dirent *file;
863 char *match_name = NULL;
864 LYS_INFORMAT format_aux, match_format = 0;
865 struct ly_set *dirs;
866 struct stat st;
867
868 LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL);
869
870 /* start to fill the dir fifo with the context's search path (if set)
871 * and the current working directory */
872 dirs = ly_set_new();
873 if (!dirs) {
874 LOGMEM(NULL);
875 return EXIT_FAILURE;
876 }
877
878 len = strlen(name);
879 if (cwd) {
880 wd = get_current_dir_name();
881 if (!wd) {
882 LOGMEM(NULL);
883 goto cleanup;
884 } else {
885 /* add implicit current working directory (./) to be searched,
886 * this directory is not searched recursively */
887 if (ly_set_add(dirs, wd, 0) == -1) {
888 goto cleanup;
889 }
890 implicit_cwd = 1;
891 }
892 }
893 if (searchpaths) {
894 for (i = 0; searchpaths[i]; i++) {
895 /* check for duplicities with the implicit current working directory */
896 if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) {
897 implicit_cwd = 0;
898 continue;
899 }
900 wd = strdup(searchpaths[i]);
901 if (!wd) {
902 LOGMEM(NULL);
903 goto cleanup;
904 } else if (ly_set_add(dirs, wd, 0) == -1) {
905 goto cleanup;
906 }
907 }
908 }
909 wd = NULL;
910
911 /* start searching */
912 while (dirs->count) {
913 free(wd);
914 free(wn); wn = NULL;
915
916 dirs->count--;
917 wd = (char *)dirs->objs[dirs->count];
918 dirs->objs[dirs->count] = NULL;
919 LOGVRB("Searching for \"%s\" in %s.", name, wd);
920
921 if (dir) {
922 closedir(dir);
923 }
924 dir = opendir(wd);
925 dir_len = strlen(wd);
926 if (!dir) {
927 LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno));
928 } else {
929 while ((file = readdir(dir))) {
930 if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) {
931 /* skip . and .. */
932 continue;
933 }
934 free(wn);
935 if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) {
936 LOGMEM(NULL);
937 goto cleanup;
938 }
939 if (stat(wn, &st) == -1) {
940 LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)",
941 file->d_name, wd, strerror(errno));
942 continue;
943 }
944 if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) {
945 /* we have another subdirectory in searchpath to explore,
946 * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */
947 if (ly_set_add(dirs, wn, 0) == -1) {
948 goto cleanup;
949 }
950 /* continue with the next item in current directory */
951 wn = NULL;
952 continue;
953 } else if (!S_ISREG(st.st_mode)) {
954 /* not a regular file (note that we see the target of symlinks instead of symlinks */
955 continue;
956 }
957
958 /* here we know that the item is a file which can contain a module */
959 if (strncmp(name, file->d_name, len) ||
960 (file->d_name[len] != '.' && file->d_name[len] != '@')) {
961 /* different filename than the module we search for */
962 continue;
963 }
964
965 /* get type according to filename suffix */
966 flen = strlen(file->d_name);
967 if (!strcmp(&file->d_name[flen - 4], ".yin")) {
968 format_aux = LYS_IN_YIN;
969 } else if (!strcmp(&file->d_name[flen - 5], ".yang")) {
970 format_aux = LYS_IN_YANG;
971 } else {
972 /* not supportde suffix/file format */
973 continue;
974 }
975
976 if (revision) {
977 /* we look for the specific revision, try to get it from the filename */
978 if (file->d_name[len] == '@') {
979 /* check revision from the filename */
980 if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) {
981 /* another revision */
982 continue;
983 } else {
984 /* exact revision */
985 free(match_name);
986 match_name = wn;
987 wn = NULL;
988 match_len = dir_len + 1 + len;
989 match_format = format_aux;
990 goto success;
991 }
992 } else {
993 /* continue trying to find exact revision match, use this only if not found */
994 free(match_name);
995 match_name = wn;
996 wn = NULL;
997 match_len = dir_len + 1 +len;
998 match_format = format_aux;
999 continue;
1000 }
1001 } else {
1002 /* remember the revision and try to find the newest one */
1003 if (match_name) {
1004 if (file->d_name[len] != '@' ||
1005 lysp_check_date(NULL, &file->d_name[len + 1], flen - (format_aux == LYS_IN_YANG ? 5 : 4) - len - 1, NULL)) {
1006 continue;
1007 } else if (match_name[match_len] == '@' &&
1008 (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) {
1009 continue;
1010 }
1011 free(match_name);
1012 }
1013
1014 match_name = wn;
1015 wn = NULL;
1016 match_len = dir_len + 1 + len;
1017 match_format = format_aux;
1018 continue;
1019 }
1020 }
1021 }
1022 }
1023
1024success:
1025 (*localfile) = match_name;
1026 match_name = NULL;
1027 if (format) {
1028 (*format) = match_format;
1029 }
1030 ret = EXIT_SUCCESS;
1031
1032cleanup:
1033 free(wn);
1034 free(wd);
1035 if (dir) {
1036 closedir(dir);
1037 }
1038 free(match_name);
1039 ly_set_free(dirs, free);
1040
1041 return ret;
1042}
1043