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