Skip to content

Commit

Permalink
Move out helper methods as private static ones.
Browse files Browse the repository at this point in the history
  • Loading branch information
cstamas committed Nov 16, 2023
1 parent 728b623 commit c7928cc
Showing 1 changed file with 30 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public interface TempFile extends Closeable {
public interface CollocatedTempFile extends TempFile {
/**
* 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;
}
Expand Down Expand Up @@ -120,31 +123,42 @@ public void move() {
@Override
public void close() throws IOException {
if (wantsMove.get() && Files.isReadable(tempFile)) {
fsyncFile();
fsyncFile(tempFile);
Files.move(tempFile, file, StandardCopyOption.ATOMIC_MOVE);
if (!IS_WINDOWS) {
fsyncParent();
fsyncParent(tempFile);
}
}
Files.deleteIfExists(tempFile);
}
};
}

private void fsyncFile() throws IOException {
try (FileChannel file = FileChannel.open(tempFile, StandardOpenOption.WRITE)) {
file.force(true);
}
}
/**
* 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);
}
}

private void fsyncParent() throws IOException {
try (FileChannel parent = FileChannel.open(tempFile.getParent(), StandardOpenOption.READ)) {
try {
parent.force(true);
} catch (IOException e) {
// ignore
}
}
/**
* 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
}
};
}
}

/**
Expand Down

0 comments on commit c7928cc

Please sign in to comment.