From 6fa7eed13810df09e763abe252c50351a68ff9b0 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 24 Oct 2026 06:00:44 +0000 Subject: [PATCH] app: handle errors when saving logs Saving the install, action or Magisk log didn't handle any error: if creating or writing the file failed (e.g. storage full, or the media store rejecting the file), the exception reached the default handler and the app exited. On Android 10 and older, a module name containing a slash also made the action log name point to a missing directory. Save logs through a shared helper that replaces slashes in the name and shows an error toast when saving fails. Assisted-by: Claude Opus 5.5 --- .../topjohnwu/magisk/arch/BaseViewModel.kt | 30 +++++++++++ .../magisk/ui/flash/FlashViewModel.kt | 29 ++++------- .../topjohnwu/magisk/ui/log/LogViewModel.kt | 52 ++++++++----------- .../magisk/ui/module/ActionViewModel.kt | 19 ++----- app/core/src/main/res/values/strings.xml | 1 + 5 files changed, 70 insertions(+), 61 deletions(-) diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/arch/BaseViewModel.kt b/app/apk/src/main/java/com/topjohnwu/magisk/arch/BaseViewModel.kt index 26d1e93f1..a99018def 100644 --- a/app/apk/src/main/java/com/topjohnwu/magisk/arch/BaseViewModel.kt +++ b/app/apk/src/main/java/com/topjohnwu/magisk/arch/BaseViewModel.kt @@ -3,12 +3,21 @@ package com.topjohnwu.magisk.arch import android.widget.Toast import androidx.annotation.StringRes import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope import com.topjohnwu.magisk.core.AppContext +import com.topjohnwu.magisk.core.R import com.topjohnwu.magisk.core.ktx.toast +import com.topjohnwu.magisk.core.utils.MediaStoreUtils +import com.topjohnwu.magisk.core.utils.MediaStoreUtils.outputStream import com.topjohnwu.magisk.ui.navigation.Route +import kotlinx.coroutines.CancellationException +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.receiveAsFlow +import kotlinx.coroutines.launch +import timber.log.Timber +import java.io.Writer abstract class BaseViewModel : ViewModel() { @@ -28,4 +37,25 @@ abstract class BaseViewModel : ViewModel() { fun navigateTo(route: Route) { _navEvents.trySend(route) } + + /** + * Save a log to the download folder with [write], then show where it was saved. Saving can + * fail for many reasons (e.g. storage full, or the media store rejecting the file), so + * report failures instead of letting them crash the app. + */ + protected fun saveLogFile(name: String, write: (Writer) -> Unit) { + viewModelScope.launch(Dispatchers.IO) { + try { + // Names can contain parts like module names, which must not create directories + val file = MediaStoreUtils.getFile(name.replace('/', '_')) + file.uri.outputStream().bufferedWriter().use(write) + showToast(file.toString()) + } catch (e: CancellationException) { + throw e + } catch (e: Exception) { + Timber.e(e) + showToast(R.string.log_save_failed) + } + } + } } diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/ui/flash/FlashViewModel.kt b/app/apk/src/main/java/com/topjohnwu/magisk/ui/flash/FlashViewModel.kt index 94bad8e6d..cef558595 100644 --- a/app/apk/src/main/java/com/topjohnwu/magisk/ui/flash/FlashViewModel.kt +++ b/app/apk/src/main/java/com/topjohnwu/magisk/ui/flash/FlashViewModel.kt @@ -14,10 +14,8 @@ import com.topjohnwu.magisk.core.ktx.timeFormatStandard import com.topjohnwu.magisk.core.ktx.toTime import com.topjohnwu.magisk.core.ktx.writeTo import com.topjohnwu.magisk.core.tasks.MagiskInstaller -import com.topjohnwu.magisk.core.utils.MediaStoreUtils import com.topjohnwu.magisk.core.utils.MediaStoreUtils.displayName import com.topjohnwu.magisk.core.utils.MediaStoreUtils.inputStream -import com.topjohnwu.magisk.core.utils.MediaStoreUtils.outputStream import com.topjohnwu.magisk.terminal.TerminalEmulator import com.topjohnwu.magisk.terminal.appendLineOnMain import com.topjohnwu.magisk.terminal.runSuCommand @@ -196,25 +194,20 @@ class FlashViewModel(private val savedState: SavedStateHandle) : BaseViewModel() } fun saveLog() { - viewModelScope.launch(Dispatchers.IO) { - val name = "magisk_install_log_%s.log".format( - System.currentTimeMillis().toTime(timeFormatStandard) - ) - val file = MediaStoreUtils.getFile(name) - file.uri.outputStream().bufferedWriter().use { writer -> - val transcript = emulator.screen.transcriptText - if (flashAction == Const.Value.FLASH_ZIP) { - writer.write(transcript) - } else { - synchronized(logItems) { - logItems.forEach { - writer.write(it) - writer.newLine() - } + val name = "magisk_install_log_%s.log".format( + System.currentTimeMillis().toTime(timeFormatStandard) + ) + saveLogFile(name) { writer -> + if (flashAction == Const.Value.FLASH_ZIP) { + writer.write(emulator.screen.transcriptText) + } else { + synchronized(logItems) { + logItems.forEach { + writer.write(it) + writer.write("\n") } } } - showToast(file.toString()) } } diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/ui/log/LogViewModel.kt b/app/apk/src/main/java/com/topjohnwu/magisk/ui/log/LogViewModel.kt index a87873035..215cacc8b 100644 --- a/app/apk/src/main/java/com/topjohnwu/magisk/ui/log/LogViewModel.kt +++ b/app/apk/src/main/java/com/topjohnwu/magisk/ui/log/LogViewModel.kt @@ -11,8 +11,6 @@ import com.topjohnwu.magisk.core.ktx.toTime import com.topjohnwu.magisk.core.model.su.SuLog import com.topjohnwu.magisk.core.repository.LogRepository import com.topjohnwu.magisk.core.su.SuEvents -import com.topjohnwu.magisk.core.utils.MediaStoreUtils -import com.topjohnwu.magisk.core.utils.MediaStoreUtils.outputStream import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow @@ -63,38 +61,34 @@ class LogViewModel( } fun saveMagiskLog() { - viewModelScope.launch(Dispatchers.IO) { - val filename = "magisk_log_%s.log".format( - System.currentTimeMillis().toTime(timeFormatStandard)) - val logFile = MediaStoreUtils.getFile(filename) - logFile.uri.outputStream().bufferedWriter().use { file -> - file.write("---Detected Device Info---\n\n") - file.write("isAB=${Info.isAB}\n") - file.write("isSAR=${Info.isSAR}\n") - file.write("ramdisk=${Info.ramdisk}\n") - val uname = Os.uname() - file.write("kernel=${uname.sysname} ${uname.machine} ${uname.release} ${uname.version}\n") + val filename = "magisk_log_%s.log".format( + System.currentTimeMillis().toTime(timeFormatStandard)) + saveLogFile(filename) { file -> + file.write("---Detected Device Info---\n\n") + file.write("isAB=${Info.isAB}\n") + file.write("isSAR=${Info.isSAR}\n") + file.write("ramdisk=${Info.ramdisk}\n") + val uname = Os.uname() + file.write("kernel=${uname.sysname} ${uname.machine} ${uname.release} ${uname.version}\n") - file.write("\n\n---System Properties---\n\n") - ProcessBuilder("getprop").start() - .inputStream.reader().use { it.copyTo(file) } + file.write("\n\n---System Properties---\n\n") + ProcessBuilder("getprop").start() + .inputStream.reader().use { it.copyTo(file) } - file.write("\n\n---Environment Variables---\n\n") - System.getenv().forEach { (key, value) -> file.write("${key}=${value}\n") } + file.write("\n\n---Environment Variables---\n\n") + System.getenv().forEach { (key, value) -> file.write("${key}=${value}\n") } - file.write("\n\n---System MountInfo---\n\n") - FileInputStream("/proc/self/mountinfo").reader().use { it.copyTo(file) } + file.write("\n\n---System MountInfo---\n\n") + FileInputStream("/proc/self/mountinfo").reader().use { it.copyTo(file) } - file.write("\n---Magisk Logs---\n") - file.write("${Info.env.versionString} (${Info.env.versionCode})\n\n") - if (Info.env.isActive) file.write(magiskLogRaw) + file.write("\n---Magisk Logs---\n") + file.write("${Info.env.versionString} (${Info.env.versionCode})\n\n") + if (Info.env.isActive) file.write(magiskLogRaw) - file.write("\n---Manager Logs---\n") - file.write("${BuildConfig.APP_VERSION_NAME} (${BuildConfig.APP_VERSION_CODE})\n\n") - ProcessBuilder("logcat", "-d").start() - .inputStream.reader().use { it.copyTo(file) } - } - showToast(logFile.toString()) + file.write("\n---Manager Logs---\n") + file.write("${BuildConfig.APP_VERSION_NAME} (${BuildConfig.APP_VERSION_CODE})\n\n") + ProcessBuilder("logcat", "-d").start() + .inputStream.reader().use { it.copyTo(file) } } } diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/ui/module/ActionViewModel.kt b/app/apk/src/main/java/com/topjohnwu/magisk/ui/module/ActionViewModel.kt index 08b047663..f43ff4739 100644 --- a/app/apk/src/main/java/com/topjohnwu/magisk/ui/module/ActionViewModel.kt +++ b/app/apk/src/main/java/com/topjohnwu/magisk/ui/module/ActionViewModel.kt @@ -5,8 +5,6 @@ import androidx.lifecycle.viewModelScope import com.topjohnwu.magisk.arch.BaseViewModel import com.topjohnwu.magisk.core.ktx.timeFormatStandard import com.topjohnwu.magisk.core.ktx.toTime -import com.topjohnwu.magisk.core.utils.MediaStoreUtils -import com.topjohnwu.magisk.core.utils.MediaStoreUtils.outputStream import com.topjohnwu.magisk.terminal.TerminalEmulator import com.topjohnwu.magisk.terminal.runSuCommand import com.topjohnwu.magisk.terminal.shellQuoted @@ -70,18 +68,11 @@ class ActionViewModel(private val savedState: SavedStateHandle) : BaseViewModel( } fun saveLog() { - viewModelScope.launch(Dispatchers.IO) { - val name = "%s_action_log_%s.log".format( - actionName, - System.currentTimeMillis().toTime(timeFormatStandard) - ) - val file = MediaStoreUtils.getFile(name) - file.uri.outputStream().bufferedWriter().use { writer -> - val transcript = emulator.screen.transcriptText - writer.write(transcript) - } - showToast(file.toString()) - } + val name = "%s_action_log_%s.log".format( + actionName, + System.currentTimeMillis().toTime(timeFormatStandard) + ) + saveLogFile(name) { it.write(emulator.screen.transcriptText) } } companion object { diff --git a/app/core/src/main/res/values/strings.xml b/app/core/src/main/res/values/strings.xml index 9c3632352..3c0ed388a 100644 --- a/app/core/src/main/res/values/strings.xml +++ b/app/core/src/main/res/values/strings.xml @@ -89,6 +89,7 @@ Save log Clear log Log successfully cleared + Failed to save the log PID: %1$d Target UID: %1$d Target PID: %s