Skip to content

Commit 39c651c

Browse files
committed
Seal FileSystem interface
1 parent 1dd5752 commit 39c651c

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

core/common/src/files/FileSystem.kt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import kotlinx.io.Source
1414
*
1515
* **This API is unstable and subject to change.**
1616
*/
17-
public interface FileSystem {
17+
public sealed interface FileSystem {
1818
/**
1919
* Path to a directory suitable for creating temporary files.
2020
*/
@@ -88,7 +88,7 @@ public interface FileSystem {
8888
* @throws kotlinx.io.files.FileNotFoundException when the file does not exist.
8989
* @throws kotlinx.io.IOException when it's not possible to open the file for reading.
9090
*/
91-
public fun read(path: Path): Source = path.source()
91+
public fun read(path: Path): Source
9292

9393
/**
9494
* Returns [Sink] to write into a file represented by the [path].
@@ -100,7 +100,7 @@ public interface FileSystem {
100100
*
101101
* @throws kotlinx.io.IOException when it's not possible to open the file for writing.
102102
*/
103-
public fun write(path: Path): Sink = path.sink()
103+
public fun write(path: Path): Sink
104104

105105
/**
106106
* Return [FileMetadata] associated with a file or directory represented by the [path].
@@ -119,11 +119,17 @@ public interface FileSystem {
119119
}
120120
}
121121

122+
internal abstract class SystemFileSystemImpl : FileSystem {
123+
override fun read(path: Path): Source = path.source()
124+
125+
override fun write(path: Path): Sink = path.sink()
126+
}
127+
122128
internal expect val SystemFileSystem: FileSystem
123129

124130
public class FileMetadata(
125131
public val isRegularFile: Boolean = false,
126132
public val isDirectory: Boolean = false
127133
)
128134

129-
public expect open class FileNotFoundException(message: String?) : IOException
135+
public expect class FileNotFoundException(message: String?) : IOException

core/js/src/files/FileSystemJs.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ internal val os: dynamic
2525
}
2626
}
2727

28-
internal actual val SystemFileSystem: FileSystem = object : FileSystem {
28+
private class JsFileSystem : SystemFileSystemImpl() {
29+
companion object {
30+
val Instance = JsFileSystem()
31+
}
32+
2933
override val temporaryDirectory: Path
3034
get() {
3135
check(os !== null) { "Module 'os' was not found" }
@@ -104,6 +108,8 @@ internal actual val SystemFileSystem: FileSystem = object : FileSystem {
104108
}
105109
}
106110

111+
internal actual val SystemFileSystem: FileSystem = JsFileSystem.Instance
112+
107113
public actual open class FileNotFoundException actual constructor(
108114
message: String?,
109115
) : IOException(message)

core/jvm/src/files/FileSystemJvm.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ private val mover: Mover by lazy {
4141
}
4242
}
4343

44-
private class JvmFileSystem : FileSystem {
44+
private class JvmFileSystem : SystemFileSystemImpl() {
4545
companion object {
4646
val Instance = JvmFileSystem()
4747
}

core/native/src/files/FileSystemNative.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import platform.posix.*
1111

1212
internal expect val NativeTempDir: Path
1313

14-
private class NativeFileSystem : FileSystem {
14+
private class NativeFileSystem : SystemFileSystemImpl() {
1515
companion object {
1616
val Instance = NativeFileSystem()
1717
}
@@ -85,7 +85,7 @@ internal expect fun atomicMoveImpl(source: Path, destination: Path)
8585

8686
internal expect fun mkdirImpl(path: String)
8787

88-
public actual val SystemFileSystem: FileSystem
88+
internal actual val SystemFileSystem: FileSystem
8989
get() = NativeFileSystem.Instance
9090

9191
public actual open class FileNotFoundException actual constructor(

0 commit comments

Comments
 (0)