-
Notifications
You must be signed in to change notification settings - Fork 0
/
DiskMonitor.cpp
341 lines (292 loc) · 8.79 KB
/
DiskMonitor.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
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "DiskMonitor.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//
static inline void ValidCtrCheck(TDiskMonitor *)
{
new TDiskMonitor(NULL);
}
//---------------------------------------------------------------------------
__fastcall TDiskMonitor::TDiskMonitor(TComponent* Owner)
: TComponent(Owner)
{
FActive = false;
FExcludeSysFiles = true;
Suspended = !FActive;
CleanupProgress = 0;
Monitors = new TStringList();
MultiplexFile = new AnsiString();
AuxiliaryFile = new AnsiString();
}
//---------------------------------------------------------------------------
__fastcall TDiskMonitor::~TDiskMonitor()
{
if (Monitors->Count > 0)
CleanupMonitors();
delete Monitors;
delete MultiplexFile;
delete AuxiliaryFile;
}
//---------------------------------------------------------------------------
namespace Diskmonitor
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TDiskMonitor)};
RegisterComponents("Celestial", classes, 0);
}
}
//---------------------------------------------------------------------------
void __fastcall TDiskMonitor::CleanupMonitors()
{
TMonitorWaitThread *Thread;
CleanupProgress = 0;
for (int i = 0; i < Monitors->Count; i++) {
Thread = (TMonitorWaitThread *)Monitors->Objects[i];
if (Thread != NULL) {
if (!Thread->Terminated) {
Thread->Terminate();
Thread->WaitFor();
}
delete Thread;
}
CleanupProgress = ((i + 1) / Monitors->Count);
}
Monitors->Clear();
ResetCounters();
}
//---------------------------------------------------------------------------
double __fastcall TDiskMonitor::GetCleanupProgress()
{
return CleanupProgress;
}
//---------------------------------------------------------------------------
//void __fastcall TDiskMonitor::EventMultiplexer(AnsiString Dir, AnsiString Filename, AnsiString Oldname, TMonitorFileInfo *Info, TMonitorFileInfo *Oldinfo, TMonitorChangedItem i)
void __fastcall TDiskMonitor::EventMultiplexer()
{
TSearchRec sr;
int attr = 0;
long size = 0;
TDateTime fdatetime = 0;
TMonitorChangedItem Evt;
if (FActive)
FReceivedEvents++;
if ((FActive) && (!Suspended)) {
FHandledEvents++;
TMonitorWaitThread *Thread;
TMonitorFileInfo *Info;
for (int i = 0; i < Monitors->Count; i++) {
Thread = (TMonitorWaitThread *)Monitors->Objects[i];
if (Thread != NULL)
if (Thread->GetEvent() != MCI_UNKNOWN)
break;
}
// Evt = i;
Evt = Thread->GetEvent();
// *MultiplexFile = Dir;
Info = Thread->GetEventFileInfo();
*MultiplexFile = Info->GetPathname() + "\\" + Info->GetFilename();
if (FOnDirEvent != NULL)
FOnDirEvent(this, MultiplexFile);
/*
if (!Oldname.IsEmpty()) { // Create or rename
*AuxiliaryFile = *MultiplexFile + "\\" + Oldname; // Old name...
*MultiplexFile = *MultiplexFile + "\\" + Filename; // New (current) name...
} else // Delete
*MultiplexFile = *MultiplexFile + "\\" + Filename;
if (Filename.IsEmpty() && Oldname.IsEmpty()) { // Directory event...
Evt = MCI_UNKNOWN;
}
*/
if (Info != NULL) {
attr = Info->GetAttributes();
size = Info->GetFileSize();
fdatetime = Info->GetLastWrite();
}
bool Accept;
if (FExcludeSysFiles) {
Accept = ((attr & faSysFile) != faSysFile);
} else
Accept = true;
if (Accept) {
switch (Evt) {
case MCI_UNKNOWN:
break;
case MCI_CREATE:
if (FOnFileCreated != NULL)
FOnFileCreated(this, MultiplexFile);
break;
case MCI_DELETE:
if (FOnFileDeleted != NULL)
FOnFileDeleted(this, MultiplexFile);
break;
case MCI_RENAME:
if (FOnFileRenamed != NULL)
FOnFileRenamed(this, AuxiliaryFile, MultiplexFile);
break;
case MCI_MODIFY:
if (FOnFileModified != NULL)
FOnFileModified(this, MultiplexFile, attr, size, fdatetime);
break;
case MCI_ATTR:
if (FOnAttributeChange != NULL)
FOnAttributeChange(this, MultiplexFile, attr);
break;
case MCI_SIZE:
if (FOnSizeChange != NULL)
FOnSizeChange(this, MultiplexFile, size);
break;
case MCI_DATE:
if (FOnModifiedTimeChange != NULL)
FOnModifiedTimeChange(this, MultiplexFile, fdatetime);
break;
default: ;
}
}
}
}
//---------------------------------------------------------------------------
bool __fastcall TDiskMonitor::DirExists(AnsiString dir)
{
bool rc;
AnsiString *d = new AnsiString();
*d = GetCurrentDir();
rc = SetCurrentDir(dir);
SetCurrentDir(*d);
return rc;
}
//---------------------------------------------------------------------------
void __fastcall TDiskMonitor::SetActive(bool act)
{
if (act != FActive) {
if (act) {
ResetCounters();
Resume();
} else
CleanupMonitors();
FActive = act;
}
}
//---------------------------------------------------------------------------
void __fastcall TDiskMonitor::SetExcludeSysFiles(bool esf)
{
FExcludeSysFiles = esf;
}
//---------------------------------------------------------------------------
int __fastcall TDiskMonitor::GetMonitorCount()
{
return Monitors->Count;
}
//---------------------------------------------------------------------------
void __fastcall TDiskMonitor::Open()
{
if (!Active) {
TMonitorWaitThread *Thread;
for (int i = 0; i < Monitors->Count; i++) {
Thread = (TMonitorWaitThread *)Monitors->Objects[i];
if (Thread != NULL)
if (Thread->Suspended)
Thread->Resume();
}
SetActive(true);
}
}
//---------------------------------------------------------------------------
void __fastcall TDiskMonitor::Close()
{
if (Active) {
TMonitorWaitThread *Thread;
for (int i = 0; i < Monitors->Count; i++) {
Thread = (TMonitorWaitThread *)Monitors->Objects[i];
if (Thread != NULL) {
if (!Thread->Terminated) {
if (Thread->Suspended)
Thread->Resume();
Thread->Terminate();
}
}
}
SetActive(false);
CleanupMonitors();
}
}
//---------------------------------------------------------------------------
void __fastcall TDiskMonitor::Suspend()
{
if (!Suspended) {
TMonitorWaitThread *Thread;
for (int i = 0; i < Monitors->Count; i++) {
Thread = (TMonitorWaitThread *)Monitors->Objects[i];
if (Thread != NULL)
if (!Thread->Suspended)
Thread->Suspend();
}
Suspended = true;
}
}
//---------------------------------------------------------------------------
void __fastcall TDiskMonitor::Resume()
{
if (Suspended) {
TMonitorWaitThread *Thread;
for (int i = 0; i < Monitors->Count; i++) {
Thread = (TMonitorWaitThread *)Monitors->Objects[i];
if (Thread != NULL)
if (Thread->Suspended)
Thread->Resume();
}
Suspended = false;
}
}
//---------------------------------------------------------------------------
void __fastcall TDiskMonitor::ResetCounters()
{
FReceivedEvents = 0;
FHandledEvents = 0;
}
//---------------------------------------------------------------------------
void __fastcall TDiskMonitor::AddMonitorPath(TFileName Pathname, bool WatchSubTree)
{
TMonitorWaitThread *Thread;
AnsiString *fdir = new AnsiString();
AnsiString *fname = new AnsiString();
bool IsDir;
*fdir = Pathname;
if (fdir->SubString(fdir->Length(), 1) == "\\")
fdir->Delete(fdir->Length(), 1);
// IsDir = DirectoryExists(*fdir);
IsDir = DirExists(*fdir);
if (!IsDir)
*fdir = ExtractFilePath(*fdir);
*fdir = fdir->LowerCase();
if (fdir->SubString(fdir->Length(), 1) == "\\")
fdir->Delete(fdir->Length(), 1);
int j = Monitors->IndexOf(*fdir);
if (j > -1) {
Thread = (TMonitorWaitThread *)Monitors->Objects[j];
if (!Thread->Suspended)
Thread->Suspend();
} else
Thread = new TMonitorWaitThread(*fdir, WatchSubTree, false, &EventMultiplexer);
if (!IsDir) {
*fname = ExtractFileName(Pathname);
*fname = fname->LowerCase();
Thread->AddFilename(fname);
}
if (j == -1)
Monitors->AddObject(*fdir, Thread);
if (Thread != NULL)
if ((FActive) && (!Suspended))
if (Thread->Suspended)
Thread->Resume();
delete fname;
delete fdir;
}
//---------------------------------------------------------------------------
#include "DiskMonitorWaitThread.cpp"
//---------------------------------------------------------------------------