Skip to main content

zccache_core/
path.rs

1//! Cross-platform path utilities.
2//!
3//! Handles path normalization, case sensitivity, and platform differences.
4
5use std::cmp::Ordering;
6use std::ffi::OsStr;
7use std::hash::{Hash, Hasher};
8use std::ops::Deref;
9use std::path::{Path, PathBuf};
10
11use serde::{Deserialize, Deserializer, Serialize, Serializer};
12
13/// A normalized, platform-aware path representation.
14///
15/// On case-insensitive filesystems (Windows, default macOS), paths are
16/// stored in a canonical form for consistent cache keying.
17#[derive(Debug, Clone)]
18pub struct NormalizedPath {
19    /// The original path, normalized but preserving original casing.
20    path: PathBuf,
21    /// Lowercased version for case-insensitive comparison, if applicable.
22    case_key: Option<String>,
23}
24
25impl PartialEq for NormalizedPath {
26    fn eq(&self, other: &Self) -> bool {
27        normalize_for_key(&self.path) == normalize_for_key(&other.path)
28    }
29}
30
31impl PartialEq<PathBuf> for NormalizedPath {
32    fn eq(&self, other: &PathBuf) -> bool {
33        self == &Self::new(other)
34    }
35}
36
37impl PartialEq<NormalizedPath> for PathBuf {
38    fn eq(&self, other: &NormalizedPath) -> bool {
39        other == self
40    }
41}
42
43impl PartialEq<Path> for NormalizedPath {
44    fn eq(&self, other: &Path) -> bool {
45        self == &Self::new(other)
46    }
47}
48
49impl PartialEq<&Path> for NormalizedPath {
50    fn eq(&self, other: &&Path) -> bool {
51        self == *other
52    }
53}
54
55impl PartialEq<NormalizedPath> for Path {
56    fn eq(&self, other: &NormalizedPath) -> bool {
57        other == self
58    }
59}
60
61impl PartialEq<&NormalizedPath> for Path {
62    fn eq(&self, other: &&NormalizedPath) -> bool {
63        *other == self
64    }
65}
66
67impl PartialEq<&PathBuf> for NormalizedPath {
68    fn eq(&self, other: &&PathBuf) -> bool {
69        self == *other
70    }
71}
72
73impl PartialEq<&NormalizedPath> for PathBuf {
74    fn eq(&self, other: &&NormalizedPath) -> bool {
75        *other == self
76    }
77}
78
79impl Eq for NormalizedPath {}
80
81impl Hash for NormalizedPath {
82    fn hash<H: Hasher>(&self, state: &mut H) {
83        normalize_for_key(&self.path).hash(state);
84    }
85}
86
87impl PartialOrd for NormalizedPath {
88    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
89        Some(self.cmp(other))
90    }
91}
92
93impl Ord for NormalizedPath {
94    fn cmp(&self, other: &Self) -> Ordering {
95        normalize_for_key(&self.path).cmp(&normalize_for_key(&other.path))
96    }
97}
98
99impl NormalizedPath {
100    /// Create a new normalized path.
101    ///
102    /// On Windows, this also computes a lowercase key for case-insensitive matching.
103    pub fn new(path: impl AsRef<Path>) -> Self {
104        let path = normalize(path.as_ref());
105        let case_key = if cfg!(windows) || cfg!(target_os = "macos") {
106            Some(normalize_for_key(&path))
107        } else {
108            None
109        };
110        Self { path, case_key }
111    }
112
113    /// Returns the underlying path.
114    #[must_use]
115    pub fn as_path(&self) -> &Path {
116        &self.path
117    }
118
119    /// Returns the case-insensitive comparison key, if applicable.
120    #[must_use]
121    pub fn case_key(&self) -> Option<&str> {
122        self.case_key.as_deref()
123    }
124
125    /// Convert back to an owned normalized `PathBuf`.
126    #[must_use]
127    pub fn into_path_buf(self) -> PathBuf {
128        self.path
129    }
130
131    /// Join a path segment onto this normalized path.
132    #[must_use]
133    pub fn join(&self, path: impl AsRef<Path>) -> Self {
134        Self::new(self.path.join(path))
135    }
136}
137
138impl AsRef<Path> for NormalizedPath {
139    fn as_ref(&self) -> &Path {
140        self.as_path()
141    }
142}
143
144impl AsRef<OsStr> for NormalizedPath {
145    fn as_ref(&self) -> &OsStr {
146        self.as_path().as_os_str()
147    }
148}
149
150impl Deref for NormalizedPath {
151    type Target = Path;
152
153    fn deref(&self) -> &Self::Target {
154        self.as_path()
155    }
156}
157
158impl From<PathBuf> for NormalizedPath {
159    fn from(path: PathBuf) -> Self {
160        Self::new(path)
161    }
162}
163
164impl From<&Path> for NormalizedPath {
165    fn from(path: &Path) -> Self {
166        Self::new(path)
167    }
168}
169
170impl From<String> for NormalizedPath {
171    fn from(path: String) -> Self {
172        Self::new(path)
173    }
174}
175
176impl From<&str> for NormalizedPath {
177    fn from(path: &str) -> Self {
178        Self::new(path)
179    }
180}
181
182impl From<&String> for NormalizedPath {
183    fn from(path: &String) -> Self {
184        Self::new(path)
185    }
186}
187
188impl Serialize for NormalizedPath {
189    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
190    where
191        S: Serializer,
192    {
193        self.path.serialize(serializer)
194    }
195}
196
197impl<'de> Deserialize<'de> for NormalizedPath {
198    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
199    where
200        D: Deserializer<'de>,
201    {
202        PathBuf::deserialize(deserializer).map(Self::new)
203    }
204}
205
206/// Normalize a path by resolving `.` and `..` components without
207/// touching the filesystem (no symlink resolution).
208///
209/// This is intentionally not `canonicalize()` --- we avoid filesystem
210/// access and symlink resolution for performance and determinism.
211#[must_use]
212pub fn normalize(path: &Path) -> PathBuf {
213    use std::path::Component;
214
215    let mut components = Vec::new();
216    for component in path.components() {
217        match component {
218            Component::CurDir => {}
219            Component::ParentDir => {
220                if let Some(Component::Normal(_)) = components.last() {
221                    components.pop();
222                } else {
223                    components.push(component);
224                }
225            }
226            _ => components.push(component),
227        }
228    }
229    components.iter().collect()
230}
231
232/// Normalize a path into a stable string key for hashing and comparisons.
233///
234/// This is the shared representation for path-based cache keys. It avoids
235/// filesystem access, strips Windows extended-length prefixes, normalizes
236/// separators, and folds case on case-insensitive platforms.
237#[must_use]
238pub fn normalize_for_key(path: &Path) -> String {
239    let normalized = normalize(path);
240
241    #[cfg(windows)]
242    {
243        let mut s = normalized.to_string_lossy().replace('\\', "/");
244        if let Some(stripped) = s.strip_prefix("//?/") {
245            s = stripped.to_string();
246        }
247        s.make_ascii_lowercase();
248        s
249    }
250
251    #[cfg(target_os = "macos")]
252    {
253        normalized.to_string_lossy().to_lowercase()
254    }
255
256    #[cfg(not(any(windows, target_os = "macos")))]
257    {
258        normalized.to_string_lossy().into_owned()
259    }
260}
261
262/// Convert an MSYS2/Git Bash style path to a native Windows path.
263///
264/// `/c/Users/foo` → `C:\Users\foo`
265///
266/// On non-Windows platforms, returns the input unchanged.
267/// On Windows, only converts paths matching the MSYS pattern `/<letter>/...`.
268/// Already-native paths (e.g., `C:\...`) pass through unchanged.
269#[must_use]
270pub fn normalize_msys_path(path: &str) -> String {
271    #[cfg(windows)]
272    {
273        let bytes = path.as_bytes();
274        // Match pattern: /X/ or /X (end of string) where X is a-zA-Z
275        if bytes.len() >= 2
276            && bytes[0] == b'/'
277            && bytes[1].is_ascii_alphabetic()
278            && (bytes.len() == 2 || bytes[2] == b'/')
279        {
280            let drive = (bytes[1] as char).to_ascii_uppercase();
281            let rest = if bytes.len() > 2 { &path[2..] } else { "" };
282            return format!("{drive}:{rest}").replace('/', "\\");
283        }
284        path.to_string()
285    }
286    #[cfg(not(windows))]
287    {
288        path.to_string()
289    }
290}
291
292#[cfg(test)]
293mod tests {
294    use super::*;
295
296    #[test]
297    fn normalize_removes_dot() {
298        let p = normalize(Path::new("a/./b/c"));
299        assert_eq!(p, PathBuf::from("a/b/c"));
300    }
301
302    #[test]
303    fn normalize_resolves_dotdot() {
304        let p = normalize(Path::new("a/b/../c"));
305        assert_eq!(p, PathBuf::from("a/c"));
306    }
307
308    #[cfg(windows)]
309    #[test]
310    fn normalize_for_key_windows_equivalent_spellings_match() {
311        let a = normalize_for_key(Path::new(r"\\?\C:\Work\src\..\src\main.cpp"));
312        let b = normalize_for_key(Path::new("c:/work/src/main.cpp"));
313        assert_eq!(a, b);
314    }
315
316    #[test]
317    fn msys_path_drive_letter() {
318        let result = normalize_msys_path("/c/Users/foo/bar");
319        #[cfg(windows)]
320        assert_eq!(result, r"C:\Users\foo\bar");
321        #[cfg(not(windows))]
322        assert_eq!(result, "/c/Users/foo/bar");
323    }
324
325    #[test]
326    fn msys_path_uppercase_drive() {
327        let result = normalize_msys_path("/D/project/build");
328        #[cfg(windows)]
329        assert_eq!(result, r"D:\project\build");
330        #[cfg(not(windows))]
331        assert_eq!(result, "/D/project/build");
332    }
333
334    #[test]
335    fn msys_path_bare_drive() {
336        let result = normalize_msys_path("/c");
337        #[cfg(windows)]
338        assert_eq!(result, "C:");
339        #[cfg(not(windows))]
340        assert_eq!(result, "/c");
341    }
342
343    #[test]
344    fn native_windows_path_unchanged() {
345        let result = normalize_msys_path(r"C:\Users\foo\bar");
346        assert_eq!(result, r"C:\Users\foo\bar");
347    }
348
349    #[test]
350    fn relative_path_unchanged() {
351        let result = normalize_msys_path("relative/path");
352        assert_eq!(result, "relative/path");
353    }
354
355    #[test]
356    fn empty_path_unchanged() {
357        let result = normalize_msys_path("");
358        assert_eq!(result, "");
359    }
360
361    #[test]
362    fn unix_absolute_path_not_drive() {
363        // /usr/bin/gcc — bytes[2] is 's', not '/', so NOT a drive letter path
364        let result = normalize_msys_path("/usr/bin/gcc");
365        assert_eq!(result, "/usr/bin/gcc");
366    }
367}