Skip to content

Commit

Permalink
(#102) FileCache: support older versions of Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Aug 28, 2022
1 parent 5d954d6 commit 3977248
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion Emulsion.ContentProxy/FileCache.fs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,30 @@ type FileCache(logger: ILogger,
)
}

let deleteFileSafe (fileInfo: FileInfo) = async {
if OperatingSystem.IsWindows() then
// NOTE: On older versions of Windows (known to reproduce on windows-2019 GitHub Actions image), the
// following scenario may be defunct:
// - open a file with FileShare.Delete (i.e. for download)
// - delete a file (i.e. during the cache cleanup)
// - try to create a file with the same name again
//
// According to this article
// (https://boostgsoc13.github.io/boost.afio/doc/html/afio/FAQ/deleting_open_files.html), it is impossible
// to do since file will occupy its disk name until the last handle is closed.
//
// In practice, this is allowed (checked at least on Windows 10 20H2 and windows-2022 GitHub Actions image),
// but is known to be broken on older versions of Windows (windows-2019).
//
// As a workaround, let's rename the file to a random name before deleting it.
//
// This workaround may be removed after these older versions of Windows goes out of support.
fileInfo.MoveTo(Path.Combine(fileInfo.DirectoryName, $"{Guid.NewGuid().ToString()}.deleted"))
fileInfo.Delete()
else
fileInfo.Delete()
}

let ensureFreeCache size = async {
if size > settings.FileSizeLimitBytes || size > settings.TotalCacheSizeLimitBytes then
return false
Expand All @@ -106,7 +130,7 @@ type FileCache(logger: ILogger,
currentSize <- currentSize + Checked.uint64 info.Length
if currentSize + size > settings.TotalCacheSizeLimitBytes then
logger.Information("Deleting a cache item \"{FileName}\" ({Size} bytes)", info.Name, info.Length)
info.Delete()
do! deleteFileSafe info

return true
}
Expand Down

0 comments on commit 3977248

Please sign in to comment.