Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Any way to allow clobbering empty files when noclobber is set?
- X-seq: zsh-workers 45995
- From: Daniel Shahaf <d.s@xxxxxxxxxxxxxxxxxx>
- To: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- Subject: Re: Any way to allow clobbering empty files when noclobber is set?
- Date: Sat, 6 Jun 2020 04:55:31 +0000
- Cc: "zsh-workers@xxxxxxx" <zsh-workers@xxxxxxx>, Martin Tournoij <martin@xxxxxxxxxx>
- In-reply-to: <CAH+w=7ZHapcvCZ=QmV2QEBxi1HGwfKOd4dJ47Ndi+5R-V2VDSg@mail.gmail.com>
- List-help: <mailto:zsh-workers-help@zsh.org>
- List-id: Zsh Workers List <zsh-workers.zsh.org>
- List-post: <mailto:zsh-workers@zsh.org>
- List-unsubscribe: <mailto:zsh-workers-unsubscribe@zsh.org>
- Mailing-list: contact zsh-workers-help@xxxxxxx; run by ezmlm
- References: <CGME20200603020919eucas1p13e26ebcbb335784d14bfb97b137f385a@eucas1p1.samsung.com> <89aed74d-db7b-47ad-b218-8158838049e9@www.fastmail.com> <94e73ebcf39d4d3f9c7ae257b1d75d16@CAMSVWEXC01.scsc.local> <CAH+w=7YpQ3CcsMhAvztGHx19FnALBV349RAz4p8iuB+_8AnsxQ@mail.gmail.com> <CAN=4vMqBCZqD04QO-hw0DwipmCVkdq+QNOEGNaSEfNxyRBqiiw@mail.gmail.com> <20200605020748.635b9bb3@tarpaulin.shahaf.local2> <CAN=4vMoC8aNBz=ZAW-BdA9W6MaBXN3SNBPpZZQobVRwjUg2wGQ@mail.gmail.com> <CAH+w=7ZHapcvCZ=QmV2QEBxi1HGwfKOd4dJ47Ndi+5R-V2VDSg@mail.gmail.com>
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