1#!/bin/sh
2# Make sure cp -pR --parents isn't too generous with parent permissions.
3
4# Copyright (C) 2006-2023 Free Software Foundation, Inc.
5
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15
16# You should have received a copy of the GNU General Public License
17# along with this program.  If not, see <https://www.gnu.org/licenses/>.
18
19. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
20print_ver_ cp
21
22# cp -p gives ENOTSUP on NFS on Linux 2.6.9 at least
23require_local_dir_
24
25umask 002
26mkdir mode ownership d || framework_failure_
27chmod g+s d 2>/dev/null # The cp test is valid either way.
28
29# Terminate any background cp process.
30pid=
31cleanup_() { kill $pid 2>/dev/null && wait $pid; }
32
33for attr in mode ownership
34do
35  mkfifo_or_skip_ $attr/fifo
36
37  # Copy a fifo's contents.  That way, we can examine d/$attr's
38  # state while cp is running.
39  timeout 10 cp --preserve=$attr -R --copy-contents --parents $attr d & pid=$!
40
41  # Check the permissions of the destination directory that 'cp' has made.
42  # 'ls' won't start until after 'cp' has made the destination directory
43  # $d/attr and has started to read the source file $attr/fifo.
44  timeout 10 sh -c "ls -ld d/$attr >$attr/fifo" || fail=1
45
46  wait $pid || fail=1
47
48  ls_output=$(cat d/$attr/fifo) || fail=1
49  case $attr,$ls_output in
50  ownership,d???--[-S]--[-S]* | \
51  mode,d????-??-?* | \
52  mode,d??[-x]?w[-x]?-[-x]* )
53    ;;
54  *)
55    fail=1;;
56  esac
57done
58
59Exit $fail
60