df: omit GCC 5 ‘assume’s
[coreutils.git] / tests / dd / sparse.sh
blobc459f4d1a1ab85391b7bbc35cf917ae7d97bbda2
1 #!/bin/sh
3 # Copyright (C) 2012-2023 Free Software Foundation, Inc.
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <https://www.gnu.org/licenses/>.
18 . "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
19 print_ver_ dd
20 is_local_dir_ . || very_expensive_
21 require_sparse_support_
23 # Ensure basic sparse generation works
24 truncate -s1M sparse
25 dd bs=32K if=sparse of=sparse.dd conv=sparse
26 test $(stat -c %s sparse) = $(stat -c %s sparse.dd) || fail=1
28 # Demonstrate that conv=sparse with oflag=append,
29 # will do ineffective seeks in the output
30 printf 'a\000\000b' > file.in
31 printf 'ab' > exp
32 dd if=file.in bs=1 conv=sparse oflag=append > out
33 compare exp out || fail=1
35 # Demonstrate conv=sparse with conv=notrunc,
36 # where data in file.out is not overwritten with NULs
37 printf '____' > out
38 printf 'a__b' > exp
39 dd if=file.in bs=1 conv=sparse,notrunc of=out
40 compare exp out || fail=1
42 # Ensure we fall back to write if seek fails
43 dd if=file.in bs=1 conv=sparse | cat > file.out
44 cmp file.in file.out || fail=1
46 # Setup for block size tests: create a 3MiB file with a 1MiB
47 # stretch of NUL bytes in the middle.
48 rm -f file.in
49 dd if=/dev/urandom of=file.in bs=1M count=3 iflag=fullblock || fail=1
50 dd if=/dev/zero of=file.in bs=1M count=1 seek=1 conv=notrunc || fail=1
52 kb_alloc() { du -k "$1"|cut -f1; }
54 # sync out data for async allocators like NFS/BTRFS
55 # sync file.in || fail=1
57 # If our just-created input file appears to be too small,
58 # skip the remaining tests. On at least Solaris 10 with NFS,
59 # file.in is reported to occupy <= 1KiB for about 50 seconds
60 # after its creation.
61 if test $(kb_alloc file.in) -gt 3000; then
63 # Ensure NUL blocks smaller than the *output* block size are not made sparse.
64 # Here, with a 2MiB block size, dd's conv=sparse must *not* introduce a hole.
65 dd if=file.in of=file.out ibs=1M obs=2M conv=sparse || fail=1
67 # Intermittently BTRFS returns 0 allocation for file.out unless synced
68 sync file.out || framework_failure_
69 test 2500 -lt $(kb_alloc file.out) || fail=1
71 # Note we recreate a sparse file first to avoid
72 # speculative preallocation seen in XFS, where a write() that
73 # extends a file can preallocate some extra space that
74 # a subsequent seek will not convert to a hole.
75 rm -f file.out
76 truncate --size=3M file.out
78 # Ensure that this 1MiB *output* block of NULs *is* converted to a hole.
79 dd if=file.in of=file.out ibs=2M obs=1M conv=sparse,notrunc
80 if test $(kb_alloc file.out) -ge 2500; then
81 # Double check the failure by creating a sparse file in
82 # the traditional manner for comparison, as we're not guaranteed
83 # that seek=1M will create a hole. apfs on darwin 19.2.0 for example
84 # was seen to not to create holes < 16MiB.
85 dd if=file.in of=manual.out bs=1M count=1 || fail=1
86 dd if=file.in of=manual.out bs=1M count=1 seek=2 conv=notrunc || fail=1
88 test $(kb_alloc file.out) -eq $(kb_alloc manual.out) || fail=1
93 Exit $fail