From 3e9b09f5d52f210fba40e81d2b4cdac2a8bd6007 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Tue, 6 Oct 2026 21:26:37 +0000 Subject: [PATCH] app: add WholeFilePatcher and ImageExtractor Define WholeFilePatcher and ImageExtractor interfaces for whole-file patching and single-image extraction pipelines. Have TarProcessor implement WholeFilePatcher, and OtaPayloadExtractor and ZipExtractor implement ImageExtractor. In MagiskInstallImpl, streamline terminology by renaming srcBoot to targetImage, useRootDir to useRootFs, and processArchive to processWholeFile. Assisted-by: Gemini 3.8 Flash --- .../magisk/core/tasks/MagiskInstaller.kt | 47 ++++++++++++------- .../magisk/core/tasks/OtaPayloadExtractor.kt | 4 +- .../magisk/core/tasks/TarProcessor.kt | 16 +++---- .../magisk/core/tasks/ZipExtractor.kt | 8 ++-- 4 files changed, 43 insertions(+), 32 deletions(-) diff --git a/app/core/src/main/java/com/topjohnwu/magisk/core/tasks/MagiskInstaller.kt b/app/core/src/main/java/com/topjohnwu/magisk/core/tasks/MagiskInstaller.kt index aad784912..0096781c6 100644 --- a/app/core/src/main/java/com/topjohnwu/magisk/core/tasks/MagiskInstaller.kt +++ b/app/core/src/main/java/com/topjohnwu/magisk/core/tasks/MagiskInstaller.kt @@ -40,16 +40,29 @@ import java.security.SecureRandom import java.util.Locale import java.util.concurrent.atomic.AtomicBoolean +interface WholeFilePatcher { + @Throws(IOException::class) + suspend fun start(channel: DataChannel): ExtendedFile + + @Throws(IOException::class) + suspend fun finish(patched: ExtendedFile) +} + +interface ImageExtractor { + @Throws(IOException::class) + suspend fun extract(channel: DataChannel) +} + abstract class MagiskInstallImpl protected constructor( protected val console: MutableList, private val logs: MutableList ) { private lateinit var installDir: ExtendedFile - private lateinit var srcBoot: ExtendedFile + private lateinit var targetImage: ExtendedFile private val shell = Shell.getShell() - private val useRootDir = shell.isRoot && Info.noDataExec + private val useRootFs = shell.isRoot && Info.noDataExec protected val context get() = ServiceLocator.deContext private val rootFS get() = RootUtils.fs @@ -82,7 +95,7 @@ abstract class MagiskInstallImpl protected constructor( console.add("! Unable to detect target image") return false } - srcBoot = rootFS.getFile(bootPath) + targetImage = rootFS.getFile(bootPath) console.add("- Target image: $bootPath") return true } @@ -169,7 +182,7 @@ abstract class MagiskInstallImpl protected constructor( return false } - if (useRootDir) { + if (useRootFs) { // Move everything to tmpfs to workaround Samsung bullshit rootFS.getFile(Const.TMPDIR).also { arrayOf( @@ -223,7 +236,7 @@ abstract class MagiskInstallImpl protected constructor( val tarMagic = head.copyOfRange(257, 262) return if (tarMagic.contentEquals("ustar".toByteArray())) { - processArchive(input) + processWholeFile(input) } else { extractAndProcessImage(input, magic) } @@ -237,14 +250,14 @@ abstract class MagiskInstallImpl protected constructor( // Patch the boot image in a tar archive, and output a new tar archive // with all the original entries and the patched boot image - private suspend fun processArchive(input: DataChannel): Boolean { + private suspend fun processWholeFile(input: DataChannel): Boolean { val outFile = MediaStoreUtils.getFile("$destName.tar") val outStream = outFile.uri.outputStream().buffered(1024 * 1024) val tar = TarProcessor(installDir, outStream, console, logs) // Process input file try { - srcBoot = tar.consume(input) + targetImage = tar.start(input) } catch (e: IOException) { runCatching { outStream.close() } outFile.delete() @@ -282,15 +295,15 @@ abstract class MagiskInstallImpl protected constructor( // Extract the boot image from the input, and output the patched boot image private suspend fun extractAndProcessImage(input: DataChannel, magic: ByteArray): Boolean { // Process input file - srcBoot = installDir.getChildFile("boot.img") + targetImage = installDir.getChildFile("boot.img") try { if (magic.contentEquals("CrAU".toByteArray())) { - OtaPayloadExtractor(srcBoot, console, logs).consume(input) + OtaPayloadExtractor(targetImage, console, logs).extract(input) } else if (magic.contentEquals("PK\u0003\u0004".toByteArray())) { - ZipExtractor(srcBoot, console, logs).consume(input) + ZipExtractor(targetImage, console, logs).extract(input) } else { console.add("- Copying image to cache") - input.stream().use { it.copyAndCloseOut(srcBoot.newOutputStream()) } + input.stream().use { it.copyAndCloseOut(targetImage.newOutputStream()) } } } catch (e: IOException) { console.add("! Process error") @@ -328,7 +341,7 @@ abstract class MagiskInstallImpl protected constructor( console.add("****************************") // Fix up binaries - srcBoot.delete() + targetImage.delete() "cp_readlink $installDir".sh() return true @@ -336,7 +349,7 @@ abstract class MagiskInstallImpl protected constructor( private fun patchBoot(): Boolean { val newBoot = installDir.getChildFile("new-boot.img") - if (!useRootDir) { + if (!useRootFs) { // Create output files before hand newBoot.createNewFile() File(installDir, "stock_boot.img").createNewFile() @@ -349,7 +362,7 @@ abstract class MagiskInstallImpl protected constructor( "PATCHVBMETAFLAG=${Info.patchBootVbmeta} " + "RECOVERYMODE=${Config.recovery} " + "LEGACYSAR=${Info.legacySAR} " + - "sh boot_patch.sh $srcBoot") + "sh boot_patch.sh $targetImage") val isSuccess = cmds.sh().isSuccess shell.newJob().add("./magiskboot cleanup", "cd /").exec() @@ -357,7 +370,7 @@ abstract class MagiskInstallImpl protected constructor( return isSuccess } - private fun flashBoot() = "direct_install $installDir $srcBoot".sh().isSuccess + private fun flashBoot() = "direct_install $installDir $targetImage".sh().isSuccess private fun postOTA(): Boolean { "post_ota".sh() @@ -369,11 +382,9 @@ abstract class MagiskInstallImpl protected constructor( return true } - private fun Array.eq() = shell.newJob().add(*this).to(console, logs).enqueue() private fun String.sh() = shell.newJob().add(this).to(console, logs).exec() private fun Array.sh() = shell.newJob().add(*this).to(console, logs).exec() private fun String.fsh() = ShellUtils.fastCmd(shell, this) - private fun Array.fsh() = ShellUtils.fastCmd(shell, *this) protected suspend fun patchFile(file: Uri) = extractFiles() && processFile(file) @@ -386,7 +397,7 @@ abstract class MagiskInstallImpl protected constructor( protected suspend fun fixEnv() = extractFiles() && "fix_env $installDir".sh().isSuccess - protected fun restore() = findImage() && "restore_imgs $srcBoot".sh().isSuccess + protected fun restore() = findImage() && "restore_imgs $targetImage".sh().isSuccess protected fun uninstall() = "run_uninstaller $AppApkPath".sh().isSuccess diff --git a/app/core/src/main/java/com/topjohnwu/magisk/core/tasks/OtaPayloadExtractor.kt b/app/core/src/main/java/com/topjohnwu/magisk/core/tasks/OtaPayloadExtractor.kt index 10609403d..fc13fe73b 100644 --- a/app/core/src/main/java/com/topjohnwu/magisk/core/tasks/OtaPayloadExtractor.kt +++ b/app/core/src/main/java/com/topjohnwu/magisk/core/tasks/OtaPayloadExtractor.kt @@ -18,9 +18,9 @@ class OtaPayloadExtractor( private val outFile: File, private val console: MutableList, private val logs: MutableList, -) { +) : ImageExtractor { @Throws(IOException::class) - fun consume(channel: DataChannel) { + override suspend fun extract(channel: DataChannel) { channel.use { val manifest = readPayloadHeader(it) val dataBase = channel.position() diff --git a/app/core/src/main/java/com/topjohnwu/magisk/core/tasks/TarProcessor.kt b/app/core/src/main/java/com/topjohnwu/magisk/core/tasks/TarProcessor.kt index 5cc4c1959..51422c770 100644 --- a/app/core/src/main/java/com/topjohnwu/magisk/core/tasks/TarProcessor.kt +++ b/app/core/src/main/java/com/topjohnwu/magisk/core/tasks/TarProcessor.kt @@ -20,19 +20,19 @@ import java.nio.ByteBuffer * Patch boot images in tar firmware packages (e.g. Samsung AP tar files). * * A single-use, two-phase processor; the caller patches the boot image in between: - * 1. [consume]: extract boot images from the input tar into [installDir], and copy all + * 1. [start]: extract boot images from the input tar into [workingDir], and copy all * other entries to the output tar. Returns the image to be patched. * 2. [finish]: write the patched image to the output tar and finalize the archive. * - * [consume] closes the input channel. The caller owns the output stream and is + * [start] closes the input channel. The caller owns the output stream and is * responsible for closing it. */ class TarProcessor( - private val installDir: ExtendedFile, + private val workingDir: ExtendedFile, output: OutputStream, private val console: MutableList, private val logs: MutableList, -) { +) : WholeFilePatcher { class NoBootException : IOException() private val tarOut = TarArchiveOutputStream(output).apply { @@ -44,7 +44,7 @@ class TarProcessor( private lateinit var target: BootItem @Throws(IOException::class) - suspend fun consume(channel: DataChannel): ExtendedFile = channel.use { + override suspend fun start(channel: DataChannel): ExtendedFile = channel.use { val tarIn = TarArchiveInputStream(it.stream().buffered(1024 * 1024)) tarIn.use { processEntries(tarIn) } } @@ -131,7 +131,7 @@ class TarProcessor( if (boot != null) { // Repack boot image to prevent auto restore Shell.cmd( - "cd $installDir", + "cd $workingDir", "chmod -R 755 .", "./magiskboot unpack boot.img", "./magiskboot repack boot.img", @@ -155,7 +155,7 @@ class TarProcessor( } @Throws(IOException::class) - suspend fun finish(patched: ExtendedFile) { + override suspend fun finish(patched: ExtendedFile) { target.write(patched) tarOut.finish() } @@ -168,7 +168,7 @@ class TarProcessor( private inner class BootItem(private val entry: TarArchiveEntry) { val name = entry.name.replace(".lz4", "") - val file: ExtendedFile = installDir.getChildFile(name) + val file: ExtendedFile = workingDir.getChildFile(name) // Write the image to the output tar, preserving the original entry's metadata suspend fun write(image: ExtendedFile = file) { diff --git a/app/core/src/main/java/com/topjohnwu/magisk/core/tasks/ZipExtractor.kt b/app/core/src/main/java/com/topjohnwu/magisk/core/tasks/ZipExtractor.kt index 4efa3677f..d70e27c4e 100644 --- a/app/core/src/main/java/com/topjohnwu/magisk/core/tasks/ZipExtractor.kt +++ b/app/core/src/main/java/com/topjohnwu/magisk/core/tasks/ZipExtractor.kt @@ -16,9 +16,9 @@ class ZipExtractor( private val outFile: File, private val console: MutableList, private val logs: MutableList, -) { +) : ImageExtractor { @Throws(IOException::class) - fun consume(channel: DataChannel) { + override suspend fun extract(channel: DataChannel) { ZipFile.builder() .setSeekableByteChannel(channel) .setIgnoreLocalFileHeader(true) @@ -49,7 +49,7 @@ class ZipExtractor( } @Throws(IOException::class) - private fun extractFromOTAPackage( + private suspend fun extractFromOTAPackage( payload: ZipArchiveEntry, channel: DataChannel, ) { @@ -58,7 +58,7 @@ class ZipExtractor( } OtaPayloadExtractor(outFile, console, logs) - .consume(channel.slice(payload.dataOffset, payload.size)) + .extract(channel.slice(payload.dataOffset, payload.size)) } @Throws(IOException::class)