2 # Test some of cp's options and how cp handles situations in
3 # which a naive implementation might overwrite the source file.
5 # Copyright (C) 1998, 1999, 2000, 2002, 2003, 2004, 2006-2008 Free Software
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.
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.
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/>.
21 if test "$VERBOSE" = yes; then
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
32 VERSION_CONTROL=numbered; export VERSION_CONTROL
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
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'
52 # FIXME: This should be bigger: like more than 8k
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
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.
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
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
99 # echo 1>&2 cp $options $args
100 cp $options $args 2>_err
103 # Normalize the program name and diagnostics in the error output,
104 # and put brackets around the output.
105 if test -s _err; then
107 s/^[^:]*:\([^:]*\).*/cp:\1/
112 # Strip off all but the file names.
113 ls=`ls -gG --ignore=_err . \
116 -e 's/^[^ ]* *[^ ]* *[^ ]* *[^ ]* *[^ ]* *[^ ]* *//'`
118 # Make sure the original is unchanged and that
119 # the destination is a copy.
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)
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)
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)
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)
188 0 -bdfl (foo foo.~1~)
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)
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~)
216 0 -dfl (foo hardlink)
218 0 -bdl (foo hardlink)
219 0 -bfl (foo hardlink)
220 0 -bdfl (foo hardlink)
226 compare $expected $actual 1>&2 || fail=1