1 // TortoiseSVN - a Windows shell extension for easy version control
3 // External Cache Copyright (C) 2005-2008 - 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"
24 #define READ_DIR_CHANGE_BUFFER_SIZE 4096
28 * Watches the file system for changes.
29 * When changes are detected, those changes are reported back to the CFolderCrawler
30 * which then can update the status cache.
32 * When a CDirectoryWatcher object is created, a new thread is started which
33 * waits for file system change notifications.
34 * To add folders to the list of watched folders, call \c AddPath().
36 * The folders are watched recursively. To prevent having too many folders watched,
37 * children of already watched folders are automatically removed from watching.
38 * This leads to having only the roots of file systems watched (e.g. C:\, D:\,...)
39 * after a few paths have been added to the watched list (at least, when the
40 * CGitStatusCache adds those paths).
42 class CDirectoryWatcher
45 CDirectoryWatcher(void);
46 ~CDirectoryWatcher(void);
49 * Adds a new path to be watched. The path \b must point to a directory.
50 * If the path is already watched because a parent of that path is already
51 * watched recursively, then the new path is just ignored and the method
54 bool AddPath(const CTGitPath
& path
);
56 * Removes a path and all its children from the watched list.
58 bool RemovePathAndChildren(const CTGitPath
& path
);
60 * Checks if a path is watched
62 bool IsPathWatched(const CTGitPath
& path
);
65 * Returns the number of recursively watched paths.
67 int GetNumberOfWatchedPaths() {return watchedPaths
.GetCount();}
70 * Sets the CFolderCrawler object which the change notifications are sent to.
72 void SetFolderCrawler(CFolderCrawler
* crawler
);
75 * Stops the watching thread.
79 CTGitPath
CloseInfoMap(HDEVNOTIFY hdev
= INVALID_HANDLE_VALUE
);
80 bool CloseHandlesForPath(const CTGitPath
& path
);
83 static unsigned int __stdcall
ThreadEntry(void* pContext
);
88 void BlockPath(const CTGitPath
& path
);
91 CComAutoCriticalSection m_critSec
;
94 volatile LONG m_bRunning
;
96 CFolderCrawler
* m_FolderCrawler
; ///< where the change reports go to
98 CTGitPathList watchedPaths
; ///< list of watched paths.
100 CTGitPath blockedPath
;
101 DWORD blockTickCount
;
105 * Helper class: provides information about watched directories.
110 CDirWatchInfo(); // private & not implemented
111 CDirWatchInfo
& operator=(const CDirWatchInfo
& rhs
);//so that they're aren't accidentally used. -- you'll get a linker error
113 CDirWatchInfo(HANDLE hDir
, const CTGitPath
& DirectoryName
);
118 bool CloseDirectoryHandle();
120 HANDLE m_hDir
; ///< handle to the directory that we're watching
121 CTGitPath m_DirName
; ///< the directory that we're watching
122 CHAR m_Buffer
[READ_DIR_CHANGE_BUFFER_SIZE
]; ///< buffer for ReadDirectoryChangesW
123 DWORD m_dwBufLength
; ///< length or returned data from ReadDirectoryChangesW -- ignored?...
124 OVERLAPPED m_Overlapped
;
125 CString m_DirPath
; ///< the directory name we're watching with a backslash at the end
126 HDEVNOTIFY m_hDevNotify
; ///< Notification handle
129 std::map
<HANDLE
, CDirWatchInfo
*> watchInfoMap
;