1 // TortoiseGit - a Windows shell extension for easy version control
3 // External Cache Copyright (C) 2005-2008, 2012 - TortoiseSVN
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "FolderCrawler.h"
22 #include "ShellCache.h"
23 #include "SmartHandle.h"
25 #define READ_DIR_CHANGE_BUFFER_SIZE 4096
29 * Watches the file system for changes.
30 * When changes are detected, those changes are reported back to the CFolderCrawler
31 * which then can update the status cache.
33 * When a CDirectoryWatcher object is created, a new thread is started which
34 * waits for file system change notifications.
35 * To add folders to the list of watched folders, call \c AddPath().
37 * The folders are watched recursively. To prevent having too many folders watched,
38 * children of already watched folders are automatically removed from watching.
39 * This leads to having only the roots of file systems watched (e.g. C:\, D:\,...)
40 * after a few paths have been added to the watched list (at least, when the
41 * CGitStatusCache adds those paths).
43 class CDirectoryWatcher
46 CDirectoryWatcher(void);
47 ~CDirectoryWatcher(void);
50 * Adds a new path to be watched. The path \b must point to a directory.
51 * If the path is already watched because a parent of that path is already
52 * watched recursively, then the new path is just ignored and the method
55 bool AddPath(const CTGitPath
& path
, bool bCloseInfoMap
= true);
57 * Removes a path and all its children from the watched list.
59 bool RemovePathAndChildren(const CTGitPath
& path
);
61 * Checks if a path is watched
63 bool IsPathWatched(const CTGitPath
& path
);
66 * Returns the number of recursively watched paths.
68 int GetNumberOfWatchedPaths() {return watchedPaths
.GetCount();}
71 * Sets the CFolderCrawler object which the change notifications are sent to.
73 void SetFolderCrawler(CFolderCrawler
* crawler
);
76 * Stops the watching thread.
80 CTGitPath
CloseInfoMap(HANDLE hDir
);
82 bool CloseHandlesForPath(const CTGitPath
& path
);
85 static unsigned int __stdcall
ThreadEntry(void* pContext
);
88 void CloseWatchHandles();
90 void BlockPath(const CTGitPath
& path
);
92 // close handle (if open) and
93 // release all async I/O objects
95 void CloseCompletionPort();
97 // enqueue the info object for deletion as soon as the
98 // completion port is no longer used
101 void ScheduleForDeletion(CDirWatchInfo
* info
);
102 void CleanupWatchInfo();
105 CComAutoCriticalSection m_critSec
;
106 CAutoGeneralHandle m_hThread
;
107 CAutoGeneralHandle m_hCompPort
;
108 volatile LONG m_bRunning
;
109 volatile LONG m_bCleaned
;
111 CFolderCrawler
* m_FolderCrawler
; ///< where the change reports go to
113 CTGitPathList watchedPaths
; ///< list of watched paths.
115 CTGitPath blockedPath
;
116 DWORD blockTickCount
;
120 * Helper class: provides information about watched directories.
125 CDirWatchInfo(); // private & not implemented
126 CDirWatchInfo
& operator=(const CDirWatchInfo
& rhs
);//so that they're aren't accidentally used. -- you'll get a linker error
128 CDirWatchInfo(HANDLE hDir
, const CTGitPath
& DirectoryName
);
133 bool CloseDirectoryHandle();
135 CAutoFile m_hDir
; ///< handle to the directory that we're watching
136 CTGitPath m_DirName
; ///< the directory that we're watching
137 CHAR m_Buffer
[READ_DIR_CHANGE_BUFFER_SIZE
]; ///< buffer for ReadDirectoryChangesW
138 OVERLAPPED m_Overlapped
;
139 CString m_DirPath
; ///< the directory name we're watching with a backslash at the end
140 HDEVNOTIFY m_hDevNotify
; ///< Notification handle
143 typedef std::map
<HANDLE
, CDirWatchInfo
*> TInfoMap
;
144 TInfoMap watchInfoMap
;
148 // scheduled for deletion upon the next CleanupWatchInfo()
149 std::vector
<CDirWatchInfo
*> infoToDelete
;