2 # Running cp S D on an NFS client while another client has just removed D
3 # would lead (w/coreutils-8.16 and earlier) to cp's initial stat call
4 # seeing (via stale NFS cache) that D exists, so that cp would then call
5 # open without the O_CREAT flag. Yet, the open must actually consult
6 # the server, which confesses that D has been deleted, thus causing the
7 # open call to fail with ENOENT.
9 # This test simulates that situation by intercepting stat for a nonexistent
10 # destination, D, and making the stat fill in the result struct for another
13 # This test is skipped on systems that lack LD_PRELOAD support; that's fine.
14 # Similarly, on a system that lacks <dlfcn.h> or __xstat, skipping it is fine.
16 # Copyright (C) 2012-2024 Free Software Foundation, Inc.
18 # This program is free software: you can redistribute it and/or modify
19 # it under the terms of the GNU General Public License as published by
20 # the Free Software Foundation, either version 3 of the License, or
21 # (at your option) any later version.
23 # This program is distributed in the hope that it will be useful,
24 # but WITHOUT ANY WARRANTY; without even the implied warranty of
25 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 # GNU General Public License for more details.
28 # You should have received a copy of the GNU General Public License
29 # along with this program. If not, see <https://www.gnu.org/licenses/>.
31 .
"${srcdir=.}/tests/init.sh"; path_prepend_ .
/src
35 # Replace each stat call with a call to this wrapper.
36 cat > k.c
<<'EOF' || framework_failure_
39 #include <sys/types.h>
42 #define __xstat __xstat_orig
50 __xstat (int ver, const char *path, struct stat *st)
52 static int (*real_stat)(int ver, const char *path, struct stat *st) = NULL;
53 fclose(fopen("preloaded", "w"));
55 real_stat = dlsym (RTLD_NEXT, "__xstat");
56 /* When asked to stat nonexistent "d",
57 return results suggesting it exists. */
58 return real_stat (ver, *path == 'd' && path[1] == 0 ? "d2" : path, st);
62 # Then compile/link it:
63 gcc_shared_ k.c k.so \
64 || framework_failure_
'failed to build shared library'
66 touch d2 || framework_failure_
67 echo xyz
> src || framework_failure_
69 # Finally, run the test:
70 LD_PRELOAD
=$LD_PRELOAD:.
/k.so
cp src d || fail
=1
72 test -f preloaded || skip_
'LD_PRELOAD was ineffective?'
74 compare src d || fail
=1