GNU bug report logs - #61105
cp/mv: want a fatal --no-clobber

Previous Next

Package: coreutils;

Reported by: Mike Frysinger <vapier <at> gentoo.org>

Date: Fri, 27 Jan 2023 20:53:02 UTC

Severity: normal

Done: Paul Eggert <eggert <at> cs.ucla.edu>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 61105 in the body.
You can then email your comments to 61105 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-coreutils <at> gnu.org:
bug#61105; Package coreutils. (Fri, 27 Jan 2023 20:53:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Mike Frysinger <vapier <at> gentoo.org>:
New bug report received and forwarded. Copy sent to bug-coreutils <at> gnu.org. (Fri, 27 Jan 2023 20:53:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Mike Frysinger <vapier <at> gentoo.org>
To: bug-coreutils <at> gnu.org
Subject: cp/mv: want a fatal --no-clobber
Date: Fri, 27 Jan 2023 15:52:09 -0500
[Message part 1 (text/plain, inline)]
i've been under the mistaken assumption that the -n/--no-clobber option exits
non-zero when the target exists, but someone pointed out to me recently that
it silently ignores existing files.  can we get a setting to control this ?

basically i've been writing things like:
if ! cp -n foo bar; then
  ... error out because bar already exists, or otherwise failed ...
fi

when really i need to write:
if [ -e bar ] || ! cp foo bar; then
  ... error out ...
fi
-mike
[signature.asc (application/pgp-signature, inline)]

Information forwarded to bug-coreutils <at> gnu.org:
bug#61105; Package coreutils. (Sat, 28 Jan 2023 00:00:02 GMT) Full text and rfc822 format available.

Message #8 received at 61105 <at> debbugs.gnu.org (full text, mbox):

From: Pádraig Brady <P <at> draigBrady.com>
To: Mike Frysinger <vapier <at> gentoo.org>, 61105 <at> debbugs.gnu.org
Subject: Re: bug#61105: cp/mv: want a fatal --no-clobber
Date: Fri, 27 Jan 2023 23:59:47 +0000
On 27/01/2023 20:52, Mike Frysinger wrote:
> i've been under the mistaken assumption that the -n/--no-clobber option exits
> non-zero when the target exists, but someone pointed out to me recently that
> it silently ignores existing files.  can we get a setting to control this ?

Yes --no-clobber={skip (default), fail} would have some value.
Especially for handling multiple files.

> 
> basically i've been writing things like:
> if ! cp -n foo bar; then
>    ... error out because bar already exists, or otherwise failed ...
> fi
> 
> when really i need to write:
> if [ -e bar ] || ! cp foo bar; then
>    ... error out ...
> fi

The above is racy if the file is created between the test and the cp.

I suppose you could leverage the shell to create the file atomically like:

cp-n-fail() {
  (set -C && cat < "$1" > "$2")
}

cp-a-n-fail() {
  cp-n-fail "$1" "$2" &&
  cp -a --attributes-only "$1" "$2"
}

But yes hack, only handles single files, doesn't handle sparseness, ...

cheers,
Pádraig




Information forwarded to bug-coreutils <at> gnu.org:
bug#61105; Package coreutils. (Sat, 28 Jan 2023 00:24:01 GMT) Full text and rfc822 format available.

Message #11 received at 61105 <at> debbugs.gnu.org (full text, mbox):

From: Mike Frysinger <vapier <at> gentoo.org>
To: Pádraig Brady <P <at> draigBrady.com>
Cc: 61105 <at> debbugs.gnu.org
Subject: Re: bug#61105: cp/mv: want a fatal --no-clobber
Date: Fri, 27 Jan 2023 19:22:58 -0500
[Message part 1 (text/plain, inline)]
On 27 Jan 2023 23:59, Pádraig Brady wrote:
> On 27/01/2023 20:52, Mike Frysinger wrote:
> > basically i've been writing things like:
> > if ! cp -n foo bar; then
> >    ... error out because bar already exists, or otherwise failed ...
> > fi
> > 
> > when really i need to write:
> > if [ -e bar ] || ! cp foo bar; then
> >    ... error out ...
> > fi
> 
> The above is racy if the file is created between the test and the cp.

yes, that's why i want to only use cp :)

although if i was super concerned about TOCTOU races, i wouldn't be using
shell in the first place
-mike
[signature.asc (application/pgp-signature, inline)]

Information forwarded to bug-coreutils <at> gnu.org:
bug#61105; Package coreutils. (Mon, 30 Jan 2023 20:13:02 GMT) Full text and rfc822 format available.

Message #14 received at 61105 <at> debbugs.gnu.org (full text, mbox):

From: Paul Eggert <eggert <at> cs.ucla.edu>
To: Mike Frysinger <vapier <at> gentoo.org>, 61105 <at> debbugs.gnu.org
Subject: Re: bug#61105: cp/mv: want a fatal --no-clobber
Date: Mon, 30 Jan 2023 12:12:28 -0800
Better, I think, would be to change cp -n to be compatible with FreeBSD, 
where 'cp -n A B' exits with status 1 if B exists. This matches what 
FreeBSD 'cp -i A B' does when you say "no". This would be more useful 
than what GNU cp does, and the FreeBSD cp -i behavior conforms to POSIX 
whereas GNU cp -i (I think accidentally) does not. This would be simpler 
than adding a fatal --no-clobber, and would encourage portability a bit.

The reason it's a POSIX conformance issue is that POSIX says exit status 
zero means "All files were copied successfully." which "cp -n A B" does 
not do when B exists.

Similarly for 'mv' (though FreeBSD mv -i does not conform to POSIX here, 
unfortunately).




Information forwarded to bug-coreutils <at> gnu.org:
bug#61105; Package coreutils. (Mon, 30 Jan 2023 21:36:02 GMT) Full text and rfc822 format available.

Message #17 received at 61105 <at> debbugs.gnu.org (full text, mbox):

From: Pádraig Brady <P <at> draigBrady.com>
To: Paul Eggert <eggert <at> cs.ucla.edu>, Mike Frysinger <vapier <at> gentoo.org>,
 61105 <at> debbugs.gnu.org
Subject: Re: bug#61105: cp/mv: want a fatal --no-clobber
Date: Mon, 30 Jan 2023 21:35:19 +0000
On 30/01/2023 20:12, Paul Eggert wrote:
> Better, I think, would be to change cp -n to be compatible with FreeBSD,
> where 'cp -n A B' exits with status 1 if B exists. This matches what
> FreeBSD 'cp -i A B' does when you say "no". This would be more useful
> than what GNU cp does, and the FreeBSD cp -i behavior conforms to POSIX
> whereas GNU cp -i (I think accidentally) does not. This would be simpler
> than adding a fatal --no-clobber, and would encourage portability a bit.
> 
> The reason it's a POSIX conformance issue is that POSIX says exit status
> zero means "All files were copied successfully." which "cp -n A B" does
> not do when B exists.
> 
> Similarly for 'mv' (though FreeBSD mv -i does not conform to POSIX here,
> unfortunately).

This is a good observation.
Also the current "skip" functionality of coreutils cp -n
is already catered for with the --update option.

cheers,
Pádraig





Reply sent to Paul Eggert <eggert <at> cs.ucla.edu>:
You have taken responsibility. (Tue, 31 Jan 2023 16:57:01 GMT) Full text and rfc822 format available.

Notification sent to Mike Frysinger <vapier <at> gentoo.org>:
bug acknowledged by developer. (Tue, 31 Jan 2023 16:57:01 GMT) Full text and rfc822 format available.

Message #22 received at 61105-done <at> debbugs.gnu.org (full text, mbox):

From: Paul Eggert <eggert <at> cs.ucla.edu>
To: Pádraig Brady <P <at> draigBrady.com>,
 Mike Frysinger <vapier <at> gentoo.org>
Cc: 61105-done <at> debbugs.gnu.org
Subject: Re: bug#61105: cp/mv: want a fatal --no-clobber
Date: Tue, 31 Jan 2023 08:55:48 -0800
[Message part 1 (text/plain, inline)]
On 2023-01-30 13:35, Pádraig Brady wrote:
> This is a good observation.
> Also the current "skip" functionality of coreutils cp -n
> is already catered for with the --update option.

For consistency, there too the exit status should reflect whether the cp 
action was done.

I installed the attached patch to implement this; comments welcome. In 
the meantime I'm boldly closing the bug report.
[0001-cp-ln-mv-when-skipping-exit-with-nonzero-status.patch (text/x-patch, attachment)]

Information forwarded to bug-coreutils <at> gnu.org:
bug#61105; Package coreutils. (Tue, 31 Jan 2023 17:12:01 GMT) Full text and rfc822 format available.

Message #25 received at 61105 <at> debbugs.gnu.org (full text, mbox):

From: Pádraig Brady <P <at> draigBrady.com>
To: Paul Eggert <eggert <at> cs.ucla.edu>, Mike Frysinger <vapier <at> gentoo.org>
Cc: 61105 <at> debbugs.gnu.org
Subject: Re: bug#61105: cp/mv: want a fatal --no-clobber
Date: Tue, 31 Jan 2023 17:11:00 +0000
On 31/01/2023 16:55, Paul Eggert wrote:
> On 2023-01-30 13:35, Pádraig Brady wrote:
>> This is a good observation.
>> Also the current "skip" functionality of coreutils cp -n
>> is already catered for with the --update option.
> 
> For consistency, there too the exit status should reflect whether the cp
> action was done.
> 
> I installed the attached patch to implement this; comments welcome. In
> the meantime I'm boldly closing the bug report.

I'm not sure I agree with making this change for --update.
Note POSIX doesn't specify -u (or -n TBH), so it's a
bit of a stretch to take its specification of
"All files were copied successfully" in this case,
when it wasn't considering these cases.

To me --update is less useful if it fails in this case.

thanks,
Pádraig




Information forwarded to bug-coreutils <at> gnu.org:
bug#61105; Package coreutils. (Tue, 31 Jan 2023 17:29:01 GMT) Full text and rfc822 format available.

Message #28 received at 61105 <at> debbugs.gnu.org (full text, mbox):

From: Paul Eggert <eggert <at> cs.ucla.edu>
To: Pádraig Brady <P <at> draigBrady.com>,
 Mike Frysinger <vapier <at> gentoo.org>
Cc: 61105 <at> debbugs.gnu.org
Subject: Re: bug#61105: cp/mv: want a fatal --no-clobber
Date: Tue, 31 Jan 2023 09:27:55 -0800
[Message part 1 (text/plain, inline)]
On 2023-01-31 09:11, Pádraig Brady wrote:
> To me --update is less useful if it fails in this case.

Fair enough, I installed the attached to revert the --update part of the 
change, so that --update will work as before.
[0001-cp-mv-skipping-due-to-u-is-success-not-failure.patch (text/x-patch, attachment)]

bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Wed, 01 Mar 2023 12:24:04 GMT) Full text and rfc822 format available.

This bug report was last modified 1 year and 51 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.