Skip to content

Commit b2a6b05

Browse files
CervatorBenjaminAmos
authored andcommitted
fix: potentially address subtle bug with file paths getting a trailing slash (#161)
1 parent d718423 commit b2a6b05

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

gestalt-module/src/main/java/org/terasology/gestalt/module/resources/ClasspathFileSource.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public Optional<FileReference> getFile(List<String> filepath) {
110110
if (filepath.stream().anyMatch(s -> s.equals(".."))) {
111111
return Optional.empty();
112112
}
113-
String fullpath = buildPathString(filepath);
113+
String fullpath = buildPathString(filepath, false);
114114
if (classLoader.getResource(fullpath) != null) {
115115
return Optional.of(new ClasspathSourceFileReference(fullpath, extractSubpath(basePath, fullpath), classLoader));
116116
} else {
@@ -120,7 +120,7 @@ public Optional<FileReference> getFile(List<String> filepath) {
120120

121121
@Override
122122
public Collection<FileReference> getFilesInPath(boolean recursive, List<String> path) {
123-
String fullPath = buildPathString(path);
123+
String fullPath = buildPathString(path, true);
124124
Stream<String> candidates = files
125125
.stream()
126126
.filter(file -> file.startsWith(fullPath))
@@ -137,7 +137,7 @@ public Collection<FileReference> getFilesInPath(boolean recursive, List<String>
137137

138138
@Override
139139
public Set<String> getSubpaths(List<String> path) {
140-
String fullPath = buildPathString(path);
140+
String fullPath = buildPathString(path, true);
141141
return files
142142
.stream()
143143
.filter(file -> file.startsWith(fullPath))
@@ -149,12 +149,15 @@ public Set<String> getSubpaths(List<String> path) {
149149
.collect(Collectors.toSet());
150150
}
151151

152-
private String buildPathString(List<String> path) {
152+
private String buildPathString(List<String> path, boolean isDirectory) {
153153
String fullPath;
154154
if (path.isEmpty() || (path.size() == 1 && path.get(0).isEmpty())) {
155155
fullPath = basePath;
156156
} else {
157-
fullPath = basePath + CLASS_PATH_JOINER.join(path) + CLASS_PATH_SEPARATOR;
157+
fullPath = basePath + CLASS_PATH_JOINER.join(path);
158+
if (isDirectory) {
159+
fullPath += CLASS_PATH_SEPARATOR;
160+
}
158161
}
159162
return fullPath;
160163
}

0 commit comments

Comments
 (0)