diff --git a/lib/libfile.c b/lib/libfile.c index 9de938b..863b683 100644 --- a/lib/libfile.c +++ b/lib/libfile.c @@ -353,10 +353,6 @@ goto out; } - /* Set O_TRUNC only if file exist and is a regular file */ - if (!s && S_ISREG(dststat.st_mode)) - mode |= O_TRUNC; - dstfd = open(dst, mode); if (dstfd < 0) { printf("could not open %s: %s\n", dst, errno_str()); @@ -364,12 +360,22 @@ goto out; } + ret = ftruncate(dstfd, 0); + if (ret) + goto out; + ret = stat(src, &srcstat); if (ret) goto out; - if (srcstat.st_size != FILESIZE_MAX) + if (srcstat.st_size != FILESIZE_MAX) { discard_range(dstfd, srcstat.st_size, 0); + if (S_ISREG(dststat.st_mode)) { + ret = ftruncate(dstfd, srcstat.st_size); + if (ret) + goto out; + } + } if (verbose) init_progression_bar(srcstat.st_size);