From e2c3894e5a43d91c1429052e9cdd3d6e1a3485b9 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Wed, 22 Nov 2023 13:26:13 +0100 Subject: [PATCH 1/3] [MRESOLVER-441] Undo the FileUtils changes for non-Windows --- https://issues.apache.org/jira/browse/MRESOLVER-441 --- .../org/eclipse/aether/util/FileUtils.java | 31 ------------------- 1 file changed, 31 deletions(-) diff --git a/maven-resolver-util/src/main/java/org/eclipse/aether/util/FileUtils.java b/maven-resolver-util/src/main/java/org/eclipse/aether/util/FileUtils.java index 0ec3880b0..3404cff5e 100644 --- a/maven-resolver-util/src/main/java/org/eclipse/aether/util/FileUtils.java +++ b/maven-resolver-util/src/main/java/org/eclipse/aether/util/FileUtils.java @@ -23,11 +23,9 @@ import java.io.InputStream; import java.io.OutputStream; import java.nio.ByteBuffer; -import java.nio.channels.FileChannel; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; -import java.nio.file.StandardOpenOption; import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.atomic.AtomicBoolean; @@ -129,9 +127,7 @@ public void close() throws IOException { if (IS_WINDOWS) { copy(tempFile, file); } else { - fsyncFile(tempFile); Files.move(tempFile, file, StandardCopyOption.ATOMIC_MOVE); - fsyncParent(tempFile); } } Files.deleteIfExists(tempFile); @@ -157,33 +153,6 @@ private static void copy(Path source, Path target) throws IOException { } } - /** - * Performs fsync: makes sure no OS "dirty buffers" exist for given file. - * - * @param target Path that must not be {@code null}, must exist as plain file. - */ - private static void fsyncFile(Path target) throws IOException { - try (FileChannel file = FileChannel.open(target, StandardOpenOption.WRITE)) { - file.force(true); - } - } - - /** - * Performs directory fsync: not usable on Windows, but some other OSes may also throw, hence thrown IO exception - * is just ignored. - * - * @param target Path that must not be {@code null}, must exist as plain file, and must have parent. - */ - private static void fsyncParent(Path target) throws IOException { - try (FileChannel parent = FileChannel.open(target.getParent(), StandardOpenOption.READ)) { - try { - parent.force(true); - } catch (IOException e) { - // ignore - } - } - } - /** * A file writer, that accepts a {@link Path} to write some content to. Note: the file denoted by path may exist, * hence implementation have to ensure it is able to achieve its goal ("replace existing" option or equivalent From f2b4ab0b1a8df6ef788496bd7ab60373197a8b65 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Wed, 22 Nov 2023 13:54:40 +0100 Subject: [PATCH 2/3] Clarify the intent in Javadoc and remove added Files use --- .../src/main/java/org/eclipse/aether/util/FileUtils.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/maven-resolver-util/src/main/java/org/eclipse/aether/util/FileUtils.java b/maven-resolver-util/src/main/java/org/eclipse/aether/util/FileUtils.java index 3404cff5e..60b2239d1 100644 --- a/maven-resolver-util/src/main/java/org/eclipse/aether/util/FileUtils.java +++ b/maven-resolver-util/src/main/java/org/eclipse/aether/util/FileUtils.java @@ -64,6 +64,11 @@ public interface CollocatedTempFile extends TempFile { * Invocation of this method merely signals that caller ultimately wants temp file to replace the target * file, but when this method returns, the move operation did not yet happen, it will happen when this * instance is closed. + *

+ * Invoking this method without writing to temp file {@link #getPath()} (thus, not creating a temp + * file to be moved) is considered a bug, a mistake of the caller. Caller of this method should ensure + * that this method is invoked ONLY when the temp file is created and moving it to its final place is + * required. */ void move() throws IOException; } @@ -123,7 +128,7 @@ public void move() { @Override public void close() throws IOException { - if (wantsMove.get() && Files.isReadable(tempFile)) { + if (wantsMove.get()) { if (IS_WINDOWS) { copy(tempFile, file); } else { From 83011a469fc3938b0f316aadf743485d04f22f47 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Wed, 22 Nov 2023 14:15:41 +0100 Subject: [PATCH 3/3] Update comment --- .../src/main/java/org/eclipse/aether/util/FileUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maven-resolver-util/src/main/java/org/eclipse/aether/util/FileUtils.java b/maven-resolver-util/src/main/java/org/eclipse/aether/util/FileUtils.java index 60b2239d1..0a430f89f 100644 --- a/maven-resolver-util/src/main/java/org/eclipse/aether/util/FileUtils.java +++ b/maven-resolver-util/src/main/java/org/eclipse/aether/util/FileUtils.java @@ -37,7 +37,7 @@ * @since 1.9.0 */ public final class FileUtils { - // Logic borrowed from Commons-Lang3: we really need only this, to decide do we fsync on directories or not + // Logic borrowed from Commons-Lang3: we really need only this, to decide do we "atomic move" or not private static final boolean IS_WINDOWS = System.getProperty("os.name", "unknown").startsWith("Windows");