|
|
|
@ -1,15 +1,14 @@
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
pub mod _impl {
|
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
|
|
|
|
|
cfg_if::cfg_if! { if #[cfg(windows)] {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct Drive2PWDmap {
|
|
|
|
|
struct Drive2PWD {
|
|
|
|
|
map: [Option<String>; 26], // Fixed-size array for A-Z
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Drive2PWDmap {
|
|
|
|
|
impl Drive2PWD {
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
|
Drive2PWDmap {
|
|
|
|
|
Drive2PWD {
|
|
|
|
|
map: Default::default(), // Initialize all to `None`
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -17,10 +16,9 @@ impl Drive2PWDmap {
|
|
|
|
|
/// Set the PWD for the drive letter in the absolute path.
|
|
|
|
|
/// Return String for error description.
|
|
|
|
|
pub fn set_pwd(&mut self, path: &Path) -> Result<(), String> {
|
|
|
|
|
if let (Some(drive_letter), Some(path_str)) = (
|
|
|
|
|
Self::extract_drive_letter(path),
|
|
|
|
|
path.to_str(),
|
|
|
|
|
) {
|
|
|
|
|
if let (Some(drive_letter), Some(path_str)) =
|
|
|
|
|
(Self::extract_drive_letter(path), path.to_str())
|
|
|
|
|
{
|
|
|
|
|
self.map[drive_letter as usize - 'A' as usize] = Some(path_str.to_string());
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
@ -33,17 +31,14 @@ impl Drive2PWDmap {
|
|
|
|
|
if drive.is_ascii_alphabetic() {
|
|
|
|
|
let drive = drive.to_ascii_uppercase();
|
|
|
|
|
let index = drive as usize - 'A' as usize;
|
|
|
|
|
Some(self.map[index]
|
|
|
|
|
.clone()
|
|
|
|
|
.unwrap_or_else(||
|
|
|
|
|
Some(self.map[index].clone().unwrap_or_else(|| {
|
|
|
|
|
if let Some(system_pwd) = get_full_path_name_w(&format!("{}:", drive)) {
|
|
|
|
|
self.map[index] = Some(system_pwd.clone());
|
|
|
|
|
system_pwd
|
|
|
|
|
} else {
|
|
|
|
|
format!(r"{}:\", drive)
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
}))
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
@ -59,7 +54,7 @@ impl Drive2PWDmap {
|
|
|
|
|
// Combine current PWD with the relative path
|
|
|
|
|
let mut base = PathBuf::from(Self::ensure_trailing_separator(&pwd));
|
|
|
|
|
base.push(path_str.split_at(2).1); // Skip the "C:" part of the relative path
|
|
|
|
|
return Some(base)
|
|
|
|
|
return Some(base);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
None // Invalid path or has no drive letter
|
|
|
|
@ -67,9 +62,10 @@ impl Drive2PWDmap {
|
|
|
|
|
|
|
|
|
|
/// Extract the drive letter from a path (e.g., `C:test` -> `C`)
|
|
|
|
|
fn extract_drive_letter(path: &Path) -> Option<char> {
|
|
|
|
|
Some(path.to_str()
|
|
|
|
|
Some(
|
|
|
|
|
path.to_str()
|
|
|
|
|
.and_then(|s| s.chars().next())
|
|
|
|
|
.filter(|c| c.is_ascii_alphabetic())?
|
|
|
|
|
.filter(|c| c.is_ascii_alphabetic())?,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -83,11 +79,11 @@ impl Drive2PWDmap {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetFullPathW
|
|
|
|
|
// Wrapper of Win32 API GetFullPathW()
|
|
|
|
|
fn get_full_path_name_w(path_str: &str) -> Option<String> {
|
|
|
|
|
use std::ffi::OsString;
|
|
|
|
|
use std::os::windows::ffi::OsStringExt;
|
|
|
|
|
use std::os::windows::ffi::OsStrExt;
|
|
|
|
|
use std::os::windows::ffi::OsStringExt;
|
|
|
|
|
use winapi::um::fileapi::GetFullPathNameW;
|
|
|
|
|
|
|
|
|
|
const MAX_PATH: usize = 260;
|
|
|
|
@ -95,7 +91,10 @@ fn get_full_path_name_w(path_str: &str) -> Option<String> {
|
|
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
|
// Convert input to wide string.
|
|
|
|
|
let wide_path: Vec<u16> = OsString::from(path_str).encode_wide().chain(Some(0)).collect();
|
|
|
|
|
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,
|
|
|
|
@ -117,16 +116,16 @@ fn get_full_path_name_w(path_str: &str) -> Option<String> {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Global singleton instance of DrivePwdMap
|
|
|
|
|
use std::sync::{Once, Mutex};
|
|
|
|
|
use std::sync::{Mutex, Once};
|
|
|
|
|
|
|
|
|
|
static INIT: Once = Once::new();
|
|
|
|
|
static mut DRIVE_PWD_MAP: Option<Mutex<Drive2PWDmap>> = None;
|
|
|
|
|
static mut DRIVE_PWD_MAP: Option<Mutex<Drive2PWD>> = None;
|
|
|
|
|
|
|
|
|
|
/// Access the singleton instance
|
|
|
|
|
fn get_drive_pwd_map() -> &'static Mutex<Drive2PWDmap> {
|
|
|
|
|
fn get_drive_pwd_map() -> &'static Mutex<Drive2PWD> {
|
|
|
|
|
unsafe {
|
|
|
|
|
INIT.call_once(|| {
|
|
|
|
|
DRIVE_PWD_MAP = Some(Mutex::new(Drive2PWDmap::new()));
|
|
|
|
|
DRIVE_PWD_MAP = Some(Mutex::new(Drive2PWD::new()));
|
|
|
|
|
});
|
|
|
|
|
DRIVE_PWD_MAP.as_ref().unwrap()
|
|
|
|
|
}
|
|
|
|
@ -170,7 +169,7 @@ mod tests {
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_expand_path() {
|
|
|
|
|
let mut drive_map = Drive2PWDmap::new();
|
|
|
|
|
let mut drive_map = Drive2PWD::new();
|
|
|
|
|
|
|
|
|
|
// Set PWD for drive C
|
|
|
|
|
assert_eq!(drive_map.set_pwd(Path::new(r"C:\Users\Home")), Ok(()));
|
|
|
|
@ -190,7 +189,13 @@ mod tests {
|
|
|
|
|
// Expand with no PWD set for the drive
|
|
|
|
|
let expanded = drive_map.expand_path(Path::new("D:test"));
|
|
|
|
|
if let Some(pwd_on_d) = get_full_path_name_w("D:") {
|
|
|
|
|
assert_eq!(expanded, Some(PathBuf::from(format!(r"{}test", Drive2PWDmap::ensure_trailing_separator(&pwd_on_d)))));
|
|
|
|
|
assert_eq!(
|
|
|
|
|
expanded,
|
|
|
|
|
Some(PathBuf::from(format!(
|
|
|
|
|
r"{}test",
|
|
|
|
|
Drive2PWD::ensure_trailing_separator(&pwd_on_d)
|
|
|
|
|
)))
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
assert_eq!(expanded, Some(PathBuf::from(r"D:\test")));
|
|
|
|
|
}
|
|
|
|
@ -198,11 +203,14 @@ mod tests {
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_set_and_get_pwd() {
|
|
|
|
|
let mut drive_map = Drive2PWDmap::new();
|
|
|
|
|
let mut drive_map = Drive2PWD::new();
|
|
|
|
|
|
|
|
|
|
// Set PWD for drive C
|
|
|
|
|
assert!(drive_map.set_pwd(Path::new(r"C:\Users\Example")).is_ok());
|
|
|
|
|
assert_eq!(drive_map.get_pwd('C'), Some(r"C:\Users\Example".to_string()));
|
|
|
|
|
assert_eq!(
|
|
|
|
|
drive_map.get_pwd('C'),
|
|
|
|
|
Some(r"C:\Users\Example".to_string())
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Set PWD for drive D
|
|
|
|
|
assert!(drive_map.set_pwd(Path::new(r"D:\Projects")).is_ok());
|
|
|
|
@ -223,7 +231,7 @@ mod tests {
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_set_pwd_invalid_path() {
|
|
|
|
|
let mut drive_map = Drive2PWDmap::new();
|
|
|
|
|
let mut drive_map = Drive2PWD::new();
|
|
|
|
|
|
|
|
|
|
// Invalid path (no drive letter)
|
|
|
|
|
let result = drive_map.set_pwd(Path::new(r"\InvalidPath"));
|
|
|
|
@ -233,7 +241,7 @@ mod tests {
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_get_pwd_invalid_drive() {
|
|
|
|
|
let mut drive_map = Drive2PWDmap::new();
|
|
|
|
|
let mut drive_map = Drive2PWD::new();
|
|
|
|
|
|
|
|
|
|
// Get PWD for a drive not set (e.g., Z)
|
|
|
|
|
assert_eq!(drive_map.get_pwd('Z'), Some(r"Z:\".to_string()));
|
|
|
|
@ -243,8 +251,6 @@ mod tests {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}} // cfg_if! if #[cfg(windows)]
|
|
|
|
|
|
|
|
|
|
/// Usage for pwd_per_drive on windows
|
|
|
|
|
///
|
|
|
|
|
/// Upon change PWD, call set_pwd_per_drive() with absolute path
|
|
|
|
@ -253,114 +259,86 @@ mod tests {
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::path::{Path, PathBuf};
|
|
|
|
|
/// use nu_path::{expand_pwd_per_drive, set_pwd_per_drive};
|
|
|
|
|
/// use nu_path::{expand_pwd, set_pwd};
|
|
|
|
|
///
|
|
|
|
|
/// //assert!(false); // Comment out to verify really tested
|
|
|
|
|
/// if cfg!(windows) {
|
|
|
|
|
/// // Set PWD for drive C
|
|
|
|
|
/// set_pwd_per_drive(Path::new(r"C:\Users\Home")).unwrap();
|
|
|
|
|
/// set_pwd(Path::new(r"C:\Users\Home")).unwrap();
|
|
|
|
|
///
|
|
|
|
|
/// // Expand a relative path
|
|
|
|
|
/// let expanded = expand_pwd_per_drive(Path::new("C:test"));
|
|
|
|
|
/// let expanded = expand_pwd(Path::new("C:test"));
|
|
|
|
|
/// assert_eq!(expanded, Some(PathBuf::from(r"C:\Users\Home\test")));
|
|
|
|
|
///
|
|
|
|
|
/// // Will NOT expand an absolute path
|
|
|
|
|
/// let expanded = expand_pwd_per_drive(Path::new(r"C:\absolute\path"));
|
|
|
|
|
/// let expanded = expand_pwd(Path::new(r"C:\absolute\path"));
|
|
|
|
|
/// assert_eq!(expanded, None);
|
|
|
|
|
///
|
|
|
|
|
/// // Expand with no drive letter
|
|
|
|
|
/// let expanded = expand_pwd_per_drive(Path::new(r"\no_drive"));
|
|
|
|
|
/// let expanded = expand_pwd(Path::new(r"\no_drive"));
|
|
|
|
|
/// assert_eq!(expanded, None);
|
|
|
|
|
///
|
|
|
|
|
/// // Expand with no PWD set for the drive
|
|
|
|
|
/// let expanded = expand_pwd_per_drive(Path::new("D:test"));
|
|
|
|
|
/// let expanded = expand_pwd(Path::new("D:test"));
|
|
|
|
|
/// assert_eq!(expanded, Some(PathBuf::from(r"D:\test")));
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
|
|
|
|
pub mod pwd_per_drive_singleton {
|
|
|
|
|
pub mod singleton {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
/// set_pwd_per_drive
|
|
|
|
|
/// On Windows, record PWD for drive, path must be absolute path
|
|
|
|
|
/// Record PWD for drive, path must be absolute path
|
|
|
|
|
/// return Ok(()) if succeeded, otherwise error message
|
|
|
|
|
/// Other platforms, return Ok(())
|
|
|
|
|
pub fn set_pwd_per_drive(_path: &Path) -> Result<(), String> {
|
|
|
|
|
cfg_if::cfg_if! { if #[cfg(target_os="windows")] {
|
|
|
|
|
|
|
|
|
|
pub fn set_pwd(_path: &Path) -> Result<(), String> {
|
|
|
|
|
if let Ok(mut pwd_per_drive) = get_drive_pwd_map().lock() {
|
|
|
|
|
pwd_per_drive.set_pwd(_path)
|
|
|
|
|
} else {
|
|
|
|
|
Err("Failed to lock map".to_string())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
|
|
|
|
}}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// expand_pwe_per_drive
|
|
|
|
|
/// On windows, input relative path, expand PWD-per-drive to construct absolute path
|
|
|
|
|
/// Input relative path, expand PWD-per-drive to construct absolute path
|
|
|
|
|
/// return PathBuf for absolute path, None if input path is invalid.
|
|
|
|
|
/// Otherwise, return None.
|
|
|
|
|
pub fn expand_pwd_per_drive(_path: &Path) -> Option<PathBuf> {
|
|
|
|
|
cfg_if::cfg_if! { if #[cfg(target_os="windows")] {
|
|
|
|
|
|
|
|
|
|
pub fn expand_pwd(_path: &Path) -> Option<PathBuf> {
|
|
|
|
|
if need_expand_pwd_per_drive(_path) {
|
|
|
|
|
if let Ok(mut pwd_per_drive) = get_drive_pwd_map().lock() {
|
|
|
|
|
return pwd_per_drive.expand_path(_path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}}
|
|
|
|
|
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cfg_if::cfg_if! { if #[cfg(target_os="windows")] {
|
|
|
|
|
/// Helper only used on Windows, if input path is relative path
|
|
|
|
|
/// with drive letter, it can be expanded with PWD-per-drive.
|
|
|
|
|
fn need_expand_pwd_per_drive(_path: &Path) -> bool {
|
|
|
|
|
if let Some(path_str) = _path.to_str() {
|
|
|
|
|
let chars: Vec<char> = path_str.chars().collect();
|
|
|
|
|
if chars.len() >= 2 {
|
|
|
|
|
return chars[1] == ':' &&
|
|
|
|
|
(chars.len() == 2 ||
|
|
|
|
|
(chars[2] != '/' &&
|
|
|
|
|
chars[2] != '\\'
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
return chars[1] == ':'
|
|
|
|
|
&& (chars.len() == 2 || (chars[2] != '/' && chars[2] != '\\'));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_usage_for_pwd_per_drive() {
|
|
|
|
|
// Set PWD for drive F
|
|
|
|
|
assert!(set_pwd_per_drive(Path::new(r"F:\Users\Home")).is_ok());
|
|
|
|
|
assert!(set_pwd(Path::new(r"F:\Users\Home")).is_ok());
|
|
|
|
|
|
|
|
|
|
if cfg!(windows) {
|
|
|
|
|
// Expand a relative path
|
|
|
|
|
let expanded = expand_pwd_per_drive(Path::new("f:test"));
|
|
|
|
|
let expanded = expand_pwd(Path::new("f:test"));
|
|
|
|
|
assert_eq!(expanded, Some(PathBuf::from(r"F:\Users\Home\test")));
|
|
|
|
|
|
|
|
|
|
// Will NOT expand an absolute path
|
|
|
|
|
let expanded = expand_pwd_per_drive(Path::new(r"F:\absolute\path"));
|
|
|
|
|
let expanded = expand_pwd(Path::new(r"F:\absolute\path"));
|
|
|
|
|
assert_eq!(expanded, None);
|
|
|
|
|
|
|
|
|
|
// Expand with no drive letter
|
|
|
|
|
let expanded = expand_pwd_per_drive(Path::new(r"\no_drive"));
|
|
|
|
|
let expanded = expand_pwd(Path::new(r"\no_drive"));
|
|
|
|
|
assert_eq!(expanded, None);
|
|
|
|
|
|
|
|
|
|
// Expand with no PWD set for the drive
|
|
|
|
|
let expanded = expand_pwd_per_drive(Path::new("G:test"));
|
|
|
|
|
let expanded = expand_pwd(Path::new("G:test"));
|
|
|
|
|
assert_eq!(expanded, Some(PathBuf::from(r"G:\test")));
|
|
|
|
|
} else {
|
|
|
|
|
// None always
|
|
|
|
|
assert_eq!(None, expand_pwd_per_drive(Path::new("F:test")));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|