2 # Test rm's behavior when the directory cannot be read.
3 # This test is skipped on systems that lack LD_PRELOAD support.
5 # Copyright (C) 2016-2024 Free Software Foundation, Inc.
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <https://www.gnu.org/licenses/>.
20 .
"${srcdir=.}/tests/init.sh"; path_prepend_ .
/src
24 mkdir
-p dir
/notempty || framework_failure_
26 # Simulate "readdir" failure.
27 cat > k.c
<<\EOF || framework_failure_
30 /* Setup so we don
't have to worry about readdir64. */
32 # define _FILE_OFFSET_BITS 64
41 struct dirent *readdir (DIR *dirp)
47 fclose (fopen ("32bit", "w"));
52 static struct dirent *(*real_readdir)(DIR *dirp);
53 if (! real_readdir && ! (real_readdir = dlsym (RTLD_NEXT, "readdir")))
55 fprintf (stderr, "Failed to find readdir()\n");
60 if (! (d = real_readdir (dirp)))
62 fprintf (stderr, "Failed to get dirent\n");
67 /* Flag that LD_PRELOAD and above functions work. */
69 fclose (fopen ("preloaded", "w"));
71 /* Return some entries to trigger partial read failure,
72 ensuring we don't
return ignored
'.' or
'..' */
73 char const
*readdir_partial
= getenv
("READDIR_PARTIAL");
74 if (readdir_partial
&& *readdir_partial
&& count
<= 3)
77 d-
>d_name
[0]='0'+count
; d-
>d_name
[1]='\0';
78 #ifdef _DIRENT_HAVE_D_NAMLEN
91 # Then compile/link it:
92 gcc_shared_ k.c k.so \
93 || framework_failure_
'failed to build shared library'
95 # Test if LD_PRELOAD works:
96 export READDIR_PARTIAL
97 for READDIR_PARTIAL
in '' '1'; do
99 (export LD_PRELOAD
=$LD_PRELOAD:.
/k.so
100 returns_
1 rm -Rf dir
2>>errt
) || fail
=1
101 if test -f 32bit
; then
102 skip_
'This test only supports 64 bit systems'
103 elif ! test -f preloaded
; then
105 skip_
"internal test failure: maybe LD_PRELOAD doesn't work?"
109 # First case is failure to read any items from dir, then assume empty.
110 # Generally that will be diagnosed when rm tries to rmdir().
111 # Second case is more general error where we fail immediately
112 # (with ENOENT in this case but it could be anything).
114 rm: cannot remove 'dir'
116 rm: traversal failed: dir
118 sed 's/\(rm:.*\):.*/\1/' errt
> err || framework_failure_
119 compare exp err || fail
=1