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