*.js files: keep the style consistent
[TortoiseGit.git] / src / Utils / PathWatcher.h
blobdb32d7b7e0ee457068722f1e769c4a5cc38986f0
1 // TortoiseGit - a Windows shell extension for easy version control
3 // External Cache Copyright (C) 2007-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.
19 #pragma once
20 #include "TGitPath.h"
21 #include "SmartHandle.h"
23 #define READ_DIR_CHANGE_BUFFER_SIZE 4096
24 #define MAX_CHANGED_PATHS 4000
26 typedef CComCritSecLock<CComCriticalSection> AutoLocker;
28 /**
29 * \ingroup Utils
30 * Watches the file system for changes.
32 * When a CPathWatcher 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.
39 class CPathWatcher
41 public:
42 CPathWatcher(void);
43 ~CPathWatcher(void);
45 /**
46 * Adds a new path to be watched. The path \b must point to a directory.
47 * If the path is already watched because a parent of that path is already
48 * watched recursively, then the new path is just ignored and the method
49 * returns false.
51 bool AddPath(const CTGitPath& path);
52 /**
53 * Removes a path and all its children from the watched list.
55 bool RemovePathAndChildren(const CTGitPath& path);
57 /**
58 * Returns the number of recursively watched paths.
60 int GetNumberOfWatchedPaths() {return watchedPaths.GetCount();}
62 /**
63 * Stops the watching thread.
65 void Stop();
67 /**
68 * Returns the number of changed paths up to now.
70 int GetNumberOfChangedPaths() { AutoLocker lock(m_critSec); return m_changedPaths.GetCount(); }
72 /**
73 * Returns the list of paths which maybe got changed, i.e., for which
74 * a change notification was received.
76 CTGitPathList GetChangedPaths() { AutoLocker lock(m_critSec); return m_changedPaths; }
78 /**
79 * Clears the list of changed paths
81 void ClearChangedPaths() { AutoLocker lock(m_critSec); m_changedPaths.Clear(); }
83 /**
84 * If the limit of changed paths has been reached, returns true.
85 * \remark the limit is necessary to avoid memory problems.
87 bool LimitReached() const { return m_bLimitReached; }
89 private:
90 static unsigned int __stdcall ThreadEntry(void* pContext);
91 void WorkerThread();
93 void ClearInfoMap();
95 private:
96 CComAutoCriticalSection m_critSec;
97 CAutoGeneralHandle m_hThread;
98 CAutoGeneralHandle m_hCompPort;
99 volatile LONG m_bRunning;
101 CTGitPathList watchedPaths; ///< list of watched paths.
102 CTGitPathList m_changedPaths; ///< list of paths which got changed
103 bool m_bLimitReached;
106 * Helper class: provides information about watched directories.
108 class CDirWatchInfo
110 private:
111 CDirWatchInfo(); // private & not implemented
112 CDirWatchInfo & operator=(const CDirWatchInfo & rhs);//so that they're aren't accidentally used. -- you'll get a linker error
113 public:
114 CDirWatchInfo(HANDLE hDir, const CTGitPath& DirectoryName);
115 ~CDirWatchInfo();
117 protected:
118 public:
119 bool CloseDirectoryHandle();
121 CAutoFile m_hDir; ///< handle to the directory that we're watching
122 CTGitPath m_DirName; ///< the directory that we're watching
123 CHAR m_Buffer[READ_DIR_CHANGE_BUFFER_SIZE]; ///< buffer for ReadDirectoryChangesW
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;
131 HDEVNOTIFY m_hdev;