blob: 393adbfdb1697606acc53e5907cea82abc643a5c [file] [log] [blame]
Radek Krejci0af5f5d2018-09-07 15:00:30 +02001/**
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 Krejcib7db73a2018-10-24 14:18:40 +020015#include "common.h"
16
Radek Krejci0af5f5d2018-09-07 15:00:30 +020017#include <errno.h>
Radek Krejci7ada9a02018-09-07 15:34:05 +020018#include <limits.h>
19#include <stdlib.h>
20#include <string.h>
Radek Krejci0e212402018-09-20 11:29:38 +020021#include <sys/stat.h>
22#include <sys/types.h>
Radek Krejci0af5f5d2018-09-07 15:00:30 +020023#include <unistd.h>
24
Radek Krejci0af5f5d2018-09-07 15:00:30 +020025#include "context.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020026#include "tree_schema_internal.h"
Radek Krejciad573502018-09-07 15:26:55 +020027#include "libyang.h"
Radek Krejci0af5f5d2018-09-07 15:00:30 +020028
Radek Krejci86d106e2018-10-18 09:53:19 +020029#define LY_INTERNAL_MODS_COUNT 6
Radek Krejci0af5f5d2018-09-07 15:00:30 +020030
Radek Krejcif3f47842018-11-15 11:22:15 +010031#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 Krejci0af5f5d2018-09-07 15:00:30 +020037#define IETF_YANG_LIB_REV "2018-01-17"
38
Radek Krejci0af5f5d2018-09-07 15:00:30 +020039static 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 Krejci86d106e2018-10-18 09:53:19 +020046 {"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 Krejci0af5f5d2018-09-07 15:00:30 +020050 /* ietf-datastores and ietf-yang-library must be right here at the end of the list! */
Radek Krejci096235c2019-01-11 11:12:19 +010051 {"ietf-datastores", "2017-08-17", (const char*)ietf_datastores_2017_08_17_yang, 1, LYS_IN_YANG},
Radek Krejci86d106e2018-10-18 09:53:19 +020052 {"ietf-yang-library", IETF_YANG_LIB_REV, (const char*)ietf_yang_library_2018_01_17_yang, 1, LYS_IN_YANG}
Radek Krejci0af5f5d2018-09-07 15:00:30 +020053};
54
55API LY_ERR
56ly_ctx_set_searchdir(struct ly_ctx *ctx, const char *search_dir)
57{
Radek Krejci0e212402018-09-20 11:29:38 +020058 struct stat st;
Radek Krejci0af5f5d2018-09-07 15:00:30 +020059 char *new_dir = NULL;
Radek Krejci0e212402018-09-20 11:29:38 +020060 unsigned int u;
Radek Krejci0af5f5d2018-09-07 15:00:30 +020061
62 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
63
64 if (search_dir) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +020065 new_dir = realpath(search_dir, NULL);
Radek Krejcia77e0e12018-09-20 12:39:15 +020066 LY_CHECK_ERR_RET(!new_dir,
Radek Krejci3a077b92019-04-09 17:10:35 +020067 LOGERR(ctx, LY_ESYS, "Unable to use search directory \"%s\" (%s).", search_dir, strerror(errno)),
68 LY_EINVAL);
69 if (strcmp(search_dir, new_dir)) {
70 LOGVRB("Canonicalizing search directory string from \"%s\" to \"%s\".", search_dir, new_dir);
71 }
72 LY_CHECK_ERR_RET(access(new_dir, R_OK | X_OK),
73 LOGERR(ctx, LY_ESYS, "Unable to fully access search directory \"%s\" (%s).", new_dir, strerror(errno)); free(new_dir),
74 LY_EINVAL);
75 LY_CHECK_ERR_RET(stat(new_dir, &st),
76 LOGERR(ctx, LY_ESYS, "stat() failed for \"%s\" (%s).", new_dir, strerror(errno)); free(new_dir),
Radek Krejcia77e0e12018-09-20 12:39:15 +020077 LY_ESYS);
Radek Krejci3a077b92019-04-09 17:10:35 +020078 LY_CHECK_ERR_RET(!S_ISDIR(st.st_mode),
79 LOGERR(ctx, LY_ESYS, "Given search directory \"%s\" is not a directory.", new_dir); free(new_dir),
80 LY_EINVAL);
Radek Krejci0e212402018-09-20 11:29:38 +020081 /* avoid path duplication */
82 for (u = 0; u < ctx->search_paths.count; ++u) {
Radek Krejci14946ab2018-09-20 13:42:06 +020083 if (!strcmp(new_dir, ctx->search_paths.objs[u])) {
Radek Krejci0e212402018-09-20 11:29:38 +020084 free(new_dir);
Radek Krejci14946ab2018-09-20 13:42:06 +020085 return LY_EEXIST;
Radek Krejci0e212402018-09-20 11:29:38 +020086 }
87 }
88 if (ly_set_add(&ctx->search_paths, new_dir, LY_SET_OPT_USEASLIST) == -1) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +020089 free(new_dir);
90 return LY_EMEM;
91 }
92
Radek Krejcie9e987e2018-10-31 12:50:27 +010093 /* new searchdir - possibly more latest revision available */
94 ly_ctx_reset_latests(ctx);
95
Radek Krejci0e212402018-09-20 11:29:38 +020096 return LY_SUCCESS;
Radek Krejci0af5f5d2018-09-07 15:00:30 +020097 } else {
98 /* consider that no change is not actually an error */
99 return LY_SUCCESS;
100 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200101}
102
103API const char * const *
104ly_ctx_get_searchdirs(const struct ly_ctx *ctx)
105{
Radek Krejci05026432018-09-20 12:13:43 +0200106 void **new;
107
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200108 LY_CHECK_ARG_RET(ctx, ctx, NULL);
Radek Krejci05026432018-09-20 12:13:43 +0200109
110 if (ctx->search_paths.count == ctx->search_paths.size) {
111 /* not enough space for terminating NULL byte */
112 new = realloc(((struct ly_ctx *)ctx)->search_paths.objs, (ctx->search_paths.size + 8) * sizeof *ctx->search_paths.objs);
113 LY_CHECK_ERR_RET(!new, LOGMEM(NULL), NULL);
114 ((struct ly_ctx *)ctx)->search_paths.size += 8;
115 ((struct ly_ctx *)ctx)->search_paths.objs = new;
116 }
117 /* set terminating NULL byte to the strings list */
118 ctx->search_paths.objs[ctx->search_paths.count] = NULL;
119
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200120 return (const char * const *)ctx->search_paths.objs;
121}
122
123API LY_ERR
Radek Krejci0759b792018-09-20 13:53:15 +0200124ly_ctx_unset_searchdirs(struct ly_ctx *ctx, const char *value)
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200125{
Radek Krejci0759b792018-09-20 13:53:15 +0200126 unsigned int index;
127
Radek Krejcicd649072018-09-20 12:14:45 +0200128 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
Radek Krejcicd649072018-09-20 12:14:45 +0200129
Michal Vaskob34480a2018-09-17 10:34:45 +0200130 if (!ctx->search_paths.count) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200131 return LY_SUCCESS;
132 }
133
Radek Krejci0759b792018-09-20 13:53:15 +0200134 if (value) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200135 /* remove specific search directory */
Radek Krejci0759b792018-09-20 13:53:15 +0200136 for (index = 0; index < ctx->search_paths.count; ++index) {
137 if (!strcmp(value, ctx->search_paths.objs[index])) {
138 break;
139 }
140 }
141 if (index == ctx->search_paths.count) {
142 LOGARG(ctx, value);
143 return LY_EINVAL;
144 } else {
145 return ly_set_rm_index(&ctx->search_paths, index, free);
146 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200147 } else {
148 /* remove them all */
Radek Krejcia40f21b2018-09-18 10:42:08 +0200149 ly_set_erase(&ctx->search_paths, free);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200150 memset(&ctx->search_paths, 0, sizeof ctx->search_paths);
151 }
152
153 return LY_SUCCESS;
154}
155
156API LY_ERR
157ly_ctx_new(const char *search_dir, int options, struct ly_ctx **new_ctx)
158{
159 struct ly_ctx *ctx = NULL;
160 struct lys_module *module;
161 char *search_dir_list;
162 char *sep, *dir;
Radek Krejci86d106e2018-10-18 09:53:19 +0200163 int i;
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200164 LY_ERR rc = LY_SUCCESS;
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200165
166 ctx = calloc(1, sizeof *ctx);
Radek Krejciad573502018-09-07 15:26:55 +0200167 LY_CHECK_ERR_RET(!ctx, LOGMEM(NULL), LY_EMEM);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200168
169 /* dictionary */
170 lydict_init(&ctx->dict);
171
Radek Krejciad573502018-09-07 15:26:55 +0200172#if 0 /* TODO when plugins implemented */
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200173 /* plugins */
174 ly_load_plugins();
Radek Krejciad573502018-09-07 15:26:55 +0200175#endif
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200176
177 /* initialize thread-specific key */
Radek Krejciad573502018-09-07 15:26:55 +0200178 while ((pthread_key_create(&ctx->errlist_key, ly_err_free)) == EAGAIN);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200179
180 /* models list */
181 ctx->flags = options;
182 if (search_dir) {
183 search_dir_list = strdup(search_dir);
184 LY_CHECK_ERR_GOTO(!search_dir_list, LOGMEM(NULL); rc = LY_EMEM, error);
185
186 for (dir = search_dir_list; (sep = strchr(dir, ':')) != NULL && rc == LY_SUCCESS; dir = sep + 1) {
187 *sep = 0;
188 rc = ly_ctx_set_searchdir(ctx, dir);
Radek Krejci14946ab2018-09-20 13:42:06 +0200189 if (rc == LY_EEXIST) {
190 /* ignore duplication */
191 rc = LY_SUCCESS;
192 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200193 }
194 if (*dir && rc == LY_SUCCESS) {
195 rc = ly_ctx_set_searchdir(ctx, dir);
Radek Krejci14946ab2018-09-20 13:42:06 +0200196 if (rc == LY_EEXIST) {
197 /* ignore duplication */
198 rc = LY_SUCCESS;
199 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200200 }
201 free(search_dir_list);
202
203 /* If ly_ctx_set_searchdir() failed, the error is already logged. Just exit */
204 if (rc != LY_SUCCESS) {
205 goto error;
206 }
207 }
208 ctx->module_set_id = 1;
209
210 /* load internal modules */
Radek Krejci86d106e2018-10-18 09:53:19 +0200211 for (i = 0; i < ((options & LY_CTX_NOYANGLIBRARY) ? (LY_INTERNAL_MODS_COUNT - 2) : LY_INTERNAL_MODS_COUNT); i++) {
Radek Krejci096235c2019-01-11 11:12:19 +0100212 module = (struct lys_module *)lys_parse_mem_module(ctx, internal_modules[i].data, internal_modules[i].format,
213 internal_modules[i].implemented, NULL, NULL);
Radek Krejci0e75e6f2018-11-08 09:40:02 +0100214 LY_CHECK_ERR_GOTO(!module, rc = ly_errcode(ctx), error);
Radek Krejci096235c2019-01-11 11:12:19 +0100215 LY_CHECK_GOTO((rc = lys_compile(module, 0)), error);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200216 }
217
218 *new_ctx = ctx;
219 return rc;
220
221error:
222 ly_ctx_destroy(ctx, NULL);
223 return rc;
224}
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200225API int
Radek Krejci3fbe89a2018-09-20 13:54:46 +0200226ly_ctx_get_options(const struct ly_ctx *ctx)
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200227{
Radek Krejci3fbe89a2018-09-20 13:54:46 +0200228 LY_CHECK_ARG_RET(ctx, ctx, 0);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200229 return ctx->flags;
230}
231
Radek Krejcica828d92018-09-20 14:19:43 +0200232API LY_ERR
233ly_ctx_set_option(struct ly_ctx *ctx, int option)
234{
235 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
236 LY_CHECK_ERR_RET(option & LY_CTX_NOYANGLIBRARY, LOGARG(ctx, option), LY_EINVAL);
237
238 /* set the option(s) */
239 ctx->flags |= option;
240
241 return LY_SUCCESS;
242}
243
244API LY_ERR
245ly_ctx_unset_option(struct ly_ctx *ctx, int option)
246{
247 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
248 LY_CHECK_ERR_RET(option & LY_CTX_NOYANGLIBRARY, LOGARG(ctx, option), LY_EINVAL);
249
250 /* unset the option(s) */
251 ctx->flags &= ~option;
252
253 return LY_SUCCESS;
254}
255
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200256API uint16_t
257ly_ctx_get_module_set_id(const struct ly_ctx *ctx)
258{
Radek Krejci3fbe89a2018-09-20 13:54:46 +0200259 LY_CHECK_ARG_RET(ctx, ctx, 0);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200260 return ctx->module_set_id;
261}
262
Radek Krejcid33273d2018-10-25 14:55:52 +0200263API void
264ly_ctx_set_module_imp_clb(struct ly_ctx *ctx, ly_module_imp_clb clb, void *user_data)
265{
266 LY_CHECK_ARG_RET(ctx, ctx,);
267
268 ctx->imp_clb = clb;
269 ctx->imp_clb_data = user_data;
270}
271
272API ly_module_imp_clb
273ly_ctx_get_module_imp_clb(const struct ly_ctx *ctx, void **user_data)
274{
275 LY_CHECK_ARG_RET(ctx, ctx, NULL);
276
277 if (user_data) {
278 *user_data = ctx->imp_clb_data;
279 }
280 return ctx->imp_clb;
281}
282
Radek Krejcib7db73a2018-10-24 14:18:40 +0200283/**
284 * @brief Iterate over the modules in the given context. Returned modules must match the given key at the offset of
285 * lysp_module and lysc_module structures (they are supposed to be placed at the same offset in both structures).
286 *
287 * @param[in] ctx Context where to iterate.
288 * @param[in] key Key value to search for.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100289 * @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 Krejcib7db73a2018-10-24 14:18:40 +0200290 * @param[in,out] Iterator to pass between the function calls. On the first call, the variable is supposed to be
291 * initiated to 0. After each call returning a module, the value is greater by 1 than the index of the returned
292 * module in the context.
293 * @return Module matching the given key, NULL if no such module found.
294 */
Radek Krejci0af46292019-01-11 16:02:31 +0100295static struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200296ly_ctx_get_module_by_iter(const struct ly_ctx *ctx, const char *key, size_t key_offset, unsigned int *index)
297{
Radek Krejci0af46292019-01-11 16:02:31 +0100298 struct lys_module *mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200299 const char *value;
300
301 for (; *index < ctx->list.count; ++(*index)) {
302 mod = ctx->list.objs[*index];
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100303 value = *(const char**)(((int8_t*)(mod)) + key_offset);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200304 if (!strcmp(key, value)) {
305 /* increment index for the next run */
306 ++(*index);
307 return mod;
308 }
309 }
310 /* done */
311 return NULL;
312}
313
314/**
315 * @brief Unifying function for ly_ctx_get_module() and ly_ctx_get_module_ns()
316 * @param[in] ctx Context where to search.
317 * @param[in] key Name or Namespace as a search key.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100318 * @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 Krejcib7db73a2018-10-24 14:18:40 +0200319 * @param[in] revision Revision date to match. If NULL, the matching module must have no revision. To search for the latest
320 * revision module, use ly_ctx_get_module_latest_by().
321 * @return Matching module if any.
322 */
Radek Krejci0af46292019-01-11 16:02:31 +0100323static struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200324ly_ctx_get_module_by(const struct ly_ctx *ctx, const char *key, size_t key_offset, const char *revision)
325{
Radek Krejci0af46292019-01-11 16:02:31 +0100326 struct lys_module *mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200327 unsigned int index = 0;
328
329 while ((mod = ly_ctx_get_module_by_iter(ctx, key, key_offset, &index))) {
330 if (!revision) {
Radek Krejci0af46292019-01-11 16:02:31 +0100331 if (!mod->revision) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200332 /* found requested module without revision */
333 return mod;
334 }
335 } else {
Radek Krejci0af46292019-01-11 16:02:31 +0100336 if (mod->revision && !strcmp(mod->revision, revision)) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200337 /* found requested module of the specific revision */
338 return mod;
339 }
340 }
341 }
342
343 return NULL;
344}
345
Radek Krejci0af46292019-01-11 16:02:31 +0100346API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200347ly_ctx_get_module_ns(const struct ly_ctx *ctx, const char *ns, const char *revision)
348{
349 LY_CHECK_ARG_RET(ctx, ctx, ns, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100350 return ly_ctx_get_module_by(ctx, ns, offsetof(struct lys_module, ns), revision);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200351}
352
Radek Krejci0af46292019-01-11 16:02:31 +0100353API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200354ly_ctx_get_module(const struct ly_ctx *ctx, const char *name, const char *revision)
355{
356 LY_CHECK_ARG_RET(ctx, ctx, name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100357 return ly_ctx_get_module_by(ctx, name, offsetof(struct lys_module, name), revision);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200358}
359
360/**
361 * @brief Unifying function for ly_ctx_get_module_latest() and ly_ctx_get_module_latest_ns()
362 * @param[in] ctx Context where to search.
363 * @param[in] key Name or Namespace as a search key.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100364 * @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 Krejcib7db73a2018-10-24 14:18:40 +0200365 * @return Matching module if any.
366 */
Radek Krejci0af46292019-01-11 16:02:31 +0100367static struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200368ly_ctx_get_module_latest_by(const struct ly_ctx *ctx, const char *key, size_t key_offset)
369{
Radek Krejci0af46292019-01-11 16:02:31 +0100370 struct lys_module *mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200371 unsigned int index = 0;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200372
373 while ((mod = ly_ctx_get_module_by_iter(ctx, key, key_offset, &index))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100374 if (mod->latest_revision) {
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200375 return mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200376 }
377 }
378
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200379 return NULL;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200380}
381
Radek Krejci0af46292019-01-11 16:02:31 +0100382API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200383ly_ctx_get_module_latest(const struct ly_ctx *ctx, const char *name)
384{
385 LY_CHECK_ARG_RET(ctx, ctx, name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100386 return ly_ctx_get_module_latest_by(ctx, name, offsetof(struct lys_module, name));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200387}
388
Radek Krejci0af46292019-01-11 16:02:31 +0100389API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200390ly_ctx_get_module_latest_ns(const struct ly_ctx *ctx, const char *ns)
391{
392 LY_CHECK_ARG_RET(ctx, ctx, ns, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100393 return ly_ctx_get_module_latest_by(ctx, ns, offsetof(struct lys_module, ns));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200394}
395
396/**
397 * @brief Unifying function for ly_ctx_get_module_implemented() and ly_ctx_get_module_implemented_ns()
398 * @param[in] ctx Context where to search.
399 * @param[in] key Name or Namespace as a search key.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100400 * @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 Krejcib7db73a2018-10-24 14:18:40 +0200401 * @return Matching module if any.
402 */
Radek Krejci0af46292019-01-11 16:02:31 +0100403static struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200404ly_ctx_get_module_implemented_by(const struct ly_ctx *ctx, const char *key, size_t key_offset)
405{
Radek Krejci0af46292019-01-11 16:02:31 +0100406 struct lys_module *mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200407 unsigned int index = 0;
408
409 while ((mod = ly_ctx_get_module_by_iter(ctx, key, key_offset, &index))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100410 if (mod->implemented) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200411 return mod;
412 }
413 }
414
415 return NULL;
416}
417
Radek Krejci0af46292019-01-11 16:02:31 +0100418API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200419ly_ctx_get_module_implemented(const struct ly_ctx *ctx, const char *name)
420{
421 LY_CHECK_ARG_RET(ctx, ctx, name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100422 return ly_ctx_get_module_implemented_by(ctx, name, offsetof(struct lys_module, name));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200423}
424
Radek Krejci0af46292019-01-11 16:02:31 +0100425API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200426ly_ctx_get_module_implemented_ns(const struct ly_ctx *ctx, const char *ns)
427{
428 LY_CHECK_ARG_RET(ctx, ctx, ns, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100429 return ly_ctx_get_module_implemented_by(ctx, ns, offsetof(struct lys_module, ns));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200430}
431
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100432struct lysp_submodule *
Radek Krejcid33273d2018-10-25 14:55:52 +0200433ly_ctx_get_submodule(const struct ly_ctx *ctx, const char *module, const char *submodule, const char *revision)
434{
435 const struct lys_module *mod;
436 struct lysp_include *inc;
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100437 unsigned int v, u;
Radek Krejcid33273d2018-10-25 14:55:52 +0200438
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100439 assert(submodule);
440
441 for (v = 0; v < ctx->list.count; ++v) {
442 mod = ctx->list.objs[v];
Radek Krejcid33273d2018-10-25 14:55:52 +0200443 if (!mod->parsed) {
444 continue;
445 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100446 if (module && strcmp(module, mod->name)) {
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100447 continue;
448 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200449
450 LY_ARRAY_FOR(mod->parsed->includes, u) {
Radek Krejci086c7132018-10-26 15:29:04 +0200451 if (mod->parsed->includes[u].submodule && !strcmp(submodule, mod->parsed->includes[u].submodule->name)) {
Radek Krejcid33273d2018-10-25 14:55:52 +0200452 inc = &mod->parsed->includes[u];
453 if (!revision) {
454 if (inc->submodule->latest_revision) {
455 return inc->submodule;
456 }
457 } else if (inc->submodule->revs && !strcmp(revision, inc->submodule->revs[0].date)) {
458 return inc->submodule;
459 }
460 }
461 }
462 }
463
464 return NULL;
465}
466
Radek Krejci096235c2019-01-11 11:12:19 +0100467/* TODO ly_ctx_load_module() via lysp_load_module() */
468
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200469API void
Radek Krejcie9e987e2018-10-31 12:50:27 +0100470ly_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];
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100477 if (mod->latest_revision == 2) {
478 mod->latest_revision = 1;
Radek Krejcie9e987e2018-10-31 12:50:27 +0100479 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100480 if (mod->parsed && mod->parsed->includes) {
481 for (v = 0; v < LY_ARRAY_SIZE(mod->parsed->includes); ++v) {
482 if (mod->parsed->includes[v].submodule->latest_revision == 2) {
483 mod->parsed->includes[v].submodule->latest_revision = 1;
Radek Krejcie9e987e2018-10-31 12:50:27 +0100484 }
485 }
486 }
487 }
488}
489
Radek Krejci95710c92019-02-11 15:49:55 +0100490LY_ERR
491ly_ctx_module_implement_internal(struct ly_ctx *ctx, struct lys_module *mod, uint8_t value)
Radek Krejci0af46292019-01-11 16:02:31 +0100492{
493 struct lys_module *m;
494
495 LY_CHECK_ARG_RET(ctx, mod, LY_EINVAL);
496
497 if (mod->implemented) {
498 return LY_SUCCESS;
499 }
500
501 /* we have module from the current context */
502 m = ly_ctx_get_module_implemented(ctx, mod->name);
503 if (m) {
504 if (m != mod) {
505 /* check collision with other implemented revision */
506 LOGERR(ctx, LY_EDENIED, "Module \"%s\" is present in the context in other implemented revision (%s).",
507 mod->name, mod->revision ? mod->revision : "module without revision");
508 return LY_EDENIED;
509 } else {
510 /* mod is already implemented */
511 return LY_SUCCESS;
512 }
513 }
514
515 /* mark the module implemented, check for collision was already done */
Radek Krejci95710c92019-02-11 15:49:55 +0100516 mod->implemented = value;
Radek Krejci0af46292019-01-11 16:02:31 +0100517
518 /* compile the schema */
Radek Krejci95710c92019-02-11 15:49:55 +0100519 LY_CHECK_RET(lys_compile(mod, LYSC_OPT_INTERNAL));
Radek Krejci0af46292019-01-11 16:02:31 +0100520
521 return LY_SUCCESS;
522}
523
Radek Krejci95710c92019-02-11 15:49:55 +0100524API LY_ERR
525ly_ctx_module_implement(struct ly_ctx *ctx, struct lys_module *mod)
526{
527 return ly_ctx_module_implement_internal(ctx, mod, 1);
528}
529
Radek Krejcie9e987e2018-10-31 12:50:27 +0100530API void
Radek Krejciad573502018-09-07 15:26:55 +0200531ly_ctx_destroy(struct ly_ctx *ctx, void (*private_destructor)(const struct lysc_node *node, void *priv))
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200532{
Radek Krejci418823a2018-09-20 16:42:13 +0200533 if (!ctx) {
534 return;
535 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200536
537 /* models list */
Michal Vaskob34480a2018-09-17 10:34:45 +0200538 for (; ctx->list.count; ctx->list.count--) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200539 /* remove the module */
Radek Krejci86d106e2018-10-18 09:53:19 +0200540 lys_module_free(ctx->list.objs[ctx->list.count - 1], private_destructor);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200541 }
542 free(ctx->list.objs);
543
544 /* search paths list */
Radek Krejcia40f21b2018-09-18 10:42:08 +0200545 ly_set_erase(&ctx->search_paths, free);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200546
547 /* clean the error list */
548 ly_err_clean(ctx, 0);
549 pthread_key_delete(ctx->errlist_key);
550
551 /* dictionary */
552 lydict_clean(&ctx->dict);
553
554#if 0 /* TODO when plugins implemented */
555 /* plugins - will be removed only if this is the last context */
556 ly_clean_plugins();
557#endif
558
559 free(ctx);
560}