From 90f780e9d27bca71115dbe3531aa9236bfa4999d Mon Sep 17 00:00:00 2001 From: Michael Barker Date: Fri, 13 Sep 2024 10:15:29 +1200 Subject: [PATCH] [Java] More robust checking of symbolic links. --- .../java/io/aeron/test/DataCollector.java | 43 ++++++++++--------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/aeron-test-support/src/main/java/io/aeron/test/DataCollector.java b/aeron-test-support/src/main/java/io/aeron/test/DataCollector.java index 7d858c3503..a96a6f2a73 100644 --- a/aeron-test-support/src/main/java/io/aeron/test/DataCollector.java +++ b/aeron-test-support/src/main/java/io/aeron/test/DataCollector.java @@ -22,7 +22,6 @@ import java.io.File; import java.io.FileFilter; import java.io.IOException; -import java.io.UncheckedIOException; import java.nio.file.*; import java.nio.file.attribute.BasicFileAttributes; import java.util.*; @@ -205,36 +204,40 @@ private List findMatchingFiles(final FileFilter filter) private static void find(final List found, final File file, final FileFilter filter) { - try + if (existsAndIsNotSymbolicLink(file)) { - if (file.exists()) + if (file.isFile()) { - final BasicFileAttributes basicFileAttributes = Files.readAttributes( - file.toPath(), BasicFileAttributes.class, NOFOLLOW_LINKS); - - if (file.isFile()) + if (filter.accept(file)) { - if (filter.accept(file)) - { - found.add(file.toPath()); - } + found.add(file.toPath()); } - else if (!basicFileAttributes.isSymbolicLink() && file.isDirectory()) + } + else if (file.isDirectory()) + { + final File[] files = file.listFiles(); + if (null != files) { - final File[] files = file.listFiles(); - if (null != files) + for (final File f : files) { - for (final File f : files) - { - find(found, f, filter); - } + find(found, f, filter); } } } } - catch (final IOException ex) + } + + private static boolean existsAndIsNotSymbolicLink(final File file) + { + try + { + final BasicFileAttributes basicFileAttributes = Files.readAttributes( + file.toPath(), BasicFileAttributes.class, NOFOLLOW_LINKS); + return !basicFileAttributes.isSymbolicLink() && file.exists(); + } + catch (IOException e) { - throw new UncheckedIOException(ex); + return false; } }