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