From 31be98dc60eceb8115375042e40e0f5decdc37ec Mon Sep 17 00:00:00 2001 From: cfig Date: Sun, 28 Feb 2021 11:04:33 +0800 Subject: [PATCH] support lzma/xz ramdisk Issue 52 --- bbootimg/build.gradle.kts | 2 +- bbootimg/src/main/kotlin/avb/Avb.kt | 2 +- bbootimg/src/main/kotlin/bootimg/Common.kt | 46 ++++++++-- .../main/resources/simplelogger.properties | 4 + helper/build.gradle.kts | 1 + .../src/main/kotlin/cfig/helper/ZipHelper.kt | 90 ++++++++++++++++++- 6 files changed, 133 insertions(+), 12 deletions(-) diff --git a/bbootimg/build.gradle.kts b/bbootimg/build.gradle.kts index ba12742..3ea15e2 100644 --- a/bbootimg/build.gradle.kts +++ b/bbootimg/build.gradle.kts @@ -31,7 +31,7 @@ dependencies { } application { - mainClassName = "cfig.packable.PackableLauncherKt" + mainClass.set("cfig.packable.PackableLauncherKt") } tasks.withType().all { diff --git a/bbootimg/src/main/kotlin/avb/Avb.kt b/bbootimg/src/main/kotlin/avb/Avb.kt index 6ea12fd..5ec49ec 100644 --- a/bbootimg/src/main/kotlin/avb/Avb.kt +++ b/bbootimg/src/main/kotlin/avb/Avb.kt @@ -426,7 +426,7 @@ class Avb { } } } else { - log.info("no companion vbmeta.img") + log.debug("no companion vbmeta.img") } } } diff --git a/bbootimg/src/main/kotlin/bootimg/Common.kt b/bbootimg/src/main/kotlin/bootimg/Common.kt index bd65879..c5ebdd0 100644 --- a/bbootimg/src/main/kotlin/bootimg/Common.kt +++ b/bbootimg/src/main/kotlin/bootimg/Common.kt @@ -123,14 +123,34 @@ class Common { ZipHelper.isGZ(s.dumpFile) -> { Files.move( Paths.get(s.dumpFile), Paths.get(s.dumpFile + ".gz"), - java.nio.file.StandardCopyOption.REPLACE_EXISTING) + java.nio.file.StandardCopyOption.REPLACE_EXISTING + ) ZipHelper.zcat(s.dumpFile + ".gz", s.dumpFile) } + ZipHelper.isXz(s.dumpFile) -> { + log.info("ramdisk is compressed xz") + Files.move( + Paths.get(s.dumpFile), Paths.get(s.dumpFile + ".xz"), + java.nio.file.StandardCopyOption.REPLACE_EXISTING + ) + ZipHelper.xzcat(s.dumpFile + ".xz", s.dumpFile) + ret = "xz" + } + ZipHelper.isLzma(s.dumpFile) -> { + log.info("ramdisk is compressed lzma") + Files.move( + Paths.get(s.dumpFile), Paths.get(s.dumpFile + ".lzma"), + java.nio.file.StandardCopyOption.REPLACE_EXISTING + ) + ZipHelper.lzcat(s.dumpFile + ".lzma", s.dumpFile) + ret = "lzma" + } ZipHelper.isLz4(s.dumpFile) -> { log.info("ramdisk is compressed lz4") Files.move( Paths.get(s.dumpFile), Paths.get(s.dumpFile + ".lz4"), - java.nio.file.StandardCopyOption.REPLACE_EXISTING) + java.nio.file.StandardCopyOption.REPLACE_EXISTING + ) ZipHelper.lz4cat(s.dumpFile + ".lz4", s.dumpFile) ret = "lz4" } @@ -234,18 +254,30 @@ class Common { //using preset fs_config fun packRootfs(rootDir: String, ramdiskGz: String) { - log.info("Packing rootfs $rootDir ...") + val root = File(rootDir).path + log.info("Packing rootfs $root ...") val fsConfig = File(ramdiskGz).parentFile.path + "/ramdisk_filelist.txt" when { ramdiskGz.endsWith(".gz") -> { val f = ramdiskGz.removeSuffix(".gz") - AndroidCpio().pack(rootDir, f, fsConfig) - ZipHelper.minigzip(ramdiskGz, FileInputStream(f)) + AndroidCpio().pack(root, f, fsConfig) + FileInputStream(f).use { ZipHelper.minigzip(ramdiskGz, it) } + } ramdiskGz.endsWith(".lz4") -> { val f = ramdiskGz.removeSuffix(".lz4") - AndroidCpio().pack(rootDir, f, fsConfig) - ZipHelper.lz4(ramdiskGz, FileInputStream(f)) + AndroidCpio().pack(root, f, fsConfig) + FileInputStream(f).use { ZipHelper.lz4(ramdiskGz, it) } + } + ramdiskGz.endsWith(".lzma") -> { + val f = ramdiskGz.removeSuffix(".lzma") + AndroidCpio().pack(root, f, fsConfig) + FileInputStream(f).use { ZipHelper.lzma(ramdiskGz, it) } + } + ramdiskGz.endsWith(".xz") -> { + val f = ramdiskGz.removeSuffix(".xz") + AndroidCpio().pack(root, f, fsConfig) + FileInputStream(f).use { ZipHelper.xz(ramdiskGz, it) } } else -> { throw IllegalArgumentException("$ramdiskGz is not supported") diff --git a/bbootimg/src/main/resources/simplelogger.properties b/bbootimg/src/main/resources/simplelogger.properties index bfc9e7f..578abb6 100644 --- a/bbootimg/src/main/resources/simplelogger.properties +++ b/bbootimg/src/main/resources/simplelogger.properties @@ -1 +1,5 @@ org.slf4j.simpleLogger.defaultLogLevel = info +org.slf4j.simpleLogger.showDateTime = true +org.slf4j.simpleLogger.dateTimeFormat = HH:mm:ss:SSS +org.slf4j.simpleLogger.showThreadName = false +org.slf4j.simpleLogger.showShortLogName = true diff --git a/helper/build.gradle.kts b/helper/build.gradle.kts index 160daad..fd87796 100644 --- a/helper/build.gradle.kts +++ b/helper/build.gradle.kts @@ -19,6 +19,7 @@ dependencies { implementation("org.apache.commons:commons-exec:1.3") implementation("org.bouncycastle:bcprov-jdk15on:1.57") implementation("org.apache.commons:commons-compress:1.20") + implementation("org.tukaani:xz:1.8") testImplementation("org.jetbrains.kotlin:kotlin-test") testImplementation("org.jetbrains.kotlin:kotlin-test-junit") diff --git a/helper/src/main/kotlin/cfig/helper/ZipHelper.kt b/helper/src/main/kotlin/cfig/helper/ZipHelper.kt index 09c6021..5224454 100644 --- a/helper/src/main/kotlin/cfig/helper/ZipHelper.kt +++ b/helper/src/main/kotlin/cfig/helper/ZipHelper.kt @@ -10,11 +10,15 @@ import org.apache.commons.compress.archivers.zip.ZipFile import org.apache.commons.compress.archivers.zip.ZipMethod import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream import org.apache.commons.compress.compressors.gzip.GzipParameters +import org.apache.commons.compress.compressors.lzma.LZMACompressorInputStream +import org.apache.commons.compress.compressors.lzma.LZMACompressorOutputStream import org.apache.commons.compress.compressors.xz.XZCompressorInputStream +import org.apache.commons.compress.compressors.xz.XZCompressorOutputStream import org.apache.commons.exec.CommandLine import org.apache.commons.exec.DefaultExecutor import org.apache.commons.exec.PumpStreamHandler import org.slf4j.LoggerFactory +import org.tukaani.xz.XZFormatException import java.io.* import java.lang.RuntimeException import java.net.URI @@ -164,7 +168,7 @@ class ZipHelper { if (ignoreError) { log.info("dumping entry: $entryName : entry not found, skip") } else { - throw IllegalArgumentException("$entryName doesn't exist") + throw IllegalArgumentException("$entryName doesn't exist") } } } @@ -195,6 +199,52 @@ class ZipHelper { this.closeArchiveEntry() } + fun isLzma(compressedFile: String): Boolean { + return try { + FileInputStream(compressedFile).use { fis -> + LZMACompressorInputStream(fis).use { + } + } + true + } catch (e: IOException) { + false + } + } + + fun lzma(compressedFile: String, fis: InputStream) { + log.info("Compress(lzma) ... ") + FileOutputStream(compressedFile).use { fos -> + LZMACompressorOutputStream(fos).use { gos -> + val buffer = ByteArray(1024) + while (true) { + val bytesRead = fis.read(buffer) + if (bytesRead <= 0) break + gos.write(buffer, 0, bytesRead) + } + } + } + log.info("compress(lzma) done: $compressedFile") + } + + /* + @function: lzcat compressedFile > decompressedFile + */ + fun lzcat(compressedFile: String, decompressedFile: String) { + FileInputStream(compressedFile).use { fileIn -> + LZMACompressorInputStream(fileIn).use { lzmaInputStream -> + FileOutputStream(decompressedFile).use { fileOutputStream -> + val buffer = ByteArray(1024) + while (true) { + val bytesRead = lzmaInputStream.read(buffer) + if (bytesRead <= 0) break + fileOutputStream.write(buffer, 0, bytesRead) + } + } + } + } + log.info("decompress(lzma) done: $compressedFile -> $decompressedFile") + } + fun isXz(compressedFile: String): Boolean { return try { FileInputStream(compressedFile).use { fis -> @@ -202,11 +252,45 @@ class ZipHelper { } } true - } catch (e: ZipException) { + } catch (e: XZFormatException) { false } } + fun xz(compressedFile: String, fis: InputStream) { + log.info("Compress(xz) ... ") + FileOutputStream(compressedFile).use { fos -> + XZCompressorOutputStream(fos).use { gos -> + val buffer = ByteArray(1024) + while (true) { + val bytesRead = fis.read(buffer) + if (bytesRead <= 0) break + gos.write(buffer, 0, bytesRead) + } + } + } + log.info("compress(xz) done: $compressedFile") + } + + /* + @function: xzcat compressedFile > decompressedFile + */ + fun xzcat(compressedFile: String, decompressedFile: String) { + FileInputStream(compressedFile).use { fileIn -> + XZCompressorInputStream(fileIn).use { zis -> + FileOutputStream(decompressedFile).use { fileOutputStream -> + val buffer = ByteArray(1024) + while (true) { + val bytesRead = zis.read(buffer) + if (bytesRead <= 0) break + fileOutputStream.write(buffer, 0, bytesRead) + } + } + } + } + log.info("decompress(xz) done: $compressedFile -> $decompressedFile") + } + fun isLz4(compressedFile: String): Boolean { return try { "lz4 -t $compressedFile".check_call() @@ -267,10 +351,10 @@ class ZipHelper { if (bytesRead <= 0) break fileOutputStream.write(buffer, 0, bytesRead) } - log.info("decompress(gz) done: $compressedFile -> $decompressedFile") } } } + log.info("decompress(gz) done: $compressedFile -> $decompressedFile") } /*