Files
HashTool/src/HashTool.Core/Hashing/HashEngine.cs
T
poli0981andClaude Opus 5.5 9c91f5f01d fix: findings of the first Qodana analysis
High-severity findings in the shipped code:
- HashEngine disposes exactly the hash functions it created (a list instead of an array with null checks).
- SingleInstance allocates the pipe header once, not on the stack in every retry, and AllowSetForegroundWindow's
  unused result is no longer declared.
- Lambdas whose first parameter was named "_" assigned tasks to that parameter instead of discarding them
  (UnhandledException handler, settings save timer).
- ChecksumWriter sets the writer's NewLine after the using declaration; LocalizationService raises the indexer change
  without a caller-info argument; VerifyCommand's Outcome drops a field nobody read.
- Nullability: converters return non-null values, the language combo box may set null, FocusManager is never null.
- Redundant usings (global usings of the app), qualifiers and default arguments; C# 14 lambda parameters.
- FocusHandOff, RowTabNavigation and HighlightTextBlock move to Views/Controls, matching their namespace.

qodana.yaml analyses the shipped code (tests and benchmarks are covered by the build, analyzers and SonarCloud),
leaves Roslyn analyzer suggestions to the build, documents each remaining exclusion, and fails on critical or high
findings.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 09:17:31 +07:00

142 lines
5.9 KiB
C#

using System;
using System.Buffers;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace HashTool.Core.Hashing;
/// <summary>Tuning knobs for <see cref="HashEngine"/>.</summary>
public sealed record HashEngineOptions
{
/// <summary>Read size for files smaller than <see cref="LargeFileThreshold"/>.</summary>
public int BufferSize { get; init; } = ByteSize.OneMB;
/// <summary>Read size for large files; bigger reads cut syscalls and let BLAKE3 hash chunks in parallel.</summary>
public int LargeFileBufferSize { get; init; } = 4 * ByteSize.OneMB;
public long LargeFileThreshold { get; init; } = ByteSize.OneGB;
/// <summary>Chunks at least this large are hashed with BLAKE3's multi-threaded mode.</summary>
public int Blake3ParallelThreshold { get; init; } = 512 * ByteSize.OneKB;
public static HashEngineOptions Default { get; } = new();
}
/// <summary>
/// Computes file digests. The file is read once no matter how many algorithms are requested, and the next block is read
/// while the current one is being hashed so that disk and CPU work overlap.
/// </summary>
public sealed class HashEngine(HashEngineOptions? options = null)
{
private readonly HashEngineOptions _options = options ?? HashEngineOptions.Default;
public HashEngineOptions Options => _options;
public async Task<HashResult> ComputeFileAsync(string path, HashType algorithm,
Action<long>? onBytesRead = null, CancellationToken cancellationToken = default)
{
var results = await ComputeFileAsync(path, [algorithm], onBytesRead, cancellationToken).ConfigureAwait(false);
return results[0];
}
public async Task<HashResult[]> ComputeFileAsync(string path, IReadOnlyList<HashType> algorithms,
Action<long>? onBytesRead = null, CancellationToken cancellationToken = default)
{
ArgumentException.ThrowIfNullOrEmpty(path);
cancellationToken.ThrowIfCancellationRequested();
// Allow other processes to keep the file open for writing or deletion: refusing to hash a file just because a
// downloader or editor still has a handle produced most of the "file in use" errors in 1.x.
var stream = new FileStream(path, new FileStreamOptions
{
Mode = FileMode.Open,
Access = FileAccess.Read,
Share = FileShare.ReadWrite | FileShare.Delete,
BufferSize = 0,
Options = FileOptions.Asynchronous | FileOptions.SequentialScan
});
await using var disposeStream = stream.ConfigureAwait(false);
var bufferSize = stream.Length >= _options.LargeFileThreshold ? _options.LargeFileBufferSize : _options.BufferSize;
return await ComputeStreamAsync(stream, algorithms, bufferSize, onBytesRead, cancellationToken).ConfigureAwait(false);
}
public Task<HashResult[]> ComputeStreamAsync(Stream stream, IReadOnlyList<HashType> algorithms,
Action<long>? onBytesRead = null, CancellationToken cancellationToken = default) =>
ComputeStreamAsync(stream, algorithms, _options.BufferSize, onBytesRead, cancellationToken);
private async Task<HashResult[]> ComputeStreamAsync(Stream stream, IReadOnlyList<HashType> algorithms, int bufferSize,
Action<long>? onBytesRead, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(stream);
ArgumentNullException.ThrowIfNull(algorithms);
if (algorithms.Count == 0) throw new ArgumentException("At least one algorithm is required.", nameof(algorithms));
// Only the functions created so far are disposed if creating one of them fails.
var functions = new List<HashFunction>(algorithms.Count);
var current = ArrayPool<byte>.Shared.Rent(bufferSize);
var next = ArrayPool<byte>.Shared.Rent(bufferSize);
ValueTask<int> pendingRead = default;
var readInFlight = false;
try
{
foreach (var algorithm in algorithms)
functions.Add(HashFunction.Create(algorithm, _options.Blake3ParallelThreshold));
pendingRead = stream.ReadAsync(current.AsMemory(0, bufferSize), cancellationToken);
readInFlight = true;
while (true)
{
var read = await pendingRead.ConfigureAwait(false);
readInFlight = false;
if (read == 0) break;
// Start reading the next block before hashing this one.
pendingRead = stream.ReadAsync(next.AsMemory(0, bufferSize), cancellationToken);
readInFlight = true;
Append(functions, current, read);
onBytesRead?.Invoke(read);
(current, next) = (next, current);
}
var results = new HashResult[functions.Count];
for (var i = 0; i < functions.Count; i++)
results[i] = new HashResult(algorithms[i], functions[i].GetHash());
return results;
}
finally
{
if (readInFlight)
{
// Never return a buffer to the pool while a read may still be writing into it.
try
{
await pendingRead.ConfigureAwait(false);
}
catch
{
// The original exception (if any) is already propagating.
}
}
foreach (var function in functions) function.Dispose();
ArrayPool<byte>.Shared.Return(current);
ArrayPool<byte>.Shared.Return(next);
}
}
private static void Append(List<HashFunction> functions, byte[] buffer, int count)
{
if (functions.Count == 1)
{
functions[0].Append(buffer.AsSpan(0, count));
return;
}
Parallel.For(0, functions.Count, i => functions[i].Append(buffer.AsSpan(0, count)));
}
}