du: Port to LibMain

This ports 'du' utility to LibMain. Also moves to use StringView and
StringView literals more instead of raw C strings.
This commit is contained in:
Kenneth Myhra 2021-12-18 18:59:15 +01:00 committed by Andreas Kling
parent 3528563e3d
commit 2e0f53c25d
Notes: sideshowbarker 2024-07-17 21:51:34 +09:00
2 changed files with 26 additions and 38 deletions

View File

@ -85,6 +85,7 @@ target_link_libraries(diff LibDiff LibMain)
target_link_libraries(dirname LibMain)
target_link_libraries(disasm LibX86)
target_link_libraries(dmesg LibMain)
target_link_libraries(du LibMain)
target_link_libraries(echo LibMain)
target_link_libraries(env LibMain)
target_link_libraries(expr LibRegex LibMain)

View File

@ -4,8 +4,6 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Assertions.h>
#include <AK/ByteBuffer.h>
#include <AK/LexicalPath.h>
#include <AK/NumberFormat.h>
#include <AK/String.h>
@ -14,8 +12,9 @@
#include <LibCore/DateTime.h>
#include <LibCore/DirIterator.h>
#include <LibCore/File.h>
#include <LibCore/System.h>
#include <LibMain/Main.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
@ -35,27 +34,24 @@ struct DuOption {
Vector<String> excluded_patterns;
};
static int parse_args(int argc, char** argv, Vector<String>& files, DuOption& du_option, int& max_depth);
static int print_space_usage(const String& path, const DuOption& du_option, int max_depth);
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);
int main(int argc, char** argv)
ErrorOr<int> serenity_main(Main::Arguments arguments)
{
Vector<String> files;
DuOption du_option;
int max_depth = INT_MAX;
if (parse_args(argc, argv, files, du_option, max_depth))
return 1;
TRY(parse_args(arguments, files, du_option, max_depth));
for (const auto& file : files) {
if (print_space_usage(file, du_option, max_depth))
return 1;
}
for (const auto& file : files)
TRY(print_space_usage(file, du_option, max_depth));
return 0;
}
int parse_args(int argc, char** argv, Vector<String>& files, DuOption& du_option, int& max_depth)
ErrorOr<void> parse_args(Main::Arguments arguments, Vector<String>& files, DuOption& du_option, int& max_depth)
{
bool summarize = false;
const char* pattern = nullptr;
@ -69,12 +65,12 @@ int parse_args(int argc, char** argv, Vector<String>& files, DuOption& du_option
"time",
0,
"time-type",
[&du_option](const char* s) {
if (!strcmp(s, "mtime") || !strcmp(s, "modification"))
[&du_option](StringView s) {
if (s == "mtime"sv || s == "modification"sv)
du_option.time_type = DuOption::TimeType::Modification;
else if (!strcmp(s, "ctime") || !strcmp(s, "status") || !strcmp(s, "use"))
else if (s == "ctime"sv || s == "status"sv || s == "use"sv)
du_option.time_type = DuOption::TimeType::Status;
else if (!strcmp(s, "atime") || !strcmp(s, "access"))
else if (s == "atime"sv || s == "access"sv)
du_option.time_type = DuOption::TimeType::Access;
else
return false;
@ -95,7 +91,7 @@ int parse_args(int argc, char** argv, Vector<String>& files, DuOption& du_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);
args_parser.parse(argc, argv);
args_parser.parse(arguments);
if (summarize)
max_depth = 0;
@ -103,9 +99,7 @@ int parse_args(int argc, char** argv, Vector<String>& files, DuOption& du_option
if (pattern)
du_option.excluded_patterns.append(pattern);
if (exclude_from) {
auto file = Core::File::construct(exclude_from);
bool success = file->open(Core::OpenMode::ReadOnly);
VERIFY(success);
auto file = TRY(Core::File::open(exclude_from, Core::OpenMode::ReadOnly));
const auto buff = file->read_all();
if (!buff.is_empty()) {
String patterns = String::copy(buff, Chomp);
@ -121,30 +115,23 @@ int parse_args(int argc, char** argv, Vector<String>& files, DuOption& du_option
files.append(".");
}
return 0;
return {};
}
int print_space_usage(const String& path, const DuOption& du_option, int max_depth)
ErrorOr<void> print_space_usage(const String& path, const DuOption& du_option, int max_depth)
{
struct stat path_stat;
if (lstat(path.characters(), &path_stat) < 0) {
perror("lstat");
return 1;
}
int ret = 0;
struct stat path_stat = TRY(Core::System::lstat(path.characters()));
if (--max_depth >= 0 && S_ISDIR(path_stat.st_mode)) {
auto di = Core::DirIterator(path, Core::DirIterator::SkipParentAndBaseDir);
if (di.has_error()) {
warnln("du: cannot read directory '{}': {}", path, di.error_string());
ret = 1;
outln("du: cannot read directory '{}': {}", path, di.error_string());
return Error::from_string_literal("An error occurred. See previous error."sv);
}
while (di.has_next()) {
const auto child_path = di.next_full_path();
if (du_option.all || Core::File::is_directory(child_path)) {
if (print_space_usage(child_path, du_option, max_depth))
return 1;
TRY(print_space_usage(child_path, du_option, max_depth));
}
}
}
@ -152,7 +139,7 @@ int print_space_usage(const String& path, const DuOption& du_option, int max_dep
const auto basename = LexicalPath::basename(path);
for (const auto& pattern : du_option.excluded_patterns) {
if (basename.matches(pattern, CaseSensitivity::CaseSensitive))
return 0;
return {};
}
off_t size = path_stat.st_size;
@ -162,7 +149,7 @@ int print_space_usage(const String& path, const DuOption& du_option, int max_dep
}
if ((du_option.threshold > 0 && size < du_option.threshold) || (du_option.threshold < 0 && size > -du_option.threshold))
return 0;
return {};
if (du_option.human_readable) {
out("{}", human_readable_size(size));
@ -191,5 +178,5 @@ int print_space_usage(const String& path, const DuOption& du_option, int max_dep
outln("\t{}\t{}", formatted_time, path);
}
return ret;
return {};
}