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