doc: move @shortcontents and @contents from end to start
[platform/upstream/coreutils.git] / tests / cp / same-file
1 #!/bin/sh
2 # Test some of cp's options and how cp handles situations in
3 # which a naive implementation might overwrite the source file.
4
5 # Copyright (C) 1998, 1999, 2000, 2002, 2003, 2004, 2006-2008 Free Software
6 # Foundation, Inc.
7
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17
18 # You should have received a copy of the GNU General Public License
19 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21 if test "$VERBOSE" = yes; then
22   set -x
23   cp --version
24 fi
25
26 . $srcdir/test-lib.sh
27
28 # Unset CDPATH.  Otherwise, output from the `cd dir' command
29 # can make this test fail.
30 (unset CDPATH) >/dev/null 2>&1 && unset CDPATH
31
32 VERSION_CONTROL=numbered; export VERSION_CONTROL
33
34 # Determine whether a hard link to a symlink points to the symlink
35 # itself or to its referent.  For example, the link from FreeBSD6.1
36 # does dereference a symlink, but the one from Linux does not.
37 ln -s no-such dangling-slink
38 ln dangling-slink hard-link > /dev/null 2>&1 \
39   && hard_link_to_symlink_does_the_deref=no \
40   || hard_link_to_symlink_does_the_deref=yes
41 rm -f no-such dangling-slink hard-link
42
43 test $hard_link_to_symlink_does_the_deref = yes \
44     && remove_these_sed='/^0 -[bf]*l .*sl1 ->/d' \
45     || remove_these_sed='/^ELIDE NO TEST OUTPUT/d'
46
47 actual=actual-$$
48 expected=expected-$$
49
50 exec 1> $actual
51
52 # FIXME: This should be bigger: like more than 8k
53 contents=XYZ
54
55 for args in 'foo symlink' 'symlink foo' 'foo foo' 'sl1 sl2' 'foo hardlink'; do
56   for options in '' -d -f -df --rem -b -bd -bf -bdf \
57                  -l -dl -fl -dfl -bl -bdl -bfl -bdfl; do
58     case $args$options in
59       # These tests are not portable.
60       # They all involve making a hard link to a symbolic link.
61       # In the past, we've skipped the tests that are not portable,
62       # by doing "continue" here and eliminating the corresponding
63       # expected output lines below.  Don't do that anymore.
64       'symlink foo'-dfl)
65         continue;;
66       'symlink foo'-bdl)
67         continue;;
68       'symlink foo'-bdfl)
69         continue;;
70       'sl1 sl2'-dfl)
71         continue;;
72       'sl1 sl2'-bd*l)
73         continue;;
74       'sl1 sl2'-dl)
75         continue;;
76     esac
77
78     # cont'd  Instead, skip them only on systems for which link does
79     # dereference a symlink.  Detect and skip such tests here.
80     case $hard_link_to_symlink_does_the_deref:$args:$options in
81       'yes:sl1 sl2:-fl')
82         continue ;;
83       'yes:sl1 sl2:-bl')
84         continue ;;
85       'yes:sl1 sl2:-bfl')
86         continue ;;
87     esac
88
89     rm -rf dir
90     mkdir dir
91     cd dir
92     echo $contents > foo
93     case "$args" in *symlink*) ln -s foo symlink ;; esac
94     case "$args" in *hardlink*) ln foo hardlink ;; esac
95     case "$args" in *sl1*) ln -s foo sl1;; esac
96     case "$args" in *sl2*) ln -s foo sl2;; esac
97     (
98       (
99         # echo 1>&2 cp $options $args
100         cp $options $args 2>_err
101         echo $? $options
102
103         # Normalize the program name and diagnostics in the error output,
104         # and put brackets around the output.
105         if test -s _err; then
106           sed '
107             s/^[^:]*:\([^:]*\).*/cp:\1/
108             1s/^/[/
109             $s/$/]/
110           ' _err
111         fi
112         # Strip off all but the file names.
113         ls=`ls -gG --ignore=_err . \
114             | sed \
115                 -e '/^total /d' \
116                 -e 's/^[^ ]*  *[^ ]*  *[^ ]*  *[^ ]*  *[^ ]*  *[^ ]*  *//'`
117         echo "($ls)"
118         # Make sure the original is unchanged and that
119         # the destination is a copy.
120         for f in $args; do
121           if test -f $f; then
122             case "`cat $f`" in
123               "$contents") ;;
124               *) echo cp FAILED;;
125             esac
126           else
127             echo symlink-loop
128           fi
129         done
130       ) | tr '\n' ' '
131       echo
132     ) | sed 's/  *$//'
133     cd ..
134   done
135   echo
136 done
137
138 cat <<\EOF | sed "$remove_these_sed" > $expected
139 1 [cp: `foo' and `symlink' are the same file] (foo symlink -> foo)
140 1 -d [cp: `foo' and `symlink' are the same file] (foo symlink -> foo)
141 1 -f [cp: `foo' and `symlink' are the same file] (foo symlink -> foo)
142 1 -df [cp: `foo' and `symlink' are the same file] (foo symlink -> foo)
143 0 --rem (foo symlink)
144 0 -b (foo symlink symlink.~1~ -> foo)
145 0 -bd (foo symlink symlink.~1~ -> foo)
146 0 -bf (foo symlink symlink.~1~ -> foo)
147 0 -bdf (foo symlink symlink.~1~ -> foo)
148 1 -l [cp: cannot create link `symlink'] (foo symlink -> foo)
149 0 -dl (foo symlink -> foo)
150 0 -fl (foo symlink)
151 0 -dfl (foo symlink)
152 0 -bl (foo symlink symlink.~1~ -> foo)
153 0 -bdl (foo symlink symlink.~1~ -> foo)
154 0 -bfl (foo symlink symlink.~1~ -> foo)
155 0 -bdfl (foo symlink symlink.~1~ -> foo)
156
157 1 [cp: `symlink' and `foo' are the same file] (foo symlink -> foo)
158 1 -d [cp: `symlink' and `foo' are the same file] (foo symlink -> foo)
159 1 -f [cp: `symlink' and `foo' are the same file] (foo symlink -> foo)
160 1 -df [cp: `symlink' and `foo' are the same file] (foo symlink -> foo)
161 1 --rem [cp: `symlink' and `foo' are the same file] (foo symlink -> foo)
162 1 -b [cp: `symlink' and `foo' are the same file] (foo symlink -> foo)
163 0 -bd (foo -> foo foo.~1~ symlink -> foo) symlink-loop symlink-loop
164 1 -bf [cp: `symlink' and `foo' are the same file] (foo symlink -> foo)
165 0 -bdf (foo -> foo foo.~1~ symlink -> foo) symlink-loop symlink-loop
166 0 -l (foo symlink -> foo)
167 0 -dl (foo symlink -> foo)
168 0 -fl (foo symlink -> foo)
169 0 -bl (foo symlink -> foo)
170 0 -bfl (foo symlink -> foo)
171
172 1 [cp: `foo' and `foo' are the same file] (foo)
173 1 -d [cp: `foo' and `foo' are the same file] (foo)
174 1 -f [cp: `foo' and `foo' are the same file] (foo)
175 1 -df [cp: `foo' and `foo' are the same file] (foo)
176 1 --rem [cp: `foo' and `foo' are the same file] (foo)
177 1 -b [cp: `foo' and `foo' are the same file] (foo)
178 1 -bd [cp: `foo' and `foo' are the same file] (foo)
179 0 -bf (foo foo.~1~)
180 0 -bdf (foo foo.~1~)
181 0 -l (foo)
182 0 -dl (foo)
183 0 -fl (foo)
184 0 -dfl (foo)
185 0 -bl (foo)
186 0 -bdl (foo)
187 0 -bfl (foo foo.~1~)
188 0 -bdfl (foo foo.~1~)
189
190 1 [cp: `sl1' and `sl2' are the same file] (foo sl1 -> foo sl2 -> foo)
191 0 -d (foo sl1 -> foo sl2 -> foo)
192 1 -f [cp: `sl1' and `sl2' are the same file] (foo sl1 -> foo sl2 -> foo)
193 0 -df (foo sl1 -> foo sl2 -> foo)
194 0 --rem (foo sl1 -> foo sl2)
195 0 -b (foo sl1 -> foo sl2 sl2.~1~ -> foo)
196 0 -bd (foo sl1 -> foo sl2 -> foo sl2.~1~ -> foo)
197 0 -bf (foo sl1 -> foo sl2 sl2.~1~ -> foo)
198 0 -bdf (foo sl1 -> foo sl2 -> foo sl2.~1~ -> foo)
199 1 -l [cp: cannot create link `sl2'] (foo sl1 -> foo sl2 -> foo)
200 0 -fl (foo sl1 -> foo sl2 -> foo)
201 0 -bl (foo sl1 -> foo sl2 -> foo sl2.~1~ -> foo)
202 0 -bfl (foo sl1 -> foo sl2 -> foo sl2.~1~ -> foo)
203
204 1 [cp: `foo' and `hardlink' are the same file] (foo hardlink)
205 1 -d [cp: `foo' and `hardlink' are the same file] (foo hardlink)
206 1 -f [cp: `foo' and `hardlink' are the same file] (foo hardlink)
207 1 -df [cp: `foo' and `hardlink' are the same file] (foo hardlink)
208 0 --rem (foo hardlink)
209 0 -b (foo hardlink hardlink.~1~)
210 0 -bd (foo hardlink hardlink.~1~)
211 0 -bf (foo hardlink hardlink.~1~)
212 0 -bdf (foo hardlink hardlink.~1~)
213 0 -l (foo hardlink)
214 0 -dl (foo hardlink)
215 0 -fl (foo hardlink)
216 0 -dfl (foo hardlink)
217 0 -bl (foo hardlink)
218 0 -bdl (foo hardlink)
219 0 -bfl (foo hardlink)
220 0 -bdfl (foo hardlink)
221
222 EOF
223
224 fail=0;
225
226 compare $expected $actual 1>&2 || fail=1
227
228 Exit $fail