2 # Exercise cp --link's behavior regarding the dereferencing of symbolic links.
4 # Copyright (C) 2013-2024 Free Software Foundation, Inc.
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.
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.
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/>.
19 .
"${srcdir=.}/tests/init.sh"; path_prepend_ .
/src
22 if { grep '^#define HAVE_LINKAT 1' "$CONFIG_HEADER" > /dev
/null \
23 && grep '#undef LINKAT_SYMLINK_NOTSUP' "$CONFIG_HEADER" > /dev
/null
; } \
24 ||
grep '^#define LINK_FOLLOWS_SYMLINKS 0' "$CONFIG_HEADER" > /dev
/null
; then
25 # With this config cp will attempt to linkat() to hardlink a symlink.
26 # So now double check the current file system supports this operation.
27 ln -s testtarget test_sl || framework_failure_
28 ln -P test_sl test_hl_sl || framework_failure_
29 ino_sl
="$(stat -c '%i' test_sl)" || framework_failure_
30 ino_hl
="$(stat -c '%i' test_hl_sl)" || framework_failure_
31 test "$ino_sl" = "$ino_hl" && can_hardlink_to_symlink
=1
34 mkdir dir || framework_failure_
35 > file || framework_failure_
36 ln -s dir dirlink || framework_failure_
37 ln -s file filelink || framework_failure_
38 ln -s nowhere danglink || framework_failure_
40 # printf format of the output line.
41 outformat
='%s|result=%s|inode=%s|type=%s|error=%s\n'
43 for src
in dirlink filelink danglink
; do
44 # Get symlink's target.
45 tgt
=$
(readlink
$src) || framework_failure_
46 # Get inodes and file type of the symlink (src) and its target (tgt).
47 # Note: this will fail for 'danglink'; catch it.
48 ino_src
="$(stat -c '%i' $src)" || framework_failure_
49 typ_src
="$(stat -c '%F' $src)" || framework_failure_
50 ino_tgt
="$(stat -c '%i' $tgt 2>/dev/null)" || ino_tgt
=
51 typ_tgt
="$(stat -c '%F' $tgt 2>/dev/null)" || typ_tgt
=
53 for o
in '' -L -H -P; do
55 # Skip the -P case where we don't or can't hardlink symlinks
56 ! test "$can_hardlink_to_symlink" && test "$o" = '-P' && continue
60 command="cp --link $o $r $src dst"
64 # Get inode and file type of the destination (which may fail, too).
65 ino_dst
="$(stat -c '%i' dst 2>/dev/null)" || ini_dst
=
66 typ_dst
="$(stat -c '%F' dst 2>/dev/null)" || typ_dst
=
68 # Print the actual result in a certain format.
78 if [ "$o" = "-P" ]; then
79 # cp --link should not dereference if -P is given.
84 elif [ "$src" = 'danglink' ]; then
85 # Dereferencing should fail for the 'danglink'.
89 exp_error
="cp: cannot stat 'danglink': No such file or directory"
90 elif [ "$src" = 'dirlink' ] && [ "$r" != '-R' ]; then
91 # Dereferencing should fail for the 'dirlink' without -R.
95 exp_error
="cp: -r not specified; omitting directory 'dirlink'"
96 elif [ "$src" = 'dirlink' ]; then
97 # cp --link -R 'dirlink' should create a new directory.
103 # cp --link 'filelink' should create a hard link to the target.
110 # Print the expected result in a certain format.
111 printf "$outformat" \
119 compare exp out ||
{ ls -lid $src $tgt dst
; fail
=1; }
121 rm -rf dst err exp out || framework_failure_