Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
actboy168 committed Apr 5, 2024
1 parent 9a63a86 commit af2add8
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 49 deletions.
10 changes: 3 additions & 7 deletions bee/filewatch/filewatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<notify> select() noexcept;
#if defined(__APPLE__)
void event_update(const char* paths[], const FSEventStreamEventFlags flags[], size_t n) noexcept;
#endif

private:
#if defined(_WIN32)
Expand All @@ -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
Expand Down
46 changes: 22 additions & 24 deletions bee/filewatch/filewatch_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -144,6 +120,28 @@ namespace bee::filewatch {
}

std::optional<notify> 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;
}
Expand Down
3 changes: 0 additions & 3 deletions bee/filewatch/filewatch_osx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::mutex> lock(m_mutex);
for (size_t i = 0; i < n; ++i) {
Expand Down
5 changes: 1 addition & 4 deletions bee/filewatch/filewatch_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ namespace bee::filewatch {
return task.start(m_recursive);
}

void watch::update() noexcept {
std::optional<notify> watch::select() noexcept {
for (auto iter = m_tasks.begin(); iter != m_tasks.end();) {
if (event_update(*iter)) {
++iter;
Expand All @@ -222,9 +222,6 @@ namespace bee::filewatch {
iter = m_tasks.erase(iter);
}
}
}

std::optional<notify> watch::select() noexcept {
if (m_notify.empty()) {
return std::nullopt;
}
Expand Down
21 changes: 10 additions & 11 deletions binding/lua_filewatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down

0 comments on commit af2add8

Please sign in to comment.