Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file context.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief Context implementations |
| 5 | * |
| 6 | * Copyright (c) 2015 - 2018 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 15 | #include "common.h" |
| 16 | |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 17 | #include <errno.h> |
Radek Krejci | 7ada9a0 | 2018-09-07 15:34:05 +0200 | [diff] [blame] | 18 | #include <limits.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
Radek Krejci | 0e21240 | 2018-09-20 11:29:38 +0200 | [diff] [blame] | 21 | #include <sys/stat.h> |
| 22 | #include <sys/types.h> |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 23 | #include <unistd.h> |
| 24 | |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 25 | #include "context.h" |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 26 | #include "tree_schema_internal.h" |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 27 | #include "libyang.h" |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 28 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 29 | #define LY_INTERNAL_MODS_COUNT 6 |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 30 | |
Radek Krejci | f3f4784 | 2018-11-15 11:22:15 +0100 | [diff] [blame] | 31 | #include "../models/ietf-yang-metadata@2016-08-05.h" |
| 32 | #include "../models/yang@2017-02-20.h" |
| 33 | #include "../models/ietf-inet-types@2013-07-15.h" |
| 34 | #include "../models/ietf-yang-types@2013-07-15.h" |
| 35 | #include "../models/ietf-datastores@2017-08-17.h" |
| 36 | #include "../models/ietf-yang-library@2018-01-17.h" |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 37 | #define IETF_YANG_LIB_REV "2018-01-17" |
| 38 | |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 39 | static struct internal_modules_s { |
| 40 | const char *name; |
| 41 | const char *revision; |
| 42 | const char *data; |
| 43 | uint8_t implemented; |
| 44 | LYS_INFORMAT format; |
| 45 | } internal_modules[LY_INTERNAL_MODS_COUNT] = { |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 46 | {"ietf-yang-metadata", "2016-08-05", (const char*)ietf_yang_metadata_2016_08_05_yang, 0, LYS_IN_YANG}, |
| 47 | {"yang", "2017-02-20", (const char*)yang_2017_02_20_yang, 1, LYS_IN_YANG}, |
| 48 | {"ietf-inet-types", "2013-07-15", (const char*)ietf_inet_types_2013_07_15_yang, 0, LYS_IN_YANG}, |
| 49 | {"ietf-yang-types", "2013-07-15", (const char*)ietf_yang_types_2013_07_15_yang, 0, LYS_IN_YANG}, |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 50 | /* ietf-datastores and ietf-yang-library must be right here at the end of the list! */ |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 51 | {"ietf-datastores", "2017-08-17", (const char*)ietf_datastores_2017_08_17_yang, 0, LYS_IN_YANG}, |
| 52 | {"ietf-yang-library", IETF_YANG_LIB_REV, (const char*)ietf_yang_library_2018_01_17_yang, 1, LYS_IN_YANG} |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | API LY_ERR |
| 56 | ly_ctx_set_searchdir(struct ly_ctx *ctx, const char *search_dir) |
| 57 | { |
Radek Krejci | 0e21240 | 2018-09-20 11:29:38 +0200 | [diff] [blame] | 58 | struct stat st; |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 59 | char *new_dir = NULL; |
Radek Krejci | 0e21240 | 2018-09-20 11:29:38 +0200 | [diff] [blame] | 60 | unsigned int u; |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 61 | |
| 62 | LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL); |
| 63 | |
| 64 | if (search_dir) { |
Radek Krejci | 0e21240 | 2018-09-20 11:29:38 +0200 | [diff] [blame] | 65 | LY_CHECK_ERR_RET(access(search_dir, R_OK | X_OK), |
| 66 | LOGERR(ctx, LY_ESYS, "Unable to use search directory \"%s\" (%s)", search_dir, strerror(errno)), |
| 67 | LY_EINVAL); |
| 68 | LY_CHECK_ERR_RET(stat(search_dir, &st), |
| 69 | LOGERR(ctx, LY_ESYS, "stat() failed for \"%s\" (%s)", search_dir, strerror(errno)), |
| 70 | LY_ESYS); |
| 71 | LY_CHECK_ERR_RET(!S_ISDIR(st.st_mode), |
| 72 | LOGERR(ctx, LY_ESYS, "Given search directory \"%s\" is not a directory.", search_dir), |
| 73 | LY_EINVAL); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 74 | new_dir = realpath(search_dir, NULL); |
Radek Krejci | a77e0e1 | 2018-09-20 12:39:15 +0200 | [diff] [blame] | 75 | LY_CHECK_ERR_RET(!new_dir, |
| 76 | LOGERR(ctx, LY_ESYS, "realpath() call failed for \"%s\" (%s).", search_dir, strerror(errno)), |
| 77 | LY_ESYS); |
Radek Krejci | 0e21240 | 2018-09-20 11:29:38 +0200 | [diff] [blame] | 78 | /* avoid path duplication */ |
| 79 | for (u = 0; u < ctx->search_paths.count; ++u) { |
Radek Krejci | 14946ab | 2018-09-20 13:42:06 +0200 | [diff] [blame] | 80 | if (!strcmp(new_dir, ctx->search_paths.objs[u])) { |
Radek Krejci | 0e21240 | 2018-09-20 11:29:38 +0200 | [diff] [blame] | 81 | free(new_dir); |
Radek Krejci | 14946ab | 2018-09-20 13:42:06 +0200 | [diff] [blame] | 82 | return LY_EEXIST; |
Radek Krejci | 0e21240 | 2018-09-20 11:29:38 +0200 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | if (ly_set_add(&ctx->search_paths, new_dir, LY_SET_OPT_USEASLIST) == -1) { |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 86 | free(new_dir); |
| 87 | return LY_EMEM; |
| 88 | } |
| 89 | |
Radek Krejci | e9e987e | 2018-10-31 12:50:27 +0100 | [diff] [blame] | 90 | /* new searchdir - possibly more latest revision available */ |
| 91 | ly_ctx_reset_latests(ctx); |
| 92 | |
Radek Krejci | 0e21240 | 2018-09-20 11:29:38 +0200 | [diff] [blame] | 93 | return LY_SUCCESS; |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 94 | } else { |
| 95 | /* consider that no change is not actually an error */ |
| 96 | return LY_SUCCESS; |
| 97 | } |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | API const char * const * |
| 101 | ly_ctx_get_searchdirs(const struct ly_ctx *ctx) |
| 102 | { |
Radek Krejci | 0502643 | 2018-09-20 12:13:43 +0200 | [diff] [blame] | 103 | void **new; |
| 104 | |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 105 | LY_CHECK_ARG_RET(ctx, ctx, NULL); |
Radek Krejci | 0502643 | 2018-09-20 12:13:43 +0200 | [diff] [blame] | 106 | |
| 107 | if (ctx->search_paths.count == ctx->search_paths.size) { |
| 108 | /* not enough space for terminating NULL byte */ |
| 109 | new = realloc(((struct ly_ctx *)ctx)->search_paths.objs, (ctx->search_paths.size + 8) * sizeof *ctx->search_paths.objs); |
| 110 | LY_CHECK_ERR_RET(!new, LOGMEM(NULL), NULL); |
| 111 | ((struct ly_ctx *)ctx)->search_paths.size += 8; |
| 112 | ((struct ly_ctx *)ctx)->search_paths.objs = new; |
| 113 | } |
| 114 | /* set terminating NULL byte to the strings list */ |
| 115 | ctx->search_paths.objs[ctx->search_paths.count] = NULL; |
| 116 | |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 117 | return (const char * const *)ctx->search_paths.objs; |
| 118 | } |
| 119 | |
| 120 | API LY_ERR |
Radek Krejci | 0759b79 | 2018-09-20 13:53:15 +0200 | [diff] [blame] | 121 | ly_ctx_unset_searchdirs(struct ly_ctx *ctx, const char *value) |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 122 | { |
Radek Krejci | 0759b79 | 2018-09-20 13:53:15 +0200 | [diff] [blame] | 123 | unsigned int index; |
| 124 | |
Radek Krejci | cd64907 | 2018-09-20 12:14:45 +0200 | [diff] [blame] | 125 | LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL); |
Radek Krejci | cd64907 | 2018-09-20 12:14:45 +0200 | [diff] [blame] | 126 | |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 127 | if (!ctx->search_paths.count) { |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 128 | return LY_SUCCESS; |
| 129 | } |
| 130 | |
Radek Krejci | 0759b79 | 2018-09-20 13:53:15 +0200 | [diff] [blame] | 131 | if (value) { |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 132 | /* remove specific search directory */ |
Radek Krejci | 0759b79 | 2018-09-20 13:53:15 +0200 | [diff] [blame] | 133 | for (index = 0; index < ctx->search_paths.count; ++index) { |
| 134 | if (!strcmp(value, ctx->search_paths.objs[index])) { |
| 135 | break; |
| 136 | } |
| 137 | } |
| 138 | if (index == ctx->search_paths.count) { |
| 139 | LOGARG(ctx, value); |
| 140 | return LY_EINVAL; |
| 141 | } else { |
| 142 | return ly_set_rm_index(&ctx->search_paths, index, free); |
| 143 | } |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 144 | } else { |
| 145 | /* remove them all */ |
Radek Krejci | a40f21b | 2018-09-18 10:42:08 +0200 | [diff] [blame] | 146 | ly_set_erase(&ctx->search_paths, free); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 147 | memset(&ctx->search_paths, 0, sizeof ctx->search_paths); |
| 148 | } |
| 149 | |
| 150 | return LY_SUCCESS; |
| 151 | } |
| 152 | |
| 153 | API LY_ERR |
| 154 | ly_ctx_new(const char *search_dir, int options, struct ly_ctx **new_ctx) |
| 155 | { |
| 156 | struct ly_ctx *ctx = NULL; |
| 157 | struct lys_module *module; |
| 158 | char *search_dir_list; |
| 159 | char *sep, *dir; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 160 | int i; |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 161 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 162 | |
| 163 | ctx = calloc(1, sizeof *ctx); |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 164 | LY_CHECK_ERR_RET(!ctx, LOGMEM(NULL), LY_EMEM); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 165 | |
| 166 | /* dictionary */ |
| 167 | lydict_init(&ctx->dict); |
| 168 | |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 169 | #if 0 /* TODO when plugins implemented */ |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 170 | /* plugins */ |
| 171 | ly_load_plugins(); |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 172 | #endif |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 173 | |
| 174 | /* initialize thread-specific key */ |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 175 | while ((pthread_key_create(&ctx->errlist_key, ly_err_free)) == EAGAIN); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 176 | |
| 177 | /* models list */ |
| 178 | ctx->flags = options; |
| 179 | if (search_dir) { |
| 180 | search_dir_list = strdup(search_dir); |
| 181 | LY_CHECK_ERR_GOTO(!search_dir_list, LOGMEM(NULL); rc = LY_EMEM, error); |
| 182 | |
| 183 | for (dir = search_dir_list; (sep = strchr(dir, ':')) != NULL && rc == LY_SUCCESS; dir = sep + 1) { |
| 184 | *sep = 0; |
| 185 | rc = ly_ctx_set_searchdir(ctx, dir); |
Radek Krejci | 14946ab | 2018-09-20 13:42:06 +0200 | [diff] [blame] | 186 | if (rc == LY_EEXIST) { |
| 187 | /* ignore duplication */ |
| 188 | rc = LY_SUCCESS; |
| 189 | } |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 190 | } |
| 191 | if (*dir && rc == LY_SUCCESS) { |
| 192 | rc = ly_ctx_set_searchdir(ctx, dir); |
Radek Krejci | 14946ab | 2018-09-20 13:42:06 +0200 | [diff] [blame] | 193 | if (rc == LY_EEXIST) { |
| 194 | /* ignore duplication */ |
| 195 | rc = LY_SUCCESS; |
| 196 | } |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 197 | } |
| 198 | free(search_dir_list); |
| 199 | |
| 200 | /* If ly_ctx_set_searchdir() failed, the error is already logged. Just exit */ |
| 201 | if (rc != LY_SUCCESS) { |
| 202 | goto error; |
| 203 | } |
| 204 | } |
| 205 | ctx->module_set_id = 1; |
| 206 | |
| 207 | /* load internal modules */ |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 208 | for (i = 0; i < ((options & LY_CTX_NOYANGLIBRARY) ? (LY_INTERNAL_MODS_COUNT - 2) : LY_INTERNAL_MODS_COUNT); i++) { |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 209 | module = (struct lys_module *)lys_parse_mem(ctx, internal_modules[i].data, internal_modules[i].format); |
Radek Krejci | 0e75e6f | 2018-11-08 09:40:02 +0100 | [diff] [blame] | 210 | LY_CHECK_ERR_GOTO(!module, rc = ly_errcode(ctx), error); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 211 | module->parsed->implemented = internal_modules[i].implemented; |
| 212 | } |
| 213 | |
| 214 | *new_ctx = ctx; |
| 215 | return rc; |
| 216 | |
| 217 | error: |
| 218 | ly_ctx_destroy(ctx, NULL); |
| 219 | return rc; |
| 220 | } |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 221 | API int |
Radek Krejci | 3fbe89a | 2018-09-20 13:54:46 +0200 | [diff] [blame] | 222 | ly_ctx_get_options(const struct ly_ctx *ctx) |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 223 | { |
Radek Krejci | 3fbe89a | 2018-09-20 13:54:46 +0200 | [diff] [blame] | 224 | LY_CHECK_ARG_RET(ctx, ctx, 0); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 225 | return ctx->flags; |
| 226 | } |
| 227 | |
Radek Krejci | ca828d9 | 2018-09-20 14:19:43 +0200 | [diff] [blame] | 228 | API LY_ERR |
| 229 | ly_ctx_set_option(struct ly_ctx *ctx, int option) |
| 230 | { |
| 231 | LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL); |
| 232 | LY_CHECK_ERR_RET(option & LY_CTX_NOYANGLIBRARY, LOGARG(ctx, option), LY_EINVAL); |
| 233 | |
| 234 | /* set the option(s) */ |
| 235 | ctx->flags |= option; |
| 236 | |
| 237 | return LY_SUCCESS; |
| 238 | } |
| 239 | |
| 240 | API LY_ERR |
| 241 | ly_ctx_unset_option(struct ly_ctx *ctx, int option) |
| 242 | { |
| 243 | LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL); |
| 244 | LY_CHECK_ERR_RET(option & LY_CTX_NOYANGLIBRARY, LOGARG(ctx, option), LY_EINVAL); |
| 245 | |
| 246 | /* unset the option(s) */ |
| 247 | ctx->flags &= ~option; |
| 248 | |
| 249 | return LY_SUCCESS; |
| 250 | } |
| 251 | |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 252 | API uint16_t |
| 253 | ly_ctx_get_module_set_id(const struct ly_ctx *ctx) |
| 254 | { |
Radek Krejci | 3fbe89a | 2018-09-20 13:54:46 +0200 | [diff] [blame] | 255 | LY_CHECK_ARG_RET(ctx, ctx, 0); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 256 | return ctx->module_set_id; |
| 257 | } |
| 258 | |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 259 | API void |
| 260 | ly_ctx_set_module_imp_clb(struct ly_ctx *ctx, ly_module_imp_clb clb, void *user_data) |
| 261 | { |
| 262 | LY_CHECK_ARG_RET(ctx, ctx,); |
| 263 | |
| 264 | ctx->imp_clb = clb; |
| 265 | ctx->imp_clb_data = user_data; |
| 266 | } |
| 267 | |
| 268 | API ly_module_imp_clb |
| 269 | ly_ctx_get_module_imp_clb(const struct ly_ctx *ctx, void **user_data) |
| 270 | { |
| 271 | LY_CHECK_ARG_RET(ctx, ctx, NULL); |
| 272 | |
| 273 | if (user_data) { |
| 274 | *user_data = ctx->imp_clb_data; |
| 275 | } |
| 276 | return ctx->imp_clb; |
| 277 | } |
| 278 | |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 279 | /** |
| 280 | * @brief Iterate over the modules in the given context. Returned modules must match the given key at the offset of |
| 281 | * lysp_module and lysc_module structures (they are supposed to be placed at the same offset in both structures). |
| 282 | * |
| 283 | * @param[in] ctx Context where to iterate. |
| 284 | * @param[in] key Key value to search for. |
| 285 | * @param[in] key_offset Key's offset in struct lysp_module and struct lysc_module to get value from the context's |
| 286 | * modules to match with the key. |
| 287 | * @param[in,out] Iterator to pass between the function calls. On the first call, the variable is supposed to be |
| 288 | * initiated to 0. After each call returning a module, the value is greater by 1 than the index of the returned |
| 289 | * module in the context. |
| 290 | * @return Module matching the given key, NULL if no such module found. |
| 291 | */ |
| 292 | static const struct lys_module * |
| 293 | ly_ctx_get_module_by_iter(const struct ly_ctx *ctx, const char *key, size_t key_offset, unsigned int *index) |
| 294 | { |
| 295 | const struct lys_module *mod; |
| 296 | const char *value; |
| 297 | |
| 298 | for (; *index < ctx->list.count; ++(*index)) { |
| 299 | mod = ctx->list.objs[*index]; |
| 300 | if (mod->compiled) { |
| 301 | value = *(const char**)(((int8_t*)(mod->compiled)) + key_offset); |
| 302 | } else { |
| 303 | value = *(const char**)(((int8_t*)(mod->parsed)) + key_offset); |
| 304 | } |
| 305 | if (!strcmp(key, value)) { |
| 306 | /* increment index for the next run */ |
| 307 | ++(*index); |
| 308 | return mod; |
| 309 | } |
| 310 | } |
| 311 | /* done */ |
| 312 | return NULL; |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * @brief Unifying function for ly_ctx_get_module() and ly_ctx_get_module_ns() |
| 317 | * @param[in] ctx Context where to search. |
| 318 | * @param[in] key Name or Namespace as a search key. |
| 319 | * @param[in] key_offset Key's offset in struct lysp_module to get value from the context's modules to match with the key. |
| 320 | * @param[in] revision Revision date to match. If NULL, the matching module must have no revision. To search for the latest |
| 321 | * revision module, use ly_ctx_get_module_latest_by(). |
| 322 | * @return Matching module if any. |
| 323 | */ |
| 324 | static const struct lys_module * |
| 325 | ly_ctx_get_module_by(const struct ly_ctx *ctx, const char *key, size_t key_offset, const char *revision) |
| 326 | { |
| 327 | const struct lys_module *mod; |
| 328 | unsigned int index = 0; |
| 329 | |
| 330 | while ((mod = ly_ctx_get_module_by_iter(ctx, key, key_offset, &index))) { |
| 331 | if (!revision) { |
Radek Krejci | f8f882a | 2018-10-31 14:51:15 +0100 | [diff] [blame] | 332 | if ((mod->compiled && !mod->compiled->revision) || (!mod->compiled && !mod->parsed->revs)) { |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 333 | /* found requested module without revision */ |
| 334 | return mod; |
| 335 | } |
| 336 | } else { |
Radek Krejci | f8f882a | 2018-10-31 14:51:15 +0100 | [diff] [blame] | 337 | if ((mod->compiled && mod->compiled->revision && !strcmp(mod->compiled->revision, revision)) || |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 338 | (!mod->compiled && mod->parsed->revs && !strcmp(mod->parsed->revs[0].date, revision))) { |
| 339 | /* found requested module of the specific revision */ |
| 340 | return mod; |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | return NULL; |
| 346 | } |
| 347 | |
| 348 | API const struct lys_module * |
| 349 | ly_ctx_get_module_ns(const struct ly_ctx *ctx, const char *ns, const char *revision) |
| 350 | { |
| 351 | LY_CHECK_ARG_RET(ctx, ctx, ns, NULL); |
| 352 | return ly_ctx_get_module_by(ctx, ns, offsetof(struct lysp_module, ns), revision); |
| 353 | } |
| 354 | |
| 355 | API const struct lys_module * |
| 356 | ly_ctx_get_module(const struct ly_ctx *ctx, const char *name, const char *revision) |
| 357 | { |
| 358 | LY_CHECK_ARG_RET(ctx, ctx, name, NULL); |
| 359 | return ly_ctx_get_module_by(ctx, name, offsetof(struct lysp_module, name), revision); |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * @brief Unifying function for ly_ctx_get_module_latest() and ly_ctx_get_module_latest_ns() |
| 364 | * @param[in] ctx Context where to search. |
| 365 | * @param[in] key Name or Namespace as a search key. |
| 366 | * @param[in] key_offset Key's offset in struct lysp_module to get value from the context's modules to match with the key. |
| 367 | * @return Matching module if any. |
| 368 | */ |
| 369 | static const struct lys_module * |
| 370 | ly_ctx_get_module_latest_by(const struct ly_ctx *ctx, const char *key, size_t key_offset) |
| 371 | { |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 372 | const struct lys_module *mod; |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 373 | unsigned int index = 0; |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 374 | |
| 375 | while ((mod = ly_ctx_get_module_by_iter(ctx, key, key_offset, &index))) { |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 376 | if ((mod->compiled && mod->compiled->latest_revision) || (!mod->compiled && mod->parsed->latest_revision)) { |
| 377 | return mod; |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 381 | return NULL; |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | API const struct lys_module * |
| 385 | ly_ctx_get_module_latest(const struct ly_ctx *ctx, const char *name) |
| 386 | { |
| 387 | LY_CHECK_ARG_RET(ctx, ctx, name, NULL); |
| 388 | return ly_ctx_get_module_latest_by(ctx, name, offsetof(struct lysp_module, name)); |
| 389 | } |
| 390 | |
| 391 | const struct lys_module * |
| 392 | ly_ctx_get_module_latest_ns(const struct ly_ctx *ctx, const char *ns) |
| 393 | { |
| 394 | LY_CHECK_ARG_RET(ctx, ctx, ns, NULL); |
| 395 | return ly_ctx_get_module_latest_by(ctx, ns, offsetof(struct lysp_module, ns)); |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * @brief Unifying function for ly_ctx_get_module_implemented() and ly_ctx_get_module_implemented_ns() |
| 400 | * @param[in] ctx Context where to search. |
| 401 | * @param[in] key Name or Namespace as a search key. |
| 402 | * @param[in] key_offset Key's offset in struct lysp_module to get value from the context's modules to match with the key. |
| 403 | * @return Matching module if any. |
| 404 | */ |
| 405 | static const struct lys_module * |
| 406 | ly_ctx_get_module_implemented_by(const struct ly_ctx *ctx, const char *key, size_t key_offset) |
| 407 | { |
| 408 | const struct lys_module *mod; |
| 409 | unsigned int index = 0; |
| 410 | |
| 411 | while ((mod = ly_ctx_get_module_by_iter(ctx, key, key_offset, &index))) { |
| 412 | if ((mod->compiled && mod->compiled->implemented) || (!mod->compiled && mod->parsed->implemented)) { |
| 413 | return mod; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | return NULL; |
| 418 | } |
| 419 | |
| 420 | API const struct lys_module * |
| 421 | ly_ctx_get_module_implemented(const struct ly_ctx *ctx, const char *name) |
| 422 | { |
| 423 | LY_CHECK_ARG_RET(ctx, ctx, name, NULL); |
| 424 | return ly_ctx_get_module_implemented_by(ctx, name, offsetof(struct lysp_module, name)); |
| 425 | } |
| 426 | |
| 427 | API const struct lys_module * |
| 428 | ly_ctx_get_module_implemented_ns(const struct ly_ctx *ctx, const char *ns) |
| 429 | { |
| 430 | LY_CHECK_ARG_RET(ctx, ctx, ns, NULL); |
| 431 | return ly_ctx_get_module_implemented_by(ctx, ns, offsetof(struct lysp_module, ns)); |
| 432 | } |
| 433 | |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 434 | struct lysp_module * |
| 435 | ly_ctx_get_submodule(const struct ly_ctx *ctx, const char *module, const char *submodule, const char *revision) |
| 436 | { |
| 437 | const struct lys_module *mod; |
| 438 | struct lysp_include *inc; |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 439 | unsigned int v, u; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 440 | |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 441 | assert(submodule); |
| 442 | |
| 443 | for (v = 0; v < ctx->list.count; ++v) { |
| 444 | mod = ctx->list.objs[v]; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 445 | if (!mod->parsed) { |
| 446 | continue; |
| 447 | } |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 448 | if (module && strcmp(module, mod->parsed->name)) { |
| 449 | continue; |
| 450 | } |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 451 | |
| 452 | LY_ARRAY_FOR(mod->parsed->includes, u) { |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 453 | if (mod->parsed->includes[u].submodule && !strcmp(submodule, mod->parsed->includes[u].submodule->name)) { |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 454 | inc = &mod->parsed->includes[u]; |
| 455 | if (!revision) { |
| 456 | if (inc->submodule->latest_revision) { |
| 457 | return inc->submodule; |
| 458 | } |
| 459 | } else if (inc->submodule->revs && !strcmp(revision, inc->submodule->revs[0].date)) { |
| 460 | return inc->submodule; |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | return NULL; |
| 467 | } |
| 468 | |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 469 | API void |
Radek Krejci | e9e987e | 2018-10-31 12:50:27 +0100 | [diff] [blame] | 470 | ly_ctx_reset_latests(struct ly_ctx *ctx) |
| 471 | { |
| 472 | unsigned int u,v ; |
| 473 | struct lys_module *mod; |
| 474 | |
| 475 | for (u = 0; u < ctx->list.count; ++u) { |
| 476 | mod = ctx->list.objs[u]; |
| 477 | if (mod->compiled && mod->compiled->latest_revision == 2) { |
| 478 | mod->compiled->latest_revision = 1; |
| 479 | } |
| 480 | if (mod->parsed) { |
| 481 | if (mod->parsed->latest_revision == 2) { |
| 482 | mod->parsed->latest_revision = 1; |
| 483 | } |
| 484 | if (mod->parsed->includes) { |
| 485 | for (v = 0; v < LY_ARRAY_SIZE(mod->parsed->includes); ++v) { |
| 486 | if (mod->parsed->includes[v].submodule->latest_revision == 2) { |
| 487 | mod->parsed->includes[v].submodule->latest_revision = 1; |
| 488 | } |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | API void |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 496 | ly_ctx_destroy(struct ly_ctx *ctx, void (*private_destructor)(const struct lysc_node *node, void *priv)) |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 497 | { |
Radek Krejci | 418823a | 2018-09-20 16:42:13 +0200 | [diff] [blame] | 498 | if (!ctx) { |
| 499 | return; |
| 500 | } |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 501 | |
| 502 | /* models list */ |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 503 | for (; ctx->list.count; ctx->list.count--) { |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 504 | /* remove the module */ |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 505 | lys_module_free(ctx->list.objs[ctx->list.count - 1], private_destructor); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 506 | } |
| 507 | free(ctx->list.objs); |
| 508 | |
| 509 | /* search paths list */ |
Radek Krejci | a40f21b | 2018-09-18 10:42:08 +0200 | [diff] [blame] | 510 | ly_set_erase(&ctx->search_paths, free); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 511 | |
| 512 | /* clean the error list */ |
| 513 | ly_err_clean(ctx, 0); |
| 514 | pthread_key_delete(ctx->errlist_key); |
| 515 | |
| 516 | /* dictionary */ |
| 517 | lydict_clean(&ctx->dict); |
| 518 | |
| 519 | #if 0 /* TODO when plugins implemented */ |
| 520 | /* plugins - will be removed only if this is the last context */ |
| 521 | ly_clean_plugins(); |
| 522 | #endif |
| 523 | |
| 524 | free(ctx); |
| 525 | } |