diff options
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/commands/Executor.kt')
-rw-r--r-- | app/src/main/java/org/pacien/tincapp/commands/Executor.kt | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/commands/Executor.kt b/app/src/main/java/org/pacien/tincapp/commands/Executor.kt index eb04f6d..fedd0d2 100644 --- a/app/src/main/java/org/pacien/tincapp/commands/Executor.kt +++ b/app/src/main/java/org/pacien/tincapp/commands/Executor.kt | |||
@@ -13,6 +13,7 @@ import java.io.InputStreamReader | |||
13 | */ | 13 | */ |
14 | internal object Executor { | 14 | internal object Executor { |
15 | private const val FAILED = -1 | 15 | private const val FAILED = -1 |
16 | private const val SUCCESS = 0 | ||
16 | 17 | ||
17 | class CommandExecutionException(msg: String) : Exception(msg) | 18 | class CommandExecutionException(msg: String) : Exception(msg) |
18 | 19 | ||
@@ -26,17 +27,23 @@ internal object Executor { | |||
26 | private external fun forkExec(argcv: Array<String>): Int | 27 | private external fun forkExec(argcv: Array<String>): Int |
27 | 28 | ||
28 | /** | 29 | /** |
29 | * @return FAILED (-1) on error, 0 on no-op, the supplied PID otherwise | 30 | * @return FAILED (-1) on error, the exit status of the process otherwise |
30 | */ | 31 | */ |
31 | private external fun wait(pid: Int): Int | 32 | private external fun wait(pid: Int): Int |
32 | 33 | ||
33 | private fun read(stream: InputStream) = BufferedReader(InputStreamReader(stream)).readLines() | 34 | private fun read(stream: InputStream) = BufferedReader(InputStreamReader(stream)).readLines() |
34 | 35 | ||
35 | fun forkExec(cmd: Command): CompletableFuture<Void> { | 36 | fun forkExec(cmd: Command): CompletableFuture<Void> { |
36 | val pid = forkExec(cmd.asArray()) | 37 | val pid = forkExec(cmd.asArray()).also { |
37 | return when (pid) { | 38 | if (it == FAILED) throw CommandExecutionException("Could not fork child process.") |
38 | FAILED -> CompletableFuture.failedFuture(CommandExecutionException("Could not fork child process.")) | 39 | } |
39 | else -> CompletableFuture.runAsync { wait(pid) } | 40 | |
41 | return CompletableFuture.runAsync { | ||
42 | when (wait(pid)) { | ||
43 | SUCCESS -> Unit | ||
44 | FAILED -> throw CommandExecutionException("Process terminated abnormally.") | ||
45 | else -> throw CommandExecutionException("Non-zero exit status code.") | ||
46 | } | ||
40 | } | 47 | } |
41 | } | 48 | } |
42 | 49 | ||