Skip to content

Commit 8aed312

Browse files
committed
Fix anonymous files on Windows.
1 parent 0491ea0 commit 8aed312

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

cap-tempfile/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ rustix = { version = "1.0.0" }
2626
[target.'cfg(any(target_os = "android", target_os = "linux"))'.dependencies]
2727
rustix-linux-procfs = "0.1.1"
2828

29-
[target.'cfg(windows)'.dev-dependencies.windows-sys]
29+
[target.'cfg(windows)'.dependencies.windows-sys]
3030
version = ">=0.60, <0.62"
3131
features = [
3232
"Win32_Foundation",

cap-tempfile/src/tempfile.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,21 @@ fn new_tempfile(d: &Dir, anonymous: bool) -> io::Result<(File, Option<String>)>
127127
#[cfg(windows)]
128128
if anonymous {
129129
use cap_std::fs::OpenOptionsExt;
130+
use windows_sys::Win32::Storage::FileSystem::{
131+
FILE_ATTRIBUTE_TEMPORARY, FILE_FLAG_DELETE_ON_CLOSE,
132+
};
130133
opts.share_mode(0);
134+
opts.custom_flags(FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE);
131135
}
132136
let (f, name) = super::retry_with_name_ignoring(io::ErrorKind::AlreadyExists, |name| {
133137
d.open_with(name, &opts)
134138
})?;
135139
if anonymous {
136-
d.remove_file(name)?;
140+
// On Windows we use `FILE_FLAG_DELETE_ON_CLOSE` instead.
141+
#[cfg(not(windows))]
142+
{
143+
d.remove_file(name)?;
144+
}
137145
Ok((f, None))
138146
} else {
139147
Ok((f, Some(name)))

0 commit comments

Comments
 (0)