diff --git a/Sources/TSCBasic/FileSystem.swift b/Sources/TSCBasic/FileSystem.swift index 982a8039..6ff7b3fb 100644 --- a/Sources/TSCBasic/FileSystem.swift +++ b/Sources/TSCBasic/FileSystem.swift @@ -493,8 +493,7 @@ private struct LocalFileSystem: FileSystem { func readFileContents(_ path: AbsolutePath) throws -> ByteString { // Open the file. - let fp = fopen(path.pathString, "rb") - if fp == nil { + guard let fp = fopen(path.pathString, "rb") else { throw FileSystemError(errno: errno, path) } defer { fclose(fp) } @@ -523,8 +522,7 @@ private struct LocalFileSystem: FileSystem { func writeFileContents(_ path: AbsolutePath, bytes: ByteString) throws { // Open the file. - let fp = fopen(path.pathString, "wb") - if fp == nil { + guard let fp = fopen(path.pathString, "wb") else { throw FileSystemError(errno: errno, path) } defer { fclose(fp) } diff --git a/Sources/TSCBasic/Process.swift b/Sources/TSCBasic/Process.swift index 6c8aa117..89d3a5be 100644 --- a/Sources/TSCBasic/Process.swift +++ b/Sources/TSCBasic/Process.swift @@ -144,6 +144,9 @@ public final class Process { /// The current OS does not support the workingDirectory API. case workingDirectoryNotSupported + + /// The stdin could not be opened. + case stdinUnavailable } public enum OutputRedirection { @@ -677,7 +680,10 @@ public final class Process { var stdinPipe: [Int32] = [-1, -1] try open(pipe: &stdinPipe) - let stdinStream = try LocalFileOutputByteStream(filePointer: fdopen(stdinPipe[1], "wb"), closeOnDeinit: true) + guard let fp = fdopen(stdinPipe[1], "wb") else { + throw Process.Error.stdinUnavailable + } + let stdinStream = try LocalFileOutputByteStream(filePointer: fp, closeOnDeinit: true) // Dupe the read portion of the remote to 0. posix_spawn_file_actions_adddup2(&fileActions, stdinPipe[0], 0) @@ -1258,6 +1264,8 @@ extension Process.Error: CustomStringConvertible { return "could not find executable for '\(program)'" case .workingDirectoryNotSupported: return "workingDirectory is not supported in this platform" + case .stdinUnavailable: + return "could not open stdin on this platform" } } } diff --git a/Sources/TSCBasic/WritableByteStream.swift b/Sources/TSCBasic/WritableByteStream.swift index aee907e3..5b6e4c2c 100644 --- a/Sources/TSCBasic/WritableByteStream.swift +++ b/Sources/TSCBasic/WritableByteStream.swift @@ -790,7 +790,7 @@ public final class LocalFileOutputByteStream: FileOutputByteStream { override final func writeImpl(_ bytes: ArraySlice) { bytes.withUnsafeBytes { bytesPtr in while true { - let n = fwrite(bytesPtr.baseAddress, 1, bytesPtr.count, filePointer) + let n = fwrite(bytesPtr.baseAddress!, 1, bytesPtr.count, filePointer) if n < 0 { if errno == EINTR { continue } errorDetected(code: errno) diff --git a/Sources/TSCTestSupport/PseudoTerminal.swift b/Sources/TSCTestSupport/PseudoTerminal.swift index 59610b61..2797c719 100644 --- a/Sources/TSCTestSupport/PseudoTerminal.swift +++ b/Sources/TSCTestSupport/PseudoTerminal.swift @@ -24,7 +24,7 @@ public final class PseudoTerminal { if openpty(&primary, &secondary, nil, nil, nil) != 0 { return nil } - guard let outStream = try? LocalFileOutputByteStream(filePointer: fdopen(secondary, "w"), closeOnDeinit: false) else { + guard let outStream = try? LocalFileOutputByteStream(filePointer: fdopen(secondary, "w")!, closeOnDeinit: false) else { return nil } self.outStream = outStream diff --git a/Tests/TSCBasicTests/PathShimTests.swift b/Tests/TSCBasicTests/PathShimTests.swift index 1af04a37..76fd6324 100644 --- a/Tests/TSCBasicTests/PathShimTests.swift +++ b/Tests/TSCBasicTests/PathShimTests.swift @@ -39,7 +39,7 @@ class WalkTests : XCTestCase { var expected: [AbsolutePath] = [ "\(root)/usr", "\(root)/bin", - "\(root)/xbin" + "\(root)/etc" ] #else let root = ""