1#!/bin/sh
2# Test cp --sparse=always
3
4# Copyright (C) 2006-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
21require_sparse_support_
22
23# Create a sparse file.
24# It has to be at least 128K in order to be sparse on some systems.
25# Make its size one larger than 128K, in order to tickle the
26# bug in coreutils-6.0.
27size=$(expr 128 \* 1024 + 1)
28dd bs=1 seek=$size of=sparse < /dev/null 2> /dev/null || framework_failure_
29
30# Avoid reflinking. We want to test hole navigation here.
31cp_no_reflink() {
32  cp --reflink=never "$@"
33}
34
35cp_no_reflink --sparse=always sparse copy || fail=1
36
37# Ensure that the copy has the same block count as the original.
38test $(stat --printf %b copy) -le $(stat --printf %b sparse) || fail=1
39
40# Ensure that --sparse={always,never} with --reflink fail.
41returns_ 1 cp --sparse=always --reflink sparse copy || fail=1
42returns_ 1 cp --sparse=never --reflink sparse copy || fail=1
43
44
45# Ensure we handle sparse/non-sparse transitions correctly
46maxn=128 # how many $hole_size chunks per file
47hole_size=$(stat -c %o copy)
48dd if=/dev/zero bs=$hole_size count=$maxn of=zeros || framework_failure_
49tr '\0' 'U' < zeros > nonzero || framework_failure_
50
51for pattern in 1 0; do
52  test "$pattern" = 1 && pattern="$(printf '%s\n%s' nonzero zeros)"
53  test "$pattern" = 0 && pattern="$(printf '%s\n%s' zeros nonzero)"
54
55  for n in 1 2 4 11 32 $maxn; do
56    parts=$(expr $maxn / $n)
57
58    rm -f file.in
59
60    # Generate non sparse file for copying with alternating
61    # hole/data patterns of size n * $hole_size
62    for i in $(yes "$pattern" | head -n$parts); do
63      dd iflag=fullblock if=$i of=file.in conv=notrunc oflag=append \
64         bs=$hole_size count=$n status=none || framework_failure_
65    done
66
67    cp_no_reflink --sparse=always file.in sparse.out || fail=1 # non sparse in
68    cp_no_reflink --sparse=always sparse.out sparse.out2 || fail=1 # sparse in
69
70    cmp file.in sparse.out || fail=1
71    cmp file.in sparse.out2 || fail=1
72
73    ls -lsh file.in sparse.*
74  done
75done
76
77Exit $fail
78