diff --git a/LightlessSyncServer/LightlessSyncStaticFilesServer/Services/CachedFileProvider.cs b/LightlessSyncServer/LightlessSyncStaticFilesServer/Services/CachedFileProvider.cs index d63c001..fbc4ce6 100644 --- a/LightlessSyncServer/LightlessSyncStaticFilesServer/Services/CachedFileProvider.cs +++ b/LightlessSyncServer/LightlessSyncStaticFilesServer/Services/CachedFileProvider.cs @@ -102,36 +102,37 @@ public sealed class CachedFileProvider : IDisposable private bool TryCopyFromColdStorage(string hash, string destinationFilePath) { - if (!_configuration.GetValueOrDefault(nameof(StaticFilesServerConfiguration.UseColdStorage), false)) return false; + if (!_configuration.GetValueOrDefault(nameof(StaticFilesServerConfiguration.UseColdStorage), false)) return false; - string coldStorageDir = _configuration.GetValueOrDefault(nameof(StaticFilesServerConfiguration.ColdStorageDirectory), string.Empty); - if (string.IsNullOrEmpty(coldStorageDir)) return false; + string coldStorageDir = _configuration.GetValueOrDefault(nameof(StaticFilesServerConfiguration.ColdStorageDirectory), string.Empty); + if (string.IsNullOrEmpty(coldStorageDir)) return false; - var coldStorageFilePath = FilePathUtil.GetFileInfoForHash(coldStorageDir, hash); - if (coldStorageFilePath == null) return false; + var coldStorageFilePath = FilePathUtil.GetFileInfoForHash(coldStorageDir, hash); + if (coldStorageFilePath == null) return false; - try - { - _logger.LogDebug("Copying {hash} from cold storage: {path}", hash, coldStorageFilePath); - var tempFileName = destinationFilePath + ".dl"; - File.Copy(coldStorageFilePath.FullName, tempFileName, true); - File.Move(tempFileName, destinationFilePath, true); - coldStorageFilePath.LastAccessTimeUtc = DateTime.UtcNow; - var destinationFile = new FileInfo(destinationFilePath); - destinationFile.LastAccessTimeUtc = DateTime.UtcNow; - destinationFile.CreationTimeUtc = DateTime.UtcNow; - destinationFile.LastWriteTimeUtc = DateTime.UtcNow; - _metrics.IncGauge(MetricsAPI.GaugeFilesTotal); - _metrics.IncGauge(MetricsAPI.GaugeFilesTotalSize, new FileInfo(destinationFilePath).Length); - return true; - } - catch (Exception ex) - { - _logger.LogWarning(ex, "Could not copy {coldStoragePath} from cold storage", coldStorageFilePath); - } + try + { + _logger.LogDebug("Copying {hash} from cold storage: {path}", hash, coldStorageFilePath); + var tempFileName = destinationFilePath + ".dl"; + File.Copy(coldStorageFilePath.FullName, tempFileName, true); + File.Move(tempFileName, destinationFilePath, true); - return false; - } + File.SetLastAccessTimeUtc(coldStorageFilePath.FullName, DateTime.UtcNow); + File.SetLastAccessTimeUtc(destinationFilePath, DateTime.UtcNow); + File.SetCreationTimeUtc(destinationFilePath, DateTime.UtcNow); + File.SetLastWriteTimeUtc(destinationFilePath, DateTime.UtcNow); + + _metrics.IncGauge(MetricsAPI.GaugeFilesTotal); + _metrics.IncGauge(MetricsAPI.GaugeFilesTotalSize, new FileInfo(destinationFilePath).Length); + return true; + } + catch (Exception ex) + { + _logger.LogWarning(ex, "Could not copy {coldStoragePath} from cold storage", coldStorageFilePath); + } + + return false; + } public async Task DownloadFileWhenRequired(string hash) { @@ -170,15 +171,22 @@ public sealed class CachedFileProvider : IDisposable public FileInfo? GetLocalFilePath(string hash) { - var fi = FilePathUtil.GetFileInfoForHash(_hotStoragePath, hash); - if (fi == null) return null; + var fi = FilePathUtil.GetFileInfoForHash(_hotStoragePath, hash); + if (fi == null) return null; - fi.LastAccessTimeUtc = DateTime.UtcNow; + try + { + File.SetLastAccessTimeUtc(fi.FullName, DateTime.UtcNow); + } + catch (Exception ex) + { + _logger.LogWarning(ex, "Failed to update LastAccessTimeUtc for file {hash}", hash); + } - _fileStatisticsService.LogFile(hash, fi.Length); + _fileStatisticsService.LogFile(hash, fi.Length); - return new FileInfo(fi.FullName); - } + return new FileInfo(fi.FullName); + } public async Task DownloadAndGetLocalFileInfo(string hash) {