diff options
author | Timothy Flynn <trflynn89@pm.me> | 2023-01-18 08:54:39 -0500 |
---|---|---|
committer | Tim Flynn <trflynn89@pm.me> | 2023-01-18 09:37:11 -0500 |
commit | 142abc0b2e8f905e4bf6ede14d92934286b07cbe (patch) | |
tree | 8db0bacc76e37deab51d79cb1b7217e0c1861f3d /Userland | |
parent | 98e6dbf50ad8061aec7836fcea839ac484580192 (diff) | |
download | serenity-142abc0b2e8f905e4bf6ede14d92934286b07cbe.zip |
LibCore+ConfigServer: Add FileWatcherFlags to replace InodeWatcherFlags
InodeWatcherFlags is an enumeration from the Kernel. To avoid using it
outside of Serenity, add a FileWatcherFlags for FileWatcher, much like
we already have FileWatcherEvent::Type.
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibCore/FileWatcher.h | 14 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/FileWatcherLinux.cpp | 11 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/FileWatcherSerenity.cpp | 20 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/FileWatcherUnimplemented.cpp | 3 | ||||
-rw-r--r-- | Userland/Services/ConfigServer/ConnectionFromClient.cpp | 2 |
5 files changed, 33 insertions, 17 deletions
diff --git a/Userland/Libraries/LibCore/FileWatcher.h b/Userland/Libraries/LibCore/FileWatcher.h index 1fef985aba..501213aedb 100644 --- a/Userland/Libraries/LibCore/FileWatcher.h +++ b/Userland/Libraries/LibCore/FileWatcher.h @@ -13,8 +13,6 @@ #include <AK/Noncopyable.h> #include <AK/NonnullRefPtr.h> #include <AK/RefCounted.h> -#include <Kernel/API/InodeWatcherEvent.h> -#include <Kernel/API/InodeWatcherFlags.h> #include <LibCore/Notifier.h> namespace Core { @@ -34,6 +32,14 @@ struct FileWatcherEvent { AK_ENUM_BITWISE_OPERATORS(FileWatcherEvent::Type); +enum class FileWatcherFlags : u32 { + None = 0, + Nonblock = 1 << 0, + CloseOnExec = 1 << 1, +}; + +AK_ENUM_BITWISE_OPERATORS(FileWatcherFlags); + class FileWatcherBase { public: virtual ~FileWatcherBase() = default; @@ -57,7 +63,7 @@ class BlockingFileWatcher final : public FileWatcherBase { AK_MAKE_NONCOPYABLE(BlockingFileWatcher); public: - explicit BlockingFileWatcher(InodeWatcherFlags = InodeWatcherFlags::None); + explicit BlockingFileWatcher(FileWatcherFlags = FileWatcherFlags::None); ~BlockingFileWatcher(); Optional<FileWatcherEvent> wait_for_event(); @@ -68,7 +74,7 @@ class FileWatcher final : public FileWatcherBase AK_MAKE_NONCOPYABLE(FileWatcher); public: - static ErrorOr<NonnullRefPtr<FileWatcher>> create(InodeWatcherFlags = InodeWatcherFlags::None); + static ErrorOr<NonnullRefPtr<FileWatcher>> create(FileWatcherFlags = FileWatcherFlags::None); ~FileWatcher(); Function<void(FileWatcherEvent const&)> on_change; diff --git a/Userland/Libraries/LibCore/FileWatcherLinux.cpp b/Userland/Libraries/LibCore/FileWatcherLinux.cpp index 56e41faff1..963e809e73 100644 --- a/Userland/Libraries/LibCore/FileWatcherLinux.cpp +++ b/Userland/Libraries/LibCore/FileWatcherLinux.cpp @@ -8,7 +8,6 @@ #include <AK/Debug.h> #include <AK/DeprecatedString.h> #include <AK/LexicalPath.h> -#include <Kernel/API/InodeWatcherFlags.h> #include <LibCore/Notifier.h> #include <errno.h> #include <limits.h> @@ -23,13 +22,13 @@ static_assert(false, "This file must only be used for Linux"); namespace Core { -static constexpr unsigned inode_watcher_flags_to_inotify_flags(InodeWatcherFlags flags) +static constexpr unsigned file_watcher_flags_to_inotify_flags(FileWatcherFlags flags) { unsigned result = 0; - if ((flags & InodeWatcherFlags::Nonblock) != InodeWatcherFlags::None) + if (has_flag(flags, FileWatcherFlags::Nonblock)) result |= IN_NONBLOCK; - if ((flags & InodeWatcherFlags::CloseOnExec) != InodeWatcherFlags::None) + if (has_flag(flags, FileWatcherFlags::CloseOnExec)) result |= IN_CLOEXEC; return result; @@ -92,9 +91,9 @@ static Optional<FileWatcherEvent> get_event_from_fd(int fd, HashMap<unsigned, De return result; } -ErrorOr<NonnullRefPtr<FileWatcher>> FileWatcher::create(InodeWatcherFlags flags) +ErrorOr<NonnullRefPtr<FileWatcher>> FileWatcher::create(FileWatcherFlags flags) { - auto watcher_fd = ::inotify_init1(inode_watcher_flags_to_inotify_flags(flags | InodeWatcherFlags::CloseOnExec)); + auto watcher_fd = ::inotify_init1(file_watcher_flags_to_inotify_flags(flags | FileWatcherFlags::CloseOnExec)); if (watcher_fd < 0) return Error::from_errno(errno); diff --git a/Userland/Libraries/LibCore/FileWatcherSerenity.cpp b/Userland/Libraries/LibCore/FileWatcherSerenity.cpp index d58f972fc3..fbf3344729 100644 --- a/Userland/Libraries/LibCore/FileWatcherSerenity.cpp +++ b/Userland/Libraries/LibCore/FileWatcherSerenity.cpp @@ -24,6 +24,18 @@ static_assert(false, "This file must only be used for SerenityOS"); namespace Core { +static constexpr unsigned file_watcher_flags_to_inode_watcher_flags(FileWatcherFlags flags) +{ + auto result = InodeWatcherFlags::None; + + if (has_flag(flags, FileWatcherFlags::Nonblock)) + result |= InodeWatcherFlags::Nonblock; + if (has_flag(flags, FileWatcherFlags::CloseOnExec)) + result |= InodeWatcherFlags::CloseOnExec; + + return static_cast<unsigned>(result); +} + static Optional<FileWatcherEvent> get_event_from_fd(int fd, HashMap<unsigned, DeprecatedString> const& wd_to_path) { u8 buffer[MAXIMUM_EVENT_SIZE]; @@ -139,8 +151,8 @@ ErrorOr<bool> FileWatcherBase::remove_watch(DeprecatedString path) return true; } -BlockingFileWatcher::BlockingFileWatcher(InodeWatcherFlags flags) - : FileWatcherBase(create_inode_watcher(static_cast<unsigned>(flags))) +BlockingFileWatcher::BlockingFileWatcher(FileWatcherFlags flags) + : FileWatcherBase(create_inode_watcher(file_watcher_flags_to_inode_watcher_flags(flags))) { VERIFY(m_watcher_fd != -1); dbgln_if(FILE_WATCHER_DEBUG, "BlockingFileWatcher created with InodeWatcher {}", m_watcher_fd); @@ -170,9 +182,9 @@ Optional<FileWatcherEvent> BlockingFileWatcher::wait_for_event() return event; } -ErrorOr<NonnullRefPtr<FileWatcher>> FileWatcher::create(InodeWatcherFlags flags) +ErrorOr<NonnullRefPtr<FileWatcher>> FileWatcher::create(FileWatcherFlags flags) { - auto watcher_fd = create_inode_watcher(static_cast<unsigned>(flags | InodeWatcherFlags::CloseOnExec)); + auto watcher_fd = create_inode_watcher(file_watcher_flags_to_inode_watcher_flags(flags | FileWatcherFlags::CloseOnExec)); if (watcher_fd < 0) return Error::from_errno(errno); diff --git a/Userland/Libraries/LibCore/FileWatcherUnimplemented.cpp b/Userland/Libraries/LibCore/FileWatcherUnimplemented.cpp index 7ee143c304..2e12ac688f 100644 --- a/Userland/Libraries/LibCore/FileWatcherUnimplemented.cpp +++ b/Userland/Libraries/LibCore/FileWatcherUnimplemented.cpp @@ -6,13 +6,12 @@ */ #include "FileWatcher.h" -#include <Kernel/API/InodeWatcherFlags.h> #include <LibCore/Notifier.h> #include <errno.h> namespace Core { -ErrorOr<NonnullRefPtr<FileWatcher>> FileWatcher::create(InodeWatcherFlags) +ErrorOr<NonnullRefPtr<FileWatcher>> FileWatcher::create(FileWatcherFlags) { return Error::from_errno(ENOTSUP); } diff --git a/Userland/Services/ConfigServer/ConnectionFromClient.cpp b/Userland/Services/ConfigServer/ConnectionFromClient.cpp index 3daf7faa15..c288af7f1e 100644 --- a/Userland/Services/ConfigServer/ConnectionFromClient.cpp +++ b/Userland/Services/ConfigServer/ConnectionFromClient.cpp @@ -39,7 +39,7 @@ static Core::ConfigFile& ensure_domain_config(DeprecatedString const& domain) auto config = Core::ConfigFile::open_for_app(domain, Core::ConfigFile::AllowWriting::Yes).release_value_but_fixme_should_propagate_errors(); // FIXME: Use a single FileWatcher with multiple watches inside. - auto watcher_or_error = Core::FileWatcher::create(InodeWatcherFlags::Nonblock); + auto watcher_or_error = Core::FileWatcher::create(Core::FileWatcherFlags::Nonblock); VERIFY(!watcher_or_error.is_error()); auto result = watcher_or_error.value()->add_watch(config->filename(), Core::FileWatcherEvent::Type::ContentModified); VERIFY(!result.is_error()); |