Skip to content

Commit e8169b1

Browse files
committed
Go back to atomic (apache#1594)
This change was done due Windows, but later the execution path was split for Windows to use oldie pre-NIO2 code to copy as for some reason that works on Windows. On other platforms, insist on ATOMIC, still make use able to override it, in case atomic is not a viable option.
1 parent 94ca521 commit e8169b1

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

maven-resolver-util/src/main/java/org/eclipse/aether/util/FileUtils.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,24 @@
3737
* @since 1.9.0
3838
*/
3939
public final class FileUtils {
40-
// Logic borrowed from Commons-Lang3: we really need only this, to decide do we "atomic move" or not
40+
/**
41+
* Logic borrowed from Commons-Lang3: we really need only this, to decide do we NIO2 file ops or not.
42+
* For some reason non-NIO2 works better on Windows.
43+
*/
4144
private static final boolean IS_WINDOWS =
4245
System.getProperty("os.name", "unknown").startsWith("Windows");
4346

47+
/**
48+
* Escape hatch if atomic move is not desired on system we run on.
49+
*
50+
* @since 2.0.12
51+
*/
52+
private static final boolean ATOMIC_MOVE =
53+
Boolean.parseBoolean(System.getProperty(FileUtils.class.getName() + "ATOMIC_MOVE", "true"));
54+
4455
private FileUtils() {
4556
// hide constructor
4657
}
47-
4858
/**
4959
* A temporary file, that is removed when closed.
5060
*/
@@ -115,6 +125,8 @@ public static CollocatedTempFile newTempFile(Path file) throws IOException {
115125
+ Long.toUnsignedString(ThreadLocalRandom.current().nextLong()) + ".tmp");
116126
return new CollocatedTempFile() {
117127
private final AtomicBoolean wantsMove = new AtomicBoolean(false);
128+
private final StandardCopyOption copyOption =
129+
FileUtils.ATOMIC_MOVE ? StandardCopyOption.ATOMIC_MOVE : StandardCopyOption.REPLACE_EXISTING;
118130

119131
@Override
120132
public Path getPath() {
@@ -132,7 +144,7 @@ public void close() throws IOException {
132144
if (IS_WINDOWS) {
133145
copy(tempFile, file);
134146
} else {
135-
Files.move(tempFile, file, StandardCopyOption.REPLACE_EXISTING);
147+
Files.move(tempFile, file, copyOption);
136148
}
137149
}
138150
Files.deleteIfExists(tempFile);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.eclipse.aether.util;
20+
21+
import java.io.IOException;
22+
import java.nio.charset.StandardCharsets;
23+
import java.nio.file.Files;
24+
import java.nio.file.Path;
25+
26+
import org.junit.jupiter.api.Test;
27+
import org.junit.jupiter.api.io.TempDir;
28+
29+
import static org.junit.jupiter.api.Assertions.assertEquals;
30+
31+
public class FileUtilsTest {
32+
@Test
33+
void smoke(@TempDir Path tmp) throws IOException {
34+
Path target = tmp.resolve("target.txt");
35+
Files.write(target, "Hello world".getBytes(StandardCharsets.UTF_8));
36+
assertEquals("Hello world", new String(Files.readAllBytes(target), StandardCharsets.UTF_8));
37+
38+
try (FileUtils.CollocatedTempFile tempFile = FileUtils.newTempFile(target)) {
39+
Files.write(tempFile.getPath(), "Hello other world".getBytes(StandardCharsets.UTF_8));
40+
tempFile.move();
41+
}
42+
assertEquals("Hello other world", new String(Files.readAllBytes(target), StandardCharsets.UTF_8));
43+
}
44+
}

0 commit comments

Comments
 (0)