Added comments, clean-up

This commit is contained in:
cake
2025-10-29 22:54:50 +01:00
parent c37e3badf1
commit 7c4d0fd5e9
4 changed files with 45 additions and 39 deletions

View File

@@ -1,16 +1,15 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Security.Cryptography;
using System.Text;
namespace LightlessSync.Utils;
public static class Crypto
{
//This buffersize seems to be the best sweetpoint for Linux and Windows
private const int _bufferSize = 65536;
#pragma warning disable SYSLIB0021 // Type or member is obsolete
private static readonly Dictionary<(string, ushort), string> _hashListPlayersSHA256 = new();
private static readonly Dictionary<(string, ushort), string> _hashListPlayersSHA256 = [];
private static readonly Dictionary<string, string> _hashListSHA256 = new(StringComparer.Ordinal);
private static readonly SHA256CryptoServiceProvider _sha256CryptoProvider = new();
@@ -23,21 +22,21 @@ public static class Crypto
public static async Task<string> GetFileHashAsync(string filePath, CancellationToken cancellationToken = default)
{
var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete, bufferSize: 65536, options: FileOptions.Asynchronous);
var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete, bufferSize: _bufferSize, options: FileOptions.Asynchronous);
await using (stream.ConfigureAwait(false))
{
using var sha1 = SHA1.Create();
var buffer = new byte[8192];
int bytesRead;
while ((bytesRead = await stream.ReadAsync(buffer, cancellationToken).ConfigureAwait(false)) > 0)
{
sha1.TransformBlock(buffer, 0, bytesRead, outputBuffer: null, 0);
}
var buffer = new byte[8192];
int bytesRead;
while ((bytesRead = await stream.ReadAsync(buffer, cancellationToken).ConfigureAwait(false)) > 0)
{
sha1.TransformBlock(buffer, 0, bytesRead, outputBuffer: null, 0);
}
sha1.TransformFinalBlock([], 0, 0);
sha1.TransformFinalBlock([], 0, 0);
return BitConverter.ToString(sha1.Hash!).Replace("-", "", StringComparison.Ordinal);
return Convert.ToHexString(sha1.Hash!);
}
}

View File

@@ -8,17 +8,18 @@ namespace LightlessSync.Utils
public enum FilesystemType
{
Unknown = 0,
NTFS,
Btrfs,
NTFS, // Compressable
Btrfs, // Compressable
Ext4,
Xfs,
Apfs,
HfsPlus,
Fat,
Exfat,
Zfs
Zfs // Compressable
}
private const string _mountPath = "/proc/mounts";
private static readonly ConcurrentDictionary<string, FilesystemType> _filesystemTypeCache = new(StringComparer.OrdinalIgnoreCase);
public static FilesystemType GetFilesystemType(string filePath, bool isWine = false)
@@ -75,8 +76,8 @@ namespace LightlessSync.Utils
try
{
var path = Path.GetFullPath(filePath);
if (!File.Exists("/proc/mounts")) return "/";
var mounts = File.ReadAllLines("/proc/mounts");
if (!File.Exists(_mountPath)) return "/";
var mounts = File.ReadAllLines(_mountPath);
string bestMount = "/";
foreach (var line in mounts)
@@ -109,7 +110,7 @@ namespace LightlessSync.Utils
try
{
var mountPoint = GetMountPoint(filePath);
var mounts = File.ReadAllLines("/proc/mounts");
var mounts = File.ReadAllLines(_mountPath);
foreach (var line in mounts)
{
@@ -140,6 +141,7 @@ namespace LightlessSync.Utils
}
}
//Extra check on
private static bool IsProbablyWine() => Environment.GetEnvironmentVariable("WINELOADERNOEXEC") != null || Environment.GetEnvironmentVariable("WINEDLLPATH") != null || Directory.Exists("/proc/self") && File.Exists("/proc/mounts");
}
}