diff --git a/README.md b/README.md index 81fccca878..87b2857fb6 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,8 @@ xcl rpf D:\data\shaders.rpf --extract NAME --out folder - XCL works on **unpacked** game files you own; it contains nothing for decrypting or circumventing DRM. - **The game folder is never modified** – writes go to the overlay. +- Files opened with `FILE_FLAG_DELETE_ON_CLOSE` stay hidden from the title after + the overlay copy is deleted, while the original stays in the game folder. - Translated shaders are reused across XCL rebuilds when the translator sources and DXC toolchain are unchanged; `XCL_TRANSLATED_CACHE=0` disables that disk cache. - Every run writes `report.json` (called stubs, unknown D3D12.X slots and interfaces, XGameRuntime diff --git a/crates/xcl/src/vfs.rs b/crates/xcl/src/vfs.rs index 469e6269bb..bd4f4ad665 100644 --- a/crates/xcl/src/vfs.rs +++ b/crates/xcl/src/vfs.rs @@ -514,15 +514,19 @@ impl Vfs { if let Some(parent) = Path::new(&ov).parent() { std::fs::create_dir_all(parent).map_err(|_| ERROR_PATH_NOT_FOUND)?; } - if keep_content && !Path::new(&ov).exists() && self.visible_in_original(rel) { - let orig = self.original_path(rel); - if Path::new(&orig).is_dir() { - std::fs::create_dir_all(&ov).map_err(|_| ERROR_ACCESS_DENIED)?; - } else { - std::fs::copy(&orig, &ov).map_err(|_| ERROR_ACCESS_DENIED)?; + if !Path::new(&ov).exists() { + if keep_content && self.visible_in_original(rel) { + let orig = self.original_path(rel); + if Path::new(&orig).is_dir() { + std::fs::create_dir_all(&ov).map_err(|_| ERROR_ACCESS_DENIED)?; + } else { + std::fs::copy(&orig, &ov).map_err(|_| ERROR_ACCESS_DENIED)?; + } } + // A new overlay entry can replace a whiteout. Reopening an existing entry + // must keep the whiteout that hides its original after delete-on-close. + self.remove_whiteout(rel); } - self.remove_whiteout(rel); self.record_write(rel); Ok(ov) } diff --git a/crates/xcl/src/vfs_hooks.rs b/crates/xcl/src/vfs_hooks.rs index 7cf09a9c3b..04cdb1d428 100644 --- a/crates/xcl/src/vfs_hooks.rs +++ b/crates/xcl/src/vfs_hooks.rs @@ -14,7 +14,7 @@ use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::Mutex; use windows_sys::Win32::Foundation::{GetLastError, SetLastError, ERROR_NOT_SAME_DEVICE, HANDLE, INVALID_HANDLE_VALUE}; -use windows_sys::Win32::Storage::FileSystem::{MOVEFILE_DELAY_UNTIL_REBOOT, WIN32_FIND_DATAA, WIN32_FIND_DATAW}; +use windows_sys::Win32::Storage::FileSystem::{FILE_FLAG_DELETE_ON_CLOSE, MOVEFILE_DELAY_UNTIL_REBOOT, WIN32_FIND_DATAA, WIN32_FIND_DATAW}; use crate::hooks::hook; use crate::util::{from_ansi_ptr, from_file_ansi_ptr, from_wide_ptr, wide}; @@ -107,14 +107,45 @@ fn apply_open_decision( } } +fn open_file( + vfs: &vfs::Vfs, + path: &str, + access: u32, + disposition: u32, + flags: u32, + passthrough: impl FnOnce() -> HANDLE, + redirected: impl FnOnce(*const u16, u32) -> HANDLE, +) -> HANDLE { + let target = vfs.classify(path); + let delete_on_close = if flags & FILE_FLAG_DELETE_ON_CLOSE != 0 { + match &target { + Target::Game { rel } if std::path::Path::new(&vfs.original_path(rel)).is_file() => Some(rel.clone()), + _ => None, + } + } else { + None + }; + let decision = vfs.open_decision_for(target, access, disposition, flags); + let handle = apply_open_decision(decision, disposition, passthrough, redirected); + if handle != INVALID_HANDLE_VALUE { + if let Some(rel) = delete_on_close { + let error = unsafe { GetLastError() }; + // An existing overlay entry still wins lookups while the handle is open. Once + // Windows removes it, the original must stay hidden instead of resurfacing. + vfs.commit_overlay_removal(&rel); + unsafe { SetLastError(error) }; + } + } + handle +} + unsafe extern "system" fn hk_create_file_w(n: *const u16, acc: u32, sh: u32, sa: *const c_void, disp: u32, fl: u32, t: HANDLE) -> HANDLE { let orig = orig!(O_CREATE_FILE_W as FnCreateFileW); let (Some(_g), Some(v)) = (Guard::enter(), vfs::get()) else { return orig(n, acc, sh, sa, disp, fl, t) }; let Some(path) = from_wide_ptr(n) else { return orig(n, acc, sh, sa, disp, fl, t) }; log_open("CreateFileW", &path, fl); - apply_open_decision( - v.open_decision(&path, acc, disp, fl), - disp, + open_file( + v, &path, acc, disp, fl, || orig(n, acc, sh, sa, disp, fl, t), |p, d| orig(p, acc, sh, sa, d, fl, t), ) @@ -125,9 +156,8 @@ unsafe extern "system" fn hk_create_file_a(n: *const u8, acc: u32, sh: u32, sa: let (Some(_g), Some(v)) = (Guard::enter(), vfs::get()) else { return orig(n, acc, sh, sa, disp, fl, t) }; let Some(path) = from_ansi_ptr(n) else { return orig(n, acc, sh, sa, disp, fl, t) }; log_open("CreateFileA", &path, fl); - apply_open_decision( - v.open_decision(&path, acc, disp, fl), - disp, + open_file( + v, &path, acc, disp, fl, || orig(n, acc, sh, sa, disp, fl, t), |p, d| orig!(O_CREATE_FILE_W as FnCreateFileW)(p, acc, sh, sa, d, fl, t), ) @@ -140,9 +170,8 @@ unsafe extern "system" fn hk_create_file_2(n: *const u16, acc: u32, sh: u32, dis // CREATEFILE2_EXTENDED_PARAMETERS { dwSize, dwFileAttributes, dwFileFlags, ... } let flags = if params.is_null() { 0 } else { *((params as *const u8).add(8) as *const u32) }; log_open("CreateFile2", &path, flags); - apply_open_decision( - v.open_decision(&path, acc, disp, flags), - disp, + open_file( + v, &path, acc, disp, flags, || orig(n, acc, sh, disp, params), |p, d| orig(p, acc, sh, d, params), ) diff --git a/crates/xcl/tests/e2e.rs b/crates/xcl/tests/e2e.rs index 57301087f2..041b82ca28 100644 --- a/crates/xcl/tests/e2e.rs +++ b/crates/xcl/tests/e2e.rs @@ -48,6 +48,12 @@ fn copy_dir(src: &Path, dst: &Path) { } } +fn copy_test_game(game: &Path) { + let manifest = Path::new(env!("CARGO_MANIFEST_DIR")); + copy_dir(&manifest.join("..").join("..").join("tests").join("data").join("testgame"), game); + std::fs::write(game.join("data/delete_on_close.txt"), b"original").unwrap(); +} + #[test] fn launcher_uses_configured_exe_in_an_ambiguous_folder() { let nonce = std::time::SystemTime::now() @@ -96,12 +102,11 @@ fn test_title_runs_and_game_folder_is_untouched() { title.display(), dll.display() ); - let manifest = Path::new(env!("CARGO_MANIFEST_DIR")); let root = Path::new(env!("CARGO_TARGET_TMPDIR")).join("e2e"); let _ = std::fs::remove_dir_all(&root); let game = root.join("game"); let data = root.join("data"); - copy_dir(&manifest.join("..").join("..").join("tests").join("data").join("testgame"), &game); + copy_test_game(&game); std::fs::copy(&title, game.join("xcl_testtitle.exe")).unwrap(); std::fs::copy(&dll, game.join("testgame_mw.dll")).unwrap(); let before = snapshot(&game); @@ -151,6 +156,7 @@ fn test_title_runs_and_game_folder_is_untouched() { assert!(!ov.join("new_save.bin").exists()); let wo = std::fs::read_to_string(tdata.join("overlay").join(".xcl_whiteouts")).unwrap(); assert!(wo.lines().any(|l| l == "data\\delete_me.txt"), "whiteouts: {wo}"); + assert!(wo.lines().any(|l| l == "data\\delete_on_close.txt"), "whiteouts: {wo}"); assert!(tdata.join("temp").join("scratch.tmp").is_file()); assert!(tdata.join("saves").join("test-scid").join("slot0.sav").is_file()); // the PLS drive (S:\) is the data folder's pls @@ -258,7 +264,7 @@ fn test_title_draws_with_a_root_signature_under_the_debug_layer() { let root = manifest.join("..").join("..").join("target").join("e2e-debug-layer"); let _ = std::fs::remove_dir_all(&root); let (game, data) = (root.join("game"), root.join("data")); - copy_dir(&manifest.join("..").join("..").join("tests").join("data").join("testgame"), &game); + copy_test_game(&game); for f in ["xcl_testtitle.exe", "testgame_mw.dll"] { std::fs::copy(bin_dir.join(f), game.join(f)).unwrap_or_else(|e| panic!("{f}: {e} – build first: cargo build --workspace")); } diff --git a/tests/testtitle/src/main.rs b/tests/testtitle/src/main.rs index d5ae267a44..5fe65572f5 100644 --- a/tests/testtitle/src/main.rs +++ b/tests/testtitle/src/main.rs @@ -513,6 +513,30 @@ fn main() { Ok(()) }); + check("files: delete-on-close hides the original after the handle closes", || { + use std::io::Read; + use std::os::windows::fs::OpenOptionsExt; + + const DELETE: u32 = 0x0001_0000; + const GENERIC_READ: u32 = 0x8000_0000; + const FILE_FLAG_DELETE_ON_CLOSE: u32 = 0x0400_0000; + let path = r"G:\data\delete_on_close.txt"; + let mut file = std::fs::OpenOptions::new() + .access_mode(GENERIC_READ | DELETE) + .custom_flags(FILE_FLAG_DELETE_ON_CLOSE) + .open(path) + .map_err(|e| e.to_string())?; + let mut contents = Vec::new(); + file.read_to_end(&mut contents).map_err(|e| e.to_string())?; + ensure!(contents == b"original", "wrong file contents before close"); + let other = std::fs::OpenOptions::new().write(true).open(path).map_err(|e| e.to_string())?; + drop(file); + drop(other); + ensure!(!std::path::Path::new(path).exists(), "original became visible again"); + ensure!(std::fs::read(path).is_err(), "original became readable again"); + Ok(()) + }); + check("files: directory listing merges overlay and hides deleted files", || { let mut names: Vec = std::fs::read_dir("G:\\data") .map_err(|e| e.to_string())? @@ -523,7 +547,7 @@ fn main() { for want in ["hello.txt", "renamed.bin", "newdir", "sub"] { ensure!(names.iter().any(|n| n == want), "missing {want} in {names:?}"); } - ensure!(!names.iter().any(|n| n == "delete_me.txt" || n == "new_save.bin"), "stale entries {names:?}"); + ensure!(!names.iter().any(|n| matches!(n.as_str(), "delete_me.txt" | "delete_on_close.txt" | "new_save.bin")), "stale entries {names:?}"); Ok(()) });