Support monitoring only some events in wxGTK wxFileSystemWatcher.
[wxWidgets.git] / src / unix / fswatcher_inotify.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/unix/fswatcher_inotify.cpp
3 // Purpose: inotify-based wxFileSystemWatcher implementation
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
22 #ifdef wxHAS_INOTIFY
23
24 #include <sys/inotify.h>
25 #include <unistd.h>
26 #include "wx/private/fswatcher.h"
27
28 // ============================================================================
29 // wxFSWatcherImpl implementation & helper wxFSWSourceHandler implementation
30 // ============================================================================
31
32 // inotify watch descriptor => wxFSWatchEntry* map
33 WX_DECLARE_HASH_MAP(int, wxFSWatchEntry*, wxIntegerHash, wxIntegerEqual,
34 wxFSWatchEntryDescriptors);
35
36 // inotify event cookie => inotify_event* map
37 WX_DECLARE_HASH_MAP(int, inotify_event*, wxIntegerHash, wxIntegerEqual,
38 wxInotifyCookies);
39
40 /**
41 * Helper class encapsulating inotify mechanism
42 */
43 class wxFSWatcherImplUnix : public wxFSWatcherImpl
44 {
45 public:
46 wxFSWatcherImplUnix(wxFileSystemWatcherBase* watcher) :
47 wxFSWatcherImpl(watcher),
48 m_source(NULL),
49 m_ifd(-1)
50 {
51 m_handler = new wxFSWSourceHandler(this);
52 }
53
54 ~wxFSWatcherImplUnix()
55 {
56 // we close inotify only if initialized before
57 if (IsOk())
58 {
59 Close();
60 }
61
62 delete m_handler;
63 }
64
65 bool Init()
66 {
67 wxCHECK_MSG( !IsOk(), false, "Inotify already initialized" );
68
69 wxEventLoopBase *loop = wxEventLoopBase::GetActive();
70 wxCHECK_MSG( loop, false, "File system watcher needs an event loop" );
71
72 m_ifd = inotify_init();
73 if ( m_ifd == -1 )
74 {
75 wxLogSysError( _("Unable to create inotify instance") );
76 return false;
77 }
78
79 m_source = loop->AddSourceForFD
80 (
81 m_ifd,
82 m_handler,
83 wxEVENT_SOURCE_INPUT | wxEVENT_SOURCE_EXCEPTION
84 );
85
86 return m_source != NULL;
87 }
88
89 void Close()
90 {
91 wxCHECK_RET( IsOk(),
92 "Inotify not initialized or invalid inotify descriptor" );
93
94 wxDELETE(m_source);
95
96 if ( close(m_ifd) != 0 )
97 {
98 wxLogSysError( _("Unable to close inotify instance") );
99 }
100 }
101
102 virtual bool DoAdd(wxSharedPtr<wxFSWatchEntryUnix> watch)
103 {
104 wxCHECK_MSG( IsOk(), false,
105 "Inotify not initialized or invalid inotify descriptor" );
106
107 int wd = DoAddInotify(watch.get());
108 if (wd == -1)
109 {
110 wxLogSysError( _("Unable to add inotify watch") );
111 return false;
112 }
113
114 wxFSWatchEntryDescriptors::value_type val(wd, watch.get());
115 if (!m_watchMap.insert(val).second)
116 {
117 wxFAIL_MSG( wxString::Format( "Path %s is already watched",
118 watch->GetPath()) );
119 return false;
120 }
121
122 return true;
123 }
124
125 virtual bool DoRemove(wxSharedPtr<wxFSWatchEntryUnix> watch)
126 {
127 wxCHECK_MSG( IsOk(), false,
128 "Inotify not initialized or invalid inotify descriptor" );
129
130 int ret = DoRemoveInotify(watch.get());
131 if (ret == -1)
132 {
133 wxLogSysError( _("Unable to remove inotify watch") );
134 return false;
135 }
136
137 if (m_watchMap.erase(watch->GetWatchDescriptor()) != 1)
138 {
139 wxFAIL_MSG( wxString::Format("Path %s is not watched",
140 watch->GetPath()) );
141 }
142 // Cache the wd in case any events arrive late
143 m_staleDescriptors.Add(watch->GetWatchDescriptor());
144
145 watch->SetWatchDescriptor(-1);
146 return true;
147 }
148
149 virtual bool RemoveAll()
150 {
151 wxFSWatchEntries::iterator it = m_watches.begin();
152 for ( ; it != m_watches.end(); ++it )
153 {
154 (void) DoRemove(it->second);
155 }
156 m_watches.clear();
157 return true;
158 }
159
160 int ReadEvents()
161 {
162 wxCHECK_MSG( IsOk(), -1,
163 "Inotify not initialized or invalid inotify descriptor" );
164
165 // read events
166 // TODO differentiate depending on params
167 char buf[128 * sizeof(inotify_event)];
168 int left = ReadEventsToBuf(buf, sizeof(buf));
169 if (left == -1)
170 return -1;
171
172 // left > 0, we have events
173 char* memory = buf;
174 int event_count = 0;
175 while (left > 0) // OPT checking 'memory' would suffice
176 {
177 event_count++;
178 inotify_event* e = (inotify_event*)memory;
179
180 // process one inotify_event
181 ProcessNativeEvent(*e);
182
183 int offset = sizeof(inotify_event) + e->len;
184 left -= offset;
185 memory += offset;
186 }
187
188 // take care of unmatched renames
189 ProcessRenames();
190
191 wxLogTrace(wxTRACE_FSWATCHER, "We had %d native events", event_count);
192 return event_count;
193 }
194
195 bool IsOk() const
196 {
197 return m_source != NULL;
198 }
199
200 protected:
201 int DoAddInotify(wxFSWatchEntry* watch)
202 {
203 int flags = Watcher2NativeFlags(watch->GetFlags());
204 int wd = inotify_add_watch(m_ifd, watch->GetPath().fn_str(), flags);
205 // finally we can set watch descriptor
206 watch->SetWatchDescriptor(wd);
207 return wd;
208 }
209
210 int DoRemoveInotify(wxFSWatchEntry* watch)
211 {
212 return inotify_rm_watch(m_ifd, watch->GetWatchDescriptor());
213 }
214
215 void ProcessNativeEvent(const inotify_event& inevt)
216 {
217 wxLogTrace(wxTRACE_FSWATCHER, InotifyEventToString(inevt));
218
219 // after removing inotify watch we get IN_IGNORED for it, but the watch
220 // will be already removed from our list at that time
221 if (inevt.mask & IN_IGNORED)
222 {
223 // It is now safe to remove it from the stale descriptors too, we
224 // won't get any more events for it.
225 // However if we're here because a dir that we're still watching
226 // has just been deleted, its wd won't be on this list
227 const int pos = m_staleDescriptors.Index(inevt.wd);
228 if ( pos != wxNOT_FOUND )
229 {
230 m_staleDescriptors.RemoveAt(static_cast<size_t>(pos));
231 wxLogTrace(wxTRACE_FSWATCHER,
232 "Removed wd %i from the stale-wd cache", inevt.wd);
233 }
234 return;
235 }
236
237 // get watch entry for this event
238 wxFSWatchEntryDescriptors::iterator it = m_watchMap.find(inevt.wd);
239 if (it == m_watchMap.end())
240 {
241 // It's not in the map; check if was recently removed from it.
242 if (m_staleDescriptors.Index(inevt.wd) != wxNOT_FOUND)
243 {
244 wxLogTrace(wxTRACE_FSWATCHER,
245 "Got an event for stale wd %i", inevt.wd);
246 }
247 else
248 {
249 wxFAIL_MSG("Event for unknown watch descriptor.");
250 }
251
252 // In any case, don't process this event: it's either for an
253 // already removed entry, or for a completely unknown one.
254 return;
255 }
256
257 wxFSWatchEntry& watch = *(it->second);
258 int nativeFlags = inevt.mask;
259 int flags = Native2WatcherFlags(nativeFlags);
260
261 // check out for error/warning condition
262 if (flags & wxFSW_EVENT_WARNING || flags & wxFSW_EVENT_ERROR)
263 {
264 wxString errMsg = GetErrorDescription(Watcher2NativeFlags(flags));
265 wxFileSystemWatcherEvent event(flags, errMsg);
266 SendEvent(event);
267 }
268 // filter out ignored events and those not asked for.
269 // we never filter out warnings or exceptions
270 else if ((flags == 0) || !(flags & watch.GetFlags()))
271 {
272 return;
273 }
274
275 // Creation
276 // We need do something here only if the original watch was recursive;
277 // we don't watch a child dir itself inside a non-tree watch.
278 // We watch only dirs explicitly, so we don't want file IN_CREATEs.
279 // Distinguish by whether nativeFlags contain IN_ISDIR
280 else if ((nativeFlags & IN_CREATE) &&
281 (watch.GetType() == wxFSWPath_Tree) && (inevt.mask & IN_ISDIR))
282 {
283 wxFileName fn = GetEventPath(watch, inevt);
284 // Though it's a dir, fn treats it as a file. So:
285 fn.AssignDir(fn.GetFullPath());
286
287 if (m_watcher->AddAny(fn, wxFSW_EVENT_ALL,
288 wxFSWPath_Tree, watch.GetFilespec()))
289 {
290 // Tell the owner, in case it's interested
291 // If there's a filespec, assume he's not
292 if (watch.GetFilespec().empty())
293 {
294 wxFileSystemWatcherEvent event(flags, fn, fn);
295 SendEvent(event);
296 }
297 }
298 }
299
300 // Deletion
301 // We watch only dirs explicitly, so we don't want file IN_DELETEs.
302 // We obviously can't check using DirExists() as the object has been
303 // deleted; and nativeFlags here doesn't contain IN_ISDIR, even for
304 // a dir. Fortunately IN_DELETE_SELF doesn't happen for files. We need
305 // to do something here only inside a tree watch, or if it's the parent
306 // dir that's deleted. Otherwise let the parent dir cope
307 else if ((nativeFlags & IN_DELETE_SELF) &&
308 ((watch.GetType() == wxFSWPath_Dir) ||
309 (watch.GetType() == wxFSWPath_Tree)))
310 {
311 // We must remove the deleted directory from the map, so that
312 // DoRemoveInotify() isn't called on it in the future. Don't assert
313 // if the wd isn't found: repeated IN_DELETE_SELFs can occur
314 wxFileName fn = GetEventPath(watch, inevt);
315 wxString path(fn.GetPathWithSep());
316
317 if (m_watchMap.erase(inevt.wd) == 1)
318 {
319 // Delete from wxFileSystemWatcher
320 wxDynamicCast(m_watcher, wxInotifyFileSystemWatcher)->
321 OnDirDeleted(path);
322
323 // Now remove from our local list of watched items
324 wxFSWatchEntries::iterator wit =
325 m_watches.find(path);
326 if (wit != m_watches.end())
327 {
328 m_watches.erase(wit);
329 }
330
331 // Cache the wd in case any events arrive late
332 m_staleDescriptors.Add(inevt.wd);
333 }
334
335 // Tell the owner, in case it's interested
336 // If there's a filespec, assume he's not
337 if (watch.GetFilespec().empty())
338 {
339 wxFileSystemWatcherEvent event(flags, fn, fn);
340 SendEvent(event);
341 }
342 }
343
344 // renames
345 else if (nativeFlags & IN_MOVE)
346 {
347 wxInotifyCookies::iterator it2 = m_cookies.find(inevt.cookie);
348 if ( it2 == m_cookies.end() )
349 {
350 int size = sizeof(inevt) + inevt.len;
351 inotify_event* e = (inotify_event*) operator new (size);
352 memcpy(e, &inevt, size);
353
354 wxInotifyCookies::value_type val(e->cookie, e);
355 m_cookies.insert(val);
356 }
357 else
358 {
359 inotify_event& oldinevt = *(it2->second);
360
361 // Tell the owner, in case it's interested
362 // If there's a filespec, assume he's not
363 if ( watch.GetFilespec().empty() )
364 {
365 wxFileSystemWatcherEvent event(flags);
366 if ( inevt.mask & IN_MOVED_FROM )
367 {
368 event.SetPath(GetEventPath(watch, inevt));
369 event.SetNewPath(GetEventPath(watch, oldinevt));
370 }
371 else
372 {
373 event.SetPath(GetEventPath(watch, oldinevt));
374 event.SetNewPath(GetEventPath(watch, inevt));
375 }
376 SendEvent(event);
377 }
378
379 m_cookies.erase(it2);
380 delete &oldinevt;
381 }
382 }
383 // every other kind of event
384 else
385 {
386 wxFileName path = GetEventPath(watch, inevt);
387 // For files, check that it matches any filespec
388 if ( MatchesFilespec(path, watch.GetFilespec()) )
389 {
390 wxFileSystemWatcherEvent event(flags, path, path);
391 SendEvent(event);
392 }
393 }
394 }
395
396 void ProcessRenames()
397 {
398 wxInotifyCookies::iterator it = m_cookies.begin();
399 while ( it != m_cookies.end() )
400 {
401 inotify_event& inevt = *(it->second);
402
403 wxLogTrace(wxTRACE_FSWATCHER, "Processing pending rename events");
404 wxLogTrace(wxTRACE_FSWATCHER, InotifyEventToString(inevt));
405
406 // get watch entry for this event
407 wxFSWatchEntryDescriptors::iterator wit = m_watchMap.find(inevt.wd);
408 wxCHECK_RET(wit != m_watchMap.end(),
409 "Watch descriptor not present in the watch map!");
410
411 // Tell the owner, in case it's interested
412 // If there's a filespec, assume he's not
413 wxFSWatchEntry& watch = *(wit->second);
414 if ( watch.GetFilespec().empty() )
415 {
416 int flags = Native2WatcherFlags(inevt.mask);
417 wxFileName path = GetEventPath(watch, inevt);
418 {
419 wxFileSystemWatcherEvent event(flags, path, path);
420 SendEvent(event);
421 }
422 }
423
424 m_cookies.erase(it);
425 delete &inevt;
426 it = m_cookies.begin();
427 }
428 }
429
430 void SendEvent(wxFileSystemWatcherEvent& evt)
431 {
432 wxLogTrace(wxTRACE_FSWATCHER, evt.ToString());
433 m_watcher->GetOwner()->ProcessEvent(evt);
434 }
435
436 int ReadEventsToBuf(char* buf, int size)
437 {
438 wxCHECK_MSG( IsOk(), false,
439 "Inotify not initialized or invalid inotify descriptor" );
440
441 memset(buf, 0, size);
442 ssize_t left = read(m_ifd, buf, size);
443 if (left == -1)
444 {
445 wxLogSysError(_("Unable to read from inotify descriptor"));
446 return -1;
447 }
448 else if (left == 0)
449 {
450 wxLogWarning(_("EOF while reading from inotify descriptor"));
451 return -1;
452 }
453
454 return left;
455 }
456
457 static wxString InotifyEventToString(const inotify_event& inevt)
458 {
459 wxString mask = (inevt.mask & IN_ISDIR) ?
460 wxString::Format("IS_DIR | %u", inevt.mask & ~IN_ISDIR) :
461 wxString::Format("%u", inevt.mask);
462 const char* name = "";
463 if (inevt.len)
464 name = inevt.name;
465 return wxString::Format("Event: wd=%d, mask=%s, cookie=%u, len=%u, "
466 "name=%s", inevt.wd, mask, inevt.cookie,
467 inevt.len, name);
468 }
469
470 static wxFileName GetEventPath(const wxFSWatchEntry& watch,
471 const inotify_event& inevt)
472 {
473 // only when dir is watched, we have non-empty e.name
474 wxFileName path = watch.GetPath();
475 if (path.IsDir() && inevt.len)
476 {
477 path = wxFileName(path.GetPath(), inevt.name);
478 }
479 return path;
480 }
481
482 static int Watcher2NativeFlags(int flags)
483 {
484 // Start with the standard case of wanting all events
485 if (flags == wxFSW_EVENT_ALL)
486 {
487 return IN_ALL_EVENTS;
488 }
489
490 static const int flag_mapping[][2] = {
491 { wxFSW_EVENT_ACCESS, IN_ACCESS },
492 { wxFSW_EVENT_MODIFY, IN_MODIFY },
493 { wxFSW_EVENT_RENAME, IN_MOVE },
494 { wxFSW_EVENT_CREATE, IN_CREATE },
495 { wxFSW_EVENT_DELETE, IN_DELETE|IN_DELETE_SELF|IN_MOVE_SELF }
496 // wxFSW_EVENT_ERROR/WARNING make no sense here
497 };
498
499 int native_flags = 0;
500 for ( unsigned int i=0; i < WXSIZEOF(flag_mapping); ++i)
501 {
502 if (flags & flag_mapping[i][0])
503 native_flags |= flag_mapping[i][1];
504 }
505
506 return native_flags;
507 }
508
509 static int Native2WatcherFlags(int flags)
510 {
511 static const int flag_mapping[][2] = {
512 { IN_ACCESS, wxFSW_EVENT_ACCESS }, // generated during read!
513 { IN_MODIFY, wxFSW_EVENT_MODIFY },
514 { IN_ATTRIB, 0 },
515 { IN_CLOSE_WRITE, 0 },
516 { IN_CLOSE_NOWRITE, 0 },
517 { IN_OPEN, 0 },
518 { IN_MOVED_FROM, wxFSW_EVENT_RENAME },
519 { IN_MOVED_TO, wxFSW_EVENT_RENAME },
520 { IN_CREATE, wxFSW_EVENT_CREATE },
521 { IN_DELETE, wxFSW_EVENT_DELETE },
522 { IN_DELETE_SELF, wxFSW_EVENT_DELETE },
523 { IN_MOVE_SELF, wxFSW_EVENT_DELETE },
524
525 { IN_UNMOUNT, wxFSW_EVENT_ERROR },
526 { IN_Q_OVERFLOW, wxFSW_EVENT_WARNING},
527
528 // ignored, because this is generated mainly by watcher::Remove()
529 { IN_IGNORED, 0 }
530 };
531
532 unsigned int i=0;
533 for ( ; i < WXSIZEOF(flag_mapping); ++i) {
534 // in this mapping multiple flags at once don't happen
535 if (flags & flag_mapping[i][0])
536 return flag_mapping[i][1];
537 }
538
539 // never reached
540 wxFAIL_MSG(wxString::Format("Unknown inotify event mask %u", flags));
541 return -1;
542 }
543
544 /**
545 * Returns error description for specified inotify mask
546 */
547 static const wxString GetErrorDescription(int flag)
548 {
549 switch ( flag )
550 {
551 case IN_UNMOUNT:
552 return _("File system containing watched object was unmounted");
553 case IN_Q_OVERFLOW:
554 return _("Event queue overflowed");
555 }
556
557 // never reached
558 wxFAIL_MSG(wxString::Format("Unknown inotify event mask %u", flag));
559 return wxEmptyString;
560 }
561
562 wxFSWSourceHandler* m_handler; // handler for inotify event source
563 wxFSWatchEntryDescriptors m_watchMap; // inotify wd=>wxFSWatchEntry* map
564 wxArrayInt m_staleDescriptors; // stores recently-removed watches
565 wxInotifyCookies m_cookies; // map to track renames
566 wxEventLoopSource* m_source; // our event loop source
567
568 // file descriptor created by inotify_init()
569 int m_ifd;
570 };
571
572
573 // ============================================================================
574 // wxFSWSourceHandler implementation
575 // ============================================================================
576
577 // once we get signaled to read, actuall event reading occurs
578 void wxFSWSourceHandler::OnReadWaiting()
579 {
580 wxLogTrace(wxTRACE_FSWATCHER, "--- OnReadWaiting ---");
581 m_service->ReadEvents();
582 }
583
584 void wxFSWSourceHandler::OnWriteWaiting()
585 {
586 wxFAIL_MSG("We never write to inotify descriptor.");
587 }
588
589 void wxFSWSourceHandler::OnExceptionWaiting()
590 {
591 wxFAIL_MSG("We never receive exceptions on inotify descriptor.");
592 }
593
594
595 // ============================================================================
596 // wxInotifyFileSystemWatcher implementation
597 // ============================================================================
598
599 wxInotifyFileSystemWatcher::wxInotifyFileSystemWatcher()
600 : wxFileSystemWatcherBase()
601 {
602 Init();
603 }
604
605 wxInotifyFileSystemWatcher::wxInotifyFileSystemWatcher(const wxFileName& path,
606 int events)
607 : wxFileSystemWatcherBase()
608 {
609 if (!Init())
610 {
611 if (m_service)
612 delete m_service;
613 return;
614 }
615
616 Add(path, events);
617 }
618
619 wxInotifyFileSystemWatcher::~wxInotifyFileSystemWatcher()
620 {
621 }
622
623 bool wxInotifyFileSystemWatcher::Init()
624 {
625 m_service = new wxFSWatcherImplUnix(this);
626 return m_service->Init();
627 }
628
629 void wxInotifyFileSystemWatcher::OnDirDeleted(const wxString& path)
630 {
631 if (!path.empty())
632 {
633 wxFSWatchInfoMap::iterator it = m_watches.find(path);
634 wxCHECK_RET(it != m_watches.end(),
635 wxString::Format("Path '%s' is not watched", path));
636
637 // path has been deleted, so we must forget it whatever its refcount
638 m_watches.erase(it);
639 }
640 }
641
642 #endif // wxHAS_INOTIFY
643
644 #endif // wxUSE_FSWATCHER