Skip to content

Commit

Permalink
Use metadata from DirEntry where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseschalken committed Nov 3, 2024
1 parent d66fde2 commit 073ab83
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/uu/du/src/du.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::error::Error;
use std::fmt::Display;
#[cfg(not(windows))]
use std::fs::Metadata;
use std::fs::{self, File};
use std::fs::{self, DirEntry, File};
use std::io::{BufRead, BufReader};
#[cfg(not(windows))]
use std::os::unix::fs::MetadataExt;
Expand Down Expand Up @@ -138,7 +138,7 @@ struct Stat {
}

impl Stat {
fn new(path: &Path, options: &TraversalOptions) -> std::io::Result<Self> {
fn new(path: &Path, dir_entry: Option<&DirEntry>, options: &TraversalOptions) -> std::io::Result<Self> {
// Determine whether to dereference (follow) the symbolic link
let should_dereference = match &options.dereference {
Deref::All => true,
Expand All @@ -149,8 +149,11 @@ impl Stat {
let metadata = if should_dereference {
// Get metadata, following symbolic links if necessary
fs::metadata(path)
} else if let Some(dir_entry) = dir_entry {
// Get metadata directly from the DirEntry, which is faster on Windows
dir_entry.metadata()
} else {
// Get metadata without following symbolic links
// Get metadata from the filesystem without following symbolic links
fs::symlink_metadata(path)
}?;

Expand Down Expand Up @@ -319,7 +322,7 @@ fn du(
'file_loop: for f in read {
match f {
Ok(entry) => {
match Stat::new(&entry.path(), options) {
match Stat::new(&entry.path(), Some(&entry), options) {
Ok(this_stat) => {
// We have an exclude list
for pattern in &options.excludes {
Expand Down Expand Up @@ -765,7 +768,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
}

// Check existence of path provided in argument
if let Ok(stat) = Stat::new(&path, &traversal_options) {
if let Ok(stat) = Stat::new(&path, None, &traversal_options) {
// Kick off the computation of disk usage from the initial path
let mut seen_inodes: HashSet<FileInfo> = HashSet::new();
if let Some(inode) = stat.inode {
Expand Down

0 comments on commit 073ab83

Please sign in to comment.