]>
Commit | Line | Data |
---|---|---|
6b8ef0b3 | 1 | ///////////////////////////////////////////////////////////////////////////// |
80fdcdb9 | 2 | // Name: src/msw/fswatcher.cpp |
6b8ef0b3 VZ |
3 | // Purpose: wxMSWFileSystemWatcher |
4 | // Author: Bartosz Bekier | |
5 | // Created: 2009-05-26 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2009 Bartosz Bekier <bartosz.bekier@gmail.com> | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // For compilers that support precompilation, includes "wx.h". | |
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #ifdef __BORLANDC__ | |
15 | #pragma hdrstop | |
16 | #endif | |
17 | ||
18 | #if wxUSE_FSWATCHER | |
19 | ||
20 | #include "wx/fswatcher.h" | |
21 | #include "wx/thread.h" | |
22 | #include "wx/sharedptr.h" | |
23 | #include "wx/msw/fswatcher.h" | |
24 | #include "wx/msw/private.h" | |
25 | #include "wx/private/fswatcher.h" | |
26 | ||
27 | // ============================================================================ | |
28 | // wxFSWatcherImplMSW implementation | |
29 | // ============================================================================ | |
30 | ||
31 | class wxFSWatcherImplMSW : public wxFSWatcherImpl | |
32 | { | |
33 | public: | |
34 | wxFSWatcherImplMSW(wxFileSystemWatcherBase* watcher); | |
35 | ||
36 | virtual ~wxFSWatcherImplMSW(); | |
37 | ||
38 | bool SetUpWatch(wxFSWatchEntryMSW& watch); | |
39 | ||
40 | void SendEvent(wxFileSystemWatcherEvent& evt); | |
41 | ||
42 | protected: | |
43 | bool Init(); | |
44 | ||
45 | // adds watch to be monitored for file system changes | |
46 | virtual bool DoAdd(wxSharedPtr<wxFSWatchEntryMSW> watch); | |
47 | ||
48 | virtual bool DoRemove(wxSharedPtr<wxFSWatchEntryMSW> watch); | |
49 | ||
50 | private: | |
51 | bool DoSetUpWatch(wxFSWatchEntryMSW& watch); | |
52 | ||
53 | static int Watcher2NativeFlags(int flags); | |
54 | ||
55 | wxIOCPService m_iocp; | |
56 | wxIOCPThread m_workerThread; | |
57 | }; | |
58 | ||
59 | wxFSWatcherImplMSW::wxFSWatcherImplMSW(wxFileSystemWatcherBase* watcher) : | |
60 | wxFSWatcherImpl(watcher), | |
61 | m_workerThread(this, &m_iocp) | |
62 | { | |
63 | } | |
64 | ||
65 | wxFSWatcherImplMSW::~wxFSWatcherImplMSW() | |
66 | { | |
67 | // order the worker thread to finish & wait | |
68 | m_workerThread.Finish(); | |
69 | if (m_workerThread.Wait() != 0) | |
70 | { | |
71 | wxLogError(_("Ungraceful worker thread termination")); | |
72 | } | |
73 | ||
74 | // remove all watches | |
75 | (void) RemoveAll(); | |
76 | } | |
77 | ||
78 | bool wxFSWatcherImplMSW::Init() | |
79 | { | |
80 | wxCHECK_MSG( !m_workerThread.IsAlive(), false, | |
81 | "Watcher service is already initialized" ); | |
82 | ||
83 | if (m_workerThread.Create() != wxTHREAD_NO_ERROR) | |
84 | { | |
85 | wxLogError(_("Unable to create IOCP worker thread")); | |
86 | return false; | |
87 | } | |
88 | ||
89 | // we have valid iocp service and thread | |
90 | if (m_workerThread.Run() != wxTHREAD_NO_ERROR) | |
91 | { | |
92 | wxLogError(_("Unable to start IOCP worker thread")); | |
93 | return false; | |
94 | } | |
95 | ||
96 | return true; | |
97 | } | |
98 | ||
99 | // adds watch to be monitored for file system changes | |
100 | bool wxFSWatcherImplMSW::DoAdd(wxSharedPtr<wxFSWatchEntryMSW> watch) | |
101 | { | |
102 | // setting up wait for directory changes | |
103 | if (!DoSetUpWatch(*watch)) | |
104 | return false; | |
105 | ||
106 | // associating handle with completion port | |
107 | return m_iocp.Add(watch); | |
108 | } | |
109 | ||
e652fc48 VZ |
110 | bool |
111 | wxFSWatcherImplMSW::DoRemove(wxSharedPtr<wxFSWatchEntryMSW> WXUNUSED(watch)) | |
6b8ef0b3 VZ |
112 | { |
113 | return true; | |
114 | } | |
115 | ||
116 | // TODO ensuring that we have not already set watch for this handle/dir? | |
117 | bool wxFSWatcherImplMSW::SetUpWatch(wxFSWatchEntryMSW& watch) | |
118 | { | |
119 | wxCHECK_MSG( watch.IsOk(), false, "Invalid watch" ); | |
120 | if (m_watches.find(watch.GetPath()) == m_watches.end()) | |
121 | { | |
122 | wxLogTrace(wxTRACE_FSWATCHER, "Path '%s' is not watched", | |
123 | watch.GetPath()); | |
124 | return false; | |
125 | } | |
126 | ||
127 | wxLogTrace(wxTRACE_FSWATCHER, "Setting up watch for file system changes..."); | |
128 | return DoSetUpWatch(watch); | |
129 | } | |
130 | ||
131 | void wxFSWatcherImplMSW::SendEvent(wxFileSystemWatcherEvent& evt) | |
132 | { | |
133 | // called from worker thread, so posting event in thread-safe way | |
134 | wxQueueEvent(m_watcher->GetOwner(), evt.Clone()); | |
135 | } | |
136 | ||
137 | bool wxFSWatcherImplMSW::DoSetUpWatch(wxFSWatchEntryMSW& watch) | |
138 | { | |
139 | int flags = Watcher2NativeFlags(watch.GetFlags()); | |
140 | int ret = ReadDirectoryChangesW(watch.GetHandle(), watch.GetBuffer(), | |
141 | wxFSWatchEntryMSW::BUFFER_SIZE, TRUE, | |
142 | flags, NULL, | |
143 | watch.GetOverlapped(), NULL); | |
144 | if (!ret) | |
145 | { | |
146 | wxLogSysError(_("Unable to set up watch for '%s'"), | |
147 | watch.GetPath()); | |
148 | } | |
149 | ||
150 | return ret != 0; | |
151 | } | |
152 | ||
153 | // TODO we should only specify those flags, which interest us | |
154 | // this needs a bit of thinking, quick impl for now | |
155 | int wxFSWatcherImplMSW::Watcher2NativeFlags(int WXUNUSED(flags)) | |
156 | { | |
157 | static DWORD all_events = FILE_NOTIFY_CHANGE_FILE_NAME | | |
158 | FILE_NOTIFY_CHANGE_DIR_NAME | FILE_NOTIFY_CHANGE_ATTRIBUTES | | |
159 | FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE | | |
160 | FILE_NOTIFY_CHANGE_LAST_ACCESS | FILE_NOTIFY_CHANGE_CREATION | | |
161 | FILE_NOTIFY_CHANGE_SECURITY; | |
162 | ||
163 | return all_events; | |
164 | } | |
165 | ||
166 | ||
167 | // ============================================================================ | |
168 | // wxFSWatcherImplMSW helper classes implementation | |
169 | // ============================================================================ | |
170 | ||
171 | wxIOCPThread::wxIOCPThread(wxFSWatcherImplMSW* service, wxIOCPService* iocp) : | |
172 | wxThread(wxTHREAD_JOINABLE), | |
173 | m_service(service), m_iocp(iocp) | |
174 | { | |
175 | } | |
176 | ||
177 | // finishes this thread | |
178 | bool wxIOCPThread::Finish() | |
179 | { | |
180 | wxLogTrace(wxTRACE_FSWATCHER, "Posting empty status!"); | |
181 | ||
182 | // send "empty" packet to i/o completion port, just to wake | |
183 | return m_iocp->PostEmptyStatus(); | |
184 | } | |
185 | ||
186 | wxThread::ExitCode wxIOCPThread::Entry() | |
187 | { | |
188 | wxLogTrace(wxTRACE_FSWATCHER, "[iocp] Started IOCP thread"); | |
189 | ||
190 | // read events in a loop until we get false, which means we should exit | |
191 | while ( ReadEvents() ); | |
192 | ||
193 | wxLogTrace(wxTRACE_FSWATCHER, "[iocp] Ended IOCP thread"); | |
194 | return (ExitCode)0; | |
195 | } | |
196 | ||
197 | // wait for events to occur, read them and send to interested parties | |
198 | // returns false it empty status was read, which means we whould exit | |
199 | // true otherwise | |
200 | bool wxIOCPThread::ReadEvents() | |
201 | { | |
202 | unsigned long count = 0; | |
203 | wxFSWatchEntryMSW* watch = NULL; | |
204 | OVERLAPPED* overlapped = NULL; | |
205 | if (!m_iocp->GetStatus(&count, &watch, &overlapped)) | |
206 | return true; // error was logged already, we don't want to exit | |
207 | ||
208 | // this is our exit condition, so we return false | |
209 | if (!count && !watch && !overlapped) | |
210 | return false; | |
211 | ||
212 | // in case of spurious wakeup | |
213 | if (!count || !watch) | |
214 | return true; | |
215 | ||
216 | wxLogTrace( wxTRACE_FSWATCHER, "[iocp] Read entry: path='%s'", | |
217 | watch->GetPath()); | |
218 | ||
219 | // extract events from buffer info our vector container | |
220 | wxVector<wxEventProcessingData> events; | |
221 | const char* memory = static_cast<const char*>(watch->GetBuffer()); | |
222 | int offset = 0; | |
223 | do | |
224 | { | |
225 | const FILE_NOTIFY_INFORMATION* e = | |
226 | static_cast<const FILE_NOTIFY_INFORMATION*>((const void*)memory); | |
227 | ||
228 | events.push_back(wxEventProcessingData(e, watch)); | |
229 | ||
230 | offset = e->NextEntryOffset; | |
231 | memory += offset; | |
232 | } | |
233 | while (offset); | |
234 | ||
235 | // process events | |
236 | ProcessNativeEvents(events); | |
237 | ||
238 | // reissue the watch. ignore possible errors, we will return true anyway | |
239 | (void) m_service->SetUpWatch(*watch); | |
240 | ||
241 | return true; | |
242 | } | |
243 | ||
244 | void wxIOCPThread::ProcessNativeEvents(wxVector<wxEventProcessingData>& events) | |
245 | { | |
246 | wxVector<wxEventProcessingData>::iterator it = events.begin(); | |
247 | for ( ; it != events.end(); ++it ) | |
248 | { | |
249 | const FILE_NOTIFY_INFORMATION& e = *(it->nativeEvent); | |
250 | const wxFSWatchEntryMSW* watch = it->watch; | |
251 | ||
252 | wxLogTrace( wxTRACE_FSWATCHER, "[iocp] %s", | |
253 | FileNotifyInformationToString(e)); | |
254 | ||
255 | int nativeFlags = e.Action; | |
256 | int flags = Native2WatcherFlags(nativeFlags); | |
257 | if (flags & wxFSW_EVENT_WARNING || flags & wxFSW_EVENT_ERROR) | |
258 | { | |
259 | // TODO think about this...do we ever have any errors to report? | |
260 | wxString errMsg = "Error occurred"; | |
261 | wxFileSystemWatcherEvent event(flags, errMsg); | |
262 | SendEvent(event); | |
263 | } | |
264 | // filter out ignored events and those not asked for. | |
265 | // we never filter out warnings or exceptions | |
266 | else if ((flags == 0) || !(flags & watch->GetFlags())) | |
267 | { | |
268 | return; | |
269 | } | |
270 | // rename case | |
271 | else if (nativeFlags == FILE_ACTION_RENAMED_OLD_NAME) | |
272 | { | |
273 | wxFileName oldpath = GetEventPath(*watch, e); | |
274 | wxFileName newpath; | |
275 | ||
276 | // newpath should be in the next entry. what if there isn't? | |
277 | ++it; | |
278 | if ( it != events.end() ) | |
279 | { | |
280 | newpath = GetEventPath(*(it->watch), *(it->nativeEvent)); | |
281 | } | |
282 | wxFileSystemWatcherEvent event(flags, oldpath, newpath); | |
283 | SendEvent(event); | |
284 | } | |
285 | // all other events | |
286 | else | |
287 | { | |
288 | // CHECK I heard that returned path can be either in short on long | |
289 | // form...need to account for that! | |
290 | wxFileName path = GetEventPath(*watch, e); | |
291 | wxFileSystemWatcherEvent event(flags, path, path); | |
292 | SendEvent(event); | |
293 | } | |
294 | } | |
295 | } | |
296 | ||
297 | void wxIOCPThread::SendEvent(wxFileSystemWatcherEvent& evt) | |
298 | { | |
299 | wxLogTrace(wxTRACE_FSWATCHER, "[iocp] EVT: %s", evt.ToString()); | |
300 | m_service->SendEvent(evt); | |
301 | } | |
302 | ||
303 | int wxIOCPThread::Native2WatcherFlags(int flags) | |
304 | { | |
305 | static const int flag_mapping[][2] = { | |
306 | { FILE_ACTION_ADDED, wxFSW_EVENT_CREATE }, | |
307 | { FILE_ACTION_REMOVED, wxFSW_EVENT_DELETE }, | |
308 | ||
309 | // TODO take attributes into account to see what happened | |
310 | { FILE_ACTION_MODIFIED, wxFSW_EVENT_MODIFY }, | |
311 | ||
312 | { FILE_ACTION_RENAMED_OLD_NAME, wxFSW_EVENT_RENAME }, | |
313 | ||
314 | // ignored as it should always be matched with ***_OLD_NAME | |
315 | { FILE_ACTION_RENAMED_NEW_NAME, 0 }, | |
316 | }; | |
317 | ||
318 | for (unsigned int i=0; i < WXSIZEOF(flag_mapping); ++i) { | |
319 | if (flags == flag_mapping[i][0]) | |
320 | return flag_mapping[i][1]; | |
321 | } | |
322 | ||
323 | // never reached | |
324 | wxFAIL_MSG(wxString::Format("Unknown file notify change %u", flags)); | |
325 | return -1; | |
326 | } | |
327 | ||
328 | wxString wxIOCPThread::FileNotifyInformationToString( | |
329 | const FILE_NOTIFY_INFORMATION& e) | |
330 | { | |
331 | wxString fname(e.FileName, e.FileNameLength / sizeof(e.FileName[0])); | |
332 | return wxString::Format("Event: offset=%d, action=%d, len=%d, " | |
333 | "name(approx)='%s'", e.NextEntryOffset, e.Action, | |
334 | e.FileNameLength, fname); | |
335 | } | |
336 | ||
337 | wxFileName wxIOCPThread::GetEventPath(const wxFSWatchEntryMSW& watch, | |
338 | const FILE_NOTIFY_INFORMATION& e) | |
339 | { | |
340 | wxFileName path = watch.GetPath(); | |
341 | if (path.IsDir()) | |
342 | { | |
343 | wxString rel(e.FileName, e.FileNameLength / sizeof(e.FileName[0])); | |
344 | int pathFlags = wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR; | |
345 | path = wxFileName(path.GetPath(pathFlags) + rel); | |
346 | } | |
347 | return path; | |
348 | } | |
349 | ||
350 | ||
351 | // ============================================================================ | |
352 | // wxMSWFileSystemWatcher implementation | |
353 | // ============================================================================ | |
354 | ||
355 | wxMSWFileSystemWatcher::wxMSWFileSystemWatcher() : | |
356 | wxFileSystemWatcherBase() | |
357 | { | |
358 | (void) Init(); | |
359 | } | |
360 | ||
361 | wxMSWFileSystemWatcher::wxMSWFileSystemWatcher(const wxFileName& path, | |
362 | int events) : | |
363 | wxFileSystemWatcherBase() | |
364 | { | |
365 | if (!Init()) | |
366 | return; | |
367 | ||
368 | Add(path, events); | |
369 | } | |
370 | ||
371 | bool wxMSWFileSystemWatcher::Init() | |
372 | { | |
373 | m_service = new wxFSWatcherImplMSW(this); | |
374 | bool ret = m_service->Init(); | |
375 | if (!ret) | |
376 | { | |
377 | delete m_service; | |
378 | } | |
379 | ||
380 | return ret; | |
381 | } | |
382 | ||
383 | #endif // wxUSE_FSWATCHER |