5#if defined(PLATFORM_LINUX)
6#include <sys/inotify.h>
11#include <unordered_map>
12#elif defined(PLATFORM_WIN)
13#define WIN32_LEAN_AND_MEAN
23constexpr size_t EVENT_BUFFER_SIZE = 4096;
26#if defined(PLATFORM_LINUX)
30 std::unordered_map<int, std::string> dirs;
34 m_impl->fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
36 SLEAK_ERROR(
"FileWatcher: inotify_init1 failed: {}",
37 std::strerror(errno));
40FileWatcher::~FileWatcher() {
41 if (m_impl->fd >= 0) close(m_impl->fd);
44bool FileWatcher::AddDirectory(
const std::string& dir) {
45 if (m_impl->fd < 0)
return false;
46 const int wd = inotify_add_watch(m_impl->fd, dir.c_str(),
47 IN_CLOSE_WRITE | IN_MOVED_TO);
50 std::strerror(errno));
53 m_impl->dirs[wd] = dir;
57void FileWatcher::Poll(std::vector<std::string>& outChanged) {
58 if (m_impl->fd < 0)
return;
59 alignas(inotify_event)
char buf[EVENT_BUFFER_SIZE];
61 const ssize_t len = read(m_impl->fd, buf,
sizeof(buf));
63 for (
char* p = buf; p < buf + len;) {
64 const auto* ev =
reinterpret_cast<const inotify_event*
>(p);
65 p +=
sizeof(inotify_event) + ev->len;
66 if (ev->len == 0)
continue;
67 const auto dir = m_impl->dirs.find(ev->wd);
68 if (dir == m_impl->dirs.end())
continue;
69 outChanged.push_back(dir->second +
"/" + ev->name);
74#elif defined(PLATFORM_WIN)
77constexpr DWORD WATCH_FILTER =
78 FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_FILE_NAME;
80struct WatchedDirectory {
82 HANDLE handle = INVALID_HANDLE_VALUE;
83 OVERLAPPED overlapped = {};
84 alignas(DWORD)
char buffer[EVENT_BUFFER_SIZE];
88bool Arm(WatchedDirectory& dir) {
89 ResetEvent(dir.overlapped.hEvent);
90 return ReadDirectoryChangesW(dir.handle, dir.buffer,
sizeof(dir.buffer),
91 FALSE, WATCH_FILTER,
nullptr, &dir.overlapped,
96std::string ToUtf8(
const WCHAR* name,
int length) {
97 const int size = WideCharToMultiByte(CP_UTF8, 0, name, length,
nullptr, 0,
99 std::string out(size,
'\0');
100 WideCharToMultiByte(CP_UTF8, 0, name, length, out.data(), size,
nullptr,
106struct FileWatcher::Impl {
107 std::vector<std::unique_ptr<WatchedDirectory> > dirs;
110FileWatcher::FileWatcher() : m_impl(std::make_unique<Impl>()) {}
112FileWatcher::~FileWatcher() {
113 for (
auto& dir : m_impl->dirs) {
115 CancelIoEx(dir->handle, &dir->overlapped);
116 GetOverlappedResult(dir->handle, &dir->overlapped, &bytes, TRUE);
117 CloseHandle(dir->overlapped.hEvent);
118 CloseHandle(dir->handle);
122bool FileWatcher::AddDirectory(
const std::string& dir) {
123 auto watched = std::make_unique<WatchedDirectory>();
126 CreateFileA(dir.c_str(), FILE_LIST_DIRECTORY,
127 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
128 nullptr, OPEN_EXISTING,
129 FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED,
nullptr);
130 if (watched->handle == INVALID_HANDLE_VALUE) {
131 SLEAK_ERROR(
"FileWatcher: can't open {} (error {})", dir,
135 watched->overlapped.hEvent = CreateEventA(
nullptr, TRUE, FALSE,
nullptr);
136 if (!watched->overlapped.hEvent || !Arm(*watched)) {
137 SLEAK_ERROR(
"FileWatcher: can't watch {} (error {})", dir,
139 if (watched->overlapped.hEvent) CloseHandle(watched->overlapped.hEvent);
140 CloseHandle(watched->handle);
143 m_impl->dirs.push_back(std::move(watched));
147void FileWatcher::Poll(std::vector<std::string>& outChanged) {
148 for (
auto& dir : m_impl->dirs) {
150 if (!GetOverlappedResult(dir->handle, &dir->overlapped, &bytes, FALSE))
152 for (DWORD offset = 0; bytes > 0;) {
153 const auto* info =
reinterpret_cast<const FILE_NOTIFY_INFORMATION*
>(
154 dir->buffer + offset);
155 if (info->Action != FILE_ACTION_REMOVED &&
156 info->Action != FILE_ACTION_RENAMED_OLD_NAME) {
158 static_cast<int>(info->FileNameLength /
sizeof(WCHAR));
159 outChanged.push_back(dir->path +
"/" +
160 ToUtf8(info->FileName, length));
162 if (info->NextEntryOffset == 0)
break;
163 offset += info->NextEntryOffset;
178 SLEAK_WARN(
"FileWatcher: not supported on this platform ({})", dir);
void Poll(std::vector< std::string > &outChanged)
Appends full paths of files saved since the last call. Never blocks.
bool AddDirectory(const std::string &dir)
Starts watching a directory. Returns false if it can't be watched.
Root namespace for everything the engine exposes.