From 46ea551b17027fa2c5399556ea6df6e91bcb2230 Mon Sep 17 00:00:00 2001 From: Lanius Trolling Date: Sun, 28 Apr 2024 12:08:50 -0400 Subject: [PATCH] Refactor blocking code in VFS --- .../kotlin/info/mechyrdia/data/DataFiles.kt | 33 +++++++++---------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/src/jvmMain/kotlin/info/mechyrdia/data/DataFiles.kt b/src/jvmMain/kotlin/info/mechyrdia/data/DataFiles.kt index 165e4fd..6858d2c 100644 --- a/src/jvmMain/kotlin/info/mechyrdia/data/DataFiles.kt +++ b/src/jvmMain/kotlin/info/mechyrdia/data/DataFiles.kt @@ -163,9 +163,8 @@ private class FlatFileStorage(val root: File) : FileStorage { if (file.isFile) return false if (file.isDirectory) return true - if (!file.parentFile.exists()) - if (!createDir(file.parentFile)) - return false + if (!file.parentFile.exists() && !createDir(file.parentFile)) + return false file.mkdir() return true @@ -174,8 +173,8 @@ private class FlatFileStorage(val root: File) : FileStorage { private fun createFile(file: File): Boolean { if (!file.exists()) { val containingDir = file.parentFile - if (!containingDir.isDirectory) - if (!createDir(containingDir)) return false + if (!containingDir.isDirectory && !createDir(containingDir)) + return false } return true @@ -199,24 +198,24 @@ private class FlatFileStorage(val root: File) : FileStorage { } override suspend fun createDir(dir: StoragePath): Boolean { - return withContext(Dispatchers.IO) { createDir(resolveFile(dir)) } + return runInterruptible(Dispatchers.IO) { createDir(resolveFile(dir)) } } override suspend fun listDir(dir: StoragePath): Map? { - return withContext(Dispatchers.IO) { resolveFile(dir).listFiles()?.associate { renderEntry(it) } } + return runInterruptible(Dispatchers.IO) { resolveFile(dir).listFiles()?.associate { renderEntry(it) } } } override suspend fun deleteDir(dir: StoragePath): Boolean { if (dir.isRoot) return false val file = resolveFile(dir) if (!file.isDirectory) return true - return withContext(Dispatchers.IO) { file.deleteRecursively() } + return runInterruptible(Dispatchers.IO) { file.deleteRecursively() } } override suspend fun statFile(path: StoragePath): StoredFileStats? { val file = resolveFile(path) if (!file.isFile) return null - return withContext(Dispatchers.IO) { + return runInterruptible(Dispatchers.IO) { val basicAttributes = Files.readAttributes(file.toPath(), BasicFileAttributes::class.java) StoredFileStats( basicAttributes.creationTime().toInstant(), @@ -229,7 +228,7 @@ private class FlatFileStorage(val root: File) : FileStorage { override suspend fun writeFile(path: StoragePath, content: ByteArray): Boolean { val file = resolveFile(path) - return withContext(Dispatchers.IO) { + return runInterruptible(Dispatchers.IO) { if (createFile(file)) { file.writeBytes(content) true @@ -241,7 +240,7 @@ private class FlatFileStorage(val root: File) : FileStorage { val file = resolveFile(path) if (!file.isFile) return null - return withContext(Dispatchers.IO) { + return runInterruptible(Dispatchers.IO) { file.readBytes() } } @@ -252,7 +251,7 @@ private class FlatFileStorage(val root: File) : FileStorage { if (!sourceFile.isFile) return false - withContext(Dispatchers.IO) { + runInterruptible(Dispatchers.IO) { sourceFile.copyTo(targetFile, overwrite = true) } @@ -262,7 +261,7 @@ private class FlatFileStorage(val root: File) : FileStorage { override suspend fun eraseFile(path: StoragePath): Boolean { val file = resolveFile(path) if (!file.isFile) return true - return withContext(Dispatchers.IO) { file.delete() } + return runInterruptible(Dispatchers.IO) { file.delete() } } } @@ -340,9 +339,9 @@ private class GridFsStorage(val table: DocumentTable, val bucket: G ?.objectId ?.let { return it } - val bytesPublisher = { ByteBuffer.allocate(0) } - .asFlow() - .asPublisher(CoroutineName("grid-fs-writer") + Dispatchers.IO) + val bytesPublisher = flow { + emit(ByteBuffer.allocate(0)) + }.asPublisher(CoroutineName("grid-fs-writer") + Dispatchers.IO) return bucket.uploadFromPublisher(GRID_FS_KEEP, bytesPublisher).awaitFirst() } @@ -419,7 +418,7 @@ private class GridFsStorage(val table: DocumentTable, val bucket: G return ByteArrayOutputStream().also { content -> bucket.downloadToPublisher(gridFsId).asFlow().collect { buffer -> val array = buffer.slice().moveToByteArray() - withContext(Dispatchers.IO) { content.write(array) } + runInterruptible(Dispatchers.IO) { content.write(array) } } }.toByteArray() } -- 2.25.1