From 0b0c4e82b9dc6097e86e20871a4ae4e63cbb18a1 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 7 Nov 2021 11:45:20 +0100 Subject: [PATCH] LibCore: Replace Result use with ErrorOr in Core::FileWatcher --- Userland/Libraries/LibCore/FileWatcher.cpp | 20 ++++++++------------ Userland/Libraries/LibCore/FileWatcher.h | 7 +++---- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/Userland/Libraries/LibCore/FileWatcher.cpp b/Userland/Libraries/LibCore/FileWatcher.cpp index bab6b1e96f..1c0cc3f131 100644 --- a/Userland/Libraries/LibCore/FileWatcher.cpp +++ b/Userland/Libraries/LibCore/FileWatcher.cpp @@ -9,7 +9,6 @@ #include #include #include -#include #include #include #include @@ -87,7 +86,7 @@ static String canonicalize_path(String path) return LexicalPath::join(cwd, move(path)).string(); } -Result FileWatcherBase::add_watch(String path, FileWatcherEvent::Type event_mask) +ErrorOr FileWatcherBase::add_watch(String path, FileWatcherEvent::Type event_mask) { String canonical_path = canonicalize_path(move(path)); @@ -110,7 +109,7 @@ Result FileWatcherBase::add_watch(String path, FileWatcherEvent::T int wd = inode_watcher_add_watch(m_watcher_fd, canonical_path.characters(), canonical_path.length(), static_cast(kernel_mask)); if (wd < 0) - return String::formatted("Could not watch file '{}' : {}", canonical_path, strerror(errno)); + return Error::from_errno(errno); m_path_to_wd.set(canonical_path, wd); m_wd_to_path.set(wd, canonical_path); @@ -119,7 +118,7 @@ Result FileWatcherBase::add_watch(String path, FileWatcherEvent::T return true; } -Result FileWatcherBase::remove_watch(String path) +ErrorOr FileWatcherBase::remove_watch(String path) { String canonical_path = canonicalize_path(move(path)); @@ -129,10 +128,8 @@ Result FileWatcherBase::remove_watch(String path) return false; } - int rc = inode_watcher_remove_watch(m_watcher_fd, it->value); - if (rc < 0) { - return String::formatted("Could not stop watching file '{}' : {}", path, strerror(errno)); - } + if (inode_watcher_remove_watch(m_watcher_fd, it->value) < 0) + return Error::from_errno(errno); m_path_to_wd.remove(it); m_wd_to_path.remove(it->value); @@ -172,12 +169,11 @@ Optional BlockingFileWatcher::wait_for_event() return event; } -Result, String> FileWatcher::create(InodeWatcherFlags flags) +ErrorOr> FileWatcher::create(InodeWatcherFlags flags) { auto watcher_fd = create_inode_watcher(static_cast(flags | InodeWatcherFlags::CloseOnExec)); - if (watcher_fd < 0) { - return String::formatted("FileWatcher: Could not create InodeWatcher: {}", strerror(errno)); - } + if (watcher_fd < 0) + return Error::from_errno(errno); auto notifier = Notifier::construct(watcher_fd, Notifier::Event::Read); return adopt_ref(*new FileWatcher(watcher_fd, move(notifier))); diff --git a/Userland/Libraries/LibCore/FileWatcher.h b/Userland/Libraries/LibCore/FileWatcher.h index c2a341afd7..4d94f7f524 100644 --- a/Userland/Libraries/LibCore/FileWatcher.h +++ b/Userland/Libraries/LibCore/FileWatcher.h @@ -12,7 +12,6 @@ #include #include #include -#include #include #include #include @@ -39,8 +38,8 @@ class FileWatcherBase { public: virtual ~FileWatcherBase() { } - Result add_watch(String path, FileWatcherEvent::Type event_mask); - Result remove_watch(String path); + ErrorOr add_watch(String path, FileWatcherEvent::Type event_mask); + ErrorOr remove_watch(String path); bool is_watching(String const& path) const { return m_path_to_wd.find(path) != m_path_to_wd.end(); } protected: @@ -69,7 +68,7 @@ class FileWatcher final : public FileWatcherBase AK_MAKE_NONCOPYABLE(FileWatcher); public: - static Result, String> create(InodeWatcherFlags = InodeWatcherFlags::None); + static ErrorOr> create(InodeWatcherFlags = InodeWatcherFlags::None); ~FileWatcher(); Function on_change;