trivial improvements

- replace String.deleteIfExists with kotlin.io.Path.deleteIfExists
- remove unused "dtb.src", use "dtb.*.src" instead
pull/140/head v14_r2
cfig 1 year ago
parent 2076fad7d3
commit 94570b1fcc
No known key found for this signature in database
GPG Key ID: B104C307F0FDABB7

@ -17,7 +17,7 @@ package cfig.bootimg
import cc.cfig.io.Struct
import cfig.utils.EnvironmentVerifier
import cfig.bootimg.cpio.AndroidCpio
import cfig.utils.DTC
import rom.fdt.DTC
import cfig.helper.Helper
import cfig.helper.ZipHelper
import cfig.utils.KernelExtractor
@ -179,10 +179,10 @@ class Common {
return ret
}
fun dumpDtb(s: Helper.Slice) {
fun dumpDtb(s: Helper.Slice, decompile: Boolean = true) {
Helper.extractFile(s.srcFile, s.dumpFile, s.offset.toLong(), s.length)
//extract DTB
if (EnvironmentVerifier().hasDtc) {
if (EnvironmentVerifier().hasDtc && decompile) {
DTC().decompile(s.dumpFile, s.dumpFile + "." + Helper.prop("config.dts_suffix"))
}
}

@ -24,7 +24,7 @@ import cfig.helper.Dumpling
import cfig.helper.Helper
import cfig.helper.ZipHelper
import cfig.packable.VBMetaParser
import cfig.utils.DTC
import rom.fdt.DTC
import cfig.utils.EnvironmentVerifier
import com.fasterxml.jackson.databind.ObjectMapper
import com.github.freva.asciitable.HorizontalAlign
@ -250,7 +250,7 @@ data class BootV2(
}
//dtb
this.dtb?.let { _ ->
Common.dumpDtb(Helper.Slice(info.output, dtb!!.position.toInt(), dtb!!.size, dtb!!.file!!))
Common.dumpDtb(Helper.Slice(info.output, dtb!!.position.toInt(), dtb!!.size, dtb!!.file!!), false)
this.dtb!!.dtbList = DTC.parseMultiple(dtb!!.file!!)
log.info("dtb sz = " + this.dtb!!.dtbList.size)
//dump info again
@ -359,7 +359,7 @@ data class BootV2(
it.addRule()
it.addRow("dtb", theDtb.file)
prints.add(Pair("dtb", theDtb.file.toString()))
if (File(theDtb.file + ".${dtsSuffix}").exists()) {
if (File(theDtb.file + ".0.${dtsSuffix}").exists()) {
it.addRow("\\-- decompiled dts [$dtbCount]", theDtb.file + ".*.${dtsSuffix}")
prints.add(Pair("\\-- decompiled dts [$dtbCount]", theDtb.file + ".*.${dtsSuffix}"))
}
@ -456,7 +456,7 @@ data class BootV2(
}
//refresh dtb size
dtb?.let { theDtb ->
if (File(theDtb.file!! + ".${dtsSuffix}").exists()) {
if (File(theDtb.file!! + ".0.${dtsSuffix}").exists()) {
DTC.packMultiple(theDtb.file!!, theDtb.dtbList)
}
theDtb.size = File(theDtb.file!!).length().toInt()

@ -24,7 +24,7 @@ import cfig.helper.Dumpling
import cfig.helper.Helper
import cfig.helper.ZipHelper
import cfig.packable.VBMetaParser
import cfig.utils.DTC
import rom.fdt.DTC
import cfig.utils.EnvironmentVerifier
import com.fasterxml.jackson.databind.ObjectMapper
import de.vandermeer.asciitable.AsciiTable
@ -258,7 +258,7 @@ data class VendorBoot(
}
}
//update dtb
if (File(this.dtb.file + ".${dtsSuffix}").exists()) {
if (File(this.dtb.file + ".0.${dtsSuffix}").exists()) {
DTC.packMultiple(this.dtb.file, this.dtb.dtbList)
}
this.dtb.size = File(this.dtb.file).length().toInt()
@ -368,7 +368,7 @@ data class VendorBoot(
}
//dtb
run {
C.dumpDtb(Helper.Slice(info.output, dtb.position.toInt(), dtb.size, dtb.file))
C.dumpDtb(Helper.Slice(info.output, dtb.position.toInt(), dtb.size, dtb.file), false)
if (dtb.size > 0) {
dtb.dtbList = DTC.parseMultiple(dtb.file)
DTC.extractMultiple(dtb.file, dtb.dtbList)
@ -438,7 +438,7 @@ data class VendorBoot(
if (this.dtb.size > 0) {
it.addRow("dtb", this.dtb.file)
prints.add(Pair("dtb", this.dtb.file))
if (File(this.dtb.file + ".${dtsSuffix}").exists()) {
if (File(this.dtb.file + ".0.${dtsSuffix}").exists()) {
it.addRow("\\-- decompiled dts [${dtb.dtbList.size}]", dtb.file + "*.${dtsSuffix}")
prints.add(Pair("\\-- decompiled dts [${dtb.dtbList.size}]", dtb.file + "*.${dtsSuffix}"))
}

@ -20,12 +20,13 @@ import cfig.bootimg.v2.BootV2
import cfig.bootimg.v2.BootV2Dialects
import cfig.bootimg.v3.BootV3
import cfig.helper.Helper
import cfig.helper.Helper.Companion.deleteIfExists
import com.fasterxml.jackson.databind.ObjectMapper
import de.vandermeer.asciitable.AsciiTable
import org.slf4j.LoggerFactory
import java.io.File
import java.io.FileInputStream
import kotlin.io.path.Path
import kotlin.io.path.deleteIfExists
class BootImgParser : IPackable {
override val loopNo: Int
@ -142,7 +143,7 @@ class BootImgParser : IPackable {
fun clear(fileName: String) {
super.clear()
listOf("", ".clear", ".google", ".clear", ".signed", ".signed2").forEach {
"$fileName$it".deleteIfExists()
Path("$fileName$it").deleteIfExists()
}
VBMetaParser().clear("vbmeta.img")
}

@ -4,11 +4,12 @@ import cfig.bootimg.Common
import cfig.helper.Helper
import cfig.helper.Helper.Companion.check_call
import cfig.helper.Helper.Companion.check_output
import cfig.helper.Helper.Companion.deleteIfExists
import cfig.packable.IPackable
import cfig.utils.DTC
import org.slf4j.LoggerFactory
import rom.fdt.DTC
import java.io.File
import kotlin.io.path.Path
import kotlin.io.path.deleteIfExists
class DeviceTreeParser : IPackable {
override fun capabilities(): List<String> {
@ -74,7 +75,7 @@ class DeviceTreeParser : IPackable {
fun clear(fileName: String) {
super.clear()
listOf("", ".new").forEach {
"$fileName$it".deleteIfExists()
Path("$fileName$it").deleteIfExists()
}
}

@ -15,17 +15,18 @@
package cfig.packable
import cfig.helper.Helper
import cfig.helper.Helper.Companion.deleteIfExists
import cfig.utils.DTC
import cfig.utils.EnvironmentVerifier
import com.fasterxml.jackson.databind.ObjectMapper
import org.apache.commons.exec.CommandLine
import org.apache.commons.exec.DefaultExecutor
import org.slf4j.LoggerFactory
import utils.Dtbo
import rom.fdt.DTC
import rom.fdt.Dtbo
import java.io.File
import java.io.FileInputStream
import java.util.*
import kotlin.io.path.Path
import kotlin.io.path.deleteIfExists
class DtboParser(val workDir: File) : IPackable {
override val loopNo: Int
@ -78,7 +79,7 @@ class DtboParser(val workDir: File) : IPackable {
fun clear(fileName: String) {
super.clear()
listOf("", ".clear", ".google", ".clear", ".signed", ".signed2").forEach {
"$fileName$it".deleteIfExists()
Path("$fileName$it").deleteIfExists()
}
VBMetaParser().clear("vbmeta.img")
}

@ -16,14 +16,15 @@ package cfig.packable
import avb.AVBInfo
import cfig.Avb
import cfig.helper.Helper
import cfig.helper.Dumpling
import cfig.helper.Helper
import cfig.helper.Helper.Companion.check_call
import cfig.helper.Helper.Companion.check_output
import cfig.helper.Helper.Companion.deleteIfExists
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.io.File
import kotlin.io.path.Path
import kotlin.io.path.deleteIfExists
interface IPackable {
val loopNo: Int
@ -79,7 +80,9 @@ interface IPackable {
File(workDir).deleteRecursively()
}
File(workDir).mkdirs()
"uiderrors".deleteIfExists()
// java.nio.file.Files.deleteIfExists() will throw exception on Windows platform, so use java.io.File
// Caused by: java.nio.file.FileSystemException: uiderrors: The process cannot access the file because it is being used by another process
File("uiderrors").deleteOnExit()
}
companion object {

@ -15,12 +15,13 @@
package cfig.packable
import cfig.helper.Helper
import rom.misc.MiscImage
import cfig.helper.Helper.Companion.deleteIfExists
import com.fasterxml.jackson.databind.ObjectMapper
import org.slf4j.LoggerFactory
import rom.misc.MiscImage
import java.io.File
import java.io.RandomAccessFile
import kotlin.io.path.Path
import kotlin.io.path.deleteIfExists
class MiscImgParser : IPackable {
override val loopNo: Int
@ -78,7 +79,7 @@ class MiscImgParser : IPackable {
fun clear(fileName: String) {
super.clear()
listOf("", ".new").forEach {
"$fileName$it".deleteIfExists()
Path("$fileName$it").deleteIfExists()
}
}

@ -15,8 +15,9 @@
package cfig.packable
import cc.cfig.droid.ota.Payload
import cfig.helper.Helper.Companion.deleteIfExists
import org.slf4j.LoggerFactory
import kotlin.io.path.Path
import kotlin.io.path.deleteIfExists
class PayloadBinParser : IPackable {
override val loopNo: Int = 0
@ -47,7 +48,7 @@ class PayloadBinParser : IPackable {
fun clear(fileName: String) {
super.clear()
fileName.deleteIfExists()
Path(fileName).deleteIfExists()
}
override fun flash(fileName: String, deviceName: String) {

@ -18,13 +18,14 @@ import avb.AVBInfo
import cfig.Avb
import cfig.helper.Dumpling
import cfig.helper.Helper
import cfig.helper.Helper.Companion.deleteIfExists
import com.fasterxml.jackson.databind.ObjectMapper
import org.slf4j.LoggerFactory
import java.io.File
import java.nio.file.Files
import java.nio.file.Paths
import java.nio.file.StandardOpenOption
import kotlin.io.path.Path
import kotlin.io.path.deleteIfExists
class VBMetaParser : IPackable {
override val loopNo: Int
@ -66,7 +67,7 @@ class VBMetaParser : IPackable {
fun clear(fileName: String) {
super.clear()
listOf("", ".signed").forEach {
"$fileName$it".deleteIfExists()
Path("$fileName$it").deleteIfExists()
}
}

@ -15,10 +15,11 @@
package cfig.packable
import cfig.bootimg.v3.VendorBoot
import cfig.helper.Helper.Companion.deleteIfExists
import com.fasterxml.jackson.databind.ObjectMapper
import org.slf4j.LoggerFactory
import java.io.File
import kotlin.io.path.Path
import kotlin.io.path.deleteIfExists
class VendorBootParser : IPackable {
override val loopNo: Int = 0
@ -59,7 +60,7 @@ class VendorBootParser : IPackable {
fun clear(fileName: String) {
super.clear()
listOf("", ".clear", ".google", ".clear", ".signed", ".signed2").forEach {
"$fileName$it".deleteIfExists()
Path("$fileName$it").deleteIfExists()
}
VBMetaParser().clear("vbmeta.img")
}

@ -12,11 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package cfig.utils
package rom.fdt
import cc.cfig.io.Struct
import cfig.helper.Dumpling
import cfig.helper.Helper
import cfig.utils.EnvironmentVerifier
import org.apache.commons.exec.CommandLine
import org.apache.commons.exec.DefaultExecutor
import org.slf4j.LoggerFactory
@ -34,7 +35,7 @@ class DTC {
)
data class FdtHeader(
var magic: Int = 0,
var magic: Long = 0L,
val totalsize: Int = 0,
val offDtStruct: Int = 0,
val offDtStrings: Int = 0,
@ -47,7 +48,7 @@ class DTC {
) {
companion object {
private const val MAGIC = 0xd00dfeedu
const val FORMAT_STRING = ">10i"
const val FORMAT_STRING = ">I9i"
const val SIZE = 40
init {
@ -58,7 +59,7 @@ class DTC {
fun parse(iS: InputStream): FdtHeader {
val info = Struct(FORMAT_STRING).unpack(iS)
val ret = FdtHeader(
info[0] as Int,
(info[0] as UInt).toLong(),
info[1] as Int,
info[2] as Int,
info[3] as Int,
@ -150,9 +151,9 @@ class DTC {
}
val remainder = File(fileName).length() - ret.sumOf { it.header.totalsize }.toLong()
if (remainder == 0L) {
log.info("Successfully parsed ${ret.size} FDT headers")
log.info("Successfully parsed ${ret.size} FDT headers from $fileName")
} else {
log.warn("Successfully parsed ${ret.size} FDT headers, remainder: $remainder bytes")
log.warn("Successfully parsed ${ret.size} FDT headers from $fileName, remainder: $remainder bytes")
}
return ret
}

@ -1,4 +1,4 @@
package utils
package rom.fdt
import avb.AVBInfo
import cc.cfig.io.Struct
@ -8,7 +8,6 @@ import cfig.bootimg.Signer
import cfig.helper.Dumpling
import cfig.helper.Helper
import cfig.packable.VBMetaParser
import cfig.utils.DTC
import com.fasterxml.jackson.databind.ObjectMapper
import de.vandermeer.asciitable.AsciiTable
import org.slf4j.LoggerFactory
@ -59,8 +58,8 @@ class Dtbo(
}
totalSize = info[1] as Int
headerSize = info[2] as Int
if (headerSize != DtboHeader.SIZE) {
log.warn("headerSize $headerSize != ${DtboHeader.SIZE}")
if (headerSize != SIZE) {
log.warn("headerSize $headerSize != $SIZE")
}
entrySize = info[3] as Int
if (entrySize != DeviceTreeTableEntry.SIZE) {
@ -186,7 +185,7 @@ class Dtbo(
.toInt()
// Part II - a
for (index in 0 until dtEntries.size) {
DTC().compile("${outDir}dt/dt.${index}.${dtsSuffix}", "${outDir}dt/dt.${index}")
DTC().compile("${outDir}dt/dt.${index}.$dtsSuffix", "${outDir}dt/dt.${index}")
}
// Part II - b
var offset = DtboHeader.SIZE + (header.entryCount * DeviceTreeTableEntry.SIZE)
@ -221,7 +220,7 @@ class Dtbo(
it.addRow("image info", outDir + info.output.removeSuffix(".img") + ".json")
it.addRule()
it.addRow("device-tree blob (${this.header.entryCount} blobs)", "${outDir}dt/dt.*")
it.addRow("\\-- device-tree source ", "${outDir}dt/dt.*.${dtsSuffix}")
it.addRow("\\-- device-tree source ", "${outDir}dt/dt.*.$dtsSuffix")
it.addRule()
it.addRow("AVB info", Avb.getJsonFileName(info.output))
it.addRule()

@ -14,20 +14,16 @@
package rom.sparse
import avb.AVBInfo
import avb.blob.Footer
import cfig.Avb
import cfig.bootimg.Common.Companion.deleleIfExists
import cfig.helper.Dumpling
import cfig.helper.Helper
import cfig.helper.Helper.Companion.deleteIfExists
import cfig.packable.IPackable
import cfig.packable.VBMetaParser
import com.fasterxml.jackson.databind.ObjectMapper
import de.vandermeer.asciitable.AsciiTable
import org.slf4j.LoggerFactory
import java.io.File
import java.io.FileInputStream
import kotlin.io.path.Path
import kotlin.io.path.deleteIfExists
class SparseImgParser : IPackable {
override val loopNo: Int
@ -87,7 +83,7 @@ class SparseImgParser : IPackable {
fun clear(fileName: String) {
super.clear()
listOf("", ".clear", ".signed").forEach {
"$fileName$it".deleteIfExists()
Path("$fileName$it").deleteIfExists()
}
VBMetaParser().clear("vbmeta.img")
}

@ -0,0 +1,30 @@
package rom.fdt
import org.junit.After
import org.junit.Before
import org.junit.Test
import kotlin.io.path.createTempFile
import kotlin.io.path.deleteIfExists
import kotlin.io.path.writeBytes
class DTCTest {
private lateinit var tempFile :java.nio.file.Path
@Before
fun before() {
tempFile = createTempFile()
val dtbData = this::class.java.classLoader.getResourceAsStream("multiple_DT.dtb")!!.readAllBytes()
tempFile.writeBytes(dtbData)
}
@After
fun after() {
tempFile.deleteIfExists()
}
@Test
fun doIt() {
val dtbList = DTC.parseMultiple(tempFile.toAbsolutePath().toString())
check(dtbList.size == 4)
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 82 KiB

@ -179,13 +179,6 @@ class Helper {
log.info("Dumping data to $dumpFile done")
}
fun String.deleteIfExists() {
if (File(this).exists()) {
log.info("deleting $this")
File(this).delete()
}
}
fun String.check_call(inWorkdir: String? = null): Boolean {
val ret: Boolean
try {

@ -190,6 +190,8 @@ def main():
verifySingleDir(resDir3, "issue_133_repack_ext4")
else:
log.info("sparse image is not supported on OS other than Linux, skip testing")
# Issue 134: multiple DTs
verifySingleDir(resDir3, "issue_134_multiple_DTs")
log.info(successLogo)

@ -1,6 +1,5 @@
package cfig.lazybox.sysinfo
import cfig.helper.Helper.Companion.deleteIfExists
import com.fasterxml.jackson.databind.ObjectMapper
import org.slf4j.LoggerFactory
import java.io.File
@ -8,6 +7,7 @@ import java.nio.file.Files
import java.nio.file.attribute.FileAttribute
import java.nio.file.attribute.PosixFilePermissions
import kotlin.io.path.Path
import kotlin.io.path.deleteIfExists
class Pidstat {
data class Cfg(
@ -63,13 +63,13 @@ class Pidstat {
val permissions: FileAttribute<*> = PosixFilePermissions.asFileAttribute(ownerWritable)
String.format(outFile, 1).let {
it.deleteIfExists()
Path(it).deleteIfExists()
Files.createFile(Path(it), permissions)
File(it).writeText("#!/bin/bash\nset -x\n$cmd1\n")
log.info("$it is ready")
}
String.format(outFile, 2).let {
it.deleteIfExists()
Path(it).deleteIfExists()
if (run2) {
Files.createFile(Path(it), permissions)
File(it).writeText("#!/bin/bash\necho $cmd2\n$cmd2\n")

@ -2,7 +2,6 @@ package cfig.lazybox.sysinfo
import cfig.helper.Helper
import cfig.helper.Helper.Companion.check_call
import cfig.helper.Helper.Companion.deleteIfExists
import cfig.helper.ZipHelper
import org.slf4j.LoggerFactory
import java.io.File
@ -10,6 +9,7 @@ import java.io.FileOutputStream
import java.io.OutputStream
import java.nio.file.Files
import java.nio.file.Paths
import kotlin.io.path.Path
import kotlin.io.path.deleteIfExists
import kotlin.io.path.writeText
@ -63,7 +63,7 @@ makeTar("%s", "%s")
fun run() {
"adb wait-for-device".check_call()
"adb root".check_call()
"sysinfo.tar".deleteIfExists()
Path("sysinfo.tar").deleteIfExists()
val prefix = "sysinfo"
File("sysinfo").let {
if (it.exists()) {

@ -1,14 +0,0 @@
package cfig.lazybox.sysinfo
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.Assertions.*
import java.io.File
class CpuInfoTest {
@Test
fun probeInfo() {
CpuInfo.construct()
}
}

@ -1 +1 @@
Subproject commit c2e5850a225bd765fa45b6c628106a82aee2a66f
Subproject commit 24b0660fb6859fe738599b1e0a1faf1aa9381613

@ -1 +1 @@
Subproject commit b5906e9ac69e0de09d8380b3f3409ed336017262
Subproject commit 106cacece826ef18b67117118a37ff034ec72f61

@ -1 +1 @@
Subproject commit 631af3d1c11343c0a2df1d044cabd44e78fdf865
Subproject commit 4ed25ab4b19cacc8f65753cd35fb4978fdb7229f
Loading…
Cancel
Save