Skip to content

Commit a23f789

Browse files
committed
Make temporaryDir independent of a particular FS instance
1 parent ff6ec3d commit a23f789

File tree

11 files changed

+27
-27
lines changed

11 files changed

+27
-27
lines changed

core/api/kotlinx-io-core.api

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,14 +212,14 @@ public abstract interface class kotlinx/io/files/FileSystem {
212212
public abstract fun delete (Lkotlinx/io/files/Path;Z)V
213213
public static synthetic fun delete$default (Lkotlinx/io/files/FileSystem;Lkotlinx/io/files/Path;ZILjava/lang/Object;)V
214214
public abstract fun exists (Lkotlinx/io/files/Path;)Z
215-
public abstract fun getTemporaryDirectory ()Lkotlinx/io/files/Path;
216215
public abstract fun metadataOrNull (Lkotlinx/io/files/Path;)Lkotlinx/io/files/FileMetadata;
217216
public abstract fun read (Lkotlinx/io/files/Path;)Lkotlinx/io/Source;
218217
public abstract fun write (Lkotlinx/io/files/Path;)Lkotlinx/io/Sink;
219218
}
220219

221220
public final class kotlinx/io/files/FileSystem$Companion {
222221
public final fun getSystem ()Lkotlinx/io/files/FileSystem;
222+
public final fun getSystemTemporaryDirectory ()Lkotlinx/io/files/Path;
223223
}
224224

225225
public final class kotlinx/io/files/Path {

core/apple/src/files/FileSystemApple.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ internal actual fun atomicMoveImpl(source: Path, destination: Path) {
2121
}
2222
}
2323

24-
internal actual val NativeTempDir: Path
24+
internal actual val SystemTemporaryDirectoryImpl: Path
2525
get() = Path(NSTemporaryDirectory())
2626

2727
internal actual fun dirnameImpl(path: String): String {

core/common/src/files/FileSystem.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@ import kotlinx.io.Source
1515
* **This API is unstable and subject to change.**
1616
*/
1717
public sealed interface FileSystem {
18-
/**
19-
* Path to a directory suitable for creating temporary files.
20-
*/
21-
public val temporaryDirectory: Path
22-
2318
/**
2419
* Returns `true` if there is a filesystem entity a [path] points to,
2520
* otherwise returns `false`.
@@ -123,6 +118,13 @@ public sealed interface FileSystem {
123118
* An instance of [FileSystem] representing a default system-wide filesystem.
124119
*/
125120
public val System: FileSystem = SystemFileSystem
121+
122+
/**
123+
* Path to a directory suitable for creating temporary files.
124+
*
125+
* This path is not bound to a particular filesystem, but rather a system-wide temporary directory.
126+
*/
127+
public val SystemTemporaryDirectory: Path = SystemTemporaryDirectoryImpl
126128
}
127129
}
128130

