1#!/bin/sh
2# Demonstrate that when moving a symlink onto a hardlink-to-that-symlink,
3# an error is presented.  Depending on your kernel (e.g., Linux, Solaris,
4# but not NetBSD), prior to coreutils-8.16, the mv would successfully perform
5# a no-op.  I.e., surprisingly, mv s1 s2 would succeed, yet fail to remove s1.
6
7# Copyright (C) 2012-2023 Free Software Foundation, Inc.
8
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18
19# You should have received a copy of the GNU General Public License
20# along with this program.  If not, see <https://www.gnu.org/licenses/>.
21
22. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
23print_ver_ mv
24
25# Create a file f, and a symlink s1 to that file.
26touch f || framework_failure_
27ln -s f s2 || framework_failure_
28
29# Attempt to create a hard link to that symlink.
30# On some systems, it's not possible: they create a hard link to the referent.
31ln s2 s1 || framework_failure_
32
33# If s1 is not a symlink, skip this test.
34test -h s1 \
35  || skip_ your kernel or file system cannot create a hard link to a symlink
36
37for opt in '' --backup; do
38
39  if test "$opt" = --backup; then
40    mv $opt s1 s2 > out 2>&1 || fail=1
41    compare /dev/null out || fail=1
42
43    # Ensure that s1 is gone.
44    test -e s1 && fail=1
45
46    # With --backup, ensure that the backup file was created.
47    ref=$(readlink s2~) || fail=1
48    test "$ref" = f || fail=1
49  else
50    echo "mv: 's1' and 's2' are the same file" > exp
51    mv $opt s1 s2 2>err && fail=1
52    compare exp err || fail=1
53
54    # Ensure that s1 is still present.
55    test -e s1 || fail=1
56
57    # Without --backup, ensure there is no backup file.
58    test -e s2~ && fail=1
59  fi
60
61done
62
63Exit $fail
64