Fix typos
[TortoiseGit.git] / src / Utils / PathWatcher.h
blobc8c8a809ff61f6f59e90dd446c80ff014829a7c6
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2023 - TortoiseGit
4 // External Cache Copyright (C) 2007-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 "SmartHandle.h"
24 #define READ_DIR_CHANGE_BUFFER_SIZE 4096
25 #define MAX_CHANGED_PATHS 4000
27 using AutoLocker = CComCritSecLock<CComCriticalSection>;
29 /**
30 * \ingroup Utils
31 * Watches the file system for changes.
33 * When a CPathWatcher 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.
40 class CPathWatcher
42 public:
43 CPathWatcher();
44 ~CPathWatcher();
46 /**
47 * Adds a new path to be watched. The path \b must point to a directory.
48 * If the path is already watched because a parent of that path is already
49 * watched recursively, then the new path is just ignored and the method
50 * returns false.
52 bool AddPath(const CTGitPath& path);
53 /**
54 * Removes a path and all its children from the watched list.
56 bool RemovePathAndChildren(const CTGitPath& path);
58 /**
59 * Returns the number of recursively watched paths.
61 int GetNumberOfWatchedPaths() const { return watchedPaths.GetCount(); }
63 /**
64 * Stops the watching thread.
66 void Stop();
68 /**
69 * Returns the number of changed paths up to now.
71 int GetNumberOfChangedPaths() { AutoLocker lock(m_critSec); return m_changedPaths.GetCount(); }
73 /**
74 * Returns the list of paths which maybe got changed, i.e., for which
75 * a change notification was received.
77 CTGitPathList GetChangedPaths() { AutoLocker lock(m_critSec); return m_changedPaths; }
79 /**
80 * Clears the list of changed paths
82 void ClearChangedPaths() { AutoLocker lock(m_critSec); m_changedPaths.Clear(); }
84 /**
85 * If the limit of changed paths has been reached, returns true.
86 * \remark the limit is necessary to avoid memory problems.
88 bool LimitReached() const { return m_bLimitReached; }
90 private:
91 static unsigned int __stdcall ThreadEntry(void* pContext);
92 void WorkerThread();
94 void ClearInfoMap();
96 private:
97 CComAutoCriticalSection m_critSec;
98 CAutoGeneralHandle m_hThread;
99 CAutoGeneralHandle m_hCompPort;
100 volatile LONG m_bRunning = TRUE;
102 CTGitPathList watchedPaths; ///< list of watched paths.
103 CTGitPathList m_changedPaths; ///< list of paths which got changed
104 bool m_bLimitReached = false;
107 * Helper class: provides information about watched directories.
109 class CDirWatchInfo
111 private:
112 CDirWatchInfo(); // private & not implemented
113 CDirWatchInfo & operator=(const CDirWatchInfo & rhs);//so that they're aren't accidentally used. -- you'll get a linker error
114 public:
115 CDirWatchInfo(CAutoFile&& hDir, const CTGitPath& DirectoryName);
116 ~CDirWatchInfo();
118 protected:
119 public:
120 bool CloseDirectoryHandle();
122 CAutoFile m_hDir; ///< handle to the directory that we're watching
123 CTGitPath m_dirName; ///< the directory that we're watching
124 CHAR m_buffer[READ_DIR_CHANGE_BUFFER_SIZE]; ///< buffer for ReadDirectoryChangesW
125 OVERLAPPED m_overlapped{};
126 CString m_dirPath; ///< the directory name we're watching with a backslash at the end
129 std::map<HANDLE, CDirWatchInfo *> watchInfoMap;