file_manager/directory: stop watch-thread correctly

This commit is contained in:
Adam
2023-10-21 19:22:35 +01:00
parent 3761a440e8
commit 37cdefbfa1
2 changed files with 7 additions and 4 deletions

View File

@@ -9,10 +9,9 @@
#include <thread>
#include <sys/inotify.h>
void Watch(std::filesystem::path path, CefRefPtr<FileManager::FileManager> file_manager, int fd) {
void Watch(std::filesystem::path path, CefRefPtr<FileManager::FileManager> 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<FileManager::FileManager> 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<FileManager::FileManager> 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
}

View File

@@ -14,6 +14,7 @@ namespace FileManager {
#if defined(__linux__)
std::thread inotify_thread;
int inotify_fd;
int inotify_wd;
#endif
public: