Skip to content

Commit 962ebf0

Browse files
committed
Windows: Test that deleting a running binary fails
1 parent 50522fa commit 962ebf0

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

Diff for: library/std/src/sys/pal/windows/fs.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1239,8 +1239,10 @@ pub fn unlink(p: &Path) -> io::Result<()> {
12391239
let mut opts = OpenOptions::new();
12401240
opts.access_mode(c::DELETE);
12411241
opts.custom_flags(c::FILE_FLAG_OPEN_REPARSE_POINT);
1242-
if File::open_native(&p_u16s, &opts).map(|f| f.posix_delete()).is_ok() {
1243-
return Ok(());
1242+
if let Ok(f) = File::open_native(&p_u16s, &opts) {
1243+
if f.posix_delete().is_ok() {
1244+
return Ok(());
1245+
}
12441246
}
12451247
}
12461248
// return the original error if any of the above fails.

Diff for: library/std/tests/win_delete_self.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#![cfg(windows)]
2+
3+
/// Attempting to delete a running binary should return an error on Windows.
4+
#[test]
5+
fn win_delete_self() {
6+
let path = std::env::current_exe().unwrap();
7+
assert!(std::fs::remove_file(path).is_err());
8+
}

0 commit comments

Comments
 (0)