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 <kzak@redhat.com>
This commit is contained in:
Karel Zak 2020-11-06 11:38:51 +01:00
parent de9cbcdcef
commit ee7f4bee9b
1 changed files with 14 additions and 7 deletions

View File

@ -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 (nbytes < step_bytes) {
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 (nbytes < step_bytes) {
DBG(MISC, ul_debug(" aligning step from %ju to %ju",
step_bytes, nbytes));
step_bytes = nbytes;
}
nbytes -= step_bytes;
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)));