Serenity Operating System
1/*
2 * Copyright (c) 2020, Fei Wu <f.eiwu@yahoo.com>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <AK/DeprecatedString.h>
8#include <AK/LexicalPath.h>
9#include <AK/NumberFormat.h>
10#include <AK/Vector.h>
11#include <LibCore/ArgsParser.h>
12#include <LibCore/DateTime.h>
13#include <LibCore/DirIterator.h>
14#include <LibCore/File.h>
15#include <LibCore/System.h>
16#include <LibMain/Main.h>
17#include <limits.h>
18#include <string.h>
19
20struct DuOption {
21 enum class TimeType {
22 NotUsed,
23 Modification,
24 Access,
25 Status
26 };
27
28 bool human_readable = false;
29 bool human_readable_si = false;
30 bool all = false;
31 bool apparent_size = false;
32 i64 threshold = 0;
33 TimeType time_type = TimeType::NotUsed;
34 Vector<DeprecatedString> excluded_patterns;
35 u64 block_size = 1024;
36 size_t max_depth = SIZE_MAX;
37};
38
39static ErrorOr<void> parse_args(Main::Arguments arguments, Vector<DeprecatedString>& files, DuOption& du_option);
40static ErrorOr<u64> print_space_usage(DeprecatedString const& path, DuOption const& du_option, size_t current_depth, bool inside_dir = false);
41
42ErrorOr<int> serenity_main(Main::Arguments arguments)
43{
44 Vector<DeprecatedString> files;
45 DuOption du_option;
46
47 TRY(parse_args(arguments, files, du_option));
48
49 for (auto const& file : files)
50 TRY(print_space_usage(file, du_option, 0));
51
52 return 0;
53}
54
55ErrorOr<void> parse_args(Main::Arguments arguments, Vector<DeprecatedString>& files, DuOption& du_option)
56{
57 bool summarize = false;
58 StringView pattern;
59 StringView exclude_from;
60 Vector<StringView> files_to_process;
61
62 Core::ArgsParser::Option time_option {
63 Core::ArgsParser::OptionArgumentMode::Required,
64 "Show time of type time-type of any file in the directory, or any of its subdirectories. "
65 "Available choices: mtime, modification, ctime, status, use, atime, access",
66 "time",
67 0,
68 "time-type",
69 [&du_option](StringView option) {
70 if (option == "mtime"sv || option == "modification"sv)
71 du_option.time_type = DuOption::TimeType::Modification;
72 else if (option == "ctime"sv || option == "status"sv || option == "use"sv)
73 du_option.time_type = DuOption::TimeType::Status;
74 else if (option == "atime"sv || option == "access"sv)
75 du_option.time_type = DuOption::TimeType::Access;
76 else
77 return false;
78
79 return true;
80 }
81 };
82
83 Core::ArgsParser::Option block_size_1k_option {
84 Core::ArgsParser::OptionArgumentMode::None,
85 "Equivalent to `--block-size 1024`",
86 nullptr,
87 'k',
88 nullptr,
89 [&du_option](StringView) {
90 du_option.block_size = 1024;
91 return true;
92 }
93 };
94
95 Core::ArgsParser args_parser;
96 args_parser.set_general_help("Display actual or apparent disk usage of files or directories.");
97 args_parser.add_option(du_option.all, "Write counts for all files, not just directories", "all", 'a');
98 args_parser.add_option(du_option.apparent_size, "Print apparent sizes, rather than disk usage", "apparent-size", 0);
99 args_parser.add_option(du_option.human_readable, "Print human-readable sizes", "human-readable", 'h');
100 args_parser.add_option(du_option.human_readable_si, "Print human-readable sizes in SI units", "si", 0);
101 args_parser.add_option(du_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");
102 args_parser.add_option(summarize, "Display only a total for each argument", "summarize", 's');
103 args_parser.add_option(du_option.threshold, "Exclude entries smaller than size if positive, or entries greater than size if negative", "threshold", 't', "size");
104 args_parser.add_option(move(time_option));
105 args_parser.add_option(pattern, "Exclude files that match pattern", "exclude", 0, "pattern");
106 args_parser.add_option(exclude_from, "Exclude files that match any pattern in file", "exclude-from", 'X', "file");
107 args_parser.add_option(du_option.block_size, "Outputs file sizes as the required blocks with the given size (defaults to 1024)", "block-size", 'B', "size");
108 args_parser.add_option(move(block_size_1k_option));
109 args_parser.add_positional_argument(files_to_process, "File to process", "file", Core::ArgsParser::Required::No);
110 args_parser.parse(arguments);
111
112 if (summarize)
113 du_option.max_depth = 0;
114
115 if (!pattern.is_empty())
116 du_option.excluded_patterns.append(pattern);
117 if (!exclude_from.is_empty()) {
118 auto file = TRY(Core::File::open(exclude_from, Core::File::OpenMode::Read));
119 auto const buff = TRY(file->read_until_eof());
120 if (!buff.is_empty()) {
121 DeprecatedString patterns = DeprecatedString::copy(buff, Chomp);
122 du_option.excluded_patterns.extend(patterns.split('\n'));
123 }
124 }
125
126 for (auto const& file : files_to_process) {
127 files.append(file);
128 }
129
130 if (files.is_empty()) {
131 files.append(".");
132 }
133
134 return {};
135}
136
137ErrorOr<u64> print_space_usage(DeprecatedString const& path, DuOption const& du_option, size_t current_depth, bool inside_dir)
138{
139 u64 size = 0;
140 struct stat path_stat = TRY(Core::System::lstat(path));
141 bool const is_directory = S_ISDIR(path_stat.st_mode);
142 if (is_directory) {
143 auto di = Core::DirIterator(path, Core::DirIterator::SkipParentAndBaseDir);
144 if (di.has_error()) {
145 auto error = di.error();
146 outln("du: cannot read directory '{}': {}", path, error);
147 return error;
148 }
149
150 while (di.has_next()) {
151 auto const child_path = di.next_full_path();
152 size += TRY(print_space_usage(child_path, du_option, current_depth + 1, true));
153 }
154 }
155
156 auto const basename = LexicalPath::basename(path);
157 for (auto const& pattern : du_option.excluded_patterns) {
158 if (basename.matches(pattern, CaseSensitivity::CaseSensitive))
159 return { 0 };
160 }
161
162 if (!du_option.apparent_size) {
163 constexpr auto block_size = 512;
164 size += path_stat.st_blocks * block_size;
165 } else {
166 size += path_stat.st_size;
167 }
168
169 bool is_beyond_depth = current_depth > du_option.max_depth;
170 bool is_inner_file = inside_dir && !is_directory;
171 bool is_outside_threshold = (du_option.threshold > 0 && size < static_cast<u64>(du_option.threshold)) || (du_option.threshold < 0 && size > static_cast<u64>(-du_option.threshold));
172
173 // All of these still count towards the full size, they are just not reported on individually.
174 if (is_beyond_depth || (is_inner_file && !du_option.all) || is_outside_threshold)
175 return size;
176
177 if (du_option.human_readable) {
178 out("{}", human_readable_size(size));
179 } else if (du_option.human_readable_si) {
180 out("{}", human_readable_size(size, AK::HumanReadableBasedOn::Base10));
181 } else {
182 out("{}", ceil_div(size, du_option.block_size));
183 }
184
185 if (du_option.time_type == DuOption::TimeType::NotUsed) {
186 outln("\t{}", path);
187 } else {
188 auto time = path_stat.st_mtime;
189 switch (du_option.time_type) {
190 case DuOption::TimeType::Access:
191 time = path_stat.st_atime;
192 break;
193 case DuOption::TimeType::Status:
194 time = path_stat.st_ctime;
195 break;
196 default:
197 break;
198 }
199
200 auto const formatted_time = Core::DateTime::from_timestamp(time).to_deprecated_string();
201 outln("\t{}\t{}", formatted_time, path);
202 }
203
204 return { size };
205}