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 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.

Nevertheless:

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

With clobber, you have #2, but not #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.



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