From f7e194dfa6fd92f9fd8fa315884a1fd70eafe665 Mon Sep 17 00:00:00 2001 From: Graham Date: Sat, 7 May 2022 20:51:05 +0100 Subject: [PATCH] Improve atomicWrite We fsync the temp file before doing the atomic move, which I think is necessary if the underlying filesystem re-orders the operations (e.g. if the atomic move is performed before the writes to the temp file have been flushed to disk). Similarly we fsync the parent dir before returning. For a single atomic write in isolation this probably isn't important, but probably is useful if a sequence of atomic writes is performed in order. Signed-off-by: Graham --- util/src/main/kotlin/org/openrs2/util/io/PathExtensions.kt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/util/src/main/kotlin/org/openrs2/util/io/PathExtensions.kt b/util/src/main/kotlin/org/openrs2/util/io/PathExtensions.kt index 0b7ca784..839fb7ed 100644 --- a/util/src/main/kotlin/org/openrs2/util/io/PathExtensions.kt +++ b/util/src/main/kotlin/org/openrs2/util/io/PathExtensions.kt @@ -109,7 +109,9 @@ public inline fun Path.useTempFile( public inline fun Path.atomicWrite(f: (Path) -> T): T { parent.useTempFile(".$fileName", ".tmp") { tempFile -> val result = f(tempFile) + tempFile.fsync() Files.move(tempFile, this, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING) + parent.fsync() return result } }