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