c1e339ac2201d4b6a448693304304dc4098b39b3
[platform/upstream/coreutils.git] / tests / mv / childproof
1 #!/bin/sh
2 # Ensure that cp/mv don't clobber a just-copied file.
3 # With fileutils-4.1 and earlier, this test would fail.
4
5 if test "$VERBOSE" = yes; then
6   set -x
7   cp --version
8   mv --version
9 fi
10
11 . $srcdir/../envvar-check
12 . $srcdir/../lang-default
13 PRIV_CHECK_ARG=require-non-root . $srcdir/../priv-check
14
15 pwd=`pwd`
16 tmp=childproof.$$
17 trap 'status=$?; cd $pwd; rm -rf $tmp && exit $status' 0
18 trap '(exit $?); exit' 1 2 13 15
19
20 framework_failure=0
21 mkdir $tmp || framework_failure=1
22 cd $tmp || framework_failure=1
23 mkdir a b c || framework_failure=1
24 echo a > a/f || framework_failure=1
25 echo b > b/f || framework_failure=1
26
27 if test $framework_failure = 1; then
28   echo 'failure in testing framework' 1>&2
29   (exit 1); exit 1
30 fi
31
32 fail=0
33
34 cp a/f b/f c 2> /dev/null && fail=1
35 test -f a/f || fail=1
36 test -f b/f || fail=1
37 test -f c/f || fail=1
38 test "`cat c/f`" = a || fail=1
39 rm -f c/f
40
41 # With --backup=numbered, it should succeed
42 cp --backup=numbered a/f b/f c || fail=1
43 test -f a/f || fail=1
44 test -f b/f || fail=1
45 test -f c/f || fail=1
46 test -f c/f.~1~ || fail=1
47 rm -f c/f*
48
49 mv a/f b/f c 2> /dev/null && fail=1
50 test -f a/f && fail=1
51 test -f b/f || fail=1
52 test -f c/f || fail=1
53 test "`cat c/f`" = a || fail=1
54
55 # Make sure mv still works when moving hard links.
56 # This is where the same_file test is necessary, and why
57 # we save file names in addition to dev/ino.
58 rm -f c/f* b/f
59 touch a/f
60 ln a/f b/g
61 mv a/f b/g c || fail=1
62 test -f a/f && fail=1
63 test -f b/g && fail=1
64 test -f c/f || fail=1
65 test -f c/g || fail=1
66
67 touch a/f b/f b/g
68 mv a/f b/f b/g c 2> /dev/null && fail=1
69 test -f a/f && fail=1  # a/f should have been moved
70 test -f b/f || fail=1  # b/f should remain
71 test -f b/g && fail=1  # b/g should have been moved
72 test -f c/f || fail=1
73 test -f c/g || fail=1
74
75 (exit $fail); exit $fail