]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
sfdisk: fix backward --move-data
authorKarel Zak <kzak@redhat.com>
Fri, 6 Nov 2020 10:38:51 +0000 (11:38 +0100)
committerKarel Zak <kzak@redhat.com>
Fri, 6 Nov 2020 10:38:51 +0000 (11:38 +0100)
* 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>
disk-utils/sfdisk.c

index 804181964ffdb04a4275a62d84defbf6ba361705..963cc745c2067e37d67e5c417a803493c183c0b8 100644 (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 (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)));