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