1#!/bin/sh
2# Miscellaneous tests for "ln".
3
4# Copyright (C) 1998-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_ ln
21
22t=tln-symlink
23d=tln-subdir
24ld=tln-symlink-to-subdir
25f=tln-file
26
27# Create a simple symlink with both source and destination files
28# in current directory.
29touch $f || framework_failure_
30rm -f $t || framework_failure_
31ln -s $f $t || fail=1
32test -f $t || fail=1
33rm $t $f
34
35# Create a symlink with source file and explicit destination directory/file.
36touch $f || framework_failure_
37rm -rf $d || framework_failure_
38mkdir $d || framework_failure_
39ln -s ../$f $d/$t || fail=1
40test -f $d/$t || fail=1
41rm -rf $d $f
42
43# Create a symlink with source file and destination directory.
44touch $f || framework_failure_
45rm -rf $d || framework_failure_
46mkdir $d || framework_failure_
47ln -s ../$f $d || fail=1
48test -f $d/$f || fail=1
49rm -rf $d $f
50
51# See whether a trailing slash is followed too far.
52touch $f || framework_failure_
53rm -rf $d || framework_failure_
54mkdir $d $d/$f || framework_failure_
55returns_ 1 ln $f $d/ 2> /dev/null || fail=1
56returns_ 1 ln -s $f $d/ 2> /dev/null || fail=1
57rm -rf $d $f
58
59# Make sure we get a failure with existing dest without -f option
60touch $t || framework_failure_
61# FIXME: don't ignore the error message but rather test
62# it to make sure it's the right one.
63returns_ 1 ln -s $t $t 2> /dev/null || fail=1
64rm $t
65
66# Make sure -sf fails when src and dest are the same
67touch $t || framework_failure_
68returns_ 1 ln -sf $t $t 2> /dev/null || fail=1
69rm $t
70
71# Create a symlink with source file and no explicit directory
72rm -rf $d || framework_failure_
73mkdir $d || framework_failure_
74touch $d/$f || framework_failure_
75ln -s $d/$f || fail=1
76test -f $f || fail=1
77rm -rf $d $f
78
79# Create a symlink with source file and destination symlink-to-directory.
80rm -rf $d $f $ld || framework_failure_
81touch $f || framework_failure_
82mkdir $d || framework_failure_
83ln -s $d $ld
84ln -s ../$f $ld || fail=1
85test -f $d/$f || fail=1
86rm -rf $d $f $ld
87
88# Create a symlink with source file and destination symlink-to-directory.
89# BUT use the new --no-dereference option.
90rm -rf $d $f $ld || framework_failure_
91touch $f || framework_failure_
92mkdir $d || framework_failure_
93ln -s $d $ld
94af=$(pwd)/$f
95ln --no-dereference -fs "$af" $ld || fail=1
96test -f $ld || fail=1
97rm -rf $d $f $ld
98
99# Try to create a symlink with backup where the destination file exists
100# and the backup file name is a hard link to the destination file.
101touch a b || framework_failure_
102ln b b~ || framework_failure_
103ln -f --b=simple a b || fail=1
104
105# ===================================================
106
107# Make sure ln can make simple backups.
108# This was fixed in 4.0.34.  Broken in 4.0r.
109for cmd in ln cp mv ginstall; do
110  rm -rf a x a.orig
111  touch a x || framework_failure_
112  $cmd --backup=simple --suffix=.orig x a || fail=1
113  test -f a.orig || fail=1
114done
115
116# ===================================================
117# With coreutils-5.2.1, this would mistakenly access argv[1][-1].
118# I'm including it here, in case some day programs like valgrind detect that.
119# Purify probably would have done so.
120ln foo '' 2> /dev/null
121
122# ===================================================
123
124Exit $fail
125