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