Hello This test verifies the behavior of cp with readonly directories.
it wasn't covered before Cheers S
From 8a1a42c3d9813384b30a141022e07f51d0c0c601 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru <[email protected]> Date: Sat, 7 Feb 2026 16:20:46 +0100 Subject: [PATCH] test: cp behavior with readonly directories * tests/cp/readonly-dir.sh: add new tests Identified here https://github.com/uutils/coreutils/issues/7961 --- tests/cp/readonly-dir.sh | 62 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100755 tests/cp/readonly-dir.sh diff --git a/tests/cp/readonly-dir.sh b/tests/cp/readonly-dir.sh new file mode 100755 index 000000000..ddb1be2a1 --- /dev/null +++ b/tests/cp/readonly-dir.sh @@ -0,0 +1,62 @@ +#!/bin/sh +# Test cp behavior with readonly directories + +# Copyright (C) 2026 Free Software Foundation, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src +print_ver_ cp + +# Test case for readonly directory permission preservation +# This addresses the specific case where 'cp -r' and 'cp -a' should +# preserve readonly directory permissions (555) instead of changing +# them to writable permissions (755). + +# Create test directory structure +mkdir -p a/b/c/d || framework_failure_ +touch a/b/c/d/bar.txt || framework_failure_ +echo "test content" > a/b/c/d/bar.txt || framework_failure_ + +# Make directories readonly (remove write permissions) +chmod -R -w a || framework_failure_ + +# Test 1: cp -r should preserve readonly directory permissions +cp -r a b || fail=1 + +# Check that the root copied directory has readonly permissions +mode_b=$(stat --format=%a b) || fail=1 +test "$mode_b" = "555" || fail=1 + +# Check subdirectories too +mode_bb=$(stat --format=%a b/b) || fail=1 +test "$mode_bb" = "555" || fail=1 + +mode_bbc=$(stat --format=%a b/b/c) || fail=1 +test "$mode_bbc" = "555" || fail=1 + +mode_bbcd=$(stat --format=%a b/b/c/d) || fail=1 +test "$mode_bbcd" = "555" || fail=1 + +# Test 2: cp -a should preserve readonly directory permissions and not fail +cp -a a c || fail=1 + +# Check that cp -a also preserved readonly permissions +mode_c=$(stat --format=%a c) || fail=1 +test "$mode_c" = "555" || fail=1 + +mode_cb=$(stat --format=%a c/b) || fail=1 +test "$mode_cb" = "555" || fail=1 + +Exit $fail -- 2.47.3
