#unc #canonicalize #windows #realpath #deunc

dunce

Normalize Windows paths to the most compatible format, avoiding UNC where possible

5 stable releases

1.0.5 Aug 4, 2024
1.0.4 Apr 19, 2023
1.0.3 Oct 6, 2022
1.0.2 Jun 8, 2021
0.1.0 Nov 22, 2017

#16 in Filesystem

Download history 477018/week @ 2024-08-13 478791/week @ 2024-08-20 453985/week @ 2024-08-27 512255/week @ 2024-09-03 477963/week @ 2024-09-10 459842/week @ 2024-09-17 501073/week @ 2024-09-24 520519/week @ 2024-10-01 518699/week @ 2024-10-08 548490/week @ 2024-10-15 544367/week @ 2024-10-22 511130/week @ 2024-10-29 515266/week @ 2024-11-05 567068/week @ 2024-11-12 556377/week @ 2024-11-19 405360/week @ 2024-11-26

2,134,555 downloads per month
Used in 2,523 crates (282 directly)

CC0-1.0 OR MIT-0 OR Apache-2.0

15KB
252 lines

Dunce (de-UNC)

In Windows the regular paths (C:\foo) are supported by all programs, but have lots of bizarre restrictions for backwards compatibility with MS-DOS. There are also Windows NT UNC paths (\\?\C:\foo), which are more robust and with fewer gotchas, but are rarely supported by Windows programs — even Microsoft's own!

This crate converts Windows UNC paths to the MS-DOS-compatible format whenever possible, but leaves UNC paths as-is when they can't be unambiguously expressed in a simpler way. This allows legacy programs to access all paths they can possibly access, and doesn't break any paths for UNC-aware programs.

For example, \\?\C:\Windows will be converted to C:\Windows, but \\?\C:\COM will be left as-is, because it contains a reserved filename.

In Rust the worst UNC offender is the fs::canonicalize() function. This crate provides a drop-in replacement for it that returns paths you'd expect.

On non-Windows platforms these functions leave paths unmodified, so it's safe to use them unconditionally for all platforms.

This crate's handling of UNC paths is safer than just unconditionally stripping the \\ prefix, because naively stripped UNC paths with hostnames change to relative directory paths. There are other normalization rules, special characters, and length limits that could change meaning of the path.

Parsing is based on https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx.

No runtime deps