From abd2b40e2553a3ce05a1ab4db4ee96bd29d26ecc Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 8 Aug 2024 18:31:56 +0000 Subject: [PATCH] [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. --- .../FileManager/FileOperations.swift | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Sources/FoundationEssentials/FileManager/FileOperations.swift b/Sources/FoundationEssentials/FileManager/FileOperations.swift index d3fc7b2d5..d93468554 100644 --- a/Sources/FoundationEssentials/FileManager/FileOperations.swift +++ b/Sources/FoundationEssentials/FileManager/FileOperations.swift @@ -872,7 +872,16 @@ enum _FileOperations { } let total: Int = Int(fileInfo.st_size) - let chunkSize: Int = Int(fileInfo.st_blksize) + // Respect the optimal block size for the file system if available + // Some platforms including WASI don't provide this information, so we + // fall back to the default chunk size 4KB, which is a common page size. + let defaultChunkSize = 1024 * 4 // 4KB + let chunkSize: Int + if fileInfo.st_blksize > 0 { + chunkSize = Int(fileInfo.st_blksize) + } else { + chunkSize = defaultChunkSize + } var current: off_t = 0 #if os(WASI)