From 334e9ea92b5a64563590d06deb99d89839d3fccf Mon Sep 17 00:00:00 2001 From: Artem Zinnatullin Date: Mon, 7 Nov 2022 20:57:08 -0700 Subject: [PATCH] Fsync before rename after copy in DiskCacheClient --- .../devtools/build/lib/remote/disk/DiskCacheClient.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/remote/disk/DiskCacheClient.java b/src/main/java/com/google/devtools/build/lib/remote/disk/DiskCacheClient.java index 6735a09978ca56..c82f9d387ccabe 100644 --- a/src/main/java/com/google/devtools/build/lib/remote/disk/DiskCacheClient.java +++ b/src/main/java/com/google/devtools/build/lib/remote/disk/DiskCacheClient.java @@ -28,6 +28,7 @@ import com.google.devtools.build.lib.remote.util.Utils; import com.google.devtools.build.lib.vfs.Path; import com.google.protobuf.ByteString; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -174,11 +175,13 @@ private void saveFile(String key, InputStream in, boolean actionResult) throws I // Write a temporary file first, and then rename, to avoid data corruption in case of a crash. Path temp = toPathNoSplit(UUID.randomUUID().toString()); - try (OutputStream out = temp.getOutputStream()) { + + try (FileOutputStream out = new FileOutputStream(temp.getPathFile())) { ByteStreams.copy(in, out); + // Fsync temp before we rename it to avoid data loss in the case of machine + // crashes (the OS may reorder the writes and the rename). + out.getFD().sync(); } - // TODO(ulfjack): Fsync temp here before we rename it to avoid data loss in the case of machine - // crashes (the OS may reorder the writes and the rename). temp.renameTo(target); } }