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