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