@@ -136,6 +138,8 @@ internal abstract class SystemFileSystemImpl : FileSystem {
136138

137139
internal expect val SystemFileSystem: FileSystem
138140

141+
internal expect val SystemTemporaryDirectoryImpl: Path
142+
139143
/**
140144
* Represents information about a file or directory obtainable from a filesystem.
141145
*/

core/common/test/files/SmokeFileTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class SmokeFileTest {
8787

8888
@Test
8989
fun checkTmpDir() {
90-
assertTrue(FileSystem.System.exists(FileSystem.System.temporaryDirectory))
90+
assertTrue(FileSystem.System.exists(FileSystem.SystemTemporaryDirectory))
9191
}
9292

9393
@OptIn(ExperimentalStdlibApi::class)

core/js/src/files/FileSystemJs.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,6 @@ private class JsFileSystem : SystemFileSystemImpl() {
3030
val Instance = JsFileSystem()
3131
}
3232

33-
override val temporaryDirectory: Path
34-
get() {
35-
check(os !== null) { "Module 'os' was not found" }
36-
return Path(os.tmpdir() as? String ?: "")
37-
}
38-
3933
override fun exists(path: Path): Boolean {
4034
check(fs !== null) { "Module 'fs' was not found" }
4135
return fs.existsSync(path.path) as Boolean
@@ -112,6 +106,12 @@ private class JsFileSystem : SystemFileSystemImpl() {
112106

113107
internal actual val SystemFileSystem: FileSystem = JsFileSystem.Instance
114108

109+
internal actual val SystemTemporaryDirectoryImpl: Path
110+
get() {
111+
check(os !== null) { "Module 'os' was not found" }
112+
return Path(os.tmpdir() as? String ?: "")
113+
}
114+
115115
public actual open class FileNotFoundException actual constructor(
116116
message: String?,
117117
) : IOException(message)

core/jvm/src/files/FileSystemJvm.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@ private class JvmFileSystem : SystemFileSystemImpl() {
4646
val Instance = JvmFileSystem()
4747
}
4848

49-
override val temporaryDirectory: Path
50-
get() = Path(System.getProperty("java.io.tmpdir"))
51-
5249
override fun exists(path: Path): Boolean {
5350
return path.file.exists()
5451
}
@@ -84,4 +81,7 @@ private class JvmFileSystem : SystemFileSystemImpl() {
8481

8582
internal actual val SystemFileSystem: FileSystem = JvmFileSystem.Instance
8683

84+
internal actual val SystemTemporaryDirectoryImpl: Path
85+
get() = Path(System.getProperty("java.io.tmpdir"))
86+
8787
public actual typealias FileNotFoundException = java.io.FileNotFoundException

core/jvm/test/utilJVM.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import kotlin.test.assertEquals
1313

1414
@OptIn(ExperimentalStdlibApi::class)
1515
actual fun tempFileName(): String {
16-
val tmpDir = FileSystem.System.temporaryDirectory.file
16+
val tmpDir = FileSystem.SystemTemporaryDirectory.file
1717
while (true) {
1818
val randomString = Random.nextBytes(32).toHexString()
1919
val res = File(tmpDir, randomString)

core/native/src/files/FileSystemNative.kt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,11 @@ import kotlinx.cinterop.*
99
import kotlinx.io.IOException
1010
import platform.posix.*
1111

12-
internal expect val NativeTempDir: Path
13-
1412
private class NativeFileSystem : SystemFileSystemImpl() {
1513
companion object {
1614
val Instance = NativeFileSystem()
1715
}
1816

19-
override val temporaryDirectory: Path
20-
get() = NativeTempDir
21-
2217
override fun exists(path: Path): Boolean {
2318
return access(path.path, F_OK) == 0
2419
}

core/native/test/util.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import kotlin.random.Random
1515

1616
@OptIn(UnsafeNumber::class, ExperimentalStdlibApi::class)
1717
actual fun tempFileName(): String {
18-
val tmpDir = FileSystem.System.temporaryDirectory.path
18+
val tmpDir = FileSystem.SystemTemporaryDirectory.path
1919
for (i in 0 until 10) {
2020
val name = Random.nextBytes(32).toHexString()
2121
val path = "$tmpDir/$name"

core/nonApple/src/files/FileSystemNonApple.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ import kotlinx.cinterop.toKString
1010
import platform.posix.getenv
1111

1212
@OptIn(ExperimentalForeignApi::class)
13-
internal actual val NativeTempDir: Path
13+
internal actual val SystemTemporaryDirectoryImpl: Path
1414
get() = Path(getenv("TMPDIR")?.toKString() ?: getenv("TMP")?.toKString() ?: "")

core/wasm/src/files/FileSystemWasm.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ import kotlinx.io.Source
1111

1212
internal fun unsupported(): Nothing = TODO("Paths are not supported for Wasm target")
1313

14+
internal actual val SystemTemporaryDirectoryImpl: Path
15+
get() = unsupported()
16+
1417
internal actual val SystemFileSystem: FileSystem = object : SystemFileSystemImpl() {
15-
override val temporaryDirectory: Path
16-
get() = unsupported()
1718

1819
override fun exists(path: Path): Boolean = unsupported()
1920

0 commit comments

Comments
 (0)