use std::ffi::OsStr; use std::fs::{self, File, OpenOptions}; use std::io::{self, Write}; use std::os::windows::ffi::OsStrExt; use std::os::windows::fs::{MetadataExt, OpenOptionsExt}; use std::os::windows::io::AsRawHandle; use std::path::{Component, Path, Prefix}; const FILE_READ_ATTRIBUTES: u32 = 0x80; const FILE_ATTRIBUTE_REPARSE_POINT: u32 = 0x400; const FILE_FLAG_BACKUP_SEMANTICS: u32 = 0x0200_0000; const FILE_FLAG_OPEN_REPARSE_POINT: u32 = 0x0020_0000; const FILE_SHARE_READ_WRITE: u32 = 0x3; const FILE_SHARE_DELETE: u32 = 0x4; const FILE_ID_INFO_CLASS: i32 = 18; #[repr(C)] #[derive(Default)] struct FileIdInfo { volume: u64, file_id: [u8; 16], } #[link(name = "kernel32")] unsafe extern "system" { fn GetFileInformationByHandleEx( file: *mut std::ffi::c_void, class: i32, information: *mut FileIdInfo, size: u32, ) -> i32; } fn unsafe_location() -> io::Error { io::Error::new(io::ErrorKind::InvalidInput, "unsafe project location") } fn directory(path: &Path, share_mode: u32) -> io::Result { let handle = OpenOptions::new() .access_mode(FILE_READ_ATTRIBUTES) .share_mode(share_mode) .custom_flags(FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT) .open(path)?; let metadata = handle.metadata()?; if !metadata.is_dir() || metadata.file_attributes() & FILE_ATTRIBUTE_REPARSE_POINT != 0 { return Err(unsafe_location()); } Ok(handle) } fn directory_identity(handle: &File) -> io::Result { let mut identity = FileIdInfo::default(); // The live handle and the initialized buffer match Windows' FILE_ID_INFO layout. let success = unsafe { GetFileInformationByHandleEx( handle.as_raw_handle(), FILE_ID_INFO_CLASS, &mut identity, std::mem::size_of::() as u32, ) }; if success == 0 { return Err(io::Error::last_os_error()); } Ok(identity) } fn operate(action: &OsStr, tickets: &Path, projects: &Path) -> io::Result<()> { let parent = projects.parent().ok_or_else(unsafe_location)?; if parent.file_name() != Some(OsStr::new("TexrraData")) || projects.file_name() != Some(OsStr::new("projects")) || !projects.is_absolute() || !matches!( projects.components().next(), Some(Component::Prefix(prefix)) if matches!(prefix.kind(), Prefix::Disk(_) | Prefix::VerbatimDisk(_)) ) || projects .components() .any(|part| part == Component::ParentDir) { return Err(unsafe_location()); } let display = projects.to_str().ok_or_else(unsafe_location)?; let path_bytes: Vec<_> = projects .as_os_str() .encode_wide() .flat_map(u16::to_le_bytes) .collect(); if path_bytes.len() > 1240 || display.chars().any(|char| { char.is_control() || matches!(char, '\u{061c}' | '\u{200e}' | '\u{200f}' | '\u{202a}'..='\u{202e}' | '\u{2066}'..='\u{2069}') }) { return Err(unsafe_location()); } // Keeping ancestor handles without FILE_SHARE_DELETE prevents redirects above the store. let _ancestors = parent .ancestors() .collect::>() .into_iter() .rev() .map(|path| directory(path, FILE_SHARE_READ_WRITE)) .collect::>>()?; let handle = directory(projects, FILE_SHARE_READ_WRITE | FILE_SHARE_DELETE)?; let backend_lock = OpenOptions::new() .read(true) .write(true) .share_mode(FILE_SHARE_READ_WRITE) .custom_flags(FILE_FLAG_OPEN_REPARSE_POINT) .open(parent.join("backend.lock")) .map_err(|error| { if error.kind() == io::ErrorKind::NotFound { unsafe_location() } else { error } })?; let lock_metadata = backend_lock.metadata()?; if !lock_metadata.is_file() || lock_metadata.file_attributes() & FILE_ATTRIBUTE_REPARSE_POINT != 0 { return Err(unsafe_location()); } backend_lock.try_lock().map_err(io::Error::from)?; let identity = directory_identity(&handle)?; let mut ticket = path_bytes.clone(); ticket.extend_from_slice(&identity.volume.to_le_bytes()); ticket.extend_from_slice(&identity.file_id); let identity_path = tickets.join("projects-identity.bin"); if action == "inspect" { write_new(&identity_path, &ticket)?; write_new(&tickets.join("projects-path.txt"), &path_bytes) } else if action == "delete" { if fs::read(identity_path)? != ticket { return Err(unsafe_location()); } // On Windows, remove_dir_all removes reparse points without traversing them. fs::remove_dir_all(projects) } else { Err(unsafe_location()) } } fn write_new(path: &Path, data: &[u8]) -> io::Result<()> { OpenOptions::new() .write(true) .create_new(true) .open(path)? .write_all(data) } pub(crate) fn run(action: &OsStr, ticket_dir: &Path, projects: &Path) -> i32 { match operate(action, ticket_dir, projects) { Ok(()) => 0, Err(error) if action == "inspect" && error.kind() == io::ErrorKind::NotFound => 2, Err(_) => 1, } }