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

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



Bart Schaefer wrote on Fri, 05 Jun 2020 18:41 -0700:
> On Thu, Jun 4, 2020 at 9:39 PM Roman Perepelitsa
> <roman.perepelitsa@xxxxxxxxx> wrote:
> >
> >     write() print -rn -- $1
> >     rm -f foo
> >     write hello >foo &
> >     write bye >foo &
> >     wait
> >
> > With regular no_clobber it has the following guarantees:
> >
> > 1. `write` is executed exactly once
> > 2. once `wait` completes, `foo` contains either "hello" or "bye"  
> 
> I don't think that's the intended typical usage of noclobber.  It's
> not set by default, and it can't have any effect outside the local
> shell.  It's meant to keep you from making silly mistakes when
> interacting with the interpreter, not as a concurrent programming
> tool.

NO_CLOBBER causes open() to be called with the O_EXCL bit, which does
affect other processes as well.

> Nevertheless:
> 
> > Is there a way to provide these guarantees with clobber_empty?  
> 

I don't understand the question, really.  If you can get these
semantics with NO_CLOBBER, why does it matter whether or not
CLOBBER_EMPTY can provide them or not?  You can always leave it off (or
locally unset it).

Or use mkdir without the -p flag as a poor man's mutex.

> With clobber, you have #2, but not #1,

Doesn't O_EXCL achieve #1?

> so I think we should focus on
> whether clobber_empty can guarantee #2.  If you want #1, there are
> other ways (but I suspect it arises from the implementation of #2 even
> so).
> 
> I think the answer is that #2 can be guaranteed if and only if
> fcntl()-based locking can be applied, or some equivalent kernel
> mechanism such that no more than closing of the (last dup of the)
> descriptor is necessary to release the lock.  The steps would have to
> be:
> 
> 1. Open the file for write without truncate.
> 2. Attempt lock, non-blocking, and immediately close/fail if not locked.
> 3. Check for zero size, and immediately unlock/close/fail when nonzero.
> 4. Return the locked descriptor.

Cheers,

Daniel



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