Skip to content

Commit 1ffad82

Browse files
authored
cp: Enabling cp force flag to run on windows (uutils#9624)
* Enabling cp force flag to run on windows * Windows requires clearing the readonly permissions before deleting
1 parent 47045a2 commit 1ffad82

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

src/uu/cp/src/cp.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -987,8 +987,6 @@ impl Options {
987987
let not_implemented_opts = vec![
988988
#[cfg(not(any(windows, unix)))]
989989
options::ONE_FILE_SYSTEM,
990-
#[cfg(windows)]
991-
options::FORCE,
992990
];
993991

994992
for not_implemented_opt in not_implemented_opts {
@@ -1991,6 +1989,16 @@ fn delete_dest_if_needed_and_allowed(
19911989
}
19921990

19931991
fn delete_path(path: &Path, options: &Options) -> CopyResult<()> {
1992+
// Windows requires clearing readonly attribute before deletion when using --force
1993+
#[cfg(windows)]
1994+
if options.force() {
1995+
if let Ok(mut perms) = fs::metadata(path).map(|m| m.permissions()) {
1996+
#[allow(clippy::permissions_set_readonly_false)]
1997+
perms.set_readonly(false);
1998+
let _ = fs::set_permissions(path, perms);
1999+
}
2000+
}
2001+
19942002
match fs::remove_file(path) {
19952003
Ok(()) => {
19962004
if options.verbose {

tests/by-util/test_cp.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use uucore::selinux::get_getfattr_output;
1111
use uutests::util::TestScenario;
1212
use uutests::{at_and_ucmd, new_ucmd, path_concat, util_name};
1313

14-
#[cfg(not(windows))]
1514
use std::fs::set_permissions;
1615

1716
use std::io::Write;
@@ -972,7 +971,6 @@ fn test_cp_arg_no_clobber_twice() {
972971
}
973972

974973
#[test]
975-
#[cfg(not(windows))]
976974
fn test_cp_arg_force() {
977975
let (at, mut ucmd) = at_and_ucmd!();
978976

0 commit comments

Comments
 (0)