|
| 1 | +using System.IO; |
1 | 2 | using System.Net; |
2 | 3 | using System.Reflection; |
3 | 4 |
|
@@ -482,49 +483,59 @@ public void Purge() |
482 | 483 | private void RemoveOrphanedResponseFiles(bool force) |
483 | 484 | { |
484 | 485 | var removeBefore = force ? DateTime.MaxValue : _timeProvider.GetUtcNow().AddMinutes(-30); |
485 | | - var orphanedResponseFiles = new Dictionary<FileName, FileInfo?>(); |
| 486 | + var loneleyFiles = |
| 487 | + new Dictionary<FileName, (FileInfo? MetadataInfo, FileInfo? ResponseInfo)>(); |
486 | 488 | foreach (var fileInfo in _rootDirectory.GetFiles("*.*", SearchOption.AllDirectories)) |
487 | 489 | { |
488 | 490 | var fileName = FileName.FromFileInfo(fileInfo); |
489 | 491 | if (fileName.IsMetadataFile) |
490 | 492 | { |
491 | 493 | var responseFileName = fileName.ToResponseFileName(); |
492 | | - if (!orphanedResponseFiles.Remove(responseFileName)) |
| 494 | + if (loneleyFiles.Remove(responseFileName)) |
493 | 495 | { |
494 | | - // Response file is not yet iterated |
495 | | - orphanedResponseFiles.Add(responseFileName, null); |
| 496 | + // Response file was already iterated |
| 497 | + continue; |
496 | 498 | } |
| 499 | + |
| 500 | + loneleyFiles.Add(responseFileName, (MetadataInfo: fileInfo, ResponseInfo: null)); |
497 | 501 | } |
498 | 502 | else if (fileName.IsResponseFile) |
499 | 503 | { |
500 | | - // It may be that we have just moved the response from temp to here, and that the metadata is about to follow, |
501 | | - // see ResponseFilePair.TryMakePermanent(). |
502 | | - // In that is the case, then we must not consider the response as orphaned, even if no metadata is present. |
503 | | - // We therefore only delete response files that were written way in the past with a safe margin. |
504 | | - |
505 | | - if (fileInfo.CreationTimeUtc < removeBefore) |
| 504 | + if (loneleyFiles.Remove(fileName)) |
506 | 505 | { |
507 | | - if (!orphanedResponseFiles.TryAdd(fileName, fileInfo)) |
508 | | - { |
509 | | - // Metadata file was already iterated |
510 | | - orphanedResponseFiles.Remove(fileName); |
511 | | - } |
| 506 | + // Metadata file was already iterated |
| 507 | + continue; |
512 | 508 | } |
| 509 | + |
| 510 | + loneleyFiles.Add(fileName, (MetadataInfo: null, ResponseInfo: fileInfo)); |
513 | 511 | } |
514 | 512 | } |
515 | 513 |
|
516 | | - foreach (var (fileName, orphanedResponseFileInfo) in orphanedResponseFiles) |
| 514 | + foreach (var (responseFileName, loneley) in loneleyFiles) |
517 | 515 | { |
518 | | - if (orphanedResponseFileInfo is null) |
| 516 | + var fileInfo = loneley.MetadataInfo ?? loneley.ResponseInfo!; |
| 517 | + |
| 518 | + // It may be that we have just moved the response from temp to here, and that the metadata is about to follow, |
| 519 | + // see ResponseFilePair.TryMakePermanent(). |
| 520 | + // In that is the case, then we must not consider the response as orphaned, even if no metadata is present. |
| 521 | + // We therefore only delete response files that were written way in the past with a safe margin. |
| 522 | + if (fileInfo.CreationTimeUtc >= removeBefore) |
519 | 523 | { |
| 524 | + continue; |
| 525 | + } |
| 526 | + |
| 527 | + if (loneley.ResponseInfo is null) |
| 528 | + { |
| 529 | + var metadataInfo = loneley.MetadataInfo!; |
| 530 | + |
520 | 531 | throw new InvalidOperationException( |
521 | | - $"Orphaned metadata file '{fileName}' found without a corresponding response file." |
| 532 | + $"Orphaned metadata file '{metadataInfo.Name}' found without the response file actually being present." |
522 | 533 | ); |
523 | 534 | } |
524 | 535 |
|
525 | 536 | try |
526 | 537 | { |
527 | | - orphanedResponseFileInfo.Delete(); |
| 538 | + loneley.ResponseInfo.Delete(); |
528 | 539 | } |
529 | 540 | catch { } |
530 | 541 | } |
|
0 commit comments