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# Copyright (C) 1998-2023 Free Software Foundation, Inc.
6
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16
17# You should have received a copy of the GNU General Public License
18# along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
20. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
21print_ver_ cp
22
23# Unset CDPATH.  Otherwise, output from the 'cd dir' command
24# can make this test fail.
25(unset CDPATH) >/dev/null 2>&1 && unset CDPATH
26
27VERSION_CONTROL=numbered; export VERSION_CONTROL
28
29# Determine whether a hard link to a symlink points to the symlink
30# itself or to its referent.  For example, the link from FreeBSD6.1
31# does dereference a symlink, but the one from Linux does not.
32ln -s no-such dangling-slink
33ln dangling-slink hard-link > /dev/null 2>&1 \
34  && hard_link_to_symlink_does_the_deref=no \
35  || hard_link_to_symlink_does_the_deref=yes
36rm -f no-such dangling-slink hard-link
37
38test $hard_link_to_symlink_does_the_deref = yes \
39    && remove_these_sed='/^0 -[bf]*l .*sl1 ->/d; /hlsl/d' \
40    || remove_these_sed='/^ELIDE NO TEST OUTPUT/d'
41
42exec 3>&1 1> actual
43
44# FIXME: This should be bigger: like more than 8k
45contents=XYZ
46
47for args in 'foo symlink' 'symlink foo' 'foo foo' 'sl1 sl2' \
48            'foo hardlink' 'hlsl sl2'; do
49  for options in '' -d -f -df --rem -b -bd -bf -bdf \
50                 -l -dl -fl -dfl -bl -bdl -bfl -bdfl -s -sf; do
51    case $args$options in
52      # These tests are not portable.
53      # They all involve making a hard link to a symbolic link.
54      # In the past, we've skipped the tests that are not portable,
55      # by doing "continue" here and eliminating the corresponding
56      # expected output lines below.  Don't do that anymore.
57      'symlink foo'-dfl)
58        continue;;
59      'symlink foo'-bdl)
60        continue;;
61      'symlink foo'-bdfl)
62        continue;;
63      'sl1 sl2'-dfl)
64        continue;;
65      'sl1 sl2'-bd*l)
66        continue;;
67      'sl1 sl2'-dl)
68        continue;;
69    esac
70
71    # cont'd  Instead, skip them only on systems for which link does
72    # dereference a symlink.  Detect and skip such tests here.
73    case $hard_link_to_symlink_does_the_deref:$args:$options in
74      'yes:sl1 sl2:-fl')
75        continue ;;
76      'yes:sl1 sl2:-bl')
77        continue ;;
78      'yes:sl1 sl2:-bfl')
79        continue ;;
80      yes:hlsl*)
81        continue ;;
82    esac
83
84    rm -rf dir
85    mkdir dir
86    cd dir
87    echo $contents > foo
88    case "$args" in *symlink*) ln -s foo symlink ;; esac
89    case "$args" in *hardlink*) ln foo hardlink ;; esac
90    case "$args" in *sl1*) ln -s foo sl1;; esac
91    case "$args" in *sl2*) ln -s foo sl2;; esac
92    case "$args" in *hlsl*) ln sl2 hlsl;; esac
93    (
94      (
95        # echo 1>&2 cp $options $args
96        cp $options $args 2>_err
97        echo $? $options
98
99        # Normalize the program name and diagnostics in the error output,
100        # and put brackets around the output.
101        if test -s _err; then
102          sed '
103            s/symbolic link/symlink/
104            s/^[^:]*:\([^:]*\).*/cp:\1/
105            1s/^/[/
106            $s/$/]/
107          ' _err
108        fi
109        # Strip off all but the file names.
110        ls=$(ls -gG --ignore=_err . \
111             | sed \
112                -e '/^total /d' \
113                -e 's/^[^ ]*  *[^ ]*  *[^ ]*  *[^ ]*  *[^ ]*  *[^ ]*  *//')
114        echo "($ls)"
115        # Make sure the original is unchanged and that
116        # the destination is a copy.
117        for f in $args; do
118          if test -f $f; then
119            case "$(cat $f)" in
120              "$contents") ;;
121              *) echo cp FAILED;;
122            esac
123          else
124            echo symlink-loop
125          fi
126        done
127      ) | tr '\n' ' '
128      echo
129    ) | sed 's/  *$//'
130    cd ..
131  done
132  echo
133done
134
135cat <<\EOF | sed "$remove_these_sed" > expected
1361 [cp: 'foo' and 'symlink' are the same file] (foo symlink -> foo)
1371 -d [cp: 'foo' and 'symlink' are the same file] (foo symlink -> foo)
1381 -f [cp: 'foo' and 'symlink' are the same file] (foo symlink -> foo)
1391 -df [cp: 'foo' and 'symlink' are the same file] (foo symlink -> foo)
1400 --rem (foo symlink)
1410 -b (foo symlink symlink.~1~ -> foo)
1420 -bd (foo symlink symlink.~1~ -> foo)
1430 -bf (foo symlink symlink.~1~ -> foo)
1440 -bdf (foo symlink symlink.~1~ -> foo)
1451 -l [cp: cannot create hard link 'symlink' to 'foo'] (foo symlink -> foo)
1461 -dl [cp: cannot create hard link 'symlink' to 'foo'] (foo symlink -> foo)
1470 -fl (foo symlink)
1480 -dfl (foo symlink)
1490 -bl (foo symlink symlink.~1~ -> foo)
1500 -bdl (foo symlink symlink.~1~ -> foo)
1510 -bfl (foo symlink symlink.~1~ -> foo)
1520 -bdfl (foo symlink symlink.~1~ -> foo)
1531 -s [cp: cannot create symlink 'symlink' to 'foo'] (foo symlink -> foo)
1540 -sf (foo symlink -> foo)
155
1561 [cp: 'symlink' and 'foo' are the same file] (foo symlink -> foo)
1571 -d [cp: 'symlink' and 'foo' are the same file] (foo symlink -> foo)
1581 -f [cp: 'symlink' and 'foo' are the same file] (foo symlink -> foo)
1591 -df [cp: 'symlink' and 'foo' are the same file] (foo symlink -> foo)
1601 --rem [cp: 'symlink' and 'foo' are the same file] (foo symlink -> foo)
1611 -b [cp: 'symlink' and 'foo' are the same file] (foo symlink -> foo)
1620 -bd (foo -> foo foo.~1~ symlink -> foo) symlink-loop symlink-loop
1631 -bf [cp: 'symlink' and 'foo' are the same file] (foo symlink -> foo)
1640 -bdf (foo -> foo foo.~1~ symlink -> foo) symlink-loop symlink-loop
1650 -l (foo symlink -> foo)
1660 -dl (foo symlink -> foo)
1670 -fl (foo symlink -> foo)
1680 -bl (foo symlink -> foo)
1690 -bfl (foo symlink -> foo)
1701 -s [cp: 'symlink' and 'foo' are the same file] (foo symlink -> foo)
1711 -sf [cp: 'symlink' and 'foo' are the same file] (foo symlink -> foo)
172
1731 [cp: 'foo' and 'foo' are the same file] (foo)
1741 -d [cp: 'foo' and 'foo' are the same file] (foo)
1751 -f [cp: 'foo' and 'foo' are the same file] (foo)
1761 -df [cp: 'foo' and 'foo' are the same file] (foo)
1771 --rem [cp: 'foo' and 'foo' are the same file] (foo)
1781 -b [cp: 'foo' and 'foo' are the same file] (foo)
1791 -bd [cp: 'foo' and 'foo' are the same file] (foo)
1800 -bf (foo foo.~1~)
1810 -bdf (foo foo.~1~)
1820 -l (foo)
1830 -dl (foo)
1840 -fl (foo)
1850 -dfl (foo)
1860 -bl (foo)
1870 -bdl (foo)
1880 -bfl (foo foo.~1~)
1890 -bdfl (foo foo.~1~)
1901 -s [cp: 'foo' and 'foo' are the same file] (foo)
1911 -sf [cp: 'foo' and 'foo' are the same file] (foo)
192
1931 [cp: 'sl1' and 'sl2' are the same file] (foo sl1 -> foo sl2 -> foo)
1940 -d (foo sl1 -> foo sl2 -> foo)
1951 -f [cp: 'sl1' and 'sl2' are the same file] (foo sl1 -> foo sl2 -> foo)
1960 -df (foo sl1 -> foo sl2 -> foo)
1970 --rem (foo sl1 -> foo sl2)
1980 -b (foo sl1 -> foo sl2 sl2.~1~ -> foo)
1990 -bd (foo sl1 -> foo sl2 -> foo sl2.~1~ -> foo)
2000 -bf (foo sl1 -> foo sl2 sl2.~1~ -> foo)
2010 -bdf (foo sl1 -> foo sl2 -> foo sl2.~1~ -> foo)
2021 -l [cp: cannot create hard link 'sl2' to 'sl1'] (foo sl1 -> foo sl2 -> foo)
2030 -fl (foo sl1 -> foo sl2)
2040 -bl (foo sl1 -> foo sl2 sl2.~1~ -> foo)
2050 -bfl (foo sl1 -> foo sl2 sl2.~1~ -> foo)
2061 -s [cp: cannot create symlink 'sl2' to 'sl1'] (foo sl1 -> foo sl2 -> foo)
2070 -sf (foo sl1 -> foo sl2 -> sl1)
208
2091 [cp: 'foo' and 'hardlink' are the same file] (foo hardlink)
2101 -d [cp: 'foo' and 'hardlink' are the same file] (foo hardlink)
2111 -f [cp: 'foo' and 'hardlink' are the same file] (foo hardlink)
2121 -df [cp: 'foo' and 'hardlink' are the same file] (foo hardlink)
2130 --rem (foo hardlink)
2140 -b (foo hardlink hardlink.~1~)
2150 -bd (foo hardlink hardlink.~1~)
2160 -bf (foo hardlink hardlink.~1~)
2170 -bdf (foo hardlink hardlink.~1~)
2180 -l (foo hardlink)
2190 -dl (foo hardlink)
2200 -fl (foo hardlink)
2210 -dfl (foo hardlink)
2220 -bl (foo hardlink)
2230 -bdl (foo hardlink)
2240 -bfl (foo hardlink)
2250 -bdfl (foo hardlink)
2261 -s [cp: 'foo' and 'hardlink' are the same file] (foo hardlink)
2271 -sf [cp: 'foo' and 'hardlink' are the same file] (foo hardlink)
228
2291 [cp: 'hlsl' and 'sl2' are the same file] (foo hlsl -> foo sl2 -> foo)
2300 -d (foo hlsl -> foo sl2 -> foo)
2311 -f [cp: 'hlsl' and 'sl2' are the same file] (foo hlsl -> foo sl2 -> foo)
2320 -df (foo hlsl -> foo sl2 -> foo)
2330 --rem (foo hlsl -> foo sl2)
2340 -b (foo hlsl -> foo sl2 sl2.~1~ -> foo)
2350 -bd (foo hlsl -> foo sl2 -> foo sl2.~1~ -> foo)
2360 -bf (foo hlsl -> foo sl2 sl2.~1~ -> foo)
2370 -bdf (foo hlsl -> foo sl2 -> foo sl2.~1~ -> foo)
2381 -l [cp: cannot create hard link 'sl2' to 'hlsl'] (foo hlsl -> foo sl2 -> foo)
2390 -dl (foo hlsl -> foo sl2 -> foo)
2400 -fl (foo hlsl -> foo sl2)
2410 -dfl (foo hlsl -> foo sl2 -> foo)
2420 -bl (foo hlsl -> foo sl2 sl2.~1~ -> foo)
2430 -bdl (foo hlsl -> foo sl2 -> foo)
2440 -bfl (foo hlsl -> foo sl2 sl2.~1~ -> foo)
2450 -bdfl (foo hlsl -> foo sl2 -> foo)
2461 -s [cp: cannot create symlink 'sl2' to 'hlsl'] (foo hlsl -> foo sl2 -> foo)
2470 -sf (foo hlsl -> foo sl2 -> hlsl)
248
249EOF
250
251exec 1>&3 3>&-
252
253compare expected actual 1>&2 || fail=1
254
255Exit $fail
256