diff options
author | Sam Atkins <atkinssj@serenityos.org> | 2023-03-01 15:55:15 +0000 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2023-03-05 20:23:42 +0100 |
commit | 774f328783db31a02eba76d7c55d53a0b41b1508 (patch) | |
tree | 4fc9c393af9d93f2925d38234c9c520bb2e05d75 | |
parent | a98ae8f35704737e37920b7ddb516ea703a3dca7 (diff) | |
download | serenity-774f328783db31a02eba76d7c55d53a0b41b1508.zip |
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.
-rw-r--r-- | Meta/Lagom/Tools/CodeGenerators/LibUnicode/GeneratorUtil.h | 16 | ||||
-rw-r--r-- | Userland/Applications/KeyboardSettings/KeyboardSettingsWidget.cpp | 2 | ||||
-rw-r--r-- | Userland/Applications/SpaceAnalyzer/Tree.cpp | 6 | ||||
-rw-r--r-- | Userland/DevTools/HackStudio/Dialogs/ProjectTemplatesModel.cpp | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/DeprecatedFile.cpp | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/DirIterator.cpp | 6 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/DirIterator.h | 7 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/FileSystemModel.cpp | 5 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/Font/FontDatabase.cpp | 2 | ||||
-rw-r--r-- | Userland/Services/SystemServer/main.cpp | 5 | ||||
-rw-r--r-- | Userland/Utilities/du.cpp | 5 | ||||
-rw-r--r-- | Userland/Utilities/ls.cpp | 12 | ||||
-rw-r--r-- | Userland/Utilities/lsblk.cpp | 5 | ||||
-rw-r--r-- | Userland/Utilities/lspci.cpp | 5 | ||||
-rw-r--r-- | Userland/Utilities/mount.cpp | 4 | ||||
-rw-r--r-- | Userland/Utilities/sysctl.cpp | 2 | ||||
-rw-r--r-- | Userland/Utilities/tree.cpp | 4 |
17 files changed, 44 insertions, 46 deletions
diff --git a/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GeneratorUtil.h b/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GeneratorUtil.h index 1918ce2478..d10ca258d3 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GeneratorUtil.h +++ b/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GeneratorUtil.h @@ -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; } diff --git a/Userland/Applications/KeyboardSettings/KeyboardSettingsWidget.cpp b/Userland/Applications/KeyboardSettings/KeyboardSettingsWidget.cpp index 83624bef9f..f77cfa546d 100644 --- a/Userland/Applications/KeyboardSettings/KeyboardSettingsWidget.cpp +++ b/Userland/Applications/KeyboardSettings/KeyboardSettingsWidget.cpp @@ -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); } diff --git a/Userland/Applications/SpaceAnalyzer/Tree.cpp b/Userland/Applications/SpaceAnalyzer/Tree.cpp index eb6258a0c0..d77b74c810 100644 --- a/Userland/Applications/SpaceAnalyzer/Tree.cpp +++ b/Userland/Applications/SpaceAnalyzer/Tree.cpp @@ -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()) { diff --git a/Userland/DevTools/HackStudio/Dialogs/ProjectTemplatesModel.cpp b/Userland/DevTools/HackStudio/Dialogs/ProjectTemplatesModel.cpp index 71c8c9db78..0f8ed977c9 100644 --- a/Userland/DevTools/HackStudio/Dialogs/ProjectTemplatesModel.cpp +++ b/Userland/DevTools/HackStudio/Dialogs/ProjectTemplatesModel.cpp @@ -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; } diff --git a/Userland/Libraries/LibCore/DeprecatedFile.cpp b/Userland/Libraries/LibCore/DeprecatedFile.cpp index 7bf17fa971..a2cb1039ad 100644 --- a/Userland/Libraries/LibCore/DeprecatedFile.cpp +++ b/Userland/Libraries/LibCore/DeprecatedFile.cpp @@ -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)); diff --git a/Userland/Libraries/LibCore/DirIterator.cpp b/Userland/Libraries/LibCore/DirIterator.cpp index 3b58467397..c9a09d9e80 100644 --- a/Userland/Libraries/LibCore/DirIterator.cpp +++ b/Userland/Libraries/LibCore/DirIterator.cpp @@ -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; } diff --git a/Userland/Libraries/LibCore/DirIterator.h b/Userland/Libraries/LibCore/DirIterator.h index b1eb618505..fcdbfbe774 100644 --- a/Userland/Libraries/LibCore/DirIterator.h +++ b/Userland/Libraries/LibCore/DirIterator.h @@ -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; diff --git a/Userland/Libraries/LibGUI/FileSystemModel.cpp b/Userland/Libraries/LibGUI/FileSystemModel.cpp index 287e12a581..d1d2e18e64 100644 --- a/Userland/Libraries/LibGUI/FileSystemModel.cpp +++ b/Userland/Libraries/LibGUI/FileSystemModel.cpp @@ -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; } diff --git a/Userland/Libraries/LibGfx/Font/FontDatabase.cpp b/Userland/Libraries/LibGfx/Font/FontDatabase.cpp index 4088f97596..900116f4c0 100644 --- a/Userland/Libraries/LibGfx/Font/FontDatabase.cpp +++ b/Userland/Libraries/LibGfx/Font/FontDatabase.cpp @@ -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()) { diff --git a/Userland/Services/SystemServer/main.cpp b/Userland/Services/SystemServer/main.cpp index 748c199ba9..8c185bc7b0 100644 --- a/Userland/Services/SystemServer/main.cpp +++ b/Userland/Services/SystemServer/main.cpp @@ -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(':'); diff --git a/Userland/Utilities/du.cpp b/Userland/Utilities/du.cpp index 2f3fc14066..bdcdd0db00 100644 --- a/Userland/Utilities/du.cpp +++ b/Userland/Utilities/du.cpp @@ -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()) { diff --git a/Userland/Utilities/ls.cpp b/Userland/Utilities/ls.cpp index b41aec19d1..df4759f3f8 100644 --- a/Userland/Utilities/ls.cpp +++ b/Userland/Utilities/ls.cpp @@ -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; } diff --git a/Userland/Utilities/lsblk.cpp b/Userland/Utilities/lsblk.cpp index eaaa1b34b7..bc2518a37f 100644 --- a/Userland/Utilities/lsblk.cpp +++ b/Userland/Utilities/lsblk.cpp @@ -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); diff --git a/Userland/Utilities/lspci.cpp b/Userland/Utilities/lspci.cpp index e8768b6faa..b26a03f4a9 100644 --- a/Userland/Utilities/lspci.cpp +++ b/Userland/Utilities/lspci.cpp @@ -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")); diff --git a/Userland/Utilities/mount.cpp b/Userland/Utilities/mount.cpp index adbaa9ef65..e579a795a5 100644 --- a/Userland/Utilities/mount.cpp +++ b/Userland/Utilities/mount.cpp @@ -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(); diff --git a/Userland/Utilities/sysctl.cpp b/Userland/Utilities/sysctl.cpp index 8d9aaafe5b..10b4e9ec95 100644 --- a/Userland/Utilities/sysctl.cpp +++ b/Userland/Utilities/sysctl.cpp @@ -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; } diff --git a/Userland/Utilities/tree.cpp b/Userland/Utilities/tree.cpp index 27755dfcdf..5038dd3432 100644 --- a/Userland/Utilities/tree.cpp +++ b/Userland/Utilities/tree.cpp @@ -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); |