Files
cleanuperr/code/Infrastructure/Verticals/Files/HardlinkFileService.cs
T
2025-02-26 22:17:31 +02:00

49 lines
1.6 KiB
C#

using System.Runtime.InteropServices;
using Microsoft.Extensions.Logging;
namespace Infrastructure.Verticals.Files;
public class HardlinkFileService : IHardlinkFileService
{
private readonly ILogger<HardlinkFileService> _logger;
private readonly UnixHardlinkFileService _unixHardlinkFileService;
private readonly WindowsHardlinkFileService _windowsHardlinkFileService;
public HardlinkFileService(
ILogger<HardlinkFileService> logger,
UnixHardlinkFileService unixHardlinkFileService,
WindowsHardlinkFileService windowsHardlinkFileService
)
{
_logger = logger;
_unixHardlinkFileService = unixHardlinkFileService;
_windowsHardlinkFileService = windowsHardlinkFileService;
}
public void PopulateInodeCounts(string directoryPath)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
_windowsHardlinkFileService.PopulateFileIndexCounts(directoryPath);
return;
}
_unixHardlinkFileService.PopulateInodeCounts(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.GetWindowsHardLinkCount(filePath, ignoreRootDir);
}
return _unixHardlinkFileService.GetHardlinkCount(filePath, ignoreRootDir);
}
}