Skip to content

Commit

Permalink
fix accessTests, fix terminal shell commands not showing stderr in ca…
Browse files Browse the repository at this point in the history
…se of failure, make user optional in getAndroidFolder
  • Loading branch information
hg42 committed Jul 29, 2024
1 parent a5be6d5 commit 4842a0b
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 39 deletions.
12 changes: 7 additions & 5 deletions src/main/java/com/machiav3lli/backup/handler/ShellHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,7 @@ class ShellHandler {
private fun runShellCommand(
shell: RunnableShellCommand,
command: String,
throwFail: Boolean = true,
): Shell.Result {
// defining stdout and stderr on our own
// otherwise we would have to set the flag redirect stderr to stdout:
Expand All @@ -752,19 +753,20 @@ class ShellHandler {
Timber.d("Command(s) $command ended with ${result.code}")
if (!result.isSuccess) {
addErrorCommand(command)
throw ShellCommandFailedException(result, command)
if (throwFail)
throw ShellCommandFailedException(result, command)
}
return result
}

@Throws(ShellCommandFailedException::class)
fun runAsUser(command: String): Shell.Result {
return runShellCommand(ShRunnableShellCommand(), command)
fun runAsUser(command: String, throwFail: Boolean = true): Shell.Result {
return runShellCommand(ShRunnableShellCommand(), command, throwFail)
}

@Throws(ShellCommandFailedException::class)
fun runAsRoot(command: String): Shell.Result {
return runShellCommand(SuRunnableShellCommand(), command)
fun runAsRoot(command: String, throwFail: Boolean = true): Shell.Result {
return runShellCommand(SuRunnableShellCommand(), command, throwFail)
}

fun runAsRootPipeInCollectErr(
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/machiav3lli/backup/items/Package.kt
Original file line number Diff line number Diff line change
Expand Up @@ -416,23 +416,23 @@ class Package {

fun getExternalDataPath(): String {
val user = ShellCommands.currentProfile.toString()
return getAndroidFolder(user, "data", SystemUtils::isWritablePath)
return getAndroidFolder("data", user, SystemUtils::isWritablePath)
?.absolutePath
?.plus("${File.separator}$packageName")
?: ""
}

fun getObbFilesPath(): String {
val user = ShellCommands.currentProfile.toString()
return getAndroidFolder(user, "obb", SystemUtils::isWritablePath)
return getAndroidFolder("obb", user, SystemUtils::isWritablePath)
?.absolutePath
?.plus("${File.separator}$packageName")
?: ""
}

fun getMediaFilesPath(): String {
val user = ShellCommands.currentProfile.toString()
return getAndroidFolder(user, "media", SystemUtils::isWritablePath)
return getAndroidFolder("media", user, SystemUtils::isWritablePath)
?.absolutePath
?.plus("${File.separator}$packageName")
?: ""
Expand Down
84 changes: 54 additions & 30 deletions src/main/java/com/machiav3lli/backup/preferences/TerminalPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ import com.machiav3lli.backup.OABX
import com.machiav3lli.backup.handler.LogsHandler
import com.machiav3lli.backup.handler.LogsHandler.Companion.logException
import com.machiav3lli.backup.handler.LogsHandler.Companion.share
import com.machiav3lli.backup.handler.ShellCommands
import com.machiav3lli.backup.handler.ShellHandler.Companion.needFreshShell
import com.machiav3lli.backup.handler.ShellHandler.Companion.runAsRoot
import com.machiav3lli.backup.handler.ShellHandler.Companion.runAsRootPipeOutCollectErr
Expand Down Expand Up @@ -114,8 +115,9 @@ import com.machiav3lli.backup.ui.compose.isAtTop
import com.machiav3lli.backup.ui.compose.item.RoundButton
import com.machiav3lli.backup.ui.compose.item.TopBar
import com.machiav3lli.backup.utils.SystemUtils
import com.machiav3lli.backup.utils.SystemUtils.applicationIssuer
import com.machiav3lli.backup.utils.SystemUtils.getAndroidFolder
import com.machiav3lli.backup.utils.TraceUtils.listNanoTiming
import com.topjohnwu.superuser.Shell
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
Expand All @@ -128,26 +130,29 @@ import java.time.LocalDateTime
//var terminalShell = shellDefaultBuilder().build()

fun shell(command: String, silent: Boolean = false): List<String> {
val lines = mutableListOf<String>()
var result: Shell.Result? = null
try {
//val env = "EPKG=\"${OABX.lastErrorPackage}\" ECMD=\"${OABX.lastErrorCommand}\""
//val result = runAsRoot("$env $command")
val result = runAsRoot(command)
val lines = mutableListOf<String>()
//result = runAsRoot("$env $command")
result = runAsRoot(command, throwFail = false)
if (!silent)
lines += listOf(
"--- # $command${if (!result.isSuccess) " -> ${result.code}" else " -> ok"}"
"--- # $command${if (!result.isSuccess) " => ${result.code}" else " => ok"}"
)
lines += result.err.map { "? $it" }
lines += result.out
return lines
} catch (e: Throwable) {
return listOfNotNull(
lines += listOfNotNull(
"--- # $command -> ERROR",
e::class.simpleName,
e.message,
e.cause?.message
)
}
if (result != null) {
lines += result.err.map { "? $it" }
lines += result.out
}
return lines
}

fun appInfo(): List<String> {
Expand Down Expand Up @@ -244,14 +249,33 @@ fun accessTest() =
suCommand.joinToString(" ") { "'$it'" }
} (used for streaming commands in backup/restore)"
) +
accessTest1("system app", "\$ANDROID_ASSETS", "packages (system app)") +
accessTest1("user app", "\$ANDROID_DATA/app", "packages (user app)") +
accessTest1("data", "\$ANDROID_DATA/user/0", "packages (data)") +
accessTest1("dedata", "\$ANDROID_DATA/user_de/0", "packages (dedata)") +
accessTest1("external", "\$EXTERNAL_STORAGE/Android/data", "packages (external)") +
accessTest1("obb", "\$EXTERNAL_STORAGE/Android/obb", "packages (obb)") +
accessTest1("media", "\$EXTERNAL_STORAGE/Android/media", "packages (media)") +
accessTest1("misc", "\$ANDROID_DATA/misc", "misc data")
accessTest1("system app",
"\$ANDROID_ASSETS", "packages (system app)"
) +
accessTest1("user app",
"\$ANDROID_DATA/app", "packages (user app)"
) +
accessTest1("data",
"\$ANDROID_DATA/user/${ShellCommands.currentProfile}", "packages (data)"
) +
accessTest1("dedata",
"\$ANDROID_DATA/user_de/${ShellCommands.currentProfile}", "packages (dedata)"
) +
accessTest1("external",
getAndroidFolder("data")?.path ?: "\$EXTERNAL_STORAGE/Android/data",
"packages (external)"
) +
accessTest1("obb",
getAndroidFolder("obb")?.path ?: "\$EXTERNAL_STORAGE/Android/obb",
"packages (obb)"
) +
accessTest1("media",
getAndroidFolder("media")?.path ?: "\$EXTERNAL_STORAGE/Android/media",
"packages (media)"
) +
accessTest1("misc",
"\$ANDROID_DATA/misc", "misc data"
)


fun threadsInfo(): List<String> {
Expand Down Expand Up @@ -743,6 +767,19 @@ fun TerminalPage(
}
}

@Preview
@Composable
fun PreviewTerminal() {

Box(
modifier = Modifier
//.height(500.dp)
//.width(500.dp)
) {
TerminalPage()
}
}

@Preview
@Composable
fun PreviewTerminalText() {
Expand Down Expand Up @@ -837,16 +874,3 @@ fun PreviewTestTextWidth() {
}
}

@Preview
@Composable
fun PreviewTerminal() {

Box(
modifier = Modifier
//.height(500.dp)
//.width(500.dp)
) {
TerminalPage()
}
}

3 changes: 2 additions & 1 deletion src/main/java/com/machiav3lli/backup/utils/SystemUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.content.pm.PackageManager
import com.machiav3lli.backup.BuildConfig
import com.machiav3lli.backup.OABX
import com.machiav3lli.backup.handler.LogsHandler
import com.machiav3lli.backup.handler.ShellCommands
import com.machiav3lli.backup.items.RootFile
import com.machiav3lli.backup.items.StorageFile
import kotlinx.coroutines.CoroutineDispatcher
Expand Down Expand Up @@ -202,8 +203,8 @@ object SystemUtils {
}

fun getAndroidFolder(
user: String,
subPath: String,
user: String = ShellCommands.currentProfile.toString(),
isUseablePath: (file: RootFile?) -> Boolean = ::isWritablePath
): RootFile? {
// only check access to Android folder and add subFolder even if it does not exist
Expand Down

0 comments on commit 4842a0b

Please sign in to comment.