-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiskMonitorWaitThread.cpp
402 lines (342 loc) · 11.4 KB
/
DiskMonitorWaitThread.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
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
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "DiskMonitorWaitThread.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// Important: Methods and properties of objects in VCL can only be
// used in a method called using Synchronize, for example:
//
// Synchronize(UpdateCaption);
//
// where UpdateCaption could look like:
//
// void __fastcall TMonitorWaitThread::UpdateCaption()
// {
// Form1->Caption = "Updated in a thread";
// }
//---------------------------------------------------------------------------
__fastcall TMonitorWaitThread::TMonitorWaitThread(AnsiString Dir, bool SubTree, bool SpecificFiles, TMonitorCallback multip)
: TThread(true)
{
FreeOnTerminate = false;
WatchSubtree = SubTree;
AllFiles = !SpecificFiles;
FMultiplexer = multip;
MyHandle = NULL;
EventInfo = NULL;
EventHistory = NULL;
FEvent = MCI_UNKNOWN;
SetTimeout(15); // 15 seconds default...
FMonitorPath = new AnsiString();
SetMonitorPath(Dir.LowerCase());
FileList = new TStringList();
FileList->Sorted = true;
AffectedFile = new AnsiString();
AuxFile = new AnsiString();
if (!Suspended)
Suspend();
SetupMonitoring();
}
//---------------------------------------------------------------------------
__fastcall TMonitorWaitThread::~TMonitorWaitThread()
{
ShutdownMonitoring();
ClearFileInfo();
delete FMonitorPath;
delete FileList;
delete AffectedFile;
delete AuxFile;
}
//---------------------------------------------------------------------------
long __fastcall TMonitorWaitThread::GetTimeout()
{
return (FTimeout / 1000);
}
//---------------------------------------------------------------------------
void __fastcall TMonitorWaitThread::SetTimeout(long sec)
{
if (sec < MIN_TIMEOUT)
FTimeout = (MIN_TIMEOUT * 1000);
else
FTimeout = (sec * 1000);
}
//---------------------------------------------------------------------------
AnsiString __fastcall TMonitorWaitThread::GetMonitorPath()
{
return *FMonitorPath;
}
//---------------------------------------------------------------------------
void __fastcall TMonitorWaitThread::SetMonitorPath(AnsiString p)
{
*FMonitorPath = p;
if (FMonitorPath->SubString(FMonitorPath->Length(), 1) == "\\")
FMonitorPath->Delete(FMonitorPath->Length(), 1);
}
//---------------------------------------------------------------------------
void __fastcall TMonitorWaitThread::AddFilename(AnsiString *fname)
{
// if (!DirectoryExists(*fname)) {
if (!DirExists(*fname)) {
AnsiString *tn = new AnsiString();
AnsiString *tf = new AnsiString();
*tn = ExtractFilePath(fname->LowerCase());
*tf = ExtractFileName(fname->LowerCase());
if (tn->SubString(tn->Length(), 1) == "\\")
tn->Delete(tn->Length(), 1);
if (tn->AnsiCompareIC(*FMonitorPath) == 0) {
if (!tf->IsEmpty()) { // *fname is a directory...
int i = FileList->Add(*tf);
LoadFileInfo(i, *tf);
}
}
delete tn;
delete tf;
}
}
//---------------------------------------------------------------------------
void __fastcall TMonitorWaitThread::SetupMonitoring()
{
FilterCond = 0;
BytesBack = 0;
NotBufSize = (sizeof(FILE_NOTIFY_INFORMATION) + 4096) * 8;
MyHandle = FindFirstChangeNotification(FMonitorPath->c_str(), WatchSubtree,
FILE_NOTIFY_CHANGE_FILE_NAME |
FILE_NOTIFY_CHANGE_ATTRIBUTES |
FILE_NOTIFY_CHANGE_SIZE |
FILE_NOTIFY_CHANGE_LAST_WRITE);
if (MyHandle == INVALID_HANDLE_VALUE)
MyHandle = NULL;
FilterCond |= FILE_NOTIFY_CHANGE_FILE_NAME;
FilterCond |= FILE_NOTIFY_CHANGE_DIR_NAME;
FilterCond |= FILE_NOTIFY_CHANGE_ATTRIBUTES;
FilterCond |= FILE_NOTIFY_CHANGE_SIZE;
FilterCond |= FILE_NOTIFY_CHANGE_LAST_WRITE;
FilterCond |= FILE_NOTIFY_CHANGE_LAST_ACCESS;
FilterCond |= FILE_NOTIFY_CHANGE_CREATION;
FilterCond |= FILE_NOTIFY_CHANGE_SECURITY;
DirHandle = CreateFile(FMonitorPath->c_str(),
FILE_LIST_DIRECTORY,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED,
NULL);
if (DirHandle == INVALID_HANDLE_VALUE)
DirHandle = NULL;
Overlap = (OVERLAPPED *)malloc(sizeof(OVERLAPPED));
memset(Overlap, 0x00, sizeof(OVERLAPPED));
NotBuf = (FILE_NOTIFY_INFORMATION *)malloc(NotBufSize);
memset(NotBuf, 0x00, NotBufSize);
if (AllFiles)
LoadAllFiles();
}
//---------------------------------------------------------------------------
void __fastcall TMonitorWaitThread::ShutdownMonitoring()
{
if (MyHandle != NULL) {
FindCloseChangeNotification(MyHandle);
MyHandle = NULL;
}
if (DirHandle != NULL) {
CloseHandle(DirHandle);
DirHandle = NULL;
}
if (Overlap != NULL)
free(Overlap);
if (NotBuf != NULL)
free(NotBuf);
}
//---------------------------------------------------------------------------
bool __fastcall TMonitorWaitThread::IsSpecificFile(AnsiString fname)
{
bool rc = false;
for (int i = 0; i < FileList->Count; i++) {
rc = (FileList->Strings[i].AnsiCompareIC(fname) == 0);
if (rc)
break;
}
return rc;
}
//---------------------------------------------------------------------------
int __fastcall TMonitorWaitThread::GetSpecificFileIndex(AnsiString fname)
{
int rc = -1;
for (int i = 0; i < FileList->Count; i++) {
if (FileList->Strings[i].AnsiCompareIC(fname) == 0) {
rc = i;
break;
}
}
return rc;
}
//---------------------------------------------------------------------------
bool __fastcall TMonitorWaitThread::DirExists(AnsiString dir)
{
bool rc;
AnsiString *d = new AnsiString();
*d = GetCurrentDir();
rc = SetCurrentDir(dir);
SetCurrentDir(*d);
return rc;
}
//---------------------------------------------------------------------------
void __fastcall TMonitorWaitThread::Execute()
{
TMonitorChangedItem Change = MCI_UNKNOWN;
int rc;
FILE_NOTIFY_INFORMATION *ptr;
DWORD Ofs;
int BufSize = 8192;
char wc_buf[8192];
if ((MyHandle == NULL) || (DirHandle == NULL))
Terminate();
*AffectedFile = "";
*AuxFile = "";
while (!Terminated) {
rc = WaitForSingleObject(MyHandle, FTimeout);
if (!Terminated) {
switch (rc) {
case WAIT_OBJECT_0:
if (ReadDirectoryChangesW(DirHandle, NotBuf, NotBufSize, WatchSubtree, FilterCond, &BytesBack, Overlap, NULL)) {
ptr = NotBuf;
Ofs = ptr->NextEntryOffset;
do {
memset(wc_buf, 0x00, BufSize);
memcpy(wc_buf, ptr->FileName, ptr->FileNameLength);
if (Change != MCI_PRIVATE) {
*AffectedFile = "";
*AuxFile = "";
}
switch (ptr->Action) {
case FILE_ACTION_ADDED:
Change = MCI_CREATE;
*AffectedFile = WideCharToString((wchar_t *)wc_buf);
break;
case FILE_ACTION_REMOVED:
Change = MCI_DELETE;
*AffectedFile = WideCharToString((wchar_t *)wc_buf);
break;
case FILE_ACTION_MODIFIED:
Change = MCI_MODIFY;
*AffectedFile = WideCharToString((wchar_t *)wc_buf);
break;
case FILE_ACTION_RENAMED_OLD_NAME: // Renamed to...
Change = MCI_PRIVATE;
*AuxFile = WideCharToString((wchar_t *)wc_buf);
break;
case FILE_ACTION_RENAMED_NEW_NAME: // Renamed from...
Change = MCI_RENAME;
*AffectedFile = WideCharToString((wchar_t *)wc_buf);
break;
default:
Change = MCI_UNKNOWN;
}
if (Change != MCI_UNKNOWN)
if (Change != MCI_PRIVATE)
if (FMultiplexer != NULL) {
int j = GetSpecificFileIndex(*AffectedFile);
if (j > -1)
EventHistory = (TMonitorFileInfo *)FileList->Objects[j];
else
EventHistory = NULL;
EventInfo = new TMonitorFileInfo(*FMonitorPath, *AffectedFile);
if (!AllFiles) {
if (EventHistory != NULL) {
// FMultiplexer(*MonDirectory, *AffectedFile, *AuxFile, c, p, Change);
FEvent = Change;
Synchronize((TThreadMethod)FMultiplexer);
FEvent = MCI_UNKNOWN;
}
} else {
// FMultiplexer(*MonDirectory, *AffectedFile, *AuxFile, c, p, Change);
FEvent = Change;
Synchronize((TThreadMethod)FMultiplexer);
FEvent = MCI_UNKNOWN;
}
delete EventInfo;
if (EventHistory != NULL) // Load latest info for file...
EventHistory->ReloadInfo();
}
if (Ofs > 0) {
ptr = (FILE_NOTIFY_INFORMATION *)((DWORD)ptr + Ofs);
Ofs = ptr->NextEntryOffset;
continue;
}
} while (Ofs > 0);
FindNextChangeNotification(MyHandle);
} else
Terminate();
break;
case WAIT_ABANDONED:
Terminate();
break;
case WAIT_TIMEOUT:
FindNextChangeNotification(MyHandle);
break;
default:
Terminate();
}
}
}
ShutdownMonitoring();
}
//---------------------------------------------------------------------------
int __fastcall TMonitorWaitThread::GetFileCount()
{
return FileList->Count;
}
//---------------------------------------------------------------------------
void __fastcall TMonitorWaitThread::ClearFileInfo()
{
TMonitorFileInfo *p;
for (int i = 0; i < FileList->Count; i++) {
p = (TMonitorFileInfo *)FileList->Objects[i];
delete p;
}
FileList->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TMonitorWaitThread::LoadFileInfo(int i, AnsiString fn)
{
TMonitorFileInfo *p;
p = (TMonitorFileInfo *)FileList->Objects[i];
if (p != NULL)
delete p;
FileList->Objects[i] = new TMonitorFileInfo(*FMonitorPath, fn);
}
//---------------------------------------------------------------------------
void __fastcall TMonitorWaitThread::LoadAllFiles()
{
TSearchRec sr;
int i;
if (FileCount > 0)
ClearFileInfo();
if (FindFirst(*FMonitorPath + "\\*.*", faAnyFile, sr) == 0) {
i = FileList->Add(sr.Name);
LoadFileInfo(i, sr.Name);
while (FindNext(sr) == 0) {
i = FileList->Add(sr.Name);
LoadFileInfo(i, sr.Name);
}
}
FindClose(sr);
}
//---------------------------------------------------------------------------
TMonitorFileInfo * __fastcall TMonitorWaitThread::GetEventFileInfo()
{
return EventInfo;
}
//---------------------------------------------------------------------------
TMonitorFileInfo * __fastcall TMonitorWaitThread::GetEventFileHistory()
{
return EventHistory;
}
//---------------------------------------------------------------------------
TMonitorChangedItem __fastcall TMonitorWaitThread::GetEvent()
{
return FEvent;
}
//---------------------------------------------------------------------------
#include "DiskMonitorFileInfo.cpp"
//---------------------------------------------------------------------------