zsh-workers
 help / color / mirror / code / Atom feed
From: Daniel Shahaf <d.s@daniel.shahaf.name>
To: Bart Schaefer <schaefer@brasslantern.com>
Cc: "zsh-workers@zsh.org" <zsh-workers@zsh.org>,
	Martin Tournoij <martin@arp242.net>
Subject: Re: Any way to allow clobbering empty files when noclobber is set?
Date: Fri, 5 Jun 2020 03:10:13 +0000	[thread overview]
Message-ID: <20200605031013.16a36d31@tarpaulin.shahaf.local2> (raw)
In-Reply-To: <CAH+w=7bwKvJ7F4JCu7CUuTVxivy8OScCFwN97DKnK9M9QXqVSA@mail.gmail.com>

Bart Schaefer wrote on Wed, 03 Jun 2020 22:00 -0700:
> On Wed, Jun 3, 2020 at 9:06 PM Daniel Shahaf <d.s@daniel.shahaf.name> wrote:>
> >
> > [ Let's call this UNLINK_EMPTY_AFTER_FAILURE for the sake of discussion. ]
> >
> > Bart Schaefer wrote on Thu, 04 Jun 2020 02:43 +00:00:  
> > >
> > > How does that user experience differ from HIST_ALLOW_CLOBBER?  
> >
> > Well, for one, because that proposal only takes effect when foo exited
> > non-zero, created bar, and bar is zero-sized?  
> 
> Fair enough, although (silly example) "false > empty" would remove the
> file?

Depends on whether ./empty was created by the redirection or not.  Files
would only be removed if they were created by the redirection operator:

    % cd "$(mktemp -d)"
    % ls
    % false > empty
    % ls
    % touch empty
    % false > empty
    % ls
    empty
    % 

> Wouldn't you at least want the reason for the unlink to be that the
> redirection failed, rather than that the command exited non-zero?
> 

I was thinking that if the command exited non-zero and didn't write
anything to stdout, then presumably the file was expected to contain
something and is of no use otherwise.  This rule DTRT's for diff(1), for
example, despite that command exiting non-zero even in some non-error cases.

Besides, if the redirection failed — I assume you mean the open(2)
failed — then isn't the unlink likely to fail as well?

> What if the redirection target is a symbolic link?  What if it isn't a
> plain file?

Again, the sequence of events is:

1. bar doesn't exist.
2. Run «foo > bar».  As part of this, we create bar as a zero-length plain file.
3. The exit code is non-zero.
4. We stat() bar to see if it's zero-length.

If in step #4 we find that bar is not even a plain file, we'll infer
that some other process has to have unlinked the plain file we created
in step 2, and we'll leave bar alone.

> > > and silently unlinking a file is not very friendly.  The /etc/nologin
> > > example in your next message comes to mind.  
> >
> > Doesn't this argument also apply to anyone who might set CLOBBER_EMPTY
> > and then lose the timestamps on empty files, as in your next message?  
> 
> Yes, but to a lesser degree, because the file is at least still
> present if it's presence means something.

Okay.  Is there a case where leaving around an empty file is worse than
removing it?  Consider, for example, «curl -o- http://example.com/pre-repvrop-change > hooks/pre-revprop-change»
in a Subversion repository.  The URL is misspelled, so the command will
create an empty pre-revprop-change file, which will allow non-undoable
changes to be made.  Furthermore, if the command had succeeded, only
some such changes would have been allowed; and if the file had not
existed, no such changes would have been allowed.  Given these
semantics, a sysadmin might well prefer to have _no_ pre-revprop-change
file rather than to have an empty one.

Another example: if I do «diff lorem ipsum > bar» and some other process
periodically polls whether bar has been created, the waiting process
might take an empty file to mean there's no difference between lorem and
ipsum.  In this situation, too, it might be better to unlink bar than to
leave it around empty.  (This could be made less contrived by changing
the scenario to someone copying a command from the output of `make -n`
and pasting it at the interactive prompt.  make(1) targets is another
case where empty files and non-existing files behave differently.)

Or perhaps something along the lines of «tar cf - foo > foo.tar; xz
foo.tar» in a single command line.  (Not a good example, though, since
here the user could've used «&&».)

Cheers,

Daniel

P.S.  In the pre-revprop-change example, the sysadmin should have
downloaded the file to a temporary name and then atomically renamed it
into place.  Some syntactic sugar for this pattern this might be nice…

  reply	other threads:[~2020-06-05  3:11 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CGME20200603020919eucas1p13e26ebcbb335784d14bfb97b137f385a@eucas1p1.samsung.com>
2020-06-03  2:08 ` Martin Tournoij
2020-06-03 12:04   ` Peter Stephenson
2020-06-04  1:48     ` Daniel Shahaf
2020-06-04  2:43       ` Bart Schaefer
2020-06-04  4:06         ` Daniel Shahaf
2020-06-04  5:00           ` Bart Schaefer
2020-06-05  3:10             ` Daniel Shahaf [this message]
2020-06-05  3:18               ` Daniel Shahaf
2020-06-06  1:07               ` Bart Schaefer
2020-06-06  4:48                 ` Daniel Shahaf
2020-06-06  7:04                   ` Bart Schaefer
2020-06-04  6:31       ` Martin Tournoij
2020-06-05  2:22         ` Daniel Shahaf
2020-06-04  2:13     ` Vin Shelton
2020-06-04  2:35       ` Bart Schaefer
2020-06-04  2:36       ` Daniel Shahaf
2020-06-04 11:57         ` Vin Shelton
2020-06-04  5:06     ` Bart Schaefer
2020-06-04  5:41       ` Roman Perepelitsa
2020-06-05  2:07         ` Daniel Shahaf
2020-06-05  4:38           ` Roman Perepelitsa
2020-06-06  1:41             ` Bart Schaefer
2020-06-06  4:55               ` Daniel Shahaf
2020-06-06  6:25                 ` Roman Perepelitsa
2020-06-06  7:08                 ` Bart Schaefer
2020-06-06  8:03                   ` Daniel Shahaf
     [not found]           ` <1941572212.466119.1591360860372@mail2.virginmedia.com>
     [not found]             ` <e7f7dfe2-eb4a-457b-85fb-091935a74c0e@www.fastmail.com>
2020-06-06 11:57               ` Peter Stephenson
2020-06-06 12:48                 ` Roman Perepelitsa
2020-06-06 15:24                   ` Bart Schaefer
2020-06-06 16:24                     ` Peter Stephenson
2020-06-07 11:55                       ` Daniel Shahaf
2020-06-07 17:00                         ` Peter Stephenson
2020-06-08  3:27                           ` Daniel Shahaf
2020-06-08  9:30                             ` Peter Stephenson
2020-06-07 17:20                         ` Bart Schaefer
2020-06-06 15:09                 ` Bart Schaefer
2020-06-04  6:47       ` Martin Tournoij
2020-06-04  9:42       ` Peter Stephenson
2020-06-04 12:20         ` Roman Perepelitsa
2020-06-04 12:26           ` Peter Stephenson
2020-06-04 12:15     ` Peter Stephenson
2020-06-04 20:35       ` Bart Schaefer
2020-06-05  1:59         ` Daniel Shahaf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200605031013.16a36d31@tarpaulin.shahaf.local2 \
    --to=d.s@daniel.shahaf.name \
    --cc=martin@arp242.net \
    --cc=schaefer@brasslantern.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).