Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Predicate;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -130,7 +131,35 @@ protected Stream<Path> listJdkPaths() throws IOException {
}

protected boolean acceptFolder(@NonNull Path jdkFolder) {
return jdkFolder.startsWith(jdksRoot) && JavaUtils.hasJavacCmd(jdkFolder);
return isUnderRoot(jdkFolder) && JavaUtils.hasJavacCmd(jdkFolder);
}

/**
* Checks if the given path is under the jdksRoot directory. This method handles
* the case where the paths may have different canonical forms (e.g. /var vs
* /private/var on macOS) by falling back to toRealPath() comparison when a
* simple startsWith check fails.
*/
protected boolean isUnderRoot(@NonNull Path jdkFolder) {
if (jdkFolder.startsWith(jdksRoot)) {
return true;
}
try {
return jdkFolder.toRealPath().startsWith(realJdksRoot());
} catch (IOException e) {
return false;
}
}

private final AtomicReference<Path> realJdksRootCache = new AtomicReference<>();

private Path realJdksRoot() throws IOException {
Path cached = realJdksRootCache.get();
if (cached == null) {
cached = jdksRoot.toRealPath();
realJdksRootCache.set(cached);
}
return cached;
}

private final Pattern validId = Pattern.compile("^[a-zA-Z0-9._+-]+$");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ protected Path getJdkPath(@NonNull String id) {
}

protected boolean acceptFolder(@NonNull Path jdkFolder) {
if (!jdkFolder.equals(defaultJdkLink) && !jdkFolder.startsWith(jdksRoot)) {
if (!jdkFolder.equals(defaultJdkLink) && !isUnderRoot(jdkFolder)) {
return false;
}
String nm = jdkFolder.getFileName().toString();
Expand Down