LibCore+Everywhere: Return an Error from DirIterator::error()

This also removes DirIterator::error_string(), since the same strerror()
string will be included when you print the Error itself. Except in `ls`
which is still using fprintf() for now.
This commit is contained in:
Sam Atkins 2023-03-01 15:55:15 +00:00 committed by Andreas Kling
parent a98ae8f357
commit 774f328783
Notes: sideshowbarker 2024-07-17 06:29:49 +09:00
17 changed files with 44 additions and 46 deletions

View file

@ -348,12 +348,8 @@ inline ErrorOr<Core::DirIterator> path_to_dir_iterator(DeprecatedString path, St
lexical_path = lexical_path.append(subpath);
Core::DirIterator iterator(lexical_path.string(), Core::DirIterator::SkipParentAndBaseDir);
if (iterator.has_error()) {
// FIXME: Make Core::DirIterator return a StringView for its error
// string.
auto const* error_string_ptr = iterator.error_string();
return Error::from_string_view({ error_string_ptr, strlen(error_string_ptr) });
}
if (iterator.has_error())
return iterator.error();
return iterator;
}
@ -361,12 +357,8 @@ inline ErrorOr<Core::DirIterator> path_to_dir_iterator(DeprecatedString path, St
inline ErrorOr<DeprecatedString> next_path_from_dir_iterator(Core::DirIterator& iterator)
{
auto next_path = iterator.next_full_path();
if (iterator.has_error()) {
// FIXME: Make Core::DirIterator return a StringView for its error
// string.
auto const* error_string_ptr = iterator.error_string();
return Error::from_string_view({ error_string_ptr, strlen(error_string_ptr) });
}
if (iterator.has_error())
return iterator.error();
return next_path;
}

View file

@ -61,7 +61,7 @@ private:
Core::DirIterator iterator("/res/keymaps/", Core::DirIterator::Flags::SkipDots);
if (iterator.has_error()) {
GUI::MessageBox::show(nullptr, DeprecatedString::formatted("Error on reading mapping file list: {}", iterator.error_string()), "Keyboard settings"sv, GUI::MessageBox::Type::Error);
GUI::MessageBox::show(nullptr, DeprecatedString::formatted("Error on reading mapping file list: {}", iterator.error()), "Keyboard settings"sv, GUI::MessageBox::Type::Error);
GUI::Application::the()->quit(-1);
}

View file

@ -13,7 +13,6 @@
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
static constexpr size_t FILES_ENCOUNTERED_UPDATE_STEP_SIZE = 25;
@ -93,8 +92,9 @@ HashMap<int, int> TreeNode::populate_filesize_tree(Vector<MountInfo>& mounts, Fu
Core::DirIterator dir_iterator(builder.to_deprecated_string(), Core::DirIterator::SkipParentAndBaseDir);
if (dir_iterator.has_error()) {
int error_sum = error_accumulator.get(dir_iterator.error()).value_or(0);
error_accumulator.set(dir_iterator.error(), error_sum + 1);
auto error_code = dir_iterator.error().code();
int error_sum = error_accumulator.get(error_code).value_or(0);
error_accumulator.set(error_code, error_sum + 1);
} else {
queue_entry.node->m_children = make<Vector<TreeNode>>();
while (dir_iterator.has_next()) {

View file

@ -111,7 +111,7 @@ void ProjectTemplatesModel::rescan_templates()
// Iterate over template manifest INI files in the templates path
Core::DirIterator di(ProjectTemplate::templates_path(), Core::DirIterator::SkipDots);
if (di.has_error()) {
warnln("DirIterator: {}", di.error_string());
warnln("DirIterator: {}", di.error());
return;
}

View file

@ -583,7 +583,7 @@ ErrorOr<void> DeprecatedFile::remove(StringView path, RecursionMode mode)
if (S_ISDIR(path_stat.st_mode) && mode == RecursionMode::Allowed) {
auto di = DirIterator(path, DirIterator::SkipParentAndBaseDir);
if (di.has_error())
return Error::from_errno(di.error());
return di.error();
while (di.has_next()) {
TRY(remove(di.next_full_path(), RecursionMode::Allowed));

View file

@ -17,7 +17,7 @@ DirIterator::DirIterator(DeprecatedString path, Flags flags)
{
m_dir = opendir(m_path.characters());
if (!m_dir) {
m_error = errno;
m_error = Error::from_errno(errno);
}
}
@ -31,7 +31,7 @@ DirIterator::~DirIterator()
DirIterator::DirIterator(DirIterator&& other)
: m_dir(other.m_dir)
, m_error(other.m_error)
, m_error(move(other.m_error))
, m_next(move(other.m_next))
, m_path(move(other.m_path))
, m_flags(other.m_flags)
@ -48,7 +48,7 @@ bool DirIterator::advance_next()
errno = 0;
auto* de = readdir(m_dir);
if (!de) {
m_error = errno;
m_error = Error::from_errno(errno);
m_next.clear();
return false;
}

View file

@ -28,9 +28,8 @@ public:
DirIterator(DirIterator&&);
DirIterator(DirIterator const&) = delete;
bool has_error() const { return m_error != 0; }
int error() const { return m_error; }
char const* error_string() const { return strerror(m_error); }
bool has_error() const { return m_error.has_value(); }
Error error() const { return Error::copy(m_error.value()); }
bool has_next();
Optional<DirectoryEntry> next();
DeprecatedString next_path();
@ -39,7 +38,7 @@ public:
private:
DIR* m_dir = nullptr;
int m_error = 0;
Optional<Error> m_error;
Optional<DirectoryEntry> m_next;
DeprecatedString m_path;
int m_flags;

View file

@ -99,8 +99,9 @@ void FileSystemModel::Node::traverse_if_needed()
auto full_path = this->full_path();
Core::DirIterator di(full_path, m_model.should_show_dotfiles() ? Core::DirIterator::SkipParentAndBaseDir : Core::DirIterator::SkipDots);
if (di.has_error()) {
m_error = di.error();
warnln("DirIterator: {}", di.error_string());
auto error = di.error();
m_error = error.code();
warnln("DirIterator: {}", error);
return;
}

View file

@ -130,7 +130,7 @@ void FontDatabase::load_all_fonts_from_path(DeprecatedString const& root)
auto current_directory = path_queue.dequeue();
Core::DirIterator dir_iterator(current_directory, Core::DirIterator::SkipParentAndBaseDir);
if (dir_iterator.has_error()) {
dbgln("FontDatabase::load_all_fonts_from_path: {}", dir_iterator.error_string());
dbgln("FontDatabase::load_all_fonts_from_path: {}", dir_iterator.error());
continue;
}
while (dir_iterator.has_next()) {

View file

@ -158,8 +158,9 @@ static ErrorOr<void> populate_devtmpfs_char_devices_based_on_sysfs()
{
Core::DirIterator di("/sys/dev/char/", Core::DirIterator::SkipParentAndBaseDir);
if (di.has_error()) {
warnln("Failed to open /sys/dev/char - {}", di.error());
return Error::from_errno(di.error());
auto error = di.error();
warnln("Failed to open /sys/dev/char - {}", error);
return error;
}
while (di.has_next()) {
auto entry_name = di.next_path().split(':');

View file

@ -142,8 +142,9 @@ ErrorOr<u64> print_space_usage(DeprecatedString const& path, DuOption const& du_
if (is_directory) {
auto di = Core::DirIterator(path, Core::DirIterator::SkipParentAndBaseDir);
if (di.has_error()) {
outln("du: cannot read directory '{}': {}", path, di.error_string());
return Error::from_string_literal("An error occurred. See previous error.");
auto error = di.error();
outln("du: cannot read directory '{}': {}", path, error);
return error;
}
while (di.has_next()) {

View file

@ -170,7 +170,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
if (di.has_error()) {
status = 1;
fprintf(stderr, "%s: %s\n", path.characters(), di.error_string());
fprintf(stderr, "%s: %s\n", path.characters(), strerror(di.error().code()));
}
while (di.has_next()) {
@ -396,7 +396,8 @@ static int do_file_system_object_long(char const* path)
Core::DirIterator di(path, flags);
if (di.has_error()) {
if (di.error() == ENOTDIR) {
auto error = di.error();
if (error.code() == ENOTDIR) {
struct stat stat {
};
int rc = lstat(path, &stat);
@ -406,7 +407,7 @@ static int do_file_system_object_long(char const* path)
return 0;
return 2;
}
fprintf(stderr, "%s: %s\n", path, di.error_string());
fprintf(stderr, "%s: %s\n", path, strerror(di.error().code()));
return 1;
}
@ -510,7 +511,8 @@ int do_file_system_object_short(char const* path)
Core::DirIterator di(path, flags);
if (di.has_error()) {
if (di.error() == ENOTDIR) {
auto error = di.error();
if (error.code() == ENOTDIR) {
size_t nprinted = 0;
bool status = print_filesystem_object_short(path, path, &nprinted);
printf("\n");
@ -518,7 +520,7 @@ int do_file_system_object_short(char const* path)
return 0;
return 2;
}
fprintf(stderr, "%s: %s\n", path, di.error_string());
fprintf(stderr, "%s: %s\n", path, strerror(di.error().code()));
return 1;
}

View file

@ -27,8 +27,9 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
Core::DirIterator di("/sys/devices/storage/", Core::DirIterator::SkipParentAndBaseDir);
if (di.has_error()) {
warnln("Failed to open /sys/devices/storage - {}", di.error());
return 1;
auto error = di.error();
warnln("Failed to open /sys/devices/storage - {}", error);
return error;
}
outln(format_row, "LUN"sv, "Command set"sv, "Block Size"sv, "Last LBA"sv);

View file

@ -70,8 +70,9 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
Core::DirIterator di("/sys/bus/pci/", Core::DirIterator::SkipParentAndBaseDir);
if (di.has_error()) {
warnln("Failed to open /sys/bus/pci - {}", di.error());
return 1;
auto error = di.error();
warnln("Failed to open /sys/bus/pci - {}", error);
return error;
}
TRY(Core::System::pledge("stdio rpath"));

View file

@ -129,8 +129,8 @@ static ErrorOr<void> mount_all()
auto fstab_directory_iterator = Core::DirIterator("/etc/fstab.d", Core::DirIterator::SkipDots);
if (fstab_directory_iterator.has_error() && fstab_directory_iterator.error() != ENOENT) {
dbgln("Failed to open /etc/fstab.d: {}", fstab_directory_iterator.error_string());
if (fstab_directory_iterator.has_error() && fstab_directory_iterator.error().code() != ENOENT) {
dbgln("Failed to open /etc/fstab.d: {}", fstab_directory_iterator.error());
} else if (!fstab_directory_iterator.has_error()) {
while (fstab_directory_iterator.has_next()) {
auto path = fstab_directory_iterator.next_full_path();

View file

@ -82,7 +82,7 @@ static int handle_show_all()
{
Core::DirIterator di("/sys/kernel/variables", Core::DirIterator::SkipDots);
if (di.has_error()) {
outln("DirIterator: {}", di.error_string());
outln("DirIterator: {}", di.error());
return 1;
}

View file

@ -48,7 +48,7 @@ static void print_directory_tree(DeprecatedString const& root_path, int depth, D
Core::DirIterator di(root_path, flag_show_hidden_files ? Core::DirIterator::SkipParentAndBaseDir : Core::DirIterator::SkipDots);
if (di.has_error()) {
warnln("{}: {}", root_path, di.error_string());
warnln("{}: {}", root_path, di.error());
return;
}
@ -56,7 +56,7 @@ static void print_directory_tree(DeprecatedString const& root_path, int depth, D
while (di.has_next()) {
DeprecatedString name = di.next_path();
if (di.has_error()) {
warnln("{}: {}", root_path, di.error_string());
warnln("{}: {}", root_path, di.error());
continue;
}
names.append(name);