clean code

This commit is contained in:
vvb2060
2026-07-28 20:10:20 -07:00
committed by John Wu
parent e0a7f41c1c
commit ff85242a93
3 changed files with 13 additions and 17 deletions
@@ -41,9 +41,9 @@ class ExtractImage(
}
}
zipFile.getRawInputStream(payload)
extractFromOTAPackage(payload, channel, outFile)
extractFromOTAPackage(payload, channel)
} else {
extractFromFactoryImage(zipFile, channel, outFile)
extractFromFactoryImage(zipFile, channel)
}
}
}
@@ -52,14 +52,13 @@ class ExtractImage(
private fun extractFromOTAPackage(
payload: ZipArchiveEntry,
channel: DataSourceChannel,
outFile: File,
) {
if (payload.method != ZipMethod.STORED.code) {
throw IOException("payload.bin is compressed, expected STORED method")
}
channel.slice(payload.dataOffset, payload.size).use { payloadChannel ->
Payload(payloadChannel).extract(outFile, { console.add(it) }, { logs.add(it) })
Payload(payloadChannel).extract(outFile, console, logs)
}
}
@@ -67,12 +66,11 @@ class ExtractImage(
private fun extractFromFactoryImage(
zipFile: ZipFile,
channel: DataSourceChannel,
outFile: File
) {
console.add("- Processing as factory image package")
findBootImageZipEntry(zipFile)?.let { entry ->
return extractImageFile(zipFile, entry, channel, outFile)
return extractImageFile(zipFile, entry, channel)
}
val imageZipEntry = zipFile.entries.asSequence().find { entry ->
@@ -81,7 +79,7 @@ class ExtractImage(
}
if (imageZipEntry != null) {
zipFile.getRawInputStream(imageZipEntry)
return extractFromInnerImageZip(imageZipEntry, channel, outFile)
return extractFromInnerImageZip(imageZipEntry, channel)
}
throw IOException("inner image ZIP not found in factory image package")
@@ -99,7 +97,6 @@ class ExtractImage(
private fun extractFromInnerImageZip(
entry: ZipArchiveEntry,
channel: DataSourceChannel,
outFile: File
) {
logs.add("Found inner image ZIP: ${entry.name}")
@@ -114,7 +111,7 @@ class ExtractImage(
.get().use { innerZipFile ->
val targetEntry = findBootImageZipEntry(innerZipFile)
?: throw IOException("boot image not found in inner image ZIP")
return extractImageFile(innerZipFile, targetEntry, innerZipChannel, outFile)
return extractImageFile(innerZipFile, targetEntry, innerZipChannel)
}
}
}
@@ -124,7 +121,6 @@ class ExtractImage(
zipFile: ZipFile,
entry: ZipArchiveEntry,
channel: DataSourceChannel,
outFile: File,
) {
console.add("- Found boot image entry: ${entry.name} (${entry.size} bytes)")
console.add("- Downloading")
@@ -368,7 +368,7 @@ abstract class MagiskInstallImpl protected constructor(
try {
if (magic.contentEquals("CrAU".toByteArray())) {
DataSourceChannel(channel).use { source ->
Payload(source).extract(boot, { console.add(it) }, { logs.add(it) })
Payload(source).extract(boot, console, logs)
}
} else if (magic.contentEquals("PK\u0003\u0004".toByteArray())) {
ExtractImage(boot, console, logs).consume(DataSourceChannel(channel))
@@ -23,15 +23,15 @@ class Payload(private val channel: DataSourceChannel) {
}
@Throws(IOException::class)
fun extract(outputFile: File, console: (String) -> Unit, logger: (String) -> Unit) {
fun extract(outputFile: File, console: MutableList<String>, logger: MutableList<String>) {
val partition = findPartition()
console("- Found partition ${partition.partition_name}")
console.add("- Found partition ${partition.partition_name}")
val actualHash = extractPartition(outputFile, partition, console)
val newPartitionInfo = partition.new_partition_info
if (newPartitionInfo?.hash == null) {
logger("Hash verification skipped")
logger.add("Hash verification skipped")
return
}
@@ -43,7 +43,7 @@ class Payload(private val channel: DataSourceChannel) {
"Hash mismatch, expected ${toHex(expectedHash)}, but got ${toHex(actualHash)}"
)
}
logger("Hash verification passed")
logger.add("Hash verification passed")
}
@Throws(IOException::class)
@@ -109,7 +109,7 @@ class Payload(private val channel: DataSourceChannel) {
private fun extractPartition(
outputFile: File,
partition: PartitionUpdate,
console: (String) -> Unit,
console: MutableList<String>,
): ByteArray {
FileChannel.open(
outputFile.toPath(),
@@ -124,7 +124,7 @@ class Payload(private val channel: DataSourceChannel) {
val count = partition.operations.size
partition.operations.forEachIndexed { index, operation ->
if (index % 5 == 0 || index == count - 1) {
console("- Downloading ${index + 1}/$count")
console.add("- Downloading ${index + 1}/$count")
}
processOperation(outChannel, operation)
}