Skip to content

SR-14953: Data.write(to:) fails when writing to /dev/stdout #3022

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

Merged
merged 1 commit into from
Aug 2, 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
9 changes: 8 additions & 1 deletion Sources/Foundation/FileHandle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,14 @@ open class FileHandle : NSObject {
throw _NSErrorWithWindowsError(GetLastError(), reading: false)
}
#else
guard fsync(_fd) >= 0 else { throw _NSErrorWithErrno(errno, reading: false) }
// Linux, macOS, OpenBSD return -1 and errno == EINVAL if trying to sync a special file,
// eg a fifo, character device etc which can be ignored.
// Additionally, Linux may return EROFS if tying to sync on a readonly filesystem, which also can be ignored.
// macOS can also return ENOTSUP for pipes but dont ignore it, so that the behaviour matches Darwin's Foundation.
if fsync(_fd) < 0 {
if errno == EINVAL || errno == EROFS { return }
throw _NSErrorWithErrno(errno, reading: false)
}
#endif
}

Expand Down
7 changes: 7 additions & 0 deletions Tests/Foundation/Tests/TestFileHandle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,12 @@ class TestFileHandle : XCTestCase {
}
#endif

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

static var allTests : [(String, (TestFileHandle) -> () throws -> ())] {
var tests: [(String, (TestFileHandle) -> () throws -> ())] = [
("testReadUpToCount", testReadUpToCount),
Expand All @@ -632,6 +638,7 @@ class TestFileHandle : XCTestCase {
("test_waitForDataInBackgroundAndNotify", test_waitForDataInBackgroundAndNotify),
/* ⚠️ */ ("test_readWriteHandlers", testExpectedToFail(test_readWriteHandlers,
/* ⚠️ */ "<rdar://problem/50860781> sporadically times out")),
("testSynchronizeOnSpecialFile", testSynchronizeOnSpecialFile),
]

#if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT
Expand Down
6 changes: 6 additions & 0 deletions Tests/Foundation/Tests/TestNSData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ class TestNSData: LoopbackServerTest {
("test_writeToURLOptions", test_writeToURLOptions),
("test_writeToURLPermissions", test_writeToURLPermissions),
("test_writeToURLPermissionsWithAtomic", test_writeToURLPermissionsWithAtomic),
("test_writeToURLSpecialFile", test_writeToURLSpecialFile),
("test_edgeNoCopyDescription", test_edgeNoCopyDescription),
("test_initializeWithBase64EncodedDataGetsDecodedData", test_initializeWithBase64EncodedDataGetsDecodedData),
("test_initializeWithBase64EncodedDataWithNonBase64CharacterIsNil", test_initializeWithBase64EncodedDataWithNonBase64CharacterIsNil),
Expand Down Expand Up @@ -623,6 +624,11 @@ class TestNSData: LoopbackServerTest {
#endif
}

func test_writeToURLSpecialFile() {
let url = URL(fileURLWithPath: "/dev/stdout")
XCTAssertNoThrow(try Data("Output to STDOUT\n".utf8).write(to: url))
}

func test_emptyDescription() {
let expected = "<>"

Expand Down