1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/unix/fswatcher_kqueue.cpp
3 // Purpose: kqueue-based wxFileSystemWatcher implementation
4 // Author: Bartosz Bekier
6 // Copyright: (c) 2009 Bartosz Bekier <bartosz.bekier@gmail.com>
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
19 #include "wx/fswatcher.h"
23 #include <sys/types.h>
24 #include <sys/event.h>
26 #include "wx/dynarray.h"
27 #include "wx/evtloop.h"
28 #include "wx/evtloopsrc.h"
30 #include "wx/private/fswatcher.h"
32 // ============================================================================
33 // wxFSWSourceHandler helper class
34 // ============================================================================
36 class wxFSWatcherImplKqueue
;
39 * Handler for handling i/o from inotify descriptor
41 class wxFSWSourceHandler
: public wxEventLoopSourceHandler
44 wxFSWSourceHandler(wxFSWatcherImplKqueue
* service
) :
48 virtual void OnReadWaiting();
49 virtual void OnWriteWaiting();
50 virtual void OnExceptionWaiting();
53 wxFSWatcherImplKqueue
* m_service
;
56 // ============================================================================
57 // wxFSWatcherImpl implementation & helper wxFSWSourceHandler implementation
58 // ============================================================================
61 * Helper class encapsulating inotify mechanism
63 class wxFSWatcherImplKqueue
: public wxFSWatcherImpl
66 wxFSWatcherImplKqueue(wxFileSystemWatcherBase
* watcher
) :
67 wxFSWatcherImpl(watcher
),
71 m_handler
= new wxFSWSourceHandler(this);
74 virtual ~wxFSWatcherImplKqueue()
76 // we close kqueue only if initialized before
87 wxCHECK_MSG( !IsOk(), false,
88 "Kqueue appears to be already initialized" );
90 wxEventLoopBase
*loop
= wxEventLoopBase::GetActive();
91 wxCHECK_MSG( loop
, false, "File system watcher needs an active loop" );
97 wxLogSysError(_("Unable to create kqueue instance"));
102 m_source
= loop
->AddSourceForFD(m_kfd
, m_handler
, wxEVENT_SOURCE_INPUT
);
104 return m_source
!= NULL
;
110 "Kqueue not initialized or invalid kqueue descriptor" );
112 if ( close(m_kfd
) != 0 )
114 wxLogSysError(_("Error closing kqueue instance"));
120 virtual bool DoAdd(wxSharedPtr
<wxFSWatchEntryKq
> watch
)
122 wxCHECK_MSG( IsOk(), false,
123 "Kqueue not initialized or invalid kqueue descriptor" );
126 int action
= EV_ADD
| EV_ENABLE
| EV_CLEAR
| EV_ERROR
;
127 int flags
= Watcher2NativeFlags(watch
->GetFlags());
128 EV_SET( &event
, watch
->GetFileDescriptor(), EVFILT_VNODE
, action
,
129 flags
, 0, watch
.get() );
131 // TODO more error conditions according to man
132 // TODO best deal with the error here
133 int ret
= kevent(m_kfd
, &event
, 1, NULL
, 0, NULL
);
136 wxLogSysError(_("Unable to add kqueue watch"));
143 virtual bool DoRemove(wxSharedPtr
<wxFSWatchEntryKq
> watch
)
145 wxCHECK_MSG( IsOk(), false,
146 "Kqueue not initialized or invalid kqueue descriptor" );
148 // TODO more error conditions according to man
149 // XXX closing file descriptor removes the watch. The logic resides in
150 // the watch which is not nice, but effective and simple
151 if ( !watch
->Close() )
153 wxLogSysError(_("Unable to remove kqueue watch"));
160 virtual bool RemoveAll()
162 wxFSWatchEntries::iterator it
= m_watches
.begin();
163 for ( ; it
!= m_watches
.end(); ++it
)
165 (void) DoRemove(it
->second
);
171 // return true if there was no error, false on error
174 wxCHECK_MSG( IsOk(), false,
175 "Kqueue not initialized or invalid kqueue descriptor" );
181 struct timespec timeout
= {0, 0};
182 int ret
= kevent(m_kfd
, NULL
, 0, &event
, 1, &timeout
);
185 wxLogSysError(_("Unable to get events from kqueue"));
193 // we have event, so process it
194 ProcessNativeEvent(event
);
198 // when ret>0 we still have events, when ret<=0 we return
199 wxFAIL_MSG( "Never reached" );
205 return m_source
!= NULL
;
209 // returns all new dirs/files present in the immediate level of the dir
210 // pointed by watch.GetPath(). "new" means created between the last time
211 // the state of watch was computed and now
212 void FindChanges(wxFSWatchEntryKq
& watch
,
213 wxArrayString
& changedFiles
,
214 wxArrayInt
& changedFlags
)
216 wxFSWatchEntryKq::wxDirState old
= watch
.GetLastState();
217 watch
.RefreshState();
218 wxFSWatchEntryKq::wxDirState curr
= watch
.GetLastState();
220 // iterate over old/curr file lists and compute changes
221 wxArrayString::iterator oit
= old
.files
.begin();
222 wxArrayString::iterator cit
= curr
.files
.begin();
223 for ( ; oit
!= old
.files
.end() && cit
!= curr
.files
.end(); )
230 else if ( *cit
<= *oit
)
232 changedFiles
.push_back(*cit
);
233 changedFlags
.push_back(wxFSW_EVENT_CREATE
);
236 else // ( *cit > *oit )
238 changedFiles
.push_back(*oit
);
239 changedFlags
.push_back(wxFSW_EVENT_DELETE
);
245 if ( oit
== old
.files
.end() )
247 for ( ; cit
!= curr
.files
.end(); ++cit
)
249 changedFiles
.push_back( *cit
);
250 changedFlags
.push_back(wxFSW_EVENT_CREATE
);
253 else if ( cit
== curr
.files
.end() )
255 for ( ; oit
!= old
.files
.end(); ++oit
)
257 changedFiles
.push_back( *oit
);
258 changedFlags
.push_back(wxFSW_EVENT_DELETE
);
262 wxASSERT( changedFiles
.size() == changedFlags
.size() );
265 wxLogTrace(wxTRACE_FSWATCHER
, "Changed files:");
266 wxArrayString::iterator it
= changedFiles
.begin();
267 wxArrayInt::iterator it2
= changedFlags
.begin();
268 for ( ; it
!= changedFiles
.end(); ++it
, ++it2
)
270 wxString action
= (*it2
== wxFSW_EVENT_CREATE
) ?
271 "created" : "deleted";
272 wxLogTrace(wxTRACE_FSWATCHER
, wxString::Format("File: '%s' %s",
278 void ProcessNativeEvent(const struct kevent
& e
)
280 wxASSERT_MSG(e
.udata
, "Null user data associated with kevent!");
282 wxLogTrace(wxTRACE_FSWATCHER
, "Event: ident=%d, filter=%d, flags=%u, "
283 "fflags=%u, data=%d, user_data=%p",
284 e
.ident
, e
.filter
, e
.flags
, e
.fflags
, e
.data
, e
.udata
);
287 wxFSWatchEntryKq
& w
= *(static_cast<wxFSWatchEntry
*>(e
.udata
));
288 int nflags
= e
.fflags
;
290 // clear ignored flags
291 nflags
&= ~NOTE_REVOKE
;
293 // TODO ignore events we didn't ask for + refactor this cascade ifs
297 // when monitoring dir, this means create/delete
298 const wxString basepath
= w
.GetPath();
299 if ( nflags
& NOTE_WRITE
&& wxDirExists(basepath
) )
301 // NOTE_LINK is set when the dir was created, but we
302 // don't care - we look for new names in directory
303 // regardless of type. Also, clear all this, because
304 // it cannot mean more by itself
305 nflags
&= ~(NOTE_WRITE
| NOTE_ATTRIB
| NOTE_LINK
);
307 wxArrayString changedFiles
;
308 wxArrayInt changedFlags
;
309 FindChanges(w
, changedFiles
, changedFlags
);
311 wxArrayString::iterator it
= changedFiles
.begin();
312 wxArrayInt::iterator changeType
= changedFlags
.begin();
313 for ( ; it
!= changedFiles
.end(); ++it
, ++changeType
)
315 const wxString fullpath
= w
.GetPath() +
316 wxFileName::GetPathSeparator() +
318 const wxFileName
path(wxDirExists(fullpath
)
319 ? wxFileName::DirName(fullpath
)
320 : wxFileName::FileName(fullpath
));
322 wxFileSystemWatcherEvent
event(*changeType
, path
, path
);
326 else if ( nflags
& NOTE_RENAME
)
328 // CHECK it'd be only possible to detect name if we had
329 // parent files listing which we could confront with now and
330 // still we couldn't be sure we have the right name...
331 nflags
&= ~NOTE_RENAME
;
332 wxFileSystemWatcherEvent
event(wxFSW_EVENT_RENAME
,
333 basepath
, wxFileName());
336 else if ( nflags
& NOTE_WRITE
|| nflags
& NOTE_EXTEND
)
338 nflags
&= ~(NOTE_WRITE
| NOTE_EXTEND
);
339 wxFileSystemWatcherEvent
event(wxFSW_EVENT_MODIFY
,
343 else if ( nflags
& NOTE_DELETE
)
345 nflags
&= ~(NOTE_DELETE
);
346 wxFileSystemWatcherEvent
event(wxFSW_EVENT_DELETE
,
350 else if ( nflags
& NOTE_ATTRIB
)
352 nflags
&= ~(NOTE_ATTRIB
);
353 wxFileSystemWatcherEvent
event(wxFSW_EVENT_ACCESS
,
358 // clear any flags that won't mean anything by themselves
359 nflags
&= ~(NOTE_LINK
);
363 void SendEvent(wxFileSystemWatcherEvent
& evt
)
365 m_watcher
->GetOwner()->ProcessEvent(evt
);
368 static int Watcher2NativeFlags(int WXUNUSED(flags
))
370 // TODO: it would be better to only subscribe to what we need
371 return NOTE_DELETE
| NOTE_WRITE
| NOTE_EXTEND
|
372 NOTE_ATTRIB
| NOTE_LINK
| NOTE_RENAME
|
376 wxFSWSourceHandler
* m_handler
; // handler for kqueue event source
377 wxEventLoopSource
* m_source
; // our event loop source
379 // descriptor created by kqueue()
384 // once we get signaled to read, actuall event reading occurs
385 void wxFSWSourceHandler::OnReadWaiting()
387 wxLogTrace(wxTRACE_FSWATCHER
, "--- OnReadWaiting ---");
388 m_service
->ReadEvents();
391 void wxFSWSourceHandler::OnWriteWaiting()
393 wxFAIL_MSG("We never write to kqueue descriptor.");
396 void wxFSWSourceHandler::OnExceptionWaiting()
398 wxFAIL_MSG("We never receive exceptions on kqueue descriptor.");
402 // ============================================================================
403 // wxKqueueFileSystemWatcher implementation
404 // ============================================================================
406 wxKqueueFileSystemWatcher::wxKqueueFileSystemWatcher()
407 : wxFileSystemWatcherBase()
412 wxKqueueFileSystemWatcher::wxKqueueFileSystemWatcher(const wxFileName
& path
,
414 : wxFileSystemWatcherBase()
425 wxKqueueFileSystemWatcher::~wxKqueueFileSystemWatcher()
429 bool wxKqueueFileSystemWatcher::Init()
431 m_service
= new wxFSWatcherImplKqueue(this);
432 return m_service
->Init();
435 #endif // wxHAS_KQUEUE
437 #endif // wxUSE_FSWATCHER