From 3ee3613af1d096c94e398ef9f72ffd34d3c17bf7 Mon Sep 17 00:00:00 2001 From: Afonso Pereia Date: Tue, 24 Nov 2026 14:19:05 +0000 Subject: [PATCH] Scripts: bound RPF extraction filenames Valid 254-byte record names exceeded Windows' filename-component limit after collision and temporary suffixes were added. Keep collision suffixes intact by shortening the original name, and use short process/counter temporary names. The actual CLI regression failed before the fix with error 123. Windows RPF integration tests afterward: 3 passed, 0 failed, 0 ignored; both duplicate payloads and the existing file were preserved. --- README.md | 2 ++ crates/xcl/src/rpf.rs | 15 +++++++++--- crates/xcl/tests/rpf_e2e.rs | 48 +++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 0710608d4d..a0bfe15b4f 100644 --- a/README.md +++ b/README.md @@ -117,6 +117,8 @@ xcl shaders D:\data\shaders.rpf --top 50 # pack statistics; --recomp / --compi xcl rpf D:\data\shaders.rpf --extract NAME --out folder ``` +- RPF extraction keeps existing files and duplicate records, shortening long names when adding + collision suffixes to fit Windows filename limits. Temporary filenames stay short. - **Without a command line** (xcl.exe opened from Explorer) XCL shows a small menu: *Choose a game…* (pick its exe or `MicrosoftGame.config`) or *Run again* (the last one). On an error the window stays open until Enter. diff --git a/crates/xcl/src/rpf.rs b/crates/xcl/src/rpf.rs index d723803089..7e80c2fc0d 100644 --- a/crates/xcl/src/rpf.rs +++ b/crates/xcl/src/rpf.rs @@ -37,11 +37,19 @@ fn extraction_path(out: &Path, name: &str, offset: usize, used: &mut BTreeSet 255 { + // Keep the collision suffix while shortening the original name. + let mut end = safe.len() - (suffix.len() - 255); + while !safe.is_char_boundary(end) { + end -= 1; + } + suffix.replace_range(end..safe.len(), ""); + } let path = out.join(suffix); if !path.exists() && used.insert(path.clone()) { return path; @@ -66,10 +74,11 @@ fn atomic_write_with( before_commit: impl FnOnce(&Path) -> io::Result<()>, ) -> io::Result<()> { let parent = path.parent().ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "output has no parent"))?; - let name = path.file_name().ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "output has no file name"))?; + path.file_name().ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "output has no file name"))?; let (temporary, mut file) = loop { let id = TEMP_ID.fetch_add(1, Ordering::Relaxed); - let candidate = parent.join(format!(".{}.tmp.{}.{id}", name.to_string_lossy(), std::process::id())); + // Scanned record names start with a letter or underscore, never this dot prefix. + let candidate = parent.join(format!(".xcl-rpf.tmp.{}.{id}", std::process::id())); match std::fs::OpenOptions::new().write(true).create_new(true).open(&candidate) { Ok(file) => break (TempPath(candidate), file), Err(error) if error.kind() == io::ErrorKind::AlreadyExists => continue, diff --git a/crates/xcl/tests/rpf_e2e.rs b/crates/xcl/tests/rpf_e2e.rs index ed7a4c0957..05c7840382 100644 --- a/crates/xcl/tests/rpf_e2e.rs +++ b/crates/xcl/tests/rpf_e2e.rs @@ -74,3 +74,51 @@ fn explicit_probe_rejects_conflicting_options() { assert!(String::from_utf8(output.stderr).unwrap().contains(args[0])); } } + +#[test] +fn extraction_preserves_long_duplicate_names_and_existing_files() { + let nonce = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_nanos(); + let root = std::env::temp_dir().join(format!("xcl-rpf-names-{}-{nonce}", std::process::id())); + let output_dir = root.join("out"); + std::fs::create_dir_all(&output_dir).unwrap(); + // Verbatim paths isolate filename-component limits from MAX_PATH handling. + let output_dir = std::fs::canonicalize(output_dir).unwrap(); + let name = format!("PS_{}", "x".repeat(251)); + std::fs::write(output_dir.join(&name), b"preserve me").unwrap(); + + let mut data = b"9FPR".to_vec(); + data.resize(0x40, 0); + for payload in [b"first payload".as_slice(), b"second payload".as_slice()] { + let compressed = miniz_oxide::deflate::compress_to_vec(payload, 6); + data.push((name.len() + 1) as u8); + data.extend_from_slice(name.as_bytes()); + data.push(0); + data.push(50); + for value in [compressed.len() as u32, 0, compressed.len() as u32, payload.len() as u32] { + data.extend_from_slice(&value.to_le_bytes()); + } + data.extend_from_slice(&compressed); + } + let archive = root.join("synthetic.rpf"); + std::fs::write(&archive, data).unwrap(); + let output = Command::new(env!("CARGO_BIN_EXE_xcl")) + .arg("rpf") + .arg(&archive) + .arg("--extract") + .arg(&name) + .arg("--out") + .arg(&output_dir) + .output(); + let preserved = std::fs::read(output_dir.join(&name)); + let files = std::fs::read_dir(&output_dir).unwrap() + .map(|entry| entry.and_then(|entry| std::fs::read(entry.path()))) + .collect::, _>>(); + std::fs::remove_dir_all(root).unwrap(); + + let output = output.unwrap(); + assert_eq!(output.status.code(), Some(0), "{}", String::from_utf8_lossy(&output.stderr)); + assert_eq!(preserved.unwrap(), b"preserve me"); + let mut files = files.unwrap(); + files.sort(); + assert_eq!(files, vec![b"first payload".to_vec(), b"preserve me".to_vec(), b"second payload".to_vec()]); +}