-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScanExecutionController.cs
More file actions
255 lines (228 loc) · 9.62 KB
/
Copy pathScanExecutionController.cs
File metadata and controls
255 lines (228 loc) · 9.62 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
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace c2flux
{
public sealed class ScanExecutionController
{
private readonly AppSettings _settings;
private readonly StatusMainFormController _statusMainFormController;
public ScanExecutionController(AppSettings settings, StatusMainFormController statusMainFormController)
{
_settings = settings;
_statusMainFormController = statusMainFormController;
}
public async Task<FileSystemEntry> ScanAsync(
string rootPath,
IProgress<ScanProgress> progress,
CancellationToken cancellationToken,
PauseToken pauseToken,
Action<string> statusKeyChanged = null)
{
DirectoryScanner directoryScanner = new DirectoryScanner(_settings);
NtQueryDirectoryScanner ntQueryDirectoryScanner = new NtQueryDirectoryScanner(_settings);
Stopwatch scannerStopwatch = Stopwatch.StartNew();
bool isRootDrivePath = IsRootDrivePath(rootPath);
bool isMftSupported = isRootDrivePath &&
NtfsMftScanner.IsSupported(rootPath);
AppAlertLog.AddVerboseInformation(
"Scan",
"MFT scanner selection",
string.Join(
Environment.NewLine,
string.Format("Path: {0}", rootPath),
string.Format("IsRootDrivePath: {0}", isRootDrivePath),
string.Format("NtfsMftScanner.IsSupported: {0}", isMftSupported),
string.Format("C2FluxScan: {0}", _settings.C2FluxScan)));
if (isMftSupported)
{
if (_settings.C2FluxScan)
{
try
{
SetStatusTextByKey("Status.MftFastScanRunning", statusKeyChanged);
C2FluxScanner c2FluxScanner = new C2FluxScanner(_settings);
scannerStopwatch.Restart();
FileSystemEntry result = await c2FluxScanner.ScanAsync(
rootPath,
progress,
cancellationToken,
pauseToken);
LogScannerPerformance("C2FluxScanner", rootPath, scannerStopwatch.Elapsed);
return result;
}
catch (OperationCanceledException)
{
throw;
}
catch (Exception c2FluxException)
{
LogScannerPerformance("C2FluxScanner", rootPath, scannerStopwatch.Elapsed);
AppAlertLog.AddWarning(
LocalizationService.GetText("Alert.Scan"),
LocalizationService.Format(
"Alert.MftUnavailable",
c2FluxException.Message));
}
}
else
{
try
{
SetStatusTextByKey("Status.MftFastScanRunning", statusKeyChanged);
NtfsMftScanner ntfsMftScanner = new NtfsMftScanner(_settings);
scannerStopwatch.Restart();
FileSystemEntry result = await ntfsMftScanner.ScanAsync(
rootPath,
progress,
cancellationToken,
pauseToken);
LogScannerPerformance("NtfsMftScanner", rootPath, scannerStopwatch.Elapsed);
return result;
}
catch (OperationCanceledException)
{
throw;
}
catch (Exception mftException)
{
LogScannerPerformance("NtfsMftScanner", rootPath, scannerStopwatch.Elapsed);
AppAlertLog.AddWarning(
LocalizationService.GetText("Alert.Scan"),
LocalizationService.Format(
"Alert.MftUnavailable",
mftException.Message));
}
}
}
try
{
SetStatusTextByKey("Status.NtQueryRunning", statusKeyChanged);
scannerStopwatch.Restart();
FileSystemEntry result = await ntQueryDirectoryScanner.ScanAsync(
rootPath,
progress,
cancellationToken,
pauseToken);
LogScannerPerformance(
"NtQueryDirectoryScanner",
rootPath,
scannerStopwatch.Elapsed,
ntQueryDirectoryScanner.ScannedFiles,
ntQueryDirectoryScanner.ScannedDirectories);
return result;
}
catch (OperationCanceledException)
{
throw;
}
catch (Exception ntQueryException)
{
LogScannerPerformance(
"NtQueryDirectoryScanner",
rootPath,
scannerStopwatch.Elapsed,
ntQueryDirectoryScanner.ScannedFiles,
ntQueryDirectoryScanner.ScannedDirectories);
AppAlertLog.AddWarning(
LocalizationService.GetText("Alert.Scan"),
LocalizationService.Format(
"Alert.NtQueryUnavailable",
ntQueryException.Message));
}
SetStatusTextByKey("Status.NtQueryUnavailableNormal", statusKeyChanged);
scannerStopwatch.Restart();
FileSystemEntry directoryResult = await directoryScanner.ScanAsync(
rootPath,
progress,
cancellationToken,
pauseToken);
LogScannerPerformance("DirectoryScanner", rootPath, scannerStopwatch.Elapsed);
return directoryResult;
}
private void LogScannerPerformance(
string scannerName,
string rootPath,
TimeSpan elapsed,
int? scannedFiles = null,
int? scannedDirectories = null)
{
string details = string.Format(
"Scanner: {0}{1}Path: {2}{1}ElapsedMilliseconds: {3:N0}",
scannerName,
Environment.NewLine,
rootPath,
elapsed.TotalMilliseconds);
if (string.Equals(
scannerName,
"NtQueryDirectoryScanner",
StringComparison.Ordinal))
{
int workerCount =
Math.Clamp(
Environment.ProcessorCount * 2,
4,
32);
int directoryQueryBufferSize =
_settings.NtQueryDirectoryBufferSize;
long maximumDirectoryQueryBufferBytes =
(long)workerCount *
directoryQueryBufferSize;
details +=
Environment.NewLine +
string.Format(
"DirectoryQueryBufferSizeBytes: {0:N0}{1}DirectoryQueryBufferSizeKiB: {2:N0}{1}WorkerCount: {3:N0}{1}MaximumDirectoryQueryBufferBytes: {4:N0}{1}MaximumDirectoryQueryBufferMiB: {5:N2}",
directoryQueryBufferSize,
Environment.NewLine,
directoryQueryBufferSize / 1024D,
workerCount,
maximumDirectoryQueryBufferBytes,
maximumDirectoryQueryBufferBytes /
(1024D * 1024D));
if (scannedFiles.HasValue)
{
details +=
Environment.NewLine +
string.Format(
"ScannedFiles: {0:N0}",
scannedFiles.Value);
}
if (scannedDirectories.HasValue)
{
details +=
Environment.NewLine +
string.Format(
"ScannedDirectories: {0:N0}",
scannedDirectories.Value);
}
}
AppAlertLog.AddVerboseInformation(
"Performance",
string.Format(
"{0}: {1:N0} ms",
scannerName,
elapsed.TotalMilliseconds),
details);
}
private void SetStatusTextByKey(string statusKey, Action<string> statusKeyChanged)
{
if (statusKeyChanged != null)
{
statusKeyChanged(statusKey);
return;
}
_statusMainFormController.SetStatusTextByKey(statusKey);
}
private static bool IsRootDrivePath(string rootPath)
{
string pathRoot = Path.GetPathRoot(rootPath);
return !string.IsNullOrWhiteSpace(pathRoot) &&
string.Equals(
Path.GetFullPath(rootPath).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar),
pathRoot.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar),
StringComparison.OrdinalIgnoreCase);
}
}
}