-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathWatcher.cpp
287 lines (248 loc) · 7.28 KB
/
Watcher.cpp
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
#include "../includes/win32/Watcher.h"
#include <sstream>
static
void stripNTPrefix(std::wstring &path) {
if (path.rfind(L"\\\\?\\UNC\\", 0) != std::wstring::npos) {
path.replace(0, 7, L"\\");
} else if (path.rfind(L"\\\\?\\", 0) != std::wstring::npos) {
path.erase(0, 4);
}
}
static
std::wstring getWStringFileName(LPWSTR cFileName, DWORD length) {
LPWSTR nullTerminatedFileName = new WCHAR[length + 1]();
memcpy(nullTerminatedFileName, cFileName, length);
std::wstring fileName = nullTerminatedFileName;
delete[] nullTerminatedFileName;
return fileName;
}
std::string Watcher::getUTF8Directory(std::wstring path) {
std::wstring::size_type found = path.rfind('\\');
std::wstringstream utf16DirectoryStream;
utf16DirectoryStream << mPath;
if (found != std::wstring::npos) {
utf16DirectoryStream
<< "\\"
<< path.substr(0, found);
}
std::wstring uft16DirectoryString = utf16DirectoryStream.str();
if (!mPathWasNtPrefixed) {
// If we were the ones that prefixed the path, we should strip it
// before returning it to the user
stripNTPrefix(uft16DirectoryString);
}
int utf8length = WideCharToMultiByte(
CP_UTF8,
0,
uft16DirectoryString.data(),
-1,
0,
0,
NULL,
NULL
);
char *utf8CString = new char[utf8length];
int failureToResolveToUTF8 = WideCharToMultiByte(
CP_UTF8,
0,
uft16DirectoryString.data(),
-1,
utf8CString,
utf8length,
NULL,
NULL
);
std::string utf8Directory = utf8CString;
delete[] utf8CString;
return utf8Directory;
}
static
std::string getUTF8FileName(std::wstring path) {
std::wstring::size_type found = path.rfind('\\');
if (found != std::wstring::npos) {
path = path.substr(found + 1);
}
int utf8length = WideCharToMultiByte(
CP_UTF8,
0,
path.data(),
-1,
0,
0,
NULL,
NULL
);
// TODO: failure cases for widechar conversion
char *utf8CString = new char[utf8length];
int failureToResolveToUTF8 = WideCharToMultiByte(
CP_UTF8,
0,
path.data(),
-1,
utf8CString,
utf8length,
NULL,
NULL
);
std::string utf8Directory = utf8CString;
delete[] utf8CString;
return utf8Directory;
}
Watcher::Watcher(std::shared_ptr<EventQueue> queue, HANDLE dirHandle, const std::wstring &path, bool pathWasNtPrefixed)
: mRunning(false),
mDirectoryHandle(dirHandle),
mQueue(queue),
mPath(path),
mPathWasNtPrefixed(pathWasNtPrefixed)
{
ZeroMemory(&mOverlapped, sizeof(OVERLAPPED));
mOverlapped.hEvent = this;
resizeBuffers(1024 * 1024);
start();
}
Watcher::~Watcher() {
stop();
}
void Watcher::resizeBuffers(std::size_t size) {
mReadBuffer.resize(size);
mWriteBuffer.resize(size);
}
void Watcher::run() {
while(mRunning) {
SleepEx(INFINITE, true);
}
}
bool Watcher::pollDirectoryChanges() {
DWORD bytes = 0;
if (!isRunning()) {
return false;
}
if (!ReadDirectoryChangesW(
mDirectoryHandle,
mWriteBuffer.data(),
static_cast<DWORD>(mWriteBuffer.size()),
TRUE, // recursive watching
FILE_NOTIFY_CHANGE_FILE_NAME
| FILE_NOTIFY_CHANGE_DIR_NAME
| FILE_NOTIFY_CHANGE_ATTRIBUTES
| FILE_NOTIFY_CHANGE_SIZE
| FILE_NOTIFY_CHANGE_LAST_WRITE
| FILE_NOTIFY_CHANGE_LAST_ACCESS
| FILE_NOTIFY_CHANGE_CREATION
| FILE_NOTIFY_CHANGE_SECURITY,
&bytes, // num bytes written
&mOverlapped,
[](DWORD errorCode, DWORD numBytes, LPOVERLAPPED overlapped) {
auto watcher = reinterpret_cast<Watcher*>(overlapped->hEvent);
watcher->eventCallback(errorCode);
}))
{
setError("Service shutdown unexpectedly");
return false;
}
return true;
}
void Watcher::eventCallback(DWORD errorCode) {
if (errorCode != ERROR_SUCCESS) {
if (errorCode == ERROR_NOTIFY_ENUM_DIR) {
setError("Buffer filled up and service needs a restart");
} else if (errorCode == ERROR_INVALID_PARAMETER) {
// resize the buffers because we're over the network, 64kb is the max buffer size for networked transmission
resizeBuffers(64 * 1024);
if (!pollDirectoryChanges()) {
setError("failed resizing buffers for network traffic");
}
} else {
setError("Service shutdown unexpectedly");
}
return;
}
std::swap(mWriteBuffer, mReadBuffer);
pollDirectoryChanges();
handleEvents();
}
void Watcher::handleEvents() {
BYTE *base = mReadBuffer.data();
while (true) {
PFILE_NOTIFY_INFORMATION info = (PFILE_NOTIFY_INFORMATION)base;
std::wstring fileName = getWStringFileName(info->FileName, info->FileNameLength);
switch (info->Action) {
case (FILE_ACTION_RENAMED_OLD_NAME):
if (info->NextEntryOffset != 0) {
base += info->NextEntryOffset;
info = (PFILE_NOTIFY_INFORMATION)base;
if (info->Action == FILE_ACTION_RENAMED_NEW_NAME) {
std::wstring fileNameNew = getWStringFileName(info->FileName, info->FileNameLength);
mQueue->enqueue(
RENAMED,
getUTF8Directory(fileName),
getUTF8FileName(fileName),
getUTF8Directory(fileName),
getUTF8FileName(fileNameNew)
);
} else {
mQueue->enqueue(DELETED, getUTF8Directory(fileName), getUTF8FileName(fileName));
}
} else {
mQueue->enqueue(DELETED, getUTF8Directory(fileName), getUTF8FileName(fileName));
}
break;
case FILE_ACTION_ADDED:
case FILE_ACTION_RENAMED_NEW_NAME: // in the case we just receive a new name and no old name in the buffer
mQueue->enqueue(CREATED, getUTF8Directory(fileName), getUTF8FileName(fileName));
break;
case FILE_ACTION_REMOVED:
mQueue->enqueue(DELETED, getUTF8Directory(fileName), getUTF8FileName(fileName));
break;
case FILE_ACTION_MODIFIED:
default:
mQueue->enqueue(MODIFIED, getUTF8Directory(fileName), getUTF8FileName(fileName));
};
if (info->NextEntryOffset == 0) {
break;
}
base += info->NextEntryOffset;
}
}
void Watcher::start() {
mRunner = std::thread([this] {
// mRunning is set to false in the d'tor
mRunning = true;
mIsRunningSemaphore.signal();
run();
});
if (!mRunner.joinable()) {
mRunning = false;
return;
}
if (!mIsRunningSemaphore.waitFor(std::chrono::seconds(10))) {
setError("Watcher is not started");
return;
}
QueueUserAPC([](__in ULONG_PTR self) {
auto watcher = reinterpret_cast<Watcher*>(self);
watcher->pollDirectoryChanges();
watcher->mHasStartedSemaphore.signal();
}, mRunner.native_handle(), (ULONG_PTR)this);
if (!mHasStartedSemaphore.waitFor(std::chrono::seconds(10))) {
setError("Watcher is not started");
}
}
void Watcher::stop() {
mRunning = false;
// schedule a NOOP APC to force the running loop in `Watcher::run()` to wake
// up, notice the changed `mRunning` and properly terminate the running loop
QueueUserAPC([](__in ULONG_PTR) {}, mRunner.native_handle(), (ULONG_PTR)this);
mRunner.join();
}
void Watcher::setError(const std::string &error) {
std::lock_guard<std::mutex> lock(mErrorMutex);
mError = error;
}
std::string Watcher::getError() const {
if (!isRunning()) {
return "Failed to start watcher";
}
std::lock_guard<std::mutex> lock(mErrorMutex);
return mError;
}