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 | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame^] | 33 | API const struct lysc_node * |
| 34 | lys_getnext(const struct lysc_node *last, const struct lysc_node *parent, const struct lysc_module *module, int options) |
| 35 | { |
| 36 | const struct lysc_node *next; |
| 37 | struct lysc_node **snode; |
| 38 | |
| 39 | LY_CHECK_ARG_RET(NULL, parent || module, NULL); |
| 40 | |
| 41 | if (!last) { |
| 42 | /* first call */ |
| 43 | |
| 44 | /* get know where to start */ |
| 45 | if (parent) { |
| 46 | /* schema subtree */ |
| 47 | snode = lysc_node_children(parent); |
| 48 | /* do not return anything if the augment does not have any children */ |
| 49 | if (!snode || !(*snode)) { |
| 50 | return NULL; |
| 51 | } |
| 52 | next = last = *snode; |
| 53 | } else { |
| 54 | /* top level data */ |
| 55 | next = last = module->data; |
| 56 | } |
| 57 | if (!next) { |
| 58 | return next; |
| 59 | } else if (!(options & LYS_GETNEXT_NOSTATECHECK)) { |
| 60 | if (!lys_is_disabled(next, 0)) { |
| 61 | return next; |
| 62 | } |
| 63 | /* continue to find next available node */ |
| 64 | } else { |
| 65 | return next; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | next = last->next; |
| 70 | repeat: |
| 71 | if (!next) { |
| 72 | return next; |
| 73 | } |
| 74 | switch (next->nodetype) { |
| 75 | case LYS_ACTION: |
| 76 | case LYS_NOTIF: |
| 77 | case LYS_LEAF: |
| 78 | case LYS_ANYXML: |
| 79 | case LYS_ANYDATA: |
| 80 | case LYS_LIST: |
| 81 | case LYS_LEAFLIST: |
| 82 | break; |
| 83 | case LYS_CONTAINER: |
| 84 | if (!(((struct lysc_node_container *)next)->flags & LYS_PRESENCE) && (options & LYS_GETNEXT_INTONPCONT)) { |
| 85 | if (((struct lysc_node_container *)next)->child) { |
| 86 | /* go into */ |
| 87 | next = ((struct lysc_node_container *)next)->child; |
| 88 | } else { |
| 89 | next = next->next; |
| 90 | } |
| 91 | goto repeat; |
| 92 | } |
| 93 | break; |
| 94 | case LYS_CHOICE: |
| 95 | if (options & LYS_GETNEXT_WITHCHOICE) { |
| 96 | return next; |
| 97 | } else if (((struct lysc_node_choice *)next)->cases) { |
| 98 | /* go into */ |
| 99 | next = ((struct lysc_node_choice *)next)->cases[0].child; |
| 100 | } else { |
| 101 | next = next->next; |
| 102 | } |
| 103 | goto repeat; |
| 104 | default: |
| 105 | /* we should not be here */ |
| 106 | LOGINT(last->module->compiled->ctx); |
| 107 | return NULL; |
| 108 | } |
| 109 | |
| 110 | if (!(options & LYS_GETNEXT_NOSTATECHECK)) { |
| 111 | /* check if the node is disabled by if-feature */ |
| 112 | if (lys_is_disabled(next, 0)) { |
| 113 | next = next->next; |
| 114 | goto repeat; |
| 115 | } |
| 116 | } |
| 117 | return next; |
| 118 | } |
| 119 | |
| 120 | API const struct lysc_node * |
| 121 | lys_child(const struct lysc_node *parent, const struct lys_module *module, |
| 122 | const char *name, size_t name_len, uint16_t nodetype, int options) |
| 123 | { |
| 124 | const struct lysc_node *node = NULL; |
| 125 | |
| 126 | LY_CHECK_ARG_RET(NULL, module, name, NULL); |
| 127 | if (!nodetype) { |
| 128 | nodetype = 0xffff; |
| 129 | } |
| 130 | |
| 131 | while ((node = lys_getnext(node, parent, module->compiled, options))) { |
| 132 | if (!(node->nodetype & nodetype)) { |
| 133 | continue; |
| 134 | } |
| 135 | if (node->module != module) { |
| 136 | continue; |
| 137 | } |
| 138 | |
| 139 | if (name_len) { |
| 140 | if (!strncmp(node->name, name, name_len) && !node->name[name_len]) { |
| 141 | return node; |
| 142 | } |
| 143 | } else { |
| 144 | if (!strcmp(node->name, name)) { |
| 145 | return node; |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | return NULL; |
| 150 | } |
| 151 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 152 | API int |
| 153 | lysc_feature_value(const struct lysc_feature *feature) |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 154 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 155 | LY_CHECK_ARG_RET(NULL, feature, -1); |
| 156 | return feature->flags & LYS_FENABLED ? 1 : 0; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | static uint8_t |
| 160 | iff_getop(uint8_t *list, int pos) |
| 161 | { |
| 162 | uint8_t *item; |
| 163 | uint8_t mask = 3, result; |
| 164 | |
| 165 | assert(pos >= 0); |
| 166 | |
| 167 | item = &list[pos / 4]; |
| 168 | result = (*item) & (mask << 2 * (pos % 4)); |
| 169 | return result >> 2 * (pos % 4); |
| 170 | } |
| 171 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 172 | static int |
| 173 | lysc_iffeature_value_(const struct lysc_iffeature *iff, int *index_e, int *index_f) |
| 174 | { |
| 175 | uint8_t op; |
| 176 | int a, b; |
| 177 | |
| 178 | op = iff_getop(iff->expr, *index_e); |
| 179 | (*index_e)++; |
| 180 | |
| 181 | switch (op) { |
| 182 | case LYS_IFF_F: |
| 183 | /* resolve feature */ |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 184 | return lysc_feature_value(iff->features[(*index_f)++]); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 185 | case LYS_IFF_NOT: |
| 186 | /* invert result */ |
| 187 | return lysc_iffeature_value_(iff, index_e, index_f) ? 0 : 1; |
| 188 | case LYS_IFF_AND: |
| 189 | case LYS_IFF_OR: |
| 190 | a = lysc_iffeature_value_(iff, index_e, index_f); |
| 191 | b = lysc_iffeature_value_(iff, index_e, index_f); |
| 192 | if (op == LYS_IFF_AND) { |
| 193 | return a && b; |
| 194 | } else { /* LYS_IFF_OR */ |
| 195 | return a || b; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | API int |
| 203 | lysc_iffeature_value(const struct lysc_iffeature *iff) |
| 204 | { |
| 205 | int index_e = 0, index_f = 0; |
| 206 | |
| 207 | LY_CHECK_ARG_RET(NULL, iff, -1); |
| 208 | |
| 209 | if (iff->expr) { |
| 210 | return lysc_iffeature_value_(iff, &index_e, &index_f); |
| 211 | } |
| 212 | return 0; |
| 213 | } |
| 214 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 215 | /** |
| 216 | * @brief Enable/Disable the specified feature in the module. |
| 217 | * |
| 218 | * If the feature is already set to the desired value, LY_SUCCESS is returned. |
| 219 | * By changing the feature, also all the feature which depends on it via their |
| 220 | * if-feature statements are again evaluated (disabled if a if-feature statemen |
| 221 | * evaluates to false). |
| 222 | * |
| 223 | * @param[in] mod Compiled module where to set (search for) the feature. |
| 224 | * @param[in] name Name of the feature to set. Asterisk ('*') can be used to |
| 225 | * set all the features in the module. |
| 226 | * @param[in] value Desired value of the feature: 1 (enable) or 0 (disable). |
| 227 | * @return LY_ERR value. |
| 228 | */ |
| 229 | static LY_ERR |
| 230 | lys_feature_change(const struct lysc_module *mod, const char *name, int value) |
| 231 | { |
| 232 | int all = 0; |
Radek Krejci | ca3db00 | 2018-11-01 10:31:01 +0100 | [diff] [blame] | 233 | unsigned int u, changed_count, disabled_count; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 234 | struct lysc_feature *f, **df; |
| 235 | struct lysc_iffeature *iff; |
| 236 | struct ly_set *changed; |
| 237 | |
| 238 | if (!mod->features) { |
| 239 | LOGERR(mod->ctx, LY_EINVAL, "Unable to switch feature since the module \"%s\" has no features.", mod->name); |
| 240 | return LY_EINVAL; |
| 241 | } |
| 242 | |
| 243 | if (!strcmp(name, "*")) { |
| 244 | /* enable all */ |
| 245 | all = 1; |
| 246 | } |
| 247 | changed = ly_set_new(); |
Radek Krejci | ca3db00 | 2018-11-01 10:31:01 +0100 | [diff] [blame] | 248 | changed_count = 0; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 249 | |
Radek Krejci | ca3db00 | 2018-11-01 10:31:01 +0100 | [diff] [blame] | 250 | run: |
| 251 | for (disabled_count = u = 0; u < LY_ARRAY_SIZE(mod->features); ++u) { |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 252 | f = &mod->features[u]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 253 | if (all || !strcmp(f->name, name)) { |
| 254 | if ((value && (f->flags & LYS_FENABLED)) || (!value && !(f->flags & LYS_FENABLED))) { |
| 255 | if (all) { |
| 256 | /* skip already set features */ |
| 257 | continue; |
| 258 | } else { |
| 259 | /* feature already set correctly */ |
| 260 | ly_set_free(changed, NULL); |
| 261 | return LY_SUCCESS; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | if (value) { /* enable */ |
| 266 | /* check referenced features if they are enabled */ |
| 267 | LY_ARRAY_FOR(f->iffeatures, struct lysc_iffeature, iff) { |
| 268 | if (!lysc_iffeature_value(iff)) { |
| 269 | if (all) { |
Radek Krejci | ca3db00 | 2018-11-01 10:31:01 +0100 | [diff] [blame] | 270 | ++disabled_count; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 271 | goto next; |
| 272 | } else { |
| 273 | LOGERR(mod->ctx, LY_EDENIED, |
| 274 | "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).", |
| 275 | f->name); |
| 276 | ly_set_free(changed, NULL); |
| 277 | return LY_EDENIED; |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | /* enable the feature */ |
| 282 | f->flags |= LYS_FENABLED; |
| 283 | } else { /* disable */ |
| 284 | /* disable the feature */ |
| 285 | f->flags &= ~LYS_FENABLED; |
| 286 | } |
| 287 | |
| 288 | /* remember the changed feature */ |
| 289 | ly_set_add(changed, f, LY_SET_OPT_USEASLIST); |
| 290 | |
| 291 | if (!all) { |
| 292 | /* stop in case changing a single feature */ |
| 293 | break; |
| 294 | } |
| 295 | } |
| 296 | next: |
| 297 | ; |
| 298 | } |
| 299 | |
| 300 | if (!all && !changed->count) { |
| 301 | LOGERR(mod->ctx, LY_EINVAL, "Feature \"%s\" not found in module \"%s\".", name, mod->name); |
| 302 | ly_set_free(changed, NULL); |
| 303 | return LY_EINVAL; |
| 304 | } |
| 305 | |
Radek Krejci | ca3db00 | 2018-11-01 10:31:01 +0100 | [diff] [blame] | 306 | if (value && all && disabled_count) { |
| 307 | if (changed_count == changed->count) { |
| 308 | /* no change in last run -> not able to enable all ... */ |
| 309 | /* ... print errors */ |
| 310 | for (u = 0; disabled_count && u < LY_ARRAY_SIZE(mod->features); ++u) { |
| 311 | if (!(mod->features[u].flags & LYS_FENABLED)) { |
| 312 | LOGERR(mod->ctx, LY_EDENIED, |
| 313 | "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).", |
| 314 | mod->features[u].name); |
| 315 | --disabled_count; |
| 316 | } |
| 317 | } |
| 318 | /* ... restore the original state */ |
| 319 | for (u = 0; u < changed->count; ++u) { |
| 320 | f = changed->objs[u]; |
| 321 | /* re-disable the feature */ |
| 322 | f->flags &= ~LYS_FENABLED; |
| 323 | } |
| 324 | |
| 325 | ly_set_free(changed, NULL); |
| 326 | return LY_EDENIED; |
| 327 | } else { |
| 328 | /* we did some change in last run, try it again */ |
| 329 | changed_count = changed->count; |
| 330 | goto run; |
| 331 | } |
| 332 | } |
| 333 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 334 | /* reflect change(s) in the dependent features */ |
| 335 | for (u = 0; u < changed->count; ++u) { |
| 336 | /* If a dependent feature is enabled, it can be now changed by the change (to false) of the value of |
| 337 | * its if-feature statements. The reverse logic, automatically enable feature when its feature is enabled |
| 338 | * is not done - by default, features are disabled and must be explicitely enabled. */ |
| 339 | f = changed->objs[u]; |
| 340 | LY_ARRAY_FOR(f->depfeatures, struct lysc_feature*, df) { |
| 341 | if (!((*df)->flags & LYS_FENABLED)) { |
| 342 | /* not enabled, nothing to do */ |
| 343 | continue; |
| 344 | } |
| 345 | /* check the feature's if-features which could change by the previous change of our feature */ |
| 346 | LY_ARRAY_FOR((*df)->iffeatures, struct lysc_iffeature, iff) { |
| 347 | if (!lysc_iffeature_value(iff)) { |
| 348 | /* the feature must be disabled now */ |
| 349 | (*df)->flags &= ~LYS_FENABLED; |
| 350 | /* add the feature into the list of changed features */ |
| 351 | ly_set_add(changed, *df, LY_SET_OPT_USEASLIST); |
| 352 | break; |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | ly_set_free(changed, NULL); |
| 359 | return LY_SUCCESS; |
| 360 | } |
| 361 | |
| 362 | API LY_ERR |
| 363 | lys_feature_enable(struct lys_module *module, const char *feature) |
| 364 | { |
| 365 | LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, LY_EINVAL); |
| 366 | |
| 367 | return lys_feature_change(module->compiled, feature, 1); |
| 368 | } |
| 369 | |
| 370 | API LY_ERR |
| 371 | lys_feature_disable(struct lys_module *module, const char *feature) |
| 372 | { |
| 373 | LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, LY_EINVAL); |
| 374 | |
| 375 | return lys_feature_change(module->compiled, feature, 0); |
| 376 | } |
| 377 | |
| 378 | API int |
| 379 | lys_feature_value(const struct lys_module *module, const char *feature) |
| 380 | { |
| 381 | struct lysc_feature *f; |
| 382 | struct lysc_module *mod; |
| 383 | unsigned int u; |
| 384 | |
| 385 | LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, -1); |
| 386 | mod = module->compiled; |
| 387 | |
| 388 | /* search for the specified feature */ |
| 389 | for (u = 0; u < LY_ARRAY_SIZE(mod->features); ++u) { |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 390 | f = &mod->features[u]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 391 | if (!strcmp(f->name, feature)) { |
| 392 | if (f->flags & LYS_FENABLED) { |
| 393 | return 1; |
| 394 | } else { |
| 395 | return 0; |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | /* feature definition not found */ |
| 401 | return -1; |
| 402 | } |
| 403 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame^] | 404 | API const struct lysc_iffeature * |
| 405 | lys_is_disabled(const struct lysc_node *node, int recursive) |
| 406 | { |
| 407 | unsigned int u; |
| 408 | struct lysc_iffeature **iff; |
| 409 | |
| 410 | LY_CHECK_ARG_RET(NULL, node, NULL); |
| 411 | |
| 412 | while(node) { |
| 413 | if (node->nodetype & LYS_CHOICE) { |
| 414 | return NULL; |
| 415 | } |
| 416 | |
| 417 | iff = lysc_node_iff(node); |
| 418 | if (iff && *iff) { |
| 419 | /* check local if-features */ |
| 420 | LY_ARRAY_FOR(*iff, u) { |
| 421 | if (!lysc_iffeature_value(&(*iff)[u])) { |
| 422 | return &(*iff)[u]; |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | if (!recursive) { |
| 428 | return NULL; |
| 429 | } |
| 430 | |
| 431 | /* go through parents */ |
| 432 | node = node->parent; |
| 433 | } |
| 434 | return NULL; |
| 435 | } |
| 436 | |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 437 | static void |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 438 | lys_latest_switch(struct lys_module *old, struct lysp_module *new) |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 439 | { |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 440 | if (old->parsed) { |
| 441 | new->latest_revision = old->parsed->latest_revision; |
| 442 | old->parsed->latest_revision = 0; |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 443 | } |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 444 | if (old->compiled) { |
| 445 | new->latest_revision = old->parsed->latest_revision; |
| 446 | old->compiled->latest_revision = 0; |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 450 | struct lys_module * |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 451 | 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] | 452 | 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] | 453 | { |
Radek Krejci | 6d6e4e4 | 2018-10-29 13:28:19 +0100 | [diff] [blame] | 454 | struct lys_module *mod = NULL, *latest, *mod_dup; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 455 | struct lysp_module *latest_p; |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 456 | struct lysp_import *imp; |
| 457 | struct lysp_include *inc; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 458 | LY_ERR ret = LY_EINVAL; |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 459 | unsigned int u, i; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 460 | struct ly_parser_ctx context = {0}; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 461 | |
| 462 | LY_CHECK_ARG_RET(ctx, ctx, data, NULL); |
| 463 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 464 | context.ctx = ctx; |
| 465 | context.line = 1; |
| 466 | |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 467 | if (main_ctx) { |
| 468 | /* map the typedefs and groupings list from main context to the submodule's context */ |
| 469 | memcpy(&context.tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes); |
| 470 | memcpy(&context.grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes); |
| 471 | } |
| 472 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 473 | mod = calloc(1, sizeof *mod); |
| 474 | LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), NULL); |
| 475 | |
| 476 | switch (format) { |
| 477 | case LYS_IN_YIN: |
| 478 | /* TODO not yet supported |
| 479 | mod = yin_read_module(ctx, data, revision, implement); |
| 480 | */ |
| 481 | break; |
| 482 | case LYS_IN_YANG: |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 483 | ret = yang_parse(&context, data, &mod->parsed); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 484 | break; |
| 485 | default: |
| 486 | LOGERR(ctx, LY_EINVAL, "Invalid schema input format."); |
| 487 | break; |
| 488 | } |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 489 | LY_CHECK_ERR_RET(ret, free(mod), NULL); |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 490 | |
| 491 | /* make sure that the newest revision is at position 0 */ |
| 492 | lysp_sort_revisions(mod->parsed->revs); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 493 | |
| 494 | if (implement) { |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 495 | /* mark the loaded module implemented */ |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 496 | if (ly_ctx_get_module_implemented(ctx, mod->parsed->name)) { |
| 497 | LOGERR(ctx, LY_EDENIED, "Module \"%s\" is already implemented in the context.", mod->parsed->name); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 498 | goto error; |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 499 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 500 | mod->parsed->implemented = 1; |
| 501 | } |
| 502 | |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 503 | if (custom_check) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 504 | LY_CHECK_GOTO(custom_check(ctx, mod->parsed, check_data), error); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 505 | } |
| 506 | |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 507 | if (mod->parsed->submodule) { /* submodule */ |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 508 | if (!main_ctx) { |
| 509 | LOGERR(ctx, LY_EDENIED, "Input data contains submodule \"%s\" which cannot be parsed directly without its main module.", |
| 510 | mod->parsed->name); |
| 511 | goto error; |
| 512 | } |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 513 | /* decide the latest revision */ |
| 514 | latest_p = ly_ctx_get_submodule(ctx, mod->parsed->belongsto, mod->parsed->name, NULL); |
| 515 | if (latest_p) { |
| 516 | if (mod->parsed->revs) { |
| 517 | if (!latest_p->revs) { |
| 518 | /* latest has no revision, so mod is anyway newer */ |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 519 | mod->parsed->latest_revision = latest_p->latest_revision; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 520 | latest_p->latest_revision = 0; |
| 521 | } else { |
| 522 | 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] | 523 | mod->parsed->latest_revision = latest_p->latest_revision; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 524 | latest_p->latest_revision = 0; |
| 525 | } |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 526 | } |
| 527 | } |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 528 | } else { |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 529 | mod->parsed->latest_revision = 1; |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 530 | } |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 531 | /* remap possibly changed and reallocated typedefs and groupings list back to the main context */ |
| 532 | memcpy(&main_ctx->tpdfs_nodes, &context.tpdfs_nodes, sizeof main_ctx->tpdfs_nodes); |
| 533 | memcpy(&main_ctx->grps_nodes, &context.grps_nodes, sizeof main_ctx->grps_nodes); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 534 | } else { /* module */ |
| 535 | /* check for duplicity in the context */ |
Radek Krejci | 6d6e4e4 | 2018-10-29 13:28:19 +0100 | [diff] [blame] | 536 | mod_dup = (struct lys_module*)ly_ctx_get_module(ctx, mod->parsed->name, mod->parsed->revs ? mod->parsed->revs[0].date : NULL); |
| 537 | if (mod_dup) { |
| 538 | if (mod_dup->parsed) { |
| 539 | /* error */ |
| 540 | if (mod->parsed->revs) { |
| 541 | LOGERR(ctx, LY_EEXIST, "Module \"%s\" of revision \"%s\" is already present in the context.", |
| 542 | mod->parsed->name, mod->parsed->revs[0].date); |
| 543 | } else { |
| 544 | LOGERR(ctx, LY_EEXIST, "Module \"%s\" with no revision is already present in the context.", |
| 545 | mod->parsed->name); |
| 546 | } |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 547 | goto error; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 548 | } else { |
Radek Krejci | 6d6e4e4 | 2018-10-29 13:28:19 +0100 | [diff] [blame] | 549 | /* add the parsed data to the currently compiled-only module in the context */ |
| 550 | mod_dup->parsed = mod->parsed; |
| 551 | free(mod); |
| 552 | mod = mod_dup; |
| 553 | goto finish_parsing; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 554 | } |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 555 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 556 | |
| 557 | #if 0 |
| 558 | /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang |
| 559 | * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the |
| 560 | * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have |
| 561 | * the anotation definitions available in the internal schema structure. There is another hack in schema |
| 562 | * printers to do not print this internally added annotation. */ |
| 563 | if (mod && ly_strequal(mod->name, "ietf-netconf", 0)) { |
| 564 | if (lyp_add_ietf_netconf_annotations(mod)) { |
| 565 | lys_free(mod, NULL, 1, 1); |
| 566 | return NULL; |
| 567 | } |
| 568 | } |
| 569 | #endif |
| 570 | |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 571 | /* decide the latest revision */ |
| 572 | latest = (struct lys_module*)ly_ctx_get_module_latest(ctx, mod->parsed->name); |
| 573 | if (latest) { |
| 574 | if (mod->parsed->revs) { |
Radek Krejci | f8f882a | 2018-10-31 14:51:15 +0100 | [diff] [blame] | 575 | if ((latest->parsed && !latest->parsed->revs) || (!latest->parsed && !latest->compiled->revision)) { |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 576 | /* latest has no revision, so mod is anyway newer */ |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 577 | lys_latest_switch(latest, mod->parsed); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 578 | } else { |
Radek Krejci | f8f882a | 2018-10-31 14:51:15 +0100 | [diff] [blame] | 579 | 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] | 580 | lys_latest_switch(latest, mod->parsed); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 581 | } |
| 582 | } |
| 583 | } |
| 584 | } else { |
| 585 | mod->parsed->latest_revision = 1; |
| 586 | } |
| 587 | |
| 588 | /* add into context */ |
| 589 | ly_set_add(&ctx->list, mod, LY_SET_OPT_USEASLIST); |
| 590 | |
Radek Krejci | 6d6e4e4 | 2018-10-29 13:28:19 +0100 | [diff] [blame] | 591 | finish_parsing: |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 592 | /* resolve imports */ |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 593 | mod->parsed->parsing = 1; |
| 594 | LY_ARRAY_FOR(mod->parsed->imports, u) { |
| 595 | imp = &mod->parsed->imports[u]; |
Radek Krejci | 6d6e4e4 | 2018-10-29 13:28:19 +0100 | [diff] [blame] | 596 | 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] | 597 | goto error_ctx; |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 598 | } |
| 599 | /* check for importing the same module twice */ |
| 600 | for (i = 0; i < u; ++i) { |
| 601 | if (imp->module == mod->parsed->imports[i].module) { |
| 602 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Single revision of the module \"%s\" referred twice.", imp->name); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 603 | goto error_ctx; |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 604 | } |
| 605 | } |
| 606 | } |
| 607 | LY_ARRAY_FOR(mod->parsed->includes, u) { |
| 608 | inc = &mod->parsed->includes[u]; |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 609 | if (!inc->submodule && lysp_load_submodule(&context, mod->parsed, inc)) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 610 | goto error_ctx; |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 611 | } |
| 612 | } |
| 613 | mod->parsed->parsing = 0; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 614 | |
| 615 | /* check name collisions - typedefs and groupings */ |
| 616 | LY_CHECK_GOTO(lysp_check_typedefs(&context), error_ctx); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 617 | } |
| 618 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 619 | return mod; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 620 | |
| 621 | error_ctx: |
| 622 | ly_set_rm(&ctx->list, mod, NULL); |
| 623 | error: |
| 624 | lys_module_free(mod, NULL); |
| 625 | ly_set_erase(&context.tpdfs_nodes, NULL); |
| 626 | return NULL; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 627 | } |
| 628 | |
Radek Krejci | d14e969 | 2018-11-01 11:00:37 +0100 | [diff] [blame] | 629 | API struct lys_module * |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 630 | lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format) |
| 631 | { |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 632 | return lys_parse_mem_(ctx, data, format, 1, NULL, NULL, NULL); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | static void |
| 636 | lys_parse_set_filename(struct ly_ctx *ctx, const char **filename, int fd) |
| 637 | { |
| 638 | #ifdef __APPLE__ |
| 639 | char path[MAXPATHLEN]; |
| 640 | #else |
| 641 | int len; |
| 642 | char path[PATH_MAX], proc_path[32]; |
| 643 | #endif |
| 644 | |
| 645 | #ifdef __APPLE__ |
| 646 | if (fcntl(fd, F_GETPATH, path) != -1) { |
| 647 | *filename = lydict_insert(ctx, path, 0); |
| 648 | } |
| 649 | #else |
| 650 | /* get URI if there is /proc */ |
| 651 | sprintf(proc_path, "/proc/self/fd/%d", fd); |
| 652 | if ((len = readlink(proc_path, path, PATH_MAX - 1)) > 0) { |
| 653 | *filename = lydict_insert(ctx, path, len); |
| 654 | } |
| 655 | #endif |
| 656 | } |
| 657 | |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 658 | struct lys_module * |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 659 | 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] | 660 | 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] | 661 | { |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 662 | struct lys_module *mod; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 663 | size_t length; |
| 664 | char *addr; |
| 665 | |
| 666 | LY_CHECK_ARG_RET(ctx, ctx, NULL); |
| 667 | if (fd < 0) { |
| 668 | LOGARG(ctx, fd); |
| 669 | return NULL; |
| 670 | } |
| 671 | |
| 672 | LY_CHECK_RET(ly_mmap(ctx, fd, &length, (void **)&addr), NULL); |
| 673 | if (!addr) { |
| 674 | LOGERR(ctx, LY_EINVAL, "Empty schema file."); |
| 675 | return NULL; |
| 676 | } |
| 677 | |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 678 | 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] | 679 | ly_munmap(addr, length); |
| 680 | |
| 681 | if (mod && !mod->parsed->filepath) { |
| 682 | lys_parse_set_filename(ctx, &mod->parsed->filepath, fd); |
| 683 | } |
| 684 | |
| 685 | return mod; |
| 686 | } |
| 687 | |
Radek Krejci | d14e969 | 2018-11-01 11:00:37 +0100 | [diff] [blame] | 688 | API struct lys_module * |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 689 | lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format) |
| 690 | { |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 691 | return lys_parse_fd_(ctx, fd, format, 1, NULL, NULL, NULL); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 692 | } |
| 693 | |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 694 | struct lys_module * |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 695 | 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] | 696 | 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] | 697 | { |
| 698 | int fd; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 699 | struct lys_module *mod; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 700 | const char *rev, *dot, *filename; |
| 701 | size_t len; |
| 702 | |
| 703 | LY_CHECK_ARG_RET(ctx, ctx, path, NULL); |
| 704 | |
| 705 | fd = open(path, O_RDONLY); |
| 706 | LY_CHECK_ERR_RET(fd == -1, LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)), NULL); |
| 707 | |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 708 | 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] | 709 | close(fd); |
| 710 | LY_CHECK_RET(!mod, NULL); |
| 711 | |
| 712 | /* check that name and revision match filename */ |
| 713 | filename = strrchr(path, '/'); |
| 714 | if (!filename) { |
| 715 | filename = path; |
| 716 | } else { |
| 717 | filename++; |
| 718 | } |
| 719 | rev = strchr(filename, '@'); |
| 720 | dot = strrchr(filename, '.'); |
| 721 | |
| 722 | /* name */ |
| 723 | len = strlen(mod->parsed->name); |
| 724 | if (strncmp(filename, mod->parsed->name, len) || |
| 725 | ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) { |
| 726 | LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->parsed->name); |
| 727 | } |
| 728 | if (rev) { |
| 729 | len = dot - ++rev; |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 730 | 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] | 731 | LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename, |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 732 | mod->parsed->revs ? mod->parsed->revs[0].date : "none"); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 733 | } |
| 734 | } |
| 735 | |
| 736 | if (!mod->parsed->filepath) { |
| 737 | /* store URI */ |
| 738 | char rpath[PATH_MAX]; |
| 739 | if (realpath(path, rpath) != NULL) { |
| 740 | mod->parsed->filepath = lydict_insert(ctx, rpath, 0); |
| 741 | } else { |
| 742 | mod->parsed->filepath = lydict_insert(ctx, path, 0); |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | return mod; |
| 747 | } |
| 748 | |
Radek Krejci | d14e969 | 2018-11-01 11:00:37 +0100 | [diff] [blame] | 749 | API struct lys_module * |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 750 | lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format) |
| 751 | { |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 752 | return lys_parse_path_(ctx, path, format, 1, NULL, NULL, NULL); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | API LY_ERR |
| 756 | lys_search_localfile(const char * const *searchpaths, int cwd, const char *name, const char *revision, |
| 757 | char **localfile, LYS_INFORMAT *format) |
| 758 | { |
| 759 | size_t len, flen, match_len = 0, dir_len; |
| 760 | int i, implicit_cwd = 0, ret = EXIT_FAILURE; |
| 761 | char *wd, *wn = NULL; |
| 762 | DIR *dir = NULL; |
| 763 | struct dirent *file; |
| 764 | char *match_name = NULL; |
| 765 | LYS_INFORMAT format_aux, match_format = 0; |
| 766 | struct ly_set *dirs; |
| 767 | struct stat st; |
| 768 | |
| 769 | LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL); |
| 770 | |
| 771 | /* start to fill the dir fifo with the context's search path (if set) |
| 772 | * and the current working directory */ |
| 773 | dirs = ly_set_new(); |
| 774 | if (!dirs) { |
| 775 | LOGMEM(NULL); |
| 776 | return EXIT_FAILURE; |
| 777 | } |
| 778 | |
| 779 | len = strlen(name); |
| 780 | if (cwd) { |
| 781 | wd = get_current_dir_name(); |
| 782 | if (!wd) { |
| 783 | LOGMEM(NULL); |
| 784 | goto cleanup; |
| 785 | } else { |
| 786 | /* add implicit current working directory (./) to be searched, |
| 787 | * this directory is not searched recursively */ |
| 788 | if (ly_set_add(dirs, wd, 0) == -1) { |
| 789 | goto cleanup; |
| 790 | } |
| 791 | implicit_cwd = 1; |
| 792 | } |
| 793 | } |
| 794 | if (searchpaths) { |
| 795 | for (i = 0; searchpaths[i]; i++) { |
| 796 | /* check for duplicities with the implicit current working directory */ |
| 797 | if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) { |
| 798 | implicit_cwd = 0; |
| 799 | continue; |
| 800 | } |
| 801 | wd = strdup(searchpaths[i]); |
| 802 | if (!wd) { |
| 803 | LOGMEM(NULL); |
| 804 | goto cleanup; |
| 805 | } else if (ly_set_add(dirs, wd, 0) == -1) { |
| 806 | goto cleanup; |
| 807 | } |
| 808 | } |
| 809 | } |
| 810 | wd = NULL; |
| 811 | |
| 812 | /* start searching */ |
| 813 | while (dirs->count) { |
| 814 | free(wd); |
| 815 | free(wn); wn = NULL; |
| 816 | |
| 817 | dirs->count--; |
| 818 | wd = (char *)dirs->objs[dirs->count]; |
| 819 | dirs->objs[dirs->count] = NULL; |
| 820 | LOGVRB("Searching for \"%s\" in %s.", name, wd); |
| 821 | |
| 822 | if (dir) { |
| 823 | closedir(dir); |
| 824 | } |
| 825 | dir = opendir(wd); |
| 826 | dir_len = strlen(wd); |
| 827 | if (!dir) { |
| 828 | LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno)); |
| 829 | } else { |
| 830 | while ((file = readdir(dir))) { |
| 831 | if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) { |
| 832 | /* skip . and .. */ |
| 833 | continue; |
| 834 | } |
| 835 | free(wn); |
| 836 | if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) { |
| 837 | LOGMEM(NULL); |
| 838 | goto cleanup; |
| 839 | } |
| 840 | if (stat(wn, &st) == -1) { |
| 841 | LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)", |
| 842 | file->d_name, wd, strerror(errno)); |
| 843 | continue; |
| 844 | } |
| 845 | if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) { |
| 846 | /* we have another subdirectory in searchpath to explore, |
| 847 | * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */ |
| 848 | if (ly_set_add(dirs, wn, 0) == -1) { |
| 849 | goto cleanup; |
| 850 | } |
| 851 | /* continue with the next item in current directory */ |
| 852 | wn = NULL; |
| 853 | continue; |
| 854 | } else if (!S_ISREG(st.st_mode)) { |
| 855 | /* not a regular file (note that we see the target of symlinks instead of symlinks */ |
| 856 | continue; |
| 857 | } |
| 858 | |
| 859 | /* here we know that the item is a file which can contain a module */ |
| 860 | if (strncmp(name, file->d_name, len) || |
| 861 | (file->d_name[len] != '.' && file->d_name[len] != '@')) { |
| 862 | /* different filename than the module we search for */ |
| 863 | continue; |
| 864 | } |
| 865 | |
| 866 | /* get type according to filename suffix */ |
| 867 | flen = strlen(file->d_name); |
| 868 | if (!strcmp(&file->d_name[flen - 4], ".yin")) { |
| 869 | format_aux = LYS_IN_YIN; |
| 870 | } else if (!strcmp(&file->d_name[flen - 5], ".yang")) { |
| 871 | format_aux = LYS_IN_YANG; |
| 872 | } else { |
| 873 | /* not supportde suffix/file format */ |
| 874 | continue; |
| 875 | } |
| 876 | |
| 877 | if (revision) { |
| 878 | /* we look for the specific revision, try to get it from the filename */ |
| 879 | if (file->d_name[len] == '@') { |
| 880 | /* check revision from the filename */ |
| 881 | if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) { |
| 882 | /* another revision */ |
| 883 | continue; |
| 884 | } else { |
| 885 | /* exact revision */ |
| 886 | free(match_name); |
| 887 | match_name = wn; |
| 888 | wn = NULL; |
| 889 | match_len = dir_len + 1 + len; |
| 890 | match_format = format_aux; |
| 891 | goto success; |
| 892 | } |
| 893 | } else { |
| 894 | /* continue trying to find exact revision match, use this only if not found */ |
| 895 | free(match_name); |
| 896 | match_name = wn; |
| 897 | wn = NULL; |
| 898 | match_len = dir_len + 1 +len; |
| 899 | match_format = format_aux; |
| 900 | continue; |
| 901 | } |
| 902 | } else { |
| 903 | /* remember the revision and try to find the newest one */ |
| 904 | if (match_name) { |
| 905 | if (file->d_name[len] != '@' || |
| 906 | lysp_check_date(NULL, &file->d_name[len + 1], flen - (format_aux == LYS_IN_YANG ? 5 : 4) - len - 1, NULL)) { |
| 907 | continue; |
| 908 | } else if (match_name[match_len] == '@' && |
| 909 | (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) { |
| 910 | continue; |
| 911 | } |
| 912 | free(match_name); |
| 913 | } |
| 914 | |
| 915 | match_name = wn; |
| 916 | wn = NULL; |
| 917 | match_len = dir_len + 1 + len; |
| 918 | match_format = format_aux; |
| 919 | continue; |
| 920 | } |
| 921 | } |
| 922 | } |
| 923 | } |
| 924 | |
| 925 | success: |
| 926 | (*localfile) = match_name; |
| 927 | match_name = NULL; |
| 928 | if (format) { |
| 929 | (*format) = match_format; |
| 930 | } |
| 931 | ret = EXIT_SUCCESS; |
| 932 | |
| 933 | cleanup: |
| 934 | free(wn); |
| 935 | free(wd); |
| 936 | if (dir) { |
| 937 | closedir(dir); |
| 938 | } |
| 939 | free(match_name); |
| 940 | ly_set_free(dirs, free); |
| 941 | |
| 942 | return ret; |
| 943 | } |
| 944 | |