Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows: accept backslashes in file names #2093

Merged
merged 2 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_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
65 changes: 40 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_check_module_filename(ctx, name, revs ? revs[0].date : NULL, info->path);
}
return LY_SUCCESS;
}
Expand Down Expand Up @@ -2613,3 +2590,41 @@ lys_stmt_flags(enum ly_stmt stmt)

return 0;
}

void
ly_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, '/');
#ifdef _WIN32
const char *backslash = strrchr(filename, '\\');

if (!basename || (basename && backslash && (backslash > basename))) {
basename = backslash;
}
#endif
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");
}
}
}
10 changes: 10 additions & 0 deletions src/tree_schema_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -732,4 +732,14 @@ 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);

/**
* @brief Warning if the filename does not match the expected module name and version
*
* @param[in] ctx Context for logging
* @param[in] name Expected module name
* @param[in] revision Expected module revision, or NULL if not to be checked
* @param[in] filename File path to be checked
*/
void ly_check_module_filename(const struct ly_ctx *ctx, const char *name, const char *revision, const char *filename);
jktjkt marked this conversation as resolved.
Show resolved Hide resolved

#endif /* LY_TREE_SCHEMA_INTERNAL_H_ */
1 change: 1 addition & 0 deletions tools/lint/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ parse_schema_path(const char *path, char **dir, char **module)

/* split the path to dirname and basename for further work */
*dir = strdup(path);
/* FIXME: this is broken on Windows */

Check notice

Code scanning / CodeQL

FIXME comment

FIXME comment: this is broken on Windows
*module = strrchr(*dir, '/');
if (!(*module)) {
*module = *dir;
Expand Down