Fix typos
[TortoiseGit.git] / src / TGitCache / DirectoryWatcher.h
blob240ad18da84c036b6ff14cdff2a02fd9a176e109
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2023 - TortoiseGit
4 // External Cache Copyright (C) 2005-2008, 2012 - TortoiseSVN
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #pragma once
21 #include "TGitPath.h"
22 #include "FolderCrawler.h"
23 #include "ShellCache.h"
24 #include "SmartHandle.h"
26 #define READ_DIR_CHANGE_BUFFER_SIZE 4096
28 /**
29 * \ingroup TGitCache
30 * Watches the file system for changes.
31 * When changes are detected, those changes are reported back to the CFolderCrawler
32 * which then can update the status cache.
34 * When a CDirectoryWatcher object is created, a new thread is started which
35 * waits for file system change notifications.
36 * To add folders to the list of watched folders, call \c AddPath().
38 * The folders are watched recursively. To prevent having too many folders watched,
39 * children of already watched folders are automatically removed from watching.
40 * This leads to having only the roots of file systems watched (e.g. C:\, D:\,...)
41 * after a few paths have been added to the watched list (at least, when the
42 * CGitStatusCache adds those paths).
44 class CDirectoryWatcher
46 public:
47 CDirectoryWatcher();
48 ~CDirectoryWatcher();
50 /**
51 * Adds a new path to be watched. The path \b must point to a directory.
52 * If the path is already watched because a parent of that path is already
53 * watched recursively, then the new path is just ignored and the method
54 * returns false.
56 bool AddPath(const CTGitPath& path, bool bCloseInfoMap = true);
57 /**
58 * Removes a path and all its children from the watched list.
60 bool RemovePathAndChildren(const CTGitPath& path);
61 /**
62 * Checks if a path is watched
64 bool IsPathWatched(const CTGitPath& path);
66 /**
67 * Returns the number of recursively watched paths.
69 int GetNumberOfWatchedPaths() {return watchedPaths.GetCount();}
71 /**
72 * Sets the CFolderCrawler object which the change notifications are sent to.
74 void SetFolderCrawler(CFolderCrawler * crawler);
76 /**
77 * Stops the watching thread.
79 void Stop();
81 CTGitPath CloseInfoMap(HANDLE hDir);
82 void ClearInfoMap();
83 bool CloseHandlesForPath(const CTGitPath& path);
85 private:
86 static unsigned int __stdcall ThreadEntry(void* pContext);
87 void WorkerThread();
89 void CloseWatchHandles();
91 void BlockPath(const CTGitPath& path);
93 // close handle (if open) and
94 // release all async I/O objects
96 void CloseCompletionPort();
98 // enqueue the info object for deletion as soon as the
99 // completion port is no longer used
101 class CDirWatchInfo;
102 void ScheduleForDeletion(CDirWatchInfo* info);
103 void CleanupWatchInfo();
105 private:
106 CComAutoCriticalSection m_critSec;
107 CAutoGeneralHandle m_hThread;
108 CAutoGeneralHandle m_hCompPort;
109 volatile LONG m_bRunning = TRUE;
110 volatile LONG m_bCleaned = FALSE;
112 CFolderCrawler* m_FolderCrawler = nullptr; ///< where the change reports go to
114 CTGitPathList watchedPaths; ///< list of watched paths.
116 CTGitPath blockedPath;
117 ULONGLONG blockTickCount = 0;
120 * \ingroup TGitCache
121 * Helper class: provides information about watched directories.
123 class CDirWatchInfo
125 private:
126 CDirWatchInfo() = delete;
127 CDirWatchInfo & operator=(const CDirWatchInfo & rhs) = delete; //so that they're aren't accidentally used. -- you'll get a linker error
128 public:
129 CDirWatchInfo(HANDLE hDir, const CTGitPath& DirectoryName);
130 ~CDirWatchInfo();
132 protected:
133 public:
134 bool CloseDirectoryHandle();
136 CAutoFile m_hDir; ///< handle to the directory that we're watching
137 CTGitPath m_DirName; ///< the directory that we're watching
138 CHAR m_Buffer[READ_DIR_CHANGE_BUFFER_SIZE]; ///< buffer for ReadDirectoryChangesW
139 OVERLAPPED m_Overlapped{};
140 CString m_DirPath; ///< the directory name we're watching with a backslash at the end
141 HDEVNOTIFY m_hDevNotify = nullptr; ///< Notification handle
144 using TInfoMap = std::map<HANDLE, CDirWatchInfo*>;
145 TInfoMap watchInfoMap;
147 // scheduled for deletion upon the next CleanupWatchInfo()
148 std::vector<CDirWatchInfo*> infoToDelete;