1#!/bin/sh
2# Ensure that cp merely warns when a non-directory source file is
3# listed on the command line more than once.  fileutils-4.1.1
4# made this fail:  cp a a d/
5# Ensure that mv fails with a similar command.
6
7# Copyright (C) 2001-2023 Free Software Foundation, Inc.
8
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18
19# You should have received a copy of the GNU General Public License
20# along with this program.  If not, see <https://www.gnu.org/licenses/>.
21
22. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
23print_ver_ cp mv
24
25skip_if_root_
26
27reset_files() { rm -fr a b d; touch a; mkdir b d; }
28
29for i in cp; do
30
31  # cp may not fail in this case.
32  reset_files
33  $i a a d/ 2> out || fail=1
34  reset_files
35  $i ./a a d/ 2>> out || fail=1
36
37  # Similarly for directories, but handle
38  # source == dest appropriately.
39  reset_files
40  $i -a ./b b d/ 2>> out || fail=1
41  reset_files
42  returns_ 1 $i -a ./b b b/ 2>> out || fail=1
43
44  # cp succeeds with --backup=numbered.
45  reset_files
46  $i --backup=numbered a a d/ 2>> out || fail=1
47
48  # But not with plain '--backup'
49  reset_files
50  returns_ 1 $i --backup a a d/ 2>> out || fail=1
51
52  cat <<EOF > exp
53$i: warning: source file 'a' specified more than once
54$i: warning: source file 'a' specified more than once
55$i: warning: source directory 'b' specified more than once
56$i: cannot copy a directory, './b', into itself, 'b/b'
57$i: warning: source directory 'b' specified more than once
58$i: will not overwrite just-created 'd/a' with 'a'
59EOF
60  compare exp out || fail=1
61done
62
63for i in mv; do
64  # But mv *does* fail in this case (it has to).
65  reset_files
66  returns_ 1 $i a a d/ 2> out || fail=1
67  returns_ 1 test -e a || fail=1
68  reset_files
69  returns_ 1 $i ./a a d/ 2>> out || fail=1
70  returns_ 1 test -e a || fail=1
71
72  # Similarly for directories, also handling
73  # source == dest appropriately.
74  reset_files
75  returns_ 1 $i ./b b d/ 2>> out || fail=1
76  returns_ 1 test -e b || fail=1
77  reset_files
78  returns_ 1 $i --verbose ./b b b/ 2>> out || fail=1
79  test -d b || fail=1
80
81  cat <<EOF > exp
82$i: cannot stat 'a': No such file or directory
83$i: cannot stat 'a': No such file or directory
84$i: cannot stat 'b': No such file or directory
85$i: cannot move './b' to a subdirectory of itself, 'b/b'
86$i: warning: source directory 'b' specified more than once
87EOF
88  compare exp out || fail=1
89done
90
91Exit $fail
92