Files: send rewritten narrow paths to the host, not to our wide wrappers
CreateFileA, GetFileAttributesA, fopen, fopen_s and the narrow std::_Fiopen turn the title's virtual-drive path into a host path and then passed it to our own wide wrapper, which looked it up a second time. When the host path starts with a letter that is itself mounted, it was rewritten again: with the game on a host G: drive (say G:\GTA6), the title's G:\x64\a.rpf became G:\GTA6\x64\a.rpf and then G:\GTA6\GTA6\x64\a.rpf, so every narrow open of a title file failed. The same happens for T: when the data folder is on a host T: drive. They now call the host's wide function directly, as DeleteFileA, SetCurrentDirectoryA, _open, _stat64i32 and remove already do.
This commit is contained in:
1 parent
62b737a69b
commit
fa4a875c30
1 file changed
+88
-16
@@ -4,9 +4,11 @@
|
|||||||
//! Every title import that takes a path is bound here. A path on a virtual
|
//! Every title import that takes a path is bound here. A path on a virtual
|
||||||
//! drive (`X:\..`, `X:/..`, `X:`, `\\?\X:\..`) is rewritten to the host
|
//! drive (`X:\..`, `X:/..`, `X:`, `\\?\X:\..`) is rewritten to the host
|
||||||
//! folder behind it and handed to the host's own function; any other path
|
//! folder behind it and handed to the host's own function; any other path
|
||||||
//! goes to the host unchanged. A rewritten narrow path goes to the wide
|
//! goes to the host unchanged. A rewritten narrow path goes to the host's
|
||||||
//! twin, so host folders outside the ANSI code page work. Letters are
|
//! wide twin, so host folders outside the ANSI code page work; never to our
|
||||||
//! chosen among those the host does not use, so no host drive is hidden.
|
//! wide wrapper, which would rewrite the host path a second time when its
|
||||||
|
//! drive letter is mounted too. Letters are chosen among those the host
|
||||||
|
//! does not use, so no host drive is hidden.
|
||||||
#![allow(non_snake_case)]
|
#![allow(non_snake_case)]
|
||||||
#![allow(clippy::not_unsafe_ptr_arg_deref)]
|
#![allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||||
#![allow(clippy::too_many_arguments)]
|
#![allow(clippy::too_many_arguments)]
|
||||||
@@ -282,15 +284,17 @@ pub extern "system" fn CreateFileA(
|
|||||||
template: Handle,
|
template: Handle,
|
||||||
) -> Handle {
|
) -> Handle {
|
||||||
match narrow(path) {
|
match narrow(path) {
|
||||||
Some(p) => CreateFileW(
|
Some(p) => {
|
||||||
p.as_ptr(),
|
host!("kernel32.dll", "CreateFileW": extern "system" fn(*const u16, u32, u32, *const c_void, u32, u32, Handle) -> Handle)(
|
||||||
access,
|
p.as_ptr(),
|
||||||
share,
|
access,
|
||||||
security,
|
share,
|
||||||
disposition,
|
security,
|
||||||
flags,
|
disposition,
|
||||||
template,
|
flags,
|
||||||
),
|
template,
|
||||||
|
)
|
||||||
|
}
|
||||||
None => {
|
None => {
|
||||||
host!("kernel32.dll", "CreateFileA": extern "system" fn(*const c_char, u32, u32, *const c_void, u32, u32, Handle) -> Handle)(
|
host!("kernel32.dll", "CreateFileA": extern "system" fn(*const c_char, u32, u32, *const c_void, u32, u32, Handle) -> Handle)(
|
||||||
path,
|
path,
|
||||||
@@ -354,7 +358,11 @@ pub extern "system" fn SetFileAttributesW(path: *const u16, attributes: u32) ->
|
|||||||
|
|
||||||
pub extern "system" fn GetFileAttributesA(path: *const c_char) -> u32 {
|
pub extern "system" fn GetFileAttributesA(path: *const c_char) -> u32 {
|
||||||
match narrow(path) {
|
match narrow(path) {
|
||||||
Some(p) => GetFileAttributesW(p.as_ptr()),
|
Some(p) => {
|
||||||
|
host!("kernel32.dll", "GetFileAttributesW": extern "system" fn(*const u16) -> u32)(
|
||||||
|
p.as_ptr(),
|
||||||
|
)
|
||||||
|
}
|
||||||
None => {
|
None => {
|
||||||
host!("kernel32.dll", "GetFileAttributesA": extern "system" fn(*const c_char) -> u32)(
|
host!("kernel32.dll", "GetFileAttributesA": extern "system" fn(*const c_char) -> u32)(
|
||||||
path,
|
path,
|
||||||
@@ -536,7 +544,10 @@ pub extern "C" fn fread(buffer: *mut u8, size: usize, count: usize, stream: File
|
|||||||
|
|
||||||
pub extern "C" fn fopen(path: *const c_char, mode: *const c_char) -> File {
|
pub extern "C" fn fopen(path: *const c_char, mode: *const c_char) -> File {
|
||||||
match narrow(path) {
|
match narrow(path) {
|
||||||
Some(p) => _wfopen(p.as_ptr(), widen(mode).as_ptr()),
|
Some(p) => host!("ucrtbase.dll", "_wfopen": extern "C" fn(*const u16, *const u16) -> File)(
|
||||||
|
p.as_ptr(),
|
||||||
|
widen(mode).as_ptr(),
|
||||||
|
),
|
||||||
None => {
|
None => {
|
||||||
host!("ucrtbase.dll", "fopen": extern "C" fn(*const c_char, *const c_char) -> File)(
|
host!("ucrtbase.dll", "fopen": extern "C" fn(*const c_char, *const c_char) -> File)(
|
||||||
path, mode,
|
path, mode,
|
||||||
@@ -547,7 +558,13 @@ pub extern "C" fn fopen(path: *const c_char, mode: *const c_char) -> File {
|
|||||||
|
|
||||||
pub extern "C" fn fopen_s(out: *mut File, path: *const c_char, mode: *const c_char) -> i32 {
|
pub extern "C" fn fopen_s(out: *mut File, path: *const c_char, mode: *const c_char) -> i32 {
|
||||||
match narrow(path) {
|
match narrow(path) {
|
||||||
Some(p) => _wfopen_s(out, p.as_ptr(), widen(mode).as_ptr()),
|
Some(p) => {
|
||||||
|
host!("ucrtbase.dll", "_wfopen_s": extern "C" fn(*mut File, *const u16, *const u16) -> i32)(
|
||||||
|
out,
|
||||||
|
p.as_ptr(),
|
||||||
|
widen(mode).as_ptr(),
|
||||||
|
)
|
||||||
|
}
|
||||||
None => {
|
None => {
|
||||||
host!("ucrtbase.dll", "fopen_s": extern "C" fn(*mut File, *const c_char, *const c_char) -> i32)(
|
host!("ucrtbase.dll", "fopen_s": extern "C" fn(*mut File, *const c_char, *const c_char) -> i32)(
|
||||||
out, path, mode,
|
out, path, mode,
|
||||||
@@ -640,7 +657,13 @@ extern "C" fn fiopen_wide(path: *const u16, mode: i32, protection: i32) -> File
|
|||||||
/// `_Fiopen(const char* path, ios_base::openmode, int protection)`.
|
/// `_Fiopen(const char* path, ios_base::openmode, int protection)`.
|
||||||
extern "C" fn fiopen(path: *const c_char, mode: i32, protection: i32) -> File {
|
extern "C" fn fiopen(path: *const c_char, mode: i32, protection: i32) -> File {
|
||||||
match narrow(path) {
|
match narrow(path) {
|
||||||
Some(p) => fiopen_wide(p.as_ptr(), mode, protection),
|
Some(p) => {
|
||||||
|
host!("msvcp140.dll", "?_Fiopen@std@@YAPEAU_iobuf@@PEB_WHH@Z": extern "C" fn(*const u16, i32, i32) -> File)(
|
||||||
|
p.as_ptr(),
|
||||||
|
mode,
|
||||||
|
protection,
|
||||||
|
)
|
||||||
|
}
|
||||||
None => {
|
None => {
|
||||||
host!("msvcp140.dll", "?_Fiopen@std@@YAPEAU_iobuf@@PEBDHH@Z": extern "C" fn(*const c_char, i32, i32) -> File)(
|
host!("msvcp140.dll", "?_Fiopen@std@@YAPEAU_iobuf@@PEBDHH@Z": extern "C" fn(*const c_char, i32, i32) -> File)(
|
||||||
path, mode, protection,
|
path, mode, protection,
|
||||||
@@ -866,4 +889,53 @@ mod tests {
|
|||||||
fclose(f);
|
fclose(f);
|
||||||
assert!(dir.path().join("plain.txt").is_file());
|
assert!(dir.path().join("plain.txt").is_file());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A narrow path is rewritten once, as its wide twin is. The inner
|
||||||
|
/// drive's folder lies on the outer virtual drive, as the title folder
|
||||||
|
/// does when it sits on a host `G:` drive and is mounted as `G:`. The
|
||||||
|
/// outer letter is no host drive, so the host finds nothing there; a
|
||||||
|
/// narrow call that reaches the outer folder rewrote the path twice.
|
||||||
|
#[test]
|
||||||
|
fn narrow_paths_are_rewritten_once() {
|
||||||
|
let dir = tempfile::tempdir().unwrap();
|
||||||
|
let folder = dir.path().join("inner");
|
||||||
|
std::fs::create_dir(&folder).unwrap();
|
||||||
|
std::fs::write(folder.join("present.txt"), b"").unwrap();
|
||||||
|
let outer = mount(dir.path()).unwrap();
|
||||||
|
let inner = mount(Path::new(&format!(r"{outer}:\inner"))).unwrap();
|
||||||
|
let path = |name: &str| format!("{inner}:/{name}\0");
|
||||||
|
|
||||||
|
const INVALID_FILE_ATTRIBUTES: u32 = u32::MAX;
|
||||||
|
let wide = GetFileAttributesW(w(&format!("{inner}:/present.txt")).as_ptr());
|
||||||
|
assert_eq!(wide, INVALID_FILE_ATTRIBUTES);
|
||||||
|
let narrow = GetFileAttributesA(path("present.txt").as_ptr().cast());
|
||||||
|
assert_eq!(narrow, wide, "GetFileAttributesA agrees with its wide twin");
|
||||||
|
|
||||||
|
const GENERIC_WRITE: u32 = 0x4000_0000;
|
||||||
|
const CREATE_ALWAYS: u32 = 2;
|
||||||
|
let null = std::ptr::null_mut();
|
||||||
|
let h = CreateFileA(
|
||||||
|
path("a.bin").as_ptr().cast(),
|
||||||
|
GENERIC_WRITE,
|
||||||
|
0,
|
||||||
|
null,
|
||||||
|
CREATE_ALWAYS,
|
||||||
|
0,
|
||||||
|
null,
|
||||||
|
);
|
||||||
|
assert_eq!(h as isize, -1);
|
||||||
|
assert!(fopen(path("b.txt").as_ptr().cast(), c"wb".as_ptr()).is_null());
|
||||||
|
let mut f: File = std::ptr::null_mut();
|
||||||
|
let error = fopen_s(&mut f, path("c.txt").as_ptr().cast(), c"wb".as_ptr());
|
||||||
|
assert_ne!(error, 0);
|
||||||
|
const OUT: i32 = 2;
|
||||||
|
assert!(fiopen(path("d.txt").as_ptr().cast(), OUT, 0x40).is_null());
|
||||||
|
|
||||||
|
// Nothing was made behind a path rewritten twice.
|
||||||
|
let names: Vec<_> = std::fs::read_dir(&folder)
|
||||||
|
.unwrap()
|
||||||
|
.map(|e| e.unwrap().file_name())
|
||||||
|
.collect();
|
||||||
|
assert_eq!(names, ["present.txt"]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user