Skip to content

Commit

Permalink
Windows: Test that deleting a running binary fails
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisDenton committed Jan 26, 2025
1 parent 50522fa commit 962ebf0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
6 changes: 4 additions & 2 deletions library/std/src/sys/pal/windows/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1239,8 +1239,10 @@ pub fn unlink(p: &Path) -> io::Result<()> {
let mut opts = OpenOptions::new();
opts.access_mode(c::DELETE);
opts.custom_flags(c::FILE_FLAG_OPEN_REPARSE_POINT);
if File::open_native(&p_u16s, &opts).map(|f| f.posix_delete()).is_ok() {
return Ok(());
if let Ok(f) = File::open_native(&p_u16s, &opts) {
if f.posix_delete().is_ok() {
return Ok(());
}
}
}
// return the original error if any of the above fails.
Expand Down
8 changes: 8 additions & 0 deletions library/std/tests/win_delete_self.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![cfg(windows)]

/// Attempting to delete a running binary should return an error on Windows.
#[test]
fn win_delete_self() {
let path = std::env::current_exe().unwrap();
assert!(std::fs::remove_file(path).is_err());
}

0 comments on commit 962ebf0

Please sign in to comment.