Skip to content

Commit 4286199

Browse files
author
Niall Pemberton
committed
IO-217 FileUtils.copyDirectoryToDirectory makes infinite loops - thanks to Roun Lee for the patch
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1002424 13f79535-47bb-0310-9956-ffa450edef68
1 parent 4353d48 commit 4286199

2 files changed

Lines changed: 13 additions & 5 deletions

File tree

src/java/org/apache/commons/io/FileUtils.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,11 @@ public static void copyDirectory(File srcDir, File destDir,
10191019
*/
10201020
private static void doCopyDirectory(File srcDir, File destDir, FileFilter filter,
10211021
boolean preserveFileDate, List<String> exclusionList) throws IOException {
1022+
// recurse
1023+
File[] files = filter == null ? srcDir.listFiles() : srcDir.listFiles(filter);
1024+
if (files == null) { // null if security restricted
1025+
throw new IOException("Failed to list contents of " + srcDir);
1026+
}
10221027
if (destDir.exists()) {
10231028
if (destDir.isDirectory() == false) {
10241029
throw new IOException("Destination '" + destDir + "' exists but is not a directory");
@@ -1034,11 +1039,6 @@ private static void doCopyDirectory(File srcDir, File destDir, FileFilter filter
10341039
if (destDir.canWrite() == false) {
10351040
throw new IOException("Destination '" + destDir + "' cannot be written to");
10361041
}
1037-
// recurse
1038-
File[] files = filter == null ? srcDir.listFiles() : srcDir.listFiles(filter);
1039-
if (files == null) { // null if security restricted
1040-
throw new IOException("Failed to list contents of " + srcDir);
1041-
}
10421042
for (File file : files) {
10431043
File copiedFile = new File(destDir, file.getName());
10441044
if (exclusionList == null || !exclusionList.contains(file.getCanonicalPath())) {

src/test/org/apache/commons/io/FileUtilsTestCase.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,14 @@ public void testCopyDirectoryToGrandChild() throws Exception {
905905
assertEquals(expectedSize, FileUtils.sizeOfDirectory(grandParentDir));
906906
}
907907

908+
/** Test for IO-217 FileUtils.copyDirectoryToDirectory makes infinite loops */
909+
public void testCopyDirectoryToItself() throws Exception {
910+
File dir = new File(getTestDirectory(), "itself");
911+
dir.mkdirs();
912+
FileUtils.copyDirectoryToDirectory(dir, dir);
913+
assertEquals(1, LIST_WALKER.list(dir).size());
914+
}
915+
908916
private void createFilesForTestCopyDirectory(File grandParentDir, File parentDir, File childDir) throws Exception {
909917
File childDir2 = new File(parentDir, "child2");
910918
File grandChildDir = new File(childDir, "grandChild");

0 commit comments

Comments
 (0)