mirror of
https://github.com/nushell/nushell.git
synced 2025-08-12 08:29:52 +02:00
Replace crate winapi with path_abs to simplify getting full path name, eliminate one segment of unsafe code block
This commit is contained in:
@ -16,7 +16,7 @@ dirs = { workspace = true }
|
|||||||
|
|
||||||
[target.'cfg(windows)'.dependencies]
|
[target.'cfg(windows)'.dependencies]
|
||||||
omnipath = { workspace = true }
|
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]
|
[target.'cfg(all(unix, not(target_os = "macos"), not(target_os = "android")))'.dependencies]
|
||||||
pwd = { workspace = true }
|
pwd = { workspace = true }
|
||||||
|
@ -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<String> {
|
fn get_full_path_name_w(path_str: &str) -> Option<String> {
|
||||||
use std::ffi::OsString;
|
if let Ok(path_abs) = PathAbs::new(path_str) {
|
||||||
use std::os::windows::ffi::OsStrExt;
|
Some(path_abs.to_str()?.to_string())
|
||||||
use std::os::windows::ffi::OsStringExt;
|
} else {
|
||||||
use winapi::um::fileapi::GetFullPathNameW;
|
None
|
||||||
|
|
||||||
const MAX_PATH: usize = 260;
|
|
||||||
let mut buffer: [u16; MAX_PATH] = [0; MAX_PATH];
|
|
||||||
|
|
||||||
unsafe {
|
|
||||||
// Convert input to wide string.
|
|
||||||
let wide_path: Vec<u16> = 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
None
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Global singleton instance of DrivePwdMap
|
/// Global singleton instance of DrivePwdMap
|
||||||
|
Reference in New Issue
Block a user