|
pub fn normalize_slashes(path: PathBuf) -> Result<Vec<u8>, PathBuf> { |
|
let mut buf = OsString::from(path).into_string()?.into_bytes(); |
|
for byte in &mut buf { |
|
if *byte == b'\\' { |
|
*byte = b'/'; |
|
} |
|
} |
|
Ok(buf) |
|
} |
This routine should check to see if the path is a "verbatim path" (also knowns as a Windows extended-length path), which means it has a prefix of \\?\. These paths should not be modified. The Ruby engine should treat them as opaque.
See: https://users.rust-lang.org/t/understanding-windows-paths/58583.
This can be checked by using the Path::components iterator and checking whether the first element yielded by this iterator is Component::Prefix where the inner prefix_component.kind().is_verbatim() gives true.
The Prefix docs give these examples for the various types of paths, which we should add tests for:
assert_eq!(Verbatim(OsStr::new("pictures")),
get_path_prefix(r"\\?\pictures\kittens"));
assert_eq!(VerbatimUNC(OsStr::new("server"), OsStr::new("share")),
get_path_prefix(r"\\?\UNC\server\share"));
assert_eq!(VerbatimDisk(b'C'), get_path_prefix(r"\\?\c:\"));
assert_eq!(DeviceNS(OsStr::new("BrainInterface")),
get_path_prefix(r"\\.\BrainInterface"));
assert_eq!(UNC(OsStr::new("server"), OsStr::new("share")),
get_path_prefix(r"\\server\share"));
assert_eq!(Disk(b'C'), get_path_prefix(r"C:\Users\Rust\Pictures\Ferris"));
The linked discourse thread also includes these examples:
\\?\UNC\server\share\folder\file.txt
\\?\C:\foo
artichoke/scolapasta-path/src/paths/windows.rs
Lines 48 to 56 in 53b84b7
This routine should check to see if the path is a "verbatim path" (also knowns as a Windows extended-length path), which means it has a prefix of
\\?\. These paths should not be modified. The Ruby engine should treat them as opaque.See: https://users.rust-lang.org/t/understanding-windows-paths/58583.
This can be checked by using the
Path::componentsiterator and checking whether the first element yielded by this iterator isComponent::Prefixwhere the innerprefix_component.kind().is_verbatim()gives true.The
Prefixdocs give these examples for the various types of paths, which we should add tests for:The linked discourse thread also includes these examples: