blob: 79d672a1ad464041e7c42e70345186e5d5262fa8 [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 Krejcie7b95092019-05-15 11:03:07 +020017#include <assert.h>
Radek Krejci0af5f5d2018-09-07 15:00:30 +020018#include <errno.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020019#include <pthread.h>
20#include <stddef.h>
Radek Krejci7ada9a02018-09-07 15:34:05 +020021#include <stdlib.h>
22#include <string.h>
Radek Krejci0e212402018-09-20 11:29:38 +020023#include <sys/stat.h>
Radek Krejci0af5f5d2018-09-07 15:00:30 +020024#include <unistd.h>
25
Radek Krejci0af5f5d2018-09-07 15:00:30 +020026#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020027#include "hash_table.h"
28#include "set.h"
29#include "tree.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020030#include "tree_schema_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020031#include "plugins_types.h"
Radek Krejci0af5f5d2018-09-07 15:00:30 +020032
Radek Krejci86d106e2018-10-18 09:53:19 +020033#define LY_INTERNAL_MODS_COUNT 6
Radek Krejci0af5f5d2018-09-07 15:00:30 +020034
Radek Krejcif3f47842018-11-15 11:22:15 +010035#include "../models/ietf-yang-metadata@2016-08-05.h"
36#include "../models/yang@2017-02-20.h"
37#include "../models/ietf-inet-types@2013-07-15.h"
38#include "../models/ietf-yang-types@2013-07-15.h"
39#include "../models/ietf-datastores@2017-08-17.h"
40#include "../models/ietf-yang-library@2018-01-17.h"
Radek Krejci0af5f5d2018-09-07 15:00:30 +020041#define IETF_YANG_LIB_REV "2018-01-17"
42
Radek Krejci0af5f5d2018-09-07 15:00:30 +020043static struct internal_modules_s {
44 const char *name;
45 const char *revision;
46 const char *data;
47 uint8_t implemented;
48 LYS_INFORMAT format;
49} internal_modules[LY_INTERNAL_MODS_COUNT] = {
Radek Krejci335332a2019-09-05 13:03:35 +020050 {"ietf-yang-metadata", "2016-08-05", (const char*)ietf_yang_metadata_2016_08_05_yang, 1, LYS_IN_YANG},
Radek Krejci86d106e2018-10-18 09:53:19 +020051 {"yang", "2017-02-20", (const char*)yang_2017_02_20_yang, 1, LYS_IN_YANG},
52 {"ietf-inet-types", "2013-07-15", (const char*)ietf_inet_types_2013_07_15_yang, 0, LYS_IN_YANG},
53 {"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 +020054 /* ietf-datastores and ietf-yang-library must be right here at the end of the list! */
Radek Krejci096235c2019-01-11 11:12:19 +010055 {"ietf-datastores", "2017-08-17", (const char*)ietf_datastores_2017_08_17_yang, 1, LYS_IN_YANG},
Radek Krejci86d106e2018-10-18 09:53:19 +020056 {"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 +020057};
58
59API LY_ERR
60ly_ctx_set_searchdir(struct ly_ctx *ctx, const char *search_dir)
61{
Radek Krejci0e212402018-09-20 11:29:38 +020062 struct stat st;
Radek Krejci0af5f5d2018-09-07 15:00:30 +020063 char *new_dir = NULL;
Radek Krejci0e212402018-09-20 11:29:38 +020064 unsigned int u;
Radek Krejci0af5f5d2018-09-07 15:00:30 +020065
66 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
67
68 if (search_dir) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +020069 new_dir = realpath(search_dir, NULL);
Radek Krejcia77e0e12018-09-20 12:39:15 +020070 LY_CHECK_ERR_RET(!new_dir,
Radek Krejci3a077b92019-04-09 17:10:35 +020071 LOGERR(ctx, LY_ESYS, "Unable to use search directory \"%s\" (%s).", search_dir, strerror(errno)),
72 LY_EINVAL);
73 if (strcmp(search_dir, new_dir)) {
74 LOGVRB("Canonicalizing search directory string from \"%s\" to \"%s\".", search_dir, new_dir);
75 }
76 LY_CHECK_ERR_RET(access(new_dir, R_OK | X_OK),
77 LOGERR(ctx, LY_ESYS, "Unable to fully access search directory \"%s\" (%s).", new_dir, strerror(errno)); free(new_dir),
78 LY_EINVAL);
79 LY_CHECK_ERR_RET(stat(new_dir, &st),
80 LOGERR(ctx, LY_ESYS, "stat() failed for \"%s\" (%s).", new_dir, strerror(errno)); free(new_dir),
Radek Krejcia77e0e12018-09-20 12:39:15 +020081 LY_ESYS);
Radek Krejci3a077b92019-04-09 17:10:35 +020082 LY_CHECK_ERR_RET(!S_ISDIR(st.st_mode),
83 LOGERR(ctx, LY_ESYS, "Given search directory \"%s\" is not a directory.", new_dir); free(new_dir),
84 LY_EINVAL);
Radek Krejci0e212402018-09-20 11:29:38 +020085 /* avoid path duplication */
86 for (u = 0; u < ctx->search_paths.count; ++u) {
Radek Krejci14946ab2018-09-20 13:42:06 +020087 if (!strcmp(new_dir, ctx->search_paths.objs[u])) {
Radek Krejci0e212402018-09-20 11:29:38 +020088 free(new_dir);
Radek Krejci14946ab2018-09-20 13:42:06 +020089 return LY_EEXIST;
Radek Krejci0e212402018-09-20 11:29:38 +020090 }
91 }
92 if (ly_set_add(&ctx->search_paths, new_dir, LY_SET_OPT_USEASLIST) == -1) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +020093 free(new_dir);
94 return LY_EMEM;
95 }
96
Radek Krejcie9e987e2018-10-31 12:50:27 +010097 /* new searchdir - possibly more latest revision available */
98 ly_ctx_reset_latests(ctx);
99
Radek Krejci0e212402018-09-20 11:29:38 +0200100 return LY_SUCCESS;
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200101 } else {
102 /* consider that no change is not actually an error */
103 return LY_SUCCESS;
104 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200105}
106
107API const char * const *
108ly_ctx_get_searchdirs(const struct ly_ctx *ctx)
109{
Radek Krejci05026432018-09-20 12:13:43 +0200110 void **new;
111
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200112 LY_CHECK_ARG_RET(ctx, ctx, NULL);
Radek Krejci05026432018-09-20 12:13:43 +0200113
114 if (ctx->search_paths.count == ctx->search_paths.size) {
115 /* not enough space for terminating NULL byte */
116 new = realloc(((struct ly_ctx *)ctx)->search_paths.objs, (ctx->search_paths.size + 8) * sizeof *ctx->search_paths.objs);
117 LY_CHECK_ERR_RET(!new, LOGMEM(NULL), NULL);
118 ((struct ly_ctx *)ctx)->search_paths.size += 8;
119 ((struct ly_ctx *)ctx)->search_paths.objs = new;
120 }
121 /* set terminating NULL byte to the strings list */
122 ctx->search_paths.objs[ctx->search_paths.count] = NULL;
123
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200124 return (const char * const *)ctx->search_paths.objs;
125}
126
127API LY_ERR
Radek Krejci0759b792018-09-20 13:53:15 +0200128ly_ctx_unset_searchdirs(struct ly_ctx *ctx, const char *value)
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200129{
Radek Krejci0759b792018-09-20 13:53:15 +0200130 unsigned int index;
131
Radek Krejcicd649072018-09-20 12:14:45 +0200132 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
Radek Krejcicd649072018-09-20 12:14:45 +0200133
Michal Vaskob34480a2018-09-17 10:34:45 +0200134 if (!ctx->search_paths.count) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200135 return LY_SUCCESS;
136 }
137
Radek Krejci0759b792018-09-20 13:53:15 +0200138 if (value) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200139 /* remove specific search directory */
Radek Krejci0759b792018-09-20 13:53:15 +0200140 for (index = 0; index < ctx->search_paths.count; ++index) {
141 if (!strcmp(value, ctx->search_paths.objs[index])) {
142 break;
143 }
144 }
145 if (index == ctx->search_paths.count) {
146 LOGARG(ctx, value);
147 return LY_EINVAL;
148 } else {
149 return ly_set_rm_index(&ctx->search_paths, index, free);
150 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200151 } else {
152 /* remove them all */
Radek Krejcia40f21b2018-09-18 10:42:08 +0200153 ly_set_erase(&ctx->search_paths, free);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200154 memset(&ctx->search_paths, 0, sizeof ctx->search_paths);
155 }
156
157 return LY_SUCCESS;
158}
159
160API LY_ERR
Radek Krejcied5acc52019-04-25 15:57:04 +0200161ly_ctx_unset_searchdir(struct ly_ctx *ctx, unsigned int index)
162{
163 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
164
165 if (!ctx->search_paths.count) {
166 return LY_SUCCESS;
167 }
168
169 if (index >= ctx->search_paths.count) {
170 LOGARG(ctx, value);
171 return LY_EINVAL;
172 } else {
173 return ly_set_rm_index(&ctx->search_paths, index, free);
174 }
175
176 return LY_SUCCESS;
177}
178
179API const struct lys_module *
180ly_ctx_load_module(struct ly_ctx *ctx, const char *name, const char *revision)
181{
182 struct lys_module *result = NULL;
183
184 LY_CHECK_ARG_RET(ctx, ctx, name, NULL);
185
186 LY_CHECK_RET(lysp_load_module(ctx, name, revision, 1, 0, &result), NULL);
187 return result;
188}
189
190API LY_ERR
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200191ly_ctx_new(const char *search_dir, int options, struct ly_ctx **new_ctx)
192{
193 struct ly_ctx *ctx = NULL;
194 struct lys_module *module;
195 char *search_dir_list;
196 char *sep, *dir;
Radek Krejci86d106e2018-10-18 09:53:19 +0200197 int i;
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200198 LY_ERR rc = LY_SUCCESS;
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200199
Radek Krejcied5acc52019-04-25 15:57:04 +0200200 LY_CHECK_ARG_RET(NULL, new_ctx, LY_EINVAL);
201
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200202 ctx = calloc(1, sizeof *ctx);
Radek Krejciad573502018-09-07 15:26:55 +0200203 LY_CHECK_ERR_RET(!ctx, LOGMEM(NULL), LY_EMEM);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200204
205 /* dictionary */
206 lydict_init(&ctx->dict);
207
Radek Krejciad573502018-09-07 15:26:55 +0200208#if 0 /* TODO when plugins implemented */
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200209 /* plugins */
210 ly_load_plugins();
Radek Krejciad573502018-09-07 15:26:55 +0200211#endif
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200212
213 /* initialize thread-specific key */
Radek Krejciad573502018-09-07 15:26:55 +0200214 while ((pthread_key_create(&ctx->errlist_key, ly_err_free)) == EAGAIN);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200215
216 /* models list */
217 ctx->flags = options;
218 if (search_dir) {
219 search_dir_list = strdup(search_dir);
220 LY_CHECK_ERR_GOTO(!search_dir_list, LOGMEM(NULL); rc = LY_EMEM, error);
221
222 for (dir = search_dir_list; (sep = strchr(dir, ':')) != NULL && rc == LY_SUCCESS; dir = sep + 1) {
223 *sep = 0;
224 rc = ly_ctx_set_searchdir(ctx, dir);
Radek Krejci14946ab2018-09-20 13:42:06 +0200225 if (rc == LY_EEXIST) {
226 /* ignore duplication */
227 rc = LY_SUCCESS;
228 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200229 }
230 if (*dir && rc == LY_SUCCESS) {
231 rc = ly_ctx_set_searchdir(ctx, dir);
Radek Krejci14946ab2018-09-20 13:42:06 +0200232 if (rc == LY_EEXIST) {
233 /* ignore duplication */
234 rc = LY_SUCCESS;
235 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200236 }
237 free(search_dir_list);
238
239 /* If ly_ctx_set_searchdir() failed, the error is already logged. Just exit */
240 if (rc != LY_SUCCESS) {
241 goto error;
242 }
243 }
244 ctx->module_set_id = 1;
245
246 /* load internal modules */
Radek Krejci86d106e2018-10-18 09:53:19 +0200247 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 +0100248 module = (struct lys_module *)lys_parse_mem_module(ctx, internal_modules[i].data, internal_modules[i].format,
249 internal_modules[i].implemented, NULL, NULL);
Radek Krejci0e75e6f2018-11-08 09:40:02 +0100250 LY_CHECK_ERR_GOTO(!module, rc = ly_errcode(ctx), error);
Radek Krejci096235c2019-01-11 11:12:19 +0100251 LY_CHECK_GOTO((rc = lys_compile(module, 0)), error);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200252 }
253
254 *new_ctx = ctx;
255 return rc;
256
257error:
258 ly_ctx_destroy(ctx, NULL);
259 return rc;
260}
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200261API int
Radek Krejci3fbe89a2018-09-20 13:54:46 +0200262ly_ctx_get_options(const struct ly_ctx *ctx)
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200263{
Radek Krejci3fbe89a2018-09-20 13:54:46 +0200264 LY_CHECK_ARG_RET(ctx, ctx, 0);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200265 return ctx->flags;
266}
267
Radek Krejcica828d92018-09-20 14:19:43 +0200268API LY_ERR
Radek Krejci3fa46b62019-09-11 10:47:30 +0200269ly_ctx_set_options(struct ly_ctx *ctx, int option)
Radek Krejcica828d92018-09-20 14:19:43 +0200270{
271 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
272 LY_CHECK_ERR_RET(option & LY_CTX_NOYANGLIBRARY, LOGARG(ctx, option), LY_EINVAL);
273
274 /* set the option(s) */
275 ctx->flags |= option;
276
277 return LY_SUCCESS;
278}
279
280API LY_ERR
Radek Krejci3fa46b62019-09-11 10:47:30 +0200281ly_ctx_unset_options(struct ly_ctx *ctx, int option)
Radek Krejcica828d92018-09-20 14:19:43 +0200282{
283 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
284 LY_CHECK_ERR_RET(option & LY_CTX_NOYANGLIBRARY, LOGARG(ctx, option), LY_EINVAL);
285
286 /* unset the option(s) */
287 ctx->flags &= ~option;
288
289 return LY_SUCCESS;
290}
291
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200292API uint16_t
293ly_ctx_get_module_set_id(const struct ly_ctx *ctx)
294{
Radek Krejci3fbe89a2018-09-20 13:54:46 +0200295 LY_CHECK_ARG_RET(ctx, ctx, 0);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200296 return ctx->module_set_id;
297}
298
Radek Krejcid33273d2018-10-25 14:55:52 +0200299API void
300ly_ctx_set_module_imp_clb(struct ly_ctx *ctx, ly_module_imp_clb clb, void *user_data)
301{
302 LY_CHECK_ARG_RET(ctx, ctx,);
303
304 ctx->imp_clb = clb;
305 ctx->imp_clb_data = user_data;
306}
307
308API ly_module_imp_clb
309ly_ctx_get_module_imp_clb(const struct ly_ctx *ctx, void **user_data)
310{
311 LY_CHECK_ARG_RET(ctx, ctx, NULL);
312
313 if (user_data) {
314 *user_data = ctx->imp_clb_data;
315 }
316 return ctx->imp_clb;
317}
318
Radek Krejcied5acc52019-04-25 15:57:04 +0200319API const struct lys_module *
320ly_ctx_get_module_iter(const struct ly_ctx *ctx, unsigned int *index)
321{
322 LY_CHECK_ARG_RET(ctx, ctx, index, NULL);
323
Radek Krejci2390fb52019-04-30 13:27:43 +0200324 if (*index < (unsigned)ctx->list.count) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200325 return ctx->list.objs[(*index)++];
Radek Krejci2390fb52019-04-30 13:27:43 +0200326 } else {
327 return NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200328 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200329}
330
Radek Krejcib7db73a2018-10-24 14:18:40 +0200331/**
332 * @brief Iterate over the modules in the given context. Returned modules must match the given key at the offset of
333 * lysp_module and lysc_module structures (they are supposed to be placed at the same offset in both structures).
334 *
335 * @param[in] ctx Context where to iterate.
336 * @param[in] key Key value to search for.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100337 * @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 +0200338 * @param[in,out] Iterator to pass between the function calls. On the first call, the variable is supposed to be
339 * initiated to 0. After each call returning a module, the value is greater by 1 than the index of the returned
340 * module in the context.
341 * @return Module matching the given key, NULL if no such module found.
342 */
Radek Krejci0af46292019-01-11 16:02:31 +0100343static struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200344ly_ctx_get_module_by_iter(const struct ly_ctx *ctx, const char *key, size_t key_offset, unsigned int *index)
345{
Radek Krejci0af46292019-01-11 16:02:31 +0100346 struct lys_module *mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200347 const char *value;
348
349 for (; *index < ctx->list.count; ++(*index)) {
350 mod = ctx->list.objs[*index];
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100351 value = *(const char**)(((int8_t*)(mod)) + key_offset);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200352 if (!strcmp(key, value)) {
353 /* increment index for the next run */
354 ++(*index);
355 return mod;
356 }
357 }
358 /* done */
359 return NULL;
360}
361
362/**
363 * @brief Unifying function for ly_ctx_get_module() and ly_ctx_get_module_ns()
364 * @param[in] ctx Context where to search.
365 * @param[in] key Name or Namespace as a search key.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100366 * @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 +0200367 * @param[in] revision Revision date to match. If NULL, the matching module must have no revision. To search for the latest
368 * revision module, use ly_ctx_get_module_latest_by().
369 * @return Matching module if any.
370 */
Radek Krejci0af46292019-01-11 16:02:31 +0100371static struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200372ly_ctx_get_module_by(const struct ly_ctx *ctx, const char *key, size_t key_offset, const char *revision)
373{
Radek Krejci0af46292019-01-11 16:02:31 +0100374 struct lys_module *mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200375 unsigned int index = 0;
376
377 while ((mod = ly_ctx_get_module_by_iter(ctx, key, key_offset, &index))) {
378 if (!revision) {
Radek Krejci0af46292019-01-11 16:02:31 +0100379 if (!mod->revision) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200380 /* found requested module without revision */
381 return mod;
382 }
383 } else {
Radek Krejci0af46292019-01-11 16:02:31 +0100384 if (mod->revision && !strcmp(mod->revision, revision)) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200385 /* found requested module of the specific revision */
386 return mod;
387 }
388 }
389 }
390
391 return NULL;
392}
393
Radek Krejci0af46292019-01-11 16:02:31 +0100394API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200395ly_ctx_get_module_ns(const struct ly_ctx *ctx, const char *ns, const char *revision)
396{
397 LY_CHECK_ARG_RET(ctx, ctx, ns, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100398 return ly_ctx_get_module_by(ctx, ns, offsetof(struct lys_module, ns), revision);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200399}
400
Radek Krejci0af46292019-01-11 16:02:31 +0100401API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200402ly_ctx_get_module(const struct ly_ctx *ctx, const char *name, const char *revision)
403{
404 LY_CHECK_ARG_RET(ctx, ctx, name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100405 return ly_ctx_get_module_by(ctx, name, offsetof(struct lys_module, name), revision);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200406}
407
408/**
409 * @brief Unifying function for ly_ctx_get_module_latest() and ly_ctx_get_module_latest_ns()
410 * @param[in] ctx Context where to search.
411 * @param[in] key Name or Namespace as a search key.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100412 * @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 +0200413 * @return Matching module if any.
414 */
Radek Krejci0af46292019-01-11 16:02:31 +0100415static struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200416ly_ctx_get_module_latest_by(const struct ly_ctx *ctx, const char *key, size_t key_offset)
417{
Radek Krejci0af46292019-01-11 16:02:31 +0100418 struct lys_module *mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200419 unsigned int index = 0;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200420
421 while ((mod = ly_ctx_get_module_by_iter(ctx, key, key_offset, &index))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100422 if (mod->latest_revision) {
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200423 return mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200424 }
425 }
426
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200427 return NULL;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200428}
429
Radek Krejci0af46292019-01-11 16:02:31 +0100430API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200431ly_ctx_get_module_latest(const struct ly_ctx *ctx, const char *name)
432{
433 LY_CHECK_ARG_RET(ctx, ctx, name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100434 return ly_ctx_get_module_latest_by(ctx, name, offsetof(struct lys_module, name));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200435}
436
Radek Krejci0af46292019-01-11 16:02:31 +0100437API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200438ly_ctx_get_module_latest_ns(const struct ly_ctx *ctx, const char *ns)
439{
440 LY_CHECK_ARG_RET(ctx, ctx, ns, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100441 return ly_ctx_get_module_latest_by(ctx, ns, offsetof(struct lys_module, ns));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200442}
443
444/**
445 * @brief Unifying function for ly_ctx_get_module_implemented() and ly_ctx_get_module_implemented_ns()
446 * @param[in] ctx Context where to search.
447 * @param[in] key Name or Namespace as a search key.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100448 * @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 +0200449 * @return Matching module if any.
450 */
Radek Krejci0af46292019-01-11 16:02:31 +0100451static struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200452ly_ctx_get_module_implemented_by(const struct ly_ctx *ctx, const char *key, size_t key_offset)
453{
Radek Krejci0af46292019-01-11 16:02:31 +0100454 struct lys_module *mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200455 unsigned int index = 0;
456
457 while ((mod = ly_ctx_get_module_by_iter(ctx, key, key_offset, &index))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100458 if (mod->implemented) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200459 return mod;
460 }
461 }
462
463 return NULL;
464}
465
Radek Krejci0af46292019-01-11 16:02:31 +0100466API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200467ly_ctx_get_module_implemented(const struct ly_ctx *ctx, const char *name)
468{
469 LY_CHECK_ARG_RET(ctx, ctx, name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100470 return ly_ctx_get_module_implemented_by(ctx, name, offsetof(struct lys_module, name));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200471}
472
Radek Krejci0af46292019-01-11 16:02:31 +0100473API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200474ly_ctx_get_module_implemented_ns(const struct ly_ctx *ctx, const char *ns)
475{
476 LY_CHECK_ARG_RET(ctx, ctx, ns, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100477 return ly_ctx_get_module_implemented_by(ctx, ns, offsetof(struct lys_module, ns));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200478}
479
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100480struct lysp_submodule *
Radek Krejcid33273d2018-10-25 14:55:52 +0200481ly_ctx_get_submodule(const struct ly_ctx *ctx, const char *module, const char *submodule, const char *revision)
482{
483 const struct lys_module *mod;
484 struct lysp_include *inc;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200485 uint32_t v;
486 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid33273d2018-10-25 14:55:52 +0200487
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100488 assert(submodule);
489
490 for (v = 0; v < ctx->list.count; ++v) {
491 mod = ctx->list.objs[v];
Radek Krejcid33273d2018-10-25 14:55:52 +0200492 if (!mod->parsed) {
493 continue;
494 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100495 if (module && strcmp(module, mod->name)) {
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100496 continue;
497 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200498
499 LY_ARRAY_FOR(mod->parsed->includes, u) {
Radek Krejci086c7132018-10-26 15:29:04 +0200500 if (mod->parsed->includes[u].submodule && !strcmp(submodule, mod->parsed->includes[u].submodule->name)) {
Radek Krejcid33273d2018-10-25 14:55:52 +0200501 inc = &mod->parsed->includes[u];
502 if (!revision) {
503 if (inc->submodule->latest_revision) {
504 return inc->submodule;
505 }
506 } else if (inc->submodule->revs && !strcmp(revision, inc->submodule->revs[0].date)) {
507 return inc->submodule;
508 }
509 }
510 }
511 }
512
513 return NULL;
514}
515
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200516API void
Radek Krejcie9e987e2018-10-31 12:50:27 +0100517ly_ctx_reset_latests(struct ly_ctx *ctx)
518{
Radek Krejcie9e987e2018-10-31 12:50:27 +0100519 struct lys_module *mod;
520
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200521 for (uint32_t v = 0; v < ctx->list.count; ++v) {
522 mod = ctx->list.objs[v];
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100523 if (mod->latest_revision == 2) {
524 mod->latest_revision = 1;
Radek Krejcie9e987e2018-10-31 12:50:27 +0100525 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100526 if (mod->parsed && mod->parsed->includes) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200527 for (LY_ARRAY_SIZE_TYPE u = 0; u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) {
528 if (mod->parsed->includes[u].submodule->latest_revision == 2) {
529 mod->parsed->includes[u].submodule->latest_revision = 1;
Radek Krejcie9e987e2018-10-31 12:50:27 +0100530 }
531 }
532 }
533 }
534}
535
536API void
Radek Krejciad573502018-09-07 15:26:55 +0200537ly_ctx_destroy(struct ly_ctx *ctx, void (*private_destructor)(const struct lysc_node *node, void *priv))
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200538{
Radek Krejci418823a2018-09-20 16:42:13 +0200539 if (!ctx) {
540 return;
541 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200542
543 /* models list */
Michal Vaskob34480a2018-09-17 10:34:45 +0200544 for (; ctx->list.count; ctx->list.count--) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200545 /* remove the module */
Radek Krejci86d106e2018-10-18 09:53:19 +0200546 lys_module_free(ctx->list.objs[ctx->list.count - 1], private_destructor);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200547 }
548 free(ctx->list.objs);
549
550 /* search paths list */
Radek Krejcia40f21b2018-09-18 10:42:08 +0200551 ly_set_erase(&ctx->search_paths, free);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200552
553 /* clean the error list */
554 ly_err_clean(ctx, 0);
555 pthread_key_delete(ctx->errlist_key);
556
557 /* dictionary */
558 lydict_clean(&ctx->dict);
559
560#if 0 /* TODO when plugins implemented */
561 /* plugins - will be removed only if this is the last context */
562 ly_clean_plugins();
563#endif
564
565 free(ctx);
566}