diff options
| author | Joel Klinghed <the_jk@spawned.biz> | 2024-08-23 00:23:04 +0200 |
|---|---|---|
| committer | Joel Klinghed <the_jk@spawned.biz> | 2024-08-23 00:23:04 +0200 |
| commit | a95264b5273748330c3126632277fd7a0db8ec91 (patch) | |
| tree | c1342d5f04d79fa99325746a19cd68d5df23f890 /libs/local/src/main | |
| parent | 399a431f1b8610b94cd38f7910ca4c70c29906d5 (diff) | |
local: Simply PathFile.write
Don't fiddle around with temp files and such. This needs to be
handled on a higher level. Otherwise, how do you know if the temp
file should be replaced or not when close is called?
Diffstat (limited to 'libs/local/src/main')
| -rw-r--r-- | libs/local/src/main/java/org/the_jk/cleversync/io/local/PathFile.kt | 38 |
1 files changed, 5 insertions, 33 deletions
diff --git a/libs/local/src/main/java/org/the_jk/cleversync/io/local/PathFile.kt b/libs/local/src/main/java/org/the_jk/cleversync/io/local/PathFile.kt index 6aeb895..7cf7690 100644 --- a/libs/local/src/main/java/org/the_jk/cleversync/io/local/PathFile.kt +++ b/libs/local/src/main/java/org/the_jk/cleversync/io/local/PathFile.kt @@ -3,13 +3,9 @@ package org.the_jk.cleversync.io.local import org.the_jk.cleversync.io.ModifiableFile import java.io.InputStream import java.io.OutputStream -import java.nio.file.Files -import java.nio.file.LinkOption import java.nio.file.Path -import java.nio.file.StandardCopyOption import java.nio.file.StandardOpenOption import java.time.Instant -import kotlin.io.path.exists import kotlin.io.path.fileSize import kotlin.io.path.getLastModifiedTime import kotlin.io.path.inputStream @@ -18,35 +14,11 @@ import kotlin.io.path.outputStream internal class PathFile(internal val path: Path) : ModifiableFile { override fun write(): OutputStream { - // If file doesn't exist, write to it directly. - if (!path.exists(LinkOption.NOFOLLOW_LINKS)) - return path.outputStream(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE) - - // Otherwise, write to temp file, only overwriting when done. - val tmp = path.parent.resolve(".#" + path.name) - val os = tmp.outputStream(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE) - return object : OutputStream() { - override fun write(value: Int) { - os.write(value) - } - - override fun write(b: ByteArray) { - os.write(b) - } - - override fun write(b: ByteArray, off: Int, len: Int) { - os.write(b, off, len) - } - - override fun flush() { - os.flush() - } - - override fun close() { - os.close() - Files.move(tmp, path, StandardCopyOption.ATOMIC_MOVE) - } - } + return path.outputStream( + StandardOpenOption.CREATE, + StandardOpenOption.WRITE, + StandardOpenOption.TRUNCATE_EXISTING, + ) } override val name: String |
