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