1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 22:17:42 +00:00

LibGUI: Use Core::FileWatcher in FileSystemModel

This replaces the manual watch_file and Notifier handling with the new
Core::FileWatcher wrapper, which reduces the manual handling and makes
the code easier to reason about :^)
This commit is contained in:
DexesTTP 2021-02-08 20:42:34 +01:00 committed by Andreas Kling
parent 2acbb811b1
commit c98ad27803
2 changed files with 18 additions and 24 deletions

View file

@ -136,28 +136,23 @@ void FileSystemModel::Node::traverse_if_needed()
children.append(move(child));
}
if (m_watch_fd >= 0)
return;
if (!m_file_watcher) {
m_watch_fd = watch_file(full_path.characters(), full_path.length());
if (m_watch_fd < 0) {
perror("watch_file");
return;
}
fcntl(m_watch_fd, F_SETFD, FD_CLOEXEC);
dbgln("Watching {} for changes, m_watch_fd={}", full_path, m_watch_fd);
m_notifier = Core::Notifier::construct(m_watch_fd, Core::Notifier::Event::Read);
m_notifier->on_ready_to_read = [this] {
char buffer[32];
int rc = read(m_notifier->fd(), buffer, sizeof(buffer));
ASSERT(rc >= 0);
// We are not already watching this file, create a new watcher
auto watcher_or_error = Core::FileWatcher::watch(full_path);
// Note : the watcher may not be created (e.g. we do not have access rights.) This is expected, just don't watch if that's the case.
if (!watcher_or_error.is_error()) {
m_file_watcher = watcher_or_error.release_value();
m_file_watcher->on_change = [this](auto) {
has_traversed = false;
mode = 0;
children.clear();
reify_if_needed();
m_model.did_update();
};
}
}
}
void FileSystemModel::Node::reify_if_needed()

View file

@ -29,7 +29,7 @@
#include <AK/HashMap.h>
#include <AK/NonnullOwnPtrVector.h>
#include <LibCore/DateTime.h>
#include <LibCore/Notifier.h>
#include <LibCore/FileWatcher.h>
#include <LibGUI/Model.h>
#include <string.h>
#include <sys/stat.h>
@ -63,7 +63,7 @@ public:
};
struct Node {
~Node() { close(m_watch_fd); }
~Node() { }
String name;
String symlink_target;
@ -106,8 +106,7 @@ public:
bool m_selected { false };
int m_watch_fd { -1 };
RefPtr<Core::Notifier> m_notifier;
RefPtr<Core::FileWatcher> m_file_watcher;
int m_error { 0 };
bool m_parent_of_root { false };