Skip to content

Commit e2db231

Browse files
committed
Support file size retrieval
1 parent 62a974d commit e2db231

File tree

6 files changed

+39
-8
lines changed

6 files changed

+39
-8
lines changed

core/api/kotlinx-io-core.api

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,9 @@ public final class kotlinx/io/Utf8Kt {
197197

198198
public final class kotlinx/io/files/FileMetadata {
199199
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
202203
public final fun isDirectory ()Z
203204
public final fun isRegularFile ()Z
204205
}

core/common/src/files/FileSystem.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,18 @@ internal abstract class SystemFileSystemImpl : FileSystem {
130130
internal expect val SystemFileSystem: FileSystem
131131

132132
public class FileMetadata(
133+
/**
134+
* Flag indicating that the metadata was retrieved for a regular file.
135+
*/
133136
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
135145
)
136146

137147
public expect class FileNotFoundException(message: String?) : IOException

core/common/test/files/SmokeFileTest.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,21 @@ class SmokeFileTest {
235235
}
236236
}
237237

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+
238253
private fun constructAbsolutePath(vararg parts: String): String {
239254
return Path.separator.toString() + parts.joinToString(Path.separator.toString())
240255
}

core/js/src/files/FileSystemJs.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,11 @@ private class JsFileSystem : SystemFileSystemImpl() {
9797
return try {
9898
val stat = fs.statSync(path.path)
9999
val mode = stat.mode as Int
100+
val isFile = (mode and fs.constants.S_IFMT as Int) == fs.constants.S_IFREG
100101
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
103105
)
104106
} catch (t: Throwable) {
105107
if (exists(path)) throw IOException("Stat failed for $path", t)

core/jvm/src/files/FileSystemJvm.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ private class JvmFileSystem : SystemFileSystemImpl() {
7777

7878
override fun metadataOrNull(path: Path): FileMetadata? {
7979
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)
8182
}
8283
}
8384

core/native/src/files/FileSystemNative.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,11 @@ private class NativeFileSystem : SystemFileSystemImpl() {
7373
throw IOException("stat failed to ${path.path}: ${strerror(errno)?.toKString()}")
7474
}
7575
val mode = struct_stat.st_mode.toInt()
76+
val isFile = (mode and S_IFMT) == S_IFREG
7677
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
7981
)
8082
}
8183
}

0 commit comments

Comments
 (0)