blob: f92c5276163f0f1a9f0a3dc32d304e1da4dd00d2 [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 Krejci7ada9a02018-09-07 15:34:05 +020017#define _BSD_SOURCE
Radek Krejci0af5f5d2018-09-07 15:00:30 +020018#include <errno.h>
Radek Krejci7ada9a02018-09-07 15:34:05 +020019#include <limits.h>
20#include <stdlib.h>
21#include <string.h>
Radek Krejci0e212402018-09-20 11:29:38 +020022#include <sys/stat.h>
23#include <sys/types.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 Krejci86d106e2018-10-18 09:53:19 +020027#include "tree_schema_internal.h"
Radek Krejciad573502018-09-07 15:26:55 +020028#include "libyang.h"
Radek Krejci0af5f5d2018-09-07 15:00:30 +020029
Radek Krejci86d106e2018-10-18 09:53:19 +020030#define LY_INTERNAL_MODS_COUNT 6
Radek Krejci0af5f5d2018-09-07 15:00:30 +020031
Radek Krejcif3f47842018-11-15 11:22:15 +010032#include "../models/ietf-yang-metadata@2016-08-05.h"
33#include "../models/yang@2017-02-20.h"
34#include "../models/ietf-inet-types@2013-07-15.h"
35#include "../models/ietf-yang-types@2013-07-15.h"
36#include "../models/ietf-datastores@2017-08-17.h"
37#include "../models/ietf-yang-library@2018-01-17.h"
Radek Krejci0af5f5d2018-09-07 15:00:30 +020038#define IETF_YANG_LIB_REV "2018-01-17"
39
Radek Krejci0af5f5d2018-09-07 15:00:30 +020040static struct internal_modules_s {
41 const char *name;
42 const char *revision;
43 const char *data;
44 uint8_t implemented;
45 LYS_INFORMAT format;
46} internal_modules[LY_INTERNAL_MODS_COUNT] = {
Radek Krejci86d106e2018-10-18 09:53:19 +020047 {"ietf-yang-metadata", "2016-08-05", (const char*)ietf_yang_metadata_2016_08_05_yang, 0, LYS_IN_YANG},
48 {"yang", "2017-02-20", (const char*)yang_2017_02_20_yang, 1, LYS_IN_YANG},
49 {"ietf-inet-types", "2013-07-15", (const char*)ietf_inet_types_2013_07_15_yang, 0, LYS_IN_YANG},
50 {"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 +020051 /* ietf-datastores and ietf-yang-library must be right here at the end of the list! */
Radek Krejci86d106e2018-10-18 09:53:19 +020052 {"ietf-datastores", "2017-08-17", (const char*)ietf_datastores_2017_08_17_yang, 0, LYS_IN_YANG},
53 {"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 +020054};
55
56API LY_ERR
57ly_ctx_set_searchdir(struct ly_ctx *ctx, const char *search_dir)
58{
Radek Krejci0e212402018-09-20 11:29:38 +020059 struct stat st;
Radek Krejci0af5f5d2018-09-07 15:00:30 +020060 char *new_dir = NULL;
Radek Krejci0e212402018-09-20 11:29:38 +020061 unsigned int u;
Radek Krejci0af5f5d2018-09-07 15:00:30 +020062
63 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
64
65 if (search_dir) {
Radek Krejci0e212402018-09-20 11:29:38 +020066 LY_CHECK_ERR_RET(access(search_dir, R_OK | X_OK),
67 LOGERR(ctx, LY_ESYS, "Unable to use search directory \"%s\" (%s)", search_dir, strerror(errno)),
68 LY_EINVAL);
69 LY_CHECK_ERR_RET(stat(search_dir, &st),
70 LOGERR(ctx, LY_ESYS, "stat() failed for \"%s\" (%s)", search_dir, strerror(errno)),
71 LY_ESYS);
72 LY_CHECK_ERR_RET(!S_ISDIR(st.st_mode),
73 LOGERR(ctx, LY_ESYS, "Given search directory \"%s\" is not a directory.", search_dir),
74 LY_EINVAL);
Radek Krejci0af5f5d2018-09-07 15:00:30 +020075 new_dir = realpath(search_dir, NULL);
Radek Krejcia77e0e12018-09-20 12:39:15 +020076 LY_CHECK_ERR_RET(!new_dir,
77 LOGERR(ctx, LY_ESYS, "realpath() call failed for \"%s\" (%s).", search_dir, strerror(errno)),
78 LY_ESYS);
Radek Krejci0e212402018-09-20 11:29:38 +020079 /* avoid path duplication */
80 for (u = 0; u < ctx->search_paths.count; ++u) {
Radek Krejci14946ab2018-09-20 13:42:06 +020081 if (!strcmp(new_dir, ctx->search_paths.objs[u])) {
Radek Krejci0e212402018-09-20 11:29:38 +020082 free(new_dir);
Radek Krejci14946ab2018-09-20 13:42:06 +020083 return LY_EEXIST;
Radek Krejci0e212402018-09-20 11:29:38 +020084 }
85 }
86 if (ly_set_add(&ctx->search_paths, new_dir, LY_SET_OPT_USEASLIST) == -1) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +020087 free(new_dir);
88 return LY_EMEM;
89 }
90
Radek Krejcie9e987e2018-10-31 12:50:27 +010091 /* new searchdir - possibly more latest revision available */
92 ly_ctx_reset_latests(ctx);
93
Radek Krejci0e212402018-09-20 11:29:38 +020094 return LY_SUCCESS;
Radek Krejci0af5f5d2018-09-07 15:00:30 +020095 } else {
96 /* consider that no change is not actually an error */
97 return LY_SUCCESS;
98 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +020099}
100
101API const char * const *
102ly_ctx_get_searchdirs(const struct ly_ctx *ctx)
103{
Radek Krejci05026432018-09-20 12:13:43 +0200104 void **new;
105
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200106 LY_CHECK_ARG_RET(ctx, ctx, NULL);
Radek Krejci05026432018-09-20 12:13:43 +0200107
108 if (ctx->search_paths.count == ctx->search_paths.size) {
109 /* not enough space for terminating NULL byte */
110 new = realloc(((struct ly_ctx *)ctx)->search_paths.objs, (ctx->search_paths.size + 8) * sizeof *ctx->search_paths.objs);
111 LY_CHECK_ERR_RET(!new, LOGMEM(NULL), NULL);
112 ((struct ly_ctx *)ctx)->search_paths.size += 8;
113 ((struct ly_ctx *)ctx)->search_paths.objs = new;
114 }
115 /* set terminating NULL byte to the strings list */
116 ctx->search_paths.objs[ctx->search_paths.count] = NULL;
117
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200118 return (const char * const *)ctx->search_paths.objs;
119}
120
121API LY_ERR
Radek Krejci0759b792018-09-20 13:53:15 +0200122ly_ctx_unset_searchdirs(struct ly_ctx *ctx, const char *value)
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200123{
Radek Krejci0759b792018-09-20 13:53:15 +0200124 unsigned int index;
125
Radek Krejcicd649072018-09-20 12:14:45 +0200126 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
Radek Krejcicd649072018-09-20 12:14:45 +0200127
Michal Vaskob34480a2018-09-17 10:34:45 +0200128 if (!ctx->search_paths.count) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200129 return LY_SUCCESS;
130 }
131
Radek Krejci0759b792018-09-20 13:53:15 +0200132 if (value) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200133 /* remove specific search directory */
Radek Krejci0759b792018-09-20 13:53:15 +0200134 for (index = 0; index < ctx->search_paths.count; ++index) {
135 if (!strcmp(value, ctx->search_paths.objs[index])) {
136 break;
137 }
138 }
139 if (index == ctx->search_paths.count) {
140 LOGARG(ctx, value);
141 return LY_EINVAL;
142 } else {
143 return ly_set_rm_index(&ctx->search_paths, index, free);
144 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200145 } else {
146 /* remove them all */
Radek Krejcia40f21b2018-09-18 10:42:08 +0200147 ly_set_erase(&ctx->search_paths, free);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200148 memset(&ctx->search_paths, 0, sizeof ctx->search_paths);
149 }
150
151 return LY_SUCCESS;
152}
153
154API LY_ERR
155ly_ctx_new(const char *search_dir, int options, struct ly_ctx **new_ctx)
156{
157 struct ly_ctx *ctx = NULL;
158 struct lys_module *module;
159 char *search_dir_list;
160 char *sep, *dir;
Radek Krejci86d106e2018-10-18 09:53:19 +0200161 int i;
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200162 LY_ERR rc = LY_SUCCESS;
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200163
164 ctx = calloc(1, sizeof *ctx);
Radek Krejciad573502018-09-07 15:26:55 +0200165 LY_CHECK_ERR_RET(!ctx, LOGMEM(NULL), LY_EMEM);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200166
167 /* dictionary */
168 lydict_init(&ctx->dict);
169
Radek Krejciad573502018-09-07 15:26:55 +0200170#if 0 /* TODO when plugins implemented */
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200171 /* plugins */
172 ly_load_plugins();
Radek Krejciad573502018-09-07 15:26:55 +0200173#endif
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200174
175 /* initialize thread-specific key */
Radek Krejciad573502018-09-07 15:26:55 +0200176 while ((pthread_key_create(&ctx->errlist_key, ly_err_free)) == EAGAIN);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200177
178 /* models list */
179 ctx->flags = options;
180 if (search_dir) {
181 search_dir_list = strdup(search_dir);
182 LY_CHECK_ERR_GOTO(!search_dir_list, LOGMEM(NULL); rc = LY_EMEM, error);
183
184 for (dir = search_dir_list; (sep = strchr(dir, ':')) != NULL && rc == LY_SUCCESS; dir = sep + 1) {
185 *sep = 0;
186 rc = ly_ctx_set_searchdir(ctx, dir);
Radek Krejci14946ab2018-09-20 13:42:06 +0200187 if (rc == LY_EEXIST) {
188 /* ignore duplication */
189 rc = LY_SUCCESS;
190 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200191 }
192 if (*dir && rc == LY_SUCCESS) {
193 rc = ly_ctx_set_searchdir(ctx, dir);
Radek Krejci14946ab2018-09-20 13:42:06 +0200194 if (rc == LY_EEXIST) {
195 /* ignore duplication */
196 rc = LY_SUCCESS;
197 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200198 }
199 free(search_dir_list);
200
201 /* If ly_ctx_set_searchdir() failed, the error is already logged. Just exit */
202 if (rc != LY_SUCCESS) {
203 goto error;
204 }
205 }
206 ctx->module_set_id = 1;
207
208 /* load internal modules */
Radek Krejci86d106e2018-10-18 09:53:19 +0200209 for (i = 0; i < ((options & LY_CTX_NOYANGLIBRARY) ? (LY_INTERNAL_MODS_COUNT - 2) : LY_INTERNAL_MODS_COUNT); i++) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200210 module = (struct lys_module *)lys_parse_mem(ctx, internal_modules[i].data, internal_modules[i].format);
Radek Krejci0e75e6f2018-11-08 09:40:02 +0100211 LY_CHECK_ERR_GOTO(!module, rc = ly_errcode(ctx), error);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200212 module->parsed->implemented = internal_modules[i].implemented;
213 }
214
215 *new_ctx = ctx;
216 return rc;
217
218error:
219 ly_ctx_destroy(ctx, NULL);
220 return rc;
221}
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200222API int
Radek Krejci3fbe89a2018-09-20 13:54:46 +0200223ly_ctx_get_options(const struct ly_ctx *ctx)
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200224{
Radek Krejci3fbe89a2018-09-20 13:54:46 +0200225 LY_CHECK_ARG_RET(ctx, ctx, 0);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200226 return ctx->flags;
227}
228
Radek Krejcica828d92018-09-20 14:19:43 +0200229API LY_ERR
230ly_ctx_set_option(struct ly_ctx *ctx, int option)
231{
232 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
233 LY_CHECK_ERR_RET(option & LY_CTX_NOYANGLIBRARY, LOGARG(ctx, option), LY_EINVAL);
234
235 /* set the option(s) */
236 ctx->flags |= option;
237
238 return LY_SUCCESS;
239}
240
241API LY_ERR
242ly_ctx_unset_option(struct ly_ctx *ctx, int option)
243{
244 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
245 LY_CHECK_ERR_RET(option & LY_CTX_NOYANGLIBRARY, LOGARG(ctx, option), LY_EINVAL);
246
247 /* unset the option(s) */
248 ctx->flags &= ~option;
249
250 return LY_SUCCESS;
251}
252
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200253API uint16_t
254ly_ctx_get_module_set_id(const struct ly_ctx *ctx)
255{
Radek Krejci3fbe89a2018-09-20 13:54:46 +0200256 LY_CHECK_ARG_RET(ctx, ctx, 0);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200257 return ctx->module_set_id;
258}
259
Radek Krejcid33273d2018-10-25 14:55:52 +0200260API void
261ly_ctx_set_module_imp_clb(struct ly_ctx *ctx, ly_module_imp_clb clb, void *user_data)
262{
263 LY_CHECK_ARG_RET(ctx, ctx,);
264
265 ctx->imp_clb = clb;
266 ctx->imp_clb_data = user_data;
267}
268
269API ly_module_imp_clb
270ly_ctx_get_module_imp_clb(const struct ly_ctx *ctx, void **user_data)
271{
272 LY_CHECK_ARG_RET(ctx, ctx, NULL);
273
274 if (user_data) {
275 *user_data = ctx->imp_clb_data;
276 }
277 return ctx->imp_clb;
278}
279
Radek Krejcib7db73a2018-10-24 14:18:40 +0200280/**
281 * @brief Iterate over the modules in the given context. Returned modules must match the given key at the offset of
282 * lysp_module and lysc_module structures (they are supposed to be placed at the same offset in both structures).
283 *
284 * @param[in] ctx Context where to iterate.
285 * @param[in] key Key value to search for.
286 * @param[in] key_offset Key's offset in struct lysp_module and struct lysc_module to get value from the context's
287 * modules to match with the key.
288 * @param[in,out] Iterator to pass between the function calls. On the first call, the variable is supposed to be
289 * initiated to 0. After each call returning a module, the value is greater by 1 than the index of the returned
290 * module in the context.
291 * @return Module matching the given key, NULL if no such module found.
292 */
293static const struct lys_module *
294ly_ctx_get_module_by_iter(const struct ly_ctx *ctx, const char *key, size_t key_offset, unsigned int *index)
295{
296 const struct lys_module *mod;
297 const char *value;
298
299 for (; *index < ctx->list.count; ++(*index)) {
300 mod = ctx->list.objs[*index];
301 if (mod->compiled) {
302 value = *(const char**)(((int8_t*)(mod->compiled)) + key_offset);
303 } else {
304 value = *(const char**)(((int8_t*)(mod->parsed)) + key_offset);
305 }
306 if (!strcmp(key, value)) {
307 /* increment index for the next run */
308 ++(*index);
309 return mod;
310 }
311 }
312 /* done */
313 return NULL;
314}
315
316/**
317 * @brief Unifying function for ly_ctx_get_module() and ly_ctx_get_module_ns()
318 * @param[in] ctx Context where to search.
319 * @param[in] key Name or Namespace as a search key.
320 * @param[in] key_offset Key's offset in struct lysp_module to get value from the context's modules to match with the key.
321 * @param[in] revision Revision date to match. If NULL, the matching module must have no revision. To search for the latest
322 * revision module, use ly_ctx_get_module_latest_by().
323 * @return Matching module if any.
324 */
325static const struct lys_module *
326ly_ctx_get_module_by(const struct ly_ctx *ctx, const char *key, size_t key_offset, const char *revision)
327{
328 const struct lys_module *mod;
329 unsigned int index = 0;
330
331 while ((mod = ly_ctx_get_module_by_iter(ctx, key, key_offset, &index))) {
332 if (!revision) {
Radek Krejcif8f882a2018-10-31 14:51:15 +0100333 if ((mod->compiled && !mod->compiled->revision) || (!mod->compiled && !mod->parsed->revs)) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200334 /* found requested module without revision */
335 return mod;
336 }
337 } else {
Radek Krejcif8f882a2018-10-31 14:51:15 +0100338 if ((mod->compiled && mod->compiled->revision && !strcmp(mod->compiled->revision, revision)) ||
Radek Krejcib7db73a2018-10-24 14:18:40 +0200339 (!mod->compiled && mod->parsed->revs && !strcmp(mod->parsed->revs[0].date, revision))) {
340 /* found requested module of the specific revision */
341 return mod;
342 }
343 }
344 }
345
346 return NULL;
347}
348
349API const struct lys_module *
350ly_ctx_get_module_ns(const struct ly_ctx *ctx, const char *ns, const char *revision)
351{
352 LY_CHECK_ARG_RET(ctx, ctx, ns, NULL);
353 return ly_ctx_get_module_by(ctx, ns, offsetof(struct lysp_module, ns), revision);
354}
355
356API const struct lys_module *
357ly_ctx_get_module(const struct ly_ctx *ctx, const char *name, const char *revision)
358{
359 LY_CHECK_ARG_RET(ctx, ctx, name, NULL);
360 return ly_ctx_get_module_by(ctx, name, offsetof(struct lysp_module, name), revision);
361}
362
363/**
364 * @brief Unifying function for ly_ctx_get_module_latest() and ly_ctx_get_module_latest_ns()
365 * @param[in] ctx Context where to search.
366 * @param[in] key Name or Namespace as a search key.
367 * @param[in] key_offset Key's offset in struct lysp_module to get value from the context's modules to match with the key.
368 * @return Matching module if any.
369 */
370static const struct lys_module *
371ly_ctx_get_module_latest_by(const struct ly_ctx *ctx, const char *key, size_t key_offset)
372{
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200373 const struct lys_module *mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200374 unsigned int index = 0;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200375
376 while ((mod = ly_ctx_get_module_by_iter(ctx, key, key_offset, &index))) {
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200377 if ((mod->compiled && mod->compiled->latest_revision) || (!mod->compiled && mod->parsed->latest_revision)) {
378 return mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200379 }
380 }
381
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200382 return NULL;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200383}
384
385API const struct lys_module *
386ly_ctx_get_module_latest(const struct ly_ctx *ctx, const char *name)
387{
388 LY_CHECK_ARG_RET(ctx, ctx, name, NULL);
389 return ly_ctx_get_module_latest_by(ctx, name, offsetof(struct lysp_module, name));
390}
391
392const struct lys_module *
393ly_ctx_get_module_latest_ns(const struct ly_ctx *ctx, const char *ns)
394{
395 LY_CHECK_ARG_RET(ctx, ctx, ns, NULL);
396 return ly_ctx_get_module_latest_by(ctx, ns, offsetof(struct lysp_module, ns));
397}
398
399/**
400 * @brief Unifying function for ly_ctx_get_module_implemented() and ly_ctx_get_module_implemented_ns()
401 * @param[in] ctx Context where to search.
402 * @param[in] key Name or Namespace as a search key.
403 * @param[in] key_offset Key's offset in struct lysp_module to get value from the context's modules to match with the key.
404 * @return Matching module if any.
405 */
406static const struct lys_module *
407ly_ctx_get_module_implemented_by(const struct ly_ctx *ctx, const char *key, size_t key_offset)
408{
409 const struct lys_module *mod;
410 unsigned int index = 0;
411
412 while ((mod = ly_ctx_get_module_by_iter(ctx, key, key_offset, &index))) {
413 if ((mod->compiled && mod->compiled->implemented) || (!mod->compiled && mod->parsed->implemented)) {
414 return mod;
415 }
416 }
417
418 return NULL;
419}
420
421API const struct lys_module *
422ly_ctx_get_module_implemented(const struct ly_ctx *ctx, const char *name)
423{
424 LY_CHECK_ARG_RET(ctx, ctx, name, NULL);
425 return ly_ctx_get_module_implemented_by(ctx, name, offsetof(struct lysp_module, name));
426}
427
428API const struct lys_module *
429ly_ctx_get_module_implemented_ns(const struct ly_ctx *ctx, const char *ns)
430{
431 LY_CHECK_ARG_RET(ctx, ctx, ns, NULL);
432 return ly_ctx_get_module_implemented_by(ctx, ns, offsetof(struct lysp_module, ns));
433}
434
Radek Krejcid33273d2018-10-25 14:55:52 +0200435struct lysp_module *
436ly_ctx_get_submodule(const struct ly_ctx *ctx, const char *module, const char *submodule, const char *revision)
437{
438 const struct lys_module *mod;
439 struct lysp_include *inc;
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100440 unsigned int v, u;
Radek Krejcid33273d2018-10-25 14:55:52 +0200441
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100442 assert(submodule);
443
444 for (v = 0; v < ctx->list.count; ++v) {
445 mod = ctx->list.objs[v];
Radek Krejcid33273d2018-10-25 14:55:52 +0200446 if (!mod->parsed) {
447 continue;
448 }
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100449 if (module && strcmp(module, mod->parsed->name)) {
450 continue;
451 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200452
453 LY_ARRAY_FOR(mod->parsed->includes, u) {
Radek Krejci086c7132018-10-26 15:29:04 +0200454 if (mod->parsed->includes[u].submodule && !strcmp(submodule, mod->parsed->includes[u].submodule->name)) {
Radek Krejcid33273d2018-10-25 14:55:52 +0200455 inc = &mod->parsed->includes[u];
456 if (!revision) {
457 if (inc->submodule->latest_revision) {
458 return inc->submodule;
459 }
460 } else if (inc->submodule->revs && !strcmp(revision, inc->submodule->revs[0].date)) {
461 return inc->submodule;
462 }
463 }
464 }
465 }
466
467 return NULL;
468}
469
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200470API void
Radek Krejcie9e987e2018-10-31 12:50:27 +0100471ly_ctx_reset_latests(struct ly_ctx *ctx)
472{
473 unsigned int u,v ;
474 struct lys_module *mod;
475
476 for (u = 0; u < ctx->list.count; ++u) {
477 mod = ctx->list.objs[u];
478 if (mod->compiled && mod->compiled->latest_revision == 2) {
479 mod->compiled->latest_revision = 1;
480 }
481 if (mod->parsed) {
482 if (mod->parsed->latest_revision == 2) {
483 mod->parsed->latest_revision = 1;
484 }
485 if (mod->parsed->includes) {
486 for (v = 0; v < LY_ARRAY_SIZE(mod->parsed->includes); ++v) {
487 if (mod->parsed->includes[v].submodule->latest_revision == 2) {
488 mod->parsed->includes[v].submodule->latest_revision = 1;
489 }
490 }
491 }
492 }
493 }
494}
495
496API void
Radek Krejciad573502018-09-07 15:26:55 +0200497ly_ctx_destroy(struct ly_ctx *ctx, void (*private_destructor)(const struct lysc_node *node, void *priv))
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200498{
Radek Krejci418823a2018-09-20 16:42:13 +0200499 if (!ctx) {
500 return;
501 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200502
503 /* models list */
Michal Vaskob34480a2018-09-17 10:34:45 +0200504 for (; ctx->list.count; ctx->list.count--) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200505 /* remove the module */
Radek Krejci86d106e2018-10-18 09:53:19 +0200506 lys_module_free(ctx->list.objs[ctx->list.count - 1], private_destructor);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200507 }
508 free(ctx->list.objs);
509
510 /* search paths list */
Radek Krejcia40f21b2018-09-18 10:42:08 +0200511 ly_set_erase(&ctx->search_paths, free);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200512
513 /* clean the error list */
514 ly_err_clean(ctx, 0);
515 pthread_key_delete(ctx->errlist_key);
516
517 /* dictionary */
518 lydict_clean(&ctx->dict);
519
520#if 0 /* TODO when plugins implemented */
521 /* plugins - will be removed only if this is the last context */
522 ly_clean_plugins();
523#endif
524
525 free(ctx);
526}