in reply to How to move/copy a file without overwriting an existing file

"O_EXCL isn't reliable on NFS" is an overstatement so severe it seems to say something different than the actual behavior. It should be "O_EXCL isn't 100% reliable on NFS if two different systems are manipulating the same file at virtually the same time". If your program is only running on one system, or if the file has already existed for a couple minutes, then O_EXCL _is_ 100% reliable.
  • Comment on Re: How to move/copy a file without overwriting an existing file

Replies are listed 'Best First'.
Re^2: How to move/copy a file without overwriting an existing file
by afoken (Chancellor) on Jan 30, 2017 at 17:37 UTC
    It should be "O_EXCL isn't 100% reliable on NFS if two different systems are manipulating the same file at virtually the same time". If your program is only running on one system, or if the file has already existed for a couple minutes, then O_EXCL _is_ 100% reliable.

    So, according to your definition, coitus interuptus is a 100% reliable method of birth control as long as you avoid some edge cases, like a man having sex with a woman.

    Regarding pregnancy, there are exactly two states: Pregnant or not. You can't be 42% pregnant. And like with pregnancy, O_EXCL on NFS (or any other networked filesystem) is either reliable and prevents access to a file by more than one process on one machine every time and under all conditions, or it is unreliable.

    For Linux, the man page for open explains the situation quite well:

    • In general, the behavior of O_EXCL is undefined if it is used without O_CREAT.
    • On NFS, O_EXCL is only supported when using NFSv3 or later on kernel 2.6 or later.
    • In NFS environments where O_EXCL support is not provided, programs that rely on it for performing locking tasks will contain a race condition.

    So, for Linux, O_EXCL is unreliable if you are running a kernel before 2.6 or use it on a non-v3 NFS. That does not mean that it is reliable with 2.6+ and NFSv3. The FreeBSD people seem to have had similar problems, especially if you mix in Solaris.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)