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