Skip to content

Commit

Permalink
try fix drive case error
Browse files Browse the repository at this point in the history
  • Loading branch information
brachy84 committed Aug 8, 2024
1 parent 098abf0 commit 2e85379
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/main/java/com/cleanroommc/groovyscript/sandbox/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,26 @@ public static String relativize(String rootPath, String longerThanRootPath) {
if (File.separatorChar != '/') {
longerThanRootPath = longerThanRootPath.replace('/', File.separatorChar);
}
return relativizeInternal(fixDriveCase(rootPath), fixDriveCase(longerThanRootPath));
}

private static String relativizeInternal(String rootPath, String longerThanRootPath) {
int index = longerThanRootPath.indexOf(rootPath);
if (index < 0) {
throw new IllegalArgumentException("The path '" + longerThanRootPath + "' does not contain the root path '" + rootPath + "'");
}
return longerThanRootPath.substring(index + rootPath.length() + 1);
}

// sometimes the paths passed to relativize() have a lower case drive letter
public static String fixDriveCase(String path) {
if (path == null || path.length() < 2) return path;
if (Character.isLowerCase(path.charAt(0)) && path.charAt(1) == ':') {
return Character.toUpperCase(path.charAt(0)) + ':' + path.substring(2);
}
return path;
}

public static String getParent(String path) {
int i = path.lastIndexOf(File.separatorChar);
if (i <= 0) return StringUtils.EMPTY;
Expand Down

0 comments on commit 2e85379

Please sign in to comment.