File tree Expand file tree Collapse file tree 6 files changed +39
-8
lines changed Expand file tree Collapse file tree 6 files changed +39
-8
lines changed Original file line number Diff line number Diff line change @@ -197,8 +197,9 @@ public final class kotlinx/io/Utf8Kt {
197
197
198
198
public final class kotlinx/io/files/FileMetadata {
199
199
public fun <init> ()V
200
- public fun <init> (ZZ)V
201
- public synthetic fun <init> (ZZILkotlin/jvm/internal/DefaultConstructorMarker;)V
200
+ public fun <init> (ZZJ)V
201
+ public synthetic fun <init> (ZZJILkotlin/jvm/internal/DefaultConstructorMarker;)V
202
+ public final fun getSize ()J
202
203
public final fun isDirectory ()Z
203
204
public final fun isRegularFile ()Z
204
205
}
Original file line number Diff line number Diff line change @@ -130,8 +130,18 @@ internal abstract class SystemFileSystemImpl : FileSystem {
130
130
internal expect val SystemFileSystem : FileSystem
131
131
132
132
public class FileMetadata (
133
+ /* *
134
+ * Flag indicating that the metadata was retrieved for a regular file.
135
+ */
133
136
public val isRegularFile : Boolean = false ,
134
- public val isDirectory : Boolean = false
137
+ /* *
138
+ * Flag indicating that the metadata was retrieved for a directory.
139
+ */
140
+ public val isDirectory : Boolean = false ,
141
+ /* *
142
+ * File size. Defined only for regular files, for other filesystem entities it will be `-1`.
143
+ */
144
+ public val size : Long = 0L
135
145
)
136
146
137
147
public expect class FileNotFoundException (message : String? ) : IOException
Original file line number Diff line number Diff line change @@ -235,6 +235,21 @@ class SmokeFileTest {
235
235
}
236
236
}
237
237
238
+ @OptIn(ExperimentalStdlibApi ::class )
239
+ @Test
240
+ fun testFileSize () {
241
+ val path = createTempPath()
242
+ val expectedSize = 123
243
+ FileSystem .System .write(path).buffered().use {
244
+ it.write(ByteArray (expectedSize))
245
+ }
246
+ val metadata = FileSystem .System .metadataOrNull(path)
247
+ assertNotNull(metadata)
248
+ assertEquals(expectedSize.toLong(), metadata.size)
249
+
250
+ assertEquals(- 1L , FileSystem .System .metadataOrNull(path.parent!! )!! .size)
251
+ }
252
+
238
253
private fun constructAbsolutePath (vararg parts : String ): String {
239
254
return Path .separator.toString() + parts.joinToString(Path .separator.toString())
240
255
}
Original file line number Diff line number Diff line change @@ -97,9 +97,11 @@ private class JsFileSystem : SystemFileSystemImpl() {
97
97
return try {
98
98
val stat = fs.statSync(path.path)
99
99
val mode = stat.mode as Int
100
+ val isFile = (mode and fs.constants.S_IFMT as Int ) == fs.constants.S_IFREG
100
101
FileMetadata (
101
- isRegularFile = (mode and fs.constants.S_IFMT as Int ) == fs.constants.S_IFREG ,
102
- isDirectory = (mode and fs.constants.S_IFMT as Int ) == fs.constants.S_IFDIR
102
+ isRegularFile = isFile,
103
+ isDirectory = (mode and fs.constants.S_IFMT as Int ) == fs.constants.S_IFDIR ,
104
+ if (isFile) (stat.size as Int ).toLong() else - 1L
103
105
)
104
106
} catch (t: Throwable ) {
105
107
if (exists(path)) throw IOException (" Stat failed for $path " , t)
Original file line number Diff line number Diff line change @@ -77,7 +77,8 @@ private class JvmFileSystem : SystemFileSystemImpl() {
77
77
78
78
override fun metadataOrNull (path : Path ): FileMetadata ? {
79
79
if (! path.file.exists()) return null
80
- return FileMetadata (path.file.isFile, path.file.isDirectory)
80
+ return FileMetadata (path.file.isFile, path.file.isDirectory,
81
+ if (path.file.isFile) path.file.length() else - 1L )
81
82
}
82
83
}
83
84
Original file line number Diff line number Diff line change @@ -73,9 +73,11 @@ private class NativeFileSystem : SystemFileSystemImpl() {
73
73
throw IOException (" stat failed to ${path.path} : ${strerror(errno)?.toKString()} " )
74
74
}
75
75
val mode = struct_stat.st_mode.toInt()
76
+ val isFile = (mode and S_IFMT ) == S_IFREG
76
77
return FileMetadata (
77
- isRegularFile = (mode and S_IFMT ) == S_IFREG ,
78
- isDirectory = (mode and S_IFMT ) == S_IFDIR
78
+ isRegularFile = isFile,
79
+ isDirectory = (mode and S_IFMT ) == S_IFDIR ,
80
+ if (isFile) struct_stat.st_size.toLong() else - 1L
79
81
)
80
82
}
81
83
}
You can’t perform that action at this time.
0 commit comments