Use useTempFile extension method in atomicWrite

Signed-off-by: Graham <gpe@openrs2.org>
Graham 4 years ago
parent 0b43e26d36
commit 22c1b8a183
  1. 5
      util/src/main/kotlin/org/openrs2/util/io/PathExtensions.kt

@ -107,13 +107,10 @@ public inline fun <T> Path.useTempFile(
} }
public inline fun <T> Path.atomicWrite(f: (Path) -> T): T { public inline fun <T> Path.atomicWrite(f: (Path) -> T): T {
val tempFile = Files.createTempFile(parent, ".$fileName", ".tmp") parent.useTempFile(".$fileName", ".tmp") { tempFile ->
try {
val result = f(tempFile) val result = f(tempFile)
Files.move(tempFile, this, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING) Files.move(tempFile, this, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING)
return result return result
} finally {
Files.deleteIfExists(tempFile)
} }
} }

Loading…
Cancel
Save