Skip to content

Commit 41cbea8

Browse files
[wasm] Fall back to a default chunk size when st_blksize is not available (#835)
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 4440638 commit 41cbea8

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

Sources/FoundationEssentials/FileManager/FileOperations.swift

+10-1
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,16 @@ enum _FileOperations {
872872
}
873873

874874
let total: Int = Int(fileInfo.st_size)
875-
let chunkSize: Int = Int(fileInfo.st_blksize)
875+
// Respect the optimal block size for the file system if available
876+
// Some platforms including WASI don't provide this information, so we
877+
// fall back to the default chunk size 4KB, which is a common page size.
878+
let defaultChunkSize = 1024 * 4 // 4KB
879+
let chunkSize: Int
880+
if fileInfo.st_blksize > 0 {
881+
chunkSize = Int(fileInfo.st_blksize)
882+
} else {
883+
chunkSize = defaultChunkSize
884+
}
876885
var current: off_t = 0
877886

878887
#if os(WASI)

0 commit comments

Comments
 (0)