|
3 | 3 | // Note: Device ID management has been moved to device::manager for better |
4 | 4 | // module organization. Import from there instead: |
5 | 5 | // use crate::device::manager::{get_current_device_id, set_current_device_id}; |
| 6 | + |
| 7 | +/// Strip Windows extended path prefixes produced by `std::fs::canonicalize()`. |
| 8 | +/// |
| 9 | +/// On Windows, `canonicalize()` returns paths like `\\?\C:\...` (local) or |
| 10 | +/// `\\?\UNC\server\share\...` (network). These prefixes break `starts_with()` |
| 11 | +/// matching throughout the codebase and must be normalized. |
| 12 | +/// |
| 13 | +/// - `\\?\UNC\server\share\...` → `\\server\share\...` |
| 14 | +/// - `\\?\C:\...` → `C:\...` |
| 15 | +/// - All other paths are returned unchanged. |
| 16 | +#[cfg(windows)] |
| 17 | +pub fn strip_windows_extended_prefix(path: std::path::PathBuf) -> std::path::PathBuf { |
| 18 | + if let Some(s) = path.to_str() { |
| 19 | + if s.starts_with(r"\\?\UNC\") { |
| 20 | + // \\?\UNC\server\share\... → \\server\share\... |
| 21 | + std::path::PathBuf::from(format!(r"\\{}", &s[8..])) |
| 22 | + } else if let Some(stripped) = s.strip_prefix(r"\\?\") { |
| 23 | + // Only strip \\?\ when followed by a drive letter (e.g. C:\). |
| 24 | + // Leave volume GUIDs (\\?\Volume{...}\) and other verbatim |
| 25 | + // forms untouched — they are invalid without the prefix. |
| 26 | + if stripped.as_bytes().get(1) == Some(&b':') { |
| 27 | + std::path::PathBuf::from(stripped) |
| 28 | + } else { |
| 29 | + path |
| 30 | + } |
| 31 | + } else { |
| 32 | + path |
| 33 | + } |
| 34 | + } else { |
| 35 | + path |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +/// No-op on non-Windows platforms. |
| 40 | +#[cfg(not(windows))] |
| 41 | +pub fn strip_windows_extended_prefix(path: std::path::PathBuf) -> std::path::PathBuf { |
| 42 | + path |
| 43 | +} |
0 commit comments