From e4cdc7f685e41218a01274952694bccdb0cb2c35 Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Sat, 23 Jul 2022 18:36:03 +0200 Subject: [PATCH] du: Only use unmodified file sizes internally This keeps us from repeatedly applying the block size calculation on already processed values. --- Userland/Utilities/du.cpp | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/Userland/Utilities/du.cpp b/Userland/Utilities/du.cpp index 593cd367f6..32b906ec5d 100644 --- a/Userland/Utilities/du.cpp +++ b/Userland/Utilities/du.cpp @@ -136,8 +136,8 @@ ErrorOr parse_args(Main::Arguments arguments, Vector& files, DuOpt ErrorOr print_space_usage(String const& path, DuOption const& du_option, size_t current_depth, bool inside_dir) { + u64 size = 0; struct stat path_stat = TRY(Core::System::lstat(path)); - u64 directory_size = 0; bool const is_directory = S_ISDIR(path_stat.st_mode); if (is_directory) { auto di = Core::DirIterator(path, Core::DirIterator::SkipParentAndBaseDir); @@ -148,7 +148,7 @@ ErrorOr print_space_usage(String const& path, DuOption const& du_option, si while (di.has_next()) { auto const child_path = di.next_full_path(); - directory_size += TRY(print_space_usage(child_path, du_option, current_depth + 1, true)); + size += TRY(print_space_usage(child_path, du_option, current_depth + 1, true)); } } @@ -158,31 +158,26 @@ ErrorOr print_space_usage(String const& path, DuOption const& du_option, si return { 0 }; } - u64 size = path_stat.st_size; if (!du_option.apparent_size) { constexpr auto block_size = 512; - size = path_stat.st_blocks * block_size; + size += path_stat.st_blocks * block_size; + } else { + size += path_stat.st_size; } if (inside_dir && !du_option.all && !is_directory) return size; - if (is_directory) - size = directory_size; - if ((du_option.threshold > 0 && size < static_cast(du_option.threshold)) || (du_option.threshold < 0 && size > static_cast(-du_option.threshold))) return { 0 }; - if (!du_option.human_readable) - size = ceil_div(size, du_option.block_size); - if (current_depth > du_option.max_depth) return { size }; if (du_option.human_readable) { out("{}", human_readable_size(size)); } else { - out("{}", size); + out("{}", ceil_div(size, du_option.block_size)); } if (du_option.time_type == DuOption::TimeType::NotUsed) {