1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/unix/fswatcher_kqueue.cpp
3 // Purpose: kqueue-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/types.h>
25 #include <sys/event.h>
27 #include "wx/dynarray.h"
28 #include "wx/evtloop.h"
29 #include "wx/evtloopsrc.h"
31 #include "wx/private/fswatcher.h"
33 // ============================================================================
34 // wxFSWSourceHandler helper class
35 // ============================================================================
37 class wxFSWatcherImplKqueue
;
40 * Handler for handling i/o from inotify descriptor
42 class wxFSWSourceHandler
: public wxEventLoopSourceHandler
45 wxFSWSourceHandler(wxFSWatcherImplKqueue
* service
) :
49 virtual void OnReadWaiting();
50 virtual void OnWriteWaiting();
51 virtual void OnExceptionWaiting();
54 wxFSWatcherImplKqueue
* m_service
;
57 // ============================================================================
58 // wxFSWatcherImpl implementation & helper wxFSWSourceHandler implementation
59 // ============================================================================
62 * Helper class encapsulating inotify mechanism
64 class wxFSWatcherImplKqueue
: public wxFSWatcherImpl
67 wxFSWatcherImplKqueue(wxFileSystemWatcherBase
* watcher
) :
68 wxFSWatcherImpl(watcher
),
72 m_handler
= new wxFSWSourceHandler(this);
75 virtual ~wxFSWatcherImplKqueue()
77 // we close kqueue only if initialized before
88 wxCHECK_MSG( !IsOk(), false,
89 "Kqueue appears to be already initialized" );
91 wxEventLoopBase
*loop
= wxEventLoopBase::GetActive();
92 wxCHECK_MSG( loop
, false, "File system watcher needs an active loop" );
98 wxLogSysError(_("Unable to create kqueue instance"));
103 m_source
= loop
->AddSourceForFD(m_kfd
, m_handler
, wxEVENT_SOURCE_INPUT
);
105 return m_source
!= NULL
;
111 "Kqueue not initialized or invalid kqueue descriptor" );
113 if ( close(m_kfd
) != 0 )
115 wxLogSysError(_("Error closing kqueue instance"));
121 virtual bool DoAdd(wxSharedPtr
<wxFSWatchEntryKq
> watch
)
123 wxCHECK_MSG( IsOk(), false,
124 "Kqueue not initialized or invalid kqueue descriptor" );
127 int action
= EV_ADD
| EV_ENABLE
| EV_CLEAR
| EV_ERROR
;
128 int flags
= Watcher2NativeFlags(watch
->GetFlags());
129 EV_SET( &event
, watch
->GetFileDescriptor(), EVFILT_VNODE
, action
,
130 flags
, 0, watch
.get() );
132 // TODO more error conditions according to man
133 // TODO best deal with the error here
134 int ret
= kevent(m_kfd
, &event
, 1, NULL
, 0, NULL
);
137 wxLogSysError(_("Unable to add kqueue watch"));
144 virtual bool DoRemove(wxSharedPtr
<wxFSWatchEntryKq
> watch
)
146 wxCHECK_MSG( IsOk(), false,
147 "Kqueue not initialized or invalid kqueue descriptor" );
149 // TODO more error conditions according to man
150 // XXX closing file descriptor removes the watch. The logic resides in
151 // the watch which is not nice, but effective and simple
152 if ( !watch
->Close() )
154 wxLogSysError(_("Unable to remove kqueue watch"));
161 virtual bool RemoveAll()
163 wxFSWatchEntries::iterator it
= m_watches
.begin();
164 for ( ; it
!= m_watches
.end(); ++it
)
166 (void) DoRemove(it
->second
);
172 // return true if there was no error, false on error
175 wxCHECK_MSG( IsOk(), false,
176 "Kqueue not initialized or invalid kqueue descriptor" );
182 struct timespec timeout
= {0, 0};
183 int ret
= kevent(m_kfd
, NULL
, 0, &event
, 1, &timeout
);
186 wxLogSysError(_("Unable to get events from kqueue"));
194 // we have event, so process it
195 ProcessNativeEvent(event
);
199 // when ret>0 we still have events, when ret<=0 we return
200 wxFAIL_MSG( "Never reached" );
206 return m_source
!= NULL
;
210 // returns all new dirs/files present in the immediate level of the dir
211 // pointed by watch.GetPath(). "new" means created between the last time
212 // the state of watch was computed and now
213 void FindChanges(wxFSWatchEntryKq
& watch
,
214 wxArrayString
& changedFiles
,
215 wxArrayInt
& changedFlags
)
217 wxFSWatchEntryKq::wxDirState old
= watch
.GetLastState();
218 watch
.RefreshState();
219 wxFSWatchEntryKq::wxDirState curr
= watch
.GetLastState();
221 // iterate over old/curr file lists and compute changes
222 wxArrayString::iterator oit
= old
.files
.begin();
223 wxArrayString::iterator cit
= curr
.files
.begin();
224 for ( ; oit
!= old
.files
.end() && cit
!= curr
.files
.end(); )
231 else if ( *cit
<= *oit
)
233 changedFiles
.push_back(*cit
);
234 changedFlags
.push_back(wxFSW_EVENT_CREATE
);
237 else // ( *cit > *oit )
239 changedFiles
.push_back(*oit
);
240 changedFlags
.push_back(wxFSW_EVENT_DELETE
);
246 if ( oit
== old
.files
.end() )
248 for ( ; cit
!= curr
.files
.end(); ++cit
)
250 changedFiles
.push_back( *cit
);
251 changedFlags
.push_back(wxFSW_EVENT_CREATE
);
254 else if ( cit
== curr
.files
.end() )
256 for ( ; oit
!= old
.files
.end(); ++oit
)
258 changedFiles
.push_back( *oit
);
259 changedFlags
.push_back(wxFSW_EVENT_DELETE
);
263 wxASSERT( changedFiles
.size() == changedFlags
.size() );
266 wxLogTrace(wxTRACE_FSWATCHER
, "Changed files:");
267 wxArrayString::iterator it
= changedFiles
.begin();
268 wxArrayInt::iterator it2
= changedFlags
.begin();
269 for ( ; it
!= changedFiles
.end(); ++it
, ++it2
)
271 wxString action
= (*it2
== wxFSW_EVENT_CREATE
) ?
272 "created" : "deleted";
273 wxLogTrace(wxTRACE_FSWATCHER
, wxString::Format("File: '%s' %s",
279 void ProcessNativeEvent(const struct kevent
& e
)
281 wxASSERT_MSG(e
.udata
, "Null user data associated with kevent!");
283 wxLogTrace(wxTRACE_FSWATCHER
, "Event: ident=%d, filter=%d, flags=%u, "
284 "fflags=%u, data=%d, user_data=%p",
285 e
.ident
, e
.filter
, e
.flags
, e
.fflags
, e
.data
, e
.udata
);
288 wxFSWatchEntryKq
& w
= *(static_cast<wxFSWatchEntry
*>(e
.udata
));
289 int nflags
= e
.fflags
;
291 // clear ignored flags
292 nflags
&= ~NOTE_REVOKE
;
294 // TODO ignore events we didn't ask for + refactor this cascade ifs
298 // when monitoring dir, this means create/delete
299 const wxString basepath
= w
.GetPath();
300 if ( nflags
& NOTE_WRITE
&& wxDirExists(basepath
) )
302 // NOTE_LINK is set when the dir was created, but we
303 // don't care - we look for new names in directory
304 // regardless of type. Also, clear all this, because
305 // it cannot mean more by itself
306 nflags
&= ~(NOTE_WRITE
| NOTE_ATTRIB
| NOTE_LINK
);
308 wxArrayString changedFiles
;
309 wxArrayInt changedFlags
;
310 FindChanges(w
, changedFiles
, changedFlags
);
312 wxArrayString::iterator it
= changedFiles
.begin();
313 wxArrayInt::iterator changeType
= changedFlags
.begin();
314 for ( ; it
!= changedFiles
.end(); ++it
, ++changeType
)
316 const wxString fullpath
= w
.GetPath() +
317 wxFileName::GetPathSeparator() +
319 const wxFileName
path(wxDirExists(fullpath
)
320 ? wxFileName::DirName(fullpath
)
321 : wxFileName::FileName(fullpath
));
323 wxFileSystemWatcherEvent
event(*changeType
, path
, path
);
327 else if ( nflags
& NOTE_RENAME
)
329 // CHECK it'd be only possible to detect name if we had
330 // parent files listing which we could confront with now and
331 // still we couldn't be sure we have the right name...
332 nflags
&= ~NOTE_RENAME
;
333 wxFileSystemWatcherEvent
event(wxFSW_EVENT_RENAME
,
334 basepath
, wxFileName());
337 else if ( nflags
& NOTE_WRITE
|| nflags
& NOTE_EXTEND
)
339 nflags
&= ~(NOTE_WRITE
| NOTE_EXTEND
);
340 wxFileSystemWatcherEvent
event(wxFSW_EVENT_MODIFY
,
344 else if ( nflags
& NOTE_DELETE
)
346 nflags
&= ~(NOTE_DELETE
);
347 wxFileSystemWatcherEvent
event(wxFSW_EVENT_DELETE
,
351 else if ( nflags
& NOTE_ATTRIB
)
353 nflags
&= ~(NOTE_ATTRIB
);
354 wxFileSystemWatcherEvent
event(wxFSW_EVENT_ACCESS
,
359 // clear any flags that won't mean anything by themselves
360 nflags
&= ~(NOTE_LINK
);
364 void SendEvent(wxFileSystemWatcherEvent
& evt
)
366 m_watcher
->GetOwner()->ProcessEvent(evt
);
369 static int Watcher2NativeFlags(int WXUNUSED(flags
))
371 // TODO: it would be better to only subscribe to what we need
372 return NOTE_DELETE
| NOTE_WRITE
| NOTE_EXTEND
|
373 NOTE_ATTRIB
| NOTE_LINK
| NOTE_RENAME
|
377 wxFSWSourceHandler
* m_handler
; // handler for kqueue event source
378 wxEventLoopSource
* m_source
; // our event loop source
380 // descriptor created by kqueue()
385 // once we get signaled to read, actuall event reading occurs
386 void wxFSWSourceHandler::OnReadWaiting()
388 wxLogTrace(wxTRACE_FSWATCHER
, "--- OnReadWaiting ---");
389 m_service
->ReadEvents();
392 void wxFSWSourceHandler::OnWriteWaiting()
394 wxFAIL_MSG("We never write to kqueue descriptor.");
397 void wxFSWSourceHandler::OnExceptionWaiting()
399 wxFAIL_MSG("We never receive exceptions on kqueue descriptor.");
403 // ============================================================================
404 // wxKqueueFileSystemWatcher implementation
405 // ============================================================================
407 wxKqueueFileSystemWatcher::wxKqueueFileSystemWatcher()
408 : wxFileSystemWatcherBase()
413 wxKqueueFileSystemWatcher::wxKqueueFileSystemWatcher(const wxFileName
& path
,
415 : wxFileSystemWatcherBase()
426 wxKqueueFileSystemWatcher::~wxKqueueFileSystemWatcher()
430 bool wxKqueueFileSystemWatcher::Init()
432 m_service
= new wxFSWatcherImplKqueue(this);
433 return m_service
->Init();
436 #endif // wxHAS_KQUEUE
438 #endif // wxUSE_FSWATCHER