Skip to content

Commit 968a3fb

Browse files
[wasm] Fall back to a default chunk size when st_blksize is not available
The `st_blksize` field in `stat` struct is not provided by WASI, so we fall back to a default chunk size 4KB, which is a common page size.
1 parent 835e7a8 commit 968a3fb

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

Sources/FoundationEssentials/FileManager/FileOperations.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,16 @@ enum _FileOperations {
881881
}
882882

883883
let total: Int = Int(fileInfo.st_size)
884-
let chunkSize: Int = Int(fileInfo.st_blksize)
884+
// Respect the optimal block size for the file system if available
885+
// Some platforms including WASI don't provide this information, so we
886+
// fall back to the default chunk size 4KB, which is a common page size.
887+
let defaultChunkSize = 1024 * 4 // 4KB
888+
let chunkSize: Int
889+
if fileInfo.st_blksize > 0 {
890+
chunkSize = Int(fileInfo.st_blksize)
891+
} else {
892+
chunkSize = defaultChunkSize
893+
}
885894
var current: off_t = 0
886895

887896
#if os(WASI)

0 commit comments

Comments
 (0)