Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FileHandle: improve the compatibility of synchronize #3045

Merged
merged 1 commit into from
Aug 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion Sources/Foundation/FileHandle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,17 @@ open class FileHandle : NSObject {

#if os(Windows)
guard FlushFileBuffers(self._handle) else {
throw _NSErrorWithWindowsError(GetLastError(), reading: false)
let dwError: DWORD = GetLastError()
// If the handle is a handle to the console output,
// `FlushFileBuffers` will fail and return `ERROR_INVALID_HANDLE` as
// console output is not buffered.
if dwError == ERROR_INVALID_HANDLE &&
GetFileType(self._handle) == FILE_TYPE_CHAR {
// Simlar to the Linux, macOS, BSD cases below, ignore the error
// on the special file type.
return
}
throw _NSErrorWithWindowsError(dwError, reading: false)
}
#else
// Linux, macOS, OpenBSD return -1 and errno == EINVAL if trying to sync a special file,
Expand Down
4 changes: 4 additions & 0 deletions Tests/Foundation/Tests/TestFileHandle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,11 @@ class TestFileHandle : XCTestCase {

func testSynchronizeOnSpecialFile() throws {
// .synchronize() on a special file shouldnt fail
#if os(Windows)
let fh = try XCTUnwrap(FileHandle(forWritingAtPath: "CON"))
#else
let fh = try XCTUnwrap(FileHandle(forWritingAtPath: "/dev/stdout"))
#endif
XCTAssertNoThrow(try fh.synchronize())
}

Expand Down