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