Skip to content

Commit 02ee164

Browse files
ChrisDentongitbot
authored and
gitbot
committed
Windows: Test that deleting a running binary fails
1 parent 49d1b4d commit 02ee164

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

std/src/sys/pal/windows/fs.rs

Lines changed: 4 additions & 2 deletions
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.

std/tests/win_delete_self.rs

Lines changed: 8 additions & 0 deletions
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)