Commit 04920883 authored by topjohnwu's avatar topjohnwu

Change code for handling tar files

parent 5e44b0b9
...@@ -24,9 +24,11 @@ import org.kamranzafar.jtar.TarHeader ...@@ -24,9 +24,11 @@ import org.kamranzafar.jtar.TarHeader
import org.kamranzafar.jtar.TarInputStream import org.kamranzafar.jtar.TarInputStream
import org.kamranzafar.jtar.TarOutputStream import org.kamranzafar.jtar.TarOutputStream
import timber.log.Timber import timber.log.Timber
import java.io.* import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.io.InputStream
import java.nio.ByteBuffer import java.nio.ByteBuffer
import java.util.*
import java.util.zip.ZipEntry import java.util.zip.ZipEntry
import java.util.zip.ZipInputStream import java.util.zip.ZipInputStream
...@@ -39,7 +41,7 @@ abstract class MagiskInstaller { ...@@ -39,7 +41,7 @@ abstract class MagiskInstaller {
private val console: MutableList<String> private val console: MutableList<String>
private val logs: MutableList<String> private val logs: MutableList<String>
private var isTar = false private var tarOut: TarOutputStream? = null
private val service: GithubRawServices by inject() private val service: GithubRawServices by inject()
private val context: Context by inject() private val context: Context by inject()
...@@ -150,7 +152,9 @@ abstract class MagiskInstaller { ...@@ -150,7 +152,9 @@ abstract class MagiskInstaller {
private fun handleTar(input: InputStream) { private fun handleTar(input: InputStream) {
console.add("- Processing tar file") console.add("- Processing tar file")
var vbmeta = false var vbmeta = false
withStreams(TarInputStream(input), TarOutputStream(destFile)) { tarIn, tarOut -> val tarOut = TarOutputStream(destFile)
this.tarOut = tarOut
TarInputStream(input).use { tarIn ->
lateinit var entry: TarEntry lateinit var entry: TarEntry
while (tarIn.nextEntry?.let { entry = it } != null) { while (tarIn.nextEntry?.let { entry = it } != null) {
if (entry.name.contains("boot.img") || entry.name.contains("recovery.img")) { if (entry.name.contains("boot.img") || entry.name.contains("recovery.img")) {
...@@ -214,8 +218,7 @@ abstract class MagiskInstaller { ...@@ -214,8 +218,7 @@ abstract class MagiskInstaller {
return false return false
} }
it.reset() it.reset()
if (Arrays.equals(magic, "ustar".toByteArray())) { if (magic.contentEquals("ustar".toByteArray())) {
isTar = true
destFile = File(Config.downloadDirectory, "magisk_patched.tar") destFile = File(Config.downloadDirectory, "magisk_patched.tar")
handleTar(it) handleTar(it)
} else { } else {
...@@ -292,15 +295,13 @@ abstract class MagiskInstaller { ...@@ -292,15 +295,13 @@ abstract class MagiskInstaller {
protected fun storeBoot(): Boolean { protected fun storeBoot(): Boolean {
val patched = SuFile.open(installDir, "new-boot.img") val patched = SuFile.open(installDir, "new-boot.img")
try { try {
val os: OutputStream val os = tarOut?.let {
if (isTar) { it.putNextEntry(newEntry(
os = TarOutputStream(destFile, true)
os.putNextEntry(newEntry(
if (srcBoot.contains("recovery")) "recovery.img" else "boot.img", if (srcBoot.contains("recovery")) "recovery.img" else "boot.img",
patched.length())) patched.length()))
} else { tarOut = null
os = destFile.outputStream() it
} } ?: destFile.outputStream()
patched.suInputStream().use { it.copyTo(os); os.close() } patched.suInputStream().use { it.copyTo(os); os.close() }
} catch (e: IOException) { } catch (e: IOException) {
console.add("! Failed to output to $destFile") console.add("! Failed to output to $destFile")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment