]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
fallocate: fix --dig-holes at end of files
authorGero Treuner <gero@70t.de>
Thu, 10 Sep 2020 19:43:03 +0000 (21:43 +0200)
committerKarel Zak <kzak@redhat.com>
Fri, 13 Nov 2020 11:02:22 +0000 (12:02 +0100)
I discovered that making a file sparse with "fallocate -d filename"
fails on the last block of a file, because - usually being partial -
the system call only zeroes that part instead of deallocating the
block. See man fallocate(2) - section "Deallocating file space".

The expected call is punching the whole block beyond eof, which
doesn't change the file length because of flag FALLOC_FL_KEEP_SIZE.

Signed-off-by: Karel Zak <kzak@redhat.com>
sys-utils/fallocate.c

index 014b94624760e4ea3ab4fc7243bd9cb986e4d2d1..ba97092fcf903a1f7b2265d55a2d4b4d7626e40a 100644 (file)
@@ -264,8 +264,11 @@ static void dig_holes(int fd, off_t file_off, off_t len)
                        off += rsz;
                }
                if (hole_sz) {
+                       off_t alloc_sz = hole_sz;
+                       if (off >= end)
+                               alloc_sz += st.st_blksize;              /* meet block boundary */
                        xfallocate(fd, FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE,
-                                       hole_start, hole_sz);
+                                       hole_start, alloc_sz);
                        ct += hole_sz;
                }
                file_off = off;