Skip to content

Commit

Permalink
refactor: one place to verify correct schema file name
Browse files Browse the repository at this point in the history
  • Loading branch information
jktjkt committed Sep 5, 2023
1 parent ad1b859 commit 545f724
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 51 deletions.
27 changes: 1 addition & 26 deletions src/tree_schema.c
Original file line number Diff line number Diff line change
Expand Up @@ -1749,8 +1749,6 @@ lys_parse_in(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format,
struct lysp_yin_ctx *yinctx = NULL;
struct lysp_ctx *pctx = NULL;
struct lysf_ctx fctx = {.ctx = ctx};
char *filename, *rev, *dot;
size_t len;
ly_bool module_created = 0;

assert(ctx && in && new_mods);
Expand Down Expand Up @@ -1831,30 +1829,7 @@ lys_parse_in(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format,

switch (in->type) {
case LY_IN_FILEPATH:
/* check that name and revision match filename */
filename = strrchr(in->method.fpath.filepath, '/');
if (!filename) {
filename = in->method.fpath.filepath;
} else {
filename++;
}
rev = strchr(filename, '@');
dot = strrchr(filename, '.');

/* name */
len = strlen(mod->name);
if (strncmp(filename, mod->name, len) ||
((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) {
LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name);
}
if (rev) {
len = dot - ++rev;
if (!mod->parsed->revs || (len != LY_REV_SIZE - 1) || 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");
}
}

ly_sanity_check_module_filename(ctx, mod->name, mod->parsed->revs ? mod->parsed->revs[0].date : NULL, in->method.fpath.filepath);
break;
case LY_IN_FD:
case LY_IN_FILE:
Expand Down
57 changes: 32 additions & 25 deletions src/tree_schema_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -696,9 +696,8 @@ static LY_ERR
lysp_load_module_check(const struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data)
{
struct lysp_load_module_check_data *info = data;
const char *filename, *dot, *rev, *name;
const char *name;
uint8_t latest_revision;
size_t len;
struct lysp_revision *revs;

name = mod ? mod->mod->name : submod->name;
Expand Down Expand Up @@ -739,29 +738,7 @@ lysp_load_module_check(const struct ly_ctx *ctx, struct lysp_module *mod, struct
}
}
if (info->path) {
/* check that name and revision match filename */
filename = strrchr(info->path, '/');
if (!filename) {
filename = info->path;
} else {
filename++;
}
/* name */
len = strlen(name);
rev = strchr(filename, '@');
dot = strrchr(info->path, '.');
if (strncmp(filename, name, len) ||
((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) {
LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, name);
}
/* revision */
if (rev) {
len = dot - ++rev;
if (!revs || (len != LY_REV_SIZE - 1) || strncmp(revs[0].date, rev, len)) {
LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
revs ? revs[0].date : "none");
}
}
ly_sanity_check_module_filename(ctx, name, revs ? revs[0].date : NULL, info->path);
}
return LY_SUCCESS;
}
Expand Down Expand Up @@ -2613,3 +2590,33 @@ lys_stmt_flags(enum ly_stmt stmt)

return 0;
}

void ly_sanity_check_module_filename(const struct ly_ctx *ctx, const char *name, const char *revision, const char *filename)
{
const char *basename, *rev, *dot;
size_t len;

/* check that name and revision match filename */
basename = strrchr(filename, '/');
if (!basename) {
basename = filename;
} else {
basename++; /* leading slash */
}
rev = strchr(basename, '@');
dot = strrchr(basename, '.');

/* name */
len = strlen(name);
if (strncmp(basename, name, len) ||
((rev && (rev != &basename[len])) || (!rev && (dot != &basename[len])))) {
LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", basename, name);
}
if (rev) {
len = dot - ++rev;
if (!revision || (len != LY_REV_SIZE - 1) || strncmp(revision, rev, len)) {
LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", basename,
revision ? revision : "none");
}
}
}
2 changes: 2 additions & 0 deletions src/tree_schema_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -732,4 +732,6 @@ uint8_t lys_stmt_flags(enum ly_stmt stmt);
*/
LY_ERR lyplg_ext_get_storage_p(const struct lysc_ext_instance *ext, int stmt, const void ***storage_p);

void ly_sanity_check_module_filename(const struct ly_ctx *ctx, const char *name, const char *revision, const char *filename);

#endif /* LY_TREE_SCHEMA_INTERNAL_H_ */

0 comments on commit 545f724

Please sign in to comment.