-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_watcher.h
488 lines (445 loc) · 15.4 KB
/
file_watcher.h
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#ifndef FILE_WATCHER_H
#define FILE_WATCHER_H
#define UNICODE
#include <windows.h>
// https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-readdirectorychangesw
// "ReadDirectoryChangesW fails with ERROR_NOACCESS when the buffer is not
// aligned on a DWORD boundary."
// https://docs.microsoft.com/en-us/cpp/cpp/align-cpp?view=msvc-160
typedef __declspec(align(4)) struct {
byte changes[2048];
HANDLE thread;
HANDLE exitEvent;
HWND window;
HANDLE folder;
OVERLAPPED watchIO;
LPCWSTR path;
int pathLength;
UINT changeMessage;
HANDLE file;
OVERLAPPED loadIO;
LPBYTE content;
UINT loadedMessage;
} WatcherData;
typedef struct {
HANDLE *file;
LPBYTE *buffer;
OVERLAPPED *io;
} LoadRequest;
typedef struct {
LPBYTE buffer;
int size;
HANDLE event;
} ParseRequest;
typedef enum {
WATCHER_SUCCESS = 0,
WATCHER_OPEN_FOLDER,
WATCHER_CREATE_EVENT,
WATCHER_WATCH,
WATCHER_CREATE_THREAD,
WATCHER_GET_CHANGES,
WATCHER_OPEN_FILE,
WATCHER_FILE_SIZE,
WATCHER_TOO_BIG,
WATCHER_NO_MEMORY,
WATCHER_READ_FILE,
WATCHER_RESULT_OF_READ,
WATCHER_CLOSE_FILE,
WATCHER_RESET_EVENT,
WATCHER_SEND_MESSAGE,
WATCHER_BAD_STATE,
WATCHER_THREAD_TIMEOUT,
WATCHER_CLOSE_FOLDER,
WATCHER_CLOSE_EVENT,
WATCHER_CLOSE_THREAD,
} WatcherError;
LPCWSTR watcherVerbs[] = {
L"fail",
L"open folder to watch for changes",
L"create an event object",
L"watch for file changes",
L"create file watcher thread",
L"retrieve file changes",
L"open file",
L"get file size",
L"fit file in 2.15 GB",
L"fit file in available memory",
L"read file",
L"get result of file read",
L"close file",
L"reset event object",
L"send message to main thread",
L"recover from bad state",
L"stop file watcher thread in time",
L"close folder",
L"clean up event object",
L"clean up file watcher thread",
};
#define WATCHER_VERB_LENGTH 33
// https://docs.microsoft.com/en-us/windows/win32/procthread/creating-threads
DWORD WINAPI fileWatcherThread(LPVOID param) {
WatcherData *data = (WatcherData*)param;
static const int EXIT_EVENT = 0, CHANGE_EVENT = 1, LOADED_EVENT = 2;
HANDLE events[3];
events[EXIT_EVENT] = data->exitEvent;
events[CHANGE_EVENT] = data->watchIO.hEvent;
events[LOADED_EVENT] = data->loadIO.hEvent;
static const int WATCH_STATE = 0, LOAD_STATE = 1, PARSE_STATE = 2;
int state = WATCH_STATE;
BOOL fileChanged = FALSE;
while (TRUE) {
BOOL shouldRead = FALSE;
DWORD waitResult = WaitForMultipleObjects(
2 + (state != WATCH_STATE), events, FALSE,
(state == WATCH_STATE && fileChanged) ? 30 : INFINITE
);
if (waitResult == WAIT_OBJECT_0 + EXIT_EVENT) {
return WATCHER_SUCCESS;
} else if (waitResult == WAIT_OBJECT_0 + CHANGE_EVENT) {
DWORD bufferLength;
BOOL overlappedResult = GetOverlappedResult(
data->folder, &data->watchIO, &bufferLength, FALSE
);
if (!overlappedResult) {
return WATCHER_GET_CHANGES;
}
FILE_NOTIFY_INFORMATION *change
= (FILE_NOTIFY_INFORMATION*)data->changes;
// https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-readdirectorychangesw#remarks
BOOL match = bufferLength == 0;
while ((LPBYTE)change < data->changes + bufferLength) {
int length = change->FileNameLength / sizeof(WCHAR);
int offset = data->pathLength - length;
if (
offset == 0 || data->path[offset - 1] == L'/'
|| data->path[offset - 1] == L'\\'
) {
match = TRUE;
for (int i = 0; i < length; i++) {
if (change->FileName[i] != data->path[offset + i]) {
match = FALSE;
break;
}
}
}
if (match || change->NextEntryOffset == 0) { break; }
change = (FILE_NOTIFY_INFORMATION*)(
(LPBYTE)change + change->NextEntryOffset
);
}
BOOL watchResult = ReadDirectoryChangesW(
data->folder,
data->changes,
sizeof(data->changes),
FALSE, // bWatchSubtree
FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_LAST_WRITE
| FILE_NOTIFY_CHANGE_SIZE,
NULL, // lpBytesReturned (synchronous calls only)
&data->watchIO,
NULL // lpCompletionRoutine
);
if (!watchResult) {
return WATCHER_WATCH;
}
shouldRead = match && state == WATCH_STATE && !fileChanged;
fileChanged = fileChanged || match;
} else if (
state == WATCH_STATE && fileChanged && waitResult == WAIT_TIMEOUT
) {
shouldRead = TRUE;
} else if (
state == LOAD_STATE && waitResult == WAIT_OBJECT_0 + LOADED_EVENT
&& data->file != INVALID_HANDLE_VALUE
) {
state = PARSE_STATE;
DWORD contentSize;
BOOL overlappedResult = GetOverlappedResult(
data->file, &data->loadIO, &contentSize, FALSE
);
if (!overlappedResult) {
CloseHandle(data->file);
data->file = INVALID_HANDLE_VALUE;
return WATCHER_RESULT_OF_READ;
}
if (!CloseHandle(data->file)) {
data->file = INVALID_HANDLE_VALUE;
return WATCHER_CLOSE_FILE;
}
data->file = INVALID_HANDLE_VALUE;
if (!ResetEvent(data->loadIO.hEvent)) {
return WATCHER_RESET_EVENT;
}
static ParseRequest request;
request.buffer = data->content;
request.size = contentSize;
request.event = data->loadIO.hEvent;
BOOL result = PostMessage(
data->window, data->loadedMessage, 0, (LPARAM)&request
);
if (!result) {
state = WATCH_STATE;
shouldRead = fileChanged;
}
} else if (
waitResult == WAIT_OBJECT_0 + LOADED_EVENT && (
state == PARSE_STATE || (
state == LOAD_STATE && data->file == INVALID_HANDLE_VALUE
)
)
) {
state = WATCH_STATE;
shouldRead = fileChanged;
if (!ResetEvent(data->loadIO.hEvent)) {
return WATCHER_RESET_EVENT;
}
} else {
return WATCHER_BAD_STATE;
}
if (shouldRead) {
// https://docs.microsoft.com/en-us/windows/win32/fileio/opening-a-file-for-reading-or-writing
data->file = CreateFile(
data->path,
GENERIC_READ,
FILE_SHARE_READ,
NULL, // default security settings
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
NULL // no attribute template
);
if (data->file == INVALID_HANDLE_VALUE) {
state = WATCH_STATE;
fileChanged = GetLastError() == ERROR_SHARING_VIOLATION;
if (
GetLastError() == ERROR_FILE_NOT_FOUND
|| GetLastError() == ERROR_PATH_NOT_FOUND
) {
static ParseRequest request;
request.buffer = NULL;
request.size = 0;
request.event = INVALID_HANDLE_VALUE;
PostMessage(
data->window, data->loadedMessage, 0, (LPARAM)&request
);
}
} else {
state = LOAD_STATE;
fileChanged = FALSE;
static LoadRequest request;
request.file = &data->file;
request.buffer = &data->content;
request.io = &data->loadIO;
BOOL result = PostMessage(
data->window, data->changeMessage, 0, (LPARAM)&request
);
if (!result) {
state = WATCH_STATE;
if (!CloseHandle(data->file)) {
data->file = INVALID_HANDLE_VALUE;
return WATCHER_CLOSE_FILE;
}
data->file = INVALID_HANDLE_VALUE;
}
}
}
}
}
WatcherError initializeWatcher(
WatcherData *data, LPWSTR path, UINT changeMessage, UINT loadedMessage
) {
data->thread = INVALID_HANDLE_VALUE;
data->exitEvent = INVALID_HANDLE_VALUE;
data->window = NULL;
data->folder = INVALID_HANDLE_VALUE;
data->watchIO.hEvent = INVALID_HANDLE_VALUE;
data->path = path;
// store pathLength so fileWatcherThread can avoid calling wcslen because
// standard library functions are not thread safe.
data->pathLength = wcslen(path);
data->changeMessage = changeMessage;
data->file = INVALID_HANDLE_VALUE;
data->loadIO.hEvent = INVALID_HANDLE_VALUE;
data->content = NULL;
data->loadedMessage = loadedMessage;
int i = data->pathLength - 1;
WCHAR oldChar = L'\0';
for (; i >= 0; i--) {
if (path[i] == L'\\' || path[i] == L'/') {
oldChar = path[i];
path[i] = L'\0';
break;
}
}
data->folder = CreateFile(
i >= 0 ? path : L".",
FILE_LIST_DIRECTORY,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
// FILE_FLAG_OVERLAPPED makes ReadDirectoryChangesW asynchronous
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED,
NULL
);
if (i >= 0) { path[i] = oldChar; }
if (data->folder == INVALID_HANDLE_VALUE) {
return WATCHER_OPEN_FOLDER;
}
data->watchIO.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (data->watchIO.hEvent == NULL) {
data->watchIO.hEvent = INVALID_HANDLE_VALUE;
CloseHandle(data->folder);
data->folder = INVALID_HANDLE_VALUE;
return WATCHER_CREATE_EVENT;
}
BOOL watchResult = ReadDirectoryChangesW(
data->folder,
data->changes,
sizeof(data->changes),
FALSE, // bWatchSubtree
FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_LAST_WRITE
| FILE_NOTIFY_CHANGE_SIZE,
NULL, // lpBytesReturned (synchronous calls only)
&data->watchIO,
NULL // lpCompletionRoutine
);
if (!watchResult) {
CloseHandle(data->watchIO.hEvent);
data->watchIO.hEvent = INVALID_HANDLE_VALUE;
CloseHandle(data->folder);
data->folder = INVALID_HANDLE_VALUE;
return WATCHER_WATCH;
}
return WATCHER_SUCCESS;
}
WatcherError watcherReadFile(LPCWSTR path, LPBYTE *buffer, DWORD *size) {
*size = 0;
// https://docs.microsoft.com/en-us/windows/win32/fileio/opening-a-file-for-reading-or-writing
HANDLE file = CreateFile(
path,
GENERIC_READ,
FILE_SHARE_READ,
NULL, // default security settings
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL // no attribute template
);
if (file == INVALID_HANDLE_VALUE) {
return WATCHER_OPEN_FILE;
}
LARGE_INTEGER fileSize;
if (!GetFileSizeEx(file, &fileSize)) {
CloseHandle(file); return WATCHER_FILE_SIZE;
} else if (fileSize.HighPart != 0 || fileSize.LowPart > (DWORD)INT_MAX) {
CloseHandle(file); return WATCHER_TOO_BIG;
}
*buffer = (LPBYTE)realloc(*buffer, fileSize.LowPart);
if (fileSize.LowPart > 0 && *buffer == NULL) {
CloseHandle(file); return WATCHER_NO_MEMORY;
}
BOOL readResult = ReadFile(
file, *buffer, fileSize.LowPart, size, NULL
);
if (!readResult) {
CloseHandle(file); return WATCHER_READ_FILE;
}
if (!CloseHandle(file)) {
return WATCHER_CLOSE_FILE;
}
return WATCHER_SUCCESS;
}
WatcherError startWatcher(WatcherData *data, HWND window) {
data->window = window;
data->exitEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (data->exitEvent == NULL) {
data->exitEvent = INVALID_HANDLE_VALUE;
return WATCHER_CREATE_EVENT;
}
data->loadIO.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (data->loadIO.hEvent == NULL) {
data->loadIO.hEvent = INVALID_HANDLE_VALUE;
return WATCHER_CREATE_EVENT;
}
DWORD threadID;
data->thread = CreateThread(
NULL, 0, fileWatcherThread, data, 0, &threadID
);
if (data->thread == NULL) {
data->thread = INVALID_HANDLE_VALUE;
return WATCHER_CREATE_THREAD;
}
return WATCHER_SUCCESS;
}
WatcherError readWatchedFile(LoadRequest *request) {
WatcherError error = WATCHER_BAD_STATE;
while (TRUE) {
LARGE_INTEGER fileSize;
if (!GetFileSizeEx(*request->file, &fileSize)) {
error = WATCHER_FILE_SIZE; break;
} else if (
fileSize.HighPart != 0 || fileSize.LowPart > (DWORD)INT_MAX
) {
error = WATCHER_TOO_BIG; break;
}
*request->buffer = (LPBYTE)realloc(
*request->buffer, fileSize.LowPart
);
if (fileSize.LowPart > 0 && *request->buffer == NULL) {
error = WATCHER_NO_MEMORY; break;
}
request->io->Offset = request->io->OffsetHigh = 0;
BOOL result = ReadFile(
*request->file,
*request->buffer,
fileSize.LowPart,
NULL,
request->io
);
if (!result && GetLastError() != ERROR_IO_PENDING) {
error = WATCHER_READ_FILE; break;
}
error = WATCHER_SUCCESS; break;
}
if (error != WATCHER_SUCCESS) {
CloseHandle(*request->file);
*request->file = INVALID_HANDLE_VALUE;
SetEvent(request->io->hEvent);
}
return error;
}
WatcherError stopWatcher(WatcherData *data) {
WatcherError error = WATCHER_SUCCESS;
BOOL closeHandleFailed = FALSE;
if (data->thread != INVALID_HANDLE_VALUE) {
SetEvent(data->exitEvent);
if (WaitForSingleObject(data->thread, 2000) != WAIT_OBJECT_0) {
error = WATCHER_THREAD_TIMEOUT;
} else {
GetExitCodeThread(data->thread, (LPDWORD)&error);
}
if (!CloseHandle(data->thread) && error == WATCHER_SUCCESS) {
error = WATCHER_CLOSE_THREAD;
}
}
if (
data->folder != INVALID_HANDLE_VALUE
&& !CloseHandle(data->folder) && error == WATCHER_SUCCESS
) { error = WATCHER_CLOSE_FOLDER; }
free(data->content);
if (
data->exitEvent != INVALID_HANDLE_VALUE
&& !CloseHandle(data->exitEvent) && error == WATCHER_SUCCESS
) { error = WATCHER_CLOSE_EVENT; }
if (
data->watchIO.hEvent != INVALID_HANDLE_VALUE
&& !CloseHandle(data->watchIO.hEvent) && error == WATCHER_SUCCESS
) { error = WATCHER_CLOSE_EVENT; }
if (
data->file != INVALID_HANDLE_VALUE
&& !CloseHandle(data->file) && error == WATCHER_SUCCESS
) { error = WATCHER_CLOSE_FILE; }
if (
data->loadIO.hEvent != INVALID_HANDLE_VALUE
&& !CloseHandle(data->loadIO.hEvent) && error == WATCHER_SUCCESS
) { error = WATCHER_CLOSE_EVENT; }
return error;
}
#endif