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 | */ |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 14 | #define _GNU_SOURCE /* asprintf */ |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 15 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 16 | #include "context.h" |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 17 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 18 | #include <assert.h> |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 19 | #include <errno.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 20 | #include <pthread.h> |
| 21 | #include <stddef.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 22 | #include <stdio.h> |
Radek Krejci | 7ada9a0 | 2018-09-07 15:34:05 +0200 | [diff] [blame] | 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
Radek Krejci | 0e21240 | 2018-09-20 11:29:38 +0200 | [diff] [blame] | 25 | #include <sys/stat.h> |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 26 | #include <unistd.h> |
| 27 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 28 | #include "common.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 29 | #include "hash_table.h" |
| 30 | #include "set.h" |
| 31 | #include "tree.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 32 | #include "tree_data.h" |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 33 | #include "tree_schema_internal.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 34 | #include "plugins_types.h" |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 35 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 36 | #define LY_INTERNAL_MODS_COUNT 6 |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 37 | |
Radek Krejci | f3f4784 | 2018-11-15 11:22:15 +0100 | [diff] [blame] | 38 | #include "../models/ietf-yang-metadata@2016-08-05.h" |
| 39 | #include "../models/yang@2017-02-20.h" |
| 40 | #include "../models/ietf-inet-types@2013-07-15.h" |
| 41 | #include "../models/ietf-yang-types@2013-07-15.h" |
Michal Vasko | cf296c8 | 2020-05-27 11:16:45 +0200 | [diff] [blame] | 42 | #include "../models/ietf-datastores@2018-02-14.h" |
| 43 | #include "../models/ietf-yang-library@2019-01-04.h" |
| 44 | #define IETF_YANG_LIB_REV "2019-01-04" |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 45 | |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 46 | static struct internal_modules_s { |
| 47 | const char *name; |
| 48 | const char *revision; |
| 49 | const char *data; |
| 50 | uint8_t implemented; |
| 51 | LYS_INFORMAT format; |
| 52 | } internal_modules[LY_INTERNAL_MODS_COUNT] = { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 53 | {"ietf-yang-metadata", "2016-08-05", (const char*)ietf_yang_metadata_2016_08_05_yang, 1, LYS_IN_YANG}, |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 54 | {"yang", "2017-02-20", (const char*)yang_2017_02_20_yang, 1, LYS_IN_YANG}, |
| 55 | {"ietf-inet-types", "2013-07-15", (const char*)ietf_inet_types_2013_07_15_yang, 0, LYS_IN_YANG}, |
| 56 | {"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] | 57 | /* ietf-datastores and ietf-yang-library must be right here at the end of the list! */ |
Michal Vasko | cf296c8 | 2020-05-27 11:16:45 +0200 | [diff] [blame] | 58 | {"ietf-datastores", "2018-02-14", (const char*)ietf_datastores_2018_02_14_yang, 1, LYS_IN_YANG}, |
| 59 | {"ietf-yang-library", IETF_YANG_LIB_REV, (const char*)ietf_yang_library_2019_01_04_yang, 1, LYS_IN_YANG} |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | API LY_ERR |
| 63 | ly_ctx_set_searchdir(struct ly_ctx *ctx, const char *search_dir) |
| 64 | { |
Radek Krejci | 0e21240 | 2018-09-20 11:29:38 +0200 | [diff] [blame] | 65 | struct stat st; |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 66 | char *new_dir = NULL; |
Radek Krejci | 0e21240 | 2018-09-20 11:29:38 +0200 | [diff] [blame] | 67 | unsigned int u; |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 68 | |
| 69 | LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL); |
| 70 | |
| 71 | if (search_dir) { |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 72 | new_dir = realpath(search_dir, NULL); |
Radek Krejci | a77e0e1 | 2018-09-20 12:39:15 +0200 | [diff] [blame] | 73 | LY_CHECK_ERR_RET(!new_dir, |
Radek Krejci | 3a077b9 | 2019-04-09 17:10:35 +0200 | [diff] [blame] | 74 | LOGERR(ctx, LY_ESYS, "Unable to use search directory \"%s\" (%s).", search_dir, strerror(errno)), |
| 75 | LY_EINVAL); |
| 76 | if (strcmp(search_dir, new_dir)) { |
| 77 | LOGVRB("Canonicalizing search directory string from \"%s\" to \"%s\".", search_dir, new_dir); |
| 78 | } |
| 79 | LY_CHECK_ERR_RET(access(new_dir, R_OK | X_OK), |
| 80 | LOGERR(ctx, LY_ESYS, "Unable to fully access search directory \"%s\" (%s).", new_dir, strerror(errno)); free(new_dir), |
| 81 | LY_EINVAL); |
| 82 | LY_CHECK_ERR_RET(stat(new_dir, &st), |
| 83 | LOGERR(ctx, LY_ESYS, "stat() failed for \"%s\" (%s).", new_dir, strerror(errno)); free(new_dir), |
Radek Krejci | a77e0e1 | 2018-09-20 12:39:15 +0200 | [diff] [blame] | 84 | LY_ESYS); |
Radek Krejci | 3a077b9 | 2019-04-09 17:10:35 +0200 | [diff] [blame] | 85 | LY_CHECK_ERR_RET(!S_ISDIR(st.st_mode), |
| 86 | LOGERR(ctx, LY_ESYS, "Given search directory \"%s\" is not a directory.", new_dir); free(new_dir), |
| 87 | LY_EINVAL); |
Radek Krejci | 0e21240 | 2018-09-20 11:29:38 +0200 | [diff] [blame] | 88 | /* avoid path duplication */ |
| 89 | for (u = 0; u < ctx->search_paths.count; ++u) { |
Radek Krejci | 14946ab | 2018-09-20 13:42:06 +0200 | [diff] [blame] | 90 | if (!strcmp(new_dir, ctx->search_paths.objs[u])) { |
Radek Krejci | 0e21240 | 2018-09-20 11:29:38 +0200 | [diff] [blame] | 91 | free(new_dir); |
Radek Krejci | 14946ab | 2018-09-20 13:42:06 +0200 | [diff] [blame] | 92 | return LY_EEXIST; |
Radek Krejci | 0e21240 | 2018-09-20 11:29:38 +0200 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | 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] | 96 | free(new_dir); |
| 97 | return LY_EMEM; |
| 98 | } |
| 99 | |
Radek Krejci | e9e987e | 2018-10-31 12:50:27 +0100 | [diff] [blame] | 100 | /* new searchdir - possibly more latest revision available */ |
| 101 | ly_ctx_reset_latests(ctx); |
| 102 | |
Radek Krejci | 0e21240 | 2018-09-20 11:29:38 +0200 | [diff] [blame] | 103 | return LY_SUCCESS; |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 104 | } else { |
| 105 | /* consider that no change is not actually an error */ |
| 106 | return LY_SUCCESS; |
| 107 | } |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | API const char * const * |
| 111 | ly_ctx_get_searchdirs(const struct ly_ctx *ctx) |
| 112 | { |
Radek Krejci | 0502643 | 2018-09-20 12:13:43 +0200 | [diff] [blame] | 113 | void **new; |
| 114 | |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 115 | LY_CHECK_ARG_RET(ctx, ctx, NULL); |
Radek Krejci | 0502643 | 2018-09-20 12:13:43 +0200 | [diff] [blame] | 116 | |
| 117 | if (ctx->search_paths.count == ctx->search_paths.size) { |
| 118 | /* not enough space for terminating NULL byte */ |
| 119 | new = realloc(((struct ly_ctx *)ctx)->search_paths.objs, (ctx->search_paths.size + 8) * sizeof *ctx->search_paths.objs); |
| 120 | LY_CHECK_ERR_RET(!new, LOGMEM(NULL), NULL); |
| 121 | ((struct ly_ctx *)ctx)->search_paths.size += 8; |
| 122 | ((struct ly_ctx *)ctx)->search_paths.objs = new; |
| 123 | } |
| 124 | /* set terminating NULL byte to the strings list */ |
| 125 | ctx->search_paths.objs[ctx->search_paths.count] = NULL; |
| 126 | |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 127 | return (const char * const *)ctx->search_paths.objs; |
| 128 | } |
| 129 | |
| 130 | API LY_ERR |
Radek Krejci | 0759b79 | 2018-09-20 13:53:15 +0200 | [diff] [blame] | 131 | ly_ctx_unset_searchdirs(struct ly_ctx *ctx, const char *value) |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 132 | { |
Radek Krejci | 0759b79 | 2018-09-20 13:53:15 +0200 | [diff] [blame] | 133 | unsigned int index; |
| 134 | |
Radek Krejci | cd64907 | 2018-09-20 12:14:45 +0200 | [diff] [blame] | 135 | LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL); |
Radek Krejci | cd64907 | 2018-09-20 12:14:45 +0200 | [diff] [blame] | 136 | |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 137 | if (!ctx->search_paths.count) { |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 138 | return LY_SUCCESS; |
| 139 | } |
| 140 | |
Radek Krejci | 0759b79 | 2018-09-20 13:53:15 +0200 | [diff] [blame] | 141 | if (value) { |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 142 | /* remove specific search directory */ |
Radek Krejci | 0759b79 | 2018-09-20 13:53:15 +0200 | [diff] [blame] | 143 | for (index = 0; index < ctx->search_paths.count; ++index) { |
| 144 | if (!strcmp(value, ctx->search_paths.objs[index])) { |
| 145 | break; |
| 146 | } |
| 147 | } |
| 148 | if (index == ctx->search_paths.count) { |
| 149 | LOGARG(ctx, value); |
| 150 | return LY_EINVAL; |
| 151 | } else { |
| 152 | return ly_set_rm_index(&ctx->search_paths, index, free); |
| 153 | } |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 154 | } else { |
| 155 | /* remove them all */ |
Radek Krejci | a40f21b | 2018-09-18 10:42:08 +0200 | [diff] [blame] | 156 | ly_set_erase(&ctx->search_paths, free); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 157 | memset(&ctx->search_paths, 0, sizeof ctx->search_paths); |
| 158 | } |
| 159 | |
| 160 | return LY_SUCCESS; |
| 161 | } |
| 162 | |
| 163 | API LY_ERR |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 164 | ly_ctx_unset_searchdir(struct ly_ctx *ctx, unsigned int index) |
| 165 | { |
| 166 | LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL); |
| 167 | |
| 168 | if (!ctx->search_paths.count) { |
| 169 | return LY_SUCCESS; |
| 170 | } |
| 171 | |
| 172 | if (index >= ctx->search_paths.count) { |
| 173 | LOGARG(ctx, value); |
| 174 | return LY_EINVAL; |
| 175 | } else { |
| 176 | return ly_set_rm_index(&ctx->search_paths, index, free); |
| 177 | } |
| 178 | |
| 179 | return LY_SUCCESS; |
| 180 | } |
| 181 | |
| 182 | API const struct lys_module * |
| 183 | ly_ctx_load_module(struct ly_ctx *ctx, const char *name, const char *revision) |
| 184 | { |
| 185 | struct lys_module *result = NULL; |
| 186 | |
| 187 | LY_CHECK_ARG_RET(ctx, ctx, name, NULL); |
| 188 | |
| 189 | LY_CHECK_RET(lysp_load_module(ctx, name, revision, 1, 0, &result), NULL); |
| 190 | return result; |
| 191 | } |
| 192 | |
| 193 | API LY_ERR |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 194 | ly_ctx_new(const char *search_dir, int options, struct ly_ctx **new_ctx) |
| 195 | { |
| 196 | struct ly_ctx *ctx = NULL; |
| 197 | struct lys_module *module; |
| 198 | char *search_dir_list; |
| 199 | char *sep, *dir; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 200 | int i; |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 201 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 202 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 203 | LY_CHECK_ARG_RET(NULL, new_ctx, LY_EINVAL); |
| 204 | |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 205 | ctx = calloc(1, sizeof *ctx); |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 206 | LY_CHECK_ERR_RET(!ctx, LOGMEM(NULL), LY_EMEM); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 207 | |
| 208 | /* dictionary */ |
| 209 | lydict_init(&ctx->dict); |
| 210 | |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 211 | #if 0 /* TODO when plugins implemented */ |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 212 | /* plugins */ |
| 213 | ly_load_plugins(); |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 214 | #endif |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 215 | |
| 216 | /* initialize thread-specific key */ |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 217 | while ((pthread_key_create(&ctx->errlist_key, ly_err_free)) == EAGAIN); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 218 | |
| 219 | /* models list */ |
| 220 | ctx->flags = options; |
| 221 | if (search_dir) { |
| 222 | search_dir_list = strdup(search_dir); |
| 223 | LY_CHECK_ERR_GOTO(!search_dir_list, LOGMEM(NULL); rc = LY_EMEM, error); |
| 224 | |
| 225 | for (dir = search_dir_list; (sep = strchr(dir, ':')) != NULL && rc == LY_SUCCESS; dir = sep + 1) { |
| 226 | *sep = 0; |
| 227 | rc = ly_ctx_set_searchdir(ctx, dir); |
Radek Krejci | 14946ab | 2018-09-20 13:42:06 +0200 | [diff] [blame] | 228 | if (rc == LY_EEXIST) { |
| 229 | /* ignore duplication */ |
| 230 | rc = LY_SUCCESS; |
| 231 | } |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 232 | } |
| 233 | if (*dir && rc == LY_SUCCESS) { |
| 234 | rc = ly_ctx_set_searchdir(ctx, dir); |
Radek Krejci | 14946ab | 2018-09-20 13:42:06 +0200 | [diff] [blame] | 235 | if (rc == LY_EEXIST) { |
| 236 | /* ignore duplication */ |
| 237 | rc = LY_SUCCESS; |
| 238 | } |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 239 | } |
| 240 | free(search_dir_list); |
| 241 | |
| 242 | /* If ly_ctx_set_searchdir() failed, the error is already logged. Just exit */ |
| 243 | if (rc != LY_SUCCESS) { |
| 244 | goto error; |
| 245 | } |
| 246 | } |
| 247 | ctx->module_set_id = 1; |
| 248 | |
| 249 | /* load internal modules */ |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 250 | for (i = 0; i < ((options & LY_CTX_NOYANGLIBRARY) ? (LY_INTERNAL_MODS_COUNT - 2) : LY_INTERNAL_MODS_COUNT); i++) { |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 251 | module = (struct lys_module *)lys_parse_mem_module(ctx, internal_modules[i].data, internal_modules[i].format, |
| 252 | internal_modules[i].implemented, NULL, NULL); |
Radek Krejci | 0e75e6f | 2018-11-08 09:40:02 +0100 | [diff] [blame] | 253 | LY_CHECK_ERR_GOTO(!module, rc = ly_errcode(ctx), error); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 254 | LY_CHECK_GOTO((rc = lys_compile(&module, 0)), error); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | *new_ctx = ctx; |
| 258 | return rc; |
| 259 | |
| 260 | error: |
| 261 | ly_ctx_destroy(ctx, NULL); |
| 262 | return rc; |
| 263 | } |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 264 | API int |
Radek Krejci | 3fbe89a | 2018-09-20 13:54:46 +0200 | [diff] [blame] | 265 | ly_ctx_get_options(const struct ly_ctx *ctx) |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 266 | { |
Radek Krejci | 3fbe89a | 2018-09-20 13:54:46 +0200 | [diff] [blame] | 267 | LY_CHECK_ARG_RET(ctx, ctx, 0); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 268 | return ctx->flags; |
| 269 | } |
| 270 | |
Radek Krejci | ca828d9 | 2018-09-20 14:19:43 +0200 | [diff] [blame] | 271 | API LY_ERR |
Radek Krejci | 3fa46b6 | 2019-09-11 10:47:30 +0200 | [diff] [blame] | 272 | ly_ctx_set_options(struct ly_ctx *ctx, int option) |
Radek Krejci | ca828d9 | 2018-09-20 14:19:43 +0200 | [diff] [blame] | 273 | { |
| 274 | LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL); |
| 275 | LY_CHECK_ERR_RET(option & LY_CTX_NOYANGLIBRARY, LOGARG(ctx, option), LY_EINVAL); |
| 276 | |
| 277 | /* set the option(s) */ |
| 278 | ctx->flags |= option; |
| 279 | |
| 280 | return LY_SUCCESS; |
| 281 | } |
| 282 | |
| 283 | API LY_ERR |
Radek Krejci | 3fa46b6 | 2019-09-11 10:47:30 +0200 | [diff] [blame] | 284 | ly_ctx_unset_options(struct ly_ctx *ctx, int option) |
Radek Krejci | ca828d9 | 2018-09-20 14:19:43 +0200 | [diff] [blame] | 285 | { |
| 286 | LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL); |
| 287 | LY_CHECK_ERR_RET(option & LY_CTX_NOYANGLIBRARY, LOGARG(ctx, option), LY_EINVAL); |
| 288 | |
| 289 | /* unset the option(s) */ |
| 290 | ctx->flags &= ~option; |
| 291 | |
| 292 | return LY_SUCCESS; |
| 293 | } |
| 294 | |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 295 | API uint16_t |
| 296 | ly_ctx_get_module_set_id(const struct ly_ctx *ctx) |
| 297 | { |
Radek Krejci | 3fbe89a | 2018-09-20 13:54:46 +0200 | [diff] [blame] | 298 | LY_CHECK_ARG_RET(ctx, ctx, 0); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 299 | return ctx->module_set_id; |
| 300 | } |
| 301 | |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 302 | API void |
| 303 | ly_ctx_set_module_imp_clb(struct ly_ctx *ctx, ly_module_imp_clb clb, void *user_data) |
| 304 | { |
| 305 | LY_CHECK_ARG_RET(ctx, ctx,); |
| 306 | |
| 307 | ctx->imp_clb = clb; |
| 308 | ctx->imp_clb_data = user_data; |
| 309 | } |
| 310 | |
| 311 | API ly_module_imp_clb |
| 312 | ly_ctx_get_module_imp_clb(const struct ly_ctx *ctx, void **user_data) |
| 313 | { |
| 314 | LY_CHECK_ARG_RET(ctx, ctx, NULL); |
| 315 | |
| 316 | if (user_data) { |
| 317 | *user_data = ctx->imp_clb_data; |
| 318 | } |
| 319 | return ctx->imp_clb; |
| 320 | } |
| 321 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 322 | API const struct lys_module * |
| 323 | ly_ctx_get_module_iter(const struct ly_ctx *ctx, unsigned int *index) |
| 324 | { |
| 325 | LY_CHECK_ARG_RET(ctx, ctx, index, NULL); |
| 326 | |
Radek Krejci | 2390fb5 | 2019-04-30 13:27:43 +0200 | [diff] [blame] | 327 | if (*index < (unsigned)ctx->list.count) { |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 328 | return ctx->list.objs[(*index)++]; |
Radek Krejci | 2390fb5 | 2019-04-30 13:27:43 +0200 | [diff] [blame] | 329 | } else { |
| 330 | return NULL; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 331 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 332 | } |
| 333 | |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 334 | /** |
| 335 | * @brief Iterate over the modules in the given context. Returned modules must match the given key at the offset of |
| 336 | * lysp_module and lysc_module structures (they are supposed to be placed at the same offset in both structures). |
| 337 | * |
| 338 | * @param[in] ctx Context where to iterate. |
| 339 | * @param[in] key Key value to search for. |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 340 | * @param[in] key_offset Key's offset in struct lys_module to get value from the context's modules to match with the key. |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 341 | * @param[in,out] Iterator to pass between the function calls. On the first call, the variable is supposed to be |
| 342 | * initiated to 0. After each call returning a module, the value is greater by 1 than the index of the returned |
| 343 | * module in the context. |
| 344 | * @return Module matching the given key, NULL if no such module found. |
| 345 | */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 346 | static struct lys_module * |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 347 | ly_ctx_get_module_by_iter(const struct ly_ctx *ctx, const char *key, size_t key_offset, unsigned int *index) |
| 348 | { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 349 | struct lys_module *mod; |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 350 | const char *value; |
| 351 | |
| 352 | for (; *index < ctx->list.count; ++(*index)) { |
| 353 | mod = ctx->list.objs[*index]; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 354 | value = *(const char**)(((int8_t*)(mod)) + key_offset); |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 355 | if (!strcmp(key, value)) { |
| 356 | /* increment index for the next run */ |
| 357 | ++(*index); |
| 358 | return mod; |
| 359 | } |
| 360 | } |
| 361 | /* done */ |
| 362 | return NULL; |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * @brief Unifying function for ly_ctx_get_module() and ly_ctx_get_module_ns() |
| 367 | * @param[in] ctx Context where to search. |
| 368 | * @param[in] key Name or Namespace as a search key. |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 369 | * @param[in] key_offset Key's offset in struct lys_module to get value from the context's modules to match with the key. |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 370 | * @param[in] revision Revision date to match. If NULL, the matching module must have no revision. To search for the latest |
| 371 | * revision module, use ly_ctx_get_module_latest_by(). |
| 372 | * @return Matching module if any. |
| 373 | */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 374 | static struct lys_module * |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 375 | ly_ctx_get_module_by(const struct ly_ctx *ctx, const char *key, size_t key_offset, const char *revision) |
| 376 | { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 377 | struct lys_module *mod; |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 378 | unsigned int index = 0; |
| 379 | |
| 380 | while ((mod = ly_ctx_get_module_by_iter(ctx, key, key_offset, &index))) { |
| 381 | if (!revision) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 382 | if (!mod->revision) { |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 383 | /* found requested module without revision */ |
| 384 | return mod; |
| 385 | } |
| 386 | } else { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 387 | if (mod->revision && !strcmp(mod->revision, revision)) { |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 388 | /* found requested module of the specific revision */ |
| 389 | return mod; |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | return NULL; |
| 395 | } |
| 396 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 397 | API struct lys_module * |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 398 | ly_ctx_get_module_ns(const struct ly_ctx *ctx, const char *ns, const char *revision) |
| 399 | { |
| 400 | LY_CHECK_ARG_RET(ctx, ctx, ns, NULL); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 401 | return ly_ctx_get_module_by(ctx, ns, offsetof(struct lys_module, ns), revision); |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 402 | } |
| 403 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 404 | API struct lys_module * |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 405 | ly_ctx_get_module(const struct ly_ctx *ctx, const char *name, const char *revision) |
| 406 | { |
| 407 | LY_CHECK_ARG_RET(ctx, ctx, name, NULL); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 408 | return ly_ctx_get_module_by(ctx, name, offsetof(struct lys_module, name), revision); |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | /** |
| 412 | * @brief Unifying function for ly_ctx_get_module_latest() and ly_ctx_get_module_latest_ns() |
| 413 | * @param[in] ctx Context where to search. |
| 414 | * @param[in] key Name or Namespace as a search key. |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 415 | * @param[in] key_offset Key's offset in struct lys_module to get value from the context's modules to match with the key. |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 416 | * @return Matching module if any. |
| 417 | */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 418 | static struct lys_module * |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 419 | ly_ctx_get_module_latest_by(const struct ly_ctx *ctx, const char *key, size_t key_offset) |
| 420 | { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 421 | struct lys_module *mod; |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 422 | unsigned int index = 0; |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 423 | |
| 424 | while ((mod = ly_ctx_get_module_by_iter(ctx, key, key_offset, &index))) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 425 | if (mod->latest_revision) { |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 426 | return mod; |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 430 | return NULL; |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 431 | } |
| 432 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 433 | API struct lys_module * |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 434 | ly_ctx_get_module_latest(const struct ly_ctx *ctx, const char *name) |
| 435 | { |
| 436 | LY_CHECK_ARG_RET(ctx, ctx, name, NULL); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 437 | return ly_ctx_get_module_latest_by(ctx, name, offsetof(struct lys_module, name)); |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 438 | } |
| 439 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 440 | API struct lys_module * |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 441 | ly_ctx_get_module_latest_ns(const struct ly_ctx *ctx, const char *ns) |
| 442 | { |
| 443 | LY_CHECK_ARG_RET(ctx, ctx, ns, NULL); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 444 | return ly_ctx_get_module_latest_by(ctx, ns, offsetof(struct lys_module, ns)); |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | /** |
| 448 | * @brief Unifying function for ly_ctx_get_module_implemented() and ly_ctx_get_module_implemented_ns() |
| 449 | * @param[in] ctx Context where to search. |
| 450 | * @param[in] key Name or Namespace as a search key. |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 451 | * @param[in] key_offset Key's offset in struct lys_module to get value from the context's modules to match with the key. |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 452 | * @return Matching module if any. |
| 453 | */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 454 | static struct lys_module * |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 455 | ly_ctx_get_module_implemented_by(const struct ly_ctx *ctx, const char *key, size_t key_offset) |
| 456 | { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 457 | struct lys_module *mod; |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 458 | unsigned int index = 0; |
| 459 | |
| 460 | while ((mod = ly_ctx_get_module_by_iter(ctx, key, key_offset, &index))) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 461 | if (mod->implemented) { |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 462 | return mod; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | return NULL; |
| 467 | } |
| 468 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 469 | API struct lys_module * |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 470 | ly_ctx_get_module_implemented(const struct ly_ctx *ctx, const char *name) |
| 471 | { |
| 472 | LY_CHECK_ARG_RET(ctx, ctx, name, NULL); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 473 | return ly_ctx_get_module_implemented_by(ctx, name, offsetof(struct lys_module, name)); |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 474 | } |
| 475 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 476 | API struct lys_module * |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 477 | ly_ctx_get_module_implemented_ns(const struct ly_ctx *ctx, const char *ns) |
| 478 | { |
| 479 | LY_CHECK_ARG_RET(ctx, ctx, ns, NULL); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 480 | return ly_ctx_get_module_implemented_by(ctx, ns, offsetof(struct lys_module, ns)); |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 481 | } |
| 482 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 483 | struct lysp_submodule * |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 484 | ly_ctx_get_submodule(const struct ly_ctx *ctx, const char *module, const char *submodule, const char *revision) |
| 485 | { |
| 486 | const struct lys_module *mod; |
| 487 | struct lysp_include *inc; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 488 | uint32_t v; |
| 489 | LY_ARRAY_SIZE_TYPE u; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 490 | |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 491 | assert(submodule); |
| 492 | |
| 493 | for (v = 0; v < ctx->list.count; ++v) { |
| 494 | mod = ctx->list.objs[v]; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 495 | if (!mod->parsed) { |
| 496 | continue; |
| 497 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 498 | if (module && strcmp(module, mod->name)) { |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 499 | continue; |
| 500 | } |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 501 | |
| 502 | LY_ARRAY_FOR(mod->parsed->includes, u) { |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 503 | 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] | 504 | inc = &mod->parsed->includes[u]; |
| 505 | if (!revision) { |
| 506 | if (inc->submodule->latest_revision) { |
| 507 | return inc->submodule; |
| 508 | } |
| 509 | } else if (inc->submodule->revs && !strcmp(revision, inc->submodule->revs[0].date)) { |
| 510 | return inc->submodule; |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | return NULL; |
| 517 | } |
| 518 | |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 519 | API void |
Radek Krejci | e9e987e | 2018-10-31 12:50:27 +0100 | [diff] [blame] | 520 | ly_ctx_reset_latests(struct ly_ctx *ctx) |
| 521 | { |
Radek Krejci | e9e987e | 2018-10-31 12:50:27 +0100 | [diff] [blame] | 522 | struct lys_module *mod; |
| 523 | |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 524 | for (uint32_t v = 0; v < ctx->list.count; ++v) { |
| 525 | mod = ctx->list.objs[v]; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 526 | if (mod->latest_revision == 2) { |
| 527 | mod->latest_revision = 1; |
Radek Krejci | e9e987e | 2018-10-31 12:50:27 +0100 | [diff] [blame] | 528 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 529 | if (mod->parsed && mod->parsed->includes) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 530 | for (LY_ARRAY_SIZE_TYPE u = 0; u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) { |
| 531 | if (mod->parsed->includes[u].submodule->latest_revision == 2) { |
| 532 | mod->parsed->includes[u].submodule->latest_revision = 1; |
Radek Krejci | e9e987e | 2018-10-31 12:50:27 +0100 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | |
Michal Vasko | 57c10cd | 2020-05-27 15:57:11 +0200 | [diff] [blame] | 539 | static LY_ERR |
| 540 | ylib_feature(struct lyd_node *parent, const struct lys_module *cur_mod) |
| 541 | { |
| 542 | LY_ARRAY_SIZE_TYPE i; |
| 543 | struct lyd_node *node; |
| 544 | |
| 545 | if (!cur_mod->implemented) { |
| 546 | /* no features can be enabled */ |
| 547 | return LY_SUCCESS; |
| 548 | } |
| 549 | |
| 550 | LY_ARRAY_FOR(cur_mod->compiled->features, i) { |
| 551 | if (!(cur_mod->compiled->features[i].flags & LYS_FENABLED)) { |
| 552 | continue; |
| 553 | } |
| 554 | |
| 555 | node = lyd_new_term(parent, NULL, "feature", cur_mod->compiled->features[i].name); |
| 556 | LY_CHECK_RET(!node, LY_EOTHER); |
| 557 | } |
| 558 | |
| 559 | return LY_SUCCESS; |
| 560 | } |
| 561 | |
| 562 | static LY_ERR |
| 563 | ylib_deviation(struct lyd_node *parent, const struct lys_module *cur_mod, int bis) |
| 564 | { |
| 565 | LY_ARRAY_SIZE_TYPE i; |
| 566 | struct lyd_node *node; |
| 567 | struct lys_module *mod; |
| 568 | |
| 569 | if (!cur_mod->implemented) { |
| 570 | /* no deviations of the module for certain */ |
| 571 | return LY_SUCCESS; |
| 572 | } |
| 573 | |
| 574 | LY_ARRAY_FOR(cur_mod->compiled->deviated_by, i) { |
| 575 | mod = cur_mod->compiled->deviated_by[i]; |
| 576 | |
| 577 | if (bis) { |
| 578 | node = lyd_new_term(parent, NULL, "deviation", mod->name); |
| 579 | LY_CHECK_RET(!node, LY_EOTHER); |
| 580 | } else { |
| 581 | node = lyd_new_list(parent, NULL, "deviation", mod->name, (mod->parsed->revs ? mod->parsed->revs[0].date : "")); |
| 582 | LY_CHECK_RET(!node, LY_EOTHER); |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | return LY_SUCCESS; |
| 587 | } |
| 588 | |
| 589 | static LY_ERR |
| 590 | ylib_submodules(struct lyd_node *parent, const struct lys_module *cur_mod, int bis) |
| 591 | { |
| 592 | LY_ARRAY_SIZE_TYPE i; |
| 593 | struct lyd_node *node, *cont; |
| 594 | struct lysp_submodule *submod; |
| 595 | int ret; |
| 596 | char *str; |
| 597 | |
| 598 | LY_ARRAY_FOR(cur_mod->parsed->includes, i) { |
| 599 | submod = cur_mod->parsed->includes[i].submodule; |
| 600 | |
| 601 | if (bis) { |
| 602 | cont = lyd_new_list(parent, NULL, "submodule", submod->name); |
| 603 | LY_CHECK_RET(!cont, LY_EOTHER); |
| 604 | |
| 605 | if (submod->revs) { |
| 606 | node = lyd_new_term(cont, NULL, "revision", submod->revs[0].date); |
| 607 | LY_CHECK_RET(!node, LY_EOTHER); |
| 608 | } |
| 609 | } else { |
| 610 | cont = lyd_new_list(parent, NULL, "submodule", submod->name, (submod->revs ? submod->revs[0].date : "")); |
| 611 | LY_CHECK_RET(!cont, LY_EOTHER); |
| 612 | } |
| 613 | |
| 614 | if (submod->filepath) { |
| 615 | ret = asprintf(&str, "file://%s", submod->filepath); |
| 616 | LY_CHECK_ERR_RET(ret == -1, LOGMEM(cur_mod->ctx), LY_EMEM); |
| 617 | |
| 618 | node = lyd_new_term(cont, NULL, bis ? "location" : "schema", str); |
| 619 | LY_CHECK_RET(!node, LY_EOTHER); |
| 620 | free(str); |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | return LY_SUCCESS; |
| 625 | } |
| 626 | |
| 627 | API uint16_t |
| 628 | ly_ctx_get_yanglib_id(const struct ly_ctx *ctx) |
| 629 | { |
| 630 | return ctx->module_set_id; |
| 631 | } |
| 632 | |
| 633 | API struct lyd_node * |
| 634 | ly_ctx_get_yanglib_data(const struct ly_ctx *ctx) |
| 635 | { |
| 636 | uint32_t i; |
| 637 | int bis = 0, ret; |
| 638 | char id[8], *str; |
| 639 | const struct lys_module *mod; |
| 640 | struct lyd_node *root = NULL, *root_bis = NULL, *cont, *set_bis, *node; |
| 641 | |
| 642 | LY_CHECK_ARG_RET(ctx, ctx, NULL); |
| 643 | |
| 644 | mod = ly_ctx_get_module_implemented(ctx, "ietf-yang-library"); |
| 645 | LY_CHECK_ERR_RET(!mod, LOGERR(ctx, LY_EINVAL, "Module \"ietf-yang-library\" is not implemented."), NULL); |
| 646 | |
| 647 | if (mod->parsed->revs && !strcmp(mod->parsed->revs[0].date, "2016-06-21")) { |
| 648 | bis = 0; |
| 649 | } else if (mod->parsed->revs && !strcmp(mod->parsed->revs[0].date, IETF_YANG_LIB_REV)) { |
| 650 | bis = 1; |
| 651 | } else { |
| 652 | LOGERR(ctx, LY_EINVAL, "Incompatible ietf-yang-library version in context."); |
| 653 | return NULL; |
| 654 | } |
| 655 | |
| 656 | root = lyd_new_inner(NULL, mod, "modules-state"); |
| 657 | LY_CHECK_GOTO(!root, error); |
| 658 | |
| 659 | if (bis) { |
| 660 | root_bis = lyd_new_inner(NULL, mod, "yang-library"); |
| 661 | LY_CHECK_GOTO(!root_bis, error); |
| 662 | |
| 663 | set_bis = lyd_new_list(root_bis, NULL, "module-set", "complete"); |
| 664 | LY_CHECK_GOTO(!set_bis, error); |
| 665 | } |
| 666 | |
| 667 | for (i = 0; i < ctx->list.count; ++i) { |
| 668 | mod = ctx->list.objs[i]; |
| 669 | |
| 670 | /* |
| 671 | * deprecated legacy |
| 672 | */ |
| 673 | cont = lyd_new_list(root, NULL, "module", mod->name, (mod->parsed->revs ? mod->parsed->revs[0].date : "")); |
| 674 | LY_CHECK_GOTO(!cont, error); |
| 675 | |
| 676 | /* schema */ |
| 677 | if (mod->filepath) { |
| 678 | ret = asprintf(&str, "file://%s", mod->filepath); |
| 679 | LY_CHECK_ERR_GOTO(ret == -1, LOGMEM(ctx), error); |
| 680 | |
| 681 | node = lyd_new_term(cont, NULL, "schema", str); |
| 682 | free(str); |
| 683 | LY_CHECK_GOTO(!node, error); |
| 684 | } |
| 685 | |
| 686 | /* namespace */ |
| 687 | node = lyd_new_term(cont, NULL, "namespace", mod->ns); |
| 688 | LY_CHECK_GOTO(!node, error); |
| 689 | |
| 690 | /* feature leaf-list */ |
| 691 | LY_CHECK_GOTO(ylib_feature(cont, mod), error); |
| 692 | |
| 693 | /* deviation list */ |
| 694 | LY_CHECK_GOTO(ylib_deviation(cont, mod, 0), error); |
| 695 | |
| 696 | /* conformance-type */ |
| 697 | node = lyd_new_term(cont, NULL, "conformance-type", (mod->implemented ? "implement" : "import")); |
| 698 | LY_CHECK_GOTO(!node, error); |
| 699 | |
| 700 | /* submodule list */ |
| 701 | LY_CHECK_GOTO(ylib_submodules(cont, mod, 0), error); |
| 702 | |
| 703 | /* |
| 704 | * current revision |
| 705 | */ |
| 706 | if (bis) { |
| 707 | /* name and revision */ |
| 708 | if (mod->implemented) { |
| 709 | cont = lyd_new_list(set_bis, NULL, "module", mod->name); |
| 710 | LY_CHECK_GOTO(!cont, error); |
| 711 | |
| 712 | if (mod->parsed->revs) { |
| 713 | node = lyd_new_term(cont, NULL, "revision", mod->parsed->revs[0].date); |
| 714 | LY_CHECK_GOTO(!node, error); |
| 715 | } |
| 716 | } else { |
| 717 | cont = lyd_new_list(set_bis, NULL, "import-only-module", mod->name, |
| 718 | (mod->parsed->revs ? mod->parsed->revs[0].date : "")); |
| 719 | LY_CHECK_GOTO(!cont, error); |
| 720 | } |
| 721 | |
| 722 | /* namespace */ |
| 723 | node = lyd_new_term(cont, NULL, "namespace", mod->ns); |
| 724 | LY_CHECK_GOTO(!node, error); |
| 725 | |
| 726 | /* location */ |
| 727 | if (mod->filepath) { |
| 728 | ret = asprintf(&str, "file://%s", mod->filepath); |
| 729 | LY_CHECK_ERR_GOTO(ret == -1, LOGMEM(ctx), error); |
| 730 | |
| 731 | node = lyd_new_term(cont, NULL, "schema", str); |
| 732 | free(str); |
| 733 | LY_CHECK_GOTO(!node, error); |
| 734 | } |
| 735 | |
| 736 | /* submodule list */ |
| 737 | LY_CHECK_GOTO(ylib_submodules(cont, mod, 1), error); |
| 738 | |
| 739 | /* feature list */ |
| 740 | LY_CHECK_GOTO(ylib_feature(cont, mod), error); |
| 741 | |
| 742 | /* deviation */ |
| 743 | LY_CHECK_GOTO(ylib_deviation(cont, mod, 1), error); |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | /* IDs */ |
| 748 | sprintf(id, "%u", ctx->module_set_id); |
| 749 | node = lyd_new_term(root, NULL, "module-set-id", id); |
| 750 | LY_CHECK_GOTO(!node, error); |
| 751 | |
| 752 | if (bis) { |
| 753 | /* create one complete schema */ |
| 754 | cont = lyd_new_list(root_bis, NULL, "schema", "complete"); |
| 755 | LY_CHECK_GOTO(!cont, error); |
| 756 | |
| 757 | node = lyd_new_term(cont, NULL, "module-set", "complete"); |
| 758 | LY_CHECK_GOTO(!node, error); |
| 759 | |
| 760 | /* content-id */ |
| 761 | node = lyd_new_term(root_bis, NULL, "content-id", id); |
| 762 | LY_CHECK_GOTO(!node, error); |
| 763 | } |
| 764 | |
| 765 | if (root_bis) { |
| 766 | if (lyd_insert_sibling(root_bis, root)) { |
| 767 | goto error; |
| 768 | } |
| 769 | root = root_bis; |
| 770 | root_bis = 0; |
| 771 | } |
| 772 | |
| 773 | /* TODO uncomment once lefref validation works |
| 774 | if (lyd_validate(&root, NULL, LYD_VALOPT_DATA_ONLY)) { |
| 775 | goto error; |
| 776 | }*/ |
| 777 | |
| 778 | return root; |
| 779 | |
| 780 | error: |
| 781 | lyd_free_all(root); |
| 782 | lyd_free_all(root_bis); |
| 783 | return NULL; |
| 784 | } |
| 785 | |
Radek Krejci | e9e987e | 2018-10-31 12:50:27 +0100 | [diff] [blame] | 786 | API void |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 787 | 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] | 788 | { |
Radek Krejci | 418823a | 2018-09-20 16:42:13 +0200 | [diff] [blame] | 789 | if (!ctx) { |
| 790 | return; |
| 791 | } |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 792 | |
| 793 | /* models list */ |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 794 | for (; ctx->list.count; ctx->list.count--) { |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 795 | /* remove the module */ |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 796 | lys_module_free(ctx->list.objs[ctx->list.count - 1], private_destructor); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 797 | } |
| 798 | free(ctx->list.objs); |
| 799 | |
| 800 | /* search paths list */ |
Radek Krejci | a40f21b | 2018-09-18 10:42:08 +0200 | [diff] [blame] | 801 | ly_set_erase(&ctx->search_paths, free); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 802 | |
| 803 | /* clean the error list */ |
| 804 | ly_err_clean(ctx, 0); |
| 805 | pthread_key_delete(ctx->errlist_key); |
| 806 | |
| 807 | /* dictionary */ |
| 808 | lydict_clean(&ctx->dict); |
| 809 | |
| 810 | #if 0 /* TODO when plugins implemented */ |
| 811 | /* plugins - will be removed only if this is the last context */ |
| 812 | ly_clean_plugins(); |
| 813 | #endif |
| 814 | |
| 815 | free(ctx); |
| 816 | } |