1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 02:07:35 +00:00

LibCore: Replace Result<T, E> use with ErrorOr<T> in Core::FileWatcher

This commit is contained in:
Andreas Kling 2021-11-07 11:45:20 +01:00
parent 4eeab4cfc8
commit 0b0c4e82b9
2 changed files with 11 additions and 16 deletions

View file

@ -9,7 +9,6 @@
#include <AK/Debug.h> #include <AK/Debug.h>
#include <AK/LexicalPath.h> #include <AK/LexicalPath.h>
#include <AK/NonnullRefPtr.h> #include <AK/NonnullRefPtr.h>
#include <AK/Result.h>
#include <AK/String.h> #include <AK/String.h>
#include <Kernel/API/InodeWatcherEvent.h> #include <Kernel/API/InodeWatcherEvent.h>
#include <Kernel/API/InodeWatcherFlags.h> #include <Kernel/API/InodeWatcherFlags.h>
@ -87,7 +86,7 @@ static String canonicalize_path(String path)
return LexicalPath::join(cwd, move(path)).string(); return LexicalPath::join(cwd, move(path)).string();
} }
Result<bool, String> FileWatcherBase::add_watch(String path, FileWatcherEvent::Type event_mask) ErrorOr<bool> FileWatcherBase::add_watch(String path, FileWatcherEvent::Type event_mask)
{ {
String canonical_path = canonicalize_path(move(path)); String canonical_path = canonicalize_path(move(path));
@ -110,7 +109,7 @@ Result<bool, String> FileWatcherBase::add_watch(String path, FileWatcherEvent::T
int wd = inode_watcher_add_watch(m_watcher_fd, canonical_path.characters(), canonical_path.length(), static_cast<unsigned>(kernel_mask)); int wd = inode_watcher_add_watch(m_watcher_fd, canonical_path.characters(), canonical_path.length(), static_cast<unsigned>(kernel_mask));
if (wd < 0) 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_path_to_wd.set(canonical_path, wd);
m_wd_to_path.set(wd, canonical_path); m_wd_to_path.set(wd, canonical_path);
@ -119,7 +118,7 @@ Result<bool, String> FileWatcherBase::add_watch(String path, FileWatcherEvent::T
return true; return true;
} }
Result<bool, String> FileWatcherBase::remove_watch(String path) ErrorOr<bool> FileWatcherBase::remove_watch(String path)
{ {
String canonical_path = canonicalize_path(move(path)); String canonical_path = canonicalize_path(move(path));
@ -129,10 +128,8 @@ Result<bool, String> FileWatcherBase::remove_watch(String path)
return false; return false;
} }
int rc = inode_watcher_remove_watch(m_watcher_fd, it->value); if (inode_watcher_remove_watch(m_watcher_fd, it->value) < 0)
if (rc < 0) { return Error::from_errno(errno);
return String::formatted("Could not stop watching file '{}' : {}", path, strerror(errno));
}
m_path_to_wd.remove(it); m_path_to_wd.remove(it);
m_wd_to_path.remove(it->value); m_wd_to_path.remove(it->value);
@ -172,12 +169,11 @@ Optional<FileWatcherEvent> BlockingFileWatcher::wait_for_event()
return event; return event;
} }
Result<NonnullRefPtr<FileWatcher>, String> FileWatcher::create(InodeWatcherFlags flags) ErrorOr<NonnullRefPtr<FileWatcher>> FileWatcher::create(InodeWatcherFlags flags)
{ {
auto watcher_fd = create_inode_watcher(static_cast<unsigned>(flags | InodeWatcherFlags::CloseOnExec)); auto watcher_fd = create_inode_watcher(static_cast<unsigned>(flags | InodeWatcherFlags::CloseOnExec));
if (watcher_fd < 0) { if (watcher_fd < 0)
return String::formatted("FileWatcher: Could not create InodeWatcher: {}", strerror(errno)); return Error::from_errno(errno);
}
auto notifier = Notifier::construct(watcher_fd, Notifier::Event::Read); auto notifier = Notifier::construct(watcher_fd, Notifier::Event::Read);
return adopt_ref(*new FileWatcher(watcher_fd, move(notifier))); return adopt_ref(*new FileWatcher(watcher_fd, move(notifier)));

View file

@ -12,7 +12,6 @@
#include <AK/Noncopyable.h> #include <AK/Noncopyable.h>
#include <AK/NonnullRefPtr.h> #include <AK/NonnullRefPtr.h>
#include <AK/RefCounted.h> #include <AK/RefCounted.h>
#include <AK/Result.h>
#include <AK/String.h> #include <AK/String.h>
#include <Kernel/API/InodeWatcherEvent.h> #include <Kernel/API/InodeWatcherEvent.h>
#include <Kernel/API/InodeWatcherFlags.h> #include <Kernel/API/InodeWatcherFlags.h>
@ -39,8 +38,8 @@ class FileWatcherBase {
public: public:
virtual ~FileWatcherBase() { } virtual ~FileWatcherBase() { }
Result<bool, String> add_watch(String path, FileWatcherEvent::Type event_mask); ErrorOr<bool> add_watch(String path, FileWatcherEvent::Type event_mask);
Result<bool, String> remove_watch(String path); ErrorOr<bool> remove_watch(String path);
bool is_watching(String const& path) const { return m_path_to_wd.find(path) != m_path_to_wd.end(); } bool is_watching(String const& path) const { return m_path_to_wd.find(path) != m_path_to_wd.end(); }
protected: protected:
@ -69,7 +68,7 @@ class FileWatcher final : public FileWatcherBase
AK_MAKE_NONCOPYABLE(FileWatcher); AK_MAKE_NONCOPYABLE(FileWatcher);
public: public:
static Result<NonnullRefPtr<FileWatcher>, String> create(InodeWatcherFlags = InodeWatcherFlags::None); static ErrorOr<NonnullRefPtr<FileWatcher>> create(InodeWatcherFlags = InodeWatcherFlags::None);
~FileWatcher(); ~FileWatcher();
Function<void(FileWatcherEvent const&)> on_change; Function<void(FileWatcherEvent const&)> on_change;