b0707ff5431bc294f88b0ade3f6803f9ded0af0c
[platform/upstream/coreutils.git] / tests / mv / atomic
1 #!/bin/sh
2 # ensure that mv doesn't first unlink its destination in one particular case
3
4 if test "$VERBOSE" = yes; then
5   set -x
6   mv --version
7 fi
8
9 pwd=`pwd`
10 t0=`echo "$0"|sed 's,.*/,,'`.tmp; tmp=$t0/$$
11 trap 'status=$?; cd $pwd; chmod -R u+rwx $t0; rm -rf $t0 && exit $status' 0
12 trap '(exit $?); exit $?' 1 2 13 15
13
14 framework_failure=0
15 mkdir -p $tmp || framework_failure=1
16 cd $tmp || framework_failure=1
17
18 # Before the fix, mv would unnecessarily unlink the destination symlink:
19 #   $ rm -rf s[12]; ln -s / s1; ln -s /tmp s2; strace -qe unlink /bin/mv -T s1 s2
20 #   unlink("s2") = 0
21 #
22 # With the fix, it doesn't call unlink:
23 #   $ rm -rf s[12]; ln -s / s1; ln -s /tmp s2; strace -qe unlink ./mv -T s1 s2
24 #   $
25
26 ln -s t1 s1 || framework_failure=1
27 ln -s t2 s2 || framework_failure=1
28
29 if test $framework_failure = 1; then
30   echo "$0: failure in testing framework" 1>&2
31   (exit 1); exit 1
32 fi
33
34 # Skip this test on systems without strace.
35 strace -V < /dev/null > ver 2>&1 || skip=1
36 if test "$skip" = 1; then
37   echo "$0: no strace program, so skipping this test" 1>&2
38   (exit 77); exit 77
39 fi
40
41 fail=0
42
43 strace -qe unlink mv -T s1 s2 > out 2>&1 || fail=1
44 cat <<\EOF > exp || fail=1
45 EOF
46
47 # Ensure that the source, s1, is gone.
48 ls -dl s1 > /dev/null 2>&1 && fail=1
49
50 # Ensure that the destination, s2, contains the link from s1.
51 test "`readlink s2`" = t1 || fail=1
52
53 cmp out exp || fail=1
54 test $fail = 1 && diff out exp 2> /dev/null
55
56 (exit $fail); exit $fail