Tizen 2.0 Release
[external/tizen-coreutils.git] / tests / mv / part-symlink
1 #!/bin/sh
2 # make sure cp and mv can handle many combinations of local and
3 # other-partition regular/symlink'd files.
4
5 # Copyright (C) 2000, 2003, 2004, 2006 Free Software Foundation, Inc.
6
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 2 of the License, or
10 # (at your option) any later version.
11
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.
16
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 # 02110-1301, USA.
21
22 if test "$VERBOSE" = yes; then
23   set -x
24   mv --version
25   cp --version
26 fi
27
28 pwd=`pwd`
29 tmp=part-sl.$$
30 trap 'status=$?; cd "$pwd" && exec 1>&2; rm -rf $tmp $other_partition_tmpdir && exit $status' 0
31 trap '(exit $?); exit' 1 2 13 15
32
33 pwd_tmp=$pwd/$tmp
34
35 . $srcdir/../other-fs-tmpdir
36 . $srcdir/../envvar-check
37 # Make sure the programs use C-locale formats/translations.
38 . $srcdir/../lang-default
39
40 # Unset CDPATH.  Otherwise, output from the `cd dir' command
41 # can make this test fail.
42 (unset CDPATH) >/dev/null 2>&1 && unset CDPATH
43
44 if test -z "$other_partition_tmpdir"; then
45   (exit 77); exit 77
46 fi
47
48 framework_failure=0
49 mkdir $tmp || framework_failure=1
50 cd $tmp || framework_failure=1
51
52 if test $framework_failure = 1; then
53   echo 'failure in testing framework'
54   exit 1
55 fi
56
57 fail=0
58
59 # Four cases:
60 # local regular file w/symlink on another partition
61 #   (loc_reg, rem_sl)
62 #   (rem_sl, loc_reg)
63 # local symlink to regular file on another partition
64 #   (loc_sl, rem_reg)
65 #   (rem_reg, loc_sl)
66
67 # Exercise those four cases for each of
68 # cp and mv, with lots of combinations of options.
69
70 actual=actual-$$
71 expected=expected-$$
72
73 exec 1> $actual
74
75 # FIXME: This should be bigger: like more than 8k
76 contents=XYZ
77
78 loc_reg=loc_reg
79 loc_sl=loc_sl
80 rem_reg=$other_partition_tmpdir/rem_reg
81 rem_sl=$other_partition_tmpdir/rem_sl
82
83 for copy in cp mv; do
84   for args in \
85       'loc_reg rem_sl' \
86       'rem_sl loc_reg' \
87       'loc_sl rem_reg' \
88       'rem_reg loc_sl' \
89       ; do
90     for options in '' --rem '--rem -d' '--rem -b' -b -bd -d; do
91       case "$options" in *d*|*--rem*) test $copy = mv && continue;; esac
92       rm -rf dir || fail=1
93       rm -f $other_partition_tmpdir/* || fail=1
94       mkdir dir || fail=1
95       cd dir || fail=1
96       case "$args" in *loc_reg*) reg_abs="`pwd`/$loc_reg" ;; esac
97       case "$args" in *rem_reg*) reg_abs=$rem_reg ;; esac
98       case "$args" in *loc_sl*) slink=$loc_sl ;; esac
99       case "$args" in *rem_sl*) slink=$rem_sl ;; esac
100
101       echo $contents > "$reg_abs" || fail=1
102       ln -nsf "$reg_abs" $slink || fail=1
103       actual_args=`echo $args|sed 's,^,$,;s/ / $/'`
104       actual_args=`eval echo $actual_args`
105
106       (
107         (
108           # echo 1>&2 cp $options $args
109           $copy $options $actual_args 2>.err
110           copy_status=$?
111           echo $copy_status $copy $options $args
112
113           # Normalize the program name in the error output,
114           # remove any site-dependent part of other-partition file name,
115           # and put brackets around the output.
116           test -s .err && {
117             echo '[' | tr -d '\n'
118             sed 's/^[^:][^:]*\(..\):/\1:/;s,'$other_partition_tmpdir/,, .err
119             echo ']' | tr -d '\n'
120             }
121           # Strip off all but the file names.
122           # Remove any site-dependent part of each file name.
123           ls="`ls -gG --ignore=.err . \
124               | sed \
125                   -e '/^total /d' \
126                   -e s,$other_partition_tmpdir/,, \
127                   -e "s,$pwd_tmp/,," \
128                   -e 's/^[^ ]*  *[^ ]*  *[^ ]*  *[^ ]*  *[^ ]*  *[^ ]*  *//'`"
129           ls2="`cd $other_partition_tmpdir && ls -gG --ignore=.err . \
130               | sed \
131                   -e '/^total /d' \
132                   -e s,$other_partition_tmpdir/,, \
133                   -e "s,$pwd_tmp/,," \
134                   -e 's/^[^ ]*  *[^ ]*  *[^ ]*  *[^ ]*  *[^ ]*  *[^ ]*  *//'`"
135           echo "($ls) ($ls2)"
136
137           # If the command failed, then it must not have changed the files.
138           if test $copy_status != 0; then
139             for f in $actual_args; do
140               test -f $f ||
141                 { echo "$copy FAILED but removed $f"; continue; }
142               case "`cat $f`" in
143                 "$contents") ;;
144                 *) echo "$copy FAILED but modified $f";;
145               esac
146             done
147           fi
148
149           if test $copy = cp; then
150             # Make sure the original is unchanged and that
151             # the destination is a copy.
152             for f in $actual_args; do
153               if test -f $f; then
154                 if test $copy_status != 0; then
155                   test
156                 fi
157                 case "`cat $f`" in
158                   "$contents") ;;
159                   *) echo $copy FAILED;;
160                 esac
161               else
162                 echo symlink-loop
163               fi
164             done
165           fi
166         ) | tr '\n' ' '
167         echo
168       ) | sed 's/  *$//'
169       cd ..
170     done
171     echo
172   done
173 done
174
175 test $fail = 1 &&
176   { (exit 1); exit; }
177
178 cat <<\EOF > $expected
179 1 cp loc_reg rem_sl [cp: `loc_reg' and `rem_sl' are the same file ](loc_reg) (rem_sl -> dir/loc_reg)
180 0 cp --rem loc_reg rem_sl (loc_reg) (rem_sl)
181 0 cp --rem -d loc_reg rem_sl (loc_reg) (rem_sl)
182 0 cp --rem -b loc_reg rem_sl (loc_reg) (rem_sl rem_sl~ -> dir/loc_reg)
183 0 cp -b loc_reg rem_sl (loc_reg) (rem_sl rem_sl~ -> dir/loc_reg)
184 0 cp -bd loc_reg rem_sl (loc_reg) (rem_sl rem_sl~ -> dir/loc_reg)
185 1 cp -d loc_reg rem_sl [cp: `loc_reg' and `rem_sl' are the same file ](loc_reg) (rem_sl -> dir/loc_reg)
186
187 1 cp rem_sl loc_reg [cp: `rem_sl' and `loc_reg' are the same file ](loc_reg) (rem_sl -> dir/loc_reg)
188 1 cp --rem rem_sl loc_reg [cp: `rem_sl' and `loc_reg' are the same file ](loc_reg) (rem_sl -> dir/loc_reg)
189 1 cp --rem -d rem_sl loc_reg [cp: `rem_sl' and `loc_reg' are the same file ](loc_reg) (rem_sl -> dir/loc_reg)
190 1 cp --rem -b rem_sl loc_reg [cp: `rem_sl' and `loc_reg' are the same file ](loc_reg) (rem_sl -> dir/loc_reg)
191 1 cp -b rem_sl loc_reg [cp: `rem_sl' and `loc_reg' are the same file ](loc_reg) (rem_sl -> dir/loc_reg)
192 0 cp -bd rem_sl loc_reg (loc_reg -> dir/loc_reg loc_reg~) (rem_sl -> dir/loc_reg) symlink-loop symlink-loop
193 1 cp -d rem_sl loc_reg [cp: `rem_sl' and `loc_reg' are the same file ](loc_reg) (rem_sl -> dir/loc_reg)
194
195 1 cp loc_sl rem_reg [cp: `loc_sl' and `rem_reg' are the same file ](loc_sl -> rem_reg) (rem_reg)
196 1 cp --rem loc_sl rem_reg [cp: `loc_sl' and `rem_reg' are the same file ](loc_sl -> rem_reg) (rem_reg)
197 1 cp --rem -d loc_sl rem_reg [cp: `loc_sl' and `rem_reg' are the same file ](loc_sl -> rem_reg) (rem_reg)
198 1 cp --rem -b loc_sl rem_reg [cp: `loc_sl' and `rem_reg' are the same file ](loc_sl -> rem_reg) (rem_reg)
199 1 cp -b loc_sl rem_reg [cp: `loc_sl' and `rem_reg' are the same file ](loc_sl -> rem_reg) (rem_reg)
200 0 cp -bd loc_sl rem_reg (loc_sl -> rem_reg) (rem_reg -> rem_reg rem_reg~) symlink-loop symlink-loop
201 1 cp -d loc_sl rem_reg [cp: `loc_sl' and `rem_reg' are the same file ](loc_sl -> rem_reg) (rem_reg)
202
203 1 cp rem_reg loc_sl [cp: `rem_reg' and `loc_sl' are the same file ](loc_sl -> rem_reg) (rem_reg)
204 0 cp --rem rem_reg loc_sl (loc_sl) (rem_reg)
205 0 cp --rem -d rem_reg loc_sl (loc_sl) (rem_reg)
206 0 cp --rem -b rem_reg loc_sl (loc_sl loc_sl~ -> rem_reg) (rem_reg)
207 0 cp -b rem_reg loc_sl (loc_sl loc_sl~ -> rem_reg) (rem_reg)
208 0 cp -bd rem_reg loc_sl (loc_sl loc_sl~ -> rem_reg) (rem_reg)
209 1 cp -d rem_reg loc_sl [cp: `rem_reg' and `loc_sl' are the same file ](loc_sl -> rem_reg) (rem_reg)
210
211 0 mv loc_reg rem_sl () (rem_sl)
212 0 mv -b loc_reg rem_sl () (rem_sl rem_sl~ -> dir/loc_reg)
213
214 1 mv rem_sl loc_reg [mv: `rem_sl' and `loc_reg' are the same file ](loc_reg) (rem_sl -> dir/loc_reg)
215 0 mv -b rem_sl loc_reg (loc_reg -> dir/loc_reg loc_reg~) ()
216
217 1 mv loc_sl rem_reg [mv: `loc_sl' and `rem_reg' are the same file ](loc_sl -> rem_reg) (rem_reg)
218 0 mv -b loc_sl rem_reg () (rem_reg -> rem_reg rem_reg~)
219
220 0 mv rem_reg loc_sl (loc_sl) ()
221 0 mv -b rem_reg loc_sl (loc_sl loc_sl~ -> rem_reg) ()
222
223 EOF
224
225 # Some folks may don't have diff.
226 cmp $expected $actual \
227   || { diff -c $expected $actual 1>&2; fail=1; }
228
229 (exit $fail); exit $fail