]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
tests: cp: ensure read-only dir perms preserved
authorSylvestre Ledru <sylvestre@debian.org>
Sat, 7 Feb 2026 15:20:46 +0000 (16:20 +0100)
committerPádraig Brady <P@draigBrady.com>
Sat, 7 Feb 2026 21:54:24 +0000 (21:54 +0000)
* tests/cp/readonly-dir.sh: Add new test.
* tests/local.mk: Reference new test.
Identified here https://github.com/uutils/coreutils/issues/7961

tests/cp/readonly-dir.sh [new file with mode: 0755]
tests/local.mk

diff --git a/tests/cp/readonly-dir.sh b/tests/cp/readonly-dir.sh
new file mode 100755 (executable)
index 0000000..a9b5379
--- /dev/null
@@ -0,0 +1,65 @@
+#!/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
+skip_if_setgid_
+
+umask 022
+
+# 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
index 60e651b947c9b738ebc5603f72f1d3cc1c681401..c7435d9af214f64a994d2c5dac52eb4bff06bad8 100644 (file)
@@ -571,6 +571,7 @@ all_tests =                                 \
   tests/cp/proc-short-read.sh                  \
   tests/cp/proc-zero-len.sh                    \
   tests/cp/r-vs-symlink.sh                     \
+  tests/cp/readonly-dir.sh                     \
   tests/cp/reflink-auto.sh                     \
   tests/cp/reflink-perm.sh                     \
   tests/cp/same-file.sh                                \