diff options
| author | Chris Denton <chris@chrisdenton.dev> | 2025-01-26 06:15:58 +0000 |
|---|---|---|
| committer | Chris Denton <chris@chrisdenton.dev> | 2025-01-26 06:17:12 +0000 |
| commit | 962ebf0a48436445cf300efc0fa552044f4ca19d (patch) | |
| tree | 33d315db7accf6e2545beb79bd8550b54ab6db98 | |
| parent | 50522fad483ad86291db1871b396f9d54f0de6e7 (diff) | |
| download | rust-962ebf0a48436445cf300efc0fa552044f4ca19d.tar.gz rust-962ebf0a48436445cf300efc0fa552044f4ca19d.zip | |
Windows: Test that deleting a running binary fails
| -rw-r--r-- | library/std/src/sys/pal/windows/fs.rs | 6 | ||||
| -rw-r--r-- | library/std/tests/win_delete_self.rs | 8 |
2 files changed, 12 insertions, 2 deletions
diff --git a/library/std/src/sys/pal/windows/fs.rs b/library/std/src/sys/pal/windows/fs.rs index 750cf7faeae..bdb55643bb1 100644 --- a/library/std/src/sys/pal/windows/fs.rs +++ b/library/std/src/sys/pal/windows/fs.rs @@ -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. diff --git a/library/std/tests/win_delete_self.rs b/library/std/tests/win_delete_self.rs new file mode 100644 index 00000000000..1c3ce4d710c --- /dev/null +++ b/library/std/tests/win_delete_self.rs @@ -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()); +} |
