From 30f74b08b43867418c55af9a0080bb39e4ba5723 Mon Sep 17 00:00:00 2001 From: Benjamin Sago Date: Thu, 29 Jun 2017 12:24:04 +0100 Subject: [PATCH] Always look up metadata MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We can do this because the only File::new invocation that already has metadata is already in the file module, so it doesn’t need its own constructor. --- src/exa.rs | 2 +- src/fs/dir.rs | 6 +++--- src/fs/file.rs | 16 ++++++++++------ 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/exa.rs b/src/exa.rs index 0376cdb..6a66e5e 100644 --- a/src/exa.rs +++ b/src/exa.rs @@ -75,7 +75,7 @@ impl<'w, W: Write + 'w> Exa<'w, W> { } for file_name in &self.args { - match File::new(Path::new(&file_name), None, None, None) { + match File::new(Path::new(&file_name), None, None) { Err(e) => { exit_status = 2; writeln!(stderr(), "{}: {}", file_name, e)?; diff --git a/src/fs/dir.rs b/src/fs/dir.rs index e9da9ba..fd4af08 100644 --- a/src/fs/dir.rs +++ b/src/fs/dir.rs @@ -119,7 +119,7 @@ impl<'dir> Files<'dir> { let filen = path_filename(path); if !self.dotfiles && filen.starts_with(".") { continue } - return Some(File::new(path, Some(self.dir), Some(filen), None) + return Some(File::new(path, Some(self.dir), Some(filen)) .map_err(|e| (path.clone(), e))) } else { @@ -150,12 +150,12 @@ impl<'dir> Iterator for Files<'dir> { fn next(&mut self) -> Option { if let Dots::DotNext = self.dots { self.dots = Dots::DotDotNext; - Some(File::new(&self.dir.path, Some(self.dir), Some(String::from(".")), None) + Some(File::new(&self.dir.path, Some(self.dir), Some(String::from("."))) .map_err(|e| (Path::new(".").to_path_buf(), e))) } else if let Dots::DotDotNext = self.dots { self.dots = Dots::FilesNext; - Some(File::new(&self.parent(), Some(self.dir), Some(String::from("..")), None) + Some(File::new(&self.parent(), Some(self.dir), Some(String::from(".."))) .map_err(|e| (self.parent(), e))) } else { diff --git a/src/fs/file.rs b/src/fs/file.rs index 3d32c38..0f24dd1 100644 --- a/src/fs/file.rs +++ b/src/fs/file.rs @@ -67,19 +67,17 @@ pub fn path_filename(path: &Path) -> String { } impl<'dir> File<'dir> { - pub fn new(path: &Path, parent: Option<&'dir Dir>, mut filename: Option, mut metadata: Option) -> IOResult> { + pub fn new(path: &Path, parent: Option<&'dir Dir>, mut filename: Option) -> IOResult> { if filename.is_none() { filename = Some(path_filename(path)); } - if metadata.is_none() { - metadata = Some(fs::symlink_metadata(path)?); - } + let metadata = fs::symlink_metadata(path)?; Ok(File { path: path.to_path_buf(), dir: parent, - metadata: metadata.unwrap(), + metadata: metadata, ext: ext(path), name: filename.unwrap(), }) @@ -184,7 +182,13 @@ impl<'dir> File<'dir> { // Use plain `metadata` instead of `symlink_metadata` - we *want* to // follow links. if let Ok(metadata) = fs::metadata(&target_path) { - FileTarget::Ok(File::new(&*display_path, None, None, Some(metadata)).unwrap()) + FileTarget::Ok(File { + dir: None, + ext: ext(&display_path), + metadata: metadata, + name: path_filename(&display_path), + path: display_path, + }) } else { FileTarget::Broken(display_path)