fc8a6908a47f2753ade086f9bf1424bb22005efc
[platform/upstream/coreutils.git] / tests / mv / hard-3
1 #!/bin/sh
2 # Ensure that using `cp --preserve=link' to copy hard-linked arguments
3 # onto existing destinations works, even when one of the link operations fails.
4 # This bug was fixed in coreutils-4.5.9.
5 # To exercise this bug is non-trivial:
6 # Set-up requires at least three hard-linked files.  In copying them,
7 # while preserving links, the initial copy must succeed, the attempt
8 # to create the second file via `link' must fail, and the final `link'
9 # (to create the third) must succeed.  Before the corresponding fix,
10 # the first and third destination files would not be linked.
11 #
12 # Note that this is nominally a test of `cp', yet it is in the tests/mv
13 # directory, because it requires use of the --preserve=link option that
14 # mv enables by default.
15
16 if test "$VERBOSE" = yes; then
17   set -x
18   cp --version
19 fi
20
21 . $srcdir/../envvar-check
22 PRIV_CHECK_ARG=require-non-root . $srcdir/../priv-check
23
24 pwd=`pwd`
25 t0=`echo "$0"|sed 's,.*/,,'`.tmp; tmp=$t0/$$
26 trap 'status=$?; cd $pwd; chmod -R u+rwx $t0; rm -rf $t0 && exit $status' 0
27 trap '(exit $?); exit $?' 1 2 13 15
28
29 framework_failure=0
30 mkdir -p $tmp || framework_failure=1
31 cd $tmp || framework_failure=1
32 mkdir -p x dst/x || framework_failure=1
33 touch dst/x/b || framework_failure=1
34 chmod a-w dst/x
35 touch a || framework_failure=1
36 ln a x/b || framework_failure=1
37 ln a c || framework_failure=1
38
39 if test $framework_failure = 1; then
40   echo "$0: failure in testing framework" 1>&2
41   (exit 1); exit 1
42 fi
43
44 fail=0
45
46 # ======================================
47 # This must fail -- because x/b cannot be unlinked.
48 cp --preserve=link --parents a x/b c dst 2> /dev/null && fail=1
49
50 # Source files must remain.
51 test -f a || fail=1
52 test -f x/b || fail=1
53 test -f c || fail=1
54 cd dst
55
56 # Three destination files must exist.
57 test -f a || fail=1
58 test -f x/b || fail=1
59 test -f c || fail=1
60
61 # The i-node numbers of a and c must be the same.
62 ia=`ls -i a|sed 's/ a$//'`
63 ic=`ls -i c|sed 's/ c$//'`
64 test $ia = $ic || fail=1
65
66 # The i-node number of x/b must be different.
67 ib=`ls -i x/b|sed 's/ x.b$//'`
68 test $ia = $ib && fail=1
69
70 (exit $fail); exit $fail