scoop:scoop.platform:0.1.0
platform/posix/process_communicate.scoop
Selected production source · platform/posix/process_communicate.scoop
1
package scoop.platform.process3
import scoop.bytes.ByteArrayBuilder4
import scoop.platform.fs.*5
import scoop.platform.io.*6
import scoop.platform.memory.NativeByteBuffer7
import scoop.platform.system.*9
internal fun ChildProcess.transferPipes(10
input: PipeSink?,11
output: PipeSource?,12
error: PipeSource?,13
bytes: Array<Byte>?,14
deadline: ProcessDeadline?,15
abort: () -> Unit16
): (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 failure27
}28
}30
internal 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 -> @Unsafe57
{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 = 0L69
while (open[0L] || open[1L] || open[2L]) {70
val milliseconds = deadline?.milliseconds() ?: -171
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 -> @Unsafe93
{94
val(ready, code) = nativePoll(input, output, error, milliseconds)95
checkNativeStatus(ready, code, "poll process pipes")96
return@errorScope ready97
}98
}99
}100
}102
private fun <R> withHandle(index: Long, block: (NativeFileHandle) -> R): R =103
if (open[index])104
files[index]!!.withHandle(block)105
else106
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 0L115
throw error116
}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
return128
throw error129
}130
if (received == 0L)131
close(index)132
else133
builder.append(buffer.readBytes(received))134
}136
private fun close(index: Long) {137
if (!open[index])138
return139
open[index] = false140
files[index]!!.close()141
}143
public override fun close() {144
var failure: Throwable? = None145
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
}158
}160
@Extern(name = "scoop_platform_pipe_nonblocking", captureErrno = true)161
private fun nativeNonblocking(handle: NativeFileHandle): (Int, Int)163
@Extern(name = "scoop_platform_process_poll", captureErrno = true)164
private fun nativePoll(165
input: NativeFileHandle,166
output: NativeFileHandle,167
error: NativeFileHandle,168
milliseconds: Int169
): (Int, Int)