@@ -139,10 +139,18 @@ private class CreatePartDataHolder(val vm: WizardActivityState, val desiredStart
139139 var code by mutableStateOf(code)
140140 var id by mutableStateOf(id)
141141 var sparse by mutableStateOf(sparse)
142+ fun resolveSectorSize (c : CreatePartDataHolder , remaining : Long ): Long {
143+ return if (! isPercent /* bytes*/ ) {
144+ size / c.meta!! .logicalSectorSizeBytes
145+ } else /* percent*/ {
146+ (BigDecimal (remaining).multiply(BigDecimal (size).divide(BigDecimal (100 )))).toLong()
147+ }
148+ }
142149 }
143150 val parts = mutableStateListOf<Part >()
144151 val inetAvailable = HashMap <String , Pair <String , String >>()
145152 val idNeeded = mutableStateListOf<String >()
153+ val extraIdNeeded = mutableListOf<String >()
146154 val chosen = mutableStateMapOf<String , DledFile >()
147155 val client by lazy { OkHttpClient ().newBuilder().readTimeout(1L , TimeUnit .HOURS ).addNetworkInterceptor {
148156 val originalResponse: Response = it.proceed(it.request())
@@ -263,7 +271,7 @@ private fun Start(c: CreatePartDataHolder) {
263271 }
264272 TextField (value = partitionName, onValueChange = {
265273 partitionName = it
266- }, isError = partitionNameInvalid, label = {
274+ }, isError = partitionNameInvalid && partitionName.isNotEmpty() , label = {
267275 Text (stringResource(R .string.part_name))
268276 })
269277 Row (horizontalArrangement = Arrangement .End , modifier = Modifier
@@ -388,6 +396,7 @@ private fun Shop(c: CreatePartDataHolder) {
388396 if (idUnneeded.contains(id))
389397 throw IllegalStateException (" id in both blockIdNeeded and extraIdNeeded" )
390398 idNeeded.add(id)
399+ this .extraIdNeeded.add(id)
391400 i++
392401 }
393402 i = 0
@@ -550,11 +559,7 @@ private fun Os(c: CreatePartDataHolder) {
550559 var sizeInSectors: Long = - 1
551560 var remaining = c.endSectorRelative - c.startSectorRelative
552561 for (iPart in c.parts.slice(0 .. i)) {
553- sizeInSectors = if (! iPart.isPercent /* bytes*/ ) {
554- iPart.size / c.meta!! .logicalSectorSizeBytes
555- } else /* percent*/ {
556- (BigDecimal (remaining).multiply(BigDecimal (iPart.size).divide(BigDecimal (100 )))).toLong()
557- }
562+ sizeInSectors = iPart.resolveSectorSize(c, remaining)
558563 remaining - = sizeInSectors
559564 }
560565 remaining + = sizeInSectors
@@ -663,13 +668,7 @@ private fun Os(c: CreatePartDataHolder) {
663668 }
664669 var remaining = c.endSectorRelative - c.startSectorRelative
665670 for (part in c.parts) {
666- val sizeInSectors = if (! part.isPercent /* bytes*/ ) {
667- part.size / c.meta!! .logicalSectorSizeBytes
668- } else /* percent*/ {
669- // remaining * (part.int/100) -> part.int percent of remaining
670- (BigDecimal (remaining).multiply(BigDecimal (part.size).divide(BigDecimal (100 )))).toLong()
671- }
672- remaining - = sizeInSectors
671+ remaining - = part.resolveSectorSize(c, remaining)
673672 }
674673 Text (stringResource(R .string.remaining_sector, remaining, c.endSectorRelative - c.startSectorRelative))
675674 }
@@ -867,24 +866,31 @@ private fun Flash(c: CreatePartDataHolder) {
867866 terminal.add(vm.activity.getString(R .string.term_creating_pt))
868867
869868 vm.logic.unmountBootset()
869+ val startSectorAbsolute = c.p.startSector + c.startSectorRelative
870+ val endSectorAbsolute = c.p.startSector + c.endSectorRelative
871+ if (endSectorAbsolute > c.p.endSector)
872+ throw IllegalArgumentException (" $endSectorAbsolute can't be bigger than ${c.p.endSector} " )
870873 c.parts.forEachIndexed { index, part ->
871874 terminal.add(vm.activity.getString(R .string.term_create_part))
872- val k = if (! part.isPercent /* bytes*/ ) {
873- part.size / c.meta!! .logicalSectorSizeBytes
874- } else /* percent*/ {
875- (BigDecimal (c.p.size - (c.startSectorRelative + (c.p.size - c.endSectorRelative))).multiply(BigDecimal (part.size).divide(BigDecimal (100 )))).toLong()
876- }
877-
878- val r = vm.logic.create(c.p, c.startSectorRelative, c.startSectorRelative + k, part.code, " " ).to(terminal).exec()
875+ val start = c.p.startSector.coerceAtLeast(startSectorAbsolute)
876+ val end = c.p.endSector.coerceAtMost(endSectorAbsolute)
877+ val k = part.resolveSectorSize(c, end - start)
878+ if (start + k > end)
879+ throw IllegalStateException (" $start + $k = ${start + k} shouldn't be bigger than $end " )
880+ if (k < 0 )
881+ throw IllegalStateException (" $k shouldn't be smaller than 0" )
882+ // create(start, end) values are relative to the free space area
883+ val r = vm.logic.create(c.p, start - c.p.startSector,
884+ (start + k) - c.p.startSector, part.code, " " ).to(terminal).exec()
879885 if (r.out .joinToString(" \n " ).contains(" kpartx" )) {
880886 terminal.add(vm.activity.getString(R .string.term_reboot_asap))
881887 }
882888 createdParts[part] = c.meta!! .nid
883889 c.meta = SDUtils .generateMeta(c.vm.deviceInfo)
884890 // do not assert there is leftover space if we just created the last partition we want to create
885- if (index != c.parts.size - 1 ) {
891+ if (index < c.parts.size - 1 ) {
886892 c.p =
887- c.meta!! .s.find { it.type == SDUtils .PartitionType .FREE && (c.startSectorRelative + k) < it.startSector } as SDUtils .Partition .FreeSpace
893+ c.meta!! .s.find { it.type == SDUtils .PartitionType .FREE && start + k < it.startSector } as SDUtils .Partition .FreeSpace
888894 }
889895 if (r.isSuccess) {
890896 terminal.add(vm.activity.getString(R .string.term_created_part))
@@ -948,13 +954,11 @@ private fun Flash(c: CreatePartDataHolder) {
948954
949955 terminal.add(vm.activity.getString(R .string.term_patching_os))
950956 var cmd = " FORMATDATA=true " + tmpFile.absolutePath + " $fn "
951- for (i in c.idNeeded) {
952- val j = c.parts.find { it.id == i }
953- if (j == null ) {
954- cmd + = " " + c.chosen[i]!! .toFile(vm).absolutePath
955- } else {
956- cmd + = createdParts[j]
957- }
957+ for (i in c.extraIdNeeded) {
958+ cmd + = " " + c.chosen[i]!! .toFile(vm).absolutePath
959+ }
960+ for (i in c.parts) {
961+ cmd + = " " + createdParts[i]
958962 }
959963 val result = vm.logic.runShFileWithArgs(cmd).to(terminal).exec()
960964 if (! result.isSuccess) {
0 commit comments