Repository navigation
Expand file tree
/
Copy pathFolderDiffService.cs
More file actions
486 lines (435 loc) · 24.7 KB
/
Copy pathFolderDiffService.cs
File metadata and controls
486 lines (435 loc) · 24.7 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
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using FolderDiffIL4DotNet.Common;
using FolderDiffIL4DotNet.Core.IO;
using FolderDiffIL4DotNet.Models;
namespace FolderDiffIL4DotNet.Services
{
/// <summary>
/// Service that compares two folders and classifies files as Unchanged / Added / Removed / Modified.
/// フォルダ間の差分を比較し、ファイルを Unchanged / Added / Removed / Modified に分類するサービス。
/// </summary>
public sealed partial class FolderDiffService : IFolderDiffService
{
private const string SPINNER_LABEL_FOLDER_DIFF = "Diffing folders";
private const string SPINNER_LABEL_DISCOVERING_FILES = "Discovering files";
private const string SPINNER_LABEL_SCANNING_ASSEMBLIES = "Scanning assemblies";
private const string LOG_NETWORK_OPTIMIZED_SKIP_IL = $"Network-optimized mode: skip {Constants.LABEL_IL} precompute to reduce network I/O.";
private const string LOG_FOLDER_DIFF_COMPLETED = "Folder diff completed.";
private const string LOG_FILE_DELETED_DURING_COMPARISON = "File '{0}' was deleted from the new folder after enumeration; classifying as Removed.";
private const string MODE_LOCAL_OPTIMIZED = "Local-optimized";
private const string MODE_SERVER_NAS_OPTIMIZED = "Server/NAS-optimized";
private const int DEFAULT_IL_PRECOMPUTE_BATCH_SIZE = 2048;
/// <summary>
/// Threshold at which discovery-phase file count is considered "large" and warrants an extra log line.
/// Above 10,000 entries the enumeration itself can take several seconds, so we notify the user proactively.
/// 大規模フォルダとして追加ログを出す和集合件数の閾値。1 万件を超えると発見フェーズ自体が数秒を要し始めるため、
/// この閾値を超えた場合に追加の進捗ログを出力してユーザーへ処理中であることを知らせます。
/// </summary>
private const int LARGE_DISCOVERY_FILE_COUNT_LOG_THRESHOLD = 10000;
private readonly IReadOnlyConfigSettings _config;
private readonly ProgressReportService _progressReporter;
private readonly string _oldFolderAbsolutePath;
private readonly string _newFolderAbsolutePath;
private readonly string _ilOutputFolderAbsolutePath;
private readonly string _ilOldFolderAbsolutePath;
private readonly string _ilNewFolderAbsolutePath;
private readonly IFileDiffService _fileDiffService;
private readonly bool _optimizeForNetworkShares;
private readonly bool _detectedNetworkOld;
private readonly bool _detectedNetworkNew;
private readonly FileDiffResultLists _fileDiffResultLists;
private readonly ILoggerService _logger;
private readonly IFileSystemService _fileSystem;
private readonly IFolderDiffExecutionStrategy _executionStrategy;
private readonly StringComparer _pathComparer;
/// <summary>
/// Initializes a new instance of <see cref="FolderDiffService"/> with default file-system and execution-strategy implementations.
/// 既定のファイルシステム実装と実行戦略で <see cref="FolderDiffService"/> を初期化します。
/// </summary>
public FolderDiffService(
IReadOnlyConfigSettings config,
ProgressReportService progressReporter,
DiffExecutionContext executionContext,
IFileDiffService fileDiffService,
FileDiffResultLists fileDiffResultLists,
ILoggerService logger)
: this(config, progressReporter, executionContext, fileDiffService, fileDiffResultLists, logger, new FileSystemService(), null)
{
}
/// <summary>
/// Constructor that allows substituting the file-system implementation for testing.
/// テスト向けにファイルシステム実装を差し替え可能なコンストラクタ。
/// </summary>
public FolderDiffService(
IReadOnlyConfigSettings config,
ProgressReportService progressReporter,
DiffExecutionContext executionContext,
IFileDiffService fileDiffService,
FileDiffResultLists fileDiffResultLists,
ILoggerService logger,
IFileSystemService fileSystem)
: this(config, progressReporter, executionContext, fileDiffService, fileDiffResultLists, logger, fileSystem, null)
{
}
/// <summary>
/// Constructor that also allows substituting the execution strategy for testing or DI.
/// テストや DI 向けに戦略オブジェクトも差し替え可能なコンストラクタ。
/// </summary>
public FolderDiffService(
IReadOnlyConfigSettings config,
ProgressReportService progressReporter,
DiffExecutionContext executionContext,
IFileDiffService fileDiffService,
FileDiffResultLists fileDiffResultLists,
ILoggerService logger,
IFileSystemService fileSystem,
IFolderDiffExecutionStrategy? executionStrategy)
{
ArgumentNullException.ThrowIfNull(config);
ArgumentNullException.ThrowIfNull(progressReporter);
ArgumentNullException.ThrowIfNull(executionContext);
ArgumentNullException.ThrowIfNull(fileDiffService);
ArgumentNullException.ThrowIfNull(fileSystem);
_config = config;
_progressReporter = progressReporter;
_oldFolderAbsolutePath = executionContext.OldFolderAbsolutePath;
_newFolderAbsolutePath = executionContext.NewFolderAbsolutePath;
_ilOutputFolderAbsolutePath = executionContext.IlOutputFolderAbsolutePath;
_ilOldFolderAbsolutePath = executionContext.IlOldFolderAbsolutePath;
_ilNewFolderAbsolutePath = executionContext.IlNewFolderAbsolutePath;
ArgumentNullException.ThrowIfNull(fileDiffResultLists);
_fileDiffResultLists = fileDiffResultLists;
ArgumentNullException.ThrowIfNull(logger);
_logger = logger;
_detectedNetworkOld = executionContext.DetectedNetworkOld;
_detectedNetworkNew = executionContext.DetectedNetworkNew;
_optimizeForNetworkShares = executionContext.OptimizeForNetworkShares;
_fileDiffService = fileDiffService;
_fileSystem = fileSystem;
_executionStrategy = executionStrategy ?? new FolderDiffExecutionStrategy(config, executionContext, fileDiffResultLists, fileSystem);
_pathComparer = DetermineRelativePathComparer(_oldFolderAbsolutePath, _newFolderAbsolutePath);
}
/// <summary>
/// Recursively scans old/new folders, excludes ignored extensions, classifies files as
/// Unchanged / Added / Removed / Modified, and aggregates results in <see cref="FileDiffResultLists"/>.
/// Progress is reported per file, using the union count of old and new relative paths as 100%.
/// 2つのフォルダ(old/new)を再帰的に走査し、無視拡張子を除外した上で
/// Unchanged / Added / Removed / Modified を分類し、<see cref="FileDiffResultLists"/> に集計します。
/// 進捗は old と new の相対パスの和集合件数を母数として 1 件ごとに報告します。
/// </summary>
public async Task ExecuteFolderDiffAsync(CancellationToken cancellationToken = default)
{
LogExecutionMode();
ClearResultCollections();
var folderDiffCompleted = false;
var currentPhase = "initializing folder diff";
var hasEnumeratedOldFiles = false;
var hasEnumeratedNewFiles = false;
int? totalFilesRelativePathCount = null;
int? maxParallel = null;
try
{
currentPhase = "enumerating files";
EnumerateAllFiles(ref hasEnumeratedOldFiles, ref hasEnumeratedNewFiles);
totalFilesRelativePathCount = _executionStrategy.ComputeUnionFileCount(
_fileDiffResultLists.OldFilesAbsolutePath,
_fileDiffResultLists.NewFilesAbsolutePath);
if (totalFilesRelativePathCount.Value == 0)
{
_progressReporter.ReportProgress(100);
folderDiffCompleted = true;
return;
}
currentPhase = "determining parallelism";
maxParallel = _executionStrategy.DetermineMaxParallel();
currentPhase = "logging discovery statistics";
LogDiscoveryStats(totalFilesRelativePathCount.Value, maxParallel.Value);
currentPhase = "scanning assembly candidates";
ScanAssemblyCandidatesAndLog();
currentPhase = "validating configured IL substrings";
ValidateILConfiguredStrings();
currentPhase = "precomputing IL cache";
await PrecomputeIlCachesAsync(maxParallel.Value, cancellationToken);
// Begin the diff classification phase so the bar restarts at 0%.
// 差分分類フェーズを開始し、バーが 0% から再スタートする。
_progressReporter.BeginPhase(SPINNER_LABEL_FOLDER_DIFF);
currentPhase = "creating IL output directories";
CreateIlOutputDirectoriesIfNeeded();
var remainingNewFilesAbsolutePathHashSet = new HashSet<string>(_fileDiffResultLists.NewFilesAbsolutePath, _pathComparer);
int processedFileCount = 0;
if (maxParallel.Value <= 1)
{
currentPhase = "classifying files sequentially";
processedFileCount = await DetermineDiffsSequentiallyAsync(remainingNewFilesAbsolutePathHashSet, totalFilesRelativePathCount.Value, processedFileCount, cancellationToken);
}
else
{
currentPhase = "classifying files in parallel";
processedFileCount = await DetermineDiffsInParallelAsync(remainingNewFilesAbsolutePathHashSet, totalFilesRelativePathCount.Value, processedFileCount, maxParallel.Value, cancellationToken);
}
currentPhase = "processing added files";
ProcessAddedFiles(remainingNewFilesAbsolutePathHashSet, processedFileCount, totalFilesRelativePathCount.Value);
folderDiffCompleted = true;
}
catch (Exception ex) when (ex is ArgumentException or DirectoryNotFoundException
or IOException or UnauthorizedAccessException
or InvalidOperationException or NotSupportedException)
{
LogExpectedFolderDiffFailure(ex, currentPhase, hasEnumeratedOldFiles, hasEnumeratedNewFiles, totalFilesRelativePathCount, maxParallel);
throw;
}
catch (Exception ex)
{
LogUnexpectedFolderDiffFailure(ex, currentPhase, hasEnumeratedOldFiles, hasEnumeratedNewFiles, totalFilesRelativePathCount, maxParallel);
throw;
}
finally
{
if (folderDiffCompleted)
{
_logger.LogMessage(AppLogLevel.Info, LOG_FOLDER_DIFF_COMPLETED, shouldOutputMessageToConsole: true);
}
}
}
private void LogExpectedFolderDiffFailure(Exception exception, string currentPhase, bool hasEnumeratedOldFiles, bool hasEnumeratedNewFiles, int? totalFilesRelativePathCount, int? maxParallel)
{
_logger.LogMessage(
AppLogLevel.Error,
BuildFailureMessage(
prefix: "An error occurred while diffing",
exception,
currentPhase,
hasEnumeratedOldFiles,
hasEnumeratedNewFiles,
totalFilesRelativePathCount,
maxParallel),
shouldOutputMessageToConsole: true,
exception);
}
private void LogUnexpectedFolderDiffFailure(Exception exception, string currentPhase, bool hasEnumeratedOldFiles, bool hasEnumeratedNewFiles, int? totalFilesRelativePathCount, int? maxParallel)
{
_logger.LogMessage(
AppLogLevel.Error,
BuildFailureMessage(
prefix: "An unexpected error occurred while diffing",
exception,
currentPhase,
hasEnumeratedOldFiles,
hasEnumeratedNewFiles,
totalFilesRelativePathCount,
maxParallel),
shouldOutputMessageToConsole: true,
exception);
}
private string BuildFailureMessage(string prefix, Exception exception, string currentPhase, bool hasEnumeratedOldFiles, bool hasEnumeratedNewFiles, int? totalFilesRelativePathCount, int? maxParallel)
{
var message = $"{prefix} '{_oldFolderAbsolutePath}' and '{_newFolderAbsolutePath}' during phase '{currentPhase}'. "
+ $"Mode={GetExecutionModeLabel()}, "
+ $"{PathShapeDiagnostics.DescribeState("OldFolder", _oldFolderAbsolutePath)}, "
+ $"{PathShapeDiagnostics.DescribeState("NewFolder", _newFolderAbsolutePath)}";
if (maxParallel.HasValue)
{
message += $", MaxParallel={maxParallel.Value}";
}
if (hasEnumeratedOldFiles)
{
message += $", OldFiles={_fileDiffResultLists.OldFilesAbsolutePath.Count}";
}
if (hasEnumeratedNewFiles)
{
message += $", NewFiles={_fileDiffResultLists.NewFilesAbsolutePath.Count}";
}
if (totalFilesRelativePathCount.HasValue)
{
message += $", UnionFiles={totalFilesRelativePathCount.Value}";
}
return message + $". Failure={exception.GetType().Name}: {exception.Message}";
}
private static StringComparer DetermineRelativePathComparer(string oldFolderAbsolutePath, string newFolderAbsolutePath)
{
return IsCaseInsensitiveDirectory(oldFolderAbsolutePath) && IsCaseInsensitiveDirectory(newFolderAbsolutePath)
? StringComparer.OrdinalIgnoreCase
: StringComparer.Ordinal;
}
private static bool IsCaseInsensitiveDirectory(string directoryPath)
{
if (OperatingSystem.IsWindows())
{
return true;
}
try
{
string fullPath = Path.GetFullPath(directoryPath);
if (!Directory.Exists(fullPath))
{
return OperatingSystem.IsMacOS();
}
string trimmedPath = fullPath.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
string? parentPath = Path.GetDirectoryName(trimmedPath);
string directoryName = Path.GetFileName(trimmedPath);
string alternateName = ToggleAsciiCase(directoryName);
if (string.IsNullOrEmpty(parentPath) || alternateName == directoryName)
{
return OperatingSystem.IsMacOS();
}
return Directory.Exists(Path.Combine(parentPath, alternateName));
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or ArgumentException or NotSupportedException)
{
return OperatingSystem.IsMacOS();
}
}
private static string ToggleAsciiCase(string value)
{
for (int i = 0; i < value.Length; i++)
{
char ch = value[i];
if (ch is >= 'a' and <= 'z')
{
return value[..i] + char.ToUpperInvariant(ch) + value[(i + 1)..];
}
if (ch is >= 'A' and <= 'Z')
{
return value[..i] + char.ToLowerInvariant(ch) + value[(i + 1)..];
}
}
return value;
}
/// <summary>
/// Logs the execution mode (local-optimised / server-NAS-optimised) and the reasoning behind it.
/// 実行モード(ローカル最適化 / サーバー・NAS 最適化)とその判定理由をログに出力します。
/// </summary>
private void LogExecutionMode()
{
var reason = $"manual={_config.OptimizeForNetworkShares}, auto={_config.AutoDetectNetworkShares}, oldIsNetwork={_detectedNetworkOld}, newIsNetwork={_detectedNetworkNew}";
_logger.LogMessage(AppLogLevel.Info, $"Execution mode: {GetExecutionModeLabel()} ({reason})", shouldOutputMessageToConsole: true);
}
private string GetExecutionModeLabel()
=> _optimizeForNetworkShares ? MODE_SERVER_NAS_OPTIMIZED : MODE_LOCAL_OPTIMIZED;
/// <summary>
/// Clears all classification results from the previous run.
/// 前回実行の分類結果をすべてクリアします。
/// </summary>
private void ClearResultCollections()
{
_fileDiffResultLists.ResetAll();
}
/// <summary>
/// Enumerates old/new folder files (excluding ignored extensions) into <see cref="FileDiffResultLists"/>.
/// 無視拡張子を除いた旧・新フォルダのファイル一覧を <see cref="FileDiffResultLists"/> に格納します。
/// </summary>
private void EnumerateAllFiles(ref bool hasEnumeratedOldFiles, ref bool hasEnumeratedNewFiles)
{
// Show a dedicated "Discovering files" phase so the user sees progress during file enumeration.
// ファイル列挙中にユーザーへ進捗を示すため、専用の「Discovering files」フェーズを表示する。
_progressReporter.BeginPhase(SPINNER_LABEL_DISCOVERING_FILES);
_fileDiffResultLists.SetOldFilesAbsolutePath(_executionStrategy.EnumerateIncludedFiles(_oldFolderAbsolutePath, FileDiffResultLists.IgnoredFileLocation.Old));
hasEnumeratedOldFiles = true;
_progressReporter.ReportProgress(50.0);
_fileDiffResultLists.SetNewFilesAbsolutePath(_executionStrategy.EnumerateIncludedFiles(_newFolderAbsolutePath, FileDiffResultLists.IgnoredFileLocation.New));
hasEnumeratedNewFiles = true;
_progressReporter.ReportProgress(100.0);
}
/// <summary>
/// Logs parallelism level and file counts after discovery. Does not perform I/O-heavy work.
/// ディスカバリ後の並列度・ファイル件数をログ出力します。I/O の重い処理は行いません。
/// </summary>
private void LogDiscoveryStats(int totalFilesRelativePathCount, int maxParallel)
{
_logger.LogMessage(
AppLogLevel.Info,
$"Parallel diff processing: maxParallel={maxParallel} (configured={_config.MaxParallelism}, OptimizeForNetworkShares={_optimizeForNetworkShares}, logical processors={Environment.ProcessorCount})",
shouldOutputMessageToConsole: true);
int oldCount = _fileDiffResultLists.OldFilesAbsolutePath.Count;
int newCount = _fileDiffResultLists.NewFilesAbsolutePath.Count;
_logger.LogMessage(AppLogLevel.Info, $"Discovery complete: old={oldCount}, new={newCount}, union(relative)={totalFilesRelativePathCount}", shouldOutputMessageToConsole: true);
if (totalFilesRelativePathCount >= LARGE_DISCOVERY_FILE_COUNT_LOG_THRESHOLD)
{
_logger.LogMessage(
AppLogLevel.Info,
$"Large file set detected (union(relative)={totalFilesRelativePathCount}). IL precompute will run in batches to limit peak memory usage.",
shouldOutputMessageToConsole: true);
}
}
/// <summary>
/// Scans files for .NET assembly candidates with per-file progress reporting under a dedicated label,
/// then logs the precompute-target summary. This eliminates the "dark period" between discovery and IL precompute.
/// ファイルを .NET アセンブリ候補としてスキャンし、専用ラベル下でファイル単位の進捗を報告した後、
/// プリコンピュート対象のサマリをログ出力します。ディスカバリと IL プリコンピュートの間の「暗黒期間」を解消します。
/// </summary>
private void ScanAssemblyCandidatesAndLog()
{
_progressReporter.BeginPhase(SPINNER_LABEL_SCANNING_ASSEMBLIES);
int dotNetAssemblyCandidates = _executionStrategy.CountDotNetAssemblyCandidates(
_fileDiffResultLists.OldFilesAbsolutePath,
_fileDiffResultLists.NewFilesAbsolutePath,
percentage => _progressReporter.ReportProgress(percentage));
_progressReporter.ReportProgress(100.0);
int totalFilesForLog = _fileDiffResultLists.OldFilesAbsolutePath
.Concat(_fileDiffResultLists.NewFilesAbsolutePath)
.Distinct(StringComparer.OrdinalIgnoreCase)
.Count();
_logger.LogMessage(
AppLogLevel.Info,
$"Precompute targets: totalFiles={totalFilesForLog}, {nameof(dotNetAssemblyCandidates)}={dotNetAssemblyCandidates}, batchSize={GetEffectiveIlPrecomputeBatchSize()}",
shouldOutputMessageToConsole: true);
}
/// <summary>
/// Records remaining new-side files (absent from old side) as Added and updates progress.
/// new 側に残っているファイル(old 側に存在しないもの)を Added として記録し、進捗を更新します。
/// </summary>
private void ProcessAddedFiles(IEnumerable<string> remainingNewFiles, int processedFileCount, int totalFilesRelativePathCount)
{
foreach (var newFileAbsolutePath in remainingNewFiles)
{
_fileDiffResultLists.AddAddedFileAbsolutePath(newFileAbsolutePath);
processedFileCount++;
_progressReporter.ReportProgress(Math.Min((double)processedFileCount * 100.0 / totalFilesRelativePathCount, 100.0));
}
}
/// <summary>
/// Validates configured IL ignore and normalization substrings and records safety warnings,
/// including short values, duplicates, and containment relationships. Also logs each warning to the console.
/// 設定された IL 行無視および正規化の部分文字列を検証し、短い値、重複、包含関係などの
/// 安全性警告を記録します。各警告はコンソールにも出力されます。
/// </summary>
private void ValidateILConfiguredStrings()
{
var normalizationWarnings = _config.ShouldILNormalizeContainingConfiguredStrings
? ILOutputService.ValidateILNormalizeContainingStrings(_config.ILNormalizeContainingStrings)
: new List<string>();
var ignoreWarnings = ILOutputService.ValidateILIgnoreContainingStrings(_config.ILIgnoreLineContainingStrings);
foreach (var warning in normalizationWarnings.Concat(ignoreWarnings))
{
_fileDiffResultLists.ILFilterWarnings.Add(warning);
_logger.LogMessage(AppLogLevel.Warning, warning, shouldOutputMessageToConsole: true);
}
}
/// <summary>
/// Records a warning when a Modified file's new-side timestamp is older than the old side.
/// Modified と判定されたファイルについて、new 側の更新日時が old 側より古い場合に警告情報を記録します。
/// </summary>
private void RecordNewFileTimestampOlderThanOldWarningIfNeeded(string fileRelativePath, string oldFileAbsolutePath, string newFileAbsolutePath)
{
if (!_config.ShouldWarnWhenNewFileTimestampIsOlderThanOldFileTimestamp)
{
return;
}
var oldLastWriteTimeUtc = _fileSystem.GetLastWriteTimeUtc(oldFileAbsolutePath);
var newLastWriteTimeUtc = _fileSystem.GetLastWriteTimeUtc(newFileAbsolutePath);
if (newLastWriteTimeUtc >= oldLastWriteTimeUtc)
{
return;
}
_fileDiffResultLists.RecordNewFileTimestampOlderThanOldWarning(
fileRelativePath,
Caching.TimestampCache.GetOrAdd(oldFileAbsolutePath),
Caching.TimestampCache.GetOrAdd(newFileAbsolutePath));
}
}
}