Refine RootSession.Transaction.iptables

This commit is contained in:
Mygod
2019-01-24 21:11:38 +08:00
parent f60c42a54c
commit 0da1943a68
5 changed files with 22 additions and 21 deletions

View File

@@ -54,16 +54,18 @@ class RootSession : AutoCloseable {
instance.haltTimeout()
instance.close()
}
fun checkOutput(command: String, result: Shell.Result, out: Boolean = result.out.isNotEmpty(),
err: Boolean = result.err.isNotEmpty()): String {
val msg = StringBuilder("$command exited with ${result.code}")
if (out) result.out.forEach { msg.append("\n$it") }
if (err) result.err.forEach { msg.append("\nE $it") }
if (!result.isSuccess || out || err) throw UnexpectedOutputException(msg.toString())
return msg.toString()
}
}
class UnexpectedOutputException(msg: String) : RuntimeException(msg)
fun checkOutput(command: String, result: Shell.Result, out: Boolean = result.out.isNotEmpty(),
err: Boolean = result.err.isNotEmpty()): String {
val msg = StringBuilder("$command exited with ${result.code}")
if (out) result.out.forEach { msg.append("\n$it") }
if (err) result.err.forEach { msg.append("\nE $it") }
if (!result.isSuccess || out || err) throw UnexpectedOutputException(msg.toString()) else return msg.toString()
}
private val shell = Shell.newInstance("su")
private val stdout = ArrayList<String>()
@@ -114,15 +116,11 @@ class RootSession : AutoCloseable {
inner class Transaction {
private val revertCommands = LinkedList<String>()
fun exec(command: String, revert: String? = null, wait: Boolean = false) {
fun exec(command: String, revert: String? = null) = checkOutput(command, execQuiet(command, revert))
fun execQuiet(command: String, revert: String? = null): Shell.Result {
if (revert != null) revertCommands.addFirst(revert) // add first just in case exec fails
if (wait) {
val result = this@RootSession.execQuiet(command)
val message = checkOutput(command, result, err = false)
if (result.err.isNotEmpty()) Timber.i(message)
} else this@RootSession.exec(command)
return this@RootSession.execQuiet(command)
}
fun execQuiet(command: String) = this@RootSession.execQuiet(command)
fun commit() = unlock()