Skip to content

Commit fb53fe9

Browse files
committed
WIP: Fixing review comments
1 parent 55f86af commit fb53fe9

File tree

8 files changed

+355
-158
lines changed

8 files changed

+355
-158
lines changed

Sources/FoundationEssentials/Subprocess/Platforms/Subprocess+Darwin.swift

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,14 @@ extension Subprocess.Configuration {
4949
defer {
5050
posix_spawn_file_actions_destroy(&fileActions)
5151
}
52-
53-
var result = posix_spawn_file_actions_adddup2(&fileActions, input.getReadFileDescriptor().rawValue, 0)
54-
guard result == 0 else {
55-
try self.cleanupAll(input: input, output: output, error: error)
56-
throw POSIXError(.init(rawValue: result) ?? .ENODEV)
52+
// Input
53+
var result: Int32 = -1
54+
if let inputRead = input.getReadFileDescriptor() {
55+
result = posix_spawn_file_actions_adddup2(&fileActions, inputRead.rawValue, 0)
56+
guard result == 0 else {
57+
try self.cleanupAll(input: input, output: output, error: error)
58+
throw POSIXError(.init(rawValue: result) ?? .ENODEV)
59+
}
5760
}
5861
if let inputWrite = input.getWriteFileDescriptor() {
5962
// Close parent side
@@ -63,10 +66,13 @@ extension Subprocess.Configuration {
6366
throw POSIXError(.init(rawValue: result) ?? .ENODEV)
6467
}
6568
}
66-
result = posix_spawn_file_actions_adddup2(&fileActions, output.getWriteFileDescriptor().rawValue, 1)
67-
guard result == 0 else {
68-
try self.cleanupAll(input: input, output: output, error: error)
69-
throw POSIXError(.init(rawValue: result) ?? .ENODEV)
69+
// Output
70+
if let outputWrite = output.getWriteFileDescriptor() {
71+
result = posix_spawn_file_actions_adddup2(&fileActions, outputWrite.rawValue, 1)
72+
guard result == 0 else {
73+
try self.cleanupAll(input: input, output: output, error: error)
74+
throw POSIXError(.init(rawValue: result) ?? .ENODEV)
75+
}
7076
}
7177
if let outputRead = output.getReadFileDescriptor() {
7278
// Close parent side
@@ -76,10 +82,13 @@ extension Subprocess.Configuration {
7682
throw POSIXError(.init(rawValue: result) ?? .ENODEV)
7783
}
7884
}
79-
result = posix_spawn_file_actions_adddup2(&fileActions, error.getWriteFileDescriptor().rawValue, 2)
80-
guard result == 0 else {
81-
try self.cleanupAll(input: input, output: output, error: error)
82-
throw POSIXError(.init(rawValue: result) ?? .ENODEV)
85+
// Error
86+
if let errorWrite = error.getWriteFileDescriptor() {
87+
result = posix_spawn_file_actions_adddup2(&fileActions, errorWrite.rawValue, 2)
88+
guard result == 0 else {
89+
try self.cleanupAll(input: input, output: output, error: error)
90+
throw POSIXError(.init(rawValue: result) ?? .ENODEV)
91+
}
8392
}
8493
if let errorRead = error.getReadFileDescriptor() {
8594
// Close parent side

Sources/FoundationEssentials/Subprocess/Subprocess+API.swift

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ extension Subprocess {
3232
output: .init(method: output.method),
3333
error: .init(method: error.method)
3434
) { subprocess in
35+
let (standardOutput, standardError) = try await subprocess.captureIOs()
3536
return (
3637
processIdentifier: subprocess.processIdentifier,
37-
standardOutput: try subprocess.captureStandardOutput(),
38-
standardError: try subprocess.captureStandardError()
38+
standardOutput: standardOutput,
39+
standardError: standardError
3940
)
4041
}
4142
return CollectedResult(
@@ -64,13 +65,14 @@ extension Subprocess {
6465
platformOptions: platformOptions,
6566
output: .init(method: output.method),
6667
error: .init(method: output.method)
67-
) { execution, writer in
68+
) { subprocess, writer in
6869
try await writer.write(input)
6970
try await writer.finish()
71+
let (standardOutput, standardError) = try await subprocess.captureIOs()
7072
return (
71-
processIdentifier: execution.processIdentifier,
72-
standardOutput: try execution.captureStandardOutput(),
73-
standardError: try execution.captureStandardError()
73+
processIdentifier: subprocess.processIdentifier,
74+
standardOutput: standardOutput,
75+
standardError: standardError
7476
)
7577
}
7678
return CollectedResult(
@@ -99,13 +101,14 @@ extension Subprocess {
99101
platformOptions: platformOptions,
100102
output: .init(method: output.method),
101103
error: .init(method: output.method)
102-
) { execution, writer in
104+
) { subprocess, writer in
103105
try await writer.write(input)
104106
try await writer.finish()
107+
let (standardOutput, standardError) = try await subprocess.captureIOs()
105108
return (
106-
processIdentifier: execution.processIdentifier,
107-
standardOutput: try execution.captureStandardOutput(),
108-
standardError: try execution.captureStandardError()
109+
processIdentifier: subprocess.processIdentifier,
110+
standardOutput: standardOutput,
111+
standardError: standardError
109112
)
110113
}
111114
return CollectedResult(

Sources/FoundationEssentials/Subprocess/Subprocess+AsyncBytes.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@ extension Subprocess {
4141
}
4242
try Task.checkCancellation()
4343
do {
44-
self.buffer = try await self.read(
45-
from: self.fileDescriptor,
46-
maxLength: AsyncBytes.bufferSize)
44+
self.buffer = try await self.fileDescriptor.read(
45+
upToLength: AsyncBytes.bufferSize)
4746
self.currentPosition = 0
4847
if self.buffer.count < AsyncBytes.bufferSize {
4948
self.finished = true

Sources/FoundationEssentials/Subprocess/Subprocess+Configuration.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ extension Subprocess {
184184
_ body: @Sendable @escaping (Subprocess, StandardInputWriter) async throws -> R
185185
) async throws -> Result<R> {
186186
let (readFd, writeFd) = try FileDescriptor.pipe()
187-
let executionInput: ExecutionInput = .customWrite(readFd, writeFd)
187+
let executionInput: ExecutionInput = .init(storage: .customWrite(readFd, writeFd))
188188
let executionOutput: ExecutionOutput = try output.createExecutionOutput()
189189
let executionError: ExecutionOutput = try error.createExecutionOutput()
190190
let process: Subprocess = try self.spawn(

0 commit comments

Comments
 (0)