]>
git.saurik.com Git - wxWidgets.git/blob - src/common/fswatchercmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/fswatchercmn.cpp
3 // Purpose: wxMswFileSystemWatcher
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"
21 #include "wx/private/fswatcher.h"
23 // ============================================================================
25 // ============================================================================
27 wxDEFINE_EVENT(wxEVT_FSWATCHER
, wxFileSystemWatcherEvent
);
29 static wxString
GetFSWEventChangeTypeName(int type
)
33 case wxFSW_EVENT_CREATE
:
35 case wxFSW_EVENT_DELETE
:
37 case wxFSW_EVENT_RENAME
:
39 case wxFSW_EVENT_MODIFY
:
41 case wxFSW_EVENT_ACCESS
:
43 case wxFSW_EVENT_ATTRIB
: // Currently this is wxGTK-only
46 case wxFSW_EVENT_UNMOUNT
: // Currently this is wxGTK-only
49 case wxFSW_EVENT_WARNING
:
51 case wxFSW_EVENT_ERROR
:
55 // should never be reached!
56 wxFAIL_MSG("Unknown change type");
57 return "INVALID_TYPE";
61 // ============================================================================
62 // wxFileSystemWatcherEvent implementation
63 // ============================================================================
65 wxString
wxFileSystemWatcherEvent::ToString() const
67 return wxString::Format("FSW_EVT type=%d (%s) path='%s'", m_changeType
,
68 GetFSWEventChangeTypeName(m_changeType
), GetPath().GetFullPath());
72 // ============================================================================
73 // wxFileSystemWatcherEvent implementation
74 // ============================================================================
76 wxFileSystemWatcherBase::wxFileSystemWatcherBase() :
77 m_service(0), m_owner(this)
81 wxFileSystemWatcherBase::~wxFileSystemWatcherBase()
90 bool wxFileSystemWatcherBase::Add(const wxFileName
& path
, int events
)
92 wxFSWPathType type
= wxFSWPath_None
;
93 if ( path
.FileExists() )
95 type
= wxFSWPath_File
;
97 else if ( path
.DirExists() )
103 // Don't overreact to being passed a non-existent item. It may have
104 // only just been deleted, in which case doing nothing is correct
105 wxLogTrace(wxTRACE_FSWATCHER
,
106 "Can't monitor non-existent path \"%s\" for changes.",
111 return AddAny(path
, events
, type
);
115 wxFileSystemWatcherBase::AddAny(const wxFileName
& path
,
118 const wxString
& filespec
)
120 wxString canonical
= GetCanonicalPath(path
);
121 if (canonical
.IsEmpty())
124 // adding a path in a platform specific way
125 wxFSWatchInfo
watch(canonical
, events
, type
, filespec
);
126 if ( !m_service
->Add(watch
) )
129 // on success, either add path to our 'watch-list'
130 // or, if already watched, inc the refcount. This may happen if
131 // a dir is Add()ed, then later AddTree() is called on a parent dir
132 wxFSWatchInfoMap::iterator it
= m_watches
.find(canonical
);
133 if ( it
== m_watches
.end() )
135 wxFSWatchInfoMap::value_type
val(canonical
, watch
);
136 m_watches
.insert(val
);
140 wxFSWatchInfo
& watch
= it
->second
;
141 int count
= watch
.IncRef();
142 wxLogTrace(wxTRACE_FSWATCHER
,
143 "'%s' is now watched %d times", canonical
, count
);
148 bool wxFileSystemWatcherBase::Remove(const wxFileName
& path
)
150 // args validation & consistency checks
151 wxString canonical
= GetCanonicalPath(path
);
152 if (canonical
.IsEmpty())
155 wxFSWatchInfoMap::iterator it
= m_watches
.find(canonical
);
156 wxCHECK_MSG(it
!= m_watches
.end(), false,
157 wxString::Format("Path '%s' is not watched", canonical
));
159 // Decrement the watch's refcount and remove from watch-list if 0
161 wxFSWatchInfo
& watch
= it
->second
;
162 if ( !watch
.DecRef() )
164 // remove in a platform specific way
165 ret
= m_service
->Remove(watch
);
172 bool wxFileSystemWatcherBase::AddTree(const wxFileName
& path
, int events
,
173 const wxString
& filespec
)
175 if (!path
.DirExists())
178 // OPT could be optimised if we stored information about relationships
180 class AddTraverser
: public wxDirTraverser
183 AddTraverser(wxFileSystemWatcherBase
* watcher
, int events
,
184 const wxString
& filespec
) :
185 m_watcher(watcher
), m_events(events
), m_filespec(filespec
)
189 virtual wxDirTraverseResult
OnFile(const wxString
& WXUNUSED(filename
))
191 // There is no need to watch individual files as we watch the
192 // parent directory which will notify us about any changes in them.
193 return wxDIR_CONTINUE
;
196 virtual wxDirTraverseResult
OnDir(const wxString
& dirname
)
198 if ( m_watcher
->AddAny(wxFileName::DirName(dirname
),
199 m_events
, wxFSWPath_Tree
, m_filespec
) )
201 wxLogTrace(wxTRACE_FSWATCHER
,
202 "--- AddTree adding directory '%s' ---", dirname
);
204 return wxDIR_CONTINUE
;
208 wxFileSystemWatcherBase
* m_watcher
;
213 wxDir
dir(path
.GetFullPath());
214 // Prevent asserts or infinite loops in trees containing symlinks
215 int flags
= wxDIR_DIRS
;
216 if ( !path
.ShouldFollowLink() )
218 flags
|= wxDIR_NO_FOLLOW
;
220 AddTraverser
traverser(this, events
, filespec
);
221 dir
.Traverse(traverser
, filespec
, flags
);
223 // Add the path itself explicitly as Traverse() doesn't return it.
224 AddAny(path
.GetPathWithSep(), events
, wxFSWPath_Tree
, filespec
);
229 bool wxFileSystemWatcherBase::RemoveTree(const wxFileName
& path
)
231 if (!path
.DirExists())
234 // OPT could be optimised if we stored information about relationships
236 class RemoveTraverser
: public wxDirTraverser
239 RemoveTraverser(wxFileSystemWatcherBase
* watcher
,
240 const wxString
& filespec
) :
241 m_watcher(watcher
), m_filespec(filespec
)
245 virtual wxDirTraverseResult
OnFile(const wxString
& WXUNUSED(filename
))
247 // We never watch the individual files when watching the tree, so
248 // nothing to do here.
249 return wxDIR_CONTINUE
;
252 virtual wxDirTraverseResult
OnDir(const wxString
& dirname
)
254 m_watcher
->Remove(wxFileName::DirName(dirname
));
255 return wxDIR_CONTINUE
;
259 wxFileSystemWatcherBase
* m_watcher
;
263 // If AddTree() used a filespec, we must use the same one
264 wxString canonical
= GetCanonicalPath(path
);
265 wxFSWatchInfoMap::iterator it
= m_watches
.find(canonical
);
266 wxCHECK_MSG( it
!= m_watches
.end(), false,
267 wxString::Format("Path '%s' is not watched", canonical
) );
268 wxFSWatchInfo watch
= it
->second
;
269 const wxString filespec
= watch
.GetFilespec();
271 #if defined(__WINDOWS__)
272 // When there's no filespec, the wxMSW AddTree() would have set a watch
273 // on only the passed 'path'. We must therefore remove only this
274 if (filespec
.empty())
278 // Otherwise fall through to the generic implementation
279 #endif // __WINDOWS__
281 wxDir
dir(path
.GetFullPath());
282 // AddTree() might have used the wxDIR_NO_FOLLOW to prevent asserts or
283 // infinite loops in trees containing symlinks. We need to do the same
284 // or we'll try to remove unwatched items. Let's hope the caller used
285 // the same ShouldFollowLink() setting as in AddTree()...
286 int flags
= wxDIR_DIRS
;
287 if ( !path
.ShouldFollowLink() )
289 flags
|= wxDIR_NO_FOLLOW
;
291 RemoveTraverser
traverser(this, filespec
);
292 dir
.Traverse(traverser
, filespec
, flags
);
294 // As in AddTree() above, handle the path itself explicitly.
300 bool wxFileSystemWatcherBase::RemoveAll()
302 m_service
->RemoveAll();
307 int wxFileSystemWatcherBase::GetWatchedPathsCount() const
309 return m_watches
.size();
312 int wxFileSystemWatcherBase::GetWatchedPaths(wxArrayString
* paths
) const
314 wxCHECK_MSG( paths
!= NULL
, -1, "Null array passed to retrieve paths");
316 wxFSWatchInfoMap::const_iterator it
= m_watches
.begin();
317 for ( ; it
!= m_watches
.end(); ++it
)
319 paths
->push_back(it
->first
);
322 return m_watches
.size();
325 #endif // wxUSE_FSWATCHER