refactor: one place to verify correct schema file name
diff --git a/src/tree_schema.c b/src/tree_schema.c
index 7a119b9..baf2c46 100644
--- a/src/tree_schema.c
+++ b/src/tree_schema.c
@@ -1749,8 +1749,6 @@
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);
@@ -1831,30 +1829,7 @@
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:
diff --git a/src/tree_schema_common.c b/src/tree_schema_common.c
index a6e2036..b676cba 100644
--- a/src/tree_schema_common.c
+++ b/src/tree_schema_common.c
@@ -696,9 +696,8 @@
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;
@@ -739,29 +738,7 @@
}
}
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;
}
@@ -2613,3 +2590,34 @@
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, '/');
+ 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");
+ }
+ }
+}
diff --git a/src/tree_schema_internal.h b/src/tree_schema_internal.h
index 95dee0b..0a2ad3f 100644
--- a/src/tree_schema_internal.h
+++ b/src/tree_schema_internal.h
@@ -732,4 +732,14 @@
*/
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);
+
#endif /* LY_TREE_SCHEMA_INTERNAL_H_ */