From 3460d08008d2fcc8ac2af28278bc896d99c36986 Mon Sep 17 00:00:00 2001 From: Seabass Date: Tue, 24 Nov 2026 13:54:12 +0000 Subject: [PATCH] VFS: a write below a missing parent fails, it does not conjure the folder prepare_write create_dir_all'd the overlay parent with no merged-view check, so CreateFileW CREATE_ALWAYS on G:\nodir\f.bin created 'nodir' where Win32 fails with ERROR_PATH_NOT_FOUND, and a write below a removed directory resurrected the folder through commit_write's remove_whiteout. stage_write (copy/move) already had exactly this guard, so CreateFileW and CopyFileW disagreed on the same path. Same case as reported in #577 (third-party static find); the create_dir multi-level behaviour is deliberately untouched - the 'directories' test codifies multi-level creation as the contract, and that is a separate maintainer call (see the #577 comment). --- crates/xcl/src/vfs.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/crates/xcl/src/vfs.rs b/crates/xcl/src/vfs.rs index dd02bcb67a..cfbae838a7 100644 --- a/crates/xcl/src/vfs.rs +++ b/crates/xcl/src/vfs.rs @@ -510,6 +510,13 @@ impl Vfs { /// Ensure `rel` exists in the overlay (copying the original when `keep_content`) /// and return the overlay path. pub fn prepare_write(&self, rel: &str, keep_content: bool) -> Result { + // Win32 fails a create-write with ERROR_PATH_NOT_FOUND when the parent folder + // does not exist; do not conjure missing parents (same check as `stage_write`). + if let Some((parent_rel, _)) = rel.rsplit_once('\\') { + if !self.is_dir(parent_rel) { + return Err(ERROR_PATH_NOT_FOUND); + } + } let ov = self.overlay_path(rel); if let Some(parent) = Path::new(&ov).parent() { std::fs::create_dir_all(parent).map_err(|_| ERROR_PATH_NOT_FOUND)?; @@ -1126,4 +1133,35 @@ mod tests { assert!(v.is_whiteout("data\\gone.txt")); assert!(v.is_whiteout("data\\sub\\deep.txt")); } + + #[test] + fn write_below_a_missing_parent_fails_without_creating_it() { + let f = Fixture::new("noparent"); + let v = &f.vfs; + // Win32: CreateFileW CREATE_ALWAYS under a folder that exists in neither layer + // fails with ERROR_PATH_NOT_FOUND (3); it does not conjure the folder. + assert_eq!(v.prepare_write("nodir\\f.bin", true), Err(ERROR_PATH_NOT_FOUND)); + assert!(!Path::new(&v.overlay_path("nodir")).exists()); + } + + #[test] + fn write_below_a_removed_dir_does_not_resurrect_it() { + let f = Fixture::new("norez"); + let v = &f.vfs; + std::fs::create_dir(f.game().join("empty")).unwrap(); + assert_eq!(v.remove_dir("empty"), Ok(())); + assert_eq!(v.prepare_write("empty\\f.bin", true), Err(ERROR_PATH_NOT_FOUND)); + assert!(!Path::new(&v.overlay_path("empty")).exists()); + assert!(v.is_whiteout("empty")); + } + + #[test] + fn write_below_a_game_only_parent_still_works() { + let f = Fixture::new("gameparent"); + let v = &f.vfs; + std::fs::create_dir(f.game().join("gonly")).unwrap(); + let p = v.prepare_write("gonly\\n.txt", false).expect("game-only parent is visible"); + std::fs::write(&p, b"new").unwrap(); + assert_eq!(std::fs::read(&p).unwrap(), b"new"); + } }