1#!/bin/sh
2# Exercise a few more corners of the copying code.
3
4# Copyright (C) 2011-2023 Free Software Foundation, Inc.
5
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15
16# You should have received a copy of the GNU General Public License
17# along with this program.  If not, see <https://www.gnu.org/licenses/>.
18
19. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
20print_ver_ cp dd
21
22touch sparse_chk
23seek_data_capable_ sparse_chk \
24  || skip_ "insufficient SEEK_DATA support"
25
26# Exercise the code that handles a file ending in a hole.
27printf x > k || framework_failure_
28dd bs=1k seek=128 of=k < /dev/null || framework_failure_
29
30# The first time through the outer loop, the input file, K, ends with a hole.
31# The second time through, we append a byte so that it does not.
32for append in no yes; do
33  test $append = yes && printf y >> k
34  for i in always never; do
35    cp --reflink=never --sparse=$i k k2 || fail=1
36    cmp k k2 || fail=1
37  done
38done
39
40# Ensure that --sparse=always can restore holes.
41rm -f k
42# Create a file starting with an "x", followed by 256K-1 0 bytes.
43printf x > k || framework_failure_
44dd bs=1k seek=1 of=k count=255 < /dev/zero || framework_failure_
45
46# cp should detect the all-zero blocks and convert some of them to holes.
47cp --debug --reflink=never --sparse=always k k2 >cp.out || fail=1
48cmp k k2 || fail=1
49grep 'sparse detection: .*zeros' cp.out || { cat cp.out; fail=1; }
50
51# cp should disable reflink AND copy offload with --sparse=never
52cp --debug --sparse=never k k2 >cp.out || fail=1
53cmp k k2 || fail=1
54grep 'copy offload: avoided, reflink: no' cp.out || { cat cp.out; fail=1; }
55
56Exit $fail
57