Skip to content

Commit 429dc96

Browse files
committed
Introduce FileDescriptorInput.standardInput, FileDescriptorOutput.standardOutput, and FileDescriptorOutput.standardError to easily bind current process' descriptors to child processes
1 parent 680621b commit 429dc96

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

Sources/Subprocess/Configuration.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ public struct Configuration: Sendable {
9494
var _spawnResult = spawnResultBox!.take()!
9595

9696
let execution = _spawnResult.execution
97+
defer {
98+
// Close process file descriptor now we finished monitoring
99+
execution.processIdentifier.close()
100+
}
97101

98102
return try await withAsyncTaskCleanupHandler {
99103
let inputIO = _spawnResult.inputWriteEnd()
@@ -118,9 +122,6 @@ public struct Configuration: Sendable {
118122
for: execution.processIdentifier
119123
)
120124

121-
// Close process file descriptor now we finished monitoring
122-
execution.processIdentifier.close()
123-
124125
return ExecutionResult(
125126
terminationStatus: terminationStatus,
126127
value: try result.get()

Sources/Subprocess/IO/AsyncIO+Linux.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ final class AsyncIO: Sendable {
288288
)
289289
if rc != 0 {
290290
_registration.withLock { storage in
291-
storage.removeValue(forKey: fileDescriptor.rawValue)
291+
_ = storage.removeValue(forKey: fileDescriptor.rawValue)
292292
}
293293

294294
let capturedError = errno

Sources/Subprocess/IO/Input.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,15 @@ extension InputProtocol where Self == FileDescriptorInput {
168168
closeAfterSpawningProcess: closeAfterSpawningProcess
169169
)
170170
}
171+
172+
/// Create a Subprocess input that reads from the standard input of
173+
/// current process. The file descriptor should not be closed afterwards.
174+
public static var standardInput: Self {
175+
return Self.fileDescriptor(
176+
.standardInput,
177+
closeAfterSpawningProcess: false
178+
)
179+
}
171180
}
172181

173182
extension InputProtocol {

Sources/Subprocess/IO/Output.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,24 @@ extension OutputProtocol where Self == FileDescriptorOutput {
218218
) -> Self {
219219
return .init(fileDescriptor: fd, closeAfterSpawningProcess: closeAfterSpawningProcess)
220220
}
221+
222+
/// Create a Subprocess output that writes output to the standard output of
223+
/// current process. The file descriptor should not be closed afterwards.
224+
public static var standardOutput: Self {
225+
return Self.fileDescriptor(
226+
.standardOutput,
227+
closeAfterSpawningProcess: false
228+
)
229+
}
230+
231+
/// Create a Subprocess output that write output to the standard error of
232+
/// current process. The file descriptor should not be closed afterwards.
233+
public static var standardError: Self {
234+
return Self.fileDescriptor(
235+
.standardError,
236+
closeAfterSpawningProcess: false
237+
)
238+
}
221239
}
222240

223241
extension OutputProtocol where Self == StringOutput<UTF8> {

0 commit comments

Comments
 (0)