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:
parent
2acbb811b1
commit
c98ad27803
2 changed files with 18 additions and 24 deletions
|
@ -136,28 +136,23 @@ void FileSystemModel::Node::traverse_if_needed()
|
||||||
children.append(move(child));
|
children.append(move(child));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_watch_fd >= 0)
|
if (!m_file_watcher) {
|
||||||
return;
|
|
||||||
|
|
||||||
m_watch_fd = watch_file(full_path.characters(), full_path.length());
|
// We are not already watching this file, create a new watcher
|
||||||
if (m_watch_fd < 0) {
|
auto watcher_or_error = Core::FileWatcher::watch(full_path);
|
||||||
perror("watch_file");
|
|
||||||
return;
|
// 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();
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
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);
|
|
||||||
|
|
||||||
has_traversed = false;
|
|
||||||
mode = 0;
|
|
||||||
children.clear();
|
|
||||||
reify_if_needed();
|
|
||||||
m_model.did_update();
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void FileSystemModel::Node::reify_if_needed()
|
void FileSystemModel::Node::reify_if_needed()
|
||||||
|
|
|
@ -29,7 +29,7 @@
|
||||||
#include <AK/HashMap.h>
|
#include <AK/HashMap.h>
|
||||||
#include <AK/NonnullOwnPtrVector.h>
|
#include <AK/NonnullOwnPtrVector.h>
|
||||||
#include <LibCore/DateTime.h>
|
#include <LibCore/DateTime.h>
|
||||||
#include <LibCore/Notifier.h>
|
#include <LibCore/FileWatcher.h>
|
||||||
#include <LibGUI/Model.h>
|
#include <LibGUI/Model.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
@ -63,7 +63,7 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Node {
|
struct Node {
|
||||||
~Node() { close(m_watch_fd); }
|
~Node() { }
|
||||||
|
|
||||||
String name;
|
String name;
|
||||||
String symlink_target;
|
String symlink_target;
|
||||||
|
@ -106,8 +106,7 @@ public:
|
||||||
|
|
||||||
bool m_selected { false };
|
bool m_selected { false };
|
||||||
|
|
||||||
int m_watch_fd { -1 };
|
RefPtr<Core::FileWatcher> m_file_watcher;
|
||||||
RefPtr<Core::Notifier> m_notifier;
|
|
||||||
|
|
||||||
int m_error { 0 };
|
int m_error { 0 };
|
||||||
bool m_parent_of_root { false };
|
bool m_parent_of_root { false };
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue