commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject svn commit: r1300447 - in /commons/proper/io/trunk/src: changes/changes.xml main/java/org/apache/commons/io/FileUtils.java test/java/org/apache/commons/io/FileUtilsTestCase.java
Date Wed, 14 Mar 2012 02:48:08 GMT
Author: sebb
Date: Wed Mar 14 02:48:08 2012
New Revision: 1300447

URL: http://svn.apache.org/viewvc?rev=1300447&view=rev
Log:
IO-300 FileUtils.moveDirectoryToDirectory removes source directory if destination is a subdirectory

Modified:
    commons/proper/io/trunk/src/changes/changes.xml
    commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java
    commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java

Modified: commons/proper/io/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/changes/changes.xml?rev=1300447&r1=1300446&r2=1300447&view=diff
==============================================================================
--- commons/proper/io/trunk/src/changes/changes.xml (original)
+++ commons/proper/io/trunk/src/changes/changes.xml Wed Mar 14 02:48:08 2012
@@ -40,6 +40,9 @@ The <action> type attribute can be add,u
 
   <body>
     <release version="2.2" date="TBA">
+      <action issue="IO-300" dev="sebb" type="fix">
+        FileUtils.moveDirectoryToDirectory removes source directory if destination is a subdirectory
+      </action>        
       <action issue="IO-307" dev="sebb" type="fix">
         ReaderInputStream#read(byte[] b, int off, int len) should check for valid parameters
       </action>        

Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java?rev=1300447&r1=1300446&r2=1300447&view=diff
==============================================================================
--- commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java (original)
+++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java Wed Mar 14
02:48:08 2012
@@ -32,6 +32,7 @@ import java.net.URLConnection;
 import java.nio.ByteBuffer;
 import java.nio.channels.FileChannel;
 import java.nio.charset.Charset;
+import java.security.InvalidParameterException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Date;
@@ -2491,6 +2492,9 @@ public class FileUtils {
         }
         boolean rename = srcDir.renameTo(destDir);
         if (!rename) {
+            if (destDir.getCanonicalPath().startsWith(srcDir.getCanonicalPath())) {
+                throw new IOException("Cannot move directory: "+srcDir+" to a subdirectory
of itself: "+destDir);
+            }
             copyDirectory( srcDir, destDir );
             deleteDirectory( srcDir );
             if (srcDir.exists()) {

Modified: commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java?rev=1300447&r1=1300446&r2=1300447&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java (original)
+++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java Wed
Mar 14 02:48:08 2012
@@ -2310,6 +2310,21 @@ public class FileUtilsTestCase extends F
         }
     }
 
+    public void testIO300() throws Exception {
+        final File testDirectory = getTestDirectory();
+        File src = new File(testDirectory, "dir1");
+        File dest = new File(src,"dir2");
+        assertTrue(dest.mkdirs());
+        assertTrue(src.exists());
+        try {
+            FileUtils.moveDirectoryToDirectory(src, dest, false);
+            fail("expected IOException");
+        } catch (IOException ioe) {
+            // expected
+        }
+        assertTrue(src.exists());
+    }
+
     public void testIO276() throws Exception {
         File dir = new File("target", "IO276");
         assertTrue(dir+" should not be present",dir.mkdirs());



Mime
View raw message