]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
tests: (fallocate) add --punch-hole test
authorChristian Goeschel Ndjomouo <cgoesc2@wgu.edu>
Sat, 2 May 2026 18:09:06 +0000 (14:09 -0400)
committerChristian Goeschel Ndjomouo <cgoesc2@wgu.edu>
Tue, 5 May 2026 13:53:39 +0000 (09:53 -0400)
Signed-off-by: Christian Goeschel Ndjomouo <cgoesc2@wgu.edu>
tests/expected/fallocate/punch-hole [new file with mode: 0644]
tests/ts/fallocate/insert-range
tests/ts/fallocate/punch-hole [new file with mode: 0755]

diff --git a/tests/expected/fallocate/punch-hole b/tests/expected/fallocate/punch-hole
new file mode 100644 (file)
index 0000000..93c180a
--- /dev/null
@@ -0,0 +1,12 @@
+file holes: 1 holes
+aa aa aa aa aa aa aa aa  aa aa aa aa aa aa aa aa  |................|
+*
+ee ee ee ee ee ee ee ee  ee ee ee ee ee ee ee ee  |................|
+*
+00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
+*
+ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
+*
+dd dd dd dd dd dd dd dd  dd dd dd dd dd dd dd dd  |................|
+*
+
index 36a28e088b89a6aea60d24e00a9a9fc093386a79..0041fd89c58441314730f13dbd6705ac287be334 100755 (executable)
@@ -25,6 +25,11 @@ ts_check_test_command "$TS_CMD_HEXDUMP"
 ts_check_test_command "$TS_CMD_FINDMNT"
 ts_check_prog "stat"
 
+fstype="$("$TS_CMD_FINDMNT" -o FSTYPE --noheadings --target "$TS_OUTDIR")"
+if [[ ! "$fstype" =~ (xfs|ext4) ]]; then
+    ts_skip "unsupported file system type $fstype"
+fi
+
 TEST_FILE="${TS_OUTDIR}/${TS_TESTNAME}.data"
 # We have to properly align the different data blocks
 # to the logical filesystem block size otherwise fallocate
diff --git a/tests/ts/fallocate/punch-hole b/tests/ts/fallocate/punch-hole
new file mode 100755 (executable)
index 0000000..fe9828d
--- /dev/null
@@ -0,0 +1,85 @@
+#!/usr/bin/env bash
+
+# This file is part of util-linux.
+#
+# This file is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This file is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# Copyright (C) 2026 Christian Goeschel Ndjomouo <cgoesc2@wgu.edu>
+
+TS_TOPDIR="${0%/*}/../.."
+TS_DESC="punch-hole"
+
+. "$TS_TOPDIR"/functions.sh
+ts_init "$*"
+
+ts_check_test_command "$TS_CMD_FALLOCATE"
+ts_check_test_command "$TS_CMD_FINDMNT"
+ts_check_test_command "$TS_CMD_HEXDUMP"
+ts_check_prog "stat"
+
+
+# The --punch-hole option is only supported by ext4, xfs, btrfs, tmpfs and gfs2
+fstype="$("$TS_CMD_FINDMNT" -o FSTYPE --noheadings --target "$TS_OUTDIR")"
+if [[ ! "$fstype" =~ (xfs|ext4|btrfs|tmpfs|gfs2) ]]; then
+    ts_skip "unsupported file system type $fstype"
+fi
+
+TEST_FILE="${TS_OUTDIR}/${TS_TESTNAME}.data"
+# We will use the logical block size to deliberately misalign
+# the punch range, so that we can include two partial blocks and
+# one full.
+BLKSIZE="$(stat --file-system --format=%s "$TS_OUTDIR")"
+
+# Starting point:
+#
+# 0        4096     8192     12288    16384    20480
+# |AAAAAAAA|EEEEEEEE|BBBBBBBB|FFFFFFFF|DDDDDDDD|
+#               |                 |
+#                   Punch range
+
+# Expected result:
+#
+# |AAAAAAAA|EEEE0000|(00000000)|0000FFFF|DDDDDDDD|
+#                   |          |
+# deallocated range -----^
+#
+# The zeroes in the deallocated range are just a 'fake'
+# report and not actually occupying any disk space, it
+# basically created a gap in the file extents map.
+# However, the zeroes in the other two partial blocks are
+# written.
+{
+  printf "%*s" "$BLKSIZE" '' | tr ' ' '\252'
+  printf "%*s" "$BLKSIZE" '' | tr ' ' '\356'
+  printf "%*s" "$BLKSIZE" '' | tr ' ' '\273'
+  printf "%*s" "$BLKSIZE" '' | tr ' ' '\377'
+  printf "%*s" "$BLKSIZE" '' | tr ' ' '\335'
+} >"$TEST_FILE"
+
+size_before="$(stat --format=%s "$TEST_FILE" 2>>"$TS_ERRLOG")"
+
+"$TS_CMD_FALLOCATE" --punch-hole --offset $(( ( BLKSIZE * 2 ) - ( BLKSIZE / 2 ) )) \
+                        --length $(( BLKSIZE * 2 )) "$TEST_FILE" >>"$TS_OUTPUT" 2>>"$TS_ERRLOG"
+
+size_after="$(stat --format=%s "$TEST_FILE" 2>>"$TS_ERRLOG")"
+
+if (( size_before != size_after )); then
+    rm -f "$TEST_FILE"
+    ts_failed "file size changed unexpectedly (before: $size_before, after: $size_after, block size: $BLKSIZE)"
+fi
+
+{
+    "$TS_CMD_FALLOCATE" --report-holes "$TEST_FILE" | grep -o 'file holes: 1 holes'
+    "$TS_CMD_HEXDUMP" --canonical "$TEST_FILE" | sed -e 's/^[[:alnum:]]*[[:space:]]*//g'
+} >>"$TS_OUTPUT" 2>>"$TS_ERRLOG"
+
+rm -f "$TEST_FILE"
+ts_finalize