1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/unix/fswatcher_inotify.cpp
3 // Purpose: inotify-based wxFileSystemWatcher implementation
4 // Author: Bartosz Bekier
7 // Copyright: (c) 2009 Bartosz Bekier <bartosz.bekier@gmail.com>
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
20 #include "wx/fswatcher.h"
24 #include <sys/inotify.h>
26 #include "wx/private/fswatcher.h"
28 // ============================================================================
29 // wxFSWatcherImpl implementation & helper wxFSWSourceHandler implementation
30 // ============================================================================
32 // inotify watch descriptor => wxFSWatchEntry* map
33 WX_DECLARE_HASH_MAP(int, wxFSWatchEntry
*, wxIntegerHash
, wxIntegerEqual
,
34 wxFSWatchEntryDescriptors
);
36 // inotify event cookie => inotify_event* map
37 WX_DECLARE_HASH_MAP(int, inotify_event
*, wxIntegerHash
, wxIntegerEqual
,
41 * Helper class encapsulating inotify mechanism
43 class wxFSWatcherImplUnix
: public wxFSWatcherImpl
46 wxFSWatcherImplUnix(wxFileSystemWatcherBase
* watcher
) :
47 wxFSWatcherImpl(watcher
),
51 m_handler
= new wxFSWSourceHandler(this);
54 ~wxFSWatcherImplUnix()
56 // we close inotify only if initialized before
67 wxCHECK_MSG( !IsOk(), false, "Inotify already initialized" );
69 wxEventLoopBase
*loop
= wxEventLoopBase::GetActive();
70 wxCHECK_MSG( loop
, false, "File system watcher needs an event loop" );
72 m_ifd
= inotify_init();
75 wxLogSysError( _("Unable to create inotify instance") );
79 m_source
= loop
->AddSourceForFD
83 wxEVENT_SOURCE_INPUT
| wxEVENT_SOURCE_EXCEPTION
86 return m_source
!= NULL
;
92 "Inotify not initialized or invalid inotify descriptor" );
96 if ( close(m_ifd
) != 0 )
98 wxLogSysError( _("Unable to close inotify instance") );
102 virtual bool DoAdd(wxSharedPtr
<wxFSWatchEntryUnix
> watch
)
104 wxCHECK_MSG( IsOk(), false,
105 "Inotify not initialized or invalid inotify descriptor" );
107 int wd
= DoAddInotify(watch
.get());
110 wxLogSysError( _("Unable to add inotify watch") );
114 wxFSWatchEntryDescriptors::value_type
val(wd
, watch
.get());
115 if (!m_watchMap
.insert(val
).second
)
117 wxFAIL_MSG( wxString::Format( "Path %s is already watched",
125 virtual bool DoRemove(wxSharedPtr
<wxFSWatchEntryUnix
> watch
)
127 wxCHECK_MSG( IsOk(), false,
128 "Inotify not initialized or invalid inotify descriptor" );
130 int ret
= DoRemoveInotify(watch
.get());
133 wxLogSysError( _("Unable to remove inotify watch") );
137 if (m_watchMap
.erase(watch
->GetWatchDescriptor()) != 1)
139 wxFAIL_MSG( wxString::Format("Path %s is not watched",
142 // Cache the wd in case any events arrive late
143 m_staleDescriptors
.Add(watch
->GetWatchDescriptor());
145 watch
->SetWatchDescriptor(-1);
149 virtual bool RemoveAll()
151 wxFSWatchEntries::iterator it
= m_watches
.begin();
152 for ( ; it
!= m_watches
.end(); ++it
)
154 (void) DoRemove(it
->second
);
162 wxCHECK_MSG( IsOk(), -1,
163 "Inotify not initialized or invalid inotify descriptor" );
166 // TODO differentiate depending on params
167 char buf
[128 * sizeof(inotify_event
)];
168 int left
= ReadEventsToBuf(buf
, sizeof(buf
));
172 // left > 0, we have events
175 while (left
> 0) // OPT checking 'memory' would suffice
178 inotify_event
* e
= (inotify_event
*)memory
;
180 // process one inotify_event
181 ProcessNativeEvent(*e
);
183 int offset
= sizeof(inotify_event
) + e
->len
;
188 // take care of unmatched renames
191 wxLogTrace(wxTRACE_FSWATCHER
, "We had %d native events", event_count
);
197 return m_source
!= NULL
;
201 int DoAddInotify(wxFSWatchEntry
* watch
)
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
);
210 int DoRemoveInotify(wxFSWatchEntry
* watch
)
212 return inotify_rm_watch(m_ifd
, watch
->GetWatchDescriptor());
215 void ProcessNativeEvent(const inotify_event
& inevt
)
217 wxLogTrace(wxTRACE_FSWATCHER
, InotifyEventToString(inevt
));
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
)
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
)
230 m_staleDescriptors
.RemoveAt(static_cast<size_t>(pos
));
231 wxLogTrace(wxTRACE_FSWATCHER
,
232 "Removed wd %i from the stale-wd cache", inevt
.wd
);
237 // get watch entry for this event
238 wxFSWatchEntryDescriptors::iterator it
= m_watchMap
.find(inevt
.wd
);
240 // wd will be -1 for IN_Q_OVERFLOW, which would trigger the wxFAIL_MSG
243 if (it
== m_watchMap
.end())
245 // It's not in the map; check if was recently removed from it.
246 if (m_staleDescriptors
.Index(inevt
.wd
) != wxNOT_FOUND
)
248 wxLogTrace(wxTRACE_FSWATCHER
,
249 "Got an event for stale wd %i", inevt
.wd
);
253 // In theory we shouldn't reach here. In practice, some
254 // events, e.g. IN_MODIFY, arrive just after the IN_IGNORED
255 // so their wd has already been discarded. Warn about them.
256 wxFileSystemWatcherEvent
262 _("Unexpected event for \"%s\": no "
263 "matching watch descriptor."),
264 inevt
.len
? inevt
.name
: ""
271 // In any case, don't process this event: it's either for an
272 // already removed entry, or for an unknown one.
277 int nativeFlags
= inevt
.mask
;
278 int flags
= Native2WatcherFlags(nativeFlags
);
280 // check out for error/warning condition
281 if (flags
& wxFSW_EVENT_WARNING
|| flags
& wxFSW_EVENT_ERROR
)
283 wxString errMsg
= GetErrorDescription(nativeFlags
);
284 wxFileSystemWatcherEvent
event(flags
, errMsg
);
291 // Although this is not supposed to happen, we seem to be getting
292 // occasional IN_ACCESS/IN_MODIFY events without valid watch value.
293 wxFileSystemWatcherEvent
299 _("Invalid inotify event for \"%s\""),
300 inevt
.len
? inevt
.name
: ""
307 wxFSWatchEntry
& watch
= *(it
->second
);
309 // Now IN_UNMOUNT. We must do so here, as it's not in the watch flags
310 if (nativeFlags
& IN_UNMOUNT
)
312 wxFileName path
= GetEventPath(watch
, inevt
);
313 wxFileSystemWatcherEvent
event(wxFSW_EVENT_UNMOUNT
, path
, path
);
316 // filter out ignored events and those not asked for.
317 // we never filter out warnings or exceptions
318 else if ((flags
== 0) || !(flags
& watch
.GetFlags()))
324 // We need do something here only if the original watch was recursive;
325 // we don't watch a child dir itself inside a non-tree watch.
326 // We watch only dirs explicitly, so we don't want file IN_CREATEs.
327 // Distinguish by whether nativeFlags contain IN_ISDIR
328 else if ((nativeFlags
& IN_CREATE
) &&
329 (watch
.GetType() == wxFSWPath_Tree
) && (inevt
.mask
& IN_ISDIR
))
331 wxFileName fn
= GetEventPath(watch
, inevt
);
332 // Though it's a dir, fn treats it as a file. So:
333 fn
.AssignDir(fn
.GetFullPath());
335 if (m_watcher
->AddAny(fn
, wxFSW_EVENT_ALL
,
336 wxFSWPath_Tree
, watch
.GetFilespec()))
338 // Tell the owner, in case it's interested
339 // If there's a filespec, assume he's not
340 if (watch
.GetFilespec().empty())
342 wxFileSystemWatcherEvent
event(flags
, fn
, fn
);
349 // We watch only dirs explicitly, so we don't want file IN_DELETEs.
350 // We obviously can't check using DirExists() as the object has been
351 // deleted; and nativeFlags here doesn't contain IN_ISDIR, even for
352 // a dir. Fortunately IN_DELETE_SELF doesn't happen for files. We need
353 // to do something here only inside a tree watch, or if it's the parent
354 // dir that's deleted. Otherwise let the parent dir cope
355 else if ((nativeFlags
& IN_DELETE_SELF
) &&
356 ((watch
.GetType() == wxFSWPath_Dir
) ||
357 (watch
.GetType() == wxFSWPath_Tree
)))
359 // We must remove the deleted directory from the map, so that
360 // DoRemoveInotify() isn't called on it in the future. Don't assert
361 // if the wd isn't found: repeated IN_DELETE_SELFs can occur
362 wxFileName fn
= GetEventPath(watch
, inevt
);
363 wxString
path(fn
.GetPathWithSep());
365 if (m_watchMap
.erase(inevt
.wd
) == 1)
367 // Delete from wxFileSystemWatcher
368 wxDynamicCast(m_watcher
, wxInotifyFileSystemWatcher
)->
371 // Now remove from our local list of watched items
372 wxFSWatchEntries::iterator wit
=
373 m_watches
.find(path
);
374 if (wit
!= m_watches
.end())
376 m_watches
.erase(wit
);
379 // Cache the wd in case any events arrive late
380 m_staleDescriptors
.Add(inevt
.wd
);
383 // Tell the owner, in case it's interested
384 // If there's a filespec, assume he's not
385 if (watch
.GetFilespec().empty())
387 wxFileSystemWatcherEvent
event(flags
, fn
, fn
);
393 else if (nativeFlags
& IN_MOVE
)
395 wxInotifyCookies::iterator it2
= m_cookies
.find(inevt
.cookie
);
396 if ( it2
== m_cookies
.end() )
398 int size
= sizeof(inevt
) + inevt
.len
;
399 inotify_event
* e
= (inotify_event
*) operator new (size
);
400 memcpy(e
, &inevt
, size
);
402 wxInotifyCookies::value_type
val(e
->cookie
, e
);
403 m_cookies
.insert(val
);
407 inotify_event
& oldinevt
= *(it2
->second
);
409 // Tell the owner, in case it's interested
410 // If there's a filespec, assume he's not
411 if ( watch
.GetFilespec().empty() )
413 wxFileSystemWatcherEvent
event(flags
);
414 if ( inevt
.mask
& IN_MOVED_FROM
)
416 event
.SetPath(GetEventPath(watch
, inevt
));
417 event
.SetNewPath(GetEventPath(watch
, oldinevt
));
421 event
.SetPath(GetEventPath(watch
, oldinevt
));
422 event
.SetNewPath(GetEventPath(watch
, inevt
));
427 m_cookies
.erase(it2
);
431 // every other kind of event
434 wxFileName path
= GetEventPath(watch
, inevt
);
435 // For files, check that it matches any filespec
436 if ( MatchesFilespec(path
, watch
.GetFilespec()) )
438 wxFileSystemWatcherEvent
event(flags
, path
, path
);
444 void ProcessRenames()
446 wxInotifyCookies::iterator it
= m_cookies
.begin();
447 while ( it
!= m_cookies
.end() )
449 inotify_event
& inevt
= *(it
->second
);
451 wxLogTrace(wxTRACE_FSWATCHER
, "Processing pending rename events");
452 wxLogTrace(wxTRACE_FSWATCHER
, InotifyEventToString(inevt
));
454 // get watch entry for this event
455 wxFSWatchEntryDescriptors::iterator wit
= m_watchMap
.find(inevt
.wd
);
456 if (wit
== m_watchMap
.end())
458 wxLogTrace(wxTRACE_FSWATCHER
,
459 "Watch descriptor not present in the watch map!");
463 // Tell the owner, in case it's interested
464 // If there's a filespec, assume he's not
465 wxFSWatchEntry
& watch
= *(wit
->second
);
466 if ( watch
.GetFilespec().empty() )
468 int flags
= Native2WatcherFlags(inevt
.mask
);
469 wxFileName path
= GetEventPath(watch
, inevt
);
471 wxFileSystemWatcherEvent
event(flags
, path
, path
);
479 it
= m_cookies
.begin();
483 void SendEvent(wxFileSystemWatcherEvent
& evt
)
485 wxLogTrace(wxTRACE_FSWATCHER
, evt
.ToString());
486 m_watcher
->GetOwner()->ProcessEvent(evt
);
489 int ReadEventsToBuf(char* buf
, int size
)
491 wxCHECK_MSG( IsOk(), false,
492 "Inotify not initialized or invalid inotify descriptor" );
494 memset(buf
, 0, size
);
495 ssize_t left
= read(m_ifd
, buf
, size
);
498 wxLogSysError(_("Unable to read from inotify descriptor"));
503 wxLogWarning(_("EOF while reading from inotify descriptor"));
510 static wxString
InotifyEventToString(const inotify_event
& inevt
)
512 wxString mask
= (inevt
.mask
& IN_ISDIR
) ?
513 wxString::Format("IS_DIR | %u", inevt
.mask
& ~IN_ISDIR
) :
514 wxString::Format("%u", inevt
.mask
);
515 const char* name
= "";
518 return wxString::Format("Event: wd=%d, mask=%s, cookie=%u, len=%u, "
519 "name=%s", inevt
.wd
, mask
, inevt
.cookie
,
523 static wxFileName
GetEventPath(const wxFSWatchEntry
& watch
,
524 const inotify_event
& inevt
)
526 // only when dir is watched, we have non-empty e.name
527 wxFileName path
= watch
.GetPath();
528 if (path
.IsDir() && inevt
.len
)
530 path
= wxFileName(path
.GetPath(), inevt
.name
);
535 static int Watcher2NativeFlags(int flags
)
537 // Start with the standard case of wanting all events
538 if (flags
== wxFSW_EVENT_ALL
)
540 return IN_ALL_EVENTS
;
543 static const int flag_mapping
[][2] = {
544 { wxFSW_EVENT_ACCESS
, IN_ACCESS
},
545 { wxFSW_EVENT_MODIFY
, IN_MODIFY
},
546 { wxFSW_EVENT_ATTRIB
, IN_ATTRIB
},
547 { wxFSW_EVENT_RENAME
, IN_MOVE
},
548 { wxFSW_EVENT_CREATE
, IN_CREATE
},
549 { wxFSW_EVENT_DELETE
, IN_DELETE
|IN_DELETE_SELF
|IN_MOVE_SELF
},
550 { wxFSW_EVENT_UNMOUNT
, IN_UNMOUNT
}
551 // wxFSW_EVENT_ERROR/WARNING make no sense here
554 int native_flags
= 0;
555 for ( unsigned int i
=0; i
< WXSIZEOF(flag_mapping
); ++i
)
557 if (flags
& flag_mapping
[i
][0])
558 native_flags
|= flag_mapping
[i
][1];
564 static int Native2WatcherFlags(int flags
)
566 static const int flag_mapping
[][2] = {
567 { IN_ACCESS
, wxFSW_EVENT_ACCESS
}, // generated during read!
568 { IN_MODIFY
, wxFSW_EVENT_MODIFY
},
569 { IN_ATTRIB
, wxFSW_EVENT_ATTRIB
},
570 { IN_CLOSE_WRITE
, 0 },
571 { IN_CLOSE_NOWRITE
, 0 },
573 { IN_MOVED_FROM
, wxFSW_EVENT_RENAME
},
574 { IN_MOVED_TO
, wxFSW_EVENT_RENAME
},
575 { IN_CREATE
, wxFSW_EVENT_CREATE
},
576 { IN_DELETE
, wxFSW_EVENT_DELETE
},
577 { IN_DELETE_SELF
, wxFSW_EVENT_DELETE
},
578 { IN_MOVE_SELF
, wxFSW_EVENT_DELETE
},
580 { IN_UNMOUNT
, wxFSW_EVENT_UNMOUNT
},
581 { IN_Q_OVERFLOW
, wxFSW_EVENT_WARNING
},
583 // ignored, because this is generated mainly by watcher::Remove()
588 for ( ; i
< WXSIZEOF(flag_mapping
); ++i
) {
589 // in this mapping multiple flags at once don't happen
590 if (flags
& flag_mapping
[i
][0])
591 return flag_mapping
[i
][1];
595 wxFAIL_MSG(wxString::Format("Unknown inotify event mask %u", flags
));
600 * Returns error description for specified inotify mask
602 static const wxString
GetErrorDescription(int flag
)
607 return _("Event queue overflowed");
611 wxFAIL_MSG(wxString::Format("Unknown inotify event mask %u", flag
));
612 return wxEmptyString
;
615 wxFSWSourceHandler
* m_handler
; // handler for inotify event source
616 wxFSWatchEntryDescriptors m_watchMap
; // inotify wd=>wxFSWatchEntry* map
617 wxArrayInt m_staleDescriptors
; // stores recently-removed watches
618 wxInotifyCookies m_cookies
; // map to track renames
619 wxEventLoopSource
* m_source
; // our event loop source
621 // file descriptor created by inotify_init()
626 // ============================================================================
627 // wxFSWSourceHandler implementation
628 // ============================================================================
630 // once we get signaled to read, actuall event reading occurs
631 void wxFSWSourceHandler::OnReadWaiting()
633 wxLogTrace(wxTRACE_FSWATCHER
, "--- OnReadWaiting ---");
634 m_service
->ReadEvents();
637 void wxFSWSourceHandler::OnWriteWaiting()
639 wxFAIL_MSG("We never write to inotify descriptor.");
642 void wxFSWSourceHandler::OnExceptionWaiting()
644 wxFAIL_MSG("We never receive exceptions on inotify descriptor.");
648 // ============================================================================
649 // wxInotifyFileSystemWatcher implementation
650 // ============================================================================
652 wxInotifyFileSystemWatcher::wxInotifyFileSystemWatcher()
653 : wxFileSystemWatcherBase()
658 wxInotifyFileSystemWatcher::wxInotifyFileSystemWatcher(const wxFileName
& path
,
660 : wxFileSystemWatcherBase()
672 wxInotifyFileSystemWatcher::~wxInotifyFileSystemWatcher()
676 bool wxInotifyFileSystemWatcher::Init()
678 m_service
= new wxFSWatcherImplUnix(this);
679 return m_service
->Init();
682 void wxInotifyFileSystemWatcher::OnDirDeleted(const wxString
& path
)
686 wxFSWatchInfoMap::iterator it
= m_watches
.find(path
);
687 wxCHECK_RET(it
!= m_watches
.end(),
688 wxString::Format("Path '%s' is not watched", path
));
690 // path has been deleted, so we must forget it whatever its refcount
695 #endif // wxHAS_INOTIFY
697 #endif // wxUSE_FSWATCHER