Scoop API

scoop:scoop.platform:0.1.0

platform/posix/process_communicate.scoop

Selected production source · platform/posix/process_communicate.scoop

1package scoop.platform.process
3import scoop.bytes.ByteArrayBuilder
4import scoop.platform.fs.*
5import scoop.platform.io.*
6import scoop.platform.memory.NativeByteBuffer
7import scoop.platform.system.*
9internal fun ChildProcess.transferPipes(
10 input: PipeSink?,
11 output: PipeSource?,
12 error: PipeSource?,
13 bytes: Array<Byte>?,
14 deadline: ProcessDeadline?,
15 abort: () -> Unit
16): (Array<Byte>, Array<Byte>) {
17 try {
18 return ProcessPipes(input, output, error).transfer(bytes, deadline)
19 } catch (failure: Throwable) {
20 try {
21 abort()
22 }
23 catch (cleanup: Throwable) {
24 throw ResourceUseException(failure, cleanup)
25 }
26 throw failure
27 }
30internal class ProcessPipes internal constructor(
31 input: PipeSink?,
32 output: PipeSource?,
33 error: PipeSource?
34): Closeable {
35 private val files: Array<File?> = [input?.file, output?.file, error?.file]
36 private val open: MutableArray<Boolean> = MutableArray<Boolean>(3L) { false }
38 init {
39 for (index in 0L ..< 3L)
40 open[index] = !(files[index]?.isClosed ?: true)
41 }
43 internal fun transfer(input: Array<Byte>?, deadline: ProcessDeadline?): (
44 Array<Byte>,
45 Array<Byte>
46 ) =
47 use {
48 val bytes = input ?: Array<Byte>(0L) { 0.toByte() }
49 val output = ByteArrayBuilder()
50 val error = ByteArrayBuilder()
51 if ((deadline?.milliseconds() ?: -1) == 0 && (open[0L] || open[1L] || open[2L]))
52 throw ProcessTimeoutException([], [])
53 for (index in 0L ..< 3L) {
54 if (open[index]) {
55 files[index]!!.withHandle {
56 fd -> @Unsafe
57 {
58 val(status, code) = nativeNonblocking(fd)
59 checkNativeStatus(status, code, "configure process pipe")
60 }
61 }
62 }
63 }
64 if (bytes.size == 0L)
65 close(0L)
66 NativeByteBuffer.allocate(8192L).use {
67 buffer ->
68 var written = 0L
69 while (open[0L] || open[1L] || open[2L]) {
70 val milliseconds = deadline?.milliseconds() ?: -1
71 if (milliseconds == 0)
72 throw ProcessTimeoutException(output.toByteArray(), error.toByteArray())
73 val ready = poll(milliseconds)
74 if ((ready and 1) != 0) {
75 written += writeInput(bytes, written)
76 if (written == bytes.size)
77 close(0L)
78 }
79 if ((ready and 2) != 0)
80 readOutput(1L, output, buffer)
81 if ((ready and 4) != 0)
82 readOutput(2L, error, buffer)
83 }
84 }
85 (output.toByteArray(), error.toByteArray())
86 }
88 private fun poll(milliseconds: Int): Int =
89 withHandle(0L) inputScope@{
90 input -> withHandle(1L) outputScope@{
91 output -> withHandle(2L) errorScope@{
92 error -> @Unsafe
93 {
94 val(ready, code) = nativePoll(input, output, error, milliseconds)
95 checkNativeStatus(ready, code, "poll process pipes")
96 return@errorScope ready
97 }
98 }
99 }
100 }
102 private fun <R> withHandle(index: Long, block: (NativeFileHandle) -> R): R =
103 if (open[index])
104 files[index]!!.withHandle(block)
105 else
106 block(NativeFileHandle(ULong.MAX_VALUE))
108 private fun writeInput(bytes: Array<Byte>, offset: Long): Long {
109 try {
110 return files[0L]!!.writeFrom(bytes, offset)
111 }
112 catch (error: PlatformException) {
113 if (error.kind == PlatformErrorKind.WouldBlock)
114 return 0L
115 throw error
116 }
117 }
119 private fun readOutput(index: Long, builder: ByteArrayBuilder, buffer: NativeByteBuffer) {
120 buffer.clear()
121 val received =
122 try {
123 files[index]!!.readInto(buffer)
124 }
125 catch (error: PlatformException) {
126 if (error.kind == PlatformErrorKind.WouldBlock)
127 return
128 throw error
129 }
130 if (received == 0L)
131 close(index)
132 else
133 builder.append(buffer.readBytes(received))
134 }
136 private fun close(index: Long) {
137 if (!open[index])
138 return
139 open[index] = false
140 files[index]!!.close()
141 }
143 public override fun close() {
144 var failure: Throwable? = None
145 for (index in 0L ..< 3L) {
146 try {
147 close(index)
148 }
149 catch (error: Throwable) {
150 failure = Some(combineProcessFailure(failure, error))
151 }
152 }
153 when (failure) {
154 case Some(val error) -> throw error;
155 case None -> {}
156 }
157 }
160@Extern(name = "scoop_platform_pipe_nonblocking", captureErrno = true)
161private fun nativeNonblocking(handle: NativeFileHandle): (Int, Int)
163@Extern(name = "scoop_platform_process_poll", captureErrno = true)
164private fun nativePoll(
165 input: NativeFileHandle,
166 output: NativeFileHandle,
167 error: NativeFileHandle,
168 milliseconds: Int
169): (Int, Int)