Merge pull request #4508 from tertsdiepraam/pwd-minirefactor

`pwd`: small refactor of some match expressions
This commit is contained in:
Daniel Hofstetter 2023-03-14 07:09:36 +01:00 committed by GitHub
commit 5967d66a20
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -15,10 +15,10 @@ use uucore::{format_usage, help_about, help_usage};
use uucore::display::println_verbatim;
use uucore::error::{FromIo, UResult};
static ABOUT: &str = help_about!("pwd.md");
const ABOUT: &str = help_about!("pwd.md");
const USAGE: &str = help_usage!("pwd.md");
static OPT_LOGICAL: &str = "logical";
static OPT_PHYSICAL: &str = "physical";
const OPT_LOGICAL: &str = "logical";
const OPT_PHYSICAL: &str = "physical";
fn physical_path() -> io::Result<PathBuf> {
// std::env::current_dir() is a thin wrapper around libc::getcwd().
@ -84,36 +84,22 @@ fn logical_path() -> io::Result<PathBuf> {
{
use std::fs::metadata;
use std::os::unix::fs::MetadataExt;
let path_info = match metadata(path) {
Ok(info) => info,
Err(_) => return false,
};
let real_info = match metadata(".") {
Ok(info) => info,
Err(_) => return false,
};
if path_info.dev() != real_info.dev() || path_info.ino() != real_info.ino() {
return false;
match (metadata(path), metadata(".")) {
(Ok(info1), Ok(info2)) => {
info1.dev() == info2.dev() && info1.ino() == info2.ino()
}
_ => false,
}
}
#[cfg(not(unix))]
{
use std::fs::canonicalize;
let canon_path = match canonicalize(path) {
Ok(path) => path,
Err(_) => return false,
};
let real_path = match canonicalize(".") {
Ok(path) => path,
Err(_) => return false,
};
if canon_path != real_path {
return false;
match (canonicalize(path), canonicalize(".")) {
(Ok(path1), Ok(path2)) => path1 == path2,
_ => false,
}
}
true
}
match env::var_os("PWD").map(PathBuf::from) {