2020-02-26 05:43:55 +03:00
/*
* Copyright ( c ) 2020 , Fei Wu < f . eiwu @ yahoo . com >
*
2021-04-22 11:24:48 +03:00
* SPDX - License - Identifier : BSD - 2 - Clause
2020-02-26 05:43:55 +03:00
*/
2020-05-26 14:52:44 +03:00
# include <AK/LexicalPath.h>
2021-06-11 20:38:56 +03:00
# include <AK/NumberFormat.h>
2020-02-26 05:43:55 +03:00
# include <AK/String.h>
# include <AK/Vector.h>
# include <LibCore/ArgsParser.h>
# include <LibCore/DateTime.h>
# include <LibCore/DirIterator.h>
# include <LibCore/File.h>
2021-12-18 20:59:15 +03:00
# include <LibCore/System.h>
# include <LibMain/Main.h>
2020-02-26 05:43:55 +03:00
# include <limits.h>
# include <string.h>
# include <sys/stat.h>
struct DuOption {
enum class TimeType {
NotUsed ,
Modification ,
Access ,
Status
} ;
2021-06-11 20:38:56 +03:00
bool human_readable = false ;
2020-02-26 05:43:55 +03:00
bool all = false ;
bool apparent_size = false ;
int threshold = 0 ;
TimeType time_type = TimeType : : NotUsed ;
Vector < String > excluded_patterns ;
} ;
2021-12-18 20:59:15 +03:00
static ErrorOr < void > parse_args ( Main : : Arguments arguments , Vector < String > & files , DuOption & du_option , int & max_depth ) ;
static ErrorOr < void > print_space_usage ( const String & path , const DuOption & du_option , int max_depth ) ;
2020-02-26 05:43:55 +03:00
2021-12-18 20:59:15 +03:00
ErrorOr < int > serenity_main ( Main : : Arguments arguments )
2020-02-26 05:43:55 +03:00
{
Vector < String > files ;
DuOption du_option ;
int max_depth = INT_MAX ;
2021-12-18 20:59:15 +03:00
TRY ( parse_args ( arguments , files , du_option , max_depth ) ) ;
2020-02-26 05:43:55 +03:00
2021-12-18 20:59:15 +03:00
for ( const auto & file : files )
TRY ( print_space_usage ( file , du_option , max_depth ) ) ;
2020-02-26 05:43:55 +03:00
return 0 ;
}
2021-12-18 20:59:15 +03:00
ErrorOr < void > parse_args ( Main : : Arguments arguments , Vector < String > & files , DuOption & du_option , int & max_depth )
2020-02-26 05:43:55 +03:00
{
bool summarize = false ;
const char * pattern = nullptr ;
const char * exclude_from = nullptr ;
Vector < const char * > files_to_process ;
Core : : ArgsParser : : Option time_option {
true ,
" Show time of type time-type of any file in the directory, or any of its subdirectories. "
" Available choices: mtime, modification, ctime, status, use, atime, access " ,
" time " ,
0 ,
" time-type " ,
2021-12-18 20:59:15 +03:00
[ & du_option ] ( StringView s ) {
if ( s = = " mtime " sv | | s = = " modification " sv )
2020-02-26 05:43:55 +03:00
du_option . time_type = DuOption : : TimeType : : Modification ;
2021-12-18 20:59:15 +03:00
else if ( s = = " ctime " sv | | s = = " status " sv | | s = = " use " sv )
2020-02-26 05:43:55 +03:00
du_option . time_type = DuOption : : TimeType : : Status ;
2021-12-18 20:59:15 +03:00
else if ( s = = " atime " sv | | s = = " access " sv )
2020-02-26 05:43:55 +03:00
du_option . time_type = DuOption : : TimeType : : Access ;
else
return false ;
return true ;
}
} ;
Core : : ArgsParser args_parser ;
2020-12-05 18:22:58 +03:00
args_parser . set_general_help ( " Display actual or apparent disk usage of files or directories. " ) ;
2020-02-26 05:43:55 +03:00
args_parser . add_option ( du_option . all , " Write counts for all files, not just directories " , " all " , ' a ' ) ;
args_parser . add_option ( du_option . apparent_size , " Print apparent sizes, rather than disk usage " , " apparent-size " , 0 ) ;
2021-06-11 20:38:56 +03:00
args_parser . add_option ( du_option . human_readable , " Print human-readable sizes " , " human-readable " , ' h ' ) ;
2020-02-26 05:43:55 +03:00
args_parser . add_option ( max_depth , " Print the total for a directory or file only if it is N or fewer levels below the command line argument " , " max-depth " , ' d ' , " N " ) ;
args_parser . add_option ( summarize , " Display only a total for each argument " , " summarize " , ' s ' ) ;
args_parser . add_option ( du_option . threshold , " Exclude entries smaller than size if positive, or entries greater than size if negative " , " threshold " , ' t ' , " size " ) ;
args_parser . add_option ( move ( time_option ) ) ;
args_parser . add_option ( pattern , " Exclude files that match pattern " , " exclude " , 0 , " pattern " ) ;
args_parser . add_option ( exclude_from , " Exclude files that match any pattern in file " , " exclude_from " , ' X ' , " file " ) ;
args_parser . add_positional_argument ( files_to_process , " File to process " , " file " , Core : : ArgsParser : : Required : : No ) ;
2021-12-18 20:59:15 +03:00
args_parser . parse ( arguments ) ;
2020-02-26 05:43:55 +03:00
if ( summarize )
max_depth = 0 ;
if ( pattern )
du_option . excluded_patterns . append ( pattern ) ;
if ( exclude_from ) {
2021-12-18 20:59:15 +03:00
auto file = TRY ( Core : : File : : open ( exclude_from , Core : : OpenMode : : ReadOnly ) ) ;
2021-05-16 09:47:46 +03:00
const auto buff = file - > read_all ( ) ;
if ( ! buff . is_empty ( ) ) {
2020-02-26 05:43:55 +03:00
String patterns = String : : copy ( buff , Chomp ) ;
2021-06-12 14:24:45 +03:00
du_option . excluded_patterns . extend ( patterns . split ( ' \n ' ) ) ;
2020-02-26 05:43:55 +03:00
}
}
for ( auto * file : files_to_process ) {
files . append ( file ) ;
}
if ( files . is_empty ( ) ) {
files . append ( " . " ) ;
}
2021-12-18 20:59:15 +03:00
return { } ;
2020-02-26 05:43:55 +03:00
}
2021-12-18 20:59:15 +03:00
ErrorOr < void > print_space_usage ( const String & path , const DuOption & du_option , int max_depth )
2020-02-26 05:43:55 +03:00
{
2021-12-18 20:59:15 +03:00
struct stat path_stat = TRY ( Core : : System : : lstat ( path . characters ( ) ) ) ;
2020-02-26 05:43:55 +03:00
if ( - - max_depth > = 0 & & S_ISDIR ( path_stat . st_mode ) ) {
auto di = Core : : DirIterator ( path , Core : : DirIterator : : SkipParentAndBaseDir ) ;
if ( di . has_error ( ) ) {
2021-12-18 20:59:15 +03:00
outln ( " du: cannot read directory '{}': {} " , path , di . error_string ( ) ) ;
return Error : : from_string_literal ( " An error occurred. See previous error. " sv ) ;
2020-02-26 05:43:55 +03:00
}
2021-12-18 20:59:15 +03:00
2020-02-26 05:43:55 +03:00
while ( di . has_next ( ) ) {
const auto child_path = di . next_full_path ( ) ;
if ( du_option . all | | Core : : File : : is_directory ( child_path ) ) {
2021-12-18 20:59:15 +03:00
TRY ( print_space_usage ( child_path , du_option , max_depth ) ) ;
2020-02-26 05:43:55 +03:00
}
}
}
2021-06-29 17:46:16 +03:00
const auto basename = LexicalPath : : basename ( path ) ;
2020-02-26 05:43:55 +03:00
for ( const auto & pattern : du_option . excluded_patterns ) {
if ( basename . matches ( pattern , CaseSensitivity : : CaseSensitive ) )
2021-12-18 20:59:15 +03:00
return { } ;
2020-02-26 05:43:55 +03:00
}
2021-05-02 01:00:52 +03:00
off_t size = path_stat . st_size ;
2020-02-26 05:43:55 +03:00
if ( du_option . apparent_size ) {
const auto block_size = 512 ;
size = path_stat . st_blocks * block_size ;
}
if ( ( du_option . threshold > 0 & & size < du_option . threshold ) | | ( du_option . threshold < 0 & & size > - du_option . threshold ) )
2021-12-18 20:59:15 +03:00
return { } ;
2020-02-26 05:43:55 +03:00
2021-06-11 20:38:56 +03:00
if ( du_option . human_readable ) {
out ( " {} " , human_readable_size ( size ) ) ;
} else {
const long long block_size = 1024 ;
size = size / block_size + ( size % block_size ! = 0 ) ;
out ( " {} " , size ) ;
}
2020-02-26 05:43:55 +03:00
2021-06-11 20:38:56 +03:00
if ( du_option . time_type = = DuOption : : TimeType : : NotUsed ) {
outln ( " \t {} " , path ) ;
} else {
2020-02-26 05:43:55 +03:00
auto time = path_stat . st_mtime ;
switch ( du_option . time_type ) {
case DuOption : : TimeType : : Access :
time = path_stat . st_atime ;
break ;
case DuOption : : TimeType : : Status :
time = path_stat . st_ctime ;
2021-07-05 20:17:08 +03:00
break ;
2020-02-26 05:43:55 +03:00
default :
break ;
}
const auto formatted_time = Core : : DateTime : : from_timestamp ( time ) . to_string ( ) ;
2021-06-11 20:38:56 +03:00
outln ( " \t {} \t {} " , formatted_time , path ) ;
2020-02-26 05:43:55 +03:00
}
2021-12-18 20:59:15 +03:00
return { } ;
2020-02-26 05:43:55 +03:00
}