55
66package kotlinx.io.files
77
8- import kotlinx.io.*
8+ import kotlinx.io.IOException
9+ import kotlinx.io.RawSink
10+ import kotlinx.io.RawSource
11+ import kotlinx.io.node.fs.*
12+ import kotlinx.io.node.os.platform
13+ import kotlinx.io.node.os.tmpdir
14+ import kotlinx.io.withCaughtException
915
1016public actual val SystemFileSystem : FileSystem = object : SystemFileSystemImpl () {
1117 override fun exists (path : Path ): Boolean {
12- check(fs != = null ) { " Module 'fs' was not found" }
13- return fs.existsSync(path.path) as Boolean
18+ return existsSync(path.path)
1419 }
1520
1621 override fun delete (path : Path , mustExist : Boolean ) {
17- check(fs != = null ) { " Module 'fs' was not found" }
1822 if (! exists(path)) {
1923 if (mustExist) {
20- throw FileNotFoundException (" File does not exist: ${ path.path} " )
24+ throw FileNotFoundException (" File does not exist: $path " )
2125 }
2226 return
2327 }
24- try {
25- val stats = fs. statSync(path.path)
26- if (stats.isDirectory() as Boolean ) {
27- fs. rmdirSync(path.path)
28+ withCaughtException {
29+ val stats = statSync(path.path) ? : throw FileNotFoundException ( " File does not exist: $path " )
30+ if (stats.isDirectory()) {
31+ rmdirSync(path.path)
2832 } else {
29- fs. rmSync(path.path)
33+ rmSync(path.path)
3034 }
31- } catch (t : Throwable ) {
32- throw IOException (" Delete failed for $path " , t )
35+ }?. also {
36+ throw IOException (" Delete failed for $path " , it )
3337 }
3438 }
3539
@@ -52,65 +56,60 @@ public actual val SystemFileSystem: FileSystem = object : SystemFileSystemImpl()
5256 p = p.parent
5357 }
5458 parts.asReversed().forEach {
55- fs. mkdirSync(it)
59+ mkdirSync(it)
5660 }
5761 }
5862
5963 override fun atomicMove (source : Path , destination : Path ) {
60- check(fs != = null ) { " Module 'fs' was not found" }
6164 if (! exists(source)) {
6265 throw FileNotFoundException (" Source does not exist: ${source.path} " )
6366 }
64- try {
65- fs. renameSync(source.path, destination.path)
66- } catch (t : Throwable ) {
67- throw IOException (" Move failed from $source to $destination " , t )
67+ withCaughtException {
68+ renameSync(source.path, destination.path)
69+ }?. also {
70+ throw IOException (" Move failed from $source to $destination " , it )
6871 }
6972 }
7073
7174 override fun metadataOrNull (path : Path ): FileMetadata ? {
72- check(fs != = null ) { " Module 'fs' was not found" }
73- return try {
74- val stat = fs.statSync(path.path)
75- val mode = stat.mode as Int
76- val isFile = (mode and fs.constants.S_IFMT as Int ) == fs.constants.S_IFREG
77- FileMetadata (
75+ if (! exists(path)) return null
76+ var metadata: FileMetadata ? = null
77+ withCaughtException {
78+ val stat = statSync(path.path) ? : return @withCaughtException
79+ val mode = stat.mode
80+ val isFile = (mode and constants.S_IFMT ) == constants.S_IFREG
81+ metadata = FileMetadata (
7882 isRegularFile = isFile,
79- isDirectory = (mode and fs. constants.S_IFMT as Int ) == fs. constants.S_IFDIR ,
80- if (isFile) ( stat.size as Int ) .toLong() else - 1L
83+ isDirectory = (mode and constants.S_IFMT ) == constants.S_IFDIR ,
84+ if (isFile) stat.size.toLong() else - 1L
8185 )
82- } catch (t: Throwable ) {
83- if (exists(path)) throw IOException (" Stat failed for $path " , t)
84- return null
86+ }?.also {
87+ throw IOException (" Stat failed for $path " , it)
8588 }
89+ return metadata
8690 }
8791
8892 override fun source (path : Path ): RawSource {
89- check(fs != = null ) { " Module 'fs' was not found" }
9093 return FileSource (path)
9194 }
9295
9396 override fun sink (path : Path , append : Boolean ): RawSink {
94- check(fs != = null ) { " Module 'fs' was not found" }
95- check(buffer != = null ) { " Module 'buffer' was not found" }
9697 return FileSink (path, append)
9798 }
9899
99100 override fun resolve (path : Path ): Path {
100- check(fs != = null ) { " Module 'fs' was not found" }
101101 if (! exists(path)) throw FileNotFoundException (path.path)
102- return Path (fs. realpathSync.native(path.path) as String )
102+ return Path (realpathSync.native(path.path))
103103 }
104104}
105105
106106public actual val SystemTemporaryDirectory : Path
107107 get() {
108- check(os != = null ) { " Module 'os' was not found" }
109- return Path (os.tmpdir() as ? String ? : " " )
108+ return Path (tmpdir() ? : " " )
110109 }
111110
112111public actual open class FileNotFoundException actual constructor(
113112 message : String? ,
114113) : IOException(message)
115114
116- internal actual val isWindows = os. platform() == " win32"
115+ internal actual val isWindows = platform() == " win32"
0 commit comments