blob: 31f6e3e3f5ef821f8f72c6b794f5c8a65fc437e9 [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
15#include <errno.h>
16#include <unistd.h>
17
Radek Krejci0af5f5d2018-09-07 15:00:30 +020018#include "context.h"
Radek Krejciad573502018-09-07 15:26:55 +020019#include "common.h"
20
21#include "libyang.h"
Radek Krejci0af5f5d2018-09-07 15:00:30 +020022
23#define LY_INTERNAL_MODS_COUNT 0 /* TODO 6 when parser available */
24
25#define IETF_YANG_METADATA_PATH "../models/ietf-yang-metadata@2016-08-05.h"
26#define YANG_PATH "../models/yang@2017-02-20.h"
27#define IETF_INET_TYPES_PATH "../models/ietf-inet-types@2013-07-15.h"
28#define IETF_YANG_TYPES_PATH "../models/ietf-yang-types@2013-07-15.h"
29#define IETF_DATASTORES "../models/ietf-datastores@2017-08-17.h"
30#define IETF_YANG_LIB_PATH "../models/ietf-yang-library@2018-01-17.h"
31#define IETF_YANG_LIB_REV "2018-01-17"
32
33#if LY_INTERNAL_MODS_COUNT
34#include IETF_YANG_METADATA_PATH
35#include YANG_PATH
36#include IETF_INET_TYPES_PATH
37#include IETF_YANG_TYPES_PATH
38#include IETF_DATASTORES
39#include IETF_YANG_LIB_PATH
Radek Krejci0af5f5d2018-09-07 15:00:30 +020040
41static struct internal_modules_s {
42 const char *name;
43 const char *revision;
44 const char *data;
45 uint8_t implemented;
46 LYS_INFORMAT format;
47} internal_modules[LY_INTERNAL_MODS_COUNT] = {
Radek Krejci0af5f5d2018-09-07 15:00:30 +020048 {"ietf-yang-metadata", "2016-08-05", (const char*)ietf_yang_metadata_2016_08_05_yin, 0, LYS_IN_YIN},
49 {"yang", "2017-02-20", (const char*)yang_2017_02_20_yin, 1, LYS_IN_YIN},
50 {"ietf-inet-types", "2013-07-15", (const char*)ietf_inet_types_2013_07_15_yin, 0, LYS_IN_YIN},
51 {"ietf-yang-types", "2013-07-15", (const char*)ietf_yang_types_2013_07_15_yin, 0, LYS_IN_YIN},
52 /* ietf-datastores and ietf-yang-library must be right here at the end of the list! */
53 {"ietf-datastores", "2017-08-17", (const char*)ietf_datastores_2017_08_17_yin, 0, LYS_IN_YIN},
54 {"ietf-yang-library", IETF_YANG_LIB_REV, (const char*)ietf_yang_library_2018_01_17_yin, 1, LYS_IN_YIN}
Radek Krejci0af5f5d2018-09-07 15:00:30 +020055};
Radek Krejciad573502018-09-07 15:26:55 +020056#endif
Radek Krejci0af5f5d2018-09-07 15:00:30 +020057
58API LY_ERR
59ly_ctx_set_searchdir(struct ly_ctx *ctx, const char *search_dir)
60{
61 char *new_dir = NULL;
62 LY_ERR rc = LY_ESYS;
63
64 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
65
66 if (search_dir) {
67 LY_CHECK_ERR_GOTO(access(search_dir, R_OK | X_OK),
68 LOGERR(ctx, LY_ESYS, "Unable to use search directory \"%s\" (%s)", search_dir, strerror(errno)),
69 cleanup);
70 new_dir = realpath(search_dir, NULL);
71 LY_CHECK_ERR_GOTO(!new_dir,
72 LOGERR(ctx, LY_ESYS, "realpath() call failed for \"%s\" (%s).", search_dir, strerror(errno)),
73 cleanup);
Radek Krejciad573502018-09-07 15:26:55 +020074 if (ly_set_add(&ctx->search_paths, new_dir, 0) == -1) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +020075 free(new_dir);
76 return LY_EMEM;
77 }
78
79 rc = LY_SUCCESS;
80 } else {
81 /* consider that no change is not actually an error */
82 return LY_SUCCESS;
83 }
84
85cleanup:
86 free(new_dir);
87 return rc;
88}
89
90API const char * const *
91ly_ctx_get_searchdirs(const struct ly_ctx *ctx)
92{
93 LY_CHECK_ARG_RET(ctx, ctx, NULL);
94 return (const char * const *)ctx->search_paths.objs;
95}
96
97API LY_ERR
98ly_ctx_unset_searchdirs(struct ly_ctx *ctx, int index)
99{
100 if (!ctx->search_paths.number) {
101 return LY_SUCCESS;
102 }
103
104 if (index >= 0) {
105 /* remove specific search directory */
Radek Krejciad573502018-09-07 15:26:55 +0200106 return ly_set_rm_index(&ctx->search_paths, index);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200107 } else {
108 /* remove them all */
109 for (; ctx->search_paths.number; ctx->search_paths.number--) {
110 free(ctx->search_paths.objs[ctx->search_paths.number - 1]);
111 }
112 free(ctx->search_paths.objs);
113 memset(&ctx->search_paths, 0, sizeof ctx->search_paths);
114 }
115
116 return LY_SUCCESS;
117}
118
119API LY_ERR
120ly_ctx_new(const char *search_dir, int options, struct ly_ctx **new_ctx)
121{
122 struct ly_ctx *ctx = NULL;
123 struct lys_module *module;
124 char *search_dir_list;
125 char *sep, *dir;
126 LY_ERR rc = LY_SUCCESS;
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200127
128 ctx = calloc(1, sizeof *ctx);
Radek Krejciad573502018-09-07 15:26:55 +0200129 LY_CHECK_ERR_RET(!ctx, LOGMEM(NULL), LY_EMEM);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200130
131 /* dictionary */
132 lydict_init(&ctx->dict);
133
Radek Krejciad573502018-09-07 15:26:55 +0200134#if 0 /* TODO when plugins implemented */
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200135 /* plugins */
136 ly_load_plugins();
Radek Krejciad573502018-09-07 15:26:55 +0200137#endif
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200138
139 /* initialize thread-specific key */
Radek Krejciad573502018-09-07 15:26:55 +0200140 while ((pthread_key_create(&ctx->errlist_key, ly_err_free)) == EAGAIN);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200141
142 /* models list */
143 ctx->flags = options;
144 if (search_dir) {
145 search_dir_list = strdup(search_dir);
146 LY_CHECK_ERR_GOTO(!search_dir_list, LOGMEM(NULL); rc = LY_EMEM, error);
147
148 for (dir = search_dir_list; (sep = strchr(dir, ':')) != NULL && rc == LY_SUCCESS; dir = sep + 1) {
149 *sep = 0;
150 rc = ly_ctx_set_searchdir(ctx, dir);
151 }
152 if (*dir && rc == LY_SUCCESS) {
153 rc = ly_ctx_set_searchdir(ctx, dir);
154 }
155 free(search_dir_list);
156
157 /* If ly_ctx_set_searchdir() failed, the error is already logged. Just exit */
158 if (rc != LY_SUCCESS) {
159 goto error;
160 }
161 }
162 ctx->module_set_id = 1;
163
Radek Krejciad573502018-09-07 15:26:55 +0200164#if 0 /* TODO when parser implemented */
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200165 /* load internal modules */
166 for (i = 0; i < (options & LY_CTX_NOYANGLIBRARY) ? LY_INTERNAL_MODS_COUNT - 2 : LY_INTERNAL_MODS_COUNT; i++) {
167 module = (struct lys_module *)lys_parse_mem(ctx, internal_modules[i].data, internal_modules[i].format);
168 LY_CHECK_GOTO(!module, error);
169 module->parsed->implemented = internal_modules[i].implemented;
170 }
Radek Krejciad573502018-09-07 15:26:55 +0200171#endif
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200172
173 *new_ctx = ctx;
174 return rc;
175
176error:
177 ly_ctx_destroy(ctx, NULL);
178 return rc;
179}
180
181API void
182ly_ctx_set_disable_searchdirs(struct ly_ctx *ctx)
183{
184 LY_CHECK_ARG_RET(ctx, ctx,);
185 ctx->flags |= LY_CTX_DISABLE_SEARCHDIRS;
186}
187
188API void
189ly_ctx_unset_disable_searchdirs(struct ly_ctx *ctx)
190{
191 LY_CHECK_ARG_RET(ctx, ctx,);
192 ctx->flags &= ~LY_CTX_DISABLE_SEARCHDIRS;
193}
194
195API void
196ly_ctx_set_disable_searchdir_cwd(struct ly_ctx *ctx)
197{
198 LY_CHECK_ARG_RET(ctx, ctx,);
199 ctx->flags |= LY_CTX_DISABLE_SEARCHDIR_CWD;
200}
201
202API void
203ly_ctx_unset_disable_searchdir_cwd(struct ly_ctx *ctx)
204{
205 LY_CHECK_ARG_RET(ctx, ctx,);
206 ctx->flags &= ~LY_CTX_DISABLE_SEARCHDIR_CWD;
207}
208
209API void
210ly_ctx_set_prefer_searchdirs(struct ly_ctx *ctx)
211{
212 LY_CHECK_ARG_RET(ctx, ctx,);
213 ctx->flags |= LY_CTX_PREFER_SEARCHDIRS;
214}
215
216API void
217ly_ctx_unset_prefer_searchdirs(struct ly_ctx *ctx)
218{
219 LY_CHECK_ARG_RET(ctx, ctx,);
220 ctx->flags &= ~LY_CTX_PREFER_SEARCHDIRS;
221}
222
223API void
224ly_ctx_set_allimplemented(struct ly_ctx *ctx)
225{
226 LY_CHECK_ARG_RET(ctx, ctx,);
227 ctx->flags |= LY_CTX_ALLIMPLEMENTED;
228}
229
230API void
231ly_ctx_unset_allimplemented(struct ly_ctx *ctx)
232{
233 LY_CHECK_ARG_RET(ctx, ctx,);
234 ctx->flags &= ~LY_CTX_ALLIMPLEMENTED;
235}
236
237API void
238ly_ctx_set_trusted(struct ly_ctx *ctx)
239{
240 LY_CHECK_ARG_RET(ctx, ctx,);
241 ctx->flags |= LY_CTX_TRUSTED;
242}
243
244API void
245ly_ctx_unset_trusted(struct ly_ctx *ctx)
246{
247 LY_CHECK_ARG_RET(ctx, ctx,);
248 ctx->flags &= ~LY_CTX_TRUSTED;
249}
250
251API int
252ly_ctx_get_options(struct ly_ctx *ctx)
253{
254 return ctx->flags;
255}
256
257API uint16_t
258ly_ctx_get_module_set_id(const struct ly_ctx *ctx)
259{
260 return ctx->module_set_id;
261}
262
263API void
Radek Krejciad573502018-09-07 15:26:55 +0200264ly_ctx_destroy(struct ly_ctx *ctx, void (*private_destructor)(const struct lysc_node *node, void *priv))
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200265{
266 LY_CHECK_ARG_RET(ctx, ctx,);
267
268 /* models list */
269 for (; ctx->list.number; ctx->list.number--) {
270 /* remove the module */
271#if 0 /* TODO when parser implemented */
272 lys_free(ctx->list[ctx->list.number - 1], private_destructor, 1, 0);
273#endif
274 }
275 free(ctx->list.objs);
276
277 /* search paths list */
278 ly_ctx_unset_searchdirs(ctx, -1);
279
280 /* clean the error list */
281 ly_err_clean(ctx, 0);
282 pthread_key_delete(ctx->errlist_key);
283
284 /* dictionary */
285 lydict_clean(&ctx->dict);
286
287#if 0 /* TODO when plugins implemented */
288 /* plugins - will be removed only if this is the last context */
289 ly_clean_plugins();
290#endif
291
292 free(ctx);
293}