Remove unnecessary use of read_link

This commit is contained in:
Abin Simon 2021-08-11 09:23:51 +05:30
parent d17bf89555
commit 682a80d866
2 changed files with 45 additions and 14 deletions

View file

@ -27,7 +27,6 @@ pub use crate::icon::Icons;
use crate::flags::{Display, Flags, Layout}; use crate::flags::{Display, Flags, Layout};
use crate::print_error; use crate::print_error;
use std::fs::read_link;
use std::io::{Error, ErrorKind}; use std::io::{Error, ErrorKind};
use std::path::{Component, Path, PathBuf}; use std::path::{Component, Path, PathBuf};
@ -161,13 +160,7 @@ impl Meta {
} }
fn calculate_total_file_size(path: &PathBuf) -> u64 { fn calculate_total_file_size(path: &PathBuf) -> u64 {
let metadata = if read_link(&path).is_ok() { let metadata = path.symlink_metadata();
// If the file is a link, retrieve the metadata without following
// the link.
path.symlink_metadata()
} else {
path.metadata()
};
let metadata = match metadata { let metadata = match metadata {
Ok(meta) => meta, Ok(meta) => meta,
Err(err) => { Err(err) => {
@ -205,12 +198,26 @@ impl Meta {
} }
pub fn from_path(path: &Path, dereference: bool) -> Result<Self, std::io::Error> { pub fn from_path(path: &Path, dereference: bool) -> Result<Self, std::io::Error> {
// If the file is a link then retrieve link metadata instead with target metadata (if present). let mut metadata = path.symlink_metadata()?;
let (metadata, symlink_meta) = if read_link(path).is_ok() && !dereference { let mut symlink_meta = None;
(path.symlink_metadata()?, path.metadata().ok()) if metadata.file_type().is_symlink() {
match path.metadata() {
Ok(m) => {
if dereference {
metadata = m;
} else { } else {
(path.metadata()?, None) symlink_meta = Some(m);
}; }
}
Err(e) => {
// This case, it is definitely a symlink or
// path.symlink_metadata would have errored out
if dereference {
return Err(e);
}
}
}
}
#[cfg(unix)] #[cfg(unix)]
let owner = Owner::from(&metadata); let owner = Owner::from(&metadata);

View file

@ -263,6 +263,30 @@ fn test_dereference_link_right_type_and_no_link() {
.stdout(predicate::str::contains(link_icon).not()); .stdout(predicate::str::contains(link_icon).not());
} }
#[cfg(unix)]
#[test]
fn test_dereference_link_broken_link() {
let dir = tempdir();
let link = dir.path().join("link");
fs::symlink("target", &link).unwrap();
cmd()
.arg("-l")
.arg("--dereference")
.arg("--ignore-config")
.arg(&link)
.assert()
.stderr(predicate::str::contains("No such file or directory"));
cmd()
.arg("-l")
.arg("-L")
.arg("--ignore-config")
.arg(link)
.assert()
.stderr(predicate::str::contains("No such file or directory"));
}
#[cfg(unix)] #[cfg(unix)]
#[test] #[test]
fn test_show_folder_content_of_symlink() { fn test_show_folder_content_of_symlink() {