mirror of
https://github.com/nushell/nushell.git
synced 2024-11-25 09:53:43 +01:00
Fix and refactor cd for Filesystem Shell. (#1452)
* Fix and refactor cd for Filesystem Shell. Reorder check conditions, don't check existence twice. If building for unix check exec bit on folder. * Import PermissionsExt only on unix target. * It seems that this is the correct way?
This commit is contained in:
parent
db24ad8f36
commit
287652573b
@ -18,6 +18,9 @@ use std::path::PathBuf;
|
|||||||
use std::sync::atomic::Ordering;
|
use std::sync::atomic::Ordering;
|
||||||
use trash as SendToTrash;
|
use trash as SendToTrash;
|
||||||
|
|
||||||
|
#[cfg(unix)]
|
||||||
|
use std::os::unix::fs::PermissionsExt;
|
||||||
|
|
||||||
pub struct FilesystemShell {
|
pub struct FilesystemShell {
|
||||||
pub(crate) path: String,
|
pub(crate) path: String,
|
||||||
pub(crate) last_path: String,
|
pub(crate) last_path: String,
|
||||||
@ -185,8 +188,15 @@ impl Shell for FilesystemShell {
|
|||||||
PathBuf::from(&self.last_path)
|
PathBuf::from(&self.last_path)
|
||||||
} else {
|
} else {
|
||||||
let path = PathBuf::from(self.path());
|
let path = PathBuf::from(self.path());
|
||||||
|
let path = dunce::canonicalize(path.join(&target)).map_err(|_| {
|
||||||
|
ShellError::labeled_error(
|
||||||
|
"Cannot change to directory",
|
||||||
|
"directory not found",
|
||||||
|
&v.tag,
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
if target.exists() && !target.is_dir() {
|
if !path.is_dir() {
|
||||||
return Err(ShellError::labeled_error(
|
return Err(ShellError::labeled_error(
|
||||||
"Cannot change to directory",
|
"Cannot change to directory",
|
||||||
"is not a directory",
|
"is not a directory",
|
||||||
@ -194,16 +204,32 @@ impl Shell for FilesystemShell {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
match dunce::canonicalize(path.join(&target)) {
|
#[cfg(unix)]
|
||||||
Ok(p) => p,
|
{
|
||||||
Err(_) => {
|
let has_exec = path
|
||||||
|
.metadata()
|
||||||
|
.map(|m| {
|
||||||
|
let mode = umask::Mode::from(m.permissions().mode());
|
||||||
|
mode.has(umask::ALL_EXEC)
|
||||||
|
})
|
||||||
|
.map_err(|e| {
|
||||||
|
ShellError::labeled_error(
|
||||||
|
"Cannot change to directory",
|
||||||
|
format!("cannot stat ({})", e),
|
||||||
|
&v.tag,
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
|
if !has_exec {
|
||||||
return Err(ShellError::labeled_error(
|
return Err(ShellError::labeled_error(
|
||||||
"Cannot change to directory",
|
"Cannot change to directory",
|
||||||
"directory not found",
|
"permission denied",
|
||||||
&v.tag,
|
&v.tag,
|
||||||
))
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
path
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user