-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathWatcher.cpp
More file actions
298 lines (270 loc) · 11.5 KB
/
Copy pathPathWatcher.cpp
File metadata and controls
298 lines (270 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// CryptSync - A folder sync tool with encryption
// Copyright (C) 2012-2014 - Stefan Kueng
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
#include "stdafx.h"
#include "PathWatcher.h"
#include "DebugOutput.h"
#include "CircularLog.h"
#include <Dbt.h>
#include <process.h>
CPathWatcher::CPathWatcher(void)
: m_hCompPort(NULL)
, m_bRunning(TRUE)
{
// enable the required privileges for this process
LPCTSTR arPrivelegeNames[] = { SE_BACKUP_NAME,
SE_RESTORE_NAME,
SE_CHANGE_NOTIFY_NAME
};
for (int i=0; i< _countof(arPrivelegeNames); ++i)
{
CAutoGeneralHandle hToken;
if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, hToken.GetPointer()))
{
TOKEN_PRIVILEGES tp = { 1 };
if (LookupPrivilegeValue(NULL, arPrivelegeNames[i], &tp.Privileges[0].Luid))
{
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(tp), NULL, NULL);
}
}
}
unsigned int threadId = 0;
m_hThread = (HANDLE)_beginthreadex(NULL,0,ThreadEntry,this,0,&threadId);
}
CPathWatcher::~CPathWatcher(void)
{
Stop();
CAutoWriteLock locker(m_guard);
ClearInfoMap();
}
void CPathWatcher::Stop()
{
InterlockedExchange(&m_bRunning, FALSE);
if (m_hCompPort)
{
PostQueuedCompletionStatus(m_hCompPort, 0, NULL, NULL);
m_hCompPort.CloseHandle();
}
if (m_hThread)
{
// the background thread sleeps for 200ms,
// so lets wait for it to finish for 1000 ms.
WaitForSingleObject(m_hThread, 1000);
m_hThread.CloseHandle();
}
}
bool CPathWatcher::RemovePath(const std::wstring& path)
{
CAutoWriteLock locker(m_guard);
CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": RemovePath for %s\n"), path.c_str());
bool bRet = (watchedPaths.erase(path) != 0);
m_hCompPort.CloseHandle();
return bRet;
}
bool CPathWatcher::AddPath(const std::wstring& path)
{
CAutoWriteLock locker(m_guard);
CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": AddPath for %s\n"), path.c_str());
watchedPaths.insert(path);
m_hCompPort.CloseHandle();
return true;
}
unsigned int CPathWatcher::ThreadEntry(void* pContext)
{
((CPathWatcher*)pContext)->WorkerThread();
return 0;
}
void CPathWatcher::WorkerThread()
{
DWORD numBytes;
CDirWatchInfo * pdi = NULL;
LPOVERLAPPED lpOverlapped;
const int bufferSize = MAX_PATH * 4;
TCHAR buf[bufferSize] = {0};
while (m_bRunning)
{
if (!watchedPaths.empty())
{
if (!m_hCompPort || !GetQueuedCompletionStatus(m_hCompPort,
&numBytes,
(PULONG_PTR) &pdi,
&lpOverlapped,
INFINITE))
{
// Error retrieving changes
// Clear the list of watched objects and recreate that list
if (!m_bRunning)
return;
{
CAutoWriteLock locker(m_guard);
ClearInfoMap();
}
DWORD lasterr = GetLastError();
if ((m_hCompPort)&&(lasterr!=ERROR_SUCCESS)&&(lasterr!=ERROR_INVALID_HANDLE))
{
m_hCompPort.CloseHandle();
}
CAutoReadLock locker(m_guard);
for (auto p = watchedPaths.cbegin(); p != watchedPaths.cend(); ++p)
{
CAutoFile hDir = CreateFile(p->c_str(),
FILE_LIST_DIRECTORY,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL, //security attributes
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS | //required privileges: SE_BACKUP_NAME and SE_RESTORE_NAME.
FILE_FLAG_OVERLAPPED,
NULL);
if (!hDir)
{
// this could happen if a watched folder has been removed/renamed
m_hCompPort.CloseHandle();
CAutoWriteLock locker(m_guard);
watchedPaths.erase(p);
break;
}
std::unique_ptr<CDirWatchInfo> pDirInfo (new CDirWatchInfo(hDir, p->c_str()));
hDir.Detach(); // the new CDirWatchInfo object owns the handle now
m_hCompPort = CreateIoCompletionPort(pDirInfo->m_hDir, m_hCompPort, (ULONG_PTR)pDirInfo.get(), 0);
if (m_hCompPort == NULL)
{
CAutoWriteLock locker(m_guard);
ClearInfoMap();
watchedPaths.erase(p);
break;
}
if (!ReadDirectoryChangesW(pDirInfo->m_hDir,
pDirInfo->m_Buffer,
READ_DIR_CHANGE_BUFFER_SIZE,
TRUE,
FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME | FILE_NOTIFY_CHANGE_LAST_WRITE,
&numBytes,// not used
&pDirInfo->m_Overlapped,
NULL)) //no completion routine!
{
CAutoWriteLock locker(m_guard);
ClearInfoMap();
watchedPaths.erase(p);
break;
}
CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": watching path %s\n"), p->c_str());
CAutoWriteLock locker(m_guard);
watchInfoMap[pDirInfo->m_hDir] = pDirInfo.get();
pDirInfo.release();
}
}
else
{
if (!m_bRunning)
return;
// NOTE: the longer this code takes to execute until ReadDirectoryChangesW
// is called again, the higher the chance that we miss some
// changes in the file system!
if (pdi)
{
if (numBytes == 0)
{
goto continuewatching;
}
PFILE_NOTIFY_INFORMATION pnotify = (PFILE_NOTIFY_INFORMATION)pdi->m_Buffer;
if ((ULONG_PTR)pnotify - (ULONG_PTR)pdi->m_Buffer > READ_DIR_CHANGE_BUFFER_SIZE)
goto continuewatching;
DWORD nOffset = pnotify->NextEntryOffset;
do
{
nOffset = pnotify->NextEntryOffset;
SecureZeroMemory(buf, bufferSize*sizeof(TCHAR));
wcsncpy_s(buf, bufferSize, pdi->m_DirPath.c_str(), bufferSize);
errno_t err = wcsncat_s(buf+pdi->m_DirPath.size(), bufferSize-pdi->m_DirPath.size(), pnotify->FileName, min(pnotify->FileNameLength/sizeof(WCHAR), bufferSize-pdi->m_DirPath.size()));
if (err == STRUNCATE)
{
pnotify = (PFILE_NOTIFY_INFORMATION)((LPBYTE)pnotify + nOffset);
continue;
}
buf[min(bufferSize-1, pdi->m_DirPath.size()+(pnotify->FileNameLength/sizeof(WCHAR)))] = 0;
pnotify = (PFILE_NOTIFY_INFORMATION)((LPBYTE)pnotify + nOffset);
CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": change notification for %s\n"), buf);
{
CAutoWriteLock locker(m_guard);
m_changedPaths.insert(std::wstring(buf));
}
if ((ULONG_PTR)pnotify - (ULONG_PTR)pdi->m_Buffer > READ_DIR_CHANGE_BUFFER_SIZE)
break;
} while (nOffset);
continuewatching:
SecureZeroMemory(pdi->m_Buffer, sizeof(pdi->m_Buffer));
SecureZeroMemory(&pdi->m_Overlapped, sizeof(pdi->m_Overlapped));
if (!ReadDirectoryChangesW(pdi->m_hDir,
pdi->m_Buffer,
READ_DIR_CHANGE_BUFFER_SIZE,
TRUE,
FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME | FILE_NOTIFY_CHANGE_LAST_WRITE,
&numBytes,// not used
&pdi->m_Overlapped,
NULL)) //no completion routine!
{
// Since the call to ReadDirectoryChangesW failed, just
// wait a while. We don't want to have this thread
// running using 100% CPU if something goes completely
// wrong.
Sleep(200);
}
}
}
}// if (watchedPaths.GetCount())
else
Sleep(200);
}// while (m_bRunning)
}
void CPathWatcher::ClearInfoMap()
{
if (!watchInfoMap.empty())
{
CAutoWriteLock locker(m_guard);
for (std::map<HANDLE, CDirWatchInfo *>::iterator I = watchInfoMap.begin(); I != watchInfoMap.end(); ++I)
{
CPathWatcher::CDirWatchInfo * info = I->second;
delete info;
info = NULL;
}
}
watchInfoMap.clear();
m_hCompPort.CloseHandle();
}
std::set<std::wstring> CPathWatcher::GetChangedPaths()
{
CAutoWriteLock locker(m_guard);
std::set<std::wstring> ret = m_changedPaths;
m_changedPaths.clear();
return ret;
}
CPathWatcher::CDirWatchInfo::CDirWatchInfo(HANDLE hDir, const std::wstring& DirectoryName)
: m_hDir(hDir)
, m_DirName(DirectoryName)
{
m_Buffer[0] = 0;
SecureZeroMemory(&m_Overlapped, sizeof(m_Overlapped));
m_DirPath = m_DirName.c_str();
if (m_DirPath.at(m_DirPath.size()-1) != '\\')
m_DirPath += _T("\\");
}
CPathWatcher::CDirWatchInfo::~CDirWatchInfo()
{
CloseDirectoryHandle();
}
bool CPathWatcher::CDirWatchInfo::CloseDirectoryHandle()
{
return m_hDir.CloseHandle();
}