Tizen 2.0 Release
[external/tizen-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 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 2 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, write to the Free Software
20 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 # 02110-1301, USA.
22
23 if test "$VERBOSE" = yes; then
24   set -x
25   cp --version
26 fi
27
28 . $srcdir/../lang-default
29 . $srcdir/../envvar-check
30
31 # Unset CDPATH.  Otherwise, output from the `cd dir' command
32 # can make this test fail.
33 (unset CDPATH) >/dev/null 2>&1 && unset CDPATH
34
35 VERSION_CONTROL=numbered; export VERSION_CONTROL
36
37 pwd=`pwd`
38 t0=`echo "$0"|sed 's,.*/,,'`.tmp;tmp=$t0/$$
39 trap 'status=$?; cd "$pwd" && exec 1>&2; rm -rf $t0 && exit $status' 0
40 trap 'exit $?' 1 2 13 15
41
42 framework_failure=0
43 mkdir -p $tmp || framework_failure=1
44 cd $tmp || framework_failure=1
45
46 if test $framework_failure = 1; then
47   echo 'failure in testing framework'
48   exit 1
49 fi
50
51 actual=actual-$$
52 expected=expected-$$
53
54 exec 1> $actual
55
56 # FIXME: This should be bigger: like more than 8k
57 contents=XYZ
58
59 for args in 'foo symlink' 'symlink foo' 'foo foo' 'sl1 sl2' 'foo hardlink'; do
60   for options in '' -d -f -df --rem -b -bd -bf -bdf \
61                  -l -dl -fl -dfl -bl -bdl -bfl -bdfl; do
62     case $args$options in
63       # These tests are not portable.
64       # They all involve making a hard link to a symbolic link.
65       'symlink foo'-dfl)
66         continue;;
67       'symlink foo'-bdl)
68         continue;;
69       'symlink foo'-bdfl)
70         continue;;
71       'sl1 sl2'-dfl)
72         continue;;
73       'sl1 sl2'-bd*l)
74         continue;;
75       'sl1 sl2'-dl)
76         continue;;
77     esac
78     rm -rf dir
79     mkdir dir
80     cd dir
81     echo $contents > foo
82     case "$args" in *symlink*) ln -s foo symlink ;; esac
83     case "$args" in *hardlink*) ln foo hardlink ;; esac
84     case "$args" in *sl1*) ln -s foo sl1;; esac
85     case "$args" in *sl2*) ln -s foo sl2;; esac
86     (
87       (
88         # echo 1>&2 cp $options $args
89         cp $options $args 2>_err
90         echo $? $options
91
92         # Normalize the program name in the error output,
93         # and put brackets around the output.
94         test -s _err && echo "[`sed 's/^[^:][^:]*:/cp:/' _err`]"
95         # Strip off all but the file names.
96         ls="`ls -gG --ignore=_err . \
97             | sed \
98                 -e '/^total /d' \
99                 -e 's/^[^ ]*  *[^ ]*  *[^ ]*  *[^ ]*  *[^ ]*  *[^ ]*  *//'`"
100         echo "($ls)"
101         # Make sure the original is unchanged and that
102         # the destination is a copy.
103         for f in $args; do
104           if test -f $f; then
105             case "`cat $f`" in
106               "$contents") ;;
107               *) echo cp FAILED;;
108             esac
109           else
110             echo symlink-loop
111           fi
112         done
113       ) | tr '\n' ' '
114       echo
115     ) | sed 's/  *$//'
116     cd ..
117   done
118   echo
119 done
120
121 cat <<\EOF > $expected
122 1 [cp: `foo' and `symlink' are the same file] (foo symlink -> foo)
123 1 -d [cp: `foo' and `symlink' are the same file] (foo symlink -> foo)
124 1 -f [cp: `foo' and `symlink' are the same file] (foo symlink -> foo)
125 1 -df [cp: `foo' and `symlink' are the same file] (foo symlink -> foo)
126 0 --rem (foo symlink)
127 0 -b (foo symlink symlink.~1~ -> foo)
128 0 -bd (foo symlink symlink.~1~ -> foo)
129 0 -bf (foo symlink symlink.~1~ -> foo)
130 0 -bdf (foo symlink symlink.~1~ -> foo)
131 0 -l (foo symlink -> foo)
132 0 -dl (foo symlink -> foo)
133 0 -fl (foo symlink -> foo)
134 0 -dfl (foo symlink)
135 0 -bl (foo symlink -> foo)
136 0 -bdl (foo symlink symlink.~1~ -> foo)
137 0 -bfl (foo symlink -> foo)
138 0 -bdfl (foo symlink symlink.~1~ -> foo)
139
140 1 [cp: `symlink' and `foo' are the same file] (foo symlink -> foo)
141 1 -d [cp: `symlink' and `foo' are the same file] (foo symlink -> foo)
142 1 -f [cp: `symlink' and `foo' are the same file] (foo symlink -> foo)
143 1 -df [cp: `symlink' and `foo' are the same file] (foo symlink -> foo)
144 1 --rem [cp: `symlink' and `foo' are the same file] (foo symlink -> foo)
145 1 -b [cp: `symlink' and `foo' are the same file] (foo symlink -> foo)
146 0 -bd (foo -> foo foo.~1~ symlink -> foo) symlink-loop symlink-loop
147 1 -bf [cp: `symlink' and `foo' are the same file] (foo symlink -> foo)
148 0 -bdf (foo -> foo foo.~1~ symlink -> foo) symlink-loop symlink-loop
149 0 -l (foo symlink -> foo)
150 0 -dl (foo symlink -> foo)
151 0 -fl (foo symlink -> foo)
152 0 -bl (foo symlink -> foo)
153 0 -bfl (foo symlink -> foo)
154
155 1 [cp: `foo' and `foo' are the same file] (foo)
156 1 -d [cp: `foo' and `foo' are the same file] (foo)
157 1 -f [cp: `foo' and `foo' are the same file] (foo)
158 1 -df [cp: `foo' and `foo' are the same file] (foo)
159 1 --rem [cp: `foo' and `foo' are the same file] (foo)
160 1 -b [cp: `foo' and `foo' are the same file] (foo)
161 1 -bd [cp: `foo' and `foo' are the same file] (foo)
162 0 -bf (foo foo.~1~)
163 0 -bdf (foo foo.~1~)
164 0 -l (foo)
165 0 -dl (foo)
166 0 -fl (foo)
167 0 -dfl (foo)
168 0 -bl (foo)
169 0 -bdl (foo)
170 0 -bfl (foo foo.~1~)
171 0 -bdfl (foo foo.~1~)
172
173 1 [cp: `sl1' and `sl2' are the same file] (foo sl1 -> foo sl2 -> foo)
174 0 -d (foo sl1 -> foo sl2 -> foo)
175 1 -f [cp: `sl1' and `sl2' are the same file] (foo sl1 -> foo sl2 -> foo)
176 0 -df (foo sl1 -> foo sl2 -> foo)
177 0 --rem (foo sl1 -> foo sl2)
178 0 -b (foo sl1 -> foo sl2 sl2.~1~ -> foo)
179 0 -bd (foo sl1 -> foo sl2 -> foo sl2.~1~ -> foo)
180 0 -bf (foo sl1 -> foo sl2 sl2.~1~ -> foo)
181 0 -bdf (foo sl1 -> foo sl2 -> foo sl2.~1~ -> foo)
182 0 -l (foo sl1 -> foo sl2 -> foo)
183 0 -fl (foo sl1 -> foo sl2 -> foo)
184 0 -bl (foo sl1 -> foo sl2 -> foo)
185 0 -bfl (foo sl1 -> foo sl2 -> foo)
186
187 1 [cp: `foo' and `hardlink' are the same file] (foo hardlink)
188 1 -d [cp: `foo' and `hardlink' are the same file] (foo hardlink)
189 1 -f [cp: `foo' and `hardlink' are the same file] (foo hardlink)
190 1 -df [cp: `foo' and `hardlink' are the same file] (foo hardlink)
191 0 --rem (foo hardlink)
192 0 -b (foo hardlink hardlink.~1~)
193 0 -bd (foo hardlink hardlink.~1~)
194 0 -bf (foo hardlink hardlink.~1~)
195 0 -bdf (foo hardlink hardlink.~1~)
196 0 -l (foo hardlink)
197 0 -dl (foo hardlink)
198 0 -fl (foo hardlink)
199 0 -dfl (foo hardlink)
200 0 -bl (foo hardlink)
201 0 -bdl (foo hardlink)
202 0 -bfl (foo hardlink)
203 0 -bdfl (foo hardlink)
204
205 EOF
206
207 fail=0;
208
209 # Some folks don't have diff.
210 cmp $expected $actual \
211   || { diff -c $expected $actual 1>&2; fail=1; }
212
213 (exit $fail); exit $fail