From 6eac0a95ec07ee675e539a159964b22a08daf670 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 4 Sep 2025 07:40:47 +0000 Subject: [PATCH] Fix OneDrive casefold crash by handling long paths in GetFinalPathNameByHandleW Co-authored-by: sxyazi <17523360+sxyazi@users.noreply.github.com> --- yazi-fs/src/provider/local/casefold.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/yazi-fs/src/provider/local/casefold.rs b/yazi-fs/src/provider/local/casefold.rs index 6b70ef5f..f90e3e8e 100644 --- a/yazi-fs/src/provider/local/casefold.rs +++ b/yazi-fs/src/provider/local/casefold.rs @@ -148,6 +148,28 @@ fn final_path(path: &Path) -> io::Result { if len == 0 { Err(io::Error::last_os_error()) + } else if len as usize > buf.len() { + // Buffer too small, allocate a larger buffer and retry + let mut large_buf = vec![0u16; len as usize]; + let new_len = unsafe { + GetFinalPathNameByHandleW( + file.as_raw_handle() as HANDLE, + large_buf.as_mut_ptr(), + large_buf.len() as u32, + VOLUME_NAME_DOS, + ) + }; + + if new_len == 0 { + Err(io::Error::last_os_error()) + } else if new_len as usize > large_buf.len() { + // Still too large, give up + Err(io::Error::new(io::ErrorKind::InvalidInput, "Path too long")) + } else if large_buf.starts_with(&[92, 92, 63, 92]) { + Ok(PathBuf::from(OsString::from_wide(&large_buf[4..new_len as usize]))) + } else { + Ok(PathBuf::from(OsString::from_wide(&large_buf[0..new_len as usize]))) + } } else if buf.starts_with(&[92, 92, 63, 92]) { Ok(PathBuf::from(OsString::from_wide(&buf[4..len as usize]))) } else {