-
-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathFileSystemWatcher.cpp
More file actions
340 lines (276 loc) · 9.4 KB
/
FileSystemWatcher.cpp
File metadata and controls
340 lines (276 loc) · 9.4 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
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
/*==============================================================================
Copyright 2018 by Roland Rabien
For more information visit www.rabiensoftware.com
==============================================================================*/
#include <juce_core/juce_core.h>
#include <juce_events/juce_events.h>
using namespace juce;
#include "FileSystemWatcher.h"
#ifdef _WIN32
#include <Windows.h>
#include <ctime>
#else
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <semaphore.h>
#include <ctime>
#endif
#if defined(__linux__) || JUCE_BSD
#include <sys/inotify.h>
#include <limits.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <poll.h>
#endif
#ifdef JUCE_LINUX
#define BUF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1))
class FileSystemWatcher::Impl : public Thread,
private AsyncUpdater
{
public:
struct Event
{
Event () {}
Event (Event const& other) = default;
Event (Event&& other) = default;
File file;
FileSystemEvent fsEvent;
bool operator== (const Event& other) const
{
return file == other.file && fsEvent == other.fsEvent;
}
};
Impl (FileSystemWatcher& o, File f)
: Thread ("FileSystemWatcher::Impl"), owner (o), folder (f)
{
fd = inotify_init();
wd = inotify_add_watch (fd,
folder.getFullPathName().toRawUTF8(),
IN_ATTRIB | IN_CREATE | IN_DELETE | IN_DELETE_SELF |
IN_MODIFY | IN_MOVE_SELF | IN_MOVED_TO | IN_MOVED_FROM);
startThread();
}
~Impl()
{
shouldQuit = true;
signalThreadShouldExit();
inotify_rm_watch (fd, wd);
close (fd);
waitForThreadToExit (1000);
}
void run() override
{
char buf[BUF_LEN];
const struct inotify_event* iNotifyEvent;
char* ptr;
while (!shouldQuit)
{
struct pollfd pfd;
pfd.fd = fd;
pfd.events = POLLIN;
// Poll with 100ms timeout
int pollResult = poll(&pfd, 1, 100);
if (threadShouldExit()) break; // Check exit condition regularly
if (pollResult > 0 && (pfd.revents & POLLIN)) {
int numRead = read(fd, buf, BUF_LEN);
if (numRead <= 0 || threadShouldExit())
break;
for (ptr = buf; ptr < buf + numRead; ptr += sizeof(struct inotify_event) + iNotifyEvent->len)
{
iNotifyEvent = (const struct inotify_event*)ptr;
Event e;
e.file = File {folder.getFullPathName() + '/' + iNotifyEvent->name};
if (iNotifyEvent->mask & IN_CREATE) e.fsEvent = FileSystemEvent::fileCreated;
else if (iNotifyEvent->mask & IN_CLOSE_WRITE) e.fsEvent = FileSystemEvent::fileUpdated;
else if (iNotifyEvent->mask & IN_MOVED_FROM) e.fsEvent = FileSystemEvent::fileRenamedOldName;
else if (iNotifyEvent->mask & IN_MOVED_TO) e.fsEvent = FileSystemEvent::fileRenamedNewName;
else if (iNotifyEvent->mask & IN_DELETE) e.fsEvent = FileSystemEvent::fileDeleted;
ScopedLock sl(lock);
bool duplicateEvent = false;
for (auto existing : events)
{
if (e == existing)
{
duplicateEvent = true;
break;
}
}
if (! duplicateEvent)
events.add (std::move (e));
}
ScopedLock sl (lock);
if (events.size() > 0)
triggerAsyncUpdate();
}
}
}
void handleAsyncUpdate() override
{
if(shouldQuit) return;
ScopedLock sl (lock);
for (auto& e : events)
owner.fileChanged (e.file, e.fsEvent);
events.clear();
}
std::atomic<bool> shouldQuit = false;
FileSystemWatcher& owner;
File folder;
CriticalSection lock;
SmallArray<Event> events;
int fd;
int wd;
};
#elif JUCE_WINDOWS
class FileSystemWatcher::Impl : private AsyncUpdater,
private Thread
{
public:
struct Event
{
File file;
FileSystemEvent fsEvent;
bool operator== (const Event& other) const
{
return file == other.file && fsEvent == other.fsEvent;
}
};
Impl (FileSystemWatcher& o, File f)
: Thread ("FileSystemWatcher::Impl"), owner (o), folder (f)
{
WCHAR path[_MAX_PATH] = {0};
wcsncpy_s (path, folder.getFullPathName().toWideCharPointer(), _MAX_PATH - 1);
folderHandle = CreateFileW (path, FILE_LIST_DIRECTORY, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (folderHandle != INVALID_HANDLE_VALUE)
startThread ();
}
~Impl() override
{
if (isThreadRunning())
{
signalThreadShouldExit();
CancelIoEx (folderHandle, nullptr);
stopThread (1000);
}
CloseHandle (folderHandle);
}
void run() override
{
const int heapSize = 16 * 1024;
uint8_t buffer[heapSize];
DWORD bytesOut = 0;
while (! threadShouldExit())
{
memset (buffer, 0, heapSize);
BOOL success = ReadDirectoryChangesW (folderHandle, buffer, heapSize, true,
FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME | FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_CREATION,
&bytesOut, nullptr, nullptr);
if (success && bytesOut > 0)
{
ScopedLock sl (lock);
uint8_t* rawData = buffer;
while (true)
{
FILE_NOTIFY_INFORMATION* fni = (FILE_NOTIFY_INFORMATION*)rawData;
Event e;
e.file = folder.getChildFile (String (fni->FileName, fni->FileNameLength / sizeof(wchar_t)));
switch (fni->Action)
{
case FILE_ACTION_ADDED:
e.fsEvent = fileCreated;
break;
case FILE_ACTION_RENAMED_NEW_NAME:
e.fsEvent = fileRenamedNewName;
break;
case FILE_ACTION_MODIFIED:
e.fsEvent = fileUpdated;
break;
case FILE_ACTION_REMOVED:
e.fsEvent = fileDeleted;
break;
case FILE_ACTION_RENAMED_OLD_NAME:
e.fsEvent = fileRenamedOldName;
break;
}
bool duplicateEvent = false;
for (auto existing : events)
{
if (e == existing)
{
duplicateEvent = true;
break;
}
}
if (! duplicateEvent)
events.add (e);
if (fni->NextEntryOffset > 0)
rawData += fni->NextEntryOffset;
else
break;
}
if (events.size() > 0)
triggerAsyncUpdate();
}
}
}
void handleAsyncUpdate() override
{
ScopedLock sl (lock);
for (auto e : events)
owner.fileChanged (e.file, e.fsEvent);
events.clear();
}
FileSystemWatcher& owner;
const File folder;
CriticalSection lock;
SmallArray<Event> events;
HANDLE folderHandle;
};
// Dummy implementation for OS where we don't support this yet
#elif JUCE_BSD
class FileSystemWatcher::Impl
{
public:
Impl (FileSystemWatcher& o, File f) : owner (o), folder (f)
{
}
~Impl()
{
}
FileSystemWatcher& owner;
const File folder;
};
#endif
#if defined JUCE_WINDOWS || defined JUCE_LINUX || defined JUCE_BSD
FileSystemWatcher::FileSystemWatcher()
{
}
FileSystemWatcher::~FileSystemWatcher()
{
}
void FileSystemWatcher::addFolder (const File& folder)
{
SmallArray<File> allFolders;
for (auto w : watched)
allFolders.add (w->folder);
if ( !allFolders.contains (folder))
watched.add (new Impl (*this, folder));
}
void FileSystemWatcher::removeFolder (const File& folder)
{
for (int i = watched.size(); --i >= 0;)
{
if (watched[i]->folder == folder)
{
watched.remove (i);
break;
}
}
}
void FileSystemWatcher::removeAllFolders()
{
watched.clear();
}
#endif