Skip to content

Commit

Permalink
Avoid calling GetFileInformationByHandleEx to get blocks if --apparen…
Browse files Browse the repository at this point in the history
…t-size is passed
  • Loading branch information
jesseschalken authored and sylvestre committed Nov 28, 2024
1 parent 75de5a0 commit 89686db
Showing 1 changed file with 24 additions and 15 deletions.
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 path.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 path.is_dir() { 0 } else { metadata.len() },
blocks: size_on_disk / 1024 * 2,
size: if options.apparent_size {
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 @@ -722,6 +731,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
count_links: matches.get_flag(options::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 @@ -744,7 +754,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

0 comments on commit 89686db

Please sign in to comment.