Always look up metadata

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.
This commit is contained in:
Benjamin Sago 2017-06-29 12:24:04 +01:00
parent 340bccbcfc
commit 30f74b08b4
3 changed files with 14 additions and 10 deletions

View File

@ -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)?;

View File

@ -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<Self::Item> {
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 {

View File

@ -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<String>, mut metadata: Option<fs::Metadata>) -> IOResult<File<'dir>> {
pub fn new(path: &Path, parent: Option<&'dir Dir>, mut filename: Option<String>) -> IOResult<File<'dir>> {
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)