Skip to content

Commit 291a35e

Browse files
committed
IO-672 - FileCopy sets date to 01 Jan 1970
1 parent aefc8f4 commit 291a35e

3 files changed

Lines changed: 24 additions & 20 deletions

File tree

src/changes/changes.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,12 @@ The <action> type attribute can be add,update,fix,remove.
116116
<action dev="ggregory" type="update" due-to="Dependabot">
117117
Update spotbugs from 4.0.6 to 4.1.1 #134.
118118
</action>
119-
<action dev="sebb" type="add">
119+
<action issue="IO-681" dev="sebb" type="add">
120120
IO-681 IOUtils.close(Closeable) should allow a list of closeables
121121
</action>
122+
<action issue="IO-672" dev="sebb" type="fix">
123+
Copying a File sets last modified date to 01 January 1970
124+
</action>
122125
</release>
123126
<!-- The release date is the date RC is cut -->
124127
<release version="2.7" date="2020-05-24" description="Java 8 required.">

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,15 +1394,17 @@ private static boolean doCopyFile(final File srcFile, final File destFile, final
13941394

13951395
final Path srcPath = srcFile.toPath();
13961396
final Path destPath = destFile.toPath();
1397-
final long newLastModifed = preserveFileDate ? srcFile.lastModified() : destFile.lastModified();
13981397
Files.copy(srcPath, destPath, copyOptions);
13991398

14001399
// TODO IO-386: Do we still need this check?
14011400
checkEqualSizes(srcFile, destFile, Files.size(srcPath), Files.size(destPath));
14021401
// TODO IO-386: Do we still need this check?
14031402
checkEqualSizes(srcFile, destFile, srcFile.length(), destFile.length());
14041403

1405-
return destFile.setLastModified(newLastModifed);
1404+
if (preserveFileDate) {
1405+
return destFile.setLastModified(srcFile.lastModified());
1406+
}
1407+
return true;
14061408
}
14071409

14081410
//-----------------------------------------------------------------------

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

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
2020
import static org.junit.jupiter.api.Assertions.assertEquals;
2121
import static org.junit.jupiter.api.Assertions.assertFalse;
22+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
2223
import static org.junit.jupiter.api.Assertions.assertNotNull;
2324
import static org.junit.jupiter.api.Assertions.assertNull;
2425
import static org.junit.jupiter.api.Assertions.assertSame;
@@ -1144,16 +1145,12 @@ public void testIsFileNewerOlder() throws Exception {
11441145
public void testCopyFile1() throws Exception {
11451146
final File destination = new File(temporaryFolder, "copy1.txt");
11461147

1147-
//Thread.sleep(LAST_MODIFIED_DELAY);
1148-
//This is to slow things down so we can catch if
1149-
//the lastModified date is not ok
1148+
backDateFile(testFile1); // set test file back 10 minutes
11501149

11511150
FileUtils.copyFile(testFile1, destination);
11521151
assertTrue(destination.exists(), "Check Exist");
11531152
assertEquals(testFile1Size, destination.length(), "Check Full copy");
1154-
/* disabled: Thread.sleep doesn't work reliantly for this case
1155-
assertTrue("Check last modified date preserved",
1156-
testFile1.lastModified() == destination.lastModified());*/
1153+
assertEquals(testFile1.lastModified(), destination.lastModified(), "Check last modified date preserved");
11571154
}
11581155

11591156
@Test
@@ -1196,15 +1193,12 @@ public void testCopyFileLarge() throws Exception {
11961193
public void testCopyFile2() throws Exception {
11971194
final File destination = new File(temporaryFolder, "copy2.txt");
11981195

1199-
//Thread.sleep(LAST_MODIFIED_DELAY);
1200-
//This is to slow things down so we can catch if
1201-
//the lastModified date is not ok
1196+
backDateFile(testFile1); // set test file back 10 minutes
12021197

12031198
FileUtils.copyFile(testFile1, destination);
12041199
assertTrue(destination.exists(), "Check Exist");
12051200
assertEquals(testFile2Size, destination.length(), "Check Full copy");
1206-
/* disabled: Thread.sleep doesn't work reliably for this case
1207-
assertTrue(testFile1.lastModified() == destination.lastModified(), "Check last modified date preserved");*/
1201+
assertEquals(testFile1.lastModified() , destination.lastModified(), "Check last modified date preserved");
12081202
}
12091203

12101204
@Test
@@ -1225,16 +1219,15 @@ public void testCopyToSelf() throws Exception {
12251219
public void testCopyFile2WithoutFileDatePreservation() throws Exception {
12261220
final File destination = new File(temporaryFolder, "copy2.txt");
12271221

1228-
//Thread.sleep(LAST_MODIFIED_DELAY);
1229-
//This is to slow things down so we can catch if
1230-
//the lastModified date is not ok
1222+
backDateFile(testFile1); // set test file back 10 minutes
12311223

1224+
final long now = new Date().getTime()-1000L; // destination file time should not be less than this (allowing for granularity)
12321225
FileUtils.copyFile(testFile1, destination, false);
12331226
assertTrue(destination.exists(), "Check Exist");
12341227
assertEquals(testFile2Size, destination.length(), "Check Full copy");
1235-
/* disabled: Thread.sleep doesn't work reliantly for this case
1236-
assertTrue("Check last modified date modified",
1237-
testFile1.lastModified() != destination.lastModified());*/
1228+
final long destLastMod = destination.lastModified();
1229+
assertNotEquals(testFile1.lastModified(), destLastMod, "Check last modified date not same as input");
1230+
assertTrue(destLastMod > now, destLastMod + " > " + now);
12381231
}
12391232

12401233
@Test
@@ -3083,6 +3076,12 @@ public void testIncorrectOutputSize() throws Exception {
30833076
}
30843077
}
30853078

3079+
private void backDateFile(File testFile){
3080+
final long mins10 = 1000*60*10;
3081+
final long lastModified1 = testFile.lastModified();
3082+
testFile.setLastModified(lastModified1-mins10);
3083+
assertNotEquals(testFile.lastModified(), lastModified1, "Should have changed source date"); // ensure it was changed
3084+
}
30863085
/**
30873086
* DirectoryWalker implementation that recursively lists all files and directories.
30883087
*/

0 commit comments

Comments
 (0)