|
| 1 | +#[cfg(unix)] |
| 2 | +use std::os::unix::ffi::OsStrExt as _; |
| 3 | +use std::{ |
| 4 | + ffi::OsStr, |
| 5 | + fmt::Debug, |
| 6 | + path::{Path, StripPrefixError}, |
| 7 | +}; |
| 8 | + |
| 9 | +use allocator_api2::alloc::Allocator; |
| 10 | +use bincode::{BorrowDecode, Encode, de::BorrowDecoder, error::DecodeError}; |
| 11 | +use bytemuck::TransparentWrapper; |
| 12 | + |
| 13 | +use super::native_str::NativeStr; |
| 14 | + |
| 15 | +/// An opaque path type used in [`super::PathAccess`]. |
| 16 | +/// |
| 17 | +/// On Windows, tracked paths are NT Object Manager paths (`\??` prefix), |
| 18 | +/// whose raw data is not meaningful for direct consumption. The only way |
| 19 | +/// to use the path is through [`strip_path_prefix`](NativePath::strip_path_prefix), |
| 20 | +/// which normalizes platform differences and extracts a workspace-relative path. |
| 21 | +#[derive(TransparentWrapper, Encode, PartialEq, Eq)] |
| 22 | +#[repr(transparent)] |
| 23 | +pub struct NativePath { |
| 24 | + inner: NativeStr, |
| 25 | +} |
| 26 | + |
| 27 | +impl NativePath { |
| 28 | + #[cfg(windows)] |
| 29 | + #[must_use] |
| 30 | + pub fn from_wide(wide: &[u16]) -> &Self { |
| 31 | + Self::wrap_ref(NativeStr::from_wide(wide)) |
| 32 | + } |
| 33 | + |
| 34 | + pub fn clone_in<'new_alloc, A>(&self, alloc: &'new_alloc A) -> &'new_alloc Self |
| 35 | + where |
| 36 | + &'new_alloc A: Allocator, |
| 37 | + { |
| 38 | + Self::wrap_ref(self.inner.clone_in(alloc)) |
| 39 | + } |
| 40 | + |
| 41 | + pub fn strip_path_prefix<P: AsRef<Path>, R, F: FnOnce(Result<&Path, StripPrefixError>) -> R>( |
| 42 | + &self, |
| 43 | + base: P, |
| 44 | + f: F, |
| 45 | + ) -> R { |
| 46 | + /// Strip the `\\?\`, `\\.\`, `\??\` prefix from a Windows path, if present. |
| 47 | + /// Does nothing on non-Windows platforms. |
| 48 | + /// |
| 49 | + /// \\?\ and \\.\ are used to enable long paths and access to device paths. |
| 50 | + /// \??\ is used in Nt* calls. |
| 51 | + /// The resulting path is not necessarily valid or points to the same location, |
| 52 | + /// but it's good enough for sanitizing paths in `NativePath::strip_path_prefix`. |
| 53 | + #[cfg_attr( |
| 54 | + not(windows), |
| 55 | + expect( |
| 56 | + clippy::missing_const_for_fn, |
| 57 | + reason = "uses non-const for loop and strip_prefix on Windows" |
| 58 | + ) |
| 59 | + )] |
| 60 | + fn strip_windows_path_prefix(p: &OsStr) -> &OsStr { |
| 61 | + #[cfg(windows)] |
| 62 | + { |
| 63 | + use os_str_bytes::OsStrBytesExt as _; |
| 64 | + for prefix in [r"\\?\", r"\\.\", r"\??\"] { |
| 65 | + if let Some(stripped) = p.strip_prefix(prefix) { |
| 66 | + return stripped; |
| 67 | + } |
| 68 | + } |
| 69 | + p |
| 70 | + } |
| 71 | + #[cfg(not(windows))] |
| 72 | + { |
| 73 | + p |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + let me = self.inner.to_cow_os_str(); |
| 78 | + let me = strip_windows_path_prefix(&me); |
| 79 | + let base = strip_windows_path_prefix(base.as_ref().as_os_str()); |
| 80 | + f(Path::new(me).strip_prefix(base)) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +impl Debug for NativePath { |
| 85 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 86 | + <NativeStr as Debug>::fmt(&self.inner, f) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +impl<'a, C> BorrowDecode<'a, C> for &'a NativePath { |
| 91 | + fn borrow_decode<D: BorrowDecoder<'a, Context = C>>( |
| 92 | + decoder: &mut D, |
| 93 | + ) -> Result<Self, DecodeError> { |
| 94 | + let inner: &'a NativeStr = BorrowDecode::borrow_decode(decoder)?; |
| 95 | + Ok(NativePath::wrap_ref(inner)) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +#[cfg(unix)] |
| 100 | +impl<'a, S: AsRef<OsStr> + ?Sized> From<&'a S> for &'a NativePath { |
| 101 | + fn from(value: &'a S) -> Self { |
| 102 | + NativePath::wrap_ref(NativeStr::from_bytes(value.as_ref().as_bytes())) |
| 103 | + } |
| 104 | +} |
0 commit comments