Directory operations are NOT atomic under NFSv2 and NFSv3
(please refer to the book 'NFS Illustrated' by Brent Callaghan,
ISBN 0-201-32570-5; Brent is a NFS-veteran at Sun).
NFSv2 has two atomic operations:
With NFSv3 the create call is also atomic.
Knowing this, you can implement spin-locks for files and
directories (in shell, not PHP):
lock current dir:
while ! ln -s . lock; do :; done
lock a file:
while ! ln -s ${f} ${f}.lock; do :; done
unlock (assumption, the running process really acquired the lock):
unlock current dir:
mv lock deleteme && rm deleteme
unlock a file:
mv ${f}.lock ${f}.deleteme && rm ${f}.deleteme
Remove is also not atomic, therefore first the rename (which
is atomic) and then the remove.
For the symlink and rename calls, both filenames have to reside on the
same filesystem. My proposal: use only simple filenames and put
file and lock into the same directory.