1#!/bin/sh
2# make sure rmdir outputs clear errors in the presence of symlinks
3
4# Copyright (C) 2021-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_ rmdir
21
22mkdir dir || framework_failure_
23ln -s dir sl || framework_failure_
24ln -s missing dl || framework_failure_
25touch file || framework_failure_
26ln -s file fl || framework_failure_
27
28# Ensure a we maintain ENOTDIR so that we provide
29# accurate errors on systems on which rmdir(2) does following the symlink/
30returns_ 1 rmdir fl/ 2> err || fail=1
31# Ensure we diagnose symlink behavior.
32printf '%s\n' "rmdir: failed to remove 'fl/': Not a directory" > exp
33compare exp err || fail=1
34
35# Also ensure accurate errors from rmdir -p when traversing symlinks
36# Up to and including 8.32 rmdir would fail like:
37#   rmdir: failed to remove directory 'sl': Not a directory
38mkdir dir/dir2 || framework_failure_
39returns_ 1 rmdir -p sl/dir2 2> err || fail=1
40# Ensure we diagnose symlink behavior.
41printf '%s\n' "rmdir: failed to remove 'sl': Not a directory" > exp
42compare exp err || fail=1
43
44# Only perform the following on systems that don't follow the symlink
45if ! rmdir sl/ 2>/dev/null; then
46  # Up to and including 8.32 rmdir would fail like:
47  #   rmdir: failed to remove 'sl/': Not a directory
48  # That's inconsistent though as rm sl/ gives:
49  #   rm: cannot remove 'sl/': Is a directory
50  # Also this is inconsistent with other systems
51  # which do follow the symlink and rmdir the target.
52
53  new_error="rmdir: failed to remove '%s': Symbolic link not followed\\n"
54
55  # Ensure we diagnose symlink behavior.
56  returns_ 1 rmdir sl/ 2> err || fail=1
57  printf "$new_error" 'sl/' > exp || framework_failure_
58  compare exp err || fail=1
59
60  # Ensure a consistent diagnosis for dangling symlinks etc.
61  returns_ 1 rmdir dl/ 2> err || fail=1
62  printf "$new_error" 'dl/' > exp || framework_failure_
63  compare exp err || fail=1
64fi
65
66Exit $fail
67