VFS: CopyFileW is redirected too

Only CopyFileExW and CopyFile2 were hooked. CopyFileW, and CopyFileA
through it, need not go through CopyFileExW, so a copy between console
paths (T:\f.txt → T:\g.txt) went to the PC's own drive and failed.
CopyFileW is hooked with the same handling as CopyFileExW; when it calls
CopyFileExW, the hook guard lets the inner call through.
This commit is contained in:
gefydev
2026-11-22 20:05:22 -03:00
parent dd713e34f5
commit 935155ca99
+20
View File
@@ -358,12 +358,14 @@ type FnCreateDirW = unsafe extern "system" fn(*const u16, *const c_void) -> i32;
type FnPathW = unsafe extern "system" fn(*const u16) -> i32;
type FnMoveFileExW = unsafe extern "system" fn(*const u16, *const u16, u32) -> i32;
type FnCopyFileExW = unsafe extern "system" fn(*const u16, *const u16, *const c_void, *const c_void, *mut i32, u32) -> i32;
type FnCopyFileW = unsafe extern "system" fn(*const u16, *const u16, i32) -> i32;
type FnCopyFile2 = unsafe extern "system" fn(*const u16, *const u16, *const c_void) -> i32;
static O_CREATE_DIR_W: AtomicUsize = AtomicUsize::new(0);
static O_REMOVE_DIR_W: AtomicUsize = AtomicUsize::new(0);
static O_DELETE_FILE_W: AtomicUsize = AtomicUsize::new(0);
static O_MOVE_FILE_EX_W: AtomicUsize = AtomicUsize::new(0);
static O_COPY_FILE_EX_W: AtomicUsize = AtomicUsize::new(0);
static O_COPY_FILE_W: AtomicUsize = AtomicUsize::new(0);
static O_COPY_FILE_2: AtomicUsize = AtomicUsize::new(0);
unsafe fn bool_result(r: Result<(), u32>) -> i32 {
@@ -491,6 +493,23 @@ unsafe extern "system" fn hk_copy_file_ex_w(src: *const u16, dst: *const u16, pr
orig(wide(&rs).as_ptr(), wide(&rd).as_ptr(), prog, data, cancel, flags)
}
/// CopyFileW (and CopyFileA, which calls it) need not go through CopyFileExW. When it does,
/// the guard lets the inner call through.
unsafe extern "system" fn hk_copy_file_w(src: *const u16, dst: *const u16, fail_if_exists: i32) -> i32 {
let orig = orig!(O_COPY_FILE_W as FnCopyFileW);
let (Some(_g), Some(v)) = (Guard::enter(), vfs::get()) else { return orig(src, dst, fail_if_exists) };
let (Some(s), Some(d)) = (from_wide_ptr(src), from_wide_ptr(dst)) else { return orig(src, dst, fail_if_exists) };
let rs = match source_of(v, &s) {
Ok((p, _)) => p,
Err(e) => return bool_result(Err(e)),
};
let rd = match dest_of(v, &d) {
Ok(p) => p,
Err(e) => return bool_result(Err(e)),
};
orig(wide(&rs).as_ptr(), wide(&rd).as_ptr(), fail_if_exists)
}
unsafe extern "system" fn hk_copy_file_2(src: *const u16, dst: *const u16, params: *const c_void) -> i32 {
let orig = orig!(O_COPY_FILE_2 as FnCopyFile2);
let (Some(_g), Some(v)) = (Guard::enter(), vfs::get()) else { return orig(src, dst, params) };
@@ -754,6 +773,7 @@ pub fn install() -> Result<(), String> {
("DeleteFileW", hk_delete_file_w as usize, &O_DELETE_FILE_W),
("MoveFileExW", hk_move_file_ex_w as usize, &O_MOVE_FILE_EX_W),
("CopyFileExW", hk_copy_file_ex_w as usize, &O_COPY_FILE_EX_W),
("CopyFileW", hk_copy_file_w as usize, &O_COPY_FILE_W),
("CopyFile2", hk_copy_file_2 as usize, &O_COPY_FILE_2),
("FindFirstFileExW", hk_find_first_ex_w as usize, &O_FIND_FIRST_EX_W),
("FindFirstFileW", hk_find_first_w as usize, &O_FIND_FIRST_W),