Skip to content

Commit 36245ac

Browse files
authored
FoundationEssentials: fix corner case of flushing file descriptor (#723)
On Windows, `_commit` can only be used for file descriptors backing files that are on disks. We would previously flush all file types which would cause an error. Thanks to Jeremy Day for tracking down the cause of the error!
1 parent 6ee772b commit 36245ac

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

Sources/FoundationEssentials/Data/Data+Writing.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,14 @@ private func write(buffer: UnsafeRawBufferPointer, toFileDescriptor fd: Int32, p
230230

231231
if !buffer.isEmpty {
232232
#if os(Windows)
233-
let res = _commit(fd)
233+
let hFile: HANDLE? = HANDLE(bitPattern: _get_osfhandle(fd))
234+
// On Windows, only call _commit if the fd corresponds to an actual file
235+
// on disk.
236+
let res: CInt = if let hFile, GetFileType(hFile) == FILE_TYPE_DISK {
237+
_commit(fd)
238+
} else {
239+
0
240+
}
234241
#else
235242
let res = fsync(fd)
236243
#endif

0 commit comments

Comments
 (0)