int ok = 0, fd, backward = 0;
fdisk_sector_t nsectors, from, to, step, i, prev;
size_t io, ss, step_bytes, cc;
- uintmax_t src, dst;
+ uintmax_t src, dst, nbytes;
int errsv, progress = 0;
struct timeval prev_time;
uint64_t bytes_per_sec = 0;
from = fdisk_partition_get_start(orig_pa);
to = fdisk_partition_get_start(pa);
+
if ((to >= from && from + nsectors >= to) ||
(from >= to && to + nsectors >= from)) {
/* source and target overlay, check if we need to copy
while (nsectors % step)
step--;
+ nbytes = nsectors * ss;
+
DBG(MISC, ul_debug(" step: %ju (%zu bytes)", (uintmax_t)step, step_bytes));
#if defined(POSIX_FADV_SEQUENTIAL) && defined(HAVE_POSIX_FADVISE)
gettimeofday(&prev_time, NULL);
prev = 0;
- for (cc = 1, i = 0; i < nsectors; i += step, cc++) {
+ for (cc = 1, i = 0; i < nsectors && nbytes > 0; i += step, cc++) {
ssize_t rc;
if (backward)
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)