From 37cdefbfa152e77045bb3a03e1980c2cce8a7524 Mon Sep 17 00:00:00 2001 From: Adam Date: Sat, 21 Oct 2023 19:22:35 +0100 Subject: [PATCH] file_manager/directory: stop watch-thread correctly --- src/file_manager/directory.cxx | 10 ++++++---- src/file_manager/directory.hxx | 1 + 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/file_manager/directory.cxx b/src/file_manager/directory.cxx index a2cc446..63e8c3f 100644 --- a/src/file_manager/directory.cxx +++ b/src/file_manager/directory.cxx @@ -9,10 +9,9 @@ #include #include -void Watch(std::filesystem::path path, CefRefPtr file_manager, int fd) { +void Watch(std::filesystem::path path, CefRefPtr file_manager, int fd, int wd) { const struct inotify_event *event; char buf[256] __attribute__((aligned(__alignof__(struct inotify_event)))); - int wd = inotify_add_watch(fd, path.c_str(), IN_CREATE | IN_DELETE | IN_MODIFY | IN_MOVE | IN_MOVE_SELF | IN_DELETE_SELF); bool run = true; while (run) { bool did_callback = false; @@ -27,6 +26,8 @@ void Watch(std::filesystem::path path, CefRefPtr file_ case IN_MOVE_SELF: case IN_DELETE_SELF: fmt::print("[B] file monitor stopping due to monitored directory being moved or deleted\n"); + // fall through + case IN_IGNORED: run = false; ptr = buf + len; // so we won't process any more events break; @@ -46,7 +47,8 @@ void Watch(std::filesystem::path path, CefRefPtr file_ FileManager::Directory::Directory(std::filesystem::path path): path(path) { #if defined(__linux__) this->inotify_fd = inotify_init1(IN_CLOEXEC); - this->inotify_thread = std::thread(Watch, path, this, this->inotify_fd); + this->inotify_wd = inotify_add_watch(this->inotify_fd, path.c_str(), IN_CREATE | IN_DELETE | IN_MODIFY | IN_MOVE | IN_MOVE_SELF | IN_DELETE_SELF | IN_IGNORED); + this->inotify_thread = std::thread(Watch, path, this, this->inotify_fd, this->inotify_wd); #endif } @@ -75,7 +77,7 @@ void FileManager::Directory::free(File file) const { void FileManager::Directory::StopFileManager() { #if defined(__linux__) - close(this->inotify_fd); + inotify_rm_watch(this->inotify_fd, this->inotify_wd); this->inotify_thread.join(); #endif } diff --git a/src/file_manager/directory.hxx b/src/file_manager/directory.hxx index b8ec4ae..c1fcbb7 100644 --- a/src/file_manager/directory.hxx +++ b/src/file_manager/directory.hxx @@ -14,6 +14,7 @@ namespace FileManager { #if defined(__linux__) std::thread inotify_thread; int inotify_fd; + int inotify_wd; #endif public: