Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Any way to allow clobbering empty files when noclobber is set?

On Sun, Jun 7, 2020 at 4:55 AM Daniel Shahaf <d.s@xxxxxxxxxxxxxxxxxx> wrote:
> In the clobbering open, we use O_CREAT|O_TRUNC, which means: if it
> doesn't exist, create it; if it already exists, truncate it.  If some
> other process calls open() on the same file immediately after our
> open() [or subsequent write()] call returns, there's nothing we can do
> about that.

Also if another process already had the existing file open, whatever
it was doing will be lost.  (Aside:  If it's an NFS file, and the
other process continues writing, this often results in a block of NUL
bytes where the NFS server fills in between the two write positions.)

> As to CLOBBER_EMPTY, the situation is similar.  We can call open() with
> O_CREAT but neither O_EXCL nor O_TRUNC and then fstat() the resulting
> file.  The same race conditions as in the previous paragraphs exist
> here too — for example, someone could open() and write() to the file
> after we fstat() it — but we neither can nor need to do any better
> about races in this case than in the others.

This in fact avoids (or at least does a better job of avoiding) the
problem with two different write positions if some other process has
already opened the file for write.

Messages sorted by: Reverse Date, Date, Thread, Author