| 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 | 2774× | pub fn current() -> Platform { |
| 26 | 2774× | Platform { |
| 27 | 2774× | exec_bits: cfg!(unix), |
| 28 | 2774× | symlinks: cfg!(unix), |
| 29 | 2774× | case_insensitive: cfg!(any(target_os = "macos", target_os = "windows")), |
| 30 | 2774× | windows_names: cfg!(target_os = "windows"), |
| 31 | 2774× | } |
| 32 | 2774× | } |
| 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 | 561× | pub fn problems<'a>(&self, paths: impl IntoIterator<Item = &'a str>) -> Vec<String> { |
| 54 | 561× | let paths: Vec<&str> = paths.into_iter().collect(); |
| 55 | 561× | let mut out = Vec::new(); |
| 56 | 561× | 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 | 553× | } |
| 63 | 561× | 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 | 554× | } |
| 70 | 561× | out |
| 71 | 561× | } |
| 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 | 11× | pub fn windows_name_problem(path: &str) -> Option<&'static str> { |
| 105 | | const RESERVED: &[&str] = &[ |
| 106 | | "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", |
| 107 | | "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9", |
| 108 | | ]; |
| 109 | 15× | for part in path.split('/') { |
| 110 | 15× | if part |
| 111 | 15× | .chars() |
| 112 | 82× | .any(|c| matches!(c, '<' | '>' | ':' | '"' | '\\' | '|' | '?' | '*') || (c as u32) < 32) |
| 113 | | { |
| 114 | 1× | return Some("contains a character Windows does not allow in names"); |
| 115 | 14× | } |
| 116 | 14× | if part.ends_with('.') || part.ends_with(' ') { |
| 117 | 1× | return Some("a name ending in a dot or space cannot be created on Windows"); |
| 118 | 13× | } |
| 119 | 13× | let stem = part.split('.').next().unwrap_or("").to_ascii_uppercase(); |
| 120 | 13× | if RESERVED.contains(&stem.as_str()) { |
| 121 | 3× | return Some("a reserved device name on Windows"); |
| 122 | 10× | } |
| 123 | | } |
| 124 | 6× | None |
| 125 | 11× | } |
| 126 | |
|
| 127 | | #[cfg(test)] |
| 128 | | mod tests { |
| 129 | | use super::*; |
| 130 | |
|
| 131 | | #[test] |
| 132 | 1× | fn collisions_and_names() { |
| 133 | 1× | assert_eq!( |
| 134 | 1× | case_collisions(&["README.md", "readme.md", "src/a.rs"]), |
| 135 | 1× | vec![("README.md".into(), "readme.md".into())] |
| 136 | | ); |
| 137 | 1× | assert_eq!( |
| 138 | 1× | case_collisions(&["Docs/a", "docs/b"]), |
| 139 | 1× | vec![("Docs".into(), "docs".into())] |
| 140 | | ); |
| 141 | 1× | assert!(case_collisions(&["a/b", "a/c", "b"]).is_empty()); |
| 142 | 1× | assert!(windows_name_problem("src/con.rs").is_some()); |
| 143 | 1× | assert!(windows_name_problem("notes/a:b.txt").is_some()); |
| 144 | 1× | assert!(windows_name_problem("trailing./x").is_some()); |
| 145 | 1× | assert!(windows_name_problem("src/console.rs").is_none()); |
| 146 | 1× | let w = Platform::windows(); |
| 147 | 1× | assert_eq!(w.problems(["A.txt", "a.txt", "aux.c"]).len(), 2); |
| 148 | 1× | assert!(Platform { |
| 149 | 1× | exec_bits: true, |
| 150 | 1× | symlinks: true, |
| 151 | 1× | case_insensitive: false, |
| 152 | 1× | windows_names: false |
| 153 | 1× | } |
| 154 | 1× | .problems(["A", "a"]) |
| 155 | 1× | .is_empty()); |
| 156 | 1× | } |
| 157 | | } |