From ee7f4bee9b503176bed57882ca5e8ba4d6a15c3e Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Fri, 6 Nov 2020 11:38:51 +0100 Subject: [PATCH] sfdisk: fix backward --move-data * fix final message where number of moved sectors overflow number of all moved sectors * align last step size before we use it for backward mode source and destination offsets calculation Addresses: https://github.com/karelzak/util-linux/issues/1176 Signed-off-by: Karel Zak --- disk-utils/sfdisk.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/disk-utils/sfdisk.c b/disk-utils/sfdisk.c index 804181964f..963cc745c2 100644 --- a/disk-utils/sfdisk.c +++ b/disk-utils/sfdisk.c @@ -529,18 +529,19 @@ static int move_partition_data(struct sfdisk *sf, size_t partno, struct fdisk_pa prev = 0; for (cc = 1, i = 0; i < nsectors && nbytes > 0; i += step, cc++) { - if (backward) - src -= step_bytes, dst -= step_bytes; - - DBG(MISC, ul_debug("#%05zu: src=%ju dst=%ju", cc, src, dst)); if (nbytes < step_bytes) { - DBG(MISC, ul_debug(" aligning step from %ju to %ju", - step_bytes, nbytes)); + DBG(MISC, ul_debug("aligning step #%05zu from %ju to %ju", + cc, step_bytes, nbytes)); step_bytes = nbytes; } nbytes -= step_bytes; + if (backward) + src -= step_bytes, dst -= step_bytes; + + DBG(MISC, ul_debug("#%05zu: src=%ju dst=%ju", cc, src, dst)); + if (!sf->noact) { /* read source */ if (lseek(fd, src, SEEK_SET) == (off_t) -1 || @@ -611,6 +612,12 @@ next: fputc(' ', stdout); fflush(stdout); fputc('\r', stdout); + + if (i > nsectors) + /* see for() above; @i has to be greater than @nsectors + * on success due to i += step */ + i = nsectors; + fprintf(stdout, _("Moved %ju from %ju sectors (%.0f%%)."), i, nsectors, 100.0 / ((double) nsectors/(i+1))); -- 2.47.2