commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject [commons-io] branch master updated: IO-672 - FileCopy sets date to 01 Jan 1970
Date Thu, 06 Aug 2020 23:27:42 GMT
This is an automated email from the ASF dual-hosted git repository.

sebb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git


The following commit(s) were added to refs/heads/master by this push:
     new 291a35e  IO-672 - FileCopy sets date to 01 Jan 1970
291a35e is described below

commit 291a35ef8be015be252664e2cbde0af741cd82f1
Author: Sebb <sebb@apache.org>
AuthorDate: Fri Aug 7 00:27:32 2020 +0100

    IO-672 - FileCopy sets date to 01 Jan 1970
---
 src/changes/changes.xml                            |  5 +++-
 src/main/java/org/apache/commons/io/FileUtils.java |  6 ++--
 .../org/apache/commons/io/FileUtilsTestCase.java   | 33 +++++++++++-----------
 3 files changed, 24 insertions(+), 20 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 6fcb369..df461c5 100644
--- a/src/changes/changes.xml
+++ b/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.">
diff --git a/src/main/java/org/apache/commons/io/FileUtils.java b/src/main/java/org/apache/commons/io/FileUtils.java
index 1586ead..7bee5bf 100644
--- a/src/main/java/org/apache/commons/io/FileUtils.java
+++ b/src/main/java/org/apache/commons/io/FileUtils.java
@@ -1394,7 +1394,6 @@ public class FileUtils {
 
         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 @@ public class FileUtils {
         // 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;
     }
 
     //-----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/io/FileUtilsTestCase.java b/src/test/java/org/apache/commons/io/FileUtilsTestCase.java
index 95f5343..92bddba 100644
--- a/src/test/java/org/apache/commons/io/FileUtilsTestCase.java
+++ b/src/test/java/org/apache/commons/io/FileUtilsTestCase.java
@@ -19,6 +19,7 @@ package org.apache.commons.io;
 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 class FileUtilsTestCase {
     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 class FileUtilsTestCase {
     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 class FileUtilsTestCase {
     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 class FileUtilsTestCase {
         }
     }
 
+    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.
      */


Mime
View raw message