forked from SerenityOS/serenity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
du.cpp
261 lines (221 loc) Β· 9.35 KB
/
du.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
* Copyright (c) 2020, Fei Wu <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/ByteString.h>
#include <AK/LexicalPath.h>
#include <AK/NumberFormat.h>
#include <AK/Vector.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/DateTime.h>
#include <LibCore/DirIterator.h>
#include <LibCore/File.h>
#include <LibCore/System.h>
#include <LibMain/Main.h>
#include <limits.h>
#include <string.h>
struct DuOption {
enum class TimeType {
NotUsed,
Modification,
Access,
Status
};
bool human_readable = false;
bool human_readable_si = false;
bool all = false;
bool apparent_size = false;
bool one_file_system = false;
TimeType time_type = TimeType::NotUsed;
Vector<ByteString> excluded_patterns;
u64 block_size = 1024;
size_t max_depth = SIZE_MAX;
u64 maximum_size_threshold = UINT64_MAX;
u64 minimum_size_threshold = 0;
};
struct VisitedFile {
dev_t device;
ino_t inode;
};
template<>
struct AK::Traits<VisitedFile> : public DefaultTraits<VisitedFile> {
static unsigned hash(VisitedFile const& visited_file)
{
return pair_int_hash(u64_hash(visited_file.device), u64_hash(visited_file.inode));
}
static bool equals(VisitedFile const& a, VisitedFile const& b)
{
return a.device == b.device && a.inode == b.inode;
}
};
static HashTable<VisitedFile> s_visited_files;
static ErrorOr<void> parse_args(Main::Arguments arguments, Vector<ByteString>& files, DuOption& du_option);
static u64 print_space_usage(ByteString const& path, DuOption const& du_option, size_t current_depth, Optional<dev_t> root_device = {});
ErrorOr<int> serenity_main(Main::Arguments arguments)
{
Vector<ByteString> files;
DuOption du_option;
TRY(parse_args(arguments, files, du_option));
for (auto const& file : files)
print_space_usage(file, du_option, 0);
return 0;
}
ErrorOr<void> parse_args(Main::Arguments arguments, Vector<ByteString>& files, DuOption& du_option)
{
bool summarize = false;
StringView pattern;
StringView exclude_from;
Vector<StringView> files_to_process;
Core::ArgsParser::Option time_option {
Core::ArgsParser::OptionArgumentMode::Required,
"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",
[&du_option](StringView option) {
if (option == "mtime"sv || option == "modification"sv)
du_option.time_type = DuOption::TimeType::Modification;
else if (option == "ctime"sv || option == "status"sv || option == "use"sv)
du_option.time_type = DuOption::TimeType::Status;
else if (option == "atime"sv || option == "access"sv)
du_option.time_type = DuOption::TimeType::Access;
else
return false;
return true;
}
};
Core::ArgsParser::Option block_size_1k_option {
Core::ArgsParser::OptionArgumentMode::None,
"Equivalent to `--block-size 1024`",
nullptr,
'k',
nullptr,
[&du_option](StringView) {
du_option.block_size = 1024;
return true;
}
};
i64 threshold = 0;
Core::ArgsParser args_parser;
args_parser.set_general_help("Display actual or apparent disk usage of files or directories.");
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");
args_parser.add_option(du_option.human_readable, "Print human-readable sizes", "human-readable", 'h');
args_parser.add_option(du_option.human_readable_si, "Print human-readable sizes in SI units", "si");
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");
args_parser.add_option(summarize, "Display only a total for each argument", "summarize", 's');
args_parser.add_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_option(du_option.one_file_system, "Don't traverse directories on different file systems", "one-file-system", 'x');
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");
args_parser.add_option(move(block_size_1k_option));
args_parser.add_option(du_option.maximum_size_threshold, "Exclude files with size above a specified size (defaults to uint64_t max value)", "max-size", 0, "size");
args_parser.add_option(du_option.minimum_size_threshold, "Exclude files with size below a specified size (defaults to 0)", "min-size", 0, "size");
args_parser.add_positional_argument(files_to_process, "File to process", "file", Core::ArgsParser::Required::No);
args_parser.parse(arguments);
if (threshold > 0)
du_option.minimum_size_threshold = static_cast<u64>(threshold);
else if (threshold < 0)
du_option.maximum_size_threshold = static_cast<u64>(-threshold);
if (du_option.maximum_size_threshold < du_option.minimum_size_threshold)
return Error::from_string_literal("Invalid minimum size exclusion is above maximum size exclusion");
if (summarize)
du_option.max_depth = 0;
if (!pattern.is_empty())
du_option.excluded_patterns.append(pattern);
if (!exclude_from.is_empty()) {
auto file = TRY(Core::File::open(exclude_from, Core::File::OpenMode::Read));
auto const buff = TRY(file->read_until_eof());
if (!buff.is_empty()) {
ByteString patterns = ByteString::copy(buff, Chomp);
du_option.excluded_patterns.extend(patterns.split('\n'));
}
}
for (auto const& file : files_to_process) {
files.append(file);
}
if (files.is_empty()) {
files.append(".");
}
return {};
}
u64 print_space_usage(ByteString const& path, DuOption const& du_option, size_t current_depth, Optional<dev_t> root_device)
{
u64 size = 0;
auto path_stat_or_error = Core::System::lstat(path);
if (path_stat_or_error.is_error()) {
warnln("du: cannot stat '{}': {}", path, path_stat_or_error.release_error());
return 0;
}
auto path_stat = path_stat_or_error.release_value();
if (!root_device.has_value()) {
root_device = path_stat.st_dev;
}
if (du_option.one_file_system && root_device.value() != path_stat.st_dev) {
return 0;
}
VisitedFile visited_file { path_stat.st_dev, path_stat.st_ino };
if (s_visited_files.contains(visited_file)) {
return 0;
}
s_visited_files.set(visited_file);
bool const is_directory = S_ISDIR(path_stat.st_mode);
if (is_directory) {
auto di = Core::DirIterator(path, Core::DirIterator::SkipParentAndBaseDir);
if (di.has_error()) {
auto error = di.error();
warnln("du: cannot read directory '{}': {}", path, error);
return 0;
}
while (di.has_next()) {
auto const child_path = di.next_full_path();
size += print_space_usage(child_path, du_option, current_depth + 1, root_device);
}
}
auto const basename = LexicalPath::basename(path);
for (auto const& pattern : du_option.excluded_patterns) {
if (basename.matches(pattern, CaseSensitivity::CaseSensitive))
return 0;
}
if (!du_option.apparent_size) {
constexpr auto block_size = 512;
size += path_stat.st_blocks * block_size;
} else {
size += path_stat.st_size;
}
bool is_beyond_depth = current_depth > du_option.max_depth;
bool is_inner_file = current_depth > 0 && !is_directory;
bool is_outside_size_range = (du_option.minimum_size_threshold > size) || (du_option.maximum_size_threshold < size);
// All of these still count towards the full size, they are just not reported on individually.
if (is_beyond_depth || (is_inner_file && !du_option.all) || is_outside_size_range)
return size;
if (du_option.human_readable) {
out("{}", human_readable_size(size));
} else if (du_option.human_readable_si) {
out("{}", human_readable_size(size, AK::HumanReadableBasedOn::Base10));
} else {
out("{}", ceil_div(size, du_option.block_size));
}
if (du_option.time_type == DuOption::TimeType::NotUsed) {
outln("\t{}", path);
} else {
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;
break;
default:
break;
}
auto const formatted_time = Core::DateTime::from_timestamp(time).to_byte_string();
outln("\t{}\t{}", formatted_time, path);
}
return size;
}