Handle correctly the special files metadata fetching

This commit is contained in:
Peltoche 2018-12-04 11:15:05 +01:00
parent 228a5bb11a
commit 823b04a258
No known key found for this signature in database
GPG key ID: CED68D0487156952
2 changed files with 20 additions and 14 deletions

View file

@ -24,11 +24,10 @@ impl<'a> Core<'a> {
if path.is_dir() {
dirs.push(path);
} else if path.is_file() {
files.push(Meta::from(path));
} else {
let err = path.metadata().unwrap_err();
println!("cannot access '{}': {}", path.display(), err);
if let Some(meta) = Meta::from_path(path) {
files.push(meta);
}
}
}
@ -74,9 +73,10 @@ impl<'a> Core<'a> {
for entry in dir {
if let Ok(entry) = entry {
let meta = Meta::from(entry.path().as_path());
if !meta.name.is_hidden() || self.options.display_all {
metas.push(meta);
if let Some(meta) = Meta::from_path(entry.path().as_path()) {
if !meta.name.is_hidden() || self.options.display_all {
metas.push(meta);
}
}
}
}

View file

@ -28,9 +28,17 @@ pub struct Meta {
pub symlink: Option<SymLink>,
}
impl<'a> From<&'a Path> for Meta {
fn from(path: &Path) -> Self {
let metadata;
impl Meta {
pub fn from_path(path: &Path) -> Option<Self> {
let mut metadata = match path.metadata() {
Ok(res) => res,
Err(err) => {
println!("cannot access '{}': {}", path.display(), err);
return None;
}
};
let file_type = FileType::from(&metadata);
if read_link(path).is_ok() {
// If the file is a link, retrieve the metadata without following
@ -42,9 +50,7 @@ impl<'a> From<&'a Path> for Meta {
metadata = path.metadata().expect("failed to retrieve metadata");
}
let file_type = FileType::from(&metadata);
Meta {
Some(Meta {
symlink: SymLink::from_path(&path),
size: Size::from(&metadata),
permissions: Permissions::from(&metadata),
@ -52,6 +58,6 @@ impl<'a> From<&'a Path> for Meta {
name: Name::new(&path, file_type),
owner: Owner::from(&metadata),
file_type,
}
})
}
}