1#!/bin/sh
2# Ensure that hard-linked files are counted (and listed) only once.
3# Likewise for excluded directories.
4# Ensure that hard links _are_ listed twice when using --count-links.
5
6# Copyright (C) 2003-2023 Free Software Foundation, Inc.
7
8# This program is free software: you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation, either version 3 of the License, or
11# (at your option) any later version.
12
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17
18# You should have received a copy of the GNU General Public License
19# along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
21. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
22print_ver_ du
23
24mkdir -p dir/sub
25( cd dir &&
26  { echo non-empty > f1
27    ln f1 f2
28    ln -s f1 f3
29    echo non-empty > sub/F; } )
30
31du -a -L --exclude=sub --count-links dir \
32  | sed 's/^[0-9][0-9]*	//' | sort -r > out || fail=1
33
34# For these tests, transform f1 or f2 or f3 (whichever name is find
35# first) to f_.  That is necessary because, depending on the type of
36# file system, du could encounter any of those linked files first,
37# thus listing that one and not the others.
38for args in '-L' 'dir' '-L dir'
39do
40  echo === >> out
41  du -a --exclude=sub $args dir \
42    | sed 's/^[0-9][0-9]*	//' | sed 's/f[123]/f_/' >> out || fail=1
43done
44
45cat <<\EOF > exp
46dir/f3
47dir/f2
48dir/f1
49dir
50===
51dir/f_
52dir
53===
54dir/f_
55dir/f_
56dir
57===
58dir/f_
59dir
60EOF
61
62compare exp out || fail=1
63
64Exit $fail
65