Minor simplifications to get_path_by_handle.

This commit is contained in:
Dan Gohman
2019-08-13 17:17:34 -07:00
committed by Jakub Konka
parent 8b576779f2
commit 2809be666a

View File

@@ -360,8 +360,7 @@ pub fn get_file_access_mode(handle: RawHandle) -> Result<AccessMode> {
pub fn get_path_by_handle(handle: RawHandle) -> Result<OsString> { pub fn get_path_by_handle(handle: RawHandle) -> Result<OsString> {
use winapi::um::fileapi::GetFinalPathNameByHandleW; use winapi::um::fileapi::GetFinalPathNameByHandleW;
let mut raw_path: Vec<u16> = Vec::with_capacity(WIDE_MAX_PATH as usize); let mut raw_path: Vec<u16> = vec![0; WIDE_MAX_PATH as usize];
raw_path.resize(WIDE_MAX_PATH as usize, 0);
let read_len = let read_len =
unsafe { GetFinalPathNameByHandleW(handle, raw_path.as_mut_ptr(), WIDE_MAX_PATH, 0) }; unsafe { GetFinalPathNameByHandleW(handle, raw_path.as_mut_ptr(), WIDE_MAX_PATH, 0) };
@@ -370,14 +369,14 @@ pub fn get_path_by_handle(handle: RawHandle) -> Result<OsString> {
// failed to read // failed to read
return Err(winerror::WinError::last()); return Err(winerror::WinError::last());
} }
if read_len > WIDE_MAX_PATH {
// path too long (practically probably impossible)
return Err(winerror::WinError::ERROR_BUFFER_OVERFLOW);
}
// concatenate paths // obtain a slice containing the written bytes, and check for it being too long
raw_path.resize(read_len as usize, 0); // (practically probably impossible)
Ok(OsString::from_wide(&raw_path)) let written_bytes = raw_path
.get(..read_len as usize)
.ok_or(winerror::WinError::ERROR_BUFFER_OVERFLOW)?;
Ok(OsString::from_wide(written_bytes))
} }
// Taken from Rust libstd, file libstd/sys/windows/fs.rs // Taken from Rust libstd, file libstd/sys/windows/fs.rs