载入中...
搜索中...
未找到
FileWatch.cpp
浏览该文件的文档.
3#include "common/config.h"
4
5#include <algorithm>
6
7#ifndef EVENGINE_WEBGPU
8
9#include <Poco/Delegate.h>
10#include <Poco/DirectoryWatcher.h>
11#include <Poco/File.h>
12#include <Poco/Path.h>
13
14#include <cstdlib>
15#include <vector>
16
17#endif
18
19namespace eve::filesystem {
20
21namespace {
22
23constexpr auto kDebounceDelay = std::chrono::milliseconds(75);
24
25} // namespace
26
27#ifdef EVENGINE_WEBGPU
28// WebGPU (browser) build has no native directory watching; DirWatch stays an
29// empty struct so the unique_ptr members in the header compile.
30struct FileWatch::DirWatch {};
31#endif
32
33#ifndef EVENGINE_WEBGPU
34namespace {
35
36std::string basenameOf(const std::string &path) {
37 Poco::Path p(path);
38 return p.getFileName();
39}
40
41std::string normalizeDir(const std::string &dir) {
42 std::string s;
43#if !defined(_WIN32)
44 // Resolve symlinks (/var → /private/var on macOS) so watch keys match event paths.
45 char *resolved = realpath(dir.c_str(), nullptr);
46 if (resolved) {
47 s = resolved;
48 free(resolved);
49 } else
50#endif
51 {
52 Poco::Path p(dir);
53 p.makeAbsolute();
54 s = p.toString();
55 }
56 while (s.size() > 1 && (s.back() == '/' || s.back() == '\\')) s.pop_back();
57 return s;
58}
59
60} // namespace
61
63 std::string realDir;
64 std::unique_ptr<Poco::DirectoryWatcher> watcher;
65 std::unordered_map<std::string, std::string> filters; // filter → reportPath
66 int refs = 0;
67};
68#endif // !EVENGINE_WEBGPU
69
70FileWatch::FileWatch() = default;
71
73
74bool FileWatch::add(const std::string &realDir, const std::string &filterName,
75 const std::string &reportPath, int scanInterval) {
76#ifdef EVENGINE_WEBGPU
77 // Browser VFS has no native directory watching; no-op stub.
78 (void)realDir;
79 (void)filterName;
80 (void)reportPath;
81 (void)scanInterval;
82 return false;
83#else
84 if (realDir.empty() || reportPath.empty()) return false;
85 const std::string dir = normalizeDir(realDir);
86 try {
87 Poco::File f(dir);
88 if (!f.exists() || !f.isDirectory()) return false;
89 } catch (...) {
90 return false;
91 }
92
93 // Poco's Delegate::notify() invokes our callback while holding the
94 // delegate's own mutex, and `-=` (DefaultStrategy::remove ->
95 // Delegate::disable()) blocks on that same mutex. So both unsubscribing
96 // the delegates AND destroying the replaced DirectoryWatcher (whose dtor
97 // joins its inotify/OS thread) must happen outside mu_: otherwise the
98 // watcher thread blocked in handlePocoEvent() on mu_ while holding the
99 // delegate mutex deadlocks against us (ABBA) right after a file write.
100 std::unique_ptr<DirWatch> doomed;
101 bool ok = false;
102 {
103 std::lock_guard<std::mutex> lock(mu_);
104
105 auto prev = reportToDir_.find(reportPath);
106 if (prev != reportToDir_.end()) {
107 auto it = byDir_.find(prev->second);
108 if (it != byDir_.end()) {
109 DirWatch *dw = it->second.get();
110 for (auto fit = dw->filters.begin(); fit != dw->filters.end();) {
111 if (fit->second == reportPath) {
112 fit = dw->filters.erase(fit);
113 --dw->refs;
114 } else {
115 ++fit;
116 }
117 }
118 if (dw->refs <= 0) {
119 doomed = std::move(it->second);
120 byDir_.erase(it);
121 }
122 }
123 reportToDir_.erase(prev);
124 }
125
126 DirWatch *dw = nullptr;
127 bool created = false;
128 auto it = byDir_.find(dir);
129 if (it == byDir_.end()) {
130 auto owned = std::make_unique<DirWatch>();
131 owned->realDir = dir;
132 try {
133 owned->watcher = std::make_unique<Poco::DirectoryWatcher>(
134 dir, Poco::DirectoryWatcher::DW_FILTER_ENABLE_ALL, scanInterval);
135 } catch (...) {
136 ok = false;
137 }
138 if (owned->watcher) {
139 dw = owned.get();
140 byDir_[dir] = std::move(owned);
141 created = true;
142 }
143 } else {
144 dw = it->second.get();
145 }
146
147 if (dw && created) {
148 dw->watcher->itemAdded += Poco::delegate(this, &FileWatch::onAdded);
149 dw->watcher->itemRemoved += Poco::delegate(this, &FileWatch::onRemoved);
150 dw->watcher->itemModified += Poco::delegate(this, &FileWatch::onModified);
151 dw->watcher->itemMovedFrom += Poco::delegate(this, &FileWatch::onMovedFrom);
152 dw->watcher->itemMovedTo += Poco::delegate(this, &FileWatch::onMovedTo);
153 }
154 if (dw) {
155 dw->filters[filterName] = reportPath;
156 ++dw->refs;
157 reportToDir_[reportPath] = dir;
158 ok = true;
159 }
160 }
161 if (doomed) unsubscribeDelegates(doomed.get());
162 return ok;
163#endif
164}
165
166bool FileWatch::remove(const std::string &reportPath) {
167#ifdef EVENGINE_WEBGPU
168 (void)reportPath;
169 return false;
170#else
171 // Same as add(): unsubscribe the delegates AND destroy the
172 // DirectoryWatcher (joins its thread) after mu_ is released, so a watcher
173 // thread blocked in handlePocoEvent() on mu_ cannot deadlock against us.
174 std::unique_ptr<DirWatch> doomed;
175 {
176 std::lock_guard<std::mutex> lock(mu_);
177 auto prev = reportToDir_.find(reportPath);
178 if (prev == reportToDir_.end()) return false;
179 const std::string dir = prev->second;
180 reportToDir_.erase(prev);
181
182 auto it = byDir_.find(dir);
183 if (it == byDir_.end()) return false;
184 DirWatch *dw = it->second.get();
185 for (auto fit = dw->filters.begin(); fit != dw->filters.end();) {
186 if (fit->second == reportPath) {
187 fit = dw->filters.erase(fit);
188 --dw->refs;
189 } else {
190 ++fit;
191 }
192 }
193 if (dw->refs <= 0) {
194 doomed = std::move(it->second);
195 byDir_.erase(it);
196 }
197 }
198 if (doomed) unsubscribeDelegates(doomed.get());
199 return true;
200#endif
201}
202
204#ifdef EVENGINE_WEBGPU
205 std::lock_guard<std::mutex> lock(mu_);
206 queue_.clear();
207 return;
208#else
209 // Unsubscribe all delegates and destroy the DirectoryWatchers (each joins
210 // its inotify/OS thread) after mu_ is released, so a watcher thread
211 // blocked in handlePocoEvent() on the same lock cannot deadlock against
212 // clear().
213 std::vector<std::unique_ptr<DirWatch>> doomed;
214 {
215 std::lock_guard<std::mutex> lock(mu_);
216 for (auto &kv : byDir_) doomed.push_back(std::move(kv.second));
217 byDir_.clear();
218 reportToDir_.clear();
219 queue_.clear();
220 }
221 for (const auto &dw : doomed) unsubscribeDelegates(dw.get());
222#endif
223}
224
225int FileWatch::count() const {
226 std::lock_guard<std::mutex> lock(mu_);
227#ifdef EVENGINE_WEBGPU
228 return 0;
229#else
230 return int(reportToDir_.size());
231#endif
232}
233
235 std::lock_guard<std::mutex> lock(mu_);
236 const auto now = std::chrono::steady_clock::now();
237 auto it = std::find_if(queue_.begin(), queue_.end(),
238 [now](const PendingEvent &event) { return event.readyAt <= now; });
239 if (it == queue_.end()) return false;
240 out = std::move(it->event);
241 queue_.erase(it);
242 return true;
243}
244
245void FileWatch::enqueue(Event event) {
246 const auto readyAt = std::chrono::steady_clock::now() + kDebounceDelay;
247 auto pending = std::find_if(queue_.begin(), queue_.end(), [&](const PendingEvent &queued) {
248 return queued.event.realPath == event.realPath;
249 });
250 if (pending != queue_.end()) {
251 pending->event = std::move(event);
252 pending->readyAt = readyAt;
253 return;
254 }
255 queue_.push_back({std::move(event), readyAt});
256}
257
258#ifndef EVENGINE_WEBGPU
259void FileWatch::unsubscribeDelegates(DirWatch *dw) {
260 if (!dw || !dw->watcher) return;
261 dw->watcher->itemAdded -= Poco::delegate(this, &FileWatch::onAdded);
262 dw->watcher->itemRemoved -= Poco::delegate(this, &FileWatch::onRemoved);
263 dw->watcher->itemModified -= Poco::delegate(this, &FileWatch::onModified);
264 dw->watcher->itemMovedFrom -= Poco::delegate(this, &FileWatch::onMovedFrom);
265 dw->watcher->itemMovedTo -= Poco::delegate(this, &FileWatch::onMovedTo);
266}
267
268void FileWatch::handlePocoEvent(const std::string &kind, const std::string &itemPath) {
269 const std::string name = basenameOf(itemPath);
270 Poco::Path parent(itemPath);
271 parent.makeParent();
272 const std::string dir = normalizeDir(parent.toString());
273
274 std::lock_guard<std::mutex> lock(mu_);
275 auto it = byDir_.find(dir);
276 if (it == byDir_.end()) {
277 for (auto &kv : byDir_) {
278 if (itemPath.size() >= kv.first.size() && itemPath.compare(0, kv.first.size(), kv.first) == 0) {
279 it = byDir_.find(kv.first);
280 break;
281 }
282 }
283 }
284 if (it == byDir_.end()) return;
285 DirWatch *dw = it->second.get();
286
287 auto push = [&](const std::string &report) {
288 Event ev;
289 ev.kind = kind;
290 ev.path = report;
291 ev.realPath = itemPath;
292 enqueue(std::move(ev));
293 };
294
295 auto all = dw->filters.find("");
296 if (all != dw->filters.end()) {
297 std::string report = all->second;
298 if (!report.empty() && report.back() != '/') report += "/";
299 report += name;
300 push(report);
301 }
302
303 auto fit = dw->filters.find(name);
304 if (fit != dw->filters.end()) push(fit->second);
305}
306
307void FileWatch::onAdded(const void *, const Poco::DirectoryWatcher::DirectoryEvent &event) {
308 handlePocoEvent("added", event.item.path());
309}
310
311void FileWatch::onRemoved(const void *, const Poco::DirectoryWatcher::DirectoryEvent &event) {
312 handlePocoEvent("removed", event.item.path());
313}
314
315void FileWatch::onModified(const void *, const Poco::DirectoryWatcher::DirectoryEvent &event) {
316 handlePocoEvent("modified", event.item.path());
317}
318
319void FileWatch::onMovedFrom(const void *, const Poco::DirectoryWatcher::DirectoryEvent &event) {
320 handlePocoEvent("movedFrom", event.item.path());
321}
322
323void FileWatch::onMovedTo(const void *, const Poco::DirectoryWatcher::DirectoryEvent &event) {
324 handlePocoEvent("movedTo", event.item.path());
325}
326#endif // !EVENGINE_WEBGPU
327
328} // namespace eve::filesystem
ActiveSource owned
std::vector< QuestEvent > pending
const std::string & s
glm::vec4 p[6]
std::string name
Definition ECS.cpp:154
const ColourScope * parent
bool all
size_t queued
Definition OnnxGpgpu.cpp:60
float f
std::string path
Definition PlayHost.cpp:111
RoadNetwork resolved
int created
ScriptStructure out
std::string kind
Share string attribute map operations through the Windows Foundation DLL.
V3 dir
Definition TreeMesh.cpp:151
bool add(const std::string &realDir, const std::string &filterName, const std::string &reportPath, int scanInterval=1)
Definition FileWatch.cpp:74
bool poll(Event &out)
Pop one event; false if empty.
bool remove(const std::string &reportPath)
Remove by reportPath previously passed to add().
std::unique_ptr< Poco::DirectoryWatcher > watcher
Definition FileWatch.cpp:64
std::unordered_map< std::string, std::string > filters
Definition FileWatch.cpp:65