From 1b23a9e119e21f4330fa05c671ca3c48404bc7af Mon Sep 17 00:00:00 2001 From: Matt Sicker Date: Mon, 17 Dec 2018 15:18:17 -0600 Subject: [PATCH] Fix symlink check logic to support junctions again Signed-off-by: Matt Sicker --- core/src/main/java/hudson/Util.java | 21 +++++++------------ .../java/jenkins/util/io/PathRemover.java | 2 +- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/core/src/main/java/hudson/Util.java b/core/src/main/java/hudson/Util.java index 1f266746aa..ea43cdb265 100644 --- a/core/src/main/java/hudson/Util.java +++ b/core/src/main/java/hudson/Util.java @@ -290,6 +290,11 @@ public class Util { * Checks if the given file represents a symlink. */ public static boolean isSymlink(@Nonnull File file) throws IOException { + return isSymlink(fileToPath(file)); + } + + @Restricted(NoExternalUse.class) + public static boolean isSymlink(@Nonnull Path path) { /* * Windows Directory Junctions are effectively the same as Linux symlinks to directories. * Unfortunately, the Java 7 NIO2 API function isSymbolicLink does not treat them as such. @@ -303,21 +308,11 @@ public class Util { * calling readAttributes. */ try { - Path path = fileToPath(file); - BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS); - if (attrs.isSymbolicLink()) { - return true; - } else if (attrs instanceof DosFileAttributes) { - /* Returns true for non-symbolic link reparse points and devices. We could call - * WindowsFileAttributes#isReparsePoint with reflection instead to exclude devices, - * but as mentioned in the above comment this does not appear to be an issue. - */ - return attrs.isOther(); - } else { - return false; - } + return path.compareTo(path.toRealPath()) != 0; } catch (NoSuchFileException e) { return false; + } catch (IOException e) { + throw new UncheckedIOException(e); } } diff --git a/core/src/main/java/jenkins/util/io/PathRemover.java b/core/src/main/java/jenkins/util/io/PathRemover.java index ff45760984..bc73bec8af 100644 --- a/core/src/main/java/jenkins/util/io/PathRemover.java +++ b/core/src/main/java/jenkins/util/io/PathRemover.java @@ -197,7 +197,7 @@ public class PathRemover { } private static List tryRemoveRecursive(@Nonnull Path path) { - List accumulatedErrors = Files.isSymbolicLink(path) ? new ArrayList<>() : + List accumulatedErrors = Util.isSymlink(path) ? new ArrayList<>() : tryRemoveDirectoryContents(path); tryRemoveFile(path).ifPresent(accumulatedErrors::add); return accumulatedErrors;