mirror of
https://github.com/kvorschk/Magisk.git
synced 2026-12-04 19:10:47 -05:00
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
This commit is contained in:
@@ -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<String>,
|
||||
private val logs: MutableList<String>
|
||||
) {
|
||||
|
||||
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<String>.eq() = shell.newJob().add(*this).to(console, logs).enqueue()
|
||||
private fun String.sh() = shell.newJob().add(this).to(console, logs).exec()
|
||||
private fun Array<String>.sh() = shell.newJob().add(*this).to(console, logs).exec()
|
||||
private fun String.fsh() = ShellUtils.fastCmd(shell, this)
|
||||
private fun Array<String>.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
|
||||
|
||||
|
||||
@@ -18,9 +18,9 @@ class OtaPayloadExtractor(
|
||||
private val outFile: File,
|
||||
private val console: MutableList<String>,
|
||||
private val logs: MutableList<String>,
|
||||
) {
|
||||
) : ImageExtractor {
|
||||
@Throws(IOException::class)
|
||||
fun consume(channel: DataChannel) {
|
||||
override suspend fun extract(channel: DataChannel) {
|
||||
channel.use {
|
||||
val manifest = readPayloadHeader(it)
|
||||
val dataBase = channel.position()
|
||||
|
||||
@@ -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<String>,
|
||||
private val logs: MutableList<String>,
|
||||
) {
|
||||
) : 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) {
|
||||
|
||||
@@ -16,9 +16,9 @@ class ZipExtractor(
|
||||
private val outFile: File,
|
||||
private val console: MutableList<String>,
|
||||
private val logs: MutableList<String>,
|
||||
) {
|
||||
) : 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)
|
||||
|
||||
Reference in New Issue
Block a user