Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file context.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief context implementation for libyang |
| 5 | * |
| 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 7 | * |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 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 |
Michal Vasko | 8de098c | 2016-02-26 10:00:25 +0100 | [diff] [blame] | 11 | * |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 12 | * https://opensource.org/licenses/BSD-3-Clause |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #define _GNU_SOURCE |
Radek Krejci | cf74825 | 2017-09-04 11:11:14 +0200 | [diff] [blame] | 16 | #include <pthread.h> |
Radek Krejci | fd4e6e3 | 2015-08-10 15:00:51 +0200 | [diff] [blame] | 17 | #include <stddef.h> |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | #include <sys/types.h> |
| 21 | #include <sys/stat.h> |
| 22 | #include <unistd.h> |
Michal Vasko | 6d2beea | 2018-08-14 08:34:21 +0200 | [diff] [blame] | 23 | #include <limits.h> |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 24 | #include <errno.h> |
| 25 | #include <fcntl.h> |
| 26 | |
| 27 | #include "common.h" |
| 28 | #include "context.h" |
Michal Vasko | 6c81070 | 2018-03-14 16:23:21 +0100 | [diff] [blame] | 29 | #include "hash_table.h" |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 30 | #include "parser.h" |
Radek Krejci | bc9cf93 | 2015-07-30 11:09:39 +0200 | [diff] [blame] | 31 | #include "tree_internal.h" |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 32 | #include "resolve.h" |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 33 | |
Radek Krejci | 858ad95 | 2017-01-04 11:16:32 +0100 | [diff] [blame] | 34 | /* |
| 35 | * counter for references to the extensions plugins (for the number of contexts) |
| 36 | * located in extensions.c |
| 37 | */ |
| 38 | extern unsigned int ext_plugins_ref; |
| 39 | |
Radek Krejci | 532e5e9 | 2017-02-22 12:59:24 +0100 | [diff] [blame] | 40 | #define IETF_YANG_METADATA_PATH "../models/ietf-yang-metadata@2016-08-05.h" |
| 41 | #define YANG_PATH "../models/yang@2017-02-20.h" |
Michal Vasko | 8d054e4 | 2015-08-03 12:42:06 +0200 | [diff] [blame] | 42 | #define IETF_INET_TYPES_PATH "../models/ietf-inet-types@2013-07-15.h" |
Michal Vasko | 21181c4 | 2015-08-03 13:46:45 +0200 | [diff] [blame] | 43 | #define IETF_YANG_TYPES_PATH "../models/ietf-yang-types@2013-07-15.h" |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 44 | #define IETF_DATASTORES "../models/ietf-datastores@2017-08-17.h" |
Michal Vasko | eb3bd0e | 2018-01-26 11:52:11 +0100 | [diff] [blame] | 45 | #define IETF_YANG_LIB_PATH "../models/ietf-yang-library@2018-01-17.h" |
| 46 | #define IETF_YANG_LIB_REV "2018-01-17" |
Michal Vasko | 8d054e4 | 2015-08-03 12:42:06 +0200 | [diff] [blame] | 47 | |
Radek Krejci | 532e5e9 | 2017-02-22 12:59:24 +0100 | [diff] [blame] | 48 | #include IETF_YANG_METADATA_PATH |
| 49 | #include YANG_PATH |
Michal Vasko | 8d054e4 | 2015-08-03 12:42:06 +0200 | [diff] [blame] | 50 | #include IETF_INET_TYPES_PATH |
Michal Vasko | 21181c4 | 2015-08-03 13:46:45 +0200 | [diff] [blame] | 51 | #include IETF_YANG_TYPES_PATH |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 52 | #include IETF_DATASTORES |
Michal Vasko | 8d054e4 | 2015-08-03 12:42:06 +0200 | [diff] [blame] | 53 | #include IETF_YANG_LIB_PATH |
| 54 | |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 55 | #define LY_INTERNAL_MODULE_COUNT 6 |
Radek Krejci | 0320341 | 2016-06-23 15:20:53 +0200 | [diff] [blame] | 56 | static struct internal_modules_s { |
| 57 | const char *name; |
| 58 | const char *revision; |
| 59 | const char *data; |
| 60 | uint8_t implemented; |
| 61 | LYS_INFORMAT format; |
Michal Vasko | 2d051a1 | 2017-04-21 09:28:57 +0200 | [diff] [blame] | 62 | } internal_modules[LY_INTERNAL_MODULE_COUNT] = { |
Radek Krejci | 532e5e9 | 2017-02-22 12:59:24 +0100 | [diff] [blame] | 63 | {"ietf-yang-metadata", "2016-08-05", (const char*)ietf_yang_metadata_2016_08_05_yin, 0, LYS_IN_YIN}, |
| 64 | {"yang", "2017-02-20", (const char*)yang_2017_02_20_yin, 1, LYS_IN_YIN}, |
Radek Krejci | 0320341 | 2016-06-23 15:20:53 +0200 | [diff] [blame] | 65 | {"ietf-inet-types", "2013-07-15", (const char*)ietf_inet_types_2013_07_15_yin, 0, LYS_IN_YIN}, |
| 66 | {"ietf-yang-types", "2013-07-15", (const char*)ietf_yang_types_2013_07_15_yin, 0, LYS_IN_YIN}, |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 67 | /* ietf-datastores and ietf-yang-library must be right here at the end of the list! */ |
| 68 | {"ietf-datastores", "2017-08-17", (const char*)ietf_datastores_2017_08_17_yin, 0, LYS_IN_YIN}, |
Michal Vasko | eb3bd0e | 2018-01-26 11:52:11 +0100 | [diff] [blame] | 69 | {"ietf-yang-library", IETF_YANG_LIB_REV, (const char*)ietf_yang_library_2018_01_17_yin, 1, LYS_IN_YIN} |
Radek Krejci | 0320341 | 2016-06-23 15:20:53 +0200 | [diff] [blame] | 70 | }; |
| 71 | |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 72 | API unsigned int |
| 73 | ly_ctx_internal_modules_count(struct ly_ctx *ctx) |
| 74 | { |
| 75 | if (!ctx) { |
| 76 | return 0; |
| 77 | } |
| 78 | return ctx->internal_module_count; |
| 79 | } |
| 80 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 81 | API struct ly_ctx * |
Radek Krejci | dd3263a | 2017-07-15 11:50:09 +0200 | [diff] [blame] | 82 | ly_ctx_new(const char *search_dir, int options) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 83 | { |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 84 | struct ly_ctx *ctx = NULL; |
Michal Vasko | 7d7de95 | 2016-05-02 17:13:14 +0200 | [diff] [blame] | 85 | struct lys_module *module; |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 86 | char *cwd = NULL; |
Igor Ternovsky | 9a10e1a | 2017-09-11 12:53:04 +1000 | [diff] [blame] | 87 | char *search_dir_list; |
| 88 | char *sep, *dir; |
Igor Ternovsky | 6e6543d | 2017-09-12 10:37:28 +1000 | [diff] [blame] | 89 | int rc = EXIT_SUCCESS; |
Radek Krejci | 0320341 | 2016-06-23 15:20:53 +0200 | [diff] [blame] | 90 | int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 91 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 92 | ctx = calloc(1, sizeof *ctx); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 93 | LY_CHECK_ERR_RETURN(!ctx, LOGMEM(NULL), NULL); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 94 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 95 | /* dictionary */ |
| 96 | lydict_init(&ctx->dict); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 97 | |
Radek Krejci | 858ad95 | 2017-01-04 11:16:32 +0100 | [diff] [blame] | 98 | /* plugins */ |
Michal Vasko | c6cd3f0 | 2018-03-02 14:07:42 +0100 | [diff] [blame] | 99 | ly_load_plugins(); |
Radek Krejci | 858ad95 | 2017-01-04 11:16:32 +0100 | [diff] [blame] | 100 | |
Radek Krejci | cf74825 | 2017-09-04 11:11:14 +0200 | [diff] [blame] | 101 | /* initialize thread-specific key */ |
| 102 | while ((i = pthread_key_create(&ctx->errlist_key, ly_err_free)) == EAGAIN); |
| 103 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 104 | /* models list */ |
| 105 | ctx->models.list = calloc(16, sizeof *ctx->models.list); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 106 | LY_CHECK_ERR_RETURN(!ctx->models.list, LOGMEM(NULL); free(ctx), NULL); |
Radek Krejci | dd3263a | 2017-07-15 11:50:09 +0200 | [diff] [blame] | 107 | ctx->models.flags = options; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 108 | ctx->models.used = 0; |
| 109 | ctx->models.size = 16; |
| 110 | if (search_dir) { |
Igor Ternovsky | 9a10e1a | 2017-09-11 12:53:04 +1000 | [diff] [blame] | 111 | search_dir_list = strdup(search_dir); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 112 | LY_CHECK_ERR_GOTO(!search_dir_list, LOGMEM(NULL), error); |
Igor Ternovsky | 9a10e1a | 2017-09-11 12:53:04 +1000 | [diff] [blame] | 113 | |
Igor Ternovsky | 6e6543d | 2017-09-12 10:37:28 +1000 | [diff] [blame] | 114 | for (dir = search_dir_list; (sep = strchr(dir, ':')) != NULL && rc == EXIT_SUCCESS; dir = sep + 1) { |
Igor Ternovsky | 9a10e1a | 2017-09-11 12:53:04 +1000 | [diff] [blame] | 115 | *sep = 0; |
Igor Ternovsky | 6e6543d | 2017-09-12 10:37:28 +1000 | [diff] [blame] | 116 | rc = ly_ctx_set_searchdir(ctx, dir); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 117 | } |
Igor Ternovsky | 6e6543d | 2017-09-12 10:37:28 +1000 | [diff] [blame] | 118 | if (*dir && rc == EXIT_SUCCESS) { |
| 119 | rc = ly_ctx_set_searchdir(ctx, dir); |
Radek Krejci | 15412ca | 2016-03-03 11:16:52 +0100 | [diff] [blame] | 120 | } |
Igor Ternovsky | 9a10e1a | 2017-09-11 12:53:04 +1000 | [diff] [blame] | 121 | free(search_dir_list); |
Igor Ternovsky | 6e6543d | 2017-09-12 10:37:28 +1000 | [diff] [blame] | 122 | /* If ly_ctx_set_searchdir() failed, the error is already logged. Just exit */ |
| 123 | if (rc != EXIT_SUCCESS) { |
| 124 | goto error; |
| 125 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 126 | } |
Michal Vasko | 14719b2 | 2015-08-03 12:47:55 +0200 | [diff] [blame] | 127 | ctx->models.module_set_id = 1; |
| 128 | |
Radek Krejci | 0320341 | 2016-06-23 15:20:53 +0200 | [diff] [blame] | 129 | /* load internal modules */ |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 130 | if (options & LY_CTX_NOYANGLIBRARY) { |
| 131 | ctx->internal_module_count = LY_INTERNAL_MODULE_COUNT - 2; |
| 132 | } else { |
| 133 | ctx->internal_module_count = LY_INTERNAL_MODULE_COUNT; |
| 134 | } |
| 135 | for (i = 0; i < ctx->internal_module_count; i++) { |
Radek Krejci | 0320341 | 2016-06-23 15:20:53 +0200 | [diff] [blame] | 136 | module = (struct lys_module *)lys_parse_mem(ctx, internal_modules[i].data, internal_modules[i].format); |
| 137 | if (!module) { |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 138 | goto error; |
Radek Krejci | 0320341 | 2016-06-23 15:20:53 +0200 | [diff] [blame] | 139 | } |
| 140 | module->implemented = internal_modules[i].implemented; |
Michal Vasko | 8d054e4 | 2015-08-03 12:42:06 +0200 | [diff] [blame] | 141 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 142 | |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 143 | /* cleanup */ |
| 144 | free(cwd); |
| 145 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 146 | return ctx; |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 147 | |
| 148 | error: |
| 149 | free(cwd); |
| 150 | ly_ctx_destroy(ctx, NULL); |
| 151 | return NULL; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 152 | } |
| 153 | |
Michal Vasko | eb3bd0e | 2018-01-26 11:52:11 +0100 | [diff] [blame] | 154 | static int |
| 155 | ly_ctx_new_yl_legacy(struct ly_ctx *ctx, struct lyd_node *yltree) |
Radek Krejci | 69333c9 | 2017-03-17 16:14:43 +0100 | [diff] [blame] | 156 | { |
Michal Vasko | 60d400b | 2017-12-13 11:14:39 +0100 | [diff] [blame] | 157 | unsigned int i, u; |
Radek Krejci | 69333c9 | 2017-03-17 16:14:43 +0100 | [diff] [blame] | 158 | struct lyd_node *module, *node; |
Michal Vasko | eb3bd0e | 2018-01-26 11:52:11 +0100 | [diff] [blame] | 159 | struct ly_set *set; |
Radek Krejci | 69333c9 | 2017-03-17 16:14:43 +0100 | [diff] [blame] | 160 | const char *name, *revision; |
Radek Krejci | 5cb217d | 2017-03-25 16:07:52 -0500 | [diff] [blame] | 161 | struct ly_set features = {0, 0, {NULL}}; |
Radek Krejci | 69333c9 | 2017-03-17 16:14:43 +0100 | [diff] [blame] | 162 | const struct lys_module *mod; |
Radek Krejci | 69333c9 | 2017-03-17 16:14:43 +0100 | [diff] [blame] | 163 | |
Michal Vasko | eb3bd0e | 2018-01-26 11:52:11 +0100 | [diff] [blame] | 164 | set = lyd_find_path(yltree, "/ietf-yang-library:yang-library/modules-state/module"); |
Michal Vasko | 60d400b | 2017-12-13 11:14:39 +0100 | [diff] [blame] | 165 | if (!set) { |
Michal Vasko | eb3bd0e | 2018-01-26 11:52:11 +0100 | [diff] [blame] | 166 | return 1; |
Michal Vasko | 60d400b | 2017-12-13 11:14:39 +0100 | [diff] [blame] | 167 | } |
| 168 | |
Radek Krejci | 69333c9 | 2017-03-17 16:14:43 +0100 | [diff] [blame] | 169 | /* process the data tree */ |
Michal Vasko | 60d400b | 2017-12-13 11:14:39 +0100 | [diff] [blame] | 170 | for (i = 0; i < set->number; ++i) { |
| 171 | module = set->set.d[i]; |
Radek Krejci | 69333c9 | 2017-03-17 16:14:43 +0100 | [diff] [blame] | 172 | |
| 173 | /* initiate */ |
| 174 | name = NULL; |
| 175 | revision = NULL; |
| 176 | ly_set_clean(&features); |
| 177 | |
| 178 | LY_TREE_FOR(module->child, node) { |
| 179 | if (!strcmp(node->schema->name, "name")) { |
| 180 | name = ((struct lyd_node_leaf_list*)node)->value_str; |
| 181 | } else if (!strcmp(node->schema->name, "revision")) { |
| 182 | revision = ((struct lyd_node_leaf_list*)node)->value_str; |
| 183 | } else if (!strcmp(node->schema->name, "feature")) { |
| 184 | ly_set_add(&features, node, LY_SET_OPT_USEASLIST); |
| 185 | } else if (!strcmp(node->schema->name, "conformance-type") && |
| 186 | ((struct lyd_node_leaf_list*)node)->value.enm->value) { |
| 187 | /* imported module - skip it, it will be loaded as a side effect |
| 188 | * of loading another module */ |
Michal Vasko | 60d400b | 2017-12-13 11:14:39 +0100 | [diff] [blame] | 189 | continue; |
Radek Krejci | 69333c9 | 2017-03-17 16:14:43 +0100 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
| 193 | /* use the gathered data to load the module */ |
| 194 | mod = ly_ctx_load_module(ctx, name, revision); |
| 195 | if (!mod) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 196 | LOGERR(ctx, LY_EINVAL, "Unable to load module specified by yang library data."); |
Michal Vasko | eb3bd0e | 2018-01-26 11:52:11 +0100 | [diff] [blame] | 197 | ly_set_free(set); |
| 198 | return 1; |
Radek Krejci | 69333c9 | 2017-03-17 16:14:43 +0100 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | /* set features */ |
| 202 | for (u = 0; u < features.number; u++) { |
| 203 | lys_features_enable(mod, ((struct lyd_node_leaf_list*)features.set.d[u])->value_str); |
| 204 | } |
Radek Krejci | 69333c9 | 2017-03-17 16:14:43 +0100 | [diff] [blame] | 205 | } |
| 206 | |
Michal Vasko | eb3bd0e | 2018-01-26 11:52:11 +0100 | [diff] [blame] | 207 | ly_set_free(set); |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | static struct ly_ctx * |
| 212 | ly_ctx_new_yl_common(const char *search_dir, const char *input, LYD_FORMAT format, int options, |
| 213 | struct lyd_node* (*parser_func)(struct ly_ctx*, const char*, LYD_FORMAT, int,...)) |
| 214 | { |
| 215 | unsigned int i, u; |
| 216 | struct lyd_node *module, *node; |
| 217 | const char *name, *revision; |
| 218 | struct ly_set features = {0, 0, {NULL}}; |
| 219 | const struct lys_module *mod; |
| 220 | struct lyd_node *yltree = NULL; |
| 221 | struct ly_ctx *ctx = NULL; |
| 222 | struct ly_set *set = NULL; |
| 223 | |
| 224 | /* create empty (with internal modules including ietf-yang-library) context */ |
| 225 | ctx = ly_ctx_new(search_dir, options); |
| 226 | if (!ctx) { |
| 227 | goto error; |
| 228 | } |
| 229 | |
| 230 | /* parse yang library data tree */ |
| 231 | yltree = parser_func(ctx, input, format, LYD_OPT_DATA, NULL); |
| 232 | if (!yltree) { |
| 233 | goto error; |
| 234 | } |
| 235 | |
| 236 | set = lyd_find_path(yltree, "/ietf-yang-library:yang-library/module-set[1]/module"); |
| 237 | if (!set) { |
| 238 | goto error; |
| 239 | } |
| 240 | |
| 241 | if (set->number == 0) { |
| 242 | /* perhaps a legacy data tree? */ |
| 243 | if (ly_ctx_new_yl_legacy(ctx, yltree)) { |
| 244 | goto error; |
| 245 | } |
| 246 | } else { |
| 247 | /* process the data tree */ |
| 248 | for (i = 0; i < set->number; ++i) { |
| 249 | module = set->set.d[i]; |
| 250 | |
| 251 | /* initiate */ |
| 252 | name = NULL; |
| 253 | revision = NULL; |
| 254 | ly_set_clean(&features); |
| 255 | |
| 256 | LY_TREE_FOR(module->child, node) { |
| 257 | if (!strcmp(node->schema->name, "name")) { |
| 258 | name = ((struct lyd_node_leaf_list*)node)->value_str; |
| 259 | } else if (!strcmp(node->schema->name, "revision")) { |
| 260 | revision = ((struct lyd_node_leaf_list*)node)->value_str; |
| 261 | } else if (!strcmp(node->schema->name, "feature")) { |
| 262 | ly_set_add(&features, node->child, LY_SET_OPT_USEASLIST); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | /* use the gathered data to load the module */ |
| 267 | mod = ly_ctx_load_module(ctx, name, revision); |
| 268 | if (!mod) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 269 | LOGERR(NULL, LY_EINVAL, "Unable to load module specified by yang library data."); |
Michal Vasko | eb3bd0e | 2018-01-26 11:52:11 +0100 | [diff] [blame] | 270 | goto error; |
| 271 | } |
| 272 | |
| 273 | /* set features */ |
| 274 | for (u = 0; u < features.number; u++) { |
| 275 | lys_features_enable(mod, ((struct lyd_node_leaf_list*)features.set.d[u])->value_str); |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
Radek Krejci | 69333c9 | 2017-03-17 16:14:43 +0100 | [diff] [blame] | 280 | if (0) { |
| 281 | /* skip context destroy in case of success */ |
| 282 | error: |
| 283 | ly_ctx_destroy(ctx, NULL); |
| 284 | ctx = NULL; |
| 285 | } |
| 286 | |
| 287 | /* cleanup */ |
| 288 | if (yltree) { |
| 289 | /* yang library data tree */ |
| 290 | lyd_free_withsiblings(yltree); |
| 291 | } |
Michal Vasko | 60d400b | 2017-12-13 11:14:39 +0100 | [diff] [blame] | 292 | if (set) { |
| 293 | ly_set_free(set); |
| 294 | } |
Radek Krejci | 69333c9 | 2017-03-17 16:14:43 +0100 | [diff] [blame] | 295 | |
| 296 | return ctx; |
| 297 | } |
| 298 | |
| 299 | API struct ly_ctx * |
Radek Krejci | dd3263a | 2017-07-15 11:50:09 +0200 | [diff] [blame] | 300 | ly_ctx_new_ylpath(const char *search_dir, const char *path, LYD_FORMAT format, int options) |
Radek Krejci | 69333c9 | 2017-03-17 16:14:43 +0100 | [diff] [blame] | 301 | { |
Radek Krejci | dd3263a | 2017-07-15 11:50:09 +0200 | [diff] [blame] | 302 | return ly_ctx_new_yl_common(search_dir, path, format, options, lyd_parse_path); |
Radek Krejci | 69333c9 | 2017-03-17 16:14:43 +0100 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | API struct ly_ctx * |
Radek Krejci | dd3263a | 2017-07-15 11:50:09 +0200 | [diff] [blame] | 306 | ly_ctx_new_ylmem(const char *search_dir, const char *data, LYD_FORMAT format, int options) |
Radek Krejci | 69333c9 | 2017-03-17 16:14:43 +0100 | [diff] [blame] | 307 | { |
Radek Krejci | dd3263a | 2017-07-15 11:50:09 +0200 | [diff] [blame] | 308 | return ly_ctx_new_yl_common(search_dir, data, format, options, lyd_parse_mem); |
| 309 | } |
| 310 | |
| 311 | static void |
| 312 | ly_ctx_set_option(struct ly_ctx *ctx, int options) |
| 313 | { |
| 314 | if (!ctx) { |
| 315 | return; |
| 316 | } |
| 317 | |
| 318 | ctx->models.flags |= options; |
| 319 | } |
| 320 | |
| 321 | static void |
| 322 | ly_ctx_unset_option(struct ly_ctx *ctx, int options) |
| 323 | { |
| 324 | if (!ctx) { |
| 325 | return; |
| 326 | } |
| 327 | |
| 328 | ctx->models.flags &= ~options; |
Radek Krejci | 69333c9 | 2017-03-17 16:14:43 +0100 | [diff] [blame] | 329 | } |
| 330 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 331 | API void |
Radek Krejci | 88b7842 | 2018-08-16 16:07:34 +0200 | [diff] [blame] | 332 | ly_ctx_set_disable_searchdirs(struct ly_ctx *ctx) |
| 333 | { |
Radek Krejci | 38f8cea | 2018-08-17 09:32:07 +0200 | [diff] [blame] | 334 | ly_ctx_set_option(ctx, LY_CTX_DISABLE_SEARCHDIRS); |
Radek Krejci | 88b7842 | 2018-08-16 16:07:34 +0200 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | API void |
| 338 | ly_ctx_unset_disable_searchdirs(struct ly_ctx *ctx) |
| 339 | { |
Radek Krejci | 38f8cea | 2018-08-17 09:32:07 +0200 | [diff] [blame] | 340 | ly_ctx_unset_option(ctx, LY_CTX_DISABLE_SEARCHDIRS); |
Radek Krejci | 88b7842 | 2018-08-16 16:07:34 +0200 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | API void |
Radek Krejci | cf3b7ca | 2018-08-17 09:55:06 +0200 | [diff] [blame] | 344 | ly_ctx_set_disable_searchdir_cwd(struct ly_ctx *ctx) |
| 345 | { |
| 346 | ly_ctx_set_option(ctx, LY_CTX_DISABLE_SEARCHDIR_CWD); |
| 347 | } |
| 348 | |
| 349 | API void |
| 350 | ly_ctx_unset_disable_searchdir_cwd(struct ly_ctx *ctx) |
| 351 | { |
| 352 | ly_ctx_unset_option(ctx, LY_CTX_DISABLE_SEARCHDIR_CWD); |
| 353 | } |
| 354 | |
| 355 | API void |
Radek Krejci | 0c4a38f | 2018-08-28 15:50:23 +0200 | [diff] [blame] | 356 | ly_ctx_set_prefer_searchdirs(struct ly_ctx *ctx) |
| 357 | { |
| 358 | ly_ctx_set_option(ctx, LY_CTX_PREFER_SEARCHDIRS); |
| 359 | } |
| 360 | |
| 361 | API void |
| 362 | ly_ctx_unset_prefer_searchdirs(struct ly_ctx *ctx) |
| 363 | { |
| 364 | ly_ctx_unset_option(ctx, LY_CTX_PREFER_SEARCHDIRS); |
| 365 | } |
| 366 | |
| 367 | API void |
Radek Krejci | 819dd4b | 2017-03-07 15:35:48 +0100 | [diff] [blame] | 368 | ly_ctx_set_allimplemented(struct ly_ctx *ctx) |
| 369 | { |
Radek Krejci | dd3263a | 2017-07-15 11:50:09 +0200 | [diff] [blame] | 370 | ly_ctx_set_option(ctx, LY_CTX_ALLIMPLEMENTED); |
Radek Krejci | 819dd4b | 2017-03-07 15:35:48 +0100 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | API void |
| 374 | ly_ctx_unset_allimplemented(struct ly_ctx *ctx) |
| 375 | { |
Radek Krejci | dd3263a | 2017-07-15 11:50:09 +0200 | [diff] [blame] | 376 | ly_ctx_unset_option(ctx, LY_CTX_ALLIMPLEMENTED); |
| 377 | } |
Radek Krejci | 819dd4b | 2017-03-07 15:35:48 +0100 | [diff] [blame] | 378 | |
Radek Krejci | dd3263a | 2017-07-15 11:50:09 +0200 | [diff] [blame] | 379 | API void |
| 380 | ly_ctx_set_trusted(struct ly_ctx *ctx) |
| 381 | { |
| 382 | ly_ctx_set_option(ctx, LY_CTX_TRUSTED); |
| 383 | } |
| 384 | |
| 385 | API void |
| 386 | ly_ctx_unset_trusted(struct ly_ctx *ctx) |
| 387 | { |
| 388 | ly_ctx_unset_option(ctx, LY_CTX_TRUSTED); |
Radek Krejci | 819dd4b | 2017-03-07 15:35:48 +0100 | [diff] [blame] | 389 | } |
| 390 | |
Igor Ternovsky | 6e6543d | 2017-09-12 10:37:28 +1000 | [diff] [blame] | 391 | API int |
Radek Krejci | 4fb7d7d | 2018-08-17 10:08:59 +0200 | [diff] [blame] | 392 | ly_ctx_get_options(struct ly_ctx *ctx) |
| 393 | { |
| 394 | return ctx->models.flags; |
| 395 | } |
| 396 | |
| 397 | API int |
Michal Vasko | 60ba9a6 | 2015-07-03 14:42:31 +0200 | [diff] [blame] | 398 | ly_ctx_set_searchdir(struct ly_ctx *ctx, const char *search_dir) |
| 399 | { |
Michal Vasko | aecdb21 | 2018-08-17 10:27:34 +0200 | [diff] [blame] | 400 | char *new_dir = NULL; |
Radek Krejci | da9f839 | 2017-03-25 19:40:56 -0500 | [diff] [blame] | 401 | int index = 0; |
| 402 | void *r; |
Igor Ternovsky | 6e6543d | 2017-09-12 10:37:28 +1000 | [diff] [blame] | 403 | int rc = EXIT_FAILURE; |
Michal Vasko | 60ba9a6 | 2015-07-03 14:42:31 +0200 | [diff] [blame] | 404 | |
| 405 | if (!ctx) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 406 | LOGARG; |
Igor Ternovsky | 6e6543d | 2017-09-12 10:37:28 +1000 | [diff] [blame] | 407 | return EXIT_FAILURE; |
Michal Vasko | 60ba9a6 | 2015-07-03 14:42:31 +0200 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | if (search_dir) { |
Michal Vasko | 6d2beea | 2018-08-14 08:34:21 +0200 | [diff] [blame] | 411 | if (access(search_dir, R_OK | X_OK)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 412 | LOGERR(ctx, LY_ESYS, "Unable to use search directory \"%s\" (%s)", |
Michal Vasko | 60ba9a6 | 2015-07-03 14:42:31 +0200 | [diff] [blame] | 413 | search_dir, strerror(errno)); |
Michal Vasko | 6d2beea | 2018-08-14 08:34:21 +0200 | [diff] [blame] | 414 | return EXIT_FAILURE; |
Michal Vasko | 60ba9a6 | 2015-07-03 14:42:31 +0200 | [diff] [blame] | 415 | } |
Radek Krejci | da9f839 | 2017-03-25 19:40:56 -0500 | [diff] [blame] | 416 | |
Michal Vasko | aecdb21 | 2018-08-17 10:27:34 +0200 | [diff] [blame] | 417 | new_dir = realpath(search_dir, NULL); |
| 418 | LY_CHECK_ERR_GOTO(!new_dir, LOGERR(ctx, LY_ESYS, "realpath() call failed (%s).", strerror(errno)), cleanup); |
Radek Krejci | da9f839 | 2017-03-25 19:40:56 -0500 | [diff] [blame] | 419 | if (!ctx->models.search_paths) { |
| 420 | ctx->models.search_paths = malloc(2 * sizeof *ctx->models.search_paths); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 421 | LY_CHECK_ERR_GOTO(!ctx->models.search_paths, LOGMEM(ctx), cleanup); |
Radek Krejci | da9f839 | 2017-03-25 19:40:56 -0500 | [diff] [blame] | 422 | index = 0; |
| 423 | } else { |
Radek Krejci | 426ea2b | 2017-06-13 12:41:51 +0200 | [diff] [blame] | 424 | for (index = 0; ctx->models.search_paths[index]; index++) { |
| 425 | /* check for duplicities */ |
Michal Vasko | aecdb21 | 2018-08-17 10:27:34 +0200 | [diff] [blame] | 426 | if (!strcmp(new_dir, ctx->models.search_paths[index])) { |
Radek Krejci | 426ea2b | 2017-06-13 12:41:51 +0200 | [diff] [blame] | 427 | /* path is already present */ |
Radek Krejci | 426ea2b | 2017-06-13 12:41:51 +0200 | [diff] [blame] | 428 | goto success; |
| 429 | } |
| 430 | } |
Radek Krejci | da9f839 | 2017-03-25 19:40:56 -0500 | [diff] [blame] | 431 | r = realloc(ctx->models.search_paths, (index + 2) * sizeof *ctx->models.search_paths); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 432 | LY_CHECK_ERR_GOTO(!r, LOGMEM(ctx), cleanup); |
Radek Krejci | da9f839 | 2017-03-25 19:40:56 -0500 | [diff] [blame] | 433 | ctx->models.search_paths = r; |
| 434 | } |
Michal Vasko | aecdb21 | 2018-08-17 10:27:34 +0200 | [diff] [blame] | 435 | ctx->models.search_paths[index] = new_dir; |
| 436 | new_dir = NULL; |
Radek Krejci | da9f839 | 2017-03-25 19:40:56 -0500 | [diff] [blame] | 437 | ctx->models.search_paths[index + 1] = NULL; |
Michal Vasko | 3eff932 | 2015-11-10 11:02:30 +0100 | [diff] [blame] | 438 | |
Radek Krejci | 426ea2b | 2017-06-13 12:41:51 +0200 | [diff] [blame] | 439 | success: |
Igor Ternovsky | 6e6543d | 2017-09-12 10:37:28 +1000 | [diff] [blame] | 440 | rc = EXIT_SUCCESS; |
Radek Krejci | 9287c70 | 2017-09-12 10:32:24 +0200 | [diff] [blame] | 441 | } else { |
| 442 | /* consider that no change is not actually an error */ |
| 443 | return EXIT_SUCCESS; |
Michal Vasko | 60ba9a6 | 2015-07-03 14:42:31 +0200 | [diff] [blame] | 444 | } |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 445 | |
| 446 | cleanup: |
Michal Vasko | aecdb21 | 2018-08-17 10:27:34 +0200 | [diff] [blame] | 447 | free(new_dir); |
Igor Ternovsky | 6e6543d | 2017-09-12 10:37:28 +1000 | [diff] [blame] | 448 | return rc; |
Michal Vasko | 60ba9a6 | 2015-07-03 14:42:31 +0200 | [diff] [blame] | 449 | } |
| 450 | |
Radek Krejci | 426ea2b | 2017-06-13 12:41:51 +0200 | [diff] [blame] | 451 | API const char * const * |
| 452 | ly_ctx_get_searchdirs(const struct ly_ctx *ctx) |
Radek Krejci | 5a79757 | 2015-10-21 15:45:45 +0200 | [diff] [blame] | 453 | { |
Radek Krejci | ee55417 | 2016-12-14 10:29:06 +0100 | [diff] [blame] | 454 | if (!ctx) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 455 | LOGARG; |
Radek Krejci | ee55417 | 2016-12-14 10:29:06 +0100 | [diff] [blame] | 456 | return NULL; |
| 457 | } |
Radek Krejci | 426ea2b | 2017-06-13 12:41:51 +0200 | [diff] [blame] | 458 | return (const char * const *)ctx->models.search_paths; |
Radek Krejci | da9f839 | 2017-03-25 19:40:56 -0500 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | API void |
Radek Krejci | db4e9ff | 2017-06-13 16:26:14 +0200 | [diff] [blame] | 462 | ly_ctx_unset_searchdirs(struct ly_ctx *ctx, int index) |
Radek Krejci | da9f839 | 2017-03-25 19:40:56 -0500 | [diff] [blame] | 463 | { |
| 464 | int i; |
| 465 | |
| 466 | if (!ctx->models.search_paths) { |
| 467 | return; |
| 468 | } |
| 469 | |
| 470 | for (i = 0; ctx->models.search_paths[i]; i++) { |
Radek Krejci | db4e9ff | 2017-06-13 16:26:14 +0200 | [diff] [blame] | 471 | if (index < 0 || index == i) { |
| 472 | free(ctx->models.search_paths[i]); |
| 473 | ctx->models.search_paths[i] = NULL; |
| 474 | } else if (i > index) { |
| 475 | ctx->models.search_paths[i - 1] = ctx->models.search_paths[i]; |
| 476 | ctx->models.search_paths[i] = NULL; |
| 477 | } |
Radek Krejci | da9f839 | 2017-03-25 19:40:56 -0500 | [diff] [blame] | 478 | } |
Radek Krejci | db4e9ff | 2017-06-13 16:26:14 +0200 | [diff] [blame] | 479 | if (index < 0 || !ctx->models.search_paths[0]) { |
| 480 | free(ctx->models.search_paths); |
| 481 | ctx->models.search_paths = NULL; |
| 482 | } |
Radek Krejci | 5a79757 | 2015-10-21 15:45:45 +0200 | [diff] [blame] | 483 | } |
| 484 | |
Michal Vasko | 60ba9a6 | 2015-07-03 14:42:31 +0200 | [diff] [blame] | 485 | API void |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 486 | ly_ctx_destroy(struct ly_ctx *ctx, void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 487 | { |
Radek Krejci | da9f839 | 2017-03-25 19:40:56 -0500 | [diff] [blame] | 488 | int i; |
| 489 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 490 | if (!ctx) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 491 | return; |
| 492 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 493 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 494 | /* models list */ |
Radek Krejci | c436a23 | 2017-02-08 14:43:11 +0100 | [diff] [blame] | 495 | for (; ctx->models.used > 0; ctx->models.used--) { |
Radek Krejci | fc8411a | 2017-02-03 10:26:05 +0100 | [diff] [blame] | 496 | /* remove the applied deviations and augments */ |
Radek Krejci | c436a23 | 2017-02-08 14:43:11 +0100 | [diff] [blame] | 497 | lys_sub_module_remove_devs_augs(ctx->models.list[ctx->models.used - 1]); |
Radek Krejci | fc8411a | 2017-02-03 10:26:05 +0100 | [diff] [blame] | 498 | /* remove the module */ |
Michal Vasko | 10681e8 | 2018-01-16 14:54:16 +0100 | [diff] [blame] | 499 | lys_free(ctx->models.list[ctx->models.used - 1], private_destructor, 1, 0); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 500 | } |
Radek Krejci | da9f839 | 2017-03-25 19:40:56 -0500 | [diff] [blame] | 501 | if (ctx->models.search_paths) { |
| 502 | for(i = 0; ctx->models.search_paths[i]; i++) { |
| 503 | free(ctx->models.search_paths[i]); |
| 504 | } |
| 505 | free(ctx->models.search_paths); |
| 506 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 507 | free(ctx->models.list); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 508 | |
Radek Krejci | cf74825 | 2017-09-04 11:11:14 +0200 | [diff] [blame] | 509 | /* clean the error list */ |
| 510 | ly_err_clean(ctx, 0); |
| 511 | pthread_key_delete(ctx->errlist_key); |
| 512 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 513 | /* dictionary */ |
| 514 | lydict_clean(&ctx->dict); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 515 | |
Radek Krejci | 858ad95 | 2017-01-04 11:16:32 +0100 | [diff] [blame] | 516 | /* plugins - will be removed only if this is the last context */ |
Michal Vasko | c6cd3f0 | 2018-03-02 14:07:42 +0100 | [diff] [blame] | 517 | ly_clean_plugins(); |
Radek Krejci | 858ad95 | 2017-01-04 11:16:32 +0100 | [diff] [blame] | 518 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 519 | free(ctx); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 520 | } |
| 521 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 522 | API const struct lys_submodule * |
Radek Krejci | 62f0da7 | 2016-03-07 11:35:43 +0100 | [diff] [blame] | 523 | ly_ctx_get_submodule2(const struct lys_module *main_module, const char *submodule) |
| 524 | { |
Radek Krejci | d4c1d0f | 2017-01-19 16:11:38 +0100 | [diff] [blame] | 525 | const struct lys_submodule *result; |
Radek Krejci | 62f0da7 | 2016-03-07 11:35:43 +0100 | [diff] [blame] | 526 | int i; |
| 527 | |
| 528 | if (!main_module || !submodule) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 529 | LOGARG; |
Radek Krejci | 62f0da7 | 2016-03-07 11:35:43 +0100 | [diff] [blame] | 530 | return NULL; |
| 531 | } |
| 532 | |
| 533 | /* search in submodules list */ |
| 534 | for (i = 0; i < main_module->inc_size; i++) { |
| 535 | result = main_module->inc[i].submodule; |
Radek Krejci | d4c1d0f | 2017-01-19 16:11:38 +0100 | [diff] [blame] | 536 | if (ly_strequal(submodule, result->name, 0)) { |
Radek Krejci | 62f0da7 | 2016-03-07 11:35:43 +0100 | [diff] [blame] | 537 | return result; |
| 538 | } |
Radek Krejci | d4c1d0f | 2017-01-19 16:11:38 +0100 | [diff] [blame] | 539 | |
| 540 | /* in YANG 1.1 all the submodules must be included in the main module, so we are done. |
| 541 | * YANG 1.0 allows (is unclear about denying it) to include a submodule only in another submodule |
| 542 | * but when libyang parses such a module it adds the include into the main module so we are also done. |
| 543 | */ |
Radek Krejci | 62f0da7 | 2016-03-07 11:35:43 +0100 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | return NULL; |
| 547 | } |
| 548 | |
| 549 | API const struct lys_submodule * |
Michal Vasko | f6d94c6 | 2016-04-05 11:21:54 +0200 | [diff] [blame] | 550 | ly_ctx_get_submodule(const struct ly_ctx *ctx, const char *module, const char *revision, const char *submodule, |
| 551 | const char *sub_revision) |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 552 | { |
Radek Krejci | e797355 | 2016-03-07 08:12:01 +0100 | [diff] [blame] | 553 | const struct lys_module *mainmod; |
Michal Vasko | f6d94c6 | 2016-04-05 11:21:54 +0200 | [diff] [blame] | 554 | const struct lys_submodule *ret = NULL, *submod; |
| 555 | uint32_t idx = 0; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 556 | |
Michal Vasko | f6d94c6 | 2016-04-05 11:21:54 +0200 | [diff] [blame] | 557 | if (!ctx || !submodule || (revision && !module)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 558 | LOGARG; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 559 | return NULL; |
| 560 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 561 | |
Michal Vasko | f6d94c6 | 2016-04-05 11:21:54 +0200 | [diff] [blame] | 562 | while ((mainmod = ly_ctx_get_module_iter(ctx, &idx))) { |
| 563 | if (module && strcmp(mainmod->name, module)) { |
| 564 | /* main module name does not match */ |
| 565 | continue; |
| 566 | } |
| 567 | |
| 568 | if (revision && (!mainmod->rev || strcmp(revision, mainmod->rev[0].date))) { |
| 569 | /* main module revision does not match */ |
| 570 | continue; |
| 571 | } |
| 572 | |
| 573 | submod = ly_ctx_get_submodule2(mainmod, submodule); |
| 574 | if (!submod) { |
| 575 | continue; |
| 576 | } |
| 577 | |
| 578 | if (!sub_revision) { |
| 579 | /* store only if newer */ |
| 580 | if (ret) { |
| 581 | if (submod->rev && (!ret->rev || (strcmp(submod->rev[0].date, ret->rev[0].date) > 0))) { |
| 582 | ret = submod; |
| 583 | } |
| 584 | } else { |
| 585 | ret = submod; |
| 586 | } |
| 587 | } else { |
| 588 | /* store only if revision matches, we are done if it does */ |
| 589 | if (!submod->rev) { |
| 590 | continue; |
| 591 | } else if (!strcmp(sub_revision, submod->rev[0].date)) { |
| 592 | ret = submod; |
| 593 | break; |
| 594 | } |
| 595 | } |
Radek Krejci | a7533f2 | 2016-03-07 07:37:45 +0100 | [diff] [blame] | 596 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 597 | |
Michal Vasko | f6d94c6 | 2016-04-05 11:21:54 +0200 | [diff] [blame] | 598 | return ret; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 599 | } |
| 600 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 601 | static const struct lys_module * |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 602 | ly_ctx_get_module_by(const struct ly_ctx *ctx, const char *key, size_t key_len, int offset, const char *revision, |
| 603 | int with_disabled, int implemented) |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 604 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 605 | int i; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 606 | char *val; |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 607 | struct lys_module *result = NULL; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 608 | |
Radek Krejci | fd4e6e3 | 2015-08-10 15:00:51 +0200 | [diff] [blame] | 609 | if (!ctx || !key) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 610 | LOGARG; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 611 | return NULL; |
| 612 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 613 | |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 614 | for (i = 0; i < ctx->models.used; i++) { |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 615 | if (!with_disabled && ctx->models.list[i]->disabled) { |
| 616 | /* skip the disabled modules */ |
| 617 | continue; |
| 618 | } |
Radek Krejci | fd4e6e3 | 2015-08-10 15:00:51 +0200 | [diff] [blame] | 619 | /* use offset to get address of the pointer to string (char**), remember that offset is in |
| 620 | * bytes, so we have to cast the pointer to the module to (char*), finally, we want to have |
| 621 | * string not the pointer to string |
| 622 | */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 623 | val = *(char **)(((char *)ctx->models.list[i]) + offset); |
| 624 | if (!ctx->models.list[i] || (!key_len && strcmp(key, val)) || (key_len && (strncmp(key, val, key_len) || val[key_len]))) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 625 | continue; |
| 626 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 627 | |
Radek Krejci | f647e61 | 2015-07-30 11:36:07 +0200 | [diff] [blame] | 628 | if (!revision) { |
| 629 | /* compare revisons and remember the newest one */ |
| 630 | if (result) { |
| 631 | if (!ctx->models.list[i]->rev_size) { |
| 632 | /* the current have no revision, keep the previous with some revision */ |
| 633 | continue; |
| 634 | } |
| 635 | if (result->rev_size && strcmp(ctx->models.list[i]->rev[0].date, result->rev[0].date) < 0) { |
| 636 | /* the previous found matching module has a newer revision */ |
| 637 | continue; |
| 638 | } |
| 639 | } |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 640 | if (implemented) { |
| 641 | if (ctx->models.list[i]->implemented) { |
| 642 | /* we have the implemented revision */ |
| 643 | result = ctx->models.list[i]; |
| 644 | break; |
| 645 | } else { |
| 646 | /* do not remember the result, we are supposed to return the implemented revision |
| 647 | * not the newest one */ |
| 648 | continue; |
| 649 | } |
| 650 | } |
Radek Krejci | f647e61 | 2015-07-30 11:36:07 +0200 | [diff] [blame] | 651 | |
| 652 | /* remember the current match and search for newer version */ |
| 653 | result = ctx->models.list[i]; |
| 654 | } else { |
| 655 | if (ctx->models.list[i]->rev_size && !strcmp(revision, ctx->models.list[i]->rev[0].date)) { |
| 656 | /* matching revision */ |
Michal Vasko | 9758650 | 2015-08-12 14:32:18 +0200 | [diff] [blame] | 657 | result = ctx->models.list[i]; |
| 658 | break; |
Radek Krejci | f647e61 | 2015-07-30 11:36:07 +0200 | [diff] [blame] | 659 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 660 | } |
| 661 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 662 | |
Radek Krejci | f647e61 | 2015-07-30 11:36:07 +0200 | [diff] [blame] | 663 | return result; |
Radek Krejci | fd4e6e3 | 2015-08-10 15:00:51 +0200 | [diff] [blame] | 664 | |
| 665 | } |
| 666 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 667 | API const struct lys_module * |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 668 | ly_ctx_get_module_by_ns(const struct ly_ctx *ctx, const char *ns, const char *revision, int implemented) |
Radek Krejci | fd4e6e3 | 2015-08-10 15:00:51 +0200 | [diff] [blame] | 669 | { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 670 | return ly_ctx_get_module_by(ctx, ns, 0, offsetof(struct lys_module, ns), revision, 0, implemented); |
Radek Krejci | fd4e6e3 | 2015-08-10 15:00:51 +0200 | [diff] [blame] | 671 | } |
| 672 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 673 | API const struct lys_module * |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 674 | ly_ctx_get_module(const struct ly_ctx *ctx, const char *name, const char *revision, int implemented) |
Radek Krejci | fd4e6e3 | 2015-08-10 15:00:51 +0200 | [diff] [blame] | 675 | { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 676 | return ly_ctx_get_module_by(ctx, name, 0, offsetof(struct lys_module, name), revision, 0, implemented); |
| 677 | } |
| 678 | |
| 679 | const struct lys_module * |
| 680 | ly_ctx_nget_module(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *revision, int implemented) |
| 681 | { |
| 682 | return ly_ctx_get_module_by(ctx, name, name_len, offsetof(struct lys_module, name), revision, 0, implemented); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 683 | } |
Michal Vasko | 3ec07dc | 2015-06-30 15:51:30 +0200 | [diff] [blame] | 684 | |
Radek Krejci | 21601a3 | 2016-03-07 11:39:27 +0100 | [diff] [blame] | 685 | API const struct lys_module * |
| 686 | ly_ctx_get_module_older(const struct ly_ctx *ctx, const struct lys_module *module) |
| 687 | { |
| 688 | int i; |
| 689 | const struct lys_module *result = NULL, *iter; |
| 690 | |
| 691 | if (!ctx || !module || !module->rev_size) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 692 | LOGARG; |
Radek Krejci | 21601a3 | 2016-03-07 11:39:27 +0100 | [diff] [blame] | 693 | return NULL; |
| 694 | } |
| 695 | |
| 696 | |
| 697 | for (i = 0; i < ctx->models.used; i++) { |
| 698 | iter = ctx->models.list[i]; |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 699 | if (iter->disabled) { |
| 700 | /* skip the disabled modules */ |
| 701 | continue; |
| 702 | } |
Radek Krejci | 21601a3 | 2016-03-07 11:39:27 +0100 | [diff] [blame] | 703 | if (iter == module || !iter->rev_size) { |
| 704 | /* iter is the module itself or iter has no revision */ |
| 705 | continue; |
| 706 | } |
| 707 | if (!ly_strequal(module->name, iter->name, 0)) { |
| 708 | /* different module */ |
| 709 | continue; |
| 710 | } |
| 711 | if (strcmp(iter->rev[0].date, module->rev[0].date) < 0) { |
| 712 | /* iter is older than module */ |
| 713 | if (result) { |
| 714 | if (strcmp(iter->rev[0].date, result->rev[0].date) > 0) { |
| 715 | /* iter is newer than current result */ |
| 716 | result = iter; |
| 717 | } |
| 718 | } else { |
| 719 | result = iter; |
| 720 | } |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | return result; |
| 725 | } |
| 726 | |
Michal Vasko | 99b0aad | 2015-12-01 12:28:51 +0100 | [diff] [blame] | 727 | API void |
Michal Vasko | ca28aab | 2017-01-13 13:50:12 +0100 | [diff] [blame] | 728 | ly_ctx_set_module_imp_clb(struct ly_ctx *ctx, ly_module_imp_clb clb, void *user_data) |
Michal Vasko | 8246596 | 2015-11-10 11:03:11 +0100 | [diff] [blame] | 729 | { |
Michal Vasko | ad9f92a | 2017-04-06 09:49:07 +0200 | [diff] [blame] | 730 | if (!ctx) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 731 | LOGARG; |
Michal Vasko | ad9f92a | 2017-04-06 09:49:07 +0200 | [diff] [blame] | 732 | return; |
| 733 | } |
| 734 | |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 735 | ctx->imp_clb = clb; |
| 736 | ctx->imp_clb_data = user_data; |
Michal Vasko | 99b0aad | 2015-12-01 12:28:51 +0100 | [diff] [blame] | 737 | } |
| 738 | |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 739 | API ly_module_imp_clb |
Michal Vasko | ca28aab | 2017-01-13 13:50:12 +0100 | [diff] [blame] | 740 | ly_ctx_get_module_imp_clb(const struct ly_ctx *ctx, void **user_data) |
Michal Vasko | 99b0aad | 2015-12-01 12:28:51 +0100 | [diff] [blame] | 741 | { |
Radek Krejci | ee55417 | 2016-12-14 10:29:06 +0100 | [diff] [blame] | 742 | if (!ctx) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 743 | LOGARG; |
Radek Krejci | ee55417 | 2016-12-14 10:29:06 +0100 | [diff] [blame] | 744 | return NULL; |
| 745 | } |
| 746 | |
Michal Vasko | 99b0aad | 2015-12-01 12:28:51 +0100 | [diff] [blame] | 747 | if (user_data) { |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 748 | *user_data = ctx->imp_clb_data; |
Michal Vasko | 99b0aad | 2015-12-01 12:28:51 +0100 | [diff] [blame] | 749 | } |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 750 | return ctx->imp_clb; |
| 751 | } |
| 752 | |
| 753 | API void |
Michal Vasko | ca28aab | 2017-01-13 13:50:12 +0100 | [diff] [blame] | 754 | ly_ctx_set_module_data_clb(struct ly_ctx *ctx, ly_module_data_clb clb, void *user_data) |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 755 | { |
Michal Vasko | ad9f92a | 2017-04-06 09:49:07 +0200 | [diff] [blame] | 756 | if (!ctx) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 757 | LOGARG; |
Michal Vasko | ad9f92a | 2017-04-06 09:49:07 +0200 | [diff] [blame] | 758 | return; |
| 759 | } |
| 760 | |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 761 | ctx->data_clb = clb; |
| 762 | ctx->data_clb_data = user_data; |
| 763 | } |
| 764 | |
| 765 | API ly_module_data_clb |
Michal Vasko | ca28aab | 2017-01-13 13:50:12 +0100 | [diff] [blame] | 766 | ly_ctx_get_module_data_clb(const struct ly_ctx *ctx, void **user_data) |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 767 | { |
Michal Vasko | ad9f92a | 2017-04-06 09:49:07 +0200 | [diff] [blame] | 768 | if (!ctx) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 769 | LOGARG; |
Michal Vasko | ad9f92a | 2017-04-06 09:49:07 +0200 | [diff] [blame] | 770 | return NULL; |
| 771 | } |
| 772 | |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 773 | if (user_data) { |
| 774 | *user_data = ctx->data_clb_data; |
| 775 | } |
| 776 | return ctx->data_clb; |
Michal Vasko | 99b0aad | 2015-12-01 12:28:51 +0100 | [diff] [blame] | 777 | } |
| 778 | |
Michal Vasko | e385e90 | 2018-07-02 10:15:11 +0200 | [diff] [blame] | 779 | #ifdef LY_ENABLED_LYD_PRIV |
| 780 | |
| 781 | API void |
| 782 | ly_ctx_set_priv_dup_clb(struct ly_ctx *ctx, void *(*priv_dup_clb)(const void *priv)) |
| 783 | { |
| 784 | ctx->priv_dup_clb = priv_dup_clb; |
| 785 | } |
| 786 | |
| 787 | #endif |
| 788 | |
Radek Krejci | 72cdfac | 2018-08-15 14:47:33 +0200 | [diff] [blame] | 789 | /* if module is !NULL, then the function searches for submodule */ |
| 790 | static struct lys_module * |
| 791 | ly_ctx_load_localfile(struct ly_ctx *ctx, struct lys_module *module, const char *name, const char *revision, |
| 792 | int implement, struct unres_schema *unres) |
| 793 | { |
| 794 | size_t len; |
| 795 | int fd, i; |
| 796 | char *filepath = NULL, *dot, *rev, *filename; |
| 797 | LYS_INFORMAT format; |
| 798 | struct lys_module *result = NULL; |
| 799 | |
Radek Krejci | cf3b7ca | 2018-08-17 09:55:06 +0200 | [diff] [blame] | 800 | if (lys_search_localfile(ly_ctx_get_searchdirs(ctx), !(ctx->models.flags & LY_CTX_DISABLE_SEARCHDIR_CWD), name, revision, |
| 801 | &filepath, &format)) { |
Radek Krejci | 72cdfac | 2018-08-15 14:47:33 +0200 | [diff] [blame] | 802 | goto cleanup; |
| 803 | } else if (!filepath) { |
| 804 | if (!module && !revision) { |
| 805 | /* otherwise the module would be already taken from the context */ |
| 806 | result = (struct lys_module *)ly_ctx_get_module(ctx, name, NULL, 0); |
| 807 | } |
| 808 | if (!result) { |
| 809 | LOGERR(ctx, LY_ESYS, "Data model \"%s\" not found.", name); |
| 810 | } |
| 811 | return result; |
| 812 | } |
| 813 | |
| 814 | LOGVRB("Loading schema from \"%s\" file.", filepath); |
| 815 | |
| 816 | /* cut the format for now */ |
| 817 | dot = strrchr(filepath, '.'); |
| 818 | dot[1] = '\0'; |
| 819 | |
| 820 | /* check that the same file was not already loaded - it make sense only in case of loading the newest revision, |
| 821 | * search also in disabled module - if the matching module is disabled, it will be enabled instead of loading it */ |
| 822 | if (!revision) { |
| 823 | for (i = 0; i < ctx->models.used; ++i) { |
| 824 | if (ctx->models.list[i]->filepath && !strcmp(name, ctx->models.list[i]->name) |
| 825 | && !strncmp(filepath, ctx->models.list[i]->filepath, strlen(filepath))) { |
| 826 | result = ctx->models.list[i]; |
| 827 | if (implement && !result->implemented) { |
| 828 | /* make it implemented now */ |
| 829 | if (lys_set_implemented(result)) { |
| 830 | result = NULL; |
| 831 | } |
| 832 | } else if (result->disabled) { |
| 833 | lys_set_enabled(result); |
| 834 | } |
| 835 | |
| 836 | goto cleanup; |
| 837 | } |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | /* add the format back */ |
| 842 | dot[1] = 'y'; |
| 843 | |
| 844 | /* open the file */ |
| 845 | fd = open(filepath, O_RDONLY); |
| 846 | if (fd < 0) { |
| 847 | LOGERR(ctx, LY_ESYS, "Unable to open data model file \"%s\" (%s).", |
| 848 | filepath, strerror(errno)); |
| 849 | goto cleanup; |
| 850 | } |
| 851 | |
| 852 | if (module) { |
| 853 | result = (struct lys_module *)lys_sub_parse_fd(module, fd, format, unres); |
| 854 | } else { |
| 855 | result = (struct lys_module *)lys_parse_fd_(ctx, fd, format, revision, implement); |
| 856 | } |
| 857 | close(fd); |
| 858 | |
| 859 | if (!result) { |
| 860 | goto cleanup; |
| 861 | } |
| 862 | |
| 863 | /* check that name and revision match filename */ |
| 864 | filename = strrchr(filepath, '/'); |
| 865 | if (!filename) { |
| 866 | filename = filepath; |
| 867 | } else { |
| 868 | filename++; |
| 869 | } |
| 870 | rev = strchr(filename, '@'); |
| 871 | /* name */ |
| 872 | len = strlen(result->name); |
| 873 | if (strncmp(filename, result->name, len) || |
| 874 | ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) { |
| 875 | LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, result->name); |
| 876 | } |
| 877 | if (rev) { |
| 878 | len = dot - ++rev; |
| 879 | if (!result->rev_size || len != 10 || strncmp(result->rev[0].date, rev, len)) { |
| 880 | LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename, |
| 881 | result->rev_size ? result->rev[0].date : "none"); |
| 882 | } |
| 883 | } |
| 884 | |
Alexandre Snarskii | 8e5e071 | 2018-09-03 18:35:38 +0300 | [diff] [blame] | 885 | if (!result->filepath) { |
| 886 | char rpath[PATH_MAX]; |
Alexandre Snarskii | 12b2218 | 2018-09-04 17:12:28 +0300 | [diff] [blame^] | 887 | if (realpath(filepath, rpath) != NULL) { |
Alexandre Snarskii | 8e5e071 | 2018-09-03 18:35:38 +0300 | [diff] [blame] | 888 | result->filepath = lydict_insert(ctx, rpath, 0); |
Alexandre Snarskii | 12b2218 | 2018-09-04 17:12:28 +0300 | [diff] [blame^] | 889 | } else { |
| 890 | result->filepath = lydict_insert(ctx, filepath, 0); |
| 891 | } |
Alexandre Snarskii | 8e5e071 | 2018-09-03 18:35:38 +0300 | [diff] [blame] | 892 | } |
| 893 | |
Radek Krejci | 72cdfac | 2018-08-15 14:47:33 +0200 | [diff] [blame] | 894 | /* success */ |
| 895 | cleanup: |
| 896 | free(filepath); |
| 897 | return result; |
| 898 | } |
| 899 | |
Radek Krejci | 0c4a38f | 2018-08-28 15:50:23 +0200 | [diff] [blame] | 900 | static struct lys_module * |
| 901 | ly_ctx_load_sub_module_clb(struct ly_ctx *ctx, struct lys_module *module, const char *name, const char *revision, |
| 902 | int implement, struct unres_schema *unres) |
| 903 | { |
| 904 | struct lys_module *mod = NULL; |
| 905 | char *module_data = NULL; |
| 906 | LYS_INFORMAT format = LYS_IN_UNKNOWN; |
| 907 | void (*module_data_free)(void *module_data) = NULL; |
| 908 | |
| 909 | ly_errno = LY_SUCCESS; |
| 910 | if (module) { |
| 911 | mod = lys_main_module(module); |
| 912 | module_data = ctx->imp_clb(mod->name, (mod->rev_size ? mod->rev[0].date : NULL), name, revision, ctx->imp_clb_data, &format, &module_data_free); |
| 913 | } else { |
| 914 | module_data = ctx->imp_clb(name, revision, NULL, NULL, ctx->imp_clb_data, &format, &module_data_free); |
| 915 | } |
| 916 | if (!module_data && (ly_errno != LY_SUCCESS)) { |
| 917 | /* callback encountered an error, do not change it */ |
| 918 | LOGERR(ctx, ly_errno, "User module retrieval callback failed!"); |
| 919 | return NULL; |
| 920 | } |
| 921 | |
| 922 | if (module_data) { |
| 923 | /* we got the module from the callback */ |
| 924 | if (module) { |
| 925 | mod = (struct lys_module *)lys_sub_parse_mem(module, module_data, format, unres); |
| 926 | } else { |
| 927 | mod = (struct lys_module *)lys_parse_mem_(ctx, module_data, format, NULL, 0, implement); |
| 928 | } |
| 929 | |
| 930 | if (module_data_free) { |
| 931 | module_data_free(module_data); |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | return mod; |
| 936 | } |
| 937 | |
Radek Krejci | bf4e465 | 2016-10-21 15:44:13 +0200 | [diff] [blame] | 938 | const struct lys_module * |
Michal Vasko | 8447515 | 2016-07-25 16:16:25 +0200 | [diff] [blame] | 939 | ly_ctx_load_sub_module(struct ly_ctx *ctx, struct lys_module *module, const char *name, const char *revision, |
Michal Vasko | 58e5f3e | 2016-07-28 11:06:34 +0200 | [diff] [blame] | 940 | int implement, struct unres_schema *unres) |
Michal Vasko | 99b0aad | 2015-12-01 12:28:51 +0100 | [diff] [blame] | 941 | { |
Michal Vasko | de70186 | 2018-08-17 10:27:49 +0200 | [diff] [blame] | 942 | struct lys_module *mod = NULL; |
Radek Krejci | 0320341 | 2016-06-23 15:20:53 +0200 | [diff] [blame] | 943 | int i; |
Michal Vasko | 8246596 | 2015-11-10 11:03:11 +0100 | [diff] [blame] | 944 | |
Michal Vasko | 8447515 | 2016-07-25 16:16:25 +0200 | [diff] [blame] | 945 | if (!module) { |
Radek Krejci | bf4e465 | 2016-10-21 15:44:13 +0200 | [diff] [blame] | 946 | /* exception for internal modules */ |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 947 | for (i = 0; i < ctx->internal_module_count; i++) { |
Michal Vasko | 8447515 | 2016-07-25 16:16:25 +0200 | [diff] [blame] | 948 | if (ly_strequal(name, internal_modules[i].name, 0)) { |
| 949 | if (!revision || ly_strequal(revision, internal_modules[i].revision, 0)) { |
| 950 | /* return internal module */ |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 951 | return (struct lys_module *)ly_ctx_get_module(ctx, name, revision, 0); |
Michal Vasko | 8447515 | 2016-07-25 16:16:25 +0200 | [diff] [blame] | 952 | } |
Radek Krejci | 0320341 | 2016-06-23 15:20:53 +0200 | [diff] [blame] | 953 | } |
| 954 | } |
Michal Vasko | 8f3160e | 2017-09-27 11:25:26 +0200 | [diff] [blame] | 955 | /* try to get the schema from the context (with or without revision), |
| 956 | * include the disabled modules in the search to avoid their duplication, |
| 957 | * they are enabled by the subsequent call to lys_set_implemented() */ |
| 958 | for (i = ctx->internal_module_count, mod = NULL; i < ctx->models.used; i++) { |
| 959 | mod = ctx->models.list[i]; /* shortcut */ |
| 960 | if (ly_strequal(name, mod->name, 0)) { |
| 961 | if (revision && mod->rev_size && !strcmp(revision, mod->rev[0].date)) { |
| 962 | /* the specific revision was already loaded */ |
| 963 | break; |
| 964 | } else if (!revision && mod->latest_revision) { |
| 965 | /* the latest revision of this module was already loaded */ |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 966 | break; |
Radek Krejci | 4456491 | 2017-10-31 16:49:29 +0100 | [diff] [blame] | 967 | } else if (implement && mod->implemented && !revision) { |
| 968 | /* we are not able to implement another module, so consider this module as the latest one */ |
| 969 | break; |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 970 | } |
Michal Vasko | 8f3160e | 2017-09-27 11:25:26 +0200 | [diff] [blame] | 971 | } |
| 972 | mod = NULL; |
| 973 | } |
| 974 | if (mod) { |
| 975 | /* module must be enabled */ |
| 976 | if (mod->disabled) { |
| 977 | lys_set_enabled(mod); |
| 978 | } |
| 979 | /* module is supposed to be implemented */ |
| 980 | if (implement && lys_set_implemented(mod)) { |
| 981 | /* the schema cannot be implemented */ |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 982 | mod = NULL; |
| 983 | } |
Michal Vasko | 8f3160e | 2017-09-27 11:25:26 +0200 | [diff] [blame] | 984 | return mod; |
Radek Krejci | bf4e465 | 2016-10-21 15:44:13 +0200 | [diff] [blame] | 985 | } |
Radek Krejci | 0320341 | 2016-06-23 15:20:53 +0200 | [diff] [blame] | 986 | } |
| 987 | |
Michal Vasko | 8f3160e | 2017-09-27 11:25:26 +0200 | [diff] [blame] | 988 | /* module is not yet in context, use the user callback or try to find the schema on our own */ |
Radek Krejci | 0c4a38f | 2018-08-28 15:50:23 +0200 | [diff] [blame] | 989 | if (ctx->imp_clb && !(ctx->models.flags & LY_CTX_PREFER_SEARCHDIRS)) { |
| 990 | search_clb: |
| 991 | if (ctx->imp_clb) { |
| 992 | mod = ly_ctx_load_sub_module_clb(ctx, module, name, revision, implement, unres); |
Michal Vasko | 8447515 | 2016-07-25 16:16:25 +0200 | [diff] [blame] | 993 | } |
Radek Krejci | 0c4a38f | 2018-08-28 15:50:23 +0200 | [diff] [blame] | 994 | if (!mod && !(ctx->models.flags & LY_CTX_PREFER_SEARCHDIRS)) { |
| 995 | goto search_file; |
Michal Vasko | 8246596 | 2015-11-10 11:03:11 +0100 | [diff] [blame] | 996 | } |
Radek Krejci | 0c4a38f | 2018-08-28 15:50:23 +0200 | [diff] [blame] | 997 | } else { |
| 998 | search_file: |
| 999 | if (!(ctx->models.flags & LY_CTX_DISABLE_SEARCHDIRS)) { |
| 1000 | /* module was not received from the callback or there is no callback set */ |
| 1001 | mod = ly_ctx_load_localfile(ctx, module, name, revision, implement, unres); |
Michal Vasko | 8447515 | 2016-07-25 16:16:25 +0200 | [diff] [blame] | 1002 | } |
Radek Krejci | 0c4a38f | 2018-08-28 15:50:23 +0200 | [diff] [blame] | 1003 | if (!mod && (ctx->models.flags & LY_CTX_PREFER_SEARCHDIRS)) { |
| 1004 | goto search_clb; |
Michal Vasko | 8246596 | 2015-11-10 11:03:11 +0100 | [diff] [blame] | 1005 | } |
Michal Vasko | 8246596 | 2015-11-10 11:03:11 +0100 | [diff] [blame] | 1006 | } |
| 1007 | |
Michal Vasko | 8f3160e | 2017-09-27 11:25:26 +0200 | [diff] [blame] | 1008 | #ifdef LY_ENABLED_LATEST_REVISIONS |
| 1009 | if (!revision && mod) { |
| 1010 | /* module is the latest revision found */ |
| 1011 | mod->latest_revision = 1; |
| 1012 | } |
| 1013 | #endif |
| 1014 | |
Michal Vasko | 8447515 | 2016-07-25 16:16:25 +0200 | [diff] [blame] | 1015 | return mod; |
| 1016 | } |
| 1017 | |
| 1018 | API const struct lys_module * |
| 1019 | ly_ctx_load_module(struct ly_ctx *ctx, const char *name, const char *revision) |
| 1020 | { |
| 1021 | if (!ctx || !name) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1022 | LOGARG; |
Michal Vasko | 8447515 | 2016-07-25 16:16:25 +0200 | [diff] [blame] | 1023 | return NULL; |
| 1024 | } |
| 1025 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1026 | return ly_ctx_load_sub_module(ctx, NULL, name, revision && revision[0] ? revision : NULL, 1, NULL); |
Michal Vasko | 8246596 | 2015-11-10 11:03:11 +0100 | [diff] [blame] | 1027 | } |
| 1028 | |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 1029 | /* |
| 1030 | * mods - set of removed modules, if NULL all modules are supposed to be removed so any backlink is invalid |
| 1031 | */ |
Michal Vasko | 4be5fa2 | 2017-02-07 10:12:32 +0100 | [diff] [blame] | 1032 | static void |
| 1033 | ctx_modules_undo_backlinks(struct ly_ctx *ctx, struct ly_set *mods) |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 1034 | { |
| 1035 | int o; |
| 1036 | uint8_t j; |
| 1037 | unsigned int u, v; |
| 1038 | struct lys_module *mod; |
| 1039 | struct lys_node *elem, *next; |
| 1040 | struct lys_node_leaf *leaf; |
| 1041 | |
| 1042 | /* maintain backlinks (start with internal ietf-yang-library which have leafs as possible targets of leafrefs */ |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 1043 | for (o = ctx->internal_module_count - 1; o < ctx->models.used; o++) { |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 1044 | mod = ctx->models.list[o]; /* shortcut */ |
| 1045 | |
| 1046 | /* 1) features */ |
| 1047 | for (j = 0; j < mod->features_size; j++) { |
| 1048 | if (!mod->features[j].depfeatures) { |
| 1049 | continue; |
| 1050 | } |
| 1051 | for (v = 0; v < mod->features[j].depfeatures->number; v++) { |
| 1052 | if (!mods || ly_set_contains(mods, ((struct lys_feature *)mod->features[j].depfeatures->set.g[v])->module) != -1) { |
| 1053 | /* depending feature is in module to remove */ |
| 1054 | ly_set_rm_index(mod->features[j].depfeatures, v); |
| 1055 | v--; |
| 1056 | } |
| 1057 | } |
| 1058 | if (!mod->features[j].depfeatures->number) { |
| 1059 | /* all backlinks removed */ |
| 1060 | ly_set_free(mod->features[j].depfeatures); |
| 1061 | mod->features[j].depfeatures = NULL; |
| 1062 | } |
| 1063 | } |
Michal Vasko | 4be5fa2 | 2017-02-07 10:12:32 +0100 | [diff] [blame] | 1064 | |
| 1065 | /* 2) identities */ |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 1066 | for (u = 0; u < mod->ident_size; u++) { |
| 1067 | if (!mod->ident[u].der) { |
| 1068 | continue; |
| 1069 | } |
| 1070 | for (v = 0; v < mod->ident[u].der->number; v++) { |
| 1071 | if (!mods || ly_set_contains(mods, ((struct lys_ident *)mod->ident[u].der->set.g[v])->module) != -1) { |
| 1072 | /* derived identity is in module to remove */ |
| 1073 | ly_set_rm_index(mod->ident[u].der, v); |
| 1074 | v--; |
| 1075 | } |
| 1076 | } |
| 1077 | if (!mod->ident[u].der->number) { |
| 1078 | /* all backlinks removed */ |
| 1079 | ly_set_free(mod->ident[u].der); |
| 1080 | mod->ident[u].der = NULL; |
| 1081 | } |
| 1082 | } |
| 1083 | |
Michal Vasko | 4be5fa2 | 2017-02-07 10:12:32 +0100 | [diff] [blame] | 1084 | /* 3) leafrefs */ |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 1085 | for (elem = next = mod->data; elem; elem = next) { |
| 1086 | if (elem->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 1087 | leaf = (struct lys_node_leaf *)elem; /* shortcut */ |
| 1088 | if (leaf->backlinks) { |
| 1089 | if (!mods) { |
| 1090 | /* remove all backlinks */ |
| 1091 | ly_set_free(leaf->backlinks); |
| 1092 | leaf->backlinks = NULL; |
| 1093 | } else { |
| 1094 | for (v = 0; v < leaf->backlinks->number; v++) { |
| 1095 | if (ly_set_contains(mods, leaf->backlinks->set.s[v]->module) != -1) { |
| 1096 | /* derived identity is in module to remove */ |
| 1097 | ly_set_rm_index(leaf->backlinks, v); |
| 1098 | v--; |
| 1099 | } |
| 1100 | } |
| 1101 | if (!leaf->backlinks->number) { |
| 1102 | /* all backlinks removed */ |
| 1103 | ly_set_free(leaf->backlinks); |
| 1104 | leaf->backlinks = NULL; |
| 1105 | } |
| 1106 | } |
| 1107 | } |
| 1108 | } |
| 1109 | |
| 1110 | /* select next element to process */ |
| 1111 | next = elem->child; |
| 1112 | /* child exception for leafs, leaflists, anyxml and groupings */ |
| 1113 | if (elem->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA | LYS_GROUPING)) { |
| 1114 | next = NULL; |
| 1115 | } |
| 1116 | if (!next) { |
| 1117 | /* no children, try siblings */ |
| 1118 | next = elem->next; |
| 1119 | } |
| 1120 | while (!next) { |
| 1121 | /* parent is already processed, go to its sibling */ |
| 1122 | elem = lys_parent(elem); |
| 1123 | if (!elem) { |
| 1124 | /* we are done, no next element to process */ |
| 1125 | break; |
| 1126 | } |
| 1127 | /* no siblings, go back through parents */ |
| 1128 | next = elem->next; |
| 1129 | } |
| 1130 | } |
| 1131 | } |
Michal Vasko | 4be5fa2 | 2017-02-07 10:12:32 +0100 | [diff] [blame] | 1132 | } |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 1133 | |
Michal Vasko | 4be5fa2 | 2017-02-07 10:12:32 +0100 | [diff] [blame] | 1134 | static int |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 1135 | ctx_modules_redo_backlinks(struct ly_set *mods) |
Michal Vasko | 4be5fa2 | 2017-02-07 10:12:32 +0100 | [diff] [blame] | 1136 | { |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 1137 | unsigned int i, j, k, s; |
| 1138 | struct lys_module *mod; |
| 1139 | struct lys_node *next, *elem; |
| 1140 | struct lys_type *type; |
| 1141 | struct lys_feature *feat; |
Michal Vasko | 4be5fa2 | 2017-02-07 10:12:32 +0100 | [diff] [blame] | 1142 | |
Michal Vasko | 4be5fa2 | 2017-02-07 10:12:32 +0100 | [diff] [blame] | 1143 | for (i = 0; i < mods->number; ++i) { |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 1144 | mod = (struct lys_module *)mods->set.g[i]; /* shortcut */ |
Michal Vasko | 4be5fa2 | 2017-02-07 10:12:32 +0100 | [diff] [blame] | 1145 | |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 1146 | /* identities */ |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 1147 | if (mod->implemented) { |
| 1148 | for (j = 0; j < mod->ident_size; j++) { |
| 1149 | for (k = 0; k < mod->ident[j].base_size; k++) { |
| 1150 | resolve_identity_backlink_update(&mod->ident[j], mod->ident[j].base[k]); |
| 1151 | } |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 1152 | } |
| 1153 | } |
Michal Vasko | 4be5fa2 | 2017-02-07 10:12:32 +0100 | [diff] [blame] | 1154 | |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 1155 | /* features */ |
| 1156 | for (j = 0; j < mod->features_size; j++) { |
| 1157 | for (k = 0; k < mod->features[j].iffeature_size; k++) { |
| 1158 | resolve_iffeature_getsizes(&mod->features[j].iffeature[k], NULL, &s); |
| 1159 | while (s--) { |
| 1160 | feat = mod->features[j].iffeature[k].features[s]; /* shortcut */ |
| 1161 | if (!feat->depfeatures) { |
| 1162 | feat->depfeatures = ly_set_new(); |
| 1163 | } |
| 1164 | ly_set_add(feat->depfeatures, &mod->features[j], LY_SET_OPT_USEASLIST); |
| 1165 | } |
| 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | /* leafrefs */ |
| 1170 | LY_TREE_DFS_BEGIN(mod->data, next, elem) { |
Michal Vasko | fa3d2f7 | 2017-04-10 13:30:44 +0200 | [diff] [blame] | 1171 | if (elem->nodetype == LYS_GROUPING) { |
| 1172 | goto next_sibling; |
| 1173 | } |
| 1174 | |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 1175 | if (elem->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 1176 | type = &((struct lys_node_leaf *)elem)->type; /* shortcut */ |
| 1177 | if (type->base == LY_TYPE_LEAFREF) { |
| 1178 | lys_leaf_add_leafref_target(type->info.lref.target, elem); |
| 1179 | } |
| 1180 | } |
| 1181 | |
Michal Vasko | fa3d2f7 | 2017-04-10 13:30:44 +0200 | [diff] [blame] | 1182 | /* select element for the next run - children first */ |
| 1183 | next = elem->child; |
| 1184 | |
| 1185 | /* child exception for leafs, leaflists and anyxml without children */ |
| 1186 | if (elem->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 1187 | next = NULL; |
| 1188 | } |
| 1189 | if (!next) { |
| 1190 | next_sibling: |
| 1191 | /* no children */ |
| 1192 | if (elem == mod->data) { |
| 1193 | /* we are done, (START) has no children */ |
| 1194 | break; |
| 1195 | } |
| 1196 | /* try siblings */ |
| 1197 | next = elem->next; |
| 1198 | } |
| 1199 | while (!next) { |
| 1200 | /* parent is already processed, go to its sibling */ |
| 1201 | elem = lys_parent(elem); |
| 1202 | |
| 1203 | /* no siblings, go back through parents */ |
| 1204 | if (lys_parent(elem) == lys_parent(mod->data)) { |
| 1205 | /* we are done, no next element to process */ |
| 1206 | break; |
| 1207 | } |
| 1208 | next = elem->next; |
| 1209 | } |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 1210 | } |
Michal Vasko | 4be5fa2 | 2017-02-07 10:12:32 +0100 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | return 0; |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 1214 | } |
| 1215 | |
Radek Krejci | 8c107fe | 2016-10-17 16:00:18 +0200 | [diff] [blame] | 1216 | API int |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 1217 | lys_set_disabled(const struct lys_module *module) |
| 1218 | { |
| 1219 | struct ly_ctx *ctx; /* shortcut */ |
| 1220 | struct lys_module *mod; |
| 1221 | struct ly_set *mods; |
| 1222 | uint8_t j, imported; |
| 1223 | int i, o; |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 1224 | unsigned int u, v; |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 1225 | |
| 1226 | if (!module) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1227 | LOGARG; |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 1228 | return EXIT_FAILURE; |
| 1229 | } else if (module->disabled) { |
| 1230 | /* already disabled module */ |
| 1231 | return EXIT_SUCCESS; |
| 1232 | } |
| 1233 | mod = (struct lys_module *)module; |
| 1234 | ctx = mod->ctx; |
| 1235 | |
| 1236 | /* avoid disabling internal modules */ |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 1237 | for (i = 0; i < ctx->internal_module_count; i++) { |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 1238 | if (mod == ctx->models.list[i]) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1239 | LOGERR(ctx, LY_EINVAL, "Internal module \"%s\" cannot be disabled.", mod->name); |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 1240 | return EXIT_FAILURE; |
| 1241 | } |
| 1242 | } |
| 1243 | |
| 1244 | /* disable the module */ |
| 1245 | mod->disabled = 1; |
| 1246 | |
| 1247 | /* get the complete list of modules to disable because of dependencies, |
| 1248 | * we are going also to disable all the imported (not implemented) modules |
| 1249 | * that are not used in any other module */ |
| 1250 | mods = ly_set_new(); |
| 1251 | ly_set_add(mods, mod, 0); |
| 1252 | checkdependency: |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 1253 | for (i = ctx->internal_module_count; i < ctx->models.used; i++) { |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 1254 | mod = ctx->models.list[i]; /* shortcut */ |
| 1255 | if (mod->disabled) { |
| 1256 | /* skip the already disabled modules */ |
| 1257 | continue; |
| 1258 | } |
| 1259 | |
| 1260 | /* check depndency of imported modules */ |
| 1261 | for (j = 0; j < mod->imp_size; j++) { |
| 1262 | for (u = 0; u < mods->number; u++) { |
| 1263 | if (mod->imp[j].module == mods->set.g[u]) { |
| 1264 | /* module is importing some module to disable, so it must be also disabled */ |
| 1265 | mod->disabled = 1; |
| 1266 | ly_set_add(mods, mod, 0); |
| 1267 | /* we have to start again because some of the already checked modules can |
| 1268 | * depend on the one we have just decided to disable */ |
| 1269 | goto checkdependency; |
| 1270 | } |
| 1271 | } |
| 1272 | } |
| 1273 | /* check if the imported module is used in any module supposed to be kept */ |
| 1274 | if (!mod->implemented) { |
| 1275 | imported = 0; |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 1276 | for (o = ctx->internal_module_count; o < ctx->models.used; o++) { |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 1277 | if (ctx->models.list[o]->disabled) { |
| 1278 | /* skip modules already disabled */ |
| 1279 | continue; |
| 1280 | } |
| 1281 | for (j = 0; j < ctx->models.list[o]->imp_size; j++) { |
| 1282 | if (ctx->models.list[o]->imp[j].module == mod) { |
| 1283 | /* the module is used in some other module not yet selected to be disabled */ |
| 1284 | imported = 1; |
| 1285 | goto imported; |
| 1286 | } |
| 1287 | } |
| 1288 | } |
| 1289 | imported: |
| 1290 | if (!imported) { |
| 1291 | /* module is not implemented and neither imported by any other module in context |
| 1292 | * which is supposed to be kept enabled after this operation, so we are going to disable also |
| 1293 | * this module */ |
| 1294 | mod->disabled = 1; |
| 1295 | ly_set_add(mods, mod, 0); |
| 1296 | /* we have to start again, this time not because other module can depend on this one |
| 1297 | * (we know that there is no such module), but because the module can import module |
| 1298 | * that could became useless. If there are no imports, we can continue */ |
| 1299 | if (mod->imp_size) { |
| 1300 | goto checkdependency; |
| 1301 | } |
| 1302 | } |
| 1303 | } |
| 1304 | } |
| 1305 | |
| 1306 | /* before removing applied deviations, augments and updating leafrefs, we have to enable the modules |
| 1307 | * to disable to allow all that operations */ |
| 1308 | for (u = 0; u < mods->number; u++) { |
| 1309 | ((struct lys_module *)mods->set.g[u])->disabled = 0; |
| 1310 | } |
| 1311 | |
| 1312 | /* maintain backlinks (start with internal ietf-yang-library which have leafs as possible targets of leafrefs */ |
Michal Vasko | 4be5fa2 | 2017-02-07 10:12:32 +0100 | [diff] [blame] | 1313 | ctx_modules_undo_backlinks(ctx, mods); |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 1314 | |
| 1315 | /* remove the applied deviations and augments */ |
Michal Vasko | adafded | 2018-03-19 14:28:26 +0100 | [diff] [blame] | 1316 | u = mods->number; |
| 1317 | while (u--) { |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 1318 | lys_sub_module_remove_devs_augs((struct lys_module *)mods->set.g[u]); |
| 1319 | } |
| 1320 | |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 1321 | /* now again disable the modules to disable and disable also all its submodules */ |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 1322 | for (u = 0; u < mods->number; u++) { |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 1323 | mod = (struct lys_module *)mods->set.g[u]; |
| 1324 | mod->disabled = 1; |
| 1325 | for (v = 0; v < mod->inc_size; v++) { |
| 1326 | mod->inc[v].submodule->disabled = 1; |
| 1327 | } |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 1328 | } |
| 1329 | |
| 1330 | /* free the set */ |
| 1331 | ly_set_free(mods); |
| 1332 | |
| 1333 | /* update the module-set-id */ |
| 1334 | ctx->models.module_set_id++; |
| 1335 | |
| 1336 | return EXIT_SUCCESS; |
| 1337 | } |
| 1338 | |
| 1339 | static void |
| 1340 | lys_set_enabled_(struct ly_set *mods, struct lys_module *mod) |
| 1341 | { |
| 1342 | unsigned int i; |
| 1343 | |
| 1344 | ly_set_add(mods, mod, 0); |
| 1345 | mod->disabled = 0; |
| 1346 | |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 1347 | for (i = 0; i < mod->inc_size; i++) { |
| 1348 | mod->inc[i].submodule->disabled = 0; |
| 1349 | } |
| 1350 | |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 1351 | /* go recursively */ |
| 1352 | for (i = 0; i < mod->imp_size; i++) { |
| 1353 | if (!mod->imp[i].module->disabled) { |
| 1354 | continue; |
| 1355 | } |
| 1356 | |
| 1357 | lys_set_enabled_(mods, mod->imp[i].module); |
| 1358 | } |
| 1359 | } |
| 1360 | |
| 1361 | API int |
| 1362 | lys_set_enabled(const struct lys_module *module) |
| 1363 | { |
| 1364 | struct ly_ctx *ctx; /* shortcut */ |
| 1365 | struct lys_module *mod; |
| 1366 | struct ly_set *mods, *disabled; |
| 1367 | int i; |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 1368 | unsigned int u, v, w; |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 1369 | |
| 1370 | if (!module) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1371 | LOGARG; |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 1372 | return EXIT_FAILURE; |
| 1373 | } else if (!module->disabled) { |
| 1374 | /* already enabled module */ |
| 1375 | return EXIT_SUCCESS; |
| 1376 | } |
| 1377 | mod = (struct lys_module *)module; |
| 1378 | ctx = mod->ctx; |
| 1379 | |
| 1380 | /* avoid disabling internal modules */ |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 1381 | for (i = 0; i < ctx->internal_module_count; i++) { |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 1382 | if (mod == ctx->models.list[i]) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1383 | LOGERR(ctx, LY_EINVAL, "Internal module \"%s\" cannot be removed.", mod->name); |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 1384 | return EXIT_FAILURE; |
| 1385 | } |
| 1386 | } |
| 1387 | |
| 1388 | mods = ly_set_new(); |
| 1389 | disabled = ly_set_new(); |
| 1390 | |
| 1391 | /* enable the module, including its dependencies */ |
| 1392 | lys_set_enabled_(mods, mod); |
| 1393 | |
| 1394 | /* we will go through the all disabled modules in the context, if the module has no dependency (import) |
| 1395 | * that is still disabled AND at least one of its imported module is from the set we are enabling now, |
| 1396 | * it is going to be also enabled. This way we try to revert everething that was possibly done by |
| 1397 | * lys_set_disabled(). */ |
| 1398 | checkdependency: |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 1399 | for (i = ctx->internal_module_count; i < ctx->models.used; i++) { |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 1400 | mod = ctx->models.list[i]; /* shortcut */ |
| 1401 | if (!mod->disabled || ly_set_contains(disabled, mod) != -1) { |
| 1402 | /* skip the enabled modules */ |
| 1403 | continue; |
| 1404 | } |
| 1405 | |
| 1406 | /* check imported modules */ |
| 1407 | for (u = 0; u < mod->imp_size; u++) { |
| 1408 | if (mod->imp[u].module->disabled) { |
| 1409 | /* it has disabled dependency so it must stay disabled */ |
| 1410 | break; |
| 1411 | } |
| 1412 | } |
| 1413 | if (u < mod->imp_size) { |
| 1414 | /* it has disabled dependency, continue with the next module in the context */ |
| 1415 | continue; |
| 1416 | } |
| 1417 | |
| 1418 | /* get know if at least one of the imported modules is being enabled this time */ |
| 1419 | for (u = 0; u < mod->imp_size; u++) { |
| 1420 | for (v = 0; v < mods->number; v++) { |
| 1421 | if (mod->imp[u].module == mods->set.g[v]) { |
| 1422 | /* yes, it is, so they are connected and we are going to enable it as well, |
| 1423 | * it is not necessary to call recursive lys_set_enable_() because we already |
| 1424 | * know that there is no disabled import to enable */ |
| 1425 | mod->disabled = 0; |
| 1426 | ly_set_add(mods, mod, 0); |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 1427 | for (w = 0; w < mod->inc_size; w++) { |
| 1428 | mod->inc[w].submodule->disabled = 0; |
| 1429 | } |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 1430 | /* we have to start again because some of the already checked modules can |
| 1431 | * depend on the one we have just decided to enable */ |
| 1432 | goto checkdependency; |
| 1433 | } |
| 1434 | } |
| 1435 | } |
| 1436 | |
| 1437 | /* this module is disabled, but it does not depend on any other disabled module and none |
| 1438 | * of its imports was not enabled in this call. No future enabling of the disabled module |
| 1439 | * will change this so we can remember the module and skip it next time we will have to go |
| 1440 | * through the all context because of the checkdependency goto. |
| 1441 | */ |
| 1442 | ly_set_add(disabled, mod, 0); |
| 1443 | } |
| 1444 | |
| 1445 | /* maintain backlinks (start with internal ietf-yang-library which have leafs as possible targets of leafrefs */ |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 1446 | ctx_modules_redo_backlinks(mods); |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 1447 | |
| 1448 | /* re-apply the deviations and augments */ |
| 1449 | for (v = 0; v < mods->number; v++) { |
Michal Vasko | b87da77 | 2018-06-15 11:31:22 +0200 | [diff] [blame] | 1450 | if (((struct lys_module *)mods->set.g[v])->implemented) { |
| 1451 | lys_sub_module_apply_devs_augs((struct lys_module *)mods->set.g[v]); |
| 1452 | } |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 1453 | } |
| 1454 | |
| 1455 | /* free the sets */ |
| 1456 | ly_set_free(mods); |
| 1457 | ly_set_free(disabled); |
| 1458 | |
| 1459 | /* update the module-set-id */ |
| 1460 | ctx->models.module_set_id++; |
| 1461 | |
| 1462 | return EXIT_SUCCESS; |
| 1463 | } |
| 1464 | |
| 1465 | API int |
| 1466 | ly_ctx_remove_module(const struct lys_module *module, |
Radek Krejci | 8c107fe | 2016-10-17 16:00:18 +0200 | [diff] [blame] | 1467 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
| 1468 | { |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 1469 | struct ly_ctx *ctx; /* shortcut */ |
Radek Krejci | 8c107fe | 2016-10-17 16:00:18 +0200 | [diff] [blame] | 1470 | struct lys_module *mod = NULL; |
| 1471 | struct ly_set *mods; |
| 1472 | uint8_t j, imported; |
| 1473 | int i, o; |
| 1474 | unsigned int u; |
| 1475 | |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 1476 | if (!module) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1477 | LOGARG; |
Radek Krejci | 8c107fe | 2016-10-17 16:00:18 +0200 | [diff] [blame] | 1478 | return EXIT_FAILURE; |
| 1479 | } |
| 1480 | |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 1481 | mod = (struct lys_module *)module; |
| 1482 | ctx = mod->ctx; |
| 1483 | |
Radek Krejci | 8c107fe | 2016-10-17 16:00:18 +0200 | [diff] [blame] | 1484 | /* avoid removing internal modules ... */ |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 1485 | for (i = 0; i < ctx->internal_module_count; i++) { |
Radek Krejci | 8c107fe | 2016-10-17 16:00:18 +0200 | [diff] [blame] | 1486 | if (mod == ctx->models.list[i]) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1487 | LOGERR(ctx, LY_EINVAL, "Internal module \"%s\" cannot be removed.", mod->name); |
Radek Krejci | 8c107fe | 2016-10-17 16:00:18 +0200 | [diff] [blame] | 1488 | return EXIT_FAILURE; |
| 1489 | } |
| 1490 | } |
| 1491 | /* ... and hide the module from the further processing of the context modules list */ |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 1492 | for (i = ctx->internal_module_count; i < ctx->models.used; i++) { |
Radek Krejci | 8c107fe | 2016-10-17 16:00:18 +0200 | [diff] [blame] | 1493 | if (mod == ctx->models.list[i]) { |
| 1494 | ctx->models.list[i] = NULL; |
| 1495 | break; |
| 1496 | } |
| 1497 | } |
| 1498 | |
| 1499 | /* get the complete list of modules to remove because of dependencies, |
| 1500 | * we are going also to remove all the imported (not implemented) modules |
| 1501 | * that are not used in any other module */ |
| 1502 | mods = ly_set_new(); |
| 1503 | ly_set_add(mods, mod, 0); |
| 1504 | checkdependency: |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 1505 | for (i = ctx->internal_module_count; i < ctx->models.used; i++) { |
Radek Krejci | 8c107fe | 2016-10-17 16:00:18 +0200 | [diff] [blame] | 1506 | mod = ctx->models.list[i]; /* shortcut */ |
| 1507 | if (!mod) { |
| 1508 | /* skip modules already selected for removing */ |
| 1509 | continue; |
| 1510 | } |
| 1511 | |
| 1512 | /* check depndency of imported modules */ |
| 1513 | for (j = 0; j < mod->imp_size; j++) { |
| 1514 | for (u = 0; u < mods->number; u++) { |
| 1515 | if (mod->imp[j].module == mods->set.g[u]) { |
| 1516 | /* module is importing some module to remove, so it must be also removed */ |
| 1517 | ly_set_add(mods, mod, 0); |
| 1518 | ctx->models.list[i] = NULL; |
| 1519 | /* we have to start again because some of the already checked modules can |
| 1520 | * depend on the one we have just decided to remove */ |
| 1521 | goto checkdependency; |
| 1522 | } |
| 1523 | } |
| 1524 | } |
| 1525 | /* check if the imported module is used in any module supposed to be kept */ |
| 1526 | if (!mod->implemented) { |
| 1527 | imported = 0; |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 1528 | for (o = ctx->internal_module_count; o < ctx->models.used; o++) { |
Radek Krejci | 8c107fe | 2016-10-17 16:00:18 +0200 | [diff] [blame] | 1529 | if (!ctx->models.list[o]) { |
| 1530 | /* skip modules already selected for removing */ |
| 1531 | continue; |
| 1532 | } |
| 1533 | for (j = 0; j < ctx->models.list[o]->imp_size; j++) { |
| 1534 | if (ctx->models.list[o]->imp[j].module == mod) { |
| 1535 | /* the module is used in some other module not yet selected to be deleted */ |
| 1536 | imported = 1; |
| 1537 | goto imported; |
| 1538 | } |
| 1539 | } |
| 1540 | } |
| 1541 | imported: |
| 1542 | if (!imported) { |
| 1543 | /* module is not implemented and neither imported by any other module in context |
| 1544 | * which is supposed to be kept after this operation, so we are going to remove also |
| 1545 | * this useless module */ |
| 1546 | ly_set_add(mods, mod, 0); |
| 1547 | ctx->models.list[i] = NULL; |
| 1548 | /* we have to start again, this time not because other module can depend on this one |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 1549 | * (we know that there is no such module), but because the module can import module |
Radek Krejci | 8c107fe | 2016-10-17 16:00:18 +0200 | [diff] [blame] | 1550 | * that could became useless. If there are no imports, we can continue */ |
| 1551 | if (mod->imp_size) { |
| 1552 | goto checkdependency; |
| 1553 | } |
| 1554 | } |
| 1555 | } |
| 1556 | } |
| 1557 | |
Radek Krejci | 8c107fe | 2016-10-17 16:00:18 +0200 | [diff] [blame] | 1558 | |
| 1559 | /* consolidate the modules list */ |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 1560 | for (i = o = ctx->internal_module_count; i < ctx->models.used; i++) { |
Radek Krejci | 8c107fe | 2016-10-17 16:00:18 +0200 | [diff] [blame] | 1561 | if (ctx->models.list[o]) { |
| 1562 | /* used cell */ |
| 1563 | o++; |
| 1564 | } else { |
| 1565 | /* the current output cell is empty, move here an input cell */ |
| 1566 | ctx->models.list[o] = ctx->models.list[i]; |
| 1567 | ctx->models.list[i] = NULL; |
| 1568 | } |
| 1569 | } |
| 1570 | /* get the last used cell to get know the number of used */ |
| 1571 | while (!ctx->models.list[o]) { |
| 1572 | o--; |
| 1573 | } |
| 1574 | ctx->models.used = o + 1; |
| 1575 | ctx->models.module_set_id++; |
| 1576 | |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 1577 | /* maintain backlinks (start with internal ietf-yang-library which have leafs as possible targets of leafrefs */ |
Michal Vasko | 4be5fa2 | 2017-02-07 10:12:32 +0100 | [diff] [blame] | 1578 | ctx_modules_undo_backlinks(ctx, mods); |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 1579 | |
| 1580 | /* free the modules */ |
| 1581 | for (u = 0; u < mods->number; u++) { |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 1582 | /* remove the applied deviations and augments */ |
| 1583 | lys_sub_module_remove_devs_augs((struct lys_module *)mods->set.g[u]); |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 1584 | /* remove the module */ |
Michal Vasko | 10681e8 | 2018-01-16 14:54:16 +0100 | [diff] [blame] | 1585 | lys_free((struct lys_module *)mods->set.g[u], private_destructor, 1, 0); |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 1586 | } |
| 1587 | ly_set_free(mods); |
| 1588 | |
Radek Krejci | 8c107fe | 2016-10-17 16:00:18 +0200 | [diff] [blame] | 1589 | return EXIT_SUCCESS; |
| 1590 | } |
| 1591 | |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 1592 | API void |
| 1593 | ly_ctx_clean(struct ly_ctx *ctx, void (*private_destructor)(const struct lys_node *node, void *priv)) |
| 1594 | { |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 1595 | if (!ctx) { |
| 1596 | return; |
| 1597 | } |
| 1598 | |
| 1599 | /* models list */ |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 1600 | for (; ctx->models.used > ctx->internal_module_count; ctx->models.used--) { |
Radek Krejci | fc8411a | 2017-02-03 10:26:05 +0100 | [diff] [blame] | 1601 | /* remove the applied deviations and augments */ |
Radek Krejci | c436a23 | 2017-02-08 14:43:11 +0100 | [diff] [blame] | 1602 | lys_sub_module_remove_devs_augs(ctx->models.list[ctx->models.used - 1]); |
Radek Krejci | fc8411a | 2017-02-03 10:26:05 +0100 | [diff] [blame] | 1603 | /* remove the module */ |
Michal Vasko | 10681e8 | 2018-01-16 14:54:16 +0100 | [diff] [blame] | 1604 | lys_free(ctx->models.list[ctx->models.used - 1], private_destructor, 1, 0); |
Radek Krejci | fc8411a | 2017-02-03 10:26:05 +0100 | [diff] [blame] | 1605 | /* clean it for safer future use */ |
Radek Krejci | c436a23 | 2017-02-08 14:43:11 +0100 | [diff] [blame] | 1606 | ctx->models.list[ctx->models.used - 1] = NULL; |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 1607 | } |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 1608 | ctx->models.module_set_id++; |
| 1609 | |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 1610 | /* maintain backlinks (actually done only with ietf-yang-library since its leafs can be target of leafref) */ |
Michal Vasko | 4be5fa2 | 2017-02-07 10:12:32 +0100 | [diff] [blame] | 1611 | ctx_modules_undo_backlinks(ctx, NULL); |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 1612 | } |
| 1613 | |
Michal Vasko | d7957c0 | 2016-04-01 10:27:26 +0200 | [diff] [blame] | 1614 | API const struct lys_module * |
| 1615 | ly_ctx_get_module_iter(const struct ly_ctx *ctx, uint32_t *idx) |
| 1616 | { |
| 1617 | if (!ctx || !idx) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1618 | LOGARG; |
Michal Vasko | d7957c0 | 2016-04-01 10:27:26 +0200 | [diff] [blame] | 1619 | return NULL; |
| 1620 | } |
| 1621 | |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 1622 | for ( ; *idx < (unsigned)ctx->models.used; (*idx)++) { |
| 1623 | if (!ctx->models.list[(*idx)]->disabled) { |
| 1624 | return ctx->models.list[(*idx)++]; |
| 1625 | } |
| 1626 | } |
| 1627 | |
| 1628 | return NULL; |
| 1629 | } |
| 1630 | |
| 1631 | API const struct lys_module * |
| 1632 | ly_ctx_get_disabled_module_iter(const struct ly_ctx *ctx, uint32_t *idx) |
| 1633 | { |
| 1634 | if (!ctx || !idx) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1635 | LOGARG; |
Michal Vasko | d7957c0 | 2016-04-01 10:27:26 +0200 | [diff] [blame] | 1636 | return NULL; |
| 1637 | } |
| 1638 | |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 1639 | for ( ; *idx < (unsigned)ctx->models.used; (*idx)++) { |
| 1640 | if (ctx->models.list[(*idx)]->disabled) { |
| 1641 | return ctx->models.list[(*idx)++]; |
| 1642 | } |
| 1643 | } |
| 1644 | |
| 1645 | return NULL; |
Michal Vasko | d7957c0 | 2016-04-01 10:27:26 +0200 | [diff] [blame] | 1646 | } |
| 1647 | |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1648 | static int |
Michal Vasko | eb3bd0e | 2018-01-26 11:52:11 +0100 | [diff] [blame] | 1649 | ylib_feature(struct lyd_node *parent, struct lys_module *cur_mod, int bis) |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1650 | { |
| 1651 | int i, j; |
Michal Vasko | eb3bd0e | 2018-01-26 11:52:11 +0100 | [diff] [blame] | 1652 | struct lyd_node *list; |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1653 | |
| 1654 | /* module features */ |
| 1655 | for (i = 0; i < cur_mod->features_size; ++i) { |
| 1656 | if (!(cur_mod->features[i].flags & LYS_FENABLED)) { |
| 1657 | continue; |
| 1658 | } |
| 1659 | |
Michal Vasko | eb3bd0e | 2018-01-26 11:52:11 +0100 | [diff] [blame] | 1660 | if (bis) { |
| 1661 | if (!(list = lyd_new(parent, NULL, "feature")) || !lyd_new_leaf(list, NULL, "name", cur_mod->features[i].name)) { |
| 1662 | return EXIT_FAILURE; |
| 1663 | } |
| 1664 | } else if (!lyd_new_leaf(parent, NULL, "feature", cur_mod->features[i].name)) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1665 | return EXIT_FAILURE; |
| 1666 | } |
| 1667 | } |
| 1668 | |
| 1669 | /* submodule features */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 1670 | for (i = 0; i < cur_mod->inc_size && cur_mod->inc[i].submodule; ++i) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1671 | for (j = 0; j < cur_mod->inc[i].submodule->features_size; ++j) { |
| 1672 | if (!(cur_mod->inc[i].submodule->features[j].flags & LYS_FENABLED)) { |
| 1673 | continue; |
| 1674 | } |
| 1675 | |
Michal Vasko | eb3bd0e | 2018-01-26 11:52:11 +0100 | [diff] [blame] | 1676 | if (bis) { |
| 1677 | if (!(list = lyd_new(parent, NULL, "feature")) |
| 1678 | || !lyd_new_leaf(list, NULL, "name", cur_mod->inc[i].submodule->features[j].name)) { |
| 1679 | return EXIT_FAILURE; |
| 1680 | } |
| 1681 | } else if (!lyd_new_leaf(parent, NULL, "feature", cur_mod->inc[i].submodule->features[j].name)) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1682 | return EXIT_FAILURE; |
| 1683 | } |
| 1684 | } |
| 1685 | } |
| 1686 | |
| 1687 | return EXIT_SUCCESS; |
| 1688 | } |
| 1689 | |
| 1690 | static int |
Michal Vasko | eb3bd0e | 2018-01-26 11:52:11 +0100 | [diff] [blame] | 1691 | ylib_deviation(struct lyd_node *parent, struct lys_module *cur_mod, int bis) |
Michal Vasko | 97d8c6d | 2016-02-12 11:05:48 +0100 | [diff] [blame] | 1692 | { |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 1693 | uint32_t i = 0, j; |
| 1694 | const struct lys_module *mod; |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1695 | struct lyd_node *cont; |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 1696 | const char *ptr; |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1697 | |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 1698 | if (cur_mod->deviated) { |
| 1699 | while ((mod = ly_ctx_get_module_iter(cur_mod->ctx, &i))) { |
| 1700 | if (mod == cur_mod) { |
| 1701 | continue; |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1702 | } |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1703 | |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 1704 | for (j = 0; j < mod->deviation_size; ++j) { |
| 1705 | ptr = strstr(mod->deviation[j].target_name, cur_mod->name); |
| 1706 | if (ptr && ptr[strlen(cur_mod->name)] == ':') { |
| 1707 | cont = lyd_new(parent, NULL, "deviation"); |
| 1708 | if (!cont) { |
| 1709 | return EXIT_FAILURE; |
| 1710 | } |
| 1711 | |
Michal Vasko | eb3bd0e | 2018-01-26 11:52:11 +0100 | [diff] [blame] | 1712 | if (bis) { |
| 1713 | if (!lyd_new_leaf(cont, NULL, "module", mod->name)) { |
| 1714 | return EXIT_FAILURE; |
| 1715 | } |
| 1716 | } else { |
| 1717 | if (!lyd_new_leaf(cont, NULL, "name", mod->name)) { |
| 1718 | return EXIT_FAILURE; |
| 1719 | } |
| 1720 | if (!lyd_new_leaf(cont, NULL, "revision", (mod->rev_size ? mod->rev[0].date : ""))) { |
| 1721 | return EXIT_FAILURE; |
| 1722 | } |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 1723 | } |
Michal Vasko | 0c2215b | 2016-08-25 14:18:43 +0200 | [diff] [blame] | 1724 | |
| 1725 | break; |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 1726 | } |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1727 | } |
| 1728 | } |
| 1729 | } |
| 1730 | |
| 1731 | return EXIT_SUCCESS; |
| 1732 | } |
| 1733 | |
| 1734 | static int |
Michal Vasko | eb3bd0e | 2018-01-26 11:52:11 +0100 | [diff] [blame] | 1735 | ylib_submodules(struct lyd_node *parent, struct lys_module *cur_mod, int bis) |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1736 | { |
| 1737 | int i; |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 1738 | char *str; |
Radek Krejci | d972391 | 2016-09-16 17:06:06 +0200 | [diff] [blame] | 1739 | struct lyd_node *item; |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1740 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 1741 | for (i = 0; i < cur_mod->inc_size && cur_mod->inc[i].submodule; ++i) { |
Radek Krejci | d972391 | 2016-09-16 17:06:06 +0200 | [diff] [blame] | 1742 | item = lyd_new(parent, NULL, "submodule"); |
Radek Krejci | 6e05cea | 2015-12-10 16:34:37 +0100 | [diff] [blame] | 1743 | if (!item) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1744 | return EXIT_FAILURE; |
| 1745 | } |
| 1746 | |
Radek Krejci | 6e05cea | 2015-12-10 16:34:37 +0100 | [diff] [blame] | 1747 | if (!lyd_new_leaf(item, NULL, "name", cur_mod->inc[i].submodule->name)) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1748 | return EXIT_FAILURE; |
| 1749 | } |
Michal Vasko | eb3bd0e | 2018-01-26 11:52:11 +0100 | [diff] [blame] | 1750 | if ((!bis || cur_mod->inc[i].submodule->rev_size) |
| 1751 | && !lyd_new_leaf(item, NULL, "revision", |
| 1752 | (cur_mod->inc[i].submodule->rev_size ? cur_mod->inc[i].submodule->rev[0].date : ""))) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1753 | return EXIT_FAILURE; |
| 1754 | } |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 1755 | if (cur_mod->inc[i].submodule->filepath) { |
Radek Krejci | 95aa201 | 2016-03-03 11:21:09 +0100 | [diff] [blame] | 1756 | if (asprintf(&str, "file://%s", cur_mod->inc[i].submodule->filepath) == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1757 | LOGMEM(cur_mod->ctx); |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 1758 | return EXIT_FAILURE; |
Michal Vasko | eb3bd0e | 2018-01-26 11:52:11 +0100 | [diff] [blame] | 1759 | } else if (!lyd_new_leaf(item, NULL, bis ? "location" : "schema", str)) { |
Radek Krejci | 19f9ede | 2016-02-25 16:29:21 +0100 | [diff] [blame] | 1760 | free(str); |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 1761 | return EXIT_FAILURE; |
| 1762 | } |
Radek Krejci | 19f9ede | 2016-02-25 16:29:21 +0100 | [diff] [blame] | 1763 | free(str); |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1764 | } |
| 1765 | } |
| 1766 | |
| 1767 | return EXIT_SUCCESS; |
| 1768 | } |
| 1769 | |
Radek Krejci | 1411659 | 2018-05-16 12:26:06 +0200 | [diff] [blame] | 1770 | API uint16_t |
| 1771 | ly_ctx_get_module_set_id(const struct ly_ctx *ctx) |
| 1772 | { |
| 1773 | return ctx->models.module_set_id; |
| 1774 | } |
| 1775 | |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1776 | API struct lyd_node * |
| 1777 | ly_ctx_info(struct ly_ctx *ctx) |
| 1778 | { |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 1779 | int i, bis = 0; |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1780 | char id[8]; |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 1781 | char *str; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 1782 | const struct lys_module *mod; |
Michal Vasko | eb3bd0e | 2018-01-26 11:52:11 +0100 | [diff] [blame] | 1783 | struct lyd_node *root, *root_bis = NULL, *cont = NULL, *set_bis = NULL; |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1784 | |
Michal Vasko | 7eccc6c | 2016-03-24 14:56:33 +0100 | [diff] [blame] | 1785 | if (!ctx) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1786 | LOGARG; |
Michal Vasko | 7eccc6c | 2016-03-24 14:56:33 +0100 | [diff] [blame] | 1787 | return NULL; |
| 1788 | } |
| 1789 | |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 1790 | mod = ly_ctx_get_module(ctx, "ietf-yang-library", NULL, 1); |
Radek Krejci | bd9e8d2 | 2016-02-03 14:11:48 +0100 | [diff] [blame] | 1791 | if (!mod || !mod->data) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1792 | LOGERR(ctx, LY_EINVAL, "ietf-yang-library is not implemented."); |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 1793 | return NULL; |
| 1794 | } |
Michal Vasko | eb3bd0e | 2018-01-26 11:52:11 +0100 | [diff] [blame] | 1795 | if (mod->rev && !strcmp(mod->rev[0].date, "2016-04-09")) { |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 1796 | bis = 0; |
| 1797 | } else if (mod->rev && !strcmp(mod->rev[0].date, IETF_YANG_LIB_REV)) { |
| 1798 | bis = 1; |
| 1799 | } else { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1800 | LOGERR(ctx, LY_EINVAL, "Incompatible ietf-yang-library version in context."); |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1801 | return NULL; |
| 1802 | } |
| 1803 | |
Radek Krejci | bd9e8d2 | 2016-02-03 14:11:48 +0100 | [diff] [blame] | 1804 | root = lyd_new(NULL, mod, "modules-state"); |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1805 | if (!root) { |
| 1806 | return NULL; |
| 1807 | } |
| 1808 | |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 1809 | if (bis) { |
Michal Vasko | eb3bd0e | 2018-01-26 11:52:11 +0100 | [diff] [blame] | 1810 | if (!(root_bis = lyd_new(NULL, mod, "yang-library")) || !(set_bis = lyd_new(root_bis, NULL, "module-set"))) { |
| 1811 | goto error; |
| 1812 | } |
| 1813 | |
| 1814 | if (!lyd_new_leaf(set_bis, NULL, "name", "complete")) { |
| 1815 | goto error; |
| 1816 | } |
| 1817 | |
| 1818 | sprintf(id, "%u", ctx->models.module_set_id); |
| 1819 | if (!lyd_new_leaf(set_bis, NULL, "checksum", id)) { |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 1820 | goto error; |
| 1821 | } |
| 1822 | } |
| 1823 | |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1824 | for (i = 0; i < ctx->models.used; ++i) { |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 1825 | if (ctx->models.list[i]->disabled) { |
| 1826 | /* skip the disabled modules */ |
| 1827 | continue; |
| 1828 | } |
| 1829 | |
Michal Vasko | eb3bd0e | 2018-01-26 11:52:11 +0100 | [diff] [blame] | 1830 | /* |
| 1831 | * deprecated legacy |
| 1832 | */ |
| 1833 | cont = lyd_new(root, NULL, "module"); |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1834 | if (!cont) { |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 1835 | goto error; |
| 1836 | } |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 1837 | /* name */ |
Michal Vasko | eb3bd0e | 2018-01-26 11:52:11 +0100 | [diff] [blame] | 1838 | if (!lyd_new_leaf(cont, NULL, "name", ctx->models.list[i]->name)) { |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 1839 | goto error; |
| 1840 | } |
| 1841 | /* revision */ |
Michal Vasko | eb3bd0e | 2018-01-26 11:52:11 +0100 | [diff] [blame] | 1842 | if (!lyd_new_leaf(cont, NULL, "revision", (ctx->models.list[i]->rev_size ? ctx->models.list[i]->rev[0].date : ""))) { |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 1843 | goto error; |
| 1844 | } |
| 1845 | /* schema */ |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 1846 | if (ctx->models.list[i]->filepath) { |
Radek Krejci | 15412ca | 2016-03-03 11:16:52 +0100 | [diff] [blame] | 1847 | if (asprintf(&str, "file://%s", ctx->models.list[i]->filepath) == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1848 | LOGMEM(ctx); |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 1849 | goto error; |
| 1850 | } |
Michal Vasko | eb3bd0e | 2018-01-26 11:52:11 +0100 | [diff] [blame] | 1851 | if (!lyd_new_leaf(cont, NULL, "schema", str)) { |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 1852 | free(str); |
| 1853 | goto error; |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 1854 | } |
Radek Krejci | 19f9ede | 2016-02-25 16:29:21 +0100 | [diff] [blame] | 1855 | free(str); |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1856 | } |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 1857 | /* namespace */ |
Michal Vasko | eb3bd0e | 2018-01-26 11:52:11 +0100 | [diff] [blame] | 1858 | if (!lyd_new_leaf(cont, NULL, "namespace", ctx->models.list[i]->ns)) { |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 1859 | goto error; |
| 1860 | } |
| 1861 | /* feature leaf-list */ |
Michal Vasko | eb3bd0e | 2018-01-26 11:52:11 +0100 | [diff] [blame] | 1862 | if (ylib_feature(cont, ctx->models.list[i], 0)) { |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 1863 | goto error; |
| 1864 | } |
| 1865 | /* deviation list */ |
Michal Vasko | eb3bd0e | 2018-01-26 11:52:11 +0100 | [diff] [blame] | 1866 | if (ylib_deviation(cont, ctx->models.list[i], 0)) { |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 1867 | goto error; |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1868 | } |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 1869 | /* conformance-type */ |
Michal Vasko | eb3bd0e | 2018-01-26 11:52:11 +0100 | [diff] [blame] | 1870 | if (!lyd_new_leaf(cont, NULL, "conformance-type", ctx->models.list[i]->implemented ? "implement" : "import")) { |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 1871 | goto error; |
| 1872 | } |
| 1873 | /* submodule list */ |
Michal Vasko | eb3bd0e | 2018-01-26 11:52:11 +0100 | [diff] [blame] | 1874 | if (ylib_submodules(cont, ctx->models.list[i], 0)) { |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 1875 | goto error; |
| 1876 | } |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 1877 | |
Michal Vasko | eb3bd0e | 2018-01-26 11:52:11 +0100 | [diff] [blame] | 1878 | /* |
| 1879 | * current revision |
| 1880 | */ |
| 1881 | if (bis) { |
| 1882 | if (ctx->models.list[i]->implemented) { |
| 1883 | if (!(cont = lyd_new(set_bis, NULL, "module"))) { |
| 1884 | goto error; |
| 1885 | } |
| 1886 | } else { |
| 1887 | if (!(cont = lyd_new(set_bis, NULL, "import-only-module"))) { |
| 1888 | goto error; |
| 1889 | } |
| 1890 | } |
| 1891 | /* name */ |
| 1892 | if (!lyd_new_leaf(cont, NULL, "name", ctx->models.list[i]->name)) { |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 1893 | goto error; |
| 1894 | } |
Michal Vasko | eb3bd0e | 2018-01-26 11:52:11 +0100 | [diff] [blame] | 1895 | /* revision */ |
| 1896 | if ((!ctx->models.list[i]->implemented || ctx->models.list[i]->rev_size) |
| 1897 | && !lyd_new_leaf(cont, NULL, "revision", ctx->models.list[i]->rev[0].date)) { |
| 1898 | goto error; |
| 1899 | } |
| 1900 | /* namespace */ |
| 1901 | if (!lyd_new_leaf(cont, NULL, "namespace", ctx->models.list[i]->ns)) { |
| 1902 | goto error; |
| 1903 | } |
| 1904 | /* location */ |
| 1905 | if (ctx->models.list[i]->filepath) { |
| 1906 | if (asprintf(&str, "file://%s", ctx->models.list[i]->filepath) == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1907 | LOGMEM(ctx); |
Michal Vasko | eb3bd0e | 2018-01-26 11:52:11 +0100 | [diff] [blame] | 1908 | goto error; |
| 1909 | } |
| 1910 | if (!lyd_new_leaf(cont, NULL, "location", str)) { |
| 1911 | free(str); |
| 1912 | goto error; |
| 1913 | } |
| 1914 | free(str); |
| 1915 | } |
| 1916 | /* submodule list */ |
| 1917 | if (ylib_submodules(cont, ctx->models.list[i], 1)) { |
| 1918 | goto error; |
| 1919 | } |
| 1920 | if (ctx->models.list[i]->implemented) { |
| 1921 | /* feature list */ |
| 1922 | if (ylib_feature(cont, ctx->models.list[i], 1)) { |
| 1923 | goto error; |
| 1924 | } |
| 1925 | /* deviation */ |
| 1926 | if (ylib_deviation(cont, ctx->models.list[i], 1)) { |
| 1927 | goto error; |
| 1928 | } |
| 1929 | } |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1930 | } |
| 1931 | } |
| 1932 | |
| 1933 | sprintf(id, "%u", ctx->models.module_set_id); |
Michal Vasko | eb3bd0e | 2018-01-26 11:52:11 +0100 | [diff] [blame] | 1934 | if (!lyd_new_leaf(root, NULL, "module-set-id", id)) { |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 1935 | goto error; |
| 1936 | } |
Michal Vasko | eb3bd0e | 2018-01-26 11:52:11 +0100 | [diff] [blame] | 1937 | if (bis && !lyd_new_leaf(root_bis, NULL, "checksum", id)) { |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 1938 | goto error; |
| 1939 | } |
| 1940 | |
| 1941 | if (root_bis) { |
| 1942 | if (lyd_insert_sibling(&root_bis, root)) { |
| 1943 | goto error; |
| 1944 | } |
| 1945 | root = root_bis; |
| 1946 | root_bis = 0; |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1947 | } |
| 1948 | |
Michal Vasko | cdb9017 | 2016-09-13 09:34:36 +0200 | [diff] [blame] | 1949 | if (lyd_validate(&root, LYD_OPT_NOSIBLINGS, NULL)) { |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 1950 | goto error; |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1951 | } |
| 1952 | |
| 1953 | return root; |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 1954 | |
| 1955 | error: |
| 1956 | lyd_free_withsiblings(root); |
| 1957 | lyd_free_withsiblings(root_bis); |
| 1958 | return NULL; |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1959 | } |
Michal Vasko | b374440 | 2017-08-03 14:23:58 +0200 | [diff] [blame] | 1960 | |
| 1961 | API const struct lys_node * |
| 1962 | ly_ctx_get_node(struct ly_ctx *ctx, const struct lys_node *start, const char *nodeid, int output) |
| 1963 | { |
| 1964 | const struct lys_node *node; |
| 1965 | |
Michal Vasko | b3a6e48 | 2017-09-14 13:50:28 +0200 | [diff] [blame] | 1966 | if ((!ctx && !start) || !nodeid || ((nodeid[0] != '/') && !start)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1967 | LOGARG; |
Michal Vasko | b374440 | 2017-08-03 14:23:58 +0200 | [diff] [blame] | 1968 | return NULL; |
| 1969 | } |
| 1970 | |
Michal Vasko | b3a6e48 | 2017-09-14 13:50:28 +0200 | [diff] [blame] | 1971 | if (!ctx) { |
| 1972 | ctx = start->module->ctx; |
| 1973 | } |
| 1974 | |
Michal Vasko | b374440 | 2017-08-03 14:23:58 +0200 | [diff] [blame] | 1975 | /* sets error and everything */ |
| 1976 | node = resolve_json_nodeid(nodeid, ctx, start, output); |
| 1977 | |
| 1978 | return node; |
| 1979 | } |
Radek Krejci | 749ebf5 | 2018-01-22 11:40:36 +0100 | [diff] [blame] | 1980 | |
Radek Krejci | c683acd | 2018-01-22 14:51:52 +0100 | [diff] [blame] | 1981 | API struct ly_set * |
| 1982 | ly_ctx_find_path(struct ly_ctx *ctx, const char *path) |
Radek Krejci | 749ebf5 | 2018-01-22 11:40:36 +0100 | [diff] [blame] | 1983 | { |
| 1984 | struct ly_set *resultset = NULL; |
Radek Krejci | 749ebf5 | 2018-01-22 11:40:36 +0100 | [diff] [blame] | 1985 | |
Radek Krejci | c683acd | 2018-01-22 14:51:52 +0100 | [diff] [blame] | 1986 | if (!ctx || !path) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1987 | LOGARG; |
Radek Krejci | 749ebf5 | 2018-01-22 11:40:36 +0100 | [diff] [blame] | 1988 | return NULL; |
| 1989 | } |
| 1990 | |
| 1991 | /* start in internal module without data to make sure that all the nodes are prefixed */ |
Radek Krejci | c683acd | 2018-01-22 14:51:52 +0100 | [diff] [blame] | 1992 | resolve_schema_nodeid(path, NULL, ctx->models.list[0], &resultset, 1, 1); |
| 1993 | return resultset; |
Radek Krejci | 749ebf5 | 2018-01-22 11:40:36 +0100 | [diff] [blame] | 1994 | } |