I have a part of a bash script, which I am executing as root:
rm -f ../../include/profile.h; \
cp profile.h ../../include/profile.h
the file ../../include/profile.h in question has permissions:
-rw-r--r-- 1 root root 12178 Nov 5 02:00 ../../include/profile.h
Most of the time, the snippet executes fine, but very rarely, impossible to reproduce, it fails:
cp: cannot create regular file `../../include/profile.h': File exists
I checked, there is no other part of the program, that would be executed in parallel, which could write the file in between the two statements, thus creating a race condition which would explain the behaviour. There is no other place than the above, which is executed only once, which would write this file.
The system is
kernel:
Linux dev64 2.6.32.63+drm33.26-64.128-a10 #6 SMP Fri Jul 25 15:21:56 PDT 2014 x86_64 x86_64 x86_64 GNU/Linux
distro:
CentOS release 6.3 (Final)
What could possibly cause this failure to appear??
rmbefore thecpis executed. Perhaps callingsyncbetween the two commands will cure it. – AFH Dec 13 '15 at 03:44syncis necessary for the script to be correct? In other words, issyncalways necessary to call between two shell commands that depends on each others effect on the filesystem? I want to know is this my error, or is this a bug in Linux kernel and/or implementation ofcp, which can be mitigated with the use ofsync. – user322908 Dec 13 '15 at 04:00syncwill provide some evidence of the latter. If so, the cache in question may be in the disc driver or in the disc controller itself (I experienced this some years ago and the disc drives had to be replaced by the manufacturer). Unfortunately, I run Ubuntu, which is Debian-based, whereas CentOS is RedHat-based, so I cannot run any meaningful tests. I have given you a possible cause, as you asked: I'm not sure I can do more. – AFH Dec 13 '15 at 13:56cp foo barwill overwritebarif it exists. The errorcp: cannot create regular file … File existsindicates something deeper than only the old file not having been removed yet. Is FUSE involved? I believe it can introduce race conditions in some circumstances (see this answer). Or maybe it's a remote filesystem and the root of the problem is on the server. I notice the question is old, the setup in question may no longer exist. – Kamil Maciorowski Mar 09 '21 at 08:44