Try not to use verbatim paths for UNC shares (#6824)

This commit is contained in:
Chris Denton 2022-10-22 17:51:52 +01:00 committed by GitHub
parent 3f555a6836
commit 89f3cbf318
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 46 additions and 11 deletions

14
Cargo.lock generated
View File

@ -1046,12 +1046,6 @@ dependencies = [
"rust_decimal",
]
[[package]]
name = "dunce"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "453440c271cf5577fd2a40e4942540cb7d0d2f85e27c8d07dd0023c925a67541"
[[package]]
name = "dyn-clone"
version = "1.0.9"
@ -2810,7 +2804,7 @@ name = "nu-path"
version = "0.70.1"
dependencies = [
"dirs-next",
"dunce",
"omnipath",
"pwd",
]
@ -3145,6 +3139,12 @@ dependencies = [
"memchr",
]
[[package]]
name = "omnipath"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e7461858c5ac9bde3fcdeedc17f58ed0469ec74f2d737b6369fc31c0b0ef576c"
[[package]]
name = "once_cell"
version = "1.15.0"

View File

@ -9,7 +9,7 @@ version = "0.70.1"
[dependencies]
dirs-next = "2.0.0"
dunce = "1.0.1"
omnipath = "0.1.1"
[target.'cfg(all(unix, not(target_os = "macos")))'.dependencies]
pwd = "1.3.1"
pwd = "1.3.1"

View File

@ -1,5 +1,7 @@
use std::path::{is_separator, Component, Path, PathBuf};
use super::helpers;
const EXPAND_STR: &str = if cfg!(windows) { r"..\" } else { "../" };
fn handle_dots_push(string: &mut String, count: u8) {
@ -108,7 +110,7 @@ pub fn expand_dots(path: impl AsRef<Path>) -> PathBuf {
_ => result.push(component),
});
dunce::simplified(&result).to_path_buf()
helpers::simiplified(&result)
}
#[cfg(test)]

View File

@ -2,6 +2,7 @@ use std::io;
use std::path::{Path, PathBuf};
use super::dots::{expand_dots, expand_ndots};
use super::helpers;
use super::tilde::expand_tilde;
// Join a path relative to another path. Paths starting with tilde are considered as absolute.
@ -30,7 +31,7 @@ fn canonicalize(path: impl AsRef<Path>) -> io::Result<PathBuf> {
let path = expand_tilde(path);
let path = expand_ndots(path);
dunce::canonicalize(path)
helpers::canonicalize(&path)
}
/// Resolve all symbolic links and all components (tilde, ., .., ...+) and return the path in its

View File

@ -1,3 +1,5 @@
#[cfg(windows)]
use omnipath::WinPathExt;
use std::path::PathBuf;
pub fn home_dir() -> Option<PathBuf> {
@ -7,3 +9,22 @@ pub fn home_dir() -> Option<PathBuf> {
pub fn config_dir() -> Option<PathBuf> {
dirs_next::config_dir()
}
#[cfg(windows)]
pub fn canonicalize(path: &std::path::Path) -> std::io::Result<std::path::PathBuf> {
path.canonicalize()?.to_winuser_path()
}
#[cfg(not(windows))]
pub fn canonicalize(path: &std::path::Path) -> std::io::Result<std::path::PathBuf> {
path.canonicalize()
}
#[cfg(windows)]
pub fn simiplified(path: &std::path::Path) -> PathBuf {
path.to_winuser_path()
.unwrap_or_else(|_| path.to_path_buf())
}
#[cfg(not(windows))]
pub fn simiplified(path: &std::path::Path) -> PathBuf {
path.to_path_buf()
}

View File

@ -419,3 +419,14 @@ fn canonicalize_with_should_fail() {
assert!(canonicalize_with(path, relative_to).is_err());
}
#[cfg(windows)]
#[test]
fn canonicalize_unc() {
// Ensure that canonicalizing UNC paths does not turn them verbatim.
// Assumes the C drive exists and that the `localhost` UNC path works.
let actual =
nu_path::canonicalize_with(r"\\localhost\c$", ".").expect("failed to canonicalize");
let expected = Path::new(r"\\localhost\c$");
assert_eq!(actual, expected);
}