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 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 <http://www.gnu.org/licenses/>.
31
. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
34
# Replace each stat call with a call to this wrapper.
35
cat > k.c <<'EOF' || framework_failure_
37
#include <sys/types.h>
40
#define __xstat __xstat_orig
48
__xstat (int ver, const char *path, struct stat *st)
50
static int (*real_stat)(int ver, const char *path, struct stat *st) = NULL;
52
real_stat = dlsym (RTLD_NEXT, "__xstat");
53
/* When asked to stat nonexistent "d",
54
return results suggesting it exists. */
55
return real_stat (ver, *path == 'd' && path[1] == 0 ? "d2" : path, st);
59
# Then compile/link it:
60
$CC -shared -fPIC -O2 k.c -o k.so -ldl \
61
|| framework_failure_ 'failed to compile with -shared -fPIC'
63
touch d2 || framework_failure_
64
echo xyz > src || framework_failure_
66
# Finally, run the test:
67
LD_PRELOAD=./k.so cp src d || fail=1
69
compare src d || fail=1