Radek Krejci | 3f5e3db | 2018-10-11 15:57:47 +0200 | [diff] [blame] | 1 | /** |
| 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 Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 14 | |
| 15 | #include "common.h" |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 16 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 17 | #include <ctype.h> |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 18 | #include <dirent.h> |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 19 | #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 Krejci | 3f5e3db | 2018-10-11 15:57:47 +0200 | [diff] [blame] | 27 | |
| 28 | #include "libyang.h" |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 29 | #include "context.h" |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 30 | #include "tree_schema_internal.h" |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 31 | #include "xpath.h" |
Radek Krejci | 3f5e3db | 2018-10-11 15:57:47 +0200 | [diff] [blame] | 32 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 33 | API int |
| 34 | lysc_feature_value(const struct lysc_feature *feature) |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 35 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 36 | LY_CHECK_ARG_RET(NULL, feature, -1); |
| 37 | return feature->flags & LYS_FENABLED ? 1 : 0; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | static uint8_t |
| 41 | iff_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 Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 53 | static int |
| 54 | lysc_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 Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 65 | return lysc_feature_value(iff->features[(*index_f)++]); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 66 | 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 | |
| 83 | API int |
| 84 | lysc_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 Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 96 | /** |
| 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 | */ |
| 110 | static LY_ERR |
| 111 | lys_feature_change(const struct lysc_module *mod, const char *name, int value) |
| 112 | { |
| 113 | int all = 0; |
Radek Krejci | ca3db00 | 2018-11-01 10:31:01 +0100 | [diff] [blame] | 114 | unsigned int u, changed_count, disabled_count; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 115 | 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 Krejci | ca3db00 | 2018-11-01 10:31:01 +0100 | [diff] [blame] | 129 | changed_count = 0; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 130 | |
Radek Krejci | ca3db00 | 2018-11-01 10:31:01 +0100 | [diff] [blame] | 131 | run: |
| 132 | for (disabled_count = u = 0; u < LY_ARRAY_SIZE(mod->features); ++u) { |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 133 | f = &mod->features[u]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 134 | 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 Krejci | ca3db00 | 2018-11-01 10:31:01 +0100 | [diff] [blame] | 151 | ++disabled_count; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 152 | 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 | } |
| 177 | next: |
| 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 Krejci | ca3db00 | 2018-11-01 10:31:01 +0100 | [diff] [blame] | 187 | 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 Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 215 | /* 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 | |
| 243 | API LY_ERR |
| 244 | lys_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 | |
| 251 | API LY_ERR |
| 252 | lys_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 | |
| 259 | API int |
| 260 | lys_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 Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 271 | f = &mod->features[u]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 272 | 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 Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 285 | static void |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 286 | lys_latest_switch(struct lys_module *old, struct lysp_module *new) |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 287 | { |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 288 | if (old->parsed) { |
| 289 | new->latest_revision = old->parsed->latest_revision; |
| 290 | old->parsed->latest_revision = 0; |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 291 | } |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 292 | if (old->compiled) { |
| 293 | new->latest_revision = old->parsed->latest_revision; |
| 294 | old->compiled->latest_revision = 0; |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 298 | struct lys_module * |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 299 | lys_parse_mem_(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, int implement, struct ly_parser_ctx *main_ctx, |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 300 | LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, void *data), void *check_data) |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 301 | { |
Radek Krejci | 6d6e4e4 | 2018-10-29 13:28:19 +0100 | [diff] [blame] | 302 | struct lys_module *mod = NULL, *latest, *mod_dup; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 303 | struct lysp_module *latest_p; |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 304 | struct lysp_import *imp; |
| 305 | struct lysp_include *inc; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 306 | LY_ERR ret = LY_EINVAL; |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 307 | unsigned int u, i; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 308 | struct ly_parser_ctx context = {0}; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 309 | |
| 310 | LY_CHECK_ARG_RET(ctx, ctx, data, NULL); |
| 311 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 312 | context.ctx = ctx; |
| 313 | context.line = 1; |
| 314 | |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 315 | 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 Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 321 | 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 Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 331 | ret = yang_parse(&context, data, &mod->parsed); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 332 | break; |
| 333 | default: |
| 334 | LOGERR(ctx, LY_EINVAL, "Invalid schema input format."); |
| 335 | break; |
| 336 | } |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 337 | LY_CHECK_ERR_RET(ret, free(mod), NULL); |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 338 | |
| 339 | /* make sure that the newest revision is at position 0 */ |
| 340 | lysp_sort_revisions(mod->parsed->revs); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 341 | |
| 342 | if (implement) { |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 343 | /* mark the loaded module implemented */ |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 344 | 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 Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 346 | goto error; |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 347 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 348 | mod->parsed->implemented = 1; |
| 349 | } |
| 350 | |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 351 | if (custom_check) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 352 | LY_CHECK_GOTO(custom_check(ctx, mod->parsed, check_data), error); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 353 | } |
| 354 | |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 355 | if (mod->parsed->submodule) { /* submodule */ |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 356 | 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 Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 361 | /* 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 Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 367 | mod->parsed->latest_revision = latest_p->latest_revision; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 368 | latest_p->latest_revision = 0; |
| 369 | } else { |
| 370 | if (strcmp(mod->parsed->revs[0].date, latest_p->revs[0].date) > 0) { |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 371 | mod->parsed->latest_revision = latest_p->latest_revision; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 372 | latest_p->latest_revision = 0; |
| 373 | } |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 374 | } |
| 375 | } |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 376 | } else { |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 377 | mod->parsed->latest_revision = 1; |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 378 | } |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 379 | /* 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 Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 382 | } else { /* module */ |
| 383 | /* check for duplicity in the context */ |
Radek Krejci | 6d6e4e4 | 2018-10-29 13:28:19 +0100 | [diff] [blame] | 384 | 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 Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 395 | goto error; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 396 | } else { |
Radek Krejci | 6d6e4e4 | 2018-10-29 13:28:19 +0100 | [diff] [blame] | 397 | /* 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 Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 402 | } |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 403 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 404 | |
| 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 Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 419 | /* 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 Krejci | f8f882a | 2018-10-31 14:51:15 +0100 | [diff] [blame] | 423 | if ((latest->parsed && !latest->parsed->revs) || (!latest->parsed && !latest->compiled->revision)) { |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 424 | /* latest has no revision, so mod is anyway newer */ |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 425 | lys_latest_switch(latest, mod->parsed); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 426 | } else { |
Radek Krejci | f8f882a | 2018-10-31 14:51:15 +0100 | [diff] [blame] | 427 | if (strcmp(mod->parsed->revs[0].date, latest->parsed ? latest->parsed->revs[0].date : latest->compiled->revision) > 0) { |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 428 | lys_latest_switch(latest, mod->parsed); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 429 | } |
| 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 Krejci | 6d6e4e4 | 2018-10-29 13:28:19 +0100 | [diff] [blame] | 439 | finish_parsing: |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 440 | /* resolve imports */ |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 441 | mod->parsed->parsing = 1; |
| 442 | LY_ARRAY_FOR(mod->parsed->imports, u) { |
| 443 | imp = &mod->parsed->imports[u]; |
Radek Krejci | 6d6e4e4 | 2018-10-29 13:28:19 +0100 | [diff] [blame] | 444 | if (!imp->module && lysp_load_module(ctx, imp->name, imp->rev[0] ? imp->rev : NULL, 0, 0, &imp->module)) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 445 | goto error_ctx; |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 446 | } |
| 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 Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 451 | goto error_ctx; |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | } |
| 455 | LY_ARRAY_FOR(mod->parsed->includes, u) { |
| 456 | inc = &mod->parsed->includes[u]; |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 457 | if (!inc->submodule && lysp_load_submodule(&context, mod->parsed, inc)) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 458 | goto error_ctx; |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | mod->parsed->parsing = 0; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 462 | |
| 463 | /* check name collisions - typedefs and groupings */ |
| 464 | LY_CHECK_GOTO(lysp_check_typedefs(&context), error_ctx); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 465 | } |
| 466 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 467 | return mod; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 468 | |
| 469 | error_ctx: |
| 470 | ly_set_rm(&ctx->list, mod, NULL); |
| 471 | error: |
| 472 | lys_module_free(mod, NULL); |
| 473 | ly_set_erase(&context.tpdfs_nodes, NULL); |
| 474 | return NULL; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 475 | } |
| 476 | |
Radek Krejci | d14e969 | 2018-11-01 11:00:37 +0100 | [diff] [blame] | 477 | API struct lys_module * |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 478 | lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format) |
| 479 | { |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 480 | return lys_parse_mem_(ctx, data, format, 1, NULL, NULL, NULL); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | static void |
| 484 | lys_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 Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 506 | struct lys_module * |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 507 | lys_parse_fd_(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, int implement, struct ly_parser_ctx *main_ctx, |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 508 | LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, void *data), void *check_data) |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 509 | { |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 510 | struct lys_module *mod; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 511 | 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 Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 526 | mod = lys_parse_mem_(ctx, addr, format, implement, main_ctx, custom_check, check_data); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 527 | 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 Krejci | d14e969 | 2018-11-01 11:00:37 +0100 | [diff] [blame] | 536 | API struct lys_module * |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 537 | lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format) |
| 538 | { |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 539 | return lys_parse_fd_(ctx, fd, format, 1, NULL, NULL, NULL); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 540 | } |
| 541 | |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 542 | struct lys_module * |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 543 | lys_parse_path_(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, int implement, struct ly_parser_ctx *main_ctx, |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 544 | LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, void *data), void *check_data) |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 545 | { |
| 546 | int fd; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 547 | struct lys_module *mod; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 548 | 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 Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 556 | mod = lys_parse_fd_(ctx, fd, format, implement, main_ctx, custom_check, check_data); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 557 | 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 Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 578 | if (!mod->parsed->revs || len != 10 || strncmp(mod->parsed->revs[0].date, rev, len)) { |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 579 | LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename, |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 580 | mod->parsed->revs ? mod->parsed->revs[0].date : "none"); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 581 | } |
| 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 Krejci | d14e969 | 2018-11-01 11:00:37 +0100 | [diff] [blame] | 597 | API struct lys_module * |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 598 | lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format) |
| 599 | { |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 600 | return lys_parse_path_(ctx, path, format, 1, NULL, NULL, NULL); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | API LY_ERR |
| 604 | lys_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 | |
| 773 | success: |
| 774 | (*localfile) = match_name; |
| 775 | match_name = NULL; |
| 776 | if (format) { |
| 777 | (*format) = match_format; |
| 778 | } |
| 779 | ret = EXIT_SUCCESS; |
| 780 | |
| 781 | cleanup: |
| 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 | |