diff --git a/crates/platform/src/zone.rs b/crates/platform/src/zone.rs index ace0ad650d..d0d26b6b15 100644 --- a/crates/platform/src/zone.rs +++ b/crates/platform/src/zone.rs @@ -18,15 +18,23 @@ const MAX_ZONE_STREAM: u64 = 64 * 1024; /// Internet zone when `source` is `None` or has no zone. Windows only; elsewhere nothing is /// written. Fails where the volume keeps no alternate data streams (FAT32, exFAT, some network /// shares), so callers treat the mark as best effort. Fails with `NotFound` when `target` isn't an -/// existing file: writing `target:Zone.Identifier` would otherwise create an empty `target`. +/// existing file: writing `target:Zone.Identifier` would otherwise create an empty `target`. The +/// target is held open, without delete sharing, until the stream is written. pub fn mark_from(target: &Path, source: Option<&Path>) -> std::io::Result<()> { #[cfg(windows)] { - if !std::fs::metadata(target)?.is_file() { + use std::os::windows::fs::OpenOptionsExt; + // FILE_SHARE_READ | FILE_SHARE_WRITE, without FILE_SHARE_DELETE: while this handle is open + // the target can't be deleted or renamed, so the stream write below can't recreate it. + const SHARE_READ_WRITE: u32 = 0x1 | 0x2; + let held = std::fs::OpenOptions::new().read(true).share_mode(SHARE_READ_WRITE).open(target)?; + if !held.metadata()?.is_file() { return Err(std::io::Error::new(std::io::ErrorKind::NotFound, "the zone's target is not a file")); } let zone = source.and_then(read_zone).unwrap_or_else(|| INTERNET_ZONE.as_bytes().to_vec()); - std::fs::write(zone_stream(target), zone) + let written = std::fs::write(zone_stream(target), zone); + drop(held); + written } #[cfg(not(windows))] {