|
| 1 | +use std::path::Path; |
| 2 | +use std::path::PathBuf; |
| 3 | + |
| 4 | +use crate::env; |
| 5 | + |
| 6 | +pub fn normalize_for_path_comparison(path: &Path) -> std::io::Result<PathBuf> { |
| 7 | + let canonical = path.canonicalize()?; |
| 8 | + Ok(normalize_for_wsl(canonical)) |
| 9 | +} |
| 10 | + |
| 11 | +fn normalize_for_wsl(path: PathBuf) -> PathBuf { |
| 12 | + normalize_for_wsl_with_flag(path, env::is_wsl()) |
| 13 | +} |
| 14 | + |
| 15 | +fn normalize_for_wsl_with_flag(path: PathBuf, is_wsl: bool) -> PathBuf { |
| 16 | + if !is_wsl { |
| 17 | + return path; |
| 18 | + } |
| 19 | + |
| 20 | + if !is_wsl_case_insensitive_path(&path) { |
| 21 | + return path; |
| 22 | + } |
| 23 | + |
| 24 | + lower_ascii_path(path) |
| 25 | +} |
| 26 | + |
| 27 | +fn is_wsl_case_insensitive_path(path: &Path) -> bool { |
| 28 | + #[cfg(target_os = "linux")] |
| 29 | + { |
| 30 | + use std::os::unix::ffi::OsStrExt; |
| 31 | + use std::path::Component; |
| 32 | + |
| 33 | + let mut components = path.components(); |
| 34 | + let Some(Component::RootDir) = components.next() else { |
| 35 | + return false; |
| 36 | + }; |
| 37 | + let Some(Component::Normal(mnt)) = components.next() else { |
| 38 | + return false; |
| 39 | + }; |
| 40 | + if !ascii_eq_ignore_case(mnt.as_bytes(), b"mnt") { |
| 41 | + return false; |
| 42 | + } |
| 43 | + let Some(Component::Normal(drive)) = components.next() else { |
| 44 | + return false; |
| 45 | + }; |
| 46 | + let drive_bytes = drive.as_bytes(); |
| 47 | + drive_bytes.len() == 1 && drive_bytes[0].is_ascii_alphabetic() |
| 48 | + } |
| 49 | + #[cfg(not(target_os = "linux"))] |
| 50 | + { |
| 51 | + let _ = path; |
| 52 | + false |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +#[cfg(target_os = "linux")] |
| 57 | +fn ascii_eq_ignore_case(left: &[u8], right: &[u8]) -> bool { |
| 58 | + left.len() == right.len() |
| 59 | + && left |
| 60 | + .iter() |
| 61 | + .zip(right) |
| 62 | + .all(|(lhs, rhs)| lhs.to_ascii_lowercase() == *rhs) |
| 63 | +} |
| 64 | + |
| 65 | +#[cfg(target_os = "linux")] |
| 66 | +fn lower_ascii_path(path: PathBuf) -> PathBuf { |
| 67 | + use std::ffi::OsString; |
| 68 | + use std::os::unix::ffi::OsStrExt; |
| 69 | + use std::os::unix::ffi::OsStringExt; |
| 70 | + |
| 71 | + // WSL mounts Windows drives under /mnt/<drive>, which are case-insensitive. |
| 72 | + let bytes = path.as_os_str().as_bytes(); |
| 73 | + let mut lowered = Vec::with_capacity(bytes.len()); |
| 74 | + for byte in bytes { |
| 75 | + lowered.push(byte.to_ascii_lowercase()); |
| 76 | + } |
| 77 | + PathBuf::from(OsString::from_vec(lowered)) |
| 78 | +} |
| 79 | + |
| 80 | +#[cfg(not(target_os = "linux"))] |
| 81 | +fn lower_ascii_path(path: PathBuf) -> PathBuf { |
| 82 | + path |
| 83 | +} |
| 84 | + |
| 85 | +#[cfg(test)] |
| 86 | +mod tests { |
| 87 | + #[cfg(target_os = "linux")] |
| 88 | + mod wsl { |
| 89 | + use super::super::normalize_for_wsl_with_flag; |
| 90 | + use pretty_assertions::assert_eq; |
| 91 | + use std::path::PathBuf; |
| 92 | + |
| 93 | + #[test] |
| 94 | + fn wsl_mnt_drive_paths_lowercase() { |
| 95 | + let normalized = normalize_for_wsl_with_flag(PathBuf::from("/mnt/C/Users/Dev"), true); |
| 96 | + |
| 97 | + assert_eq!(normalized, PathBuf::from("/mnt/c/users/dev")); |
| 98 | + } |
| 99 | + |
| 100 | + #[test] |
| 101 | + fn wsl_non_drive_paths_unchanged() { |
| 102 | + let path = PathBuf::from("/mnt/cc/Users/Dev"); |
| 103 | + let normalized = normalize_for_wsl_with_flag(path.clone(), true); |
| 104 | + |
| 105 | + assert_eq!(normalized, path); |
| 106 | + } |
| 107 | + |
| 108 | + #[test] |
| 109 | + fn wsl_non_mnt_paths_unchanged() { |
| 110 | + let path = PathBuf::from("/home/Dev"); |
| 111 | + let normalized = normalize_for_wsl_with_flag(path.clone(), true); |
| 112 | + |
| 113 | + assert_eq!(normalized, path); |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments