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