From a74d7f612134097408096369c448a87845ca0d2f Mon Sep 17 00:00:00 2001 From: Markus KARG Date: Wed, 15 Jun 2016 00:23:58 +0200 Subject: [PATCH] bug fix: silently fails overwriting symlinks When A is an existing symlink to B, then createSymbolicLink(A,C) does neither overwrite A->B by A->C (as expected in analogy to the behavior of copy(A,C)) nor does it throw an exception nor does it return A->B to indicate the failure, but it actually "silently fails", i. e. it returns A->C! This certainly is heavily problematic, unsymmetric to what copy(File,File) and Files.createSymbolicLink(Path,Path) do, and certainly unwanted and buggy behavior. The solution is to delete any existing target before creating the symlic, hence copying the behavior of copy(File,File). --- .../apache/maven/shared/utils/io/Java7Support.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/io/Java7Support.java b/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/io/Java7Support.java index 550de2f4f..867c0512f 100644 --- a/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/io/Java7Support.java +++ b/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/io/Java7Support.java @@ -40,6 +40,8 @@ public class Java7Support private static Method delete; + private static Method deleteIfExists; + private static Method toPath; private static Method exists; @@ -66,6 +68,7 @@ public class Java7Support Class linkOption = cl.loadClass( "java.nio.file.LinkOption" ); isSymbolicLink = files.getMethod( "isSymbolicLink", path ); delete = files.getMethod( "delete", path ); + deleteIfExists = files.getMethod( "deleteIfExists", path ); readSymlink = files.getMethod( "readSymbolicLink", path ); emptyFileAttributes = Array.newInstance( fa, 0 ); @@ -170,13 +173,10 @@ public static boolean exists( @Nonnull File file ) { try { - if ( !exists( symlink ) ) - { - Object link = toPath.invoke( symlink ); - Object path = createSymlink.invoke( null, link, toPath.invoke( target ), emptyFileAttributes ); - return (File) toFile.invoke( path ); - } - return symlink; + Object link = toPath.invoke( symlink ); + deleteIfExists.invoke( null, link ); + Object path = createSymlink.invoke( null, link, toPath.invoke( target ), emptyFileAttributes ); + return (File) toFile.invoke( path ); } catch ( IllegalAccessException e ) {