From 0850974860af4cc848a60ef2c20391a3b066df9b Mon Sep 17 00:00:00 2001 From: Zhenping Zhao Date: Sat, 23 Nov 2024 11:49:51 -0800 Subject: [PATCH] Replace crate winapi with path_abs to simplify getting full path name, eliminate one segment of unsafe code block --- crates/nu-path/Cargo.toml | 2 +- crates/nu-path/src/pwd_per_drive.rs | 38 +++++------------------------ 2 files changed, 7 insertions(+), 33 deletions(-) diff --git a/crates/nu-path/Cargo.toml b/crates/nu-path/Cargo.toml index 178e62b090..2a81c7daad 100644 --- a/crates/nu-path/Cargo.toml +++ b/crates/nu-path/Cargo.toml @@ -16,7 +16,7 @@ dirs = { workspace = true } [target.'cfg(windows)'.dependencies] omnipath = { workspace = true } -winapi = { version = "0.3.9", features = ["fileapi"] } +path_abs = "0.5.1" [target.'cfg(all(unix, not(target_os = "macos"), not(target_os = "android")))'.dependencies] pwd = { workspace = true } diff --git a/crates/nu-path/src/pwd_per_drive.rs b/crates/nu-path/src/pwd_per_drive.rs index 10be4a5ac7..30ac248d84 100644 --- a/crates/nu-path/src/pwd_per_drive.rs +++ b/crates/nu-path/src/pwd_per_drive.rs @@ -79,40 +79,14 @@ pub mod _impl { } } - // Wrapper of Win32 API GetFullPathW() + use path_abs::{PathAbs, PathInfo}; + fn get_full_path_name_w(path_str: &str) -> Option { - use std::ffi::OsString; - use std::os::windows::ffi::OsStrExt; - use std::os::windows::ffi::OsStringExt; - use winapi::um::fileapi::GetFullPathNameW; - - const MAX_PATH: usize = 260; - let mut buffer: [u16; MAX_PATH] = [0; MAX_PATH]; - - unsafe { - // Convert input to wide string. - let wide_path: Vec = OsString::from(path_str) - .encode_wide() - .chain(Some(0)) - .collect(); - let length = GetFullPathNameW( - wide_path.as_ptr(), - buffer.len() as u32, - buffer.as_mut_ptr(), - std::ptr::null_mut(), - ); - - if length > 0 && (length as usize) < MAX_PATH { - let path = OsString::from_wide(&buffer[..length as usize]); - if let Some(path_str) = path.to_str() { - let path_string = path_str.to_string(); - { - return Some(path_string); - } - } - } + if let Ok(path_abs) = PathAbs::new(path_str) { + Some(path_abs.to_str()?.to_string()) + } else { + None } - None } /// Global singleton instance of DrivePwdMap