Skip to content

Commit

Permalink
wip tidy up some more
Browse files Browse the repository at this point in the history
  • Loading branch information
nift4 committed Jul 31, 2024
1 parent 9ee6dd3 commit 5b49fa0
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 179 deletions.
19 changes: 6 additions & 13 deletions app/src/main/java/org/andbootmgr/app/BackupRestoreFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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())
Expand All @@ -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))
Expand Down
23 changes: 13 additions & 10 deletions app/src/main/java/org/andbootmgr/app/CreatePartFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}

Expand Down Expand Up @@ -904,14 +904,19 @@ 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()
entry["title"] = gn
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
Expand All @@ -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(
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand All @@ -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))
}
Expand Down
13 changes: 6 additions & 7 deletions app/src/main/java/org/andbootmgr/app/DeviceInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,20 @@ 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 {
return !SuFile.open(logic.abmDb, "db.conf").exists()
}
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
Expand Down
32 changes: 16 additions & 16 deletions app/src/main/java/org/andbootmgr/app/DeviceLogic.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand All @@ -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")
Expand All @@ -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())
}
Expand All @@ -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))
}
}
26 changes: 10 additions & 16 deletions app/src/main/java/org/andbootmgr/app/DroidBootFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
Expand All @@ -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))
}
Expand Down Expand Up @@ -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))
Expand Down
36 changes: 9 additions & 27 deletions app/src/main/java/org/andbootmgr/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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")
}
}) {
Expand Down Expand Up @@ -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!!)
Expand All @@ -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()
}) {
Expand Down
Loading

0 comments on commit 5b49fa0

Please sign in to comment.