From 5b49fa0fbfbbe31a2a0a161e480e6c7c2e3336df Mon Sep 17 00:00:00 2001 From: nift4 Date: Wed, 31 Jul 2024 16:00:56 +0200 Subject: [PATCH] wip tidy up some more --- .../org/andbootmgr/app/BackupRestoreFlow.kt | 19 +-- .../java/org/andbootmgr/app/CreatePartFlow.kt | 23 ++-- .../java/org/andbootmgr/app/DeviceInfo.kt | 13 +- .../java/org/andbootmgr/app/DeviceLogic.kt | 32 ++--- .../java/org/andbootmgr/app/DroidBootFlow.kt | 26 ++-- .../java/org/andbootmgr/app/MainActivity.kt | 36 ++--- .../java/org/andbootmgr/app/UpdateFlow.kt | 12 +- .../java/org/andbootmgr/app/util/SDUtils.kt | 127 ++++++------------ 8 files changed, 109 insertions(+), 179 deletions(-) diff --git a/app/src/main/java/org/andbootmgr/app/BackupRestoreFlow.kt b/app/src/main/java/org/andbootmgr/app/BackupRestoreFlow.kt index 2224b419..8e5ae753 100644 --- a/app/src/main/java/org/andbootmgr/app/BackupRestoreFlow.kt +++ b/app/src/main/java/org/andbootmgr/app/BackupRestoreFlow.kt @@ -52,7 +52,7 @@ private class CreateBackupDataHolder(val vm: WizardActivityState){ @Composable private fun ChooseAction(c: CreateBackupDataHolder) { - c.meta = remember { SDUtils.generateMeta(c.vm.deviceInfo!!.bdev, c.vm.deviceInfo.pbdev) } + c.meta = remember { SDUtils.generateMeta(c.vm.deviceInfo!!) } c.pi = remember { c.vm.activity.intent.getIntExtra("partitionid", -1) } Column(horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center, @@ -125,23 +125,18 @@ private fun Flash(c: CreateBackupDataHolder) { Terminal(c.vm, logFile = "flash_${System.currentTimeMillis()}.txt") { terminal -> terminal.add(c.vm.activity.getString(R.string.term_starting)) try { - if (!Shell.cmd(SDUtils.umsd(c.meta!!.dumpKernelPartition(c.pi))).to(terminal).exec().isSuccess) + val p = c.meta!!.dumpKernelPartition(c.pi) + if (!c.vm.logic.unmount(p).to(terminal).exec().isSuccess) throw IOException(c.vm.activity.getString(R.string.term_cant_umount)) if (c.action == 1) { c.vm.copy( - SuFileInputStream.open( - File( - c.vm.deviceInfo!!.pbdev + (c.pi) - ) - ), + SuFileInputStream.open(File(p.path)), c.vm.activity.contentResolver.openOutputStream(c.path!!)!! ) } else if (c.action == 2) { c.vm.copyPriv( c.vm.activity.contentResolver.openInputStream(c.path!!)!!, - File( - c.vm.deviceInfo!!.pbdev + (c.pi) - ) + File(p.path) ) } else if (c.action == 3) { val f = File(c.vm.logic.cacheDir, System.currentTimeMillis().toString()) @@ -150,9 +145,7 @@ private fun Flash(c: CreateBackupDataHolder) { File( c.vm.logic.assetDir, "Toolkit/simg2img" - ).absolutePath + " ${f.absolutePath} ${ - c.vm.deviceInfo!!.pbdev + (c.pi) - }" + ).absolutePath + " ${f.absolutePath} ${p.path}" ).to(terminal).exec() if (!result2.isSuccess) { terminal.add(c.vm.activity.getString(R.string.term_failure)) diff --git a/app/src/main/java/org/andbootmgr/app/CreatePartFlow.kt b/app/src/main/java/org/andbootmgr/app/CreatePartFlow.kt index c00acf4d..1e1d2b75 100644 --- a/app/src/main/java/org/andbootmgr/app/CreatePartFlow.kt +++ b/app/src/main/java/org/andbootmgr/app/CreatePartFlow.kt @@ -186,7 +186,7 @@ private class CreatePartDataHolder(val vm: WizardActivityState): ProgressListene @Composable fun lateInit() { noobMode = LocalContext.current.getSharedPreferences("abm", 0).getBoolean("noob_mode", BuildConfig.DEFAULT_NOOB_MODE) - meta = SDUtils.generateMeta(vm.deviceInfo!!.bdev, vm.deviceInfo.pbdev) + meta = SDUtils.generateMeta(vm.deviceInfo!!) (meta?.s?.find { vm.activity.intent.getLongExtra("part_sid", -1L) == it.startSector } as SDUtils.Partition.FreeSpace?)?.also { p = it } } @@ -904,6 +904,11 @@ private fun Flash(c: CreatePartDataHolder) { // After creating partitions: fun installMore() { + val meta = SDUtils.generateMeta(vm.deviceInfo!!) + if (meta == null) { + terminal.add(vm.activity.getString(R.string.term_cant_get_meta)) + return + } terminal.add(vm.activity.getString(R.string.term_building_cfg)) val entry = ConfigFile() @@ -911,7 +916,7 @@ private fun Flash(c: CreatePartDataHolder) { entry["linux"] = "$fn/zImage" entry["initrd"] = "$fn/initrd.cpio.gz" entry["dtb"] = "$fn/dtb.dtb" - if (vm.deviceInfo!!.havedtbo) + if (vm.deviceInfo.havedtbo) entry["dtbo"] = "$fn/dtbo.dtbo" entry["options"] = c.cmdline entry["xtype"] = c.rtype @@ -930,7 +935,7 @@ private fun Flash(c: CreatePartDataHolder) { val j = c.idVals.indexOf(i) terminal.add(vm.activity.getString(R.string.term_flashing_s, i)) val f = c.chosen[i]!! - val tp = File(c.vm.deviceInfo!!.pbdev + parts[j]) + val tp = File(meta.dumpKernelPartition(j).path) if (c.sparseVals[j]) { val f2 = f.toFile(c.vm) val result2 = Shell.cmd( @@ -994,13 +999,13 @@ private fun Flash(c: CreatePartDataHolder) { } vm.logic.unmountBootset() - val r = Shell.cmd(SDUtils.umsd(c.meta!!) + " && " + c.p.create(offset, offset + k, code, "")).to(terminal).exec() + val r = vm.logic.create(c.p, offset, offset + k, code, "").to(terminal).exec() try { if (r.out.join("\n").contains("kpartx")) { terminal.add(vm.activity.getString(R.string.term_reboot_asap)) } parts[it] = c.meta!!.nid.toString() - c.meta = SDUtils.generateMeta(c.vm.deviceInfo!!.bdev, c.vm.deviceInfo.pbdev) + c.meta = SDUtils.generateMeta(c.vm.deviceInfo!!) if (it + 1 < c.count.intValue) { c.p = c.meta!!.s.find { it1 -> it1.type == SDUtils.PartitionType.FREE && (offset + k) < it1.startSector } as SDUtils.Partition.FreeSpace } @@ -1011,7 +1016,7 @@ private fun Flash(c: CreatePartDataHolder) { makeOne(it + 1) } else { terminal.add(vm.activity.getString(R.string.term_created_pt)) - vm.logic.mountBootset(vm.deviceInfo) + vm.logic.mountBootset(vm.deviceInfo!!) installMore() } } else { @@ -1025,14 +1030,12 @@ private fun Flash(c: CreatePartDataHolder) { makeOne(0) } else { // Portable partition terminal.add(vm.activity.getString(R.string.term_create_part)) - val r = Shell.cmd( - SDUtils.umsd(c.meta!!) + " && " + c.p.create( + val r = vm.logic.create(c.p, c.l.toLong(), c.u.toLong(), "0700", c.t!! - ) - ).to(terminal).exec() + ).to(terminal).exec() if (r.out.join("\n").contains("kpartx")) { terminal.add(vm.activity.getString(R.string.term_reboot_asap)) } diff --git a/app/src/main/java/org/andbootmgr/app/DeviceInfo.kt b/app/src/main/java/org/andbootmgr/app/DeviceInfo.kt index 74597b2d..110551dd 100644 --- a/app/src/main/java/org/andbootmgr/app/DeviceInfo.kt +++ b/app/src/main/java/org/andbootmgr/app/DeviceInfo.kt @@ -42,9 +42,8 @@ abstract class MetaOnSdDeviceInfo : DeviceInfo { override val metaonsd: Boolean = true override fun isInstalled(logic: DeviceLogic): Boolean { return SuFile.open(bdev).exists() && run { - val meta: SDUtils.SDPartitionMeta? = - SDUtils.generateMeta(bdev, pbdev) - meta?.let { (meta.countPartitions() > 0) && (meta.dumpPartition(0).type == SDUtils.PartitionType.RESERVED) } == true + val meta = SDUtils.generateMeta(this) + meta?.let { (meta.p.isNotEmpty()) && (meta.dumpKernelPartition(0).type == SDUtils.PartitionType.RESERVED) } == true } } override fun isCorrupt(logic: DeviceLogic): Boolean { @@ -52,11 +51,11 @@ abstract class MetaOnSdDeviceInfo : DeviceInfo { } override fun getAbmSettings(logic: DeviceLogic): String? { if (SuFile.open(bdev).exists()) - SDUtils.generateMeta(bdev, pbdev)?.let { meta -> - if (meta.countPartitions() > 0) { - val part = meta.dumpPartition(0) + SDUtils.generateMeta(this)?.let { meta -> + if (meta.p.isNotEmpty()) { + val part = meta.dumpKernelPartition(0) if (part.type == SDUtils.PartitionType.RESERVED) - return pbdev + part.id + return part.path } } return null diff --git a/app/src/main/java/org/andbootmgr/app/DeviceLogic.kt b/app/src/main/java/org/andbootmgr/app/DeviceLogic.kt index bd1f543b..f41c0ec1 100644 --- a/app/src/main/java/org/andbootmgr/app/DeviceLogic.kt +++ b/app/src/main/java/org/andbootmgr/app/DeviceLogic.kt @@ -16,14 +16,7 @@ class DeviceLogic(ctx: Context) { val abmEntries = File(abmDb, "entries") var mounted = false fun mountBootset(d: DeviceInfo): Boolean { - when (val code = Shell.cmd("mountpoint -q ${abmBootset.absolutePath}").exec().code) { - 0 -> { - mounted = true - return true - } - 1 -> mounted = false - else -> throw IllegalStateException("mountpoint returned exit code $code, expected 0 or 1") - } + if (checkMounted()) return true val ast = d.getAbmSettings(this) ?: return false val result = Shell .cmd("mount $ast ${abmBootset.absolutePath}") @@ -43,14 +36,7 @@ class DeviceLogic(ctx: Context) { return true } fun unmountBootset(): Boolean { - when (val code = Shell.cmd("mountpoint -q ${abmBootset.absolutePath}").exec().code) { - 0 -> mounted = true - 1 -> { - mounted = false - return true - } - else -> throw IllegalStateException("mountpoint returned exit code $code, expected 0 or 1") - } + if (!checkMounted()) return true val result = Shell.cmd("umount ${abmBootset.absolutePath}").exec() if (!result.isSuccess) { val out = result.out.join("\n") + result.err.join("\n") @@ -66,6 +52,14 @@ class DeviceLogic(ctx: Context) { mounted = false return true } + fun checkMounted(): Boolean { + mounted = when (val code = Shell.cmd("mountpoint -q ${abmBootset.absolutePath}").exec().code) { + 0 -> true + 1 -> false + else -> throw IllegalStateException("mountpoint returned exit code $code, expected 0 or 1") + } + return mounted + } fun mount(p: SDUtils.Partition): Shell.Job { return Shell.cmd(p.mount()) } @@ -75,4 +69,10 @@ class DeviceLogic(ctx: Context) { fun delete(p: SDUtils.Partition): Shell.Job { return Shell.cmd(SDUtils.umsd(p.meta) + " && " + p.delete()) } + fun rename(p: SDUtils.Partition, name: String): Shell.Job { + return Shell.cmd(SDUtils.umsd(p.meta) + " && " + p.rename(name)) + } + fun create(p: SDUtils.Partition.FreeSpace, start: Long, end: Long, typeCode: String, name: String): Shell.Job { + return Shell.cmd(SDUtils.umsd(p.meta) + " && " + p.create(start, end, typeCode, name)) + } } \ No newline at end of file diff --git a/app/src/main/java/org/andbootmgr/app/DroidBootFlow.kt b/app/src/main/java/org/andbootmgr/app/DroidBootFlow.kt index 6ec46733..c09dbcaf 100644 --- a/app/src/main/java/org/andbootmgr/app/DroidBootFlow.kt +++ b/app/src/main/java/org/andbootmgr/app/DroidBootFlow.kt @@ -165,23 +165,16 @@ private fun Flash(vm: WizardActivityState) { val flashType = "DroidBootFlashType" Terminal(vm, logFile = "blflash_${System.currentTimeMillis()}.txt") { terminal -> terminal.add(vm.activity.getString(R.string.term_preparing_fs)) - if (vm.logic.mounted) { + if (vm.logic.checkMounted()) { terminal.add(vm.activity.getString(R.string.term_mount_state_bad)) return@Terminal } - if (!SuFile.open(vm.logic.abmDir.toURI()).exists()) { - if (!SuFile.open(vm.logic.abmDir.toURI()).mkdir()) { - terminal.add(vm.activity.getString(R.string.term_cant_create_abm_dir)) - return@Terminal - } - } if (!SuFile.open(vm.logic.abmBootset.toURI()).exists()) { if (!SuFile.open(vm.logic.abmBootset.toURI()).mkdir()) { terminal.add(vm.activity.getString(R.string.term_cant_create_mount_point)) return@Terminal } } - if (!SuFile.open(File(vm.logic.abmBootset, ".NOT_MOUNTED").toURI()).exists()) { if (!SuFile.open(File(vm.logic.abmBootset, ".NOT_MOUNTED").toURI()).createNewFile()) { terminal.add(vm.activity.getString(R.string.term_cant_create_placeholder)) @@ -190,7 +183,7 @@ private fun Flash(vm: WizardActivityState) { } if (vm.deviceInfo!!.metaonsd) { - var meta = SDUtils.generateMeta(vm.deviceInfo.bdev, vm.deviceInfo.pbdev) + var meta = SDUtils.generateMeta(vm.deviceInfo) if (meta == null) { terminal.add(vm.activity.getString(R.string.term_cant_get_meta)) return@Terminal @@ -204,16 +197,17 @@ private fun Flash(vm: WizardActivityState) { terminal.add(vm.activity.getString(R.string.term_failed_create_pt)) return@Terminal } - meta = SDUtils.generateMeta(vm.deviceInfo.bdev, vm.deviceInfo.pbdev) - val r = Shell.cmd( - SDUtils.umsd(meta!!) + " && " + (meta.dump(0) as SDUtils.Partition.FreeSpace) - .create( + meta = SDUtils.generateMeta(vm.deviceInfo) + if (meta == null) { + terminal.add(vm.activity.getString(R.string.term_cant_get_meta)) + return@Terminal + } + val r = vm.logic.create(meta.s[0] as SDUtils.Partition.FreeSpace, 0, (meta.sectors - 2048) / 41 + 2048, "8301", "abm_settings" - ) - ).to(terminal).exec() + ).to(terminal).exec() if (r.out.join("\n").contains("old")) { terminal.add(vm.activity.getString(R.string.term_reboot_asap)) } @@ -275,7 +269,7 @@ private fun Flash(vm: WizardActivityState) { terminal.add(vm.activity.getString(R.string.term_cant_write_bl)) vm.copyPriv( SuFileInputStream.open(vm.deviceInfo.blBlock), - File(vm.logic.abmDir, "backup_lk1.img") + File(vm.logic.fileDir, "backup_lk1.img") ) try { vm.copyPriv(vm.flashStream(flashType), File(vm.deviceInfo.blBlock)) diff --git a/app/src/main/java/org/andbootmgr/app/MainActivity.kt b/app/src/main/java/org/andbootmgr/app/MainActivity.kt index 64855313..054b9f8a 100644 --- a/app/src/main/java/org/andbootmgr/app/MainActivity.kt +++ b/app/src/main/java/org/andbootmgr/app/MainActivity.kt @@ -465,14 +465,7 @@ private fun PartTool(vm: MainActivityState) { ) } - var parts by remember { - mutableStateOf( - SDUtils.generateMeta( - vm.deviceInfo!!.bdev, - vm.deviceInfo!!.pbdev - ) - ) - } + var parts by remember { mutableStateOf(SDUtils.generateMeta(vm.deviceInfo!!)) } if (parts == null) { Text(stringResource(R.string.part_wizard_err)) return @@ -740,12 +733,9 @@ private fun PartTool(vm: MainActivityState) { if (!e) { processing = true rename = false - Shell.cmd(SDUtils.umsd(parts!!) + " && " + p.rename(t)).submit { r -> + vm.logic!!.rename(p, t).submit { r -> result = r.out.join("\n") + r.err.join("\n") - parts = SDUtils.generateMeta( - vm.deviceInfo!!.bdev, - vm.deviceInfo!!.pbdev - ) + parts = SDUtils.generateMeta(vm.deviceInfo!!) editPartID = parts?.s!!.findLast { it.id == p.id } processing = false } @@ -780,8 +770,7 @@ private fun PartTool(vm: MainActivityState) { vm.logic!!.mountBootset(vm.deviceInfo!!) processing = false editPartID = null - parts = - SDUtils.generateMeta(vm.deviceInfo!!.bdev, vm.deviceInfo!!.pbdev) + parts = SDUtils.generateMeta(vm.deviceInfo!!) result = it.out.join("\n") + it.err.join("\n") } }) { @@ -1004,18 +993,12 @@ private fun PartTool(vm: MainActivityState) { Thread { var tresult = "" if (e.has("xpart") && !e["xpart"].isNullOrBlank()) { - val allp = e["xpart"]!!.split(":").stream() + val allp = e["xpart"]!!.split(":") .map { parts!!.dumpKernelPartition(Integer.valueOf(it)) } - .map { it.delete() }.collect( - Collectors.toList() - ) vm.logic!!.unmountBootset() - for (s in allp) { // Do not chain, but regenerate meta and unmount every time. Thanks void - val r = Shell.cmd( - SDUtils.umsd(parts!!) + " && " + s - ).exec() - parts = - SDUtils.generateMeta(vm.deviceInfo!!.bdev, vm.deviceInfo!!.pbdev) + for (p in allp) { // Do not chain, but regenerate meta and unmount every time. Thanks void + val r = vm.logic!!.delete(p).exec() + parts = SDUtils.generateMeta(vm.deviceInfo!!) tresult += r.out.join("\n") + r.err.join("\n") + "\n" } vm.logic!!.mountBootset(vm.deviceInfo!!) @@ -1029,8 +1012,7 @@ private fun PartTool(vm: MainActivityState) { tresult += vm.activity!!.getString(R.string.cannot_delete, f.absolutePath) editEntryID = null processing = false - parts = - SDUtils.generateMeta(vm.deviceInfo!!.bdev, vm.deviceInfo!!.pbdev) + parts = SDUtils.generateMeta(vm.deviceInfo!!) result = tresult }.start() }) { diff --git a/app/src/main/java/org/andbootmgr/app/UpdateFlow.kt b/app/src/main/java/org/andbootmgr/app/UpdateFlow.kt index 8830fe85..5ec2f254 100644 --- a/app/src/main/java/org/andbootmgr/app/UpdateFlow.kt +++ b/app/src/main/java/org/andbootmgr/app/UpdateFlow.kt @@ -129,8 +129,7 @@ private fun Start(u: UpdateFlowDataHolder) { val sp = u.e!!["xpart"]!!.split(":") val p = j.getJSONObject("parts") for (k in p.keys()) { - val v = p.getString(k) - partMapping[sp[k.toInt()].toInt()] = v + partMapping[sp[k.toInt()].toInt()] = p.getString(k) } } updateJson = j.optString("updateJson") @@ -243,6 +242,8 @@ private fun dlFile(u: UpdateFlowDataHolder, l: String): File? { private fun Flash(u: UpdateFlowDataHolder) { Terminal(u.vm, logFile = "update_${System.currentTimeMillis()}.txt") { terminal -> val sp = u.e!!["xpart"]!!.split(":") + val meta = SDUtils.generateMeta(u.vm.deviceInfo!!)!! + Shell.cmd(SDUtils.umsd(SDUtils.generateMeta(u.vm.deviceInfo)!!)).exec() if (u.hasUpdate) { // online u.vm.btnsOverride = true @@ -287,9 +288,8 @@ private fun Flash(u: UpdateFlowDataHolder) { for (p in u.partMapping.entries) { val v = sp.find { p.key.toString() == it } terminal.add(u.vm.activity.getString(R.string.term_flashing_p, v)) - val k = u.vm.deviceInfo!!.pbdev + p.key val f2 = pmap[p.key]!! - val tp = File(k) + val tp = File(meta.dumpKernelPartition(p.key).path) if (u.sparse.contains(p.key)) { val result2 = Shell.cmd( File( @@ -317,7 +317,7 @@ private fun Flash(u: UpdateFlowDataHolder) { for (i in sp) { cmd += " $i" } - val r = Shell.cmd(SDUtils.umsd(SDUtils.generateMeta(u.vm.deviceInfo.bdev, u.vm.deviceInfo.pbdev)!!) + " && " + cmd).to(terminal).exec() + val r = Shell.cmd(cmd).to(terminal).exec() bootfile.forEach { it.delete() } if (!r.isSuccess) { throw IllegalStateException(u.vm.activity.getString(R.string.term_script_fail)) @@ -350,7 +350,7 @@ private fun Flash(u: UpdateFlowDataHolder) { for (i in sp) { cmd += " $i" } - val r = Shell.cmd(SDUtils.umsd(SDUtils.generateMeta(u.vm.deviceInfo.bdev, u.vm.deviceInfo.pbdev)!!) + " && " + cmd).to(terminal).exec() + val r = Shell.cmd(cmd).to(terminal).exec() bootfile.forEach { it.delete() } if (!r.isSuccess) { throw IllegalStateException(u.vm.activity.getString(R.string.term_script_fail)) diff --git a/app/src/main/java/org/andbootmgr/app/util/SDUtils.kt b/app/src/main/java/org/andbootmgr/app/util/SDUtils.kt index 30409cd7..85749503 100644 --- a/app/src/main/java/org/andbootmgr/app/util/SDUtils.kt +++ b/app/src/main/java/org/andbootmgr/app/util/SDUtils.kt @@ -2,77 +2,30 @@ package org.andbootmgr.app.util import android.util.Log import com.topjohnwu.superuser.Shell +import org.andbootmgr.app.DeviceInfo import org.andbootmgr.app.join import java.util.* import java.util.stream.Collectors object SDUtils { - // Unmount partition - fun umsd(t: PartitionType?, major: Int, minor: Int): String { - return when (t) { - PartitionType.FREE -> "true" - PartitionType.PORTABLE -> "sm unmount public:$major,$minor" - PartitionType.ADOPTED -> "sm unmount private:$major,$minor" - PartitionType.RESERVED, PartitionType.UNKNOWN -> "echo 'Warning: Unsure on how to unmount this partition.'" - else -> "echo 'Warning: Unsure on how to unmount this partition.'" - } - } - - // Unmount partition - fun umsd(t: Partition): String { - return umsd(t.type, t.major, t.minor) - } - - // Unmount partition - fun msd(t: PartitionType?, major: Int, minor: Int): String { - return when (t) { - PartitionType.FREE -> "true" - PartitionType.PORTABLE -> "sm mount public:$major,$minor" - PartitionType.ADOPTED -> "sm mount private:$major,$minor" - PartitionType.RESERVED, PartitionType.UNKNOWN -> "echo 'Warning: Unsure on how to mount this partition.'" - else -> "echo 'Warning: Unsure on how to mount this partition.'" - } - } - - // Unmount partition - fun msd(t: Partition): String { - return msd(t.type, t.major, t.minor) - } - - // Delete partition - fun rmp(t: Partition): String { - if (t.type == PartitionType.FREE) - return "echo 'Tried to delete free space'" - return "sgdisk " + t.meta.path + " --delete " + t.id - } - - // Rename partition - fun rep(t: Partition, newName: String): String { - if (t.type == PartitionType.FREE) - return "echo 'Tried to rename free space'" - return "sgdisk " + t.meta.path + " --change-name " + t.id + ":'" + newName.replace("'", "") + "'" - } - - // Unmount drive fun umsd(meta: SDPartitionMeta): String { val s = StringBuilder() - for (p in meta.p) s.append(umsd(p)).append(" && ") + for (p in meta.p) s.append(p.unmount()).append(" && ") val e = s.toString() return if (e.isEmpty()) "true" else e.substring(0, e.length - 4) } - // Generate meta - fun generateMeta(bdev: String, pbdev: String): SDPartitionMeta? { + fun generateMeta(deviceInfo: DeviceInfo): SDPartitionMeta? { val meta: SDPartitionMeta val r = - Shell.cmd("printf \"mm:%d:%d\\n\" `stat -c '0x%t 0x%T' $bdev` && sgdisk $bdev --print") + Shell.cmd("printf \"mm:%d:%d\\n\" `stat -c '0x%t 0x%T' ${deviceInfo.bdev}` && sgdisk ${deviceInfo.bdev} --print") .exec() meta = if (r.isSuccess) SDPartitionMeta() else return null try { - meta.path = bdev - meta.ppath = pbdev + meta.path = deviceInfo.bdev + meta.ppath = deviceInfo.pbdev meta.nid = 1 var temp: Long = -1 var o: String @@ -140,6 +93,7 @@ object SDUtils { meta, type, id, + meta.ppath + id, ocut[1].toLong() /* startSector */, ocut[2].toLong()/* endSector */, meta.logicalSectorSizeBytes, @@ -154,7 +108,7 @@ object SDUtils { } else if (o=="Found invalid GPT and valid MBR; converting MBR to GPT format"){ meta.ismbr=true } else if(o=="***************************************************************" || o=="in memory. ") { - + assert(true) } else { Log.e("ABM SDUtils", "can't handle $o") return null @@ -206,6 +160,7 @@ object SDUtils { var meta: SDPartitionMeta, val type: PartitionType, val id: Int, + open val path: String, val startSector: Long, val endSector: Long, bytes: Int, @@ -231,22 +186,51 @@ object SDUtils { } fun mount(): String { - return msd(this) + return when (this.type) { + PartitionType.FREE -> "echo 'Why are you trying to mount free space?!'" + PartitionType.PORTABLE -> "sm mount public:${this.major},${this.minor}" + PartitionType.ADOPTED -> "sm mount private:${this.major},${this.minor}" + else -> "echo 'Warning: Unsure on how to mount this partition.'" + } } fun unmount(): String { - return umsd(this) + return when (type) { + PartitionType.FREE -> "true" + PartitionType.PORTABLE -> "sm unmount public:${this.major},${this.minor}" + PartitionType.ADOPTED -> "sm unmount private:${this.major},${this.minor}" + PartitionType.RESERVED -> { + if (name == "abm_settings") { + // TODO unmount bootset + "true" + } else { + "echo 'Warning: Unsure on how to unmount this partition.'" + } + } + PartitionType.SYSTEM, PartitionType.DATA -> { + // TODO rework this when dual android is supported by looking at current os' xpart + "true" + } + else -> "echo 'Warning: Unsure on how to unmount this partition.'" + } } fun delete(): String { - return rmp(this) + if (type == PartitionType.FREE) + return "echo 'Tried to delete free space'" + return "sgdisk " + meta.path + " --delete " + id } fun rename(newName: String): String { - return rep(this, newName) + if (type == PartitionType.FREE) + return "echo 'Tried to rename free space'; exit 1" + return "sgdisk " + meta.path + " --change-name " + id + ":'" + newName.replace("'", "") + "'" } class FreeSpace(meta: SDPartitionMeta, start: Long, end: Long, bytes: Int) : Partition(meta, - PartitionType.FREE, 0, (start / 2048 + 1) * 2048, end, bytes, "", "", 0, 0) { + PartitionType.FREE, 0, "", (start / 2048 + 1) * 2048, end, bytes, "", "", 0, 0) { + + override val path + get() = throw IllegalArgumentException() override fun toString(): String { return "FreeSpace{" + @@ -320,34 +304,9 @@ object SDUtils { '}' } - // Count actual partitions - fun countPartitions(): Int { - return p.size - } - - // Get partition - fun dumpPartition(id: Int): Partition { - return p[id] - } - // Get partition by kernel id fun dumpKernelPartition(id: Int): Partition { return p.stream().filter { it.id == id }.findFirst().get() } - - // Count entries for partition wizard - fun count(): Int { - return u.size - } - - // Get entry for partition wizard - fun dump(id: Int): Partition { - return u[id] - } - - // Get entry for partition wizard from sorted list - fun dumpS(id: Int): Partition { - return s[id] - } } }