From 9af302920a81e7f40392236718b5dc7186e72a7a Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Sat, 23 Jul 2022 18:25:21 +0200 Subject: [PATCH] du: Use 64-bit integers when handling file sizes or related values We may very well dip into files larger than 4G at some point, so 32-bit values are not enough, and the 64-bit sized `off_t` doesn't fully make sense either, as it features negative values. Instead, switch to the explicit type of `u64` everywhere, which is the same size on all platforms and is unsigned. The exception to this is the threshold, which needs to be signed instead of unsigned. --- Userland/Utilities/du.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Userland/Utilities/du.cpp b/Userland/Utilities/du.cpp index 81c816d115..593cd367f6 100644 --- a/Userland/Utilities/du.cpp +++ b/Userland/Utilities/du.cpp @@ -29,15 +29,15 @@ struct DuOption { bool human_readable = false; bool all = false; bool apparent_size = false; - int threshold = 0; + i64 threshold = 0; TimeType time_type = TimeType::NotUsed; Vector excluded_patterns; - size_t block_size = 1024; + u64 block_size = 1024; size_t max_depth = SIZE_MAX; }; static ErrorOr parse_args(Main::Arguments arguments, Vector& files, DuOption& du_option); -static ErrorOr print_space_usage(String const& path, DuOption const& du_option, size_t current_depth, bool inside_dir = false); +static ErrorOr print_space_usage(String const& path, DuOption const& du_option, size_t current_depth, bool inside_dir = false); ErrorOr serenity_main(Main::Arguments arguments) { @@ -134,10 +134,10 @@ ErrorOr parse_args(Main::Arguments arguments, Vector& files, DuOpt return {}; } -ErrorOr print_space_usage(String const& path, DuOption const& du_option, size_t current_depth, bool inside_dir) +ErrorOr print_space_usage(String const& path, DuOption const& du_option, size_t current_depth, bool inside_dir) { struct stat path_stat = TRY(Core::System::lstat(path)); - off_t directory_size = 0; + 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); @@ -158,7 +158,7 @@ ErrorOr print_space_usage(String const& path, DuOption const& du_option, return { 0 }; } - size_t size = path_stat.st_size; + u64 size = path_stat.st_size; if (!du_option.apparent_size) { constexpr auto block_size = 512; size = path_stat.st_blocks * block_size; @@ -170,7 +170,7 @@ ErrorOr print_space_usage(String const& path, DuOption const& du_option, 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))) + 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)