Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

du: Avoid unnecessary call to GetFileInformationByHandleEx #6841

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 24 additions & 15 deletions src/uu/du/src/du.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ struct TraversalOptions {
dereference: Deref,
count_links: bool,
verbose: bool,
apparent_size: bool,
excludes: Vec<Pattern>,
}

Expand All @@ -90,7 +91,6 @@ struct StatPrinter {
inodes: bool,
max_depth: Option<usize>,
threshold: Option<Threshold>,
apparent_size: bool,
size_format: SizeFormat,
time: Option<Time>,
time_format: String,
Expand Down Expand Up @@ -128,8 +128,8 @@ struct FileInfo {
struct Stat {
path: PathBuf,
is_dir: bool,
/// size on disk unless --apparent-size is passed
size: u64,
blocks: u64,
inodes: u64,
inode: Option<FileInfo>,
created: Option<u64>,
Expand Down Expand Up @@ -171,8 +171,17 @@ impl Stat {
Ok(Self {
path: path.to_path_buf(),
is_dir: metadata.is_dir(),
size: if metadata.is_dir() { 0 } else { metadata.len() },
blocks: metadata.blocks(),
size: if options.apparent_size {
if metadata.is_dir() {
0
} else {
metadata.len()
}
} else {
// The st_blocks field indicates the number of blocks allocated to the file, 512-byte units.
// See: http://linux.die.net/man/2/stat
metadata.blocks() * 512
},
inodes: 1,
inode: Some(file_info),
created: birth_u64(&metadata),
Expand All @@ -183,14 +192,20 @@ impl Stat {

#[cfg(windows)]
{
let size_on_disk = get_size_on_disk(path);
let file_info = get_file_info(path);

Ok(Self {
path: path.to_path_buf(),
is_dir: metadata.is_dir(),
size: if metadata.is_dir() { 0 } else { metadata.len() },
blocks: size_on_disk / 1024 * 2,
size: if options.apparent_size {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

almost duplicate code, maybe move it above the windows / not windows

if metadata.is_dir() {
0
} else {
metadata.len()
}
} else {
get_size_on_disk(path)
},
inodes: 1,
inode: file_info,
created: windows_creation_time_to_unix_time(metadata.creation_time()),
Expand Down Expand Up @@ -377,7 +392,6 @@ fn du(

if !options.separate_dirs {
my_stat.size += this_stat.size;
my_stat.blocks += this_stat.blocks;
my_stat.inodes += this_stat.inodes;
}
print_tx.send(Ok(StatPrintInfo {
Expand All @@ -386,7 +400,6 @@ fn du(
}))?;
} else {
my_stat.size += this_stat.size;
my_stat.blocks += this_stat.blocks;
my_stat.inodes += 1;
if options.all {
print_tx.send(Ok(StatPrintInfo {
Expand Down Expand Up @@ -508,12 +521,8 @@ impl StatPrinter {
fn choose_size(&self, stat: &Stat) -> u64 {
if self.inodes {
stat.inodes
} else if self.apparent_size {
stat.size
} else {
// The st_blocks field indicates the number of blocks allocated to the file, 512-byte units.
// See: http://linux.die.net/man/2/stat
stat.blocks * 512
stat.size
}
}

Expand Down Expand Up @@ -728,6 +737,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
count_links,
verbose: matches.get_flag(options::VERBOSE),
excludes: build_exclude_patterns(&matches)?,
apparent_size: matches.get_flag(options::APPARENT_SIZE) || matches.get_flag(options::BYTES),
};

let time_format = if time.is_some() {
Expand All @@ -750,7 +760,6 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
})
})
.transpose()?,
apparent_size: matches.get_flag(options::APPARENT_SIZE) || matches.get_flag(options::BYTES),
time,
time_format,
line_ending: LineEnding::from_zero_flag(matches.get_flag(options::NULL)),
Expand Down
Loading