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