Skip to content

Commit

Permalink
Avoid Windows invalid UNC path.
Browse files Browse the repository at this point in the history
On Windows, Path::join may makes invalid UNC path.
It was fixed on nightly (rust-lang/rust#89270), but not yet on stable.
  • Loading branch information
kazatsuyu committed Nov 5, 2021
1 parent 0f40b7b commit 13831f6
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/util/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use path_abs::PathAbs;
use std::{
fmt::{self, Display},
io,
path::{Path, PathBuf},
path::{Component, Path, PathBuf},
};
use thiserror::Error;

Expand Down Expand Up @@ -84,7 +84,33 @@ impl Display for PathNotPrefixed {
}

pub fn prefix_path(root: impl AsRef<Path>, path: impl AsRef<Path>) -> PathBuf {
root.as_ref().join(path)
let root = root.as_ref();
let path = path.as_ref();
let is_verbatim = if let Some(Component::Prefix(prefix)) = root.components().next() {
prefix.kind().is_verbatim()
} else {
false
};
if !is_verbatim {
return root.join(path);
}
let mut buf = root.components().collect::<Vec<_>>();
for component in path.components() {
match component {
Component::RootDir => {
buf.truncate(1);
buf.push(component);
}
Component::CurDir => {}
Component::ParentDir => {
if let Some(_) = buf.last() {
buf.pop();
}
}
_ => buf.push(component),
};
}
buf.into_iter().collect()
}

pub fn unprefix_path(
Expand Down

0 comments on commit 13831f6

Please sign in to comment.