FileCopy sets date to 01 Jan 1970
MODIFY
src/changes/changes.xml
src/changes/changes.xml
@@ -116,9 +116,12 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="update" due-to="Dependabot">
Update spotbugs from 4.0.6 to 4.1.1 #134.
</action>
- <action dev="sebb" type="add">
+ <action issue="IO-681" dev="sebb" type="add">
IO-681 IOUtils.close(Closeable) should allow a list of closeables
</action>
+ <action issue="IO-672" dev="sebb" type="fix">
+ Copying a File sets last modified date to 01 January 1970
+ </action>
</release>
<!-- The release date is the date RC is cut -->
<release version="2.7" date="2020-05-24" description="Java 8 required.">

MODIFY
src/main/java/org/apache/commons/io/FileUtils.java
src/main/java/org/apache/commons/io/FileUtils.java
@@ -1394,7 +1394,6 @@ private static boolean doCopyFile(final File srcFile, final File destFile, final
final Path srcPath = srcFile.toPath();
final Path destPath = destFile.toPath();
- final long newLastModifed = preserveFileDate ? srcFile.lastModified() : destFile.lastModified();
Files.copy(srcPath, destPath, copyOptions);
// TODO IO-386: Do we still need this check?
@@ -1402,7 +1401,10 @@ private static boolean doCopyFile(final File srcFile, final File destFile, final
// TODO IO-386: Do we still need this check?
checkEqualSizes(srcFile, destFile, srcFile.length(), destFile.length());
- return destFile.setLastModified(newLastModifed);
+ if (preserveFileDate) {
+ return destFile.setLastModified(srcFile.lastModified());
+ }
+ return true;
}
//-----------------------------------------------------------------------

MODIFY
src/test/java/org/apache/commons/io/FileUtilsTestCase.java
src/test/java/org/apache/commons/io/FileUtilsTestCase.java
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
@@ -1144,16 +1145,12 @@ public void testIsFileNewerOlder() throws Exception {
public void testCopyFile1() throws Exception {
final File destination = new File(temporaryFolder, "copy1.txt");
- //Thread.sleep(LAST_MODIFIED_DELAY);
- //This is to slow things down so we can catch if
- //the lastModified date is not ok
+ backDateFile(testFile1); // set test file back 10 minutes
FileUtils.copyFile(testFile1, destination);
assertTrue(destination.exists(), "Check Exist");
assertEquals(testFile1Size, destination.length(), "Check Full copy");
- /* disabled: Thread.sleep doesn't work reliantly for this case
- assertTrue("Check last modified date preserved",
- testFile1.lastModified() == destination.lastModified());*/
+ assertEquals(testFile1.lastModified(), destination.lastModified(), "Check last modified date preserved");
}
@Test
@@ -1196,15 +1193,12 @@ public void testCopyFileLarge() throws Exception {
public void testCopyFile2() throws Exception {
final File destination = new File(temporaryFolder, "copy2.txt");
- //Thread.sleep(LAST_MODIFIED_DELAY);
- //This is to slow things down so we can catch if
- //the lastModified date is not ok
+ backDateFile(testFile1); // set test file back 10 minutes
FileUtils.copyFile(testFile1, destination);
assertTrue(destination.exists(), "Check Exist");
assertEquals(testFile2Size, destination.length(), "Check Full copy");
- /* disabled: Thread.sleep doesn't work reliably for this case
- assertTrue(testFile1.lastModified() == destination.lastModified(), "Check last modified date preserved");*/
+ assertEquals(testFile1.lastModified() , destination.lastModified(), "Check last modified date preserved");
}
@Test
@@ -1225,16 +1219,15 @@ public void testCopyToSelf() throws Exception {
public void testCopyFile2WithoutFileDatePreservation() throws Exception {
final File destination = new File(temporaryFolder, "copy2.txt");
- //Thread.sleep(LAST_MODIFIED_DELAY);
- //This is to slow things down so we can catch if
- //the lastModified date is not ok
+ backDateFile(testFile1); // set test file back 10 minutes
+ final long now = new Date().getTime()-1000L; // destination file time should not be less than this (allowing for granularity)
FileUtils.copyFile(testFile1, destination, false);
assertTrue(destination.exists(), "Check Exist");
assertEquals(testFile2Size, destination.length(), "Check Full copy");
- /* disabled: Thread.sleep doesn't work reliantly for this case
- assertTrue("Check last modified date modified",
- testFile1.lastModified() != destination.lastModified());*/
+ final long destLastMod = destination.lastModified();
+ assertNotEquals(testFile1.lastModified(), destLastMod, "Check last modified date not same as input");
+ assertTrue(destLastMod > now, destLastMod + " > " + now);
}
@Test
@@ -3083,6 +3076,12 @@ public void testIncorrectOutputSize() throws Exception {
}
}
+ private void backDateFile(File testFile){
+ final long mins10 = 1000*60*10;
+ final long lastModified1 = testFile.lastModified();
+ testFile.setLastModified(lastModified1-mins10);
+ assertNotEquals(testFile.lastModified(), lastModified1, "Should have changed source date"); // ensure it was changed
+ }
/**
* DirectoryWalker implementation that recursively lists all files and directories.
*/

IO-672 - FileCopy sets date to 01 Jan 1970