From 3689b3fdd56df2a54d8d76d71689f946d08ab1f4 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 3 Apr 2020 21:46:07 +0200 Subject: [PATCH] Recursively copy directory with symbolic link Closes gh-24823 --- .../org/springframework/util/FileSystemUtils.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 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 70af393bc01f..1a532aa15925 100644 --- a/spring-core/src/main/java/org/springframework/util/FileSystemUtils.java +++ b/spring-core/src/main/java/org/springframework/util/FileSystemUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,9 +24,12 @@ import java.nio.file.SimpleFileVisitor; import java.nio.file.StandardCopyOption; import java.nio.file.attribute.BasicFileAttributes; +import java.util.EnumSet; import org.springframework.lang.Nullable; +import static java.nio.file.FileVisitOption.FOLLOW_LINKS; + /** * Utility methods for working with the file system. * @@ -65,11 +68,11 @@ public static boolean deleteRecursively(@Nullable File root) { } /** - * Delete the supplied {@link File} - for directories, + * Delete the supplied {@link File} — for directories, * recursively delete any nested directories or files as well. * @param root the root {@code File} to delete * @return {@code true} if the {@code File} existed and was deleted, - * or {@code false} it it did not exist + * or {@code false} if it did not exist * @throws IOException in the case of I/O errors * @since 5.0 */ @@ -123,7 +126,7 @@ public static void copyRecursively(Path src, Path dest) throws IOException { BasicFileAttributes srcAttr = Files.readAttributes(src, BasicFileAttributes.class); if (srcAttr.isDirectory()) { - Files.walkFileTree(src, new SimpleFileVisitor() { + Files.walkFileTree(src, EnumSet.of(FOLLOW_LINKS), Integer.MAX_VALUE, new SimpleFileVisitor() { @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { Files.createDirectories(dest.resolve(src.relativize(dir)));