summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@spawned.biz>2024-08-23 00:23:04 +0200
committerJoel Klinghed <the_jk@spawned.biz>2024-08-23 00:23:04 +0200
commita95264b5273748330c3126632277fd7a0db8ec91 (patch)
treec1342d5f04d79fa99325746a19cd68d5df23f890
parent399a431f1b8610b94cd38f7910ca4c70c29906d5 (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?
-rw-r--r--libs/local/src/main/java/org/the_jk/cleversync/io/local/PathFile.kt38
-rw-r--r--libs/local/src/test/java/org/the_jk/cleversync/local/LocalTreeTest.kt1
2 files changed, 5 insertions, 34 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
diff --git a/libs/local/src/test/java/org/the_jk/cleversync/local/LocalTreeTest.kt b/libs/local/src/test/java/org/the_jk/cleversync/local/LocalTreeTest.kt
index 21002e3..a7d0afa 100644
--- a/libs/local/src/test/java/org/the_jk/cleversync/local/LocalTreeTest.kt
+++ b/libs/local/src/test/java/org/the_jk/cleversync/local/LocalTreeTest.kt
@@ -98,7 +98,6 @@ class LocalTreeTest {
os.write(127)
os.write(byteArrayOf(1))
os.write(byteArrayOf(2), 0, 0)
- assertThat(foo.size).isEqualTo(4.toULong())
}
assertThat(foo.size).isEqualTo(2.toULong())
assertThat(tree.list().files).hasSize(1)