coreutils icon indicating copy to clipboard operation
coreutils copied to clipboard

`coreutils rm -f` fails with read-only files on Windows

Open salsifis opened this issue 1 year ago • 1 comments

On Windows, the call to fs::remove_file fails if the file is read-only.

I think, when using the -f switch, the following snippet (adapted from the doc of the fs library) should be used

let mut perms = fs::metadata(path)?.permissions();
perms.set_readonly(false);
fs::set_permissions(path, perms)?;

salsifis avatar Jan 10 '24 07:01 salsifis

That is standard Windows behaviour. The problem with the proposed workaround is that it's not atomic and it's also permanent (e.g. if deleting should fail for some other reason).

Modern versions of Windows have FILE_DISPOSITION_FLAG_IGNORE_READONLY_ATTRIBUTE to handle this situation but Rust's std doesn't currently have a public function that does this (it does however have an internal only function).

ChrisDenton avatar Jan 10 '24 22:01 ChrisDenton