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