cp: handle a race condition more sensibly
[coreutils.git] / tests / cp / nfs-removal-race
blob6a435b0c5d379acfca87679b87535bf93988f35d
1 #!/bin/sh
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
11 # file and return 0.
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=.}/init.sh"; path_prepend_ ../src
32 print_ver_ cp
34 # Replace each stat call with a call to this wrapper.
35 cat > k.c <<'EOF' || framework_failure_
36 #define _GNU_SOURCE
37 #include <sys/types.h>
38 #include <dlfcn.h>
40 #define __xstat __xstat_orig
42 #include <sys/stat.h>
43 #include <stddef.h>
45 #undef __xstat
47 int
48 __xstat (int ver, const char *path, struct stat *st)
50 static int (*real_stat)(int ver, const char *path, struct stat *st) = NULL;
51 if (!real_stat)
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);
57 EOF
59 # Then compile/link it:
60 $CC -shared -fPIC -O2 k.c -o k.so \
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
70 Exit $fail