This is an automated email from the ASF dual-hosted git repository.

cstamas pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-resolver.git


The following commit(s) were added to refs/heads/master by this push:
     new 4f6b1f0c6 Keep the last modified time when a move falls back to a 
stream copy (#2319)
4f6b1f0c6 is described below

commit 4f6b1f0c6579e61cbefbaac70ebee2cd07369f13
Author: Alexander Bartash <[email protected]>
AuthorDate: Mon Nov 16 21:28:11 2026 +0200

    Keep the last modified time when a move falls back to a stream copy (#2319)
    
    Related:
    - https://github.com/apache/maven-resolver/pull/2318
    - https://github.com/apache/maven-resolver/pull/2319
    
    On Windows the final move of a collocated temp file falls back to a stream 
copy in two cases: all rename attempts are refused because another process 
holds the target open, or the file system does not support an atomic move and 
the file is staged next to the target first. The copy left the target with the 
current time, so the time that the transport had set on the temp file from the 
remote "Last-Modified" was lost. A rename keeps it.
    
    classicCopy now sets the last modified time of the target to that of the 
source, so the local repository holds the same time whichever way the file 
reaches its place. The time is set on a best-effort basis: the fallback exists 
to let the build complete, so it does not fail when only the time cannot be set.
    
    This is the 2.x part of #2317; the 1.9.x line loses the time on every 
download on Windows and is fixed separately.
    
    Generated-by: Claude Code (claude-fable-5-2)
    
    ---
    
    To make clear that you license your contribution under the [Apache License 
Version 2.0, January 2004](https://www.apache.org/licenses/LICENSE-2.0) check 
one of the following:
    
    - [x] I hereby declare this contribution to be licensed under the [Apache 
License Version 2.0, January 2004](https://www.apache.org/licenses/LICENSE-2.0)
    - [x] I hereby declare that I have filed an [Apache Individual Contributor 
License Agreement](https://www.apache.org/licenses/icla.pdf).
---
 .../aether/spi/io/PathProcessorSupport.java        |  9 +++
 .../aether/spi/io/PathProcessorSupportTest.java    | 64 ++++++++++++++++++++++
 2 files changed, 73 insertions(+)

diff --git 
a/maven-resolver-spi/src/main/java/org/eclipse/aether/spi/io/PathProcessorSupport.java
 
b/maven-resolver-spi/src/main/java/org/eclipse/aether/spi/io/PathProcessorSupport.java
index 568064413..2b701e225 100644
--- 
a/maven-resolver-spi/src/main/java/org/eclipse/aether/spi/io/PathProcessorSupport.java
+++ 
b/maven-resolver-spi/src/main/java/org/eclipse/aether/spi/io/PathProcessorSupport.java
@@ -357,6 +357,10 @@ public class PathProcessorSupport implements PathProcessor 
{
      * Pre-NIO2 way to copy files. Opens the target with truncation; normally 
callers stage into a temporary file and
      * rename into place to avoid exposing partially written content. Used as 
a last resort on Windows when all atomic
      * rename attempts are exhausted due to a persistent file lock (see {@link 
#retryingMove}).
+     * <p>
+     * The target gets the last modified time of the source,
+     * which the transports set from the remote "Last-Modified" header,
+     * as it does with a move.
      */
     protected void classicCopy(Path source, Path target) throws IOException {
         ByteBuffer buffer = ByteBuffer.allocate(1024 * 32);
@@ -371,5 +375,10 @@ public class PathProcessorSupport implements PathProcessor 
{
                 os.write(array, 0, bytes);
             }
         }
+        try {
+            setLastModified(target, 
Files.getLastModifiedTime(source).toMillis());
+        } catch (IOException e) {
+            LOGGER.debug("Could not set the last modified time of {}", target, 
e);
+        }
     }
 }
diff --git 
a/maven-resolver-spi/src/test/java/org/eclipse/aether/spi/io/PathProcessorSupportTest.java
 
b/maven-resolver-spi/src/test/java/org/eclipse/aether/spi/io/PathProcessorSupportTest.java
index 24d165970..becf7225a 100644
--- 
a/maven-resolver-spi/src/test/java/org/eclipse/aether/spi/io/PathProcessorSupportTest.java
+++ 
b/maven-resolver-spi/src/test/java/org/eclipse/aether/spi/io/PathProcessorSupportTest.java
@@ -25,6 +25,9 @@ import java.nio.file.AtomicMoveNotSupportedException;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.StandardCopyOption;
+import java.nio.file.attribute.FileTime;
+import java.time.Instant;
+import java.time.temporal.ChronoUnit;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.stream.Stream;
 
@@ -45,6 +48,12 @@ class PathProcessorSupportTest {
         StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING
     };
 
+    /**
+     * A time in the past, in whole seconds, as file systems differ in the 
precision they store.
+     */
+    private static final FileTime LAST_MODIFIED =
+            
FileTime.from(Instant.now().truncatedTo(ChronoUnit.SECONDS).minus(12, 
ChronoUnit.DAYS));
+
     private Path write(Path dir, String name, String content) throws 
IOException {
         Path file = dir.resolve(name);
         Files.write(file, content.getBytes(StandardCharsets.UTF_8));
@@ -155,6 +164,61 @@ class PathProcessorSupportTest {
         assertEquals("new content", read(target), "target must contain the new 
content after classic-copy fallback");
     }
 
+    @Test
+    void retryingMoveKeepsLastModified(@TempDir Path tempDir) throws 
IOException {
+        PathProcessorSupport subject = new PathProcessorSupport();
+        Path source = write(tempDir, "source.txt", "new content");
+        Path target = write(tempDir, "target.txt", "old content");
+        Files.setLastModifiedTime(source, LAST_MODIFIED);
+
+        subject.retryingMove(source, target, ATOMIC_OPTIONS);
+
+        assertEquals(LAST_MODIFIED, Files.getLastModifiedTime(target));
+    }
+
+    @Test
+    void retryingMoveKeepsLastModifiedOnClassicCopyFallback(@TempDir Path 
tempDir) throws IOException {
+        // all rename attempts fail, so the content reaches the target via the 
classic-copy fallback
+        PathProcessorSupport subject = new PathProcessorSupport() {
+            @Override
+            protected void fileSystemMove(Path source, Path target, 
StandardCopyOption... copyOptions)
+                    throws IOException {
+                throw new AccessDeniedException(target.toString());
+            }
+        };
+        Path source = write(tempDir, "source.txt", "new content");
+        Path target = write(tempDir, "target.txt", "old content");
+        Files.setLastModifiedTime(source, LAST_MODIFIED);
+
+        subject.retryingMove(source, target, ATOMIC_OPTIONS);
+
+        assertEquals("new content", read(target));
+        assertEquals(LAST_MODIFIED, Files.getLastModifiedTime(target));
+    }
+
+    @Test
+    void retryingMoveKeepsLastModifiedWhenAtomicMoveNotSupported(@TempDir Path 
tempDir) throws IOException {
+        // the first move fails, so the content is staged with a classic copy 
next to the target and then renamed
+        PathProcessorSupport subject = new PathProcessorSupport() {
+            @Override
+            protected void fileSystemMove(Path source, Path target, 
StandardCopyOption... copyOptions)
+                    throws IOException {
+                if (source.getFileName().toString().equals("source.txt")) {
+                    throw new 
AtomicMoveNotSupportedException(source.toString(), target.toString(), "test");
+                }
+                super.fileSystemMove(source, target, copyOptions);
+            }
+        };
+        Path source = write(tempDir, "source.txt", "new content");
+        Path target = write(tempDir, "target.txt", "old content");
+        Files.setLastModifiedTime(source, LAST_MODIFIED);
+
+        subject.retryingMove(source, target, ATOMIC_OPTIONS);
+
+        assertEquals("new content", read(target));
+        assertEquals(LAST_MODIFIED, Files.getLastModifiedTime(target));
+    }
+
     @Test
     void retryingMoveStagesCollocatedCopyWhenAtomicMoveNotSupported(@TempDir 
Path tempDir) throws IOException {
         PathProcessorSupport subject = new PathProcessorSupport() {

Reply via email to