summaryrefslogtreecommitdiff
path: root/tests/cp/same-file
blob: 597bfa4d4e28b1fbd3d4793f3542edcb252f5fe3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/bin/sh
# Test some of cp's options and how cp handles situations in
# which a naive implementation might overwrite the source file.

: ${CP=cp}

if test "$VERBOSE" = yes; then
  set -x
  $CP --version
fi

LANGUAGE=C; export LANGUAGE
LANG=C; export LANG

VERSION_CONTROL=numbered; export VERSION_CONTROL

pwd=`pwd`
PATH=$pwd/../../src:$PATH

actual=actual-$$
expected=expected-$$
trap "cd $pwd; rm -rf $actual $expected dir" 0 1 2 3 15

exec 1> $actual

contents=XYZ
for args in 'foo symlink' 'symlink foo' 'foo foo' 'sl1 sl2' 'foo hardlink'; do
  for options in '' -b -d -bd -bdf; do
    rm -rf dir
    mkdir dir
    cd dir
    echo $contents > foo
    case "$args" in *symlink*) ln -s foo symlink ;; esac
    case "$args" in *hardlink*) ln foo hardlink ;; esac
    case "$args" in *sl1*) ln -s foo sl1;; esac
    case "$args" in *sl2*) ln -s foo sl2;; esac
    (
      (
	$CP $options $args 2>.err
	echo $?

	# Normalize the program name in the error output,
	# and put brackets around the output.
	test -s .err && echo "[`sed 's/^[^:][^:]*:/cp:/' .err`]"
	# Strip off all but the file names.
	ls="`ls -lG * \
	    | sed 's/^..............................................//'`"
	echo "($ls)"
	# Make sure the original is unchanged and that
	# the destination is a copy.
	for f in $args; do
	  if test -f $f; then
	    case "`cat $f`" in
	      "$contents") ;;
	      *) echo cp FAILED;;
	    esac
	  else
	    echo symlink-loop
	  fi
	done
      ) | tr '\012' ' '
      echo
    ) | sed 's/  *$//'
    cd ..
  done
  echo
done

cat <<\EOF > $expected
1 [cp: `foo' and `symlink' are the same file] (foo symlink -> foo)
0 (foo symlink symlink.~1~ -> foo)
1 [cp: `foo' and `symlink' are the same file] (foo symlink -> foo)
0 (foo symlink symlink.~1~ -> foo)
0 (foo symlink symlink.~1~ -> foo)

1 [cp: `symlink' and `foo' are the same file] (foo symlink -> foo)
1 [cp: `symlink' and `foo' are the same file] (foo symlink -> foo)
1 [cp: `symlink' and `foo' are the same file] (foo symlink -> foo)
0 (foo -> foo foo.~1~ symlink -> foo) symlink-loop symlink-loop
0 (foo -> foo foo.~1~ symlink -> foo) symlink-loop symlink-loop

1 [cp: `foo' and `foo' are the same file] (foo)
1 [cp: `foo' and `foo' are the same file] (foo)
1 [cp: `foo' and `foo' are the same file] (foo)
1 [cp: `foo' and `foo' are the same file] (foo)
0 (foo foo.~1~)

1 [cp: `sl1' and `sl2' are the same file] (foo sl1 -> foo sl2 -> foo)
0 (foo sl1 -> foo sl2 sl2.~1~ -> foo)
1 [cp: `sl1' and `sl2' are the same file] (foo sl1 -> foo sl2 -> foo)
0 (foo sl1 -> foo sl2 -> foo sl2.~1~ -> foo)
0 (foo sl1 -> foo sl2 -> foo sl2.~1~ -> foo)

1 [cp: `foo' and `hardlink' are the same file] (foo hardlink)
0 (foo hardlink hardlink.~1~)
1 [cp: `foo' and `hardlink' are the same file] (foo hardlink)
0 (foo hardlink hardlink.~1~)
0 (foo hardlink hardlink.~1~)

EOF

cmp $expected $actual || diff -u $expected $actual 1>&2