From fabc9c28d7e115431e25da81a0dca47787a683aa Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 3 Aug 2017 14:21:00 +0200 Subject: [PATCH] Align new FileSystemUtils NIO implementation with original behavior Issue: SPR-15845 Issue: SPR-15846 --- .../springframework/util/FileSystemUtils.java | 54 ++++++++++--------- 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/FileSystemUtils.java b/spring-core/src/main/java/org/springframework/util/FileSystemUtils.java index 96c5424d3f..4a1c133b4e 100644 --- a/spring-core/src/main/java/org/springframework/util/FileSystemUtils.java +++ b/spring-core/src/main/java/org/springframework/util/FileSystemUtils.java @@ -22,6 +22,7 @@ import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.SimpleFileVisitor; +import java.nio.file.StandardCopyOption; import java.nio.file.attribute.BasicFileAttributes; import org.springframework.lang.Nullable; @@ -51,15 +52,16 @@ public abstract class FileSystemUtils { * otherwise {@code false} */ public static boolean deleteRecursively(@Nullable File root) { - if (root != null) { - try { - return deleteRecursively(root.toPath()); - } - catch (IOException ex) { - return false; - } + if (root == null) { + return false; + } + + try { + return deleteRecursively(root.toPath()); + } + catch (IOException ex) { + return false; } - return false; } /** @@ -72,22 +74,26 @@ public abstract class FileSystemUtils { * @since 5.0 */ public static boolean deleteRecursively(@Nullable Path root) throws IOException { - if (root != null) { - Files.walkFileTree(root, new SimpleFileVisitor() { - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { - Files.delete(file); - return FileVisitResult.CONTINUE; - } - @Override - public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { - Files.delete(dir); - return FileVisitResult.CONTINUE; - } - }); - return Files.deleteIfExists(root); + if (root == null) { + return false; } - return false; + if (!Files.exists(root)) { + return false; + } + + Files.walkFileTree(root, new SimpleFileVisitor() { + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + Files.delete(file); + return FileVisitResult.CONTINUE; + } + @Override + public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { + Files.delete(dir); + return FileVisitResult.CONTINUE; + } + }); + return true; } /** @@ -125,7 +131,7 @@ public abstract class FileSystemUtils { } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { - Files.copy(file, dest.resolve(src.relativize(file))); + Files.copy(file, dest.resolve(src.relativize(file)), StandardCopyOption.REPLACE_EXISTING); return FileVisitResult.CONTINUE; } });