diff --git a/bee/filewatch/filewatch.h b/bee/filewatch/filewatch.h index 9f0d9ab5..5db929e5 100644 --- a/bee/filewatch/filewatch.h +++ b/bee/filewatch/filewatch.h @@ -48,14 +48,15 @@ namespace bee::filewatch { watch() noexcept; ~watch() noexcept; - void stop() noexcept; void add(const string_type& path) noexcept; void set_recursive(bool enable) noexcept; bool set_follow_symlinks(bool enable) noexcept; bool set_filter(filter f = DefaultFilter) noexcept; - void update() noexcept; std::optional select() noexcept; +#if defined(__APPLE__) + void event_update(const char* paths[], const FSEventStreamEventFlags flags[], size_t n) noexcept; +#endif private: #if defined(_WIN32) @@ -64,11 +65,6 @@ namespace bee::filewatch { bool create_stream(CFArrayRef cf_paths) noexcept; void destroy_stream() noexcept; void update_stream() noexcept; - - public: - void event_update(const char* paths[], const FSEventStreamEventFlags flags[], size_t n) noexcept; - - private: #elif defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) void event_update(void* event) noexcept; #endif diff --git a/bee/filewatch/filewatch_linux.cpp b/bee/filewatch/filewatch_linux.cpp index 66aa3b8c..2f4ab5e3 100644 --- a/bee/filewatch/filewatch_linux.cpp +++ b/bee/filewatch/filewatch_linux.cpp @@ -89,30 +89,6 @@ namespace bee::filewatch { return true; } - void watch::update() noexcept { - if (m_inotify_fd == -1) { - return; - } - - struct pollfd pfd_read; - pfd_read.fd = m_inotify_fd; - pfd_read.events = POLLIN; - if (poll(&pfd_read, 1, 0) != 1) { - return; - } - - std::byte buf[4096]; - ssize_t n = read(m_inotify_fd, buf, sizeof buf); - if (n == 0 || n == -1) { - return; - } - for (std::byte* p = buf; p < buf + n;) { - auto event = (struct inotify_event*)p; - event_update(event); - p += sizeof(*event) + event->len; - } - } - void watch::event_update(void* e) noexcept { inotify_event* event = (inotify_event*)e; if (event->mask & IN_Q_OVERFLOW) { @@ -144,6 +120,28 @@ namespace bee::filewatch { } std::optional watch::select() noexcept { + if (m_inotify_fd == -1) { + return std::nullopt; + } + + struct pollfd pfd_read; + pfd_read.fd = m_inotify_fd; + pfd_read.events = POLLIN; + if (poll(&pfd_read, 1, 0) != 1) { + return std::nullopt; + } + + std::byte buf[4096]; + ssize_t n = read(m_inotify_fd, buf, sizeof buf); + if (n == 0 || n == -1) { + return std::nullopt; + } + for (std::byte* p = buf; p < buf + n;) { + auto event = (struct inotify_event*)p; + event_update(event); + p += sizeof(*event) + event->len; + } + if (m_notify.empty()) { return std::nullopt; } diff --git a/bee/filewatch/filewatch_osx.cpp b/bee/filewatch/filewatch_osx.cpp index 47470877..044de249 100644 --- a/bee/filewatch/filewatch_osx.cpp +++ b/bee/filewatch/filewatch_osx.cpp @@ -108,9 +108,6 @@ namespace bee::filewatch { CFRelease(cf_paths); } - void watch::update() noexcept { - } - void watch::event_update(const char* paths[], const FSEventStreamEventFlags flags[], size_t n) noexcept { std::unique_lock lock(m_mutex); for (size_t i = 0; i < n; ++i) { diff --git a/bee/filewatch/filewatch_win.cpp b/bee/filewatch/filewatch_win.cpp index 2949fb4c..a5a26565 100644 --- a/bee/filewatch/filewatch_win.cpp +++ b/bee/filewatch/filewatch_win.cpp @@ -213,7 +213,7 @@ namespace bee::filewatch { return task.start(m_recursive); } - void watch::update() noexcept { + std::optional watch::select() noexcept { for (auto iter = m_tasks.begin(); iter != m_tasks.end();) { if (event_update(*iter)) { ++iter; @@ -222,9 +222,6 @@ namespace bee::filewatch { iter = m_tasks.erase(iter); } } - } - - std::optional watch::select() noexcept { if (m_notify.empty()) { return std::nullopt; } diff --git a/binding/lua_filewatch.cpp b/binding/lua_filewatch.cpp index 74b198a5..e0dd6d63 100644 --- a/binding/lua_filewatch.cpp +++ b/binding/lua_filewatch.cpp @@ -18,8 +18,8 @@ namespace bee::lua_filewatch { } static int add(lua_State* L) { - filewatch::watch& self = to(L, 1); - auto path = lua::checkstring(L, 2); + auto& self = to(L, 1); + auto path = lua::checkstring(L, 2); std::error_code ec; fs::path abspath = fs::absolute(path, ec); if (ec) { @@ -32,23 +32,23 @@ namespace bee::lua_filewatch { } static int set_recursive(lua_State* L) { - filewatch::watch& self = to(L, 1); - bool enable = lua_toboolean(L, 2); + auto& self = to(L, 1); + bool enable = lua_toboolean(L, 2); self.set_recursive(enable); lua_pushboolean(L, 1); return 1; } static int set_follow_symlinks(lua_State* L) { - filewatch::watch& self = to(L, 1); - bool enable = lua_toboolean(L, 2); - bool ok = self.set_follow_symlinks(enable); + auto& self = to(L, 1); + bool enable = lua_toboolean(L, 2); + bool ok = self.set_follow_symlinks(enable); lua_pushboolean(L, ok); return 1; } static int set_filter(lua_State* L) { - filewatch::watch& self = to(L, 1); + auto& self = to(L, 1); if (lua_isnoneornil(L, 2)) { bool ok = self.set_filter(); lua_pushboolean(L, ok); @@ -76,8 +76,7 @@ namespace bee::lua_filewatch { } static int select(lua_State* L) { - filewatch::watch& self = to(L, 1); - self.update(); + auto& self = to(L, 1); auto notify = self.select(); if (!notify) { return 0; @@ -97,7 +96,7 @@ namespace bee::lua_filewatch { } static int mt_close(lua_State* L) { - filewatch::watch& self = to(L, 1); + auto& self = to(L, 1); self.stop(); return 0; }