Skip to content

Commit c6d7a8b

Browse files
committed
Simplify removal of orphaned response files
1 parent bd22813 commit c6d7a8b

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

src/HttpClient.Cache/Files/FileCache.cs

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.IO;
12
using System.Net;
23
using System.Reflection;
34

@@ -482,49 +483,59 @@ public void Purge()
482483
private void RemoveOrphanedResponseFiles(bool force)
483484
{
484485
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)>();
486488
foreach (var fileInfo in _rootDirectory.GetFiles("*.*", SearchOption.AllDirectories))
487489
{
488490
var fileName = FileName.FromFileInfo(fileInfo);
489491
if (fileName.IsMetadataFile)
490492
{
491493
var responseFileName = fileName.ToResponseFileName();
492-
if (!orphanedResponseFiles.Remove(responseFileName))
494+
if (loneleyFiles.Remove(responseFileName))
493495
{
494-
// Response file is not yet iterated
495-
orphanedResponseFiles.Add(responseFileName, null);
496+
// Response file was already iterated
497+
continue;
496498
}
499+
500+
loneleyFiles.Add(responseFileName, (MetadataInfo: fileInfo, ResponseInfo: null));
497501
}
498502
else if (fileName.IsResponseFile)
499503
{
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))
506505
{
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;
512508
}
509+
510+
loneleyFiles.Add(fileName, (MetadataInfo: null, ResponseInfo: fileInfo));
513511
}
514512
}
515513

516-
foreach (var (fileName, orphanedResponseFileInfo) in orphanedResponseFiles)
514+
foreach (var (responseFileName, loneley) in loneleyFiles)
517515
{
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)
519523
{
524+
continue;
525+
}
526+
527+
if (loneley.ResponseInfo is null)
528+
{
529+
var metadataInfo = loneley.MetadataInfo!;
530+
520531
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."
522533
);
523534
}
524535

525536
try
526537
{
527-
orphanedResponseFileInfo.Delete();
538+
loneley.ResponseInfo.Delete();
528539
}
529540
catch { }
530541
}

0 commit comments

Comments
 (0)