surjection_core/remote/
unc.rs1use std::path::{Path, PathBuf};
13
14use crate::env::Os;
15
16pub trait UncResolver: Send + Sync {
18 fn universal_name(&self, path: &Path) -> Option<PathBuf>;
20}
21
22#[derive(Debug, Clone, Copy, Default)]
24pub struct NoUnc;
25
26impl UncResolver for NoUnc {
27 fn universal_name(&self, _path: &Path) -> Option<PathBuf> {
28 None
29 }
30}
31
32#[derive(Debug, Clone, Copy, Default)]
34pub struct SystemUnc;
35
36impl UncResolver for SystemUnc {
37 fn universal_name(&self, path: &Path) -> Option<PathBuf> {
38 if !cfg!(windows) || !is_drive_path(path) {
39 return None;
40 }
41 let resolved = std::fs::canonicalize(path).ok()?;
42 let text = resolved.to_str()?;
43 text.strip_prefix(r"\\?\UNC\")
44 .map(|rest| PathBuf::from(format!(r"\\{rest}")))
45 }
46}
47
48pub fn is_drive_path(path: &Path) -> bool {
50 let bytes = path.as_os_str().as_encoded_bytes();
51 bytes.len() >= 3
52 && bytes[0].is_ascii_alphabetic()
53 && bytes[1] == b':'
54 && matches!(bytes[2], b'\\' | b'/')
55}
56
57pub fn resolve(os: Os, resolver: &dyn UncResolver, typed: &Path) -> PathBuf {
60 if os == Os::Windows && is_drive_path(typed) {
61 resolver
62 .universal_name(typed)
63 .unwrap_or_else(|| typed.to_path_buf())
64 } else {
65 typed.to_path_buf()
66 }
67}