ab63a975bf087a114634133ee917c1638612ea90
[platform/upstream/coreutils.git] / tests / ln / misc
1 #!/bin/sh
2
3 if test "$VERBOSE" = yes; then
4   set -x
5   ln --version
6 fi
7
8 pwd=`pwd`
9 tmp=t2-ln.$$
10 trap 'status=$?; cd $pwd; rm -rf $tmp && exit $status' 0
11 trap '(exit $?); exit' 1 2 13 15
12
13 framework_failure=0
14 mkdir $tmp || framework_failure=1
15 cd $tmp || framework_failure=1
16
17 t=tln-symlink
18 d=tln-subdir
19 ld=tln-symlink-to-subdir
20 f=tln-file
21 fail=0
22
23 # Create a simple symlink with both source and destination files
24 # in current directory.
25 touch $f || framework_failure=1
26 rm -f $t || framework_failure=1
27 ln -s $f $t || fail=1
28 test -f $t || fail=1
29 rm $t $f
30
31 # Create a symlink with source file and explicit destination directory/file.
32 touch $f || framework_failure=1
33 rm -rf $d || framework_failure=1
34 mkdir $d || framework_failure=1
35 ln -s ../$f $d/$t || fail=1
36 test -f $d/$t || fail=1
37 rm -rf $d $f
38
39 # Create a symlink with source file and destination directory.
40 touch $f || framework_failure=1
41 rm -rf $d || framework_failure=1
42 mkdir $d || framework_failure=1
43 ln -s ../$f $d || fail=1
44 test -f $d/$f || fail=1
45 rm -rf $d $f
46
47 # See whether a trailing slash is followed too far.
48 touch $f || framework_failure=1
49 rm -rf $d || framework_failure=1
50 mkdir $d $d/$f || framework_failure=1
51 ln $f $d/ 2> /dev/null && fail=1
52 ln -s $f $d/ 2> /dev/null && fail=1
53 rm -rf $d $f
54
55 # Make sure we get a failure with existing dest without -f option
56 touch $t || framework_failure=1
57 # FIXME: don't ignore the error message but rather test
58 # it to make sure it's the right one.
59 ln -s $t $t 2> /dev/null && fail=1
60 rm $t
61
62 # Make sure -sf fails when src and dest are the same
63 touch $t || framework_failure=1
64 ln -sf $t $t 2> /dev/null && fail=1
65 rm $t
66
67 # Create a symlink with source file and no explicit directory
68 rm -rf $d || framework_failure=1
69 mkdir $d || framework_failure=1
70 touch $d/$f || framework_failure=1
71 ln -s $d/$f || fail=1
72 test -f $f || fail=1
73 rm -rf $d $f
74
75 # Create a symlink with source file and destination symlink-to-directory.
76 rm -rf $d $f $ld || framework_failure=1
77 touch $f || framework_failure=1
78 mkdir $d || framework_failure=1
79 ln -s $d $ld
80 ln -s ../$f $ld || fail=1
81 test -f $d/$f || fail=1
82 rm -rf $d $f $ld
83
84 # Create a symlink with source file and destination symlink-to-directory.
85 # BUT use the new --no-dereference option.
86 rm -rf $d $f $ld || framework_failure=1
87 touch $f || framework_failure=1
88 mkdir $d || framework_failure=1
89 ln -s $d $ld
90 af=`pwd`/$f
91 ln --no-dereference -fs $af $ld || fail=1
92 test -f $ld || fail=1
93 rm -rf $d $f $ld
94
95 # Try to create a symlink with backup where the destination file exists
96 # and the backup file name is a hard link to the destination file.
97 touch a b || framework_failure=1
98 ln b b~ || framework_failure=1
99 ln -f --b=simple a b || fail=1
100
101 # ===================================================
102 # determine if link(2) follows symlinks on this system
103 touch a || framework_failure=1
104 ln -s a symlink || framework_failure=1
105 ln symlink hard-to-sym > /dev/null 2>&1 || framework_failure=1
106 ls=`ls -lG hard-to-sym`x
107 case "$ls" in
108   *'hard-to-symx') link_follows_symlink=yes ;;
109   *'hard-to-sym -> ax') link_follows_symlink=no ;;
110   *) framework_failure=1 ;;
111 esac
112
113 if test $link_follows_symlink = no; then
114   # Create a hard link to a dangling symlink.
115   # This is not portable.  At least sunos4.1.4 and OpenBSD 2.3 fail this test.
116   # They get this:
117   # ln: cannot create hard link `hard-to-dangle' to `no-such-dir': \
118   #   No such file or directory
119   #
120   ln -s /no-such-dir || fail=1
121   ln no-such-dir hard-to-dangle > /dev/null 2>&1 || fail=1
122 fi
123 rm -rf a symlink hard-to-sym hard-to-dangle
124 # ===================================================
125
126 # Make sure ln can make simple backups.
127 # This was fixed in 4.0.34.  Broken in 4.0r.
128 for cmd in ln cp mv ginstall; do
129   rm -rf a x a.orig
130   touch a x || framework_failure=1
131   $cmd --backup=simple --suffix=.orig x a || fail=1
132   test -f a.orig || fail=1
133 done
134
135 # ===================================================
136 # With coreutils-5.2.1, this would mistakenly access argv[1][-1].
137 # I'm including it here, in case some day programs like valgrind detect that.
138 # Purify probably would have done so.
139 ln foo '' 2> /dev/null
140
141 # ===================================================
142
143 if test $framework_failure = 1; then
144   echo 'failure in testing framework' 1>&2
145   exit 1
146 fi
147
148 exit $fail