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 11:43 AM Peter Stephenson
<p.w.stephenson@xxxxxxxxxxxx> wrote:
>  At the least we'd need to open the existing file without
> truncating (then either close it or leave it open for writing
> if it was already empty).

If you mean opening the file with O_APPEND, this could result in
unexpected file content. Suppose N processes concurrently write "hello
world" to /foo/bar with the new no_clobber_non_empty option. The final
content of the file can be up to N * strlen("hello world") bytes long
with mangled content. If writing were done with the regular
no_clobber, there would be a guarantee that at any point readers of
the file would either not see the file, or would see content that is a
prefix of "hello world".

I should also note that the existing no_clobber option can be used to
implement file locks.

    emulate zsh -o no_clobber
    if ! print -n >/var/lock; then
     print -ru2 'cannot acquire lock'
     exit 1
    fi
    do_exclusive_stuff
    unlink /var/lock

Regardless of what gets decided w.r.t. no-clobber extensions, I hope
this would keep working.

Roman.



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