1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 05:07:45 +00:00

du: Only use unmodified file sizes internally

This keeps us from repeatedly applying the block size calculation on
already processed values.
This commit is contained in:
Tim Schumacher 2022-07-23 18:36:03 +02:00 committed by Linus Groh
parent 9af302920a
commit e4cdc7f685

View file

@ -136,8 +136,8 @@ ErrorOr<void> parse_args(Main::Arguments arguments, Vector<String>& files, DuOpt
ErrorOr<u64> print_space_usage(String const& path, DuOption const& du_option, size_t current_depth, bool inside_dir) ErrorOr<u64> 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)); struct stat path_stat = TRY(Core::System::lstat(path));
u64 directory_size = 0;
bool const is_directory = S_ISDIR(path_stat.st_mode); bool const is_directory = S_ISDIR(path_stat.st_mode);
if (is_directory) { if (is_directory) {
auto di = Core::DirIterator(path, Core::DirIterator::SkipParentAndBaseDir); auto di = Core::DirIterator(path, Core::DirIterator::SkipParentAndBaseDir);
@ -148,7 +148,7 @@ ErrorOr<u64> print_space_usage(String const& path, DuOption const& du_option, si
while (di.has_next()) { while (di.has_next()) {
auto const child_path = di.next_full_path(); 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<u64> print_space_usage(String const& path, DuOption const& du_option, si
return { 0 }; return { 0 };
} }
u64 size = path_stat.st_size;
if (!du_option.apparent_size) { if (!du_option.apparent_size) {
constexpr auto block_size = 512; 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) if (inside_dir && !du_option.all && !is_directory)
return size; return size;
if (is_directory)
size = directory_size;
if ((du_option.threshold > 0 && size < static_cast<u64>(du_option.threshold)) || (du_option.threshold < 0 && size > static_cast<u64>(-du_option.threshold))) if ((du_option.threshold > 0 && size < static_cast<u64>(du_option.threshold)) || (du_option.threshold < 0 && size > static_cast<u64>(-du_option.threshold)))
return { 0 }; return { 0 };
if (!du_option.human_readable)
size = ceil_div(size, du_option.block_size);
if (current_depth > du_option.max_depth) if (current_depth > du_option.max_depth)
return { size }; return { size };
if (du_option.human_readable) { if (du_option.human_readable) {
out("{}", human_readable_size(size)); out("{}", human_readable_size(size));
} else { } else {
out("{}", size); out("{}", ceil_div(size, du_option.block_size));
} }
if (du_option.time_type == DuOption::TimeType::NotUsed) { if (du_option.time_type == DuOption::TimeType::NotUsed) {