Skip to content

Commit 026737b

Browse files
jktjktmichalvasko
authored andcommitted
refactor: one place to verify correct schema file name
1 parent 4f6af88 commit 026737b

File tree

3 files changed

+44
-51
lines changed

3 files changed

+44
-51
lines changed

src/tree_schema.c

+1-26
Original file line numberDiff line numberDiff line change
@@ -1749,8 +1749,6 @@ lys_parse_in(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format,
17491749
struct lysp_yin_ctx *yinctx = NULL;
17501750
struct lysp_ctx *pctx = NULL;
17511751
struct lysf_ctx fctx = {.ctx = ctx};
1752-
char *filename, *rev, *dot;
1753-
size_t len;
17541752
ly_bool module_created = 0;
17551753

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

18321830
switch (in->type) {
18331831
case LY_IN_FILEPATH:
1834-
/* check that name and revision match filename */
1835-
filename = strrchr(in->method.fpath.filepath, '/');
1836-
if (!filename) {
1837-
filename = in->method.fpath.filepath;
1838-
} else {
1839-
filename++;
1840-
}
1841-
rev = strchr(filename, '@');
1842-
dot = strrchr(filename, '.');
1843-
1844-
/* name */
1845-
len = strlen(mod->name);
1846-
if (strncmp(filename, mod->name, len) ||
1847-
((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) {
1848-
LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name);
1849-
}
1850-
if (rev) {
1851-
len = dot - ++rev;
1852-
if (!mod->parsed->revs || (len != LY_REV_SIZE - 1) || strncmp(mod->parsed->revs[0].date, rev, len)) {
1853-
LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
1854-
mod->parsed->revs ? mod->parsed->revs[0].date : "none");
1855-
}
1856-
}
1857-
1832+
ly_check_module_filename(ctx, mod->name, mod->parsed->revs ? mod->parsed->revs[0].date : NULL, in->method.fpath.filepath);
18581833
break;
18591834
case LY_IN_FD:
18601835
case LY_IN_FILE:

src/tree_schema_common.c

+33-25
Original file line numberDiff line numberDiff line change
@@ -696,9 +696,8 @@ static LY_ERR
696696
lysp_load_module_check(const struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data)
697697
{
698698
struct lysp_load_module_check_data *info = data;
699-
const char *filename, *dot, *rev, *name;
699+
const char *name;
700700
uint8_t latest_revision;
701-
size_t len;
702701
struct lysp_revision *revs;
703702

704703
name = mod ? mod->mod->name : submod->name;
@@ -739,29 +738,7 @@ lysp_load_module_check(const struct ly_ctx *ctx, struct lysp_module *mod, struct
739738
}
740739
}
741740
if (info->path) {
742-
/* check that name and revision match filename */
743-
filename = strrchr(info->path, '/');
744-
if (!filename) {
745-
filename = info->path;
746-
} else {
747-
filename++;
748-
}
749-
/* name */
750-
len = strlen(name);
751-
rev = strchr(filename, '@');
752-
dot = strrchr(info->path, '.');
753-
if (strncmp(filename, name, len) ||
754-
((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) {
755-
LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, name);
756-
}
757-
/* revision */
758-
if (rev) {
759-
len = dot - ++rev;
760-
if (!revs || (len != LY_REV_SIZE - 1) || strncmp(revs[0].date, rev, len)) {
761-
LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
762-
revs ? revs[0].date : "none");
763-
}
764-
}
741+
ly_check_module_filename(ctx, name, revs ? revs[0].date : NULL, info->path);
765742
}
766743
return LY_SUCCESS;
767744
}
@@ -2613,3 +2590,34 @@ lys_stmt_flags(enum ly_stmt stmt)
26132590

26142591
return 0;
26152592
}
2593+
2594+
void
2595+
ly_check_module_filename(const struct ly_ctx *ctx, const char *name, const char *revision, const char *filename)
2596+
{
2597+
const char *basename, *rev, *dot;
2598+
size_t len;
2599+
2600+
/* check that name and revision match filename */
2601+
basename = strrchr(filename, '/');
2602+
if (!basename) {
2603+
basename = filename;
2604+
} else {
2605+
basename++; /* leading slash */
2606+
}
2607+
rev = strchr(basename, '@');
2608+
dot = strrchr(basename, '.');
2609+
2610+
/* name */
2611+
len = strlen(name);
2612+
if (strncmp(basename, name, len) ||
2613+
((rev && (rev != &basename[len])) || (!rev && (dot != &basename[len])))) {
2614+
LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", basename, name);
2615+
}
2616+
if (rev) {
2617+
len = dot - ++rev;
2618+
if (!revision || (len != LY_REV_SIZE - 1) || strncmp(revision, rev, len)) {
2619+
LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", basename,
2620+
revision ? revision : "none");
2621+
}
2622+
}
2623+
}

src/tree_schema_internal.h

+10
Original file line numberDiff line numberDiff line change
@@ -732,4 +732,14 @@ uint8_t lys_stmt_flags(enum ly_stmt stmt);
732732
*/
733733
LY_ERR lyplg_ext_get_storage_p(const struct lysc_ext_instance *ext, int stmt, const void ***storage_p);
734734

735+
/**
736+
* @brief Warning if the filename does not match the expected module name and version
737+
*
738+
* @param[in] ctx Context for logging
739+
* @param[in] name Expected module name
740+
* @param[in] revision Expected module revision, or NULL if not to be checked
741+
* @param[in] filename File path to be checked
742+
*/
743+
void ly_check_module_filename(const struct ly_ctx *ctx, const char *name, const char *revision, const char *filename);
744+
735745
#endif /* LY_TREE_SCHEMA_INTERNAL_H_ */

0 commit comments

Comments
 (0)