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, 14 insertions, 3 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 eace2e7..a3b5cea 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,8 @@ import java.io.InputStreamReader | |||
13 | */ | 13 | */ |
14 | internal object Executor { | 14 | internal object Executor { |
15 | 15 | ||
16 | private const val FAILED = -1 | ||
17 | |||
16 | class CommandExecutionException(msg: String) : Exception(msg) | 18 | class CommandExecutionException(msg: String) : Exception(msg) |
17 | 19 | ||
18 | init { | 20 | init { |
@@ -20,14 +22,23 @@ internal object Executor { | |||
20 | } | 22 | } |
21 | 23 | ||
22 | /** | 24 | /** |
23 | * @return -1 on error, forked child PID otherwise | 25 | * @return FAILED (-1) on error, forked child PID otherwise |
24 | */ | 26 | */ |
25 | private external fun forkExec(argcv: Array<String>): Int | 27 | private external fun forkExec(argcv: Array<String>): Int |
26 | 28 | ||
29 | /** | ||
30 | * @return FAILED (-1) on error, 0 on no-op, the supplied PID otherwise | ||
31 | */ | ||
32 | private external fun wait(pid: Int): Int | ||
33 | |||
27 | private fun read(stream: InputStream) = BufferedReader(InputStreamReader(stream)).readLines() | 34 | private fun read(stream: InputStream) = BufferedReader(InputStreamReader(stream)).readLines() |
28 | 35 | ||
29 | fun forkExec(cmd: Command) { | 36 | fun forkExec(cmd: Command): CompletableFuture<Void> { |
30 | if (forkExec(cmd.asArray()) == -1) throw CommandExecutionException("Could not fork child process.") | 37 | val pid = forkExec(cmd.asArray()) |
38 | return when (pid) { | ||
39 | FAILED -> CompletableFuture.failedFuture(CommandExecutionException("Could not fork child process.")) | ||
40 | else -> CompletableFuture.runAsync { wait(pid) } | ||
41 | } | ||
31 | } | 42 | } |
32 | 43 | ||
33 | fun call(cmd: Command): CompletableFuture<List<String>> { | 44 | fun call(cmd: Command): CompletableFuture<List<String>> { |