Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file context.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief context implementation for libyang |
| 5 | * |
| 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in |
| 15 | * the documentation and/or other materials provided with the |
| 16 | * distribution. |
| 17 | * 3. Neither the name of the Company nor the names of its contributors |
| 18 | * may be used to endorse or promote products derived from this |
| 19 | * software without specific prior written permission. |
| 20 | */ |
| 21 | |
| 22 | #define _GNU_SOURCE |
| 23 | #include <dirent.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <unistd.h> |
| 29 | #include <errno.h> |
| 30 | #include <fcntl.h> |
| 31 | |
| 32 | #include "common.h" |
| 33 | #include "context.h" |
| 34 | #include "dict.h" |
| 35 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 36 | API struct ly_ctx * |
| 37 | ly_ctx_new(const char *search_dir) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 38 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 39 | struct ly_ctx *ctx; |
| 40 | char *cwd; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 41 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 42 | ctx = calloc(1, sizeof *ctx); |
| 43 | if (!ctx) { |
| 44 | LOGMEM; |
| 45 | return NULL; |
| 46 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 47 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 48 | /* dictionary */ |
| 49 | lydict_init(&ctx->dict); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 50 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 51 | /* models list */ |
| 52 | ctx->models.list = calloc(16, sizeof *ctx->models.list); |
| 53 | ctx->models.used = 0; |
| 54 | ctx->models.size = 16; |
| 55 | if (search_dir) { |
| 56 | cwd = get_current_dir_name(); |
| 57 | if (chdir(search_dir)) { |
| 58 | LOGERR(LY_ESYS, "Unable to use search directory \"%s\" (%s)", |
| 59 | search_dir, strerror(errno)); |
| 60 | free(cwd); |
| 61 | ly_ctx_destroy(ctx); |
| 62 | return NULL; |
| 63 | } |
| 64 | ctx->models.search_path = get_current_dir_name(); |
| 65 | chdir(cwd); |
| 66 | free(cwd); |
| 67 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 68 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 69 | return ctx; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 70 | } |
| 71 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 72 | API void |
Michal Vasko | 60ba9a6 | 2015-07-03 14:42:31 +0200 | [diff] [blame^] | 73 | ly_ctx_set_searchdir(struct ly_ctx *ctx, const char *search_dir) |
| 74 | { |
| 75 | char *cwd; |
| 76 | |
| 77 | if (!ctx) { |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | if (search_dir) { |
| 82 | cwd = get_current_dir_name(); |
| 83 | if (chdir(search_dir)) { |
| 84 | LOGERR(LY_ESYS, "Unable to use search directory \"%s\" (%s)", |
| 85 | search_dir, strerror(errno)); |
| 86 | free(cwd); |
| 87 | return; |
| 88 | } |
| 89 | ctx->models.search_path = get_current_dir_name(); |
| 90 | chdir(cwd); |
| 91 | free(cwd); |
| 92 | } else { |
| 93 | free(ctx->models.search_path); |
| 94 | ctx->models.search_path = NULL; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | API void |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 99 | ly_ctx_destroy(struct ly_ctx *ctx) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 100 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 101 | if (!ctx) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 102 | return; |
| 103 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 104 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 105 | /* models list */ |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 106 | while (ctx->models.used) { |
| 107 | ly_module_free(ctx->models.list[0]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 108 | } |
| 109 | free(ctx->models.search_path); |
| 110 | free(ctx->models.list); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 111 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 112 | /* dictionary */ |
| 113 | lydict_clean(&ctx->dict); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 114 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 115 | free(ctx); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 116 | } |
| 117 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 118 | static struct ly_module * |
| 119 | search_file(struct ly_ctx *ctx, struct ly_module *module, const char *name, const char *revision) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 120 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 121 | size_t len, flen; |
| 122 | int fd; |
| 123 | char *cwd; |
| 124 | DIR *dir; |
| 125 | struct dirent *file; |
| 126 | LY_MINFORMAT format; |
| 127 | struct ly_module *result = NULL; |
| 128 | int localsearch = 1; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 129 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 130 | len = strlen(name); |
| 131 | cwd = get_current_dir_name(); |
| 132 | dir = opendir(cwd); |
| 133 | LOGVRB("Searching for \"%s\" in %s.", name, cwd); |
| 134 | if (!dir) { |
| 135 | LOGWRN("Unable to open local directory for searching referenced modules (%s)", |
| 136 | strerror(errno)); |
| 137 | /* try search directory */ |
| 138 | goto searchpath; |
| 139 | } |
Radek Krejci | 3327750 | 2015-05-27 15:38:15 +0200 | [diff] [blame] | 140 | |
| 141 | search: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 142 | while ((file = readdir(dir))) { |
| 143 | if (strncmp(name, file->d_name, len)) { |
| 144 | continue; |
| 145 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 146 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 147 | flen = strlen(file->d_name); |
| 148 | if (revision && flen > len + 5) { |
| 149 | /* check revision from the filename */ |
| 150 | /* TODO */ |
| 151 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 152 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 153 | /* get type according to filename suffix */ |
| 154 | if (!strcmp(&file->d_name[flen - 4], ".yin")) { |
| 155 | format = LY_IN_YIN; |
| 156 | } else if (!strcmp(&file->d_name[flen - 5], ".yang")) { |
| 157 | format = LY_IN_YANG; |
| 158 | } else { |
| 159 | continue; |
| 160 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 161 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 162 | /* open the file */ |
| 163 | fd = open(file->d_name, O_RDONLY); |
| 164 | if (fd < 0) { |
| 165 | LOGERR(LY_ESYS, "Unable to open data model file \"%s\" (%s).", |
| 166 | file->d_name, strerror(errno)); |
| 167 | goto cleanup; |
| 168 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 169 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 170 | if (module) { |
| 171 | result = (struct ly_module *)ly_submodule_read_fd(module, fd, format); |
| 172 | } else { |
| 173 | result = ly_module_read_fd(ctx, fd, format); |
| 174 | } |
| 175 | close(fd); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 176 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 177 | if (result) { |
| 178 | break; |
| 179 | } |
| 180 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 181 | |
Radek Krejci | 3327750 | 2015-05-27 15:38:15 +0200 | [diff] [blame] | 182 | searchpath: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 183 | if (!ctx->models.search_path) { |
| 184 | LOGWRN("No search path defined for the current context."); |
| 185 | } else if (!result && localsearch) { |
| 186 | /* search in local directory done, try context's search_path */ |
| 187 | closedir(dir); |
| 188 | dir = opendir(ctx->models.search_path); |
| 189 | if (!dir) { |
| 190 | LOGERR(LY_ESYS, "Unable to open data model search directory \"%s\" (%s).", |
| 191 | ctx->models.search_path, strerror(errno)); |
| 192 | goto cleanup; |
| 193 | } |
Radek Krejci | 3327750 | 2015-05-27 15:38:15 +0200 | [diff] [blame] | 194 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 195 | chdir(ctx->models.search_path); |
| 196 | LOGVRB("Searching for \"%s\" in %s.", name, ctx->models.search_path); |
Radek Krejci | 3327750 | 2015-05-27 15:38:15 +0200 | [diff] [blame] | 197 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 198 | localsearch = 0; |
| 199 | goto search; |
| 200 | } |
Radek Krejci | 3327750 | 2015-05-27 15:38:15 +0200 | [diff] [blame] | 201 | |
Radek Krejci | d2b7c57 | 2015-05-26 15:59:03 +0200 | [diff] [blame] | 202 | cleanup: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 203 | chdir(cwd); |
| 204 | free(cwd); |
| 205 | closedir(dir); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 206 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 207 | return result; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 208 | } |
| 209 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 210 | struct ly_submodule * |
| 211 | ly_ctx_get_submodule(struct ly_module *module, const char *name, const char *revision) |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 212 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 213 | struct ly_submodule *result; |
| 214 | int i; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 215 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 216 | if (!module || !name) { |
| 217 | ly_errno = LY_EINVAL; |
| 218 | return NULL; |
| 219 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 220 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 221 | /* search in modules included by the main module */ |
| 222 | if (module->type) { |
| 223 | module = ((struct ly_submodule *)module)->belongsto; |
| 224 | } |
| 225 | for (i = 0; i < module->inc_size; i++) { |
| 226 | result = module->inc[i].submodule; |
| 227 | if (strcmp(name, result->name)) { |
| 228 | continue; |
| 229 | } |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 230 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 231 | if (!revision || (result->rev_size && !strcmp(revision, result->rev[0].date))) { |
| 232 | return result; |
| 233 | } |
| 234 | } |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 235 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 236 | /* not found in context, try to get it from the search directory */ |
| 237 | result = (struct ly_submodule *)search_file(module->ctx, module, name, revision); |
| 238 | if (!result) { |
| 239 | LOGERR(LY_EVALID, "Submodule \"%s\" of the \"%s\" data model not found (search path is \"%s\")", |
| 240 | name, module->name, module->ctx->models.search_path); |
| 241 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 242 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 243 | return result; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 244 | } |
| 245 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 246 | API struct ly_module * |
| 247 | ly_ctx_get_module(struct ly_ctx *ctx, const char *name, const char *revision, int read) |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 248 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 249 | int i; |
| 250 | struct ly_module *result = NULL; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 251 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 252 | if (!ctx || !name) { |
| 253 | ly_errno = LY_EINVAL; |
| 254 | return NULL; |
| 255 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 256 | |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 257 | for (i = 0; i < ctx->models.used; i++) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 258 | result = ctx->models.list[i]; |
| 259 | if (!result || strcmp(name, result->name)) { |
| 260 | continue; |
| 261 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 262 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 263 | if (!revision || (result->rev_size && !strcmp(revision, result->rev[0].date))) { |
| 264 | return result; |
| 265 | } |
| 266 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 267 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 268 | if (!read) { |
Michal Vasko | 41c47dd | 2015-06-30 15:23:19 +0200 | [diff] [blame] | 269 | return NULL; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 270 | } |
Radek Krejci | 0af1387 | 2015-05-30 11:50:52 +0200 | [diff] [blame] | 271 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 272 | /* not found in context, try to get it from the search directory */ |
| 273 | result = search_file(ctx, NULL, name, revision); |
| 274 | if (!result) { |
| 275 | LOGERR(LY_EVALID, "Data model \"%s\" not found (search path is \"%s\")", name, ctx->models.search_path); |
| 276 | } |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 277 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 278 | return result; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 279 | } |
Michal Vasko | 3ec07dc | 2015-06-30 15:51:30 +0200 | [diff] [blame] | 280 | |
| 281 | API char ** |
| 282 | ly_ctx_get_module_names(struct ly_ctx *ctx) |
| 283 | { |
| 284 | int i; |
| 285 | char **result = NULL; |
| 286 | unsigned int count = 0; |
| 287 | |
| 288 | if (!ctx) { |
| 289 | ly_errno = LY_EINVAL; |
| 290 | return NULL; |
| 291 | } |
| 292 | |
| 293 | for (i = 0; i < ctx->models.used; i++) { |
| 294 | ++count; |
| 295 | result = realloc(result, count * sizeof *result); |
| 296 | result[count-1] = strdup(ctx->models.list[i]->name); |
| 297 | } |
| 298 | ++count; |
| 299 | result = realloc(result, count * sizeof *result); |
| 300 | result[count-1] = NULL; |
| 301 | |
| 302 | return result; |
| 303 | } |