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