5
5
6
6
package kotlinx.io.files
7
7
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
9
15
10
16
public actual val SystemFileSystem : FileSystem = object : SystemFileSystemImpl () {
11
17
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)
14
19
}
15
20
16
21
override fun delete (path : Path , mustExist : Boolean ) {
17
- check(fs != = null ) { " Module 'fs' was not found" }
18
22
if (! exists(path)) {
19
23
if (mustExist) {
20
- throw FileNotFoundException (" File does not exist: ${ path.path} " )
24
+ throw FileNotFoundException (" File does not exist: $path " )
21
25
}
22
26
return
23
27
}
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)
28
32
} else {
29
- fs. rmSync(path.path)
33
+ rmSync(path.path)
30
34
}
31
- } catch (t : Throwable ) {
32
- throw IOException (" Delete failed for $path " , t )
35
+ }?. also {
36
+ throw IOException (" Delete failed for $path " , it )
33
37
}
34
38
}
35
39
@@ -52,65 +56,60 @@ public actual val SystemFileSystem: FileSystem = object : SystemFileSystemImpl()
52
56
p = p.parent
53
57
}
54
58
parts.asReversed().forEach {
55
- fs. mkdirSync(it)
59
+ mkdirSync(it)
56
60
}
57
61
}
58
62
59
63
override fun atomicMove (source : Path , destination : Path ) {
60
- check(fs != = null ) { " Module 'fs' was not found" }
61
64
if (! exists(source)) {
62
65
throw FileNotFoundException (" Source does not exist: ${source.path} " )
63
66
}
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 )
68
71
}
69
72
}
70
73
71
74
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 (
78
82
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
81
85
)
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)
85
88
}
89
+ return metadata
86
90
}
87
91
88
92
override fun source (path : Path ): RawSource {
89
- check(fs != = null ) { " Module 'fs' was not found" }
90
93
return FileSource (path)
91
94
}
92
95
93
96
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" }
96
97
return FileSink (path, append)
97
98
}
98
99
99
100
override fun resolve (path : Path ): Path {
100
- check(fs != = null ) { " Module 'fs' was not found" }
101
101
if (! exists(path)) throw FileNotFoundException (path.path)
102
- return Path (fs. realpathSync.native(path.path) as String )
102
+ return Path (realpathSync.native(path.path))
103
103
}
104
104
}
105
105
106
106
public actual val SystemTemporaryDirectory : Path
107
107
get() {
108
- check(os != = null ) { " Module 'os' was not found" }
109
- return Path (os.tmpdir() as ? String ? : " " )
108
+ return Path (tmpdir() ? : " " )
110
109
}
111
110
112
111
public actual open class FileNotFoundException actual constructor(
113
112
message : String? ,
114
113
) : IOException(message)
115
114
116
- internal actual val isWindows = os. platform() == " win32"
115
+ internal actual val isWindows = platform() == " win32"
0 commit comments