| /** |
| * @file tree_schema.c |
| * @author Radek Krejci <rkrejci@cesnet.cz> |
| * @brief Schema tree implementation |
| * |
| * Copyright (c) 2015 - 2018 CESNET, z.s.p.o. |
| * |
| * This source code is licensed under BSD 3-Clause License (the "License"). |
| * You may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * https://opensource.org/licenses/BSD-3-Clause |
| */ |
| |
| #include "common.h" |
| |
| #include <dirent.h> |
| #include <errno.h> |
| #include <fcntl.h> |
| #include <stdio.h> |
| #include <sys/stat.h> |
| #include <sys/types.h> |
| #include <unistd.h> |
| |
| #include "libyang.h" |
| #include "context.h" |
| #include "tree_schema_internal.h" |
| #include "xpath.h" |
| |
| API const struct lysc_node * |
| lys_getnext(const struct lysc_node *last, const struct lysc_node *parent, const struct lysc_module *module, int options) |
| { |
| const struct lysc_node *next; |
| struct lysc_node **snode; |
| |
| LY_CHECK_ARG_RET(NULL, parent || module, NULL); |
| |
| if (!last) { |
| /* first call */ |
| |
| /* get know where to start */ |
| if (parent) { |
| /* schema subtree */ |
| snode = lysc_node_children(parent); |
| /* do not return anything if the augment does not have any children */ |
| if (!snode || !(*snode)) { |
| return NULL; |
| } |
| next = last = *snode; |
| } else { |
| /* top level data */ |
| next = last = module->data; |
| } |
| if (!next) { |
| return next; |
| } else if (!(options & LYS_GETNEXT_NOSTATECHECK)) { |
| if (!lys_is_disabled(next, 0)) { |
| return next; |
| } |
| /* continue to find next available node */ |
| } else { |
| return next; |
| } |
| } |
| |
| next = last->next; |
| repeat: |
| if (!next) { |
| return next; |
| } |
| switch (next->nodetype) { |
| case LYS_ACTION: |
| case LYS_NOTIF: |
| case LYS_LEAF: |
| case LYS_ANYXML: |
| case LYS_ANYDATA: |
| case LYS_LIST: |
| case LYS_LEAFLIST: |
| break; |
| case LYS_CONTAINER: |
| if (!(((struct lysc_node_container *)next)->flags & LYS_PRESENCE) && (options & LYS_GETNEXT_INTONPCONT)) { |
| if (((struct lysc_node_container *)next)->child) { |
| /* go into */ |
| next = ((struct lysc_node_container *)next)->child; |
| } else { |
| next = next->next; |
| } |
| goto repeat; |
| } |
| break; |
| case LYS_CHOICE: |
| if (options & LYS_GETNEXT_WITHCHOICE) { |
| return next; |
| } else if (((struct lysc_node_choice *)next)->cases) { |
| /* go into */ |
| next = ((struct lysc_node_choice *)next)->cases[0].child; |
| } else { |
| next = next->next; |
| } |
| goto repeat; |
| default: |
| /* we should not be here */ |
| LOGINT(last->module->compiled->ctx); |
| return NULL; |
| } |
| |
| if (!(options & LYS_GETNEXT_NOSTATECHECK)) { |
| /* check if the node is disabled by if-feature */ |
| if (lys_is_disabled(next, 0)) { |
| next = next->next; |
| goto repeat; |
| } |
| } |
| return next; |
| } |
| |
| API const struct lysc_node * |
| lys_child(const struct lysc_node *parent, const struct lys_module *module, |
| const char *name, size_t name_len, uint16_t nodetype, int options) |
| { |
| const struct lysc_node *node = NULL; |
| |
| LY_CHECK_ARG_RET(NULL, module, name, NULL); |
| if (!nodetype) { |
| nodetype = 0xffff; |
| } |
| |
| while ((node = lys_getnext(node, parent, module->compiled, options))) { |
| if (!(node->nodetype & nodetype)) { |
| continue; |
| } |
| if (node->module != module) { |
| continue; |
| } |
| |
| if (name_len) { |
| if (!strncmp(node->name, name, name_len) && !node->name[name_len]) { |
| return node; |
| } |
| } else { |
| if (!strcmp(node->name, name)) { |
| return node; |
| } |
| } |
| } |
| return NULL; |
| } |
| |
| API int |
| lysc_feature_value(const struct lysc_feature *feature) |
| { |
| LY_CHECK_ARG_RET(NULL, feature, -1); |
| return feature->flags & LYS_FENABLED ? 1 : 0; |
| } |
| |
| static uint8_t |
| iff_getop(uint8_t *list, int pos) |
| { |
| uint8_t *item; |
| uint8_t mask = 3, result; |
| |
| assert(pos >= 0); |
| |
| item = &list[pos / 4]; |
| result = (*item) & (mask << 2 * (pos % 4)); |
| return result >> 2 * (pos % 4); |
| } |
| |
| static int |
| lysc_iffeature_value_(const struct lysc_iffeature *iff, int *index_e, int *index_f) |
| { |
| uint8_t op; |
| int a, b; |
| |
| op = iff_getop(iff->expr, *index_e); |
| (*index_e)++; |
| |
| switch (op) { |
| case LYS_IFF_F: |
| /* resolve feature */ |
| return lysc_feature_value(iff->features[(*index_f)++]); |
| case LYS_IFF_NOT: |
| /* invert result */ |
| return lysc_iffeature_value_(iff, index_e, index_f) ? 0 : 1; |
| case LYS_IFF_AND: |
| case LYS_IFF_OR: |
| a = lysc_iffeature_value_(iff, index_e, index_f); |
| b = lysc_iffeature_value_(iff, index_e, index_f); |
| if (op == LYS_IFF_AND) { |
| return a && b; |
| } else { /* LYS_IFF_OR */ |
| return a || b; |
| } |
| } |
| |
| return 0; |
| } |
| |
| API int |
| lysc_iffeature_value(const struct lysc_iffeature *iff) |
| { |
| int index_e = 0, index_f = 0; |
| |
| LY_CHECK_ARG_RET(NULL, iff, -1); |
| |
| if (iff->expr) { |
| return lysc_iffeature_value_(iff, &index_e, &index_f); |
| } |
| return 0; |
| } |
| |
| /** |
| * @brief Enable/Disable the specified feature in the module. |
| * |
| * If the feature is already set to the desired value, LY_SUCCESS is returned. |
| * By changing the feature, also all the feature which depends on it via their |
| * if-feature statements are again evaluated (disabled if a if-feature statemen |
| * evaluates to false). |
| * |
| * @param[in] mod Compiled module where to set (search for) the feature. |
| * @param[in] name Name of the feature to set. Asterisk ('*') can be used to |
| * set all the features in the module. |
| * @param[in] value Desired value of the feature: 1 (enable) or 0 (disable). |
| * @return LY_ERR value. |
| */ |
| static LY_ERR |
| lys_feature_change(const struct lysc_module *mod, const char *name, int value) |
| { |
| int all = 0; |
| unsigned int u, changed_count, disabled_count; |
| struct lysc_feature *f, **df; |
| struct lysc_iffeature *iff; |
| struct ly_set *changed; |
| |
| if (!mod->features) { |
| LOGERR(mod->ctx, LY_EINVAL, "Unable to switch feature since the module \"%s\" has no features.", mod->name); |
| return LY_EINVAL; |
| } |
| |
| if (!strcmp(name, "*")) { |
| /* enable all */ |
| all = 1; |
| } |
| changed = ly_set_new(); |
| changed_count = 0; |
| |
| run: |
| for (disabled_count = u = 0; u < LY_ARRAY_SIZE(mod->features); ++u) { |
| f = &mod->features[u]; |
| if (all || !strcmp(f->name, name)) { |
| if ((value && (f->flags & LYS_FENABLED)) || (!value && !(f->flags & LYS_FENABLED))) { |
| if (all) { |
| /* skip already set features */ |
| continue; |
| } else { |
| /* feature already set correctly */ |
| ly_set_free(changed, NULL); |
| return LY_SUCCESS; |
| } |
| } |
| |
| if (value) { /* enable */ |
| /* check referenced features if they are enabled */ |
| LY_ARRAY_FOR(f->iffeatures, struct lysc_iffeature, iff) { |
| if (!lysc_iffeature_value(iff)) { |
| if (all) { |
| ++disabled_count; |
| goto next; |
| } else { |
| LOGERR(mod->ctx, LY_EDENIED, |
| "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).", |
| f->name); |
| ly_set_free(changed, NULL); |
| return LY_EDENIED; |
| } |
| } |
| } |
| /* enable the feature */ |
| f->flags |= LYS_FENABLED; |
| } else { /* disable */ |
| /* disable the feature */ |
| f->flags &= ~LYS_FENABLED; |
| } |
| |
| /* remember the changed feature */ |
| ly_set_add(changed, f, LY_SET_OPT_USEASLIST); |
| |
| if (!all) { |
| /* stop in case changing a single feature */ |
| break; |
| } |
| } |
| next: |
| ; |
| } |
| |
| if (!all && !changed->count) { |
| LOGERR(mod->ctx, LY_EINVAL, "Feature \"%s\" not found in module \"%s\".", name, mod->name); |
| ly_set_free(changed, NULL); |
| return LY_EINVAL; |
| } |
| |
| if (value && all && disabled_count) { |
| if (changed_count == changed->count) { |
| /* no change in last run -> not able to enable all ... */ |
| /* ... print errors */ |
| for (u = 0; disabled_count && u < LY_ARRAY_SIZE(mod->features); ++u) { |
| if (!(mod->features[u].flags & LYS_FENABLED)) { |
| LOGERR(mod->ctx, LY_EDENIED, |
| "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).", |
| mod->features[u].name); |
| --disabled_count; |
| } |
| } |
| /* ... restore the original state */ |
| for (u = 0; u < changed->count; ++u) { |
| f = changed->objs[u]; |
| /* re-disable the feature */ |
| f->flags &= ~LYS_FENABLED; |
| } |
| |
| ly_set_free(changed, NULL); |
| return LY_EDENIED; |
| } else { |
| /* we did some change in last run, try it again */ |
| changed_count = changed->count; |
| goto run; |
| } |
| } |
| |
| /* reflect change(s) in the dependent features */ |
| for (u = 0; u < changed->count; ++u) { |
| /* If a dependent feature is enabled, it can be now changed by the change (to false) of the value of |
| * its if-feature statements. The reverse logic, automatically enable feature when its feature is enabled |
| * is not done - by default, features are disabled and must be explicitely enabled. */ |
| f = changed->objs[u]; |
| LY_ARRAY_FOR(f->depfeatures, struct lysc_feature*, df) { |
| if (!((*df)->flags & LYS_FENABLED)) { |
| /* not enabled, nothing to do */ |
| continue; |
| } |
| /* check the feature's if-features which could change by the previous change of our feature */ |
| LY_ARRAY_FOR((*df)->iffeatures, struct lysc_iffeature, iff) { |
| if (!lysc_iffeature_value(iff)) { |
| /* the feature must be disabled now */ |
| (*df)->flags &= ~LYS_FENABLED; |
| /* add the feature into the list of changed features */ |
| ly_set_add(changed, *df, LY_SET_OPT_USEASLIST); |
| break; |
| } |
| } |
| } |
| } |
| |
| ly_set_free(changed, NULL); |
| return LY_SUCCESS; |
| } |
| |
| API LY_ERR |
| lys_feature_enable(struct lys_module *module, const char *feature) |
| { |
| LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, LY_EINVAL); |
| |
| return lys_feature_change(module->compiled, feature, 1); |
| } |
| |
| API LY_ERR |
| lys_feature_disable(struct lys_module *module, const char *feature) |
| { |
| LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, LY_EINVAL); |
| |
| return lys_feature_change(module->compiled, feature, 0); |
| } |
| |
| API int |
| lys_feature_value(const struct lys_module *module, const char *feature) |
| { |
| struct lysc_feature *f; |
| struct lysc_module *mod; |
| unsigned int u; |
| |
| LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, -1); |
| mod = module->compiled; |
| |
| /* search for the specified feature */ |
| for (u = 0; u < LY_ARRAY_SIZE(mod->features); ++u) { |
| f = &mod->features[u]; |
| if (!strcmp(f->name, feature)) { |
| if (f->flags & LYS_FENABLED) { |
| return 1; |
| } else { |
| return 0; |
| } |
| } |
| } |
| |
| /* feature definition not found */ |
| return -1; |
| } |
| |
| API const struct lysc_iffeature * |
| lys_is_disabled(const struct lysc_node *node, int recursive) |
| { |
| unsigned int u; |
| struct lysc_iffeature **iff; |
| |
| LY_CHECK_ARG_RET(NULL, node, NULL); |
| |
| while(node) { |
| if (node->nodetype & LYS_CHOICE) { |
| return NULL; |
| } |
| |
| iff = lysc_node_iff(node); |
| if (iff && *iff) { |
| /* check local if-features */ |
| LY_ARRAY_FOR(*iff, u) { |
| if (!lysc_iffeature_value(&(*iff)[u])) { |
| return &(*iff)[u]; |
| } |
| } |
| } |
| |
| if (!recursive) { |
| return NULL; |
| } |
| |
| /* go through parents */ |
| node = node->parent; |
| } |
| return NULL; |
| } |
| |
| static void |
| lys_latest_switch(struct lys_module *old, struct lysp_module *new) |
| { |
| if (old->parsed) { |
| new->latest_revision = old->parsed->latest_revision; |
| old->parsed->latest_revision = 0; |
| } |
| if (old->compiled) { |
| new->latest_revision = old->parsed->latest_revision; |
| old->compiled->latest_revision = 0; |
| } |
| } |
| |
| struct lys_module * |
| lys_parse_mem_(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, int implement, struct ly_parser_ctx *main_ctx, |
| LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, void *data), void *check_data) |
| { |
| struct lys_module *mod = NULL, *latest, *mod_dup; |
| struct lysp_module *latest_p; |
| struct lysp_import *imp; |
| struct lysp_include *inc; |
| LY_ERR ret = LY_EINVAL; |
| unsigned int u, i; |
| struct ly_parser_ctx context = {0}; |
| |
| LY_CHECK_ARG_RET(ctx, ctx, data, NULL); |
| |
| context.ctx = ctx; |
| context.line = 1; |
| |
| if (main_ctx) { |
| /* map the typedefs and groupings list from main context to the submodule's context */ |
| memcpy(&context.tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes); |
| memcpy(&context.grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes); |
| } |
| |
| mod = calloc(1, sizeof *mod); |
| LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), NULL); |
| |
| switch (format) { |
| case LYS_IN_YIN: |
| /* TODO not yet supported |
| mod = yin_read_module(ctx, data, revision, implement); |
| */ |
| break; |
| case LYS_IN_YANG: |
| ret = yang_parse(&context, data, &mod->parsed); |
| break; |
| default: |
| LOGERR(ctx, LY_EINVAL, "Invalid schema input format."); |
| break; |
| } |
| LY_CHECK_ERR_RET(ret, free(mod), NULL); |
| |
| /* make sure that the newest revision is at position 0 */ |
| lysp_sort_revisions(mod->parsed->revs); |
| |
| if (implement) { |
| /* mark the loaded module implemented */ |
| if (ly_ctx_get_module_implemented(ctx, mod->parsed->name)) { |
| LOGERR(ctx, LY_EDENIED, "Module \"%s\" is already implemented in the context.", mod->parsed->name); |
| goto error; |
| } |
| mod->parsed->implemented = 1; |
| } |
| |
| if (custom_check) { |
| LY_CHECK_GOTO(custom_check(ctx, mod->parsed, check_data), error); |
| } |
| |
| if (mod->parsed->submodule) { /* submodule */ |
| if (!main_ctx) { |
| LOGERR(ctx, LY_EDENIED, "Input data contains submodule \"%s\" which cannot be parsed directly without its main module.", |
| mod->parsed->name); |
| goto error; |
| } |
| /* decide the latest revision */ |
| latest_p = ly_ctx_get_submodule(ctx, mod->parsed->belongsto, mod->parsed->name, NULL); |
| if (latest_p) { |
| if (mod->parsed->revs) { |
| if (!latest_p->revs) { |
| /* latest has no revision, so mod is anyway newer */ |
| mod->parsed->latest_revision = latest_p->latest_revision; |
| latest_p->latest_revision = 0; |
| } else { |
| if (strcmp(mod->parsed->revs[0].date, latest_p->revs[0].date) > 0) { |
| mod->parsed->latest_revision = latest_p->latest_revision; |
| latest_p->latest_revision = 0; |
| } |
| } |
| } |
| } else { |
| mod->parsed->latest_revision = 1; |
| } |
| /* remap possibly changed and reallocated typedefs and groupings list back to the main context */ |
| memcpy(&main_ctx->tpdfs_nodes, &context.tpdfs_nodes, sizeof main_ctx->tpdfs_nodes); |
| memcpy(&main_ctx->grps_nodes, &context.grps_nodes, sizeof main_ctx->grps_nodes); |
| } else { /* module */ |
| /* check for duplicity in the context */ |
| mod_dup = (struct lys_module*)ly_ctx_get_module(ctx, mod->parsed->name, mod->parsed->revs ? mod->parsed->revs[0].date : NULL); |
| if (mod_dup) { |
| if (mod_dup->parsed) { |
| /* error */ |
| if (mod->parsed->revs) { |
| LOGERR(ctx, LY_EEXIST, "Module \"%s\" of revision \"%s\" is already present in the context.", |
| mod->parsed->name, mod->parsed->revs[0].date); |
| } else { |
| LOGERR(ctx, LY_EEXIST, "Module \"%s\" with no revision is already present in the context.", |
| mod->parsed->name); |
| } |
| goto error; |
| } else { |
| /* add the parsed data to the currently compiled-only module in the context */ |
| mod_dup->parsed = mod->parsed; |
| free(mod); |
| mod = mod_dup; |
| goto finish_parsing; |
| } |
| } |
| |
| #if 0 |
| /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang |
| * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the |
| * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have |
| * the anotation definitions available in the internal schema structure. There is another hack in schema |
| * printers to do not print this internally added annotation. */ |
| if (mod && ly_strequal(mod->name, "ietf-netconf", 0)) { |
| if (lyp_add_ietf_netconf_annotations(mod)) { |
| lys_free(mod, NULL, 1, 1); |
| return NULL; |
| } |
| } |
| #endif |
| |
| /* decide the latest revision */ |
| latest = (struct lys_module*)ly_ctx_get_module_latest(ctx, mod->parsed->name); |
| if (latest) { |
| if (mod->parsed->revs) { |
| if ((latest->parsed && !latest->parsed->revs) || (!latest->parsed && !latest->compiled->revision)) { |
| /* latest has no revision, so mod is anyway newer */ |
| lys_latest_switch(latest, mod->parsed); |
| } else { |
| if (strcmp(mod->parsed->revs[0].date, latest->parsed ? latest->parsed->revs[0].date : latest->compiled->revision) > 0) { |
| lys_latest_switch(latest, mod->parsed); |
| } |
| } |
| } |
| } else { |
| mod->parsed->latest_revision = 1; |
| } |
| |
| /* add into context */ |
| ly_set_add(&ctx->list, mod, LY_SET_OPT_USEASLIST); |
| |
| finish_parsing: |
| /* resolve imports */ |
| mod->parsed->parsing = 1; |
| LY_ARRAY_FOR(mod->parsed->imports, u) { |
| imp = &mod->parsed->imports[u]; |
| if (!imp->module && lysp_load_module(ctx, imp->name, imp->rev[0] ? imp->rev : NULL, 0, 0, &imp->module)) { |
| goto error_ctx; |
| } |
| /* check for importing the same module twice */ |
| for (i = 0; i < u; ++i) { |
| if (imp->module == mod->parsed->imports[i].module) { |
| LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Single revision of the module \"%s\" referred twice.", imp->name); |
| goto error_ctx; |
| } |
| } |
| } |
| LY_ARRAY_FOR(mod->parsed->includes, u) { |
| inc = &mod->parsed->includes[u]; |
| if (!inc->submodule && lysp_load_submodule(&context, mod->parsed, inc)) { |
| goto error_ctx; |
| } |
| } |
| mod->parsed->parsing = 0; |
| |
| /* check name collisions - typedefs and groupings */ |
| LY_CHECK_GOTO(lysp_check_typedefs(&context), error_ctx); |
| } |
| |
| return mod; |
| |
| error_ctx: |
| ly_set_rm(&ctx->list, mod, NULL); |
| error: |
| lys_module_free(mod, NULL); |
| ly_set_erase(&context.tpdfs_nodes, NULL); |
| return NULL; |
| } |
| |
| API struct lys_module * |
| lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format) |
| { |
| return lys_parse_mem_(ctx, data, format, 1, NULL, NULL, NULL); |
| } |
| |
| static void |
| lys_parse_set_filename(struct ly_ctx *ctx, const char **filename, int fd) |
| { |
| #ifdef __APPLE__ |
| char path[MAXPATHLEN]; |
| #else |
| int len; |
| char path[PATH_MAX], proc_path[32]; |
| #endif |
| |
| #ifdef __APPLE__ |
| if (fcntl(fd, F_GETPATH, path) != -1) { |
| *filename = lydict_insert(ctx, path, 0); |
| } |
| #else |
| /* get URI if there is /proc */ |
| sprintf(proc_path, "/proc/self/fd/%d", fd); |
| if ((len = readlink(proc_path, path, PATH_MAX - 1)) > 0) { |
| *filename = lydict_insert(ctx, path, len); |
| } |
| #endif |
| } |
| |
| struct lys_module * |
| lys_parse_fd_(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, int implement, struct ly_parser_ctx *main_ctx, |
| LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, void *data), void *check_data) |
| { |
| struct lys_module *mod; |
| size_t length; |
| char *addr; |
| |
| LY_CHECK_ARG_RET(ctx, ctx, NULL); |
| if (fd < 0) { |
| LOGARG(ctx, fd); |
| return NULL; |
| } |
| |
| LY_CHECK_RET(ly_mmap(ctx, fd, &length, (void **)&addr), NULL); |
| if (!addr) { |
| LOGERR(ctx, LY_EINVAL, "Empty schema file."); |
| return NULL; |
| } |
| |
| mod = lys_parse_mem_(ctx, addr, format, implement, main_ctx, custom_check, check_data); |
| ly_munmap(addr, length); |
| |
| if (mod && !mod->parsed->filepath) { |
| lys_parse_set_filename(ctx, &mod->parsed->filepath, fd); |
| } |
| |
| return mod; |
| } |
| |
| API struct lys_module * |
| lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format) |
| { |
| return lys_parse_fd_(ctx, fd, format, 1, NULL, NULL, NULL); |
| } |
| |
| struct lys_module * |
| lys_parse_path_(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, int implement, struct ly_parser_ctx *main_ctx, |
| LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, void *data), void *check_data) |
| { |
| int fd; |
| struct lys_module *mod; |
| const char *rev, *dot, *filename; |
| size_t len; |
| |
| LY_CHECK_ARG_RET(ctx, ctx, path, NULL); |
| |
| fd = open(path, O_RDONLY); |
| LY_CHECK_ERR_RET(fd == -1, LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)), NULL); |
| |
| mod = lys_parse_fd_(ctx, fd, format, implement, main_ctx, custom_check, check_data); |
| close(fd); |
| LY_CHECK_RET(!mod, NULL); |
| |
| /* check that name and revision match filename */ |
| filename = strrchr(path, '/'); |
| if (!filename) { |
| filename = path; |
| } else { |
| filename++; |
| } |
| rev = strchr(filename, '@'); |
| dot = strrchr(filename, '.'); |
| |
| /* name */ |
| len = strlen(mod->parsed->name); |
| if (strncmp(filename, mod->parsed->name, len) || |
| ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) { |
| LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->parsed->name); |
| } |
| if (rev) { |
| len = dot - ++rev; |
| if (!mod->parsed->revs || len != 10 || strncmp(mod->parsed->revs[0].date, rev, len)) { |
| LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename, |
| mod->parsed->revs ? mod->parsed->revs[0].date : "none"); |
| } |
| } |
| |
| if (!mod->parsed->filepath) { |
| /* store URI */ |
| char rpath[PATH_MAX]; |
| if (realpath(path, rpath) != NULL) { |
| mod->parsed->filepath = lydict_insert(ctx, rpath, 0); |
| } else { |
| mod->parsed->filepath = lydict_insert(ctx, path, 0); |
| } |
| } |
| |
| return mod; |
| } |
| |
| API struct lys_module * |
| lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format) |
| { |
| return lys_parse_path_(ctx, path, format, 1, NULL, NULL, NULL); |
| } |
| |
| API LY_ERR |
| lys_search_localfile(const char * const *searchpaths, int cwd, const char *name, const char *revision, |
| char **localfile, LYS_INFORMAT *format) |
| { |
| size_t len, flen, match_len = 0, dir_len; |
| int i, implicit_cwd = 0, ret = EXIT_FAILURE; |
| char *wd, *wn = NULL; |
| DIR *dir = NULL; |
| struct dirent *file; |
| char *match_name = NULL; |
| LYS_INFORMAT format_aux, match_format = 0; |
| struct ly_set *dirs; |
| struct stat st; |
| |
| LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL); |
| |
| /* start to fill the dir fifo with the context's search path (if set) |
| * and the current working directory */ |
| dirs = ly_set_new(); |
| if (!dirs) { |
| LOGMEM(NULL); |
| return EXIT_FAILURE; |
| } |
| |
| len = strlen(name); |
| if (cwd) { |
| wd = get_current_dir_name(); |
| if (!wd) { |
| LOGMEM(NULL); |
| goto cleanup; |
| } else { |
| /* add implicit current working directory (./) to be searched, |
| * this directory is not searched recursively */ |
| if (ly_set_add(dirs, wd, 0) == -1) { |
| goto cleanup; |
| } |
| implicit_cwd = 1; |
| } |
| } |
| if (searchpaths) { |
| for (i = 0; searchpaths[i]; i++) { |
| /* check for duplicities with the implicit current working directory */ |
| if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) { |
| implicit_cwd = 0; |
| continue; |
| } |
| wd = strdup(searchpaths[i]); |
| if (!wd) { |
| LOGMEM(NULL); |
| goto cleanup; |
| } else if (ly_set_add(dirs, wd, 0) == -1) { |
| goto cleanup; |
| } |
| } |
| } |
| wd = NULL; |
| |
| /* start searching */ |
| while (dirs->count) { |
| free(wd); |
| free(wn); wn = NULL; |
| |
| dirs->count--; |
| wd = (char *)dirs->objs[dirs->count]; |
| dirs->objs[dirs->count] = NULL; |
| LOGVRB("Searching for \"%s\" in %s.", name, wd); |
| |
| if (dir) { |
| closedir(dir); |
| } |
| dir = opendir(wd); |
| dir_len = strlen(wd); |
| if (!dir) { |
| LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno)); |
| } else { |
| while ((file = readdir(dir))) { |
| if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) { |
| /* skip . and .. */ |
| continue; |
| } |
| free(wn); |
| if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) { |
| LOGMEM(NULL); |
| goto cleanup; |
| } |
| if (stat(wn, &st) == -1) { |
| LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)", |
| file->d_name, wd, strerror(errno)); |
| continue; |
| } |
| if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) { |
| /* we have another subdirectory in searchpath to explore, |
| * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */ |
| if (ly_set_add(dirs, wn, 0) == -1) { |
| goto cleanup; |
| } |
| /* continue with the next item in current directory */ |
| wn = NULL; |
| continue; |
| } else if (!S_ISREG(st.st_mode)) { |
| /* not a regular file (note that we see the target of symlinks instead of symlinks */ |
| continue; |
| } |
| |
| /* here we know that the item is a file which can contain a module */ |
| if (strncmp(name, file->d_name, len) || |
| (file->d_name[len] != '.' && file->d_name[len] != '@')) { |
| /* different filename than the module we search for */ |
| continue; |
| } |
| |
| /* get type according to filename suffix */ |
| flen = strlen(file->d_name); |
| if (!strcmp(&file->d_name[flen - 4], ".yin")) { |
| format_aux = LYS_IN_YIN; |
| } else if (!strcmp(&file->d_name[flen - 5], ".yang")) { |
| format_aux = LYS_IN_YANG; |
| } else { |
| /* not supportde suffix/file format */ |
| continue; |
| } |
| |
| if (revision) { |
| /* we look for the specific revision, try to get it from the filename */ |
| if (file->d_name[len] == '@') { |
| /* check revision from the filename */ |
| if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) { |
| /* another revision */ |
| continue; |
| } else { |
| /* exact revision */ |
| free(match_name); |
| match_name = wn; |
| wn = NULL; |
| match_len = dir_len + 1 + len; |
| match_format = format_aux; |
| goto success; |
| } |
| } else { |
| /* continue trying to find exact revision match, use this only if not found */ |
| free(match_name); |
| match_name = wn; |
| wn = NULL; |
| match_len = dir_len + 1 +len; |
| match_format = format_aux; |
| continue; |
| } |
| } else { |
| /* remember the revision and try to find the newest one */ |
| if (match_name) { |
| if (file->d_name[len] != '@' || |
| lysp_check_date(NULL, &file->d_name[len + 1], flen - (format_aux == LYS_IN_YANG ? 5 : 4) - len - 1, NULL)) { |
| continue; |
| } else if (match_name[match_len] == '@' && |
| (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) { |
| continue; |
| } |
| free(match_name); |
| } |
| |
| match_name = wn; |
| wn = NULL; |
| match_len = dir_len + 1 + len; |
| match_format = format_aux; |
| continue; |
| } |
| } |
| } |
| } |
| |
| success: |
| (*localfile) = match_name; |
| match_name = NULL; |
| if (format) { |
| (*format) = match_format; |
| } |
| ret = EXIT_SUCCESS; |
| |
| cleanup: |
| free(wn); |
| free(wd); |
| if (dir) { |
| closedir(dir); |
| } |
| free(match_name); |
| ly_set_free(dirs, free); |
| |
| return ret; |
| } |
| |