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