using System.Runtime.InteropServices; using Microsoft.Extensions.Logging; namespace Cleanuparr.Infrastructure.Features.Files; public class HardLinkFileService : IHardLinkFileService { private readonly ILogger _logger; private readonly IUnixHardLinkFileService _unixHardLinkFileService; private readonly IWindowsHardLinkFileService _windowsHardLinkFileService; public HardLinkFileService( ILogger logger, IUnixHardLinkFileService unixHardLinkFileService, IWindowsHardLinkFileService windowsHardLinkFileService ) { _logger = logger; _unixHardLinkFileService = unixHardLinkFileService; _windowsHardLinkFileService = windowsHardLinkFileService; } public void PopulateFileCounts(string directoryPath) { _logger.LogTrace("populating file counts from {dir}", directoryPath); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { _windowsHardLinkFileService.PopulateFileCounts(directoryPath); return; } _unixHardLinkFileService.PopulateFileCounts(directoryPath); } public void PopulateFileCounts(IEnumerable directoryPaths) { foreach (var directoryPath in directoryPaths.Where(d => !string.IsNullOrEmpty(d))) { PopulateFileCounts(directoryPath); } } public long GetHardLinkCount(string filePath, bool ignoreRootDir) { if (!File.Exists(filePath)) { _logger.LogDebug("file {file} does not exist", filePath); return -1; } if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { return _windowsHardLinkFileService.GetHardLinkCount(filePath, ignoreRootDir); } return _unixHardLinkFileService.GetHardLinkCount(filePath, ignoreRootDir); } }