-
Notifications
You must be signed in to change notification settings - Fork 126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[MRESOLVER-372] Rework the FileUtils collocated temp file #364
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -20,10 +20,16 @@ | |||||||||||||||
|
||||||||||||||||
import java.io.Closeable; | ||||||||||||||||
import java.io.IOException; | ||||||||||||||||
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; | ||||||||||||||||
|
||||||||||||||||
import static java.util.Objects.requireNonNull; | ||||||||||||||||
|
||||||||||||||||
|
@@ -33,6 +39,10 @@ | |||||||||||||||
* @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 | ||||||||||||||||
private static final boolean IS_WINDOWS = | ||||||||||||||||
System.getProperty("os.name", "unknown").startsWith("Windows"); | ||||||||||||||||
|
||||||||||||||||
private FileUtils() { | ||||||||||||||||
// hide constructor | ||||||||||||||||
} | ||||||||||||||||
|
@@ -52,7 +62,10 @@ public interface TempFile extends Closeable { | |||||||||||||||
*/ | ||||||||||||||||
public interface CollocatedTempFile extends TempFile { | ||||||||||||||||
/** | ||||||||||||||||
* Atomically moves temp file to target file it is collocated with. | ||||||||||||||||
* Upon close, atomically moves temp file to target file it is collocated with overwriting target (if exists). | ||||||||||||||||
* 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. | ||||||||||||||||
*/ | ||||||||||||||||
void move() throws IOException; | ||||||||||||||||
} | ||||||||||||||||
|
@@ -98,23 +111,79 @@ public static CollocatedTempFile newTempFile(Path file) throws IOException { | |||||||||||||||
Path tempFile = parent.resolve(file.getFileName() + "." | ||||||||||||||||
+ Long.toUnsignedString(ThreadLocalRandom.current().nextLong()) + ".tmp"); | ||||||||||||||||
return new CollocatedTempFile() { | ||||||||||||||||
private final AtomicBoolean wantsMove = new AtomicBoolean(false); | ||||||||||||||||
|
||||||||||||||||
@Override | ||||||||||||||||
public Path getPath() { | ||||||||||||||||
return tempFile; | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
@Override | ||||||||||||||||
public void move() throws IOException { | ||||||||||||||||
Files.move(tempFile, file, StandardCopyOption.ATOMIC_MOVE); | ||||||||||||||||
public void move() { | ||||||||||||||||
wantsMove.set(true); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
@Override | ||||||||||||||||
public void close() throws IOException { | ||||||||||||||||
if (wantsMove.get() && Files.isReadable(tempFile)) { | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not safe. The Lines 90 to 96 in e721b01
The code is using a BufferedOutputStream so that it may not be completely written to the disk. I'm not sure if there's any guarantee on the ordering of the close() methods in the try/finally block.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, so order is specified as being the reversed order of declaration, which makes sense of course, so this should work because the BufferedOutputStream will be closed just before the CollocatedTempFile. |
||||||||||||||||
if (IS_WINDOWS) { | ||||||||||||||||
copy(tempFile, file); | ||||||||||||||||
} else { | ||||||||||||||||
fsyncFile(tempFile); | ||||||||||||||||
Files.move(tempFile, file, StandardCopyOption.ATOMIC_MOVE); | ||||||||||||||||
fsyncParent(tempFile); | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
Files.deleteIfExists(tempFile); | ||||||||||||||||
} | ||||||||||||||||
}; | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
/** | ||||||||||||||||
* On Windows we use pre-NIO2 way to copy files, as for some reason it works. Beat me why. | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Beat, why? |
||||||||||||||||
*/ | ||||||||||||||||
private static void copy(Path source, Path target) throws IOException { | ||||||||||||||||
ByteBuffer buffer = ByteBuffer.allocate(1024 * 32); | ||||||||||||||||
byte[] array = buffer.array(); | ||||||||||||||||
try (InputStream is = Files.newInputStream(source); | ||||||||||||||||
OutputStream os = Files.newOutputStream(target)) { | ||||||||||||||||
while (true) { | ||||||||||||||||
int bytes = is.read(array); | ||||||||||||||||
if (bytes < 0) { | ||||||||||||||||
break; | ||||||||||||||||
} | ||||||||||||||||
os.write(array, 0, bytes); | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
/** | ||||||||||||||||
* 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); | ||||||||||||||||
} | ||||||||||||||||
slawekjaranowski marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
/** | ||||||||||||||||
* 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 | ||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe we have plexus utils code for this, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and
SystemUtils
in commons-langThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but if external library is not used I would not like add next dependency for one simple function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With this, I agree, but if we use it anyway, I wouldn't write the code myself.