Skip to content

Commit 4127f85

Browse files
committed
Bugzilla 28496
Fix for: org.apache.commons.io.FileUtils.copyFile shouldn't allow to copy a file on itself Submitted by: dany rizzi <drizzi.at.largesys.it> git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@140558 13f79535-47bb-0310-9956-ffa450edef68
1 parent eebc440 commit 4127f85

2 files changed

Lines changed: 31 additions & 3 deletions

File tree

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
* @author <a href="mailto:jefft@apache.org">Jeff Turner</a>
7979
* @author Matthew Hawthorne
8080
* @author <a href="mailto:jeremias@apache.org">Jeremias Maerki</a>
81-
* @version $Id: FileUtils.java,v 1.29 2004/03/12 22:21:38 jeremias Exp $
81+
* @version $Id: FileUtils.java,v 1.30 2004/04/23 22:47:39 jeremias Exp $
8282
*/
8383
public class FileUtils {
8484

@@ -416,6 +416,13 @@ public static void copyFile(File source, File destination, boolean preserveFileD
416416
throw new IOException(message);
417417
}
418418

419+
//makes sure it is not the same file
420+
if (source.getCanonicalPath().equals(destination.getCanonicalPath())) {
421+
String message =
422+
"Unable to write file " + source + " on itself.";
423+
throw new IOException(message);
424+
}
425+
419426
FileInputStream input = new FileInputStream(source);
420427
try {
421428
FileOutputStream output = new FileOutputStream(destination);

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
*
3333
* @author Peter Donald
3434
* @author Matthew Hawthorne
35-
* @version $Id: FileUtilsTestCase.java,v 1.17 2004/04/23 22:30:42 jeremias Exp $
35+
* @version $Id: FileUtilsTestCase.java,v 1.18 2004/04/23 22:47:39 jeremias Exp $
3636
* @see FileUtils
3737
*/
3838
public class FileUtilsTestCase extends FileBasedTestCase {
@@ -180,6 +180,7 @@ public void testCopyURLToFile() throws Exception {
180180
} finally {
181181
fis.close();
182182
}
183+
//TODO Maybe test copy to itself like for copyFile()
183184
}
184185

185186
// forceMkdir
@@ -279,6 +280,19 @@ public void testCopyFile2() throws Exception {
279280
assertTrue("Check last modified date preserved",
280281
testFile1.lastModified() == destination.lastModified());
281282
}
283+
284+
public void testCopyToSelf() throws Exception {
285+
File destination = new File(getTestDirectory(), "copy3.txt");
286+
//Prepare a test file
287+
FileUtils.copyFile(testFile1, destination);
288+
289+
try {
290+
FileUtils.copyFile(destination, destination);
291+
fail("file copy to self should not be possible");
292+
} catch (IOException ioe) {
293+
//we want the exception, copy to self should be illegal
294+
}
295+
}
282296

283297
public void testCopyFile2WithoutFileDatePreservation() throws Exception {
284298
File destination = new File(getTestDirectory(), "copy2.txt");
@@ -330,7 +344,14 @@ public void testCopyFile1ToDir() throws Exception {
330344
assertTrue("Check Full copy", destination.length() == testFile1Size);
331345
/* disabled: Thread.sleep doesn't work reliantly for this case
332346
assertTrue("Check last modified date preserved",
333-
testFile1.lastModified() == destination.lastModified());*/
347+
testFile1.lastModified() == destination.lastModified());*/
348+
349+
try {
350+
FileUtils.copyFileToDirectory(destination, directory);
351+
fail("Should not be able to copy a file into the same directory as itself");
352+
} catch (IOException ioe) {
353+
//we want that, cannot copy to the same directory as the original file
354+
}
334355
}
335356

336357
public void testCopyFile2ToDir() throws Exception {

0 commit comments

Comments
 (0)