Skip to main content

simple_path/
simple_path.rs

1#![cfg_attr(not(target_os = "windows"), allow(unused))]
2use crate::Display;
3#[cfg(windows)]
4use crate::{LongUnc, PathExt, Volumes};
5use std::{
6    borrow::Cow,
7    fs, io,
8    path::{Path, PathBuf, StripPrefixError},
9};
10
11/// Simplifies [Win32 File Namespaces] paths (the "`\\?\`" prefix)
12/// for better readability and compatibility.
13///
14/// The following code is a snap-in replacement of [`fs::canonicalize`].
15/// ```no_run
16/// # use simple_path::SimplePath;
17/// # let path = "";
18/// SimplePath::default().canonicalize(path);
19/// ```
20///
21/// If you have `net use Z: \\server\share`:
22/// | | `C:\dir` | `Z:\x` |
23/// | --- | --- | --- |
24/// | [`fs::canonicalize`] | `\\?\C:\dir` | `\\?\UNC\server\share\x` |
25/// | `SimplePath` | `C:\dir` | `\\server\share\x` |
26/// | `SimplePath` with [`map_to_drive`] | `C:\dir` | `Z:\x` |
27///
28/// [`map_to_drive`]: `SimplePath::map_to_drive`
29/// [Win32 File Namespaces]: https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#win32-file-namespaces
30#[derive(Clone, Debug, Default)]
31pub struct SimplePath {
32    /// Disallow simplifications
33    /// if the result is a "long path" (longer than 260 characters).
34    /// Initially `false`.
35    ///
36    /// Long paths may not be supported by some programs and APIs.
37    /// In such cases, using the [Win32 File Namespaces] (the "`\\?\`" prefix)
38    /// can often work around the limitation.
39    /// Setting this option to `true` can improve
40    /// the compatibility with such cases.
41    ///
42    /// On the other hand, some other programs such as PowerShell v7
43    /// can handle long paths,
44    /// but they can't handle the "`\\?\`" prefix.
45    /// They work best with `false`.
46    ///
47    /// Please also see the [Maximum Path Length Limitation].
48    ///
49    /// [Maximum Path Length Limitation]: https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation
50    /// [Win32 File Namespaces]: https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#win32-file-namespaces
51    pub disallow_long: bool,
52
53    /// Simplify all long UNC paths (prefixed by "`\\?\UNC\`").
54    /// Initially `false`.
55    ///
56    /// Technically speaking,
57    /// since the "`\\?\`" prefix ([Win32 File Namespaces])
58    /// disables all string parsing and
59    /// sends the following string directly to the file system,
60    /// simplifying the path is not always guaranteed to be safe or equivalent.
61    ///
62    /// For this reason,
63    /// the `SimplePath` simplifies connected network shares only by default.
64    /// Set this option to `true`
65    /// to simplify all paths prefixed by "`\\?\UNC\`".
66    ///
67    /// Please also see the [safety] note.
68    ///
69    /// # Examples
70    /// ```
71    /// # use simple_path::SimplePath;
72    /// # use std::path::Path;
73    /// let path = Path::new(r"\\?\UNC\server\share\dir");
74    /// let simple = SimplePath { allow_unknown_unc: true, ..Default::default() };
75    /// #[cfg(windows)]
76    /// assert_eq!(&*simple.simplify(path).unwrap().unwrap(), r"\\server\share\dir");
77    /// ```
78    ///
79    /// [safety]: https://github.com/kojiishi/simple-path#safety
80    /// [Win32 File Namespaces]: https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#win32-file-namespaces
81    pub allow_unknown_unc: bool,
82
83    /// Map to network share drive names when possible.
84    /// Initially `false`.
85    ///
86    /// # Examples
87    ///
88    /// ```
89    /// # use simple_path::SimplePath;
90    /// # fn test() -> std::io::Result<()> {
91    /// let path = "file.txt";
92    /// let simple = SimplePath { map_to_drive: true, ..Default::default() };
93    /// let canonicalized = simple.canonicalize(path)?;
94    /// # Ok(())
95    /// # }
96    /// ```
97    /// If the `file.txt` is in a network drive,
98    /// the result is `Z:\dir\file.txt`
99    /// instead of `\\server\share\dir\file.txt`.
100    ///
101    /// The following code tries to preserve the original form of the `path`.
102    /// ```
103    /// # use simple_path::SimplePath;
104    /// # fn test(path: &std::path::Path) -> std::io::Result<()> {
105    /// SimplePath {
106    ///     map_to_drive: !path.as_os_str().as_encoded_bytes().starts_with(br"\\"),
107    ///     ..Default::default()
108    /// }.canonicalize(path)?;
109    /// # Ok(())
110    /// # }
111    /// ```
112    pub map_to_drive: bool,
113
114    /// Skip the [`dunce`] simplification.
115    /// Initially `false`.
116    ///
117    /// [`dunce`]: https://crates.io/crates/dunce
118    pub skip_dunce: bool,
119
120    /// It is highly recommended to always use `, ..Default::default()`.
121    /// Otherwise builds fail when new fields are added.
122    ///
123    /// This field is not used in any ways,
124    /// but exists to allow using `, ..Default::default()`
125    /// even when all other fields are specified.
126    pub _unused: bool,
127
128    #[cfg(all(test, windows))]
129    volumes: Option<Volumes>,
130}
131
132impl SimplePath {
133    #[cfg(all(test, windows))]
134    pub(crate) fn mock() -> SimplePath {
135        SimplePath {
136            volumes: Some(Volumes::mock()),
137            ..Default::default()
138        }
139    }
140
141    /// A snap-in replacement for [`fs::canonicalize`].
142    /// It calls [`fs::canonicalize`] and [`simplify`].
143    ///
144    /// On other platforms than Windows,
145    /// this is equivalent to [`fs::canonicalize`].
146    ///
147    /// # Examples
148    /// ```
149    /// # fn test(path: &std::path::Path) -> std::io::Result<()> {
150    /// use simple_path::SimplePath;
151    /// let canonicalized = SimplePath::default().canonicalize(path)?;
152    /// println!("{}", canonicalized.display());
153    /// # Ok(()) }
154    /// ```
155    ///
156    /// [`fs::canonicalize`]: https://doc.rust-lang.org/std/fs/fn.canonicalize.html
157    /// [`simplify`]: SimplePath::simplify
158    #[inline]
159    pub fn canonicalize(&self, path: impl AsRef<Path>) -> io::Result<PathBuf> {
160        let canonicalized = fs::canonicalize(path)?;
161        #[cfg(windows)]
162        if let Some(simplified) = self.simplify(&canonicalized)? {
163            return Ok(simplified.into_owned());
164        }
165        Ok(canonicalized)
166    }
167
168    /// Try to simplify the given `path`.
169    ///
170    /// Returns `Ok(None)`
171    /// if no simplification is applied,
172    /// or on other platforms than Windows.
173    #[inline]
174    pub fn simplify<'a>(&self, path: &'a Path) -> io::Result<Option<Cow<'a, Path>>> {
175        #[cfg(windows)]
176        return self._simplify(path).map_err(io_error_from_anyhow);
177        #[cfg(not(windows))]
178        Ok(None)
179    }
180
181    #[cfg(windows)]
182    fn _simplify<'a>(&self, path: &'a Path) -> anyhow::Result<Option<Cow<'a, Path>>> {
183        if let Ok(long_unc) = LongUnc::try_from(path)
184            && long_unc.is_sub_prefix_unc()
185        {
186            // Try mapped network drives.
187            let drive_path = if !self.allow_unknown_unc || self.map_to_drive {
188                self.drive_path(path)?
189            } else {
190                None
191            };
192            if self.map_to_drive
193                && let Some(drive_path) = &drive_path
194                && drive_path.has_drive()
195                && !drive_path.has_invalid_chars()
196                && (!self.disallow_long || !drive_path.is_longer_than_max_path())
197            {
198                return Ok(Some(Cow::Owned(drive_path.to_path_buf())));
199            }
200
201            // Try short UNC (`\\server\share`).
202            if (self.allow_unknown_unc || drive_path.is_some())
203                && !long_unc.has_invalid_chars()
204                && (!self.disallow_long || !long_unc.is_short_unc_longer_than_max_path())
205            {
206                return Ok(Some(Cow::Owned(long_unc.to_short_unc())));
207            }
208        }
209
210        // Try `dunce::simplified`.
211        if !self.skip_dunce {
212            let simplified = dunce::simplified(path);
213            if !std::ptr::eq(path, simplified) {
214                return Ok(Some(Cow::Borrowed(simplified)));
215            }
216        }
217        Ok(None)
218    }
219
220    #[cfg(windows)]
221    #[inline]
222    fn drive_path<'a>(&self, path: &'a Path) -> anyhow::Result<Option<crate::DrivePath<'a>>> {
223        #[cfg(test)]
224        if let Some(volumes) = &self.volumes {
225            return Ok(volumes._drive_path(path));
226        }
227        Volumes::drive_path(path)
228    }
229
230    /// Refreshes the cached information.
231    pub fn refresh() -> io::Result<()> {
232        #[cfg(windows)]
233        Volumes::refresh().map_err(io_error_from_anyhow)?;
234        Ok(())
235    }
236
237    /// Returns an object that implements [`Display`][`core::fmt::Display`]
238    /// for printing simplified paths.
239    ///
240    /// # Examples
241    ///
242    /// ```
243    /// # use std::path::Path;
244    /// # use simple_path::SimplePath;
245    /// # fn test() -> std::io::Result<()> {
246    /// let path = Path::new("file").canonicalize()?;
247    /// println!("{}", SimplePath::default().display(&path));
248    /// # Ok(())
249    /// # }
250    /// ```
251    pub fn display<'a>(&'a self, path: &'a Path) -> Display<'a> {
252        Display::new(self, path)
253    }
254
255    /// A snap-in replacement for [`Path::strip_prefix`]
256    /// with a fix for [a leading directory separator "`\`" left for UNC paths
257    /// on Windows](https://github.com/rust-lang/rust/issues/155183).
258    ///
259    /// # Examples
260    ///
261    /// ```
262    /// # use std::path::{Path, StripPrefixError};
263    /// # use simple_path::SimplePath;
264    /// # fn t<'a>(path: &'a Path, base: &'a Path) -> Result<&'a Path, StripPrefixError> {
265    /// SimplePath::strip_prefix(path, base)
266    /// # }
267    /// ```
268    pub fn strip_prefix(path: &Path, base: impl AsRef<Path>) -> Result<&Path, StripPrefixError> {
269        #[cfg(windows)]
270        return PathExt::strip_prefix_fix(path, base);
271        #[cfg(not(windows))]
272        path.strip_prefix(base)
273    }
274}
275
276fn io_error_from_anyhow(error: anyhow::Error) -> io::Error {
277    match error.downcast::<io::Error>() {
278        Ok(io_error) => io_error,
279        Err(other_error) => io::Error::other(other_error),
280    }
281}
282
283#[cfg(test)]
284mod tests {
285    use super::*;
286
287    #[cfg(windows)]
288    #[test]
289    fn simplify_drive() {
290        let mut simple = SimplePath::mock();
291        assert_eq!(simple.simplify(Path::new(r"C:\foo")).unwrap(), None);
292        simple.allow_unknown_unc = true;
293        assert_eq!(simple.simplify(Path::new(r"C:\foo")).unwrap(), None);
294    }
295
296    #[cfg(windows)]
297    #[test]
298    fn simplify_drive_unc() {
299        let mut simple = SimplePath::mock();
300        let path = Path::new(r"\\?\UNC\server\share\foo");
301        let path2 = Path::new(r"\\?\UNC\server2\share2\foo2");
302        assert_eq!(
303            simple.simplify(path).unwrap(),
304            Some(Cow::Owned(PathBuf::from(r"\\server\share\foo")))
305        );
306        assert_eq!(
307            simple.simplify(path2).unwrap(),
308            Some(Cow::Owned(PathBuf::from(r"\\server2\share2\foo2")))
309        );
310
311        simple.map_to_drive = true;
312        assert_eq!(
313            simple.simplify(path).unwrap(),
314            Some(Cow::Owned(PathBuf::from(r"X:\foo")))
315        );
316        assert_eq!(
317            simple.simplify(path2).unwrap(),
318            Some(Cow::Owned(PathBuf::from(r"Z:\foo2")))
319        );
320    }
321
322    #[cfg(windows)]
323    #[test]
324    fn simplify_dunce() {
325        let simple = SimplePath::default();
326        assert_eq!(
327            simple.simplify(Path::new(r"\\?\C:\foo")).unwrap(),
328            Some(Cow::Borrowed(Path::new(r"C:\foo")))
329        );
330    }
331
332    #[cfg(windows)]
333    #[test]
334    fn simplify_dunce_skip() {
335        let simple = SimplePath {
336            skip_dunce: true,
337            ..Default::default()
338        };
339        assert_eq!(simple.simplify(Path::new(r"\\?\C:\foo")).unwrap(), None);
340    }
341
342    #[cfg(windows)]
343    #[test]
344    fn simplify_unmapped_connected_share() {
345        let mut simple = SimplePath::mock();
346        let path = Path::new(r"\\?\UNC\server0\share0\foo");
347        assert_eq!(
348            simple.simplify(path).unwrap(),
349            Some(Cow::Owned(PathBuf::from(r"\\server0\share0\foo")))
350        );
351
352        // Even with map_to_drive = true, it should simplify to the UNC path,
353        // because the drive letter is '\0'.
354        simple.map_to_drive = true;
355        assert_eq!(
356            simple.simplify(path).unwrap(),
357            Some(Cow::Owned(PathBuf::from(r"\\server0\share0\foo")))
358        );
359    }
360
361    #[cfg(windows)]
362    #[test]
363    fn simplify_unknown_unc() -> anyhow::Result<()> {
364        let mut simple = SimplePath::mock();
365        let unknown = Path::new(r"\\?\UNC\server\unknown\foo");
366        let mapped = Path::new(r"\\?\UNC\server\share\foo");
367        assert_eq!(simple.simplify(unknown)?, None);
368
369        // `unknown` should be simplified if `allow_unknown_unc`.
370        simple.allow_unknown_unc = true;
371        assert_eq!(
372            simple.simplify(unknown)?,
373            Some(Cow::Owned(PathBuf::from(r"\\server\unknown\foo")))
374        );
375        assert_eq!(
376            simple.simplify(mapped)?,
377            Some(Cow::Owned(PathBuf::from(r"\\server\share\foo")))
378        );
379
380        // `map_to_drive` should still be in effect.
381        simple.map_to_drive = true;
382        assert_eq!(
383            simple.simplify(mapped)?,
384            Some(Cow::Owned(PathBuf::from(r"X:\foo")))
385        );
386
387        // `allow_unknown_unc` should simplify only for "`\\?\UNC\`".
388        assert_eq!(simple.simplify(Path::new(r"\\.\COM1:"))?, None);
389        simple.skip_dunce = true;
390        assert_eq!(simple.simplify(Path::new(r"\\?\C:\foo"))?, None);
391        Ok(())
392    }
393}