Bug 9106 - cp -i (prompt before overwrite) does not work
Summary: cp -i (prompt before overwrite) does not work
Status: RESOLVED FIXED
Alias: None
Product: Busybox
Classification: Unclassified
Component: Other (show other bugs)
Version: 1.24.x
Hardware: All Linux
: P5 normal
Target Milestone: ---
Assignee: unassigned
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-07-29 12:13 UTC by Fredrik Fornwall
Modified: 2016-08-13 21:29 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Fredrik Fornwall 2016-07-29 12:13:45 UTC
Passing the -i flag (prompt before overwrite) to busybox cp does not seem to work:

$ touch 1 2
$ busybox cp -i 1 2
$

Expected behaviour (as observed with coreutils):
$ touch 1 2
$ cp -i 1 2
cp: overwrite '2'?
Comment 1 Daniel Sahlberg 2016-08-12 20:16:45 UTC
The behaviour seems dependent on ENABLE_FEATURE_NON_POSIX_CP (and therefor the config option CONFIG_FEATURE_NON_POSIX_CP, "Non-POSIX, but safer, copying to special nodes"). If the config option is NOT set, then the result is as described by Fredrik Fornwall. (If the config option IS set, then the result is as expected).

I believe this is because the -i option is only checked in ask_and_unlink(). If !ENABLE_FEATURE_NON_POSIX_CP, then open is done with O_TRUNC which succeeds and therefor ask_and_unlink is never called.

I searched for Posix CP and google returned the following reference: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/cp.html

As I read it, you'd need to FIRST check if a file exists, then (if -i is specified) ask the user and only if the response is affirmative you try to open the file (with O_TRUNC) and if that FAILS, finally try unlink.

Unfortunately I'm not sure I know enough to contribute to a solution. I could give a try if someone more experienced would care to guide me :-). Otherwise I hope the above analysis is helpful!
Comment 2 Denys Vlasenko 2016-08-13 21:24:21 UTC
Fixed in git. Thanks!