diff --git a/LightlessSync/FileCache/CacheMonitor.cs b/LightlessSync/FileCache/CacheMonitor.cs index c724225..95c3150 100644 --- a/LightlessSync/FileCache/CacheMonitor.cs +++ b/LightlessSync/FileCache/CacheMonitor.cs @@ -126,7 +126,7 @@ public sealed class CacheMonitor : DisposableMediatorSubscriberBase Logger.LogWarning("Lightless file path is not set, cannot start the FSW for Lightless."); return; } - var fsType = FileSystemHelper.GetFilesystemType(_configService.Current.CacheFolder); + var fsType = FileSystemHelper.GetFilesystemType(_configService.Current.CacheFolder, _dalamudUtil.IsWine); if (fsType == FileSystemHelper.FilesystemType.NTFS) { diff --git a/LightlessSync/FileCache/FileCompactor.cs b/LightlessSync/FileCache/FileCompactor.cs index e961fbd..d1aa60a 100644 --- a/LightlessSync/FileCache/FileCompactor.cs +++ b/LightlessSync/FileCache/FileCompactor.cs @@ -4,6 +4,7 @@ using LightlessSync.Utils; using Microsoft.Extensions.Logging; using System.Collections.Concurrent; using System.Diagnostics; +using System.IO; using System.Runtime.InteropServices; using System.Threading.Channels; using static LightlessSync.Utils.FileSystemHelper; @@ -88,7 +89,7 @@ public sealed class FileCompactor : IDisposable public long GetFileSizeOnDisk(FileInfo fileInfo) { - var fsType = GetFilesystemType(fileInfo.FullName); + var fsType = GetFilesystemType(fileInfo.FullName, _dalamudUtilService.IsWine); if (fsType != FilesystemType.Btrfs && fsType != FilesystemType.NTFS) { @@ -216,7 +217,7 @@ public sealed class FileCompactor : IDisposable return; } - var fsType = GetFilesystemType(filePath); + var fsType = GetFilesystemType(filePath, _dalamudUtilService.IsWine); var oldSize = fi.Length; int clusterSize = GetClusterSize(fi); @@ -252,7 +253,7 @@ public sealed class FileCompactor : IDisposable } // BTRFS Compression - if (fsType == FileSystemHelper.FilesystemType.Btrfs) + if (fsType == FilesystemType.Btrfs) { if (!IsBtrfsCompressedFile(filePath)) { @@ -317,20 +318,18 @@ public sealed class FileCompactor : IDisposable private void DecompressFile(string path) { _logger.LogDebug("Removing compression from {file}", path); - var fsType = GetFilesystemType(path); + var fsType = GetFilesystemType(path, _dalamudUtilService.IsWine); //NTFS Decompression if (fsType == FilesystemType.NTFS && !_dalamudUtilService.IsWine) { try { - using (var fs = new FileStream(path, FileMode.Open)) - { + using var fs = new FileStream(path, FileMode.Open); #pragma warning disable S3869 // "SafeHandle.DangerousGetHandle" should not be called - var hDevice = fs.SafeFileHandle.DangerousGetHandle(); + var hDevice = fs.SafeFileHandle.DangerousGetHandle(); #pragma warning restore S3869 // "SafeHandle.DangerousGetHandle" should not be called - _ = DeviceIoControl(hDevice, FSCTL_DELETE_EXTERNAL_BACKING, nint.Zero, 0, nint.Zero, 0, out _, out _); - } + _ = DeviceIoControl(hDevice, FSCTL_DELETE_EXTERNAL_BACKING, nint.Zero, 0, nint.Zero, 0, out _, out _); } catch (Exception ex) { @@ -380,7 +379,6 @@ public sealed class FileCompactor : IDisposable } else { - // Log output only in debug mode to avoid clutter if (!string.IsNullOrWhiteSpace(stdout)) _logger.LogDebug("btrfs defragment output for {file}: {out}", path, stdout.Trim()); @@ -414,7 +412,7 @@ public sealed class FileCompactor : IDisposable int clusterSize; - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !_dalamudUtilService.IsWine) { int result = GetDiskFreeSpaceW( root, @@ -602,7 +600,7 @@ public sealed class FileCompactor : IDisposable { var parts = line.Split(' '); if (parts.Length < 4) continue; - var mountPoint = parts[1].Replace("\\040", " ", StringComparison.Ordinal); // unescape spaces + var mountPoint = parts[1].Replace("\\040", " ", StringComparison.Ordinal); string normalized; try { normalized = Path.GetFullPath(mountPoint); } catch { normalized = mountPoint; } @@ -635,7 +633,7 @@ public sealed class FileCompactor : IDisposable if (!_pendingCompactions.TryAdd(filePath, 0)) return; - var fsType = GetFilesystemType(filePath); + var fsType = GetFilesystemType(filePath, _dalamudUtilService.IsWine); if (fsType != FilesystemType.NTFS && fsType != FilesystemType.Btrfs) { @@ -700,7 +698,7 @@ public sealed class FileCompactor : IDisposable } catch (OperationCanceledException) { - // expected during shutdown + _logger.LogDebug("Queue has been cancelled by token"); } } } diff --git a/LightlessSync/Utils/FileSystemHelper.cs b/LightlessSync/Utils/FileSystemHelper.cs index a80c900..a5bb427 100644 --- a/LightlessSync/Utils/FileSystemHelper.cs +++ b/LightlessSync/Utils/FileSystemHelper.cs @@ -21,12 +21,12 @@ namespace LightlessSync.Utils private static readonly ConcurrentDictionary _filesystemTypeCache = new(StringComparer.OrdinalIgnoreCase); - public static FilesystemType GetFilesystemType(string filePath) + public static FilesystemType GetFilesystemType(string filePath, bool isWine = false) { try { string rootPath; - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && (!IsProbablyWine() || !isWine)) { var info = new FileInfo(filePath); var dir = info.Directory ?? new DirectoryInfo(filePath); @@ -44,7 +44,7 @@ namespace LightlessSync.Utils FilesystemType detected; - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && (!IsProbablyWine() || !isWine)) { var root = new DriveInfo(rootPath); var format = root.DriveFormat?.ToUpperInvariant() ?? string.Empty; @@ -139,5 +139,7 @@ namespace LightlessSync.Utils return FilesystemType.Unknown; } } + + private static bool IsProbablyWine() => Environment.GetEnvironmentVariable("WINELOADERNOEXEC") != null || Environment.GetEnvironmentVariable("WINEDLLPATH") != null || Directory.Exists("/proc/self") && File.Exists("/proc/mounts"); } }