| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | | use std::collections::BTreeMap; |
| 12 | |
|
| 13 | |
|
| 14 | | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 15 | | pub struct Platform { |
| 16 | | pub exec_bits: bool, |
| 17 | | pub symlinks: bool, |
| 18 | | pub case_insensitive: bool, |
| 19 | | |
| 20 | | pub windows_names: bool, |
| 21 | | } |
| 22 | |
|
| 23 | | impl Platform { |
| 24 | | |
| 25 | 2883× | pub fn current() -> Platform { |
| 26 | 2883× | Platform { |
| 27 | 2883× | exec_bits: cfg!(unix), |
| 28 | 2883× | symlinks: cfg!(unix), |
| 29 | 2883× | case_insensitive: cfg!(any(target_os = "macos", target_os = "windows")), |
| 30 | 2883× | windows_names: cfg!(target_os = "windows"), |
| 31 | 2883× | } |
| 32 | 2883× | } |
| 33 | |
|
| 34 | 8× | pub fn windows() -> Platform { |
| 35 | 8× | Platform { |
| 36 | 8× | exec_bits: false, |
| 37 | 8× | symlinks: false, |
| 38 | 8× | case_insensitive: true, |
| 39 | 8× | windows_names: true, |
| 40 | 8× | } |
| 41 | 8× | } |
| 42 | |
|
| 43 | 1× | pub fn macos() -> Platform { |
| 44 | 1× | Platform { |
| 45 | 1× | exec_bits: true, |
| 46 | 1× | symlinks: true, |
| 47 | 1× | case_insensitive: true, |
| 48 | 1× | windows_names: false, |
| 49 | 1× | } |
| 50 | 1× | } |
| 51 | |
|
| 52 | | |
| 53 | 593× | pub fn problems<'a>(&self, paths: impl IntoIterator<Item = &'a str>) -> Vec<String> { |
| 54 | 593× | let paths: Vec<&str> = paths.into_iter().collect(); |
| 55 | 593× | let mut out = Vec::new(); |
| 56 | 593× | if self.case_insensitive { |
| 57 | 8× | out.extend( |
| 58 | 8× | case_collisions(&paths).into_iter().map(|(a, b)| { |
| 59 | 3× | format!("{a} and {b} differ only in case; one will hide the other") |
| 60 | 3× | }), |
| 61 | | ); |
| 62 | 585× | } |
| 63 | 593× | if self.windows_names { |
| 64 | 7× | out.extend( |
| 65 | 7× | paths |
| 66 | 7× | .iter() |
| 67 | 7× | .filter_map(|p| windows_name_problem(p).map(|why| format!("{p}: {why}"))), |
| 68 | | ); |
| 69 | 586× | } |
| 70 | 593× | out |
| 71 | 593× | } |
| 72 | | } |
| 73 | |
|
| 74 | |
|
| 75 | 11× | pub fn case_collisions(paths: &[&str]) -> Vec<(String, String)> { |
| 76 | 11× | let mut seen: BTreeMap<String, &str> = BTreeMap::new(); |
| 77 | 11× | let mut out = Vec::new(); |
| 78 | | |
| 79 | 11× | let mut prefixes: Vec<&str> = Vec::new(); |
| 80 | 19× | for p in paths { |
| 81 | 19× | let mut end = 0; |
| 82 | 26× | for part in p.split('/') { |
| 83 | 26× | end += part.len(); |
| 84 | 26× | prefixes.push(&p[..end]); |
| 85 | 26× | end += 1; |
| 86 | 26× | } |
| 87 | | } |
| 88 | 11× | prefixes.sort(); |
| 89 | 11× | prefixes.dedup(); |
| 90 | 23× | for p in prefixes { |
| 91 | 23× | let key = p.to_lowercase(); |
| 92 | 23× | match seen.get(&key) { |
| 93 | 5× | Some(first) if *first != p => out.push((first.to_string(), p.to_string())), |
| 94 | 0× | Some(_) => {} |
| 95 | 18× | None => { |
| 96 | 18× | seen.insert(key, p); |
| 97 | 18× | } |
| 98 | | } |
| 99 | | } |
| 100 | 11× | out |
| 101 | 11× | } |
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | 2946× | pub fn canonical(path: &std::path::Path) -> std::io::Result<std::path::PathBuf> { |
| 107 | 2946× | path.canonicalize().map(plain) |
| 108 | 2946× | } |
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | 2953× | pub fn plain(p: std::path::PathBuf) -> std::path::PathBuf { |
| 115 | 2953× | let Some(s) = p.to_str() else { return p }; |
| 116 | 2953× | let unchanged_by_plain = |rest: &str| { |
| 117 | 5× | rest.len() < 260 |
| 118 | 9× | && !rest.split('\\').any(|c| { |
| 119 | 9× | c == "." |
| 120 | 9× | || c == ".." |
| 121 | 9× | || c.ends_with('.') |
| 122 | 8× | || c.ends_with(' ') |
| 123 | 8× | || (!c.is_empty() && windows_name_problem(c).is_some()) |
| 124 | 9× | }) |
| 125 | 5× | }; |
| 126 | 2953× | if let Some(rest) = s.strip_prefix(r"\\?\UNC\") { |
| 127 | 1× | if unchanged_by_plain(rest) { |
| 128 | 1× | return std::path::PathBuf::from(format!(r"\\{rest}")); |
| 129 | 0× | } |
| 130 | 2952× | } else if let Some(rest) = s.strip_prefix(r"\\?\") { |
| 131 | 5× | let b = rest.as_bytes(); |
| 132 | 5× | let drive = b.len() >= 3 && b[0].is_ascii_alphabetic() && b[1] == b':' && b[2] == b'\\'; |
| 133 | 5× | if drive && unchanged_by_plain(&rest[3..]) { |
| 134 | 1× | return std::path::PathBuf::from(rest); |
| 135 | 4× | } |
| 136 | 2947× | } |
| 137 | 2951× | p |
| 138 | 2953× | } |
| 139 | |
|
| 140 | |
|
| 141 | 19× | pub fn windows_name_problem(path: &str) -> Option<&'static str> { |
| 142 | | const RESERVED: &[&str] = &[ |
| 143 | | "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", |
| 144 | | "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9", |
| 145 | | ]; |
| 146 | 23× | for part in path.split('/') { |
| 147 | 23× | if part |
| 148 | 23× | .chars() |
| 149 | 116× | .any(|c| matches!(c, '<' | '>' | ':' | '"' | '\\' | '|' | '?' | '*') || (c as u32) < 32) |
| 150 | | { |
| 151 | 1× | return Some("contains a character Windows does not allow in names"); |
| 152 | 22× | } |
| 153 | 22× | if part.ends_with('.') || part.ends_with(' ') { |
| 154 | 1× | return Some("a name ending in a dot or space cannot be created on Windows"); |
| 155 | 21× | } |
| 156 | 21× | let stem = part.split('.').next().unwrap_or("").to_ascii_uppercase(); |
| 157 | 21× | if RESERVED.contains(&stem.as_str()) { |
| 158 | 4× | return Some("a reserved device name on Windows"); |
| 159 | 17× | } |
| 160 | | } |
| 161 | 13× | None |
| 162 | 19× | } |
| 163 | |
|
| 164 | | #[cfg(test)] |
| 165 | | mod tests { |
| 166 | | use super::*; |
| 167 | |
|
| 168 | | |
| 169 | | |
| 170 | | #[test] |
| 171 | 1× | fn a_verbatim_windows_path_is_shown_plain_when_it_can_be() { |
| 172 | | use std::path::PathBuf; |
| 173 | 7× | let p = |s: &str| plain(PathBuf::from(s)).to_string_lossy().into_owned(); |
| 174 | 1× | assert_eq!(p(r"\\?\C:\work\demo"), r"C:\work\demo"); |
| 175 | 1× | assert_eq!(p(r"\\?\UNC\server\share\repo"), r"\\server\share\repo"); |
| 176 | 1× | assert_eq!( |
| 177 | 1× | p(r"\\?\C:\work\CON"), |
| 178 | | r"\\?\C:\work\CON", |
| 179 | | "a reserved name stays verbatim" |
| 180 | | ); |
| 181 | 1× | assert_eq!(p(r"\\?\C:\work\trailing."), r"\\?\C:\work\trailing."); |
| 182 | 1× | assert_eq!( |
| 183 | 1× | p(r"\\?\Volume{1234}\x"), |
| 184 | | r"\\?\Volume{1234}\x", |
| 185 | | "not a drive path" |
| 186 | | ); |
| 187 | 1× | let long = format!(r"\\?\C:\{}", "a".repeat(300)); |
| 188 | 1× | assert_eq!(p(&long), long, "past the old length limit"); |
| 189 | 1× | assert_eq!( |
| 190 | 1× | p("/home/andy/repo"), |
| 191 | | "/home/andy/repo", |
| 192 | | "nothing to do elsewhere" |
| 193 | | ); |
| 194 | 1× | } |
| 195 | |
|
| 196 | | #[test] |
| 197 | 1× | fn collisions_and_names() { |
| 198 | 1× | assert_eq!( |
| 199 | 1× | case_collisions(&["README.md", "readme.md", "src/a.rs"]), |
| 200 | 1× | vec![("README.md".into(), "readme.md".into())] |
| 201 | | ); |
| 202 | 1× | assert_eq!( |
| 203 | 1× | case_collisions(&["Docs/a", "docs/b"]), |
| 204 | 1× | vec![("Docs".into(), "docs".into())] |
| 205 | | ); |
| 206 | 1× | assert!(case_collisions(&["a/b", "a/c", "b"]).is_empty()); |
| 207 | 1× | assert!(windows_name_problem("src/con.rs").is_some()); |
| 208 | 1× | assert!(windows_name_problem("notes/a:b.txt").is_some()); |
| 209 | 1× | assert!(windows_name_problem("trailing./x").is_some()); |
| 210 | 1× | assert!(windows_name_problem("src/console.rs").is_none()); |
| 211 | 1× | let w = Platform::windows(); |
| 212 | 1× | assert_eq!(w.problems(["A.txt", "a.txt", "aux.c"]).len(), 2); |
| 213 | 1× | assert!(Platform { |
| 214 | 1× | exec_bits: true, |
| 215 | 1× | symlinks: true, |
| 216 | 1× | case_insensitive: false, |
| 217 | 1× | windows_names: false |
| 218 | 1× | } |
| 219 | 1× | .problems(["A", "a"]) |
| 220 | 1× | .is_empty()); |
| 221 | 1× | } |
| 222 | | } |