mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-12-02 20:20:35 -05:00
fix(platform): hold the zone's target open, without delete sharing, until the stream is written
Addresses review: between the existence check and the stream write, another process could delete or rename the target, and the write would then recreate it as an empty file. The target is now opened without FILE_SHARE_DELETE, so it can't be deleted or renamed until the zone is written, and the check reads that handle's metadata. Refs #8622
This commit is contained in:
1 parent
59cd00129c
commit
59f8798200
1 file changed
+11
-3
@@ -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))]
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user