Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support cancellation in GetFileHash and VerifyFileHash #9966

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions src/Tasks/FileIO/GetFileHash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Build.Framework;
using Microsoft.Build.Shared;
Expand All @@ -17,7 +18,7 @@ namespace Microsoft.Build.Tasks
/// <summary>
/// Computes the checksum for a single file.
/// </summary>
public sealed class GetFileHash : TaskExtension
public sealed class GetFileHash : TaskExtension, ICancelableTask
{
internal const string _defaultFileHashAlgorithm = "SHA256";
internal const string _hashEncodingHex = "hex";
Expand Down Expand Up @@ -77,8 +78,10 @@ public override bool Execute()
return false;
}

var parallelOptions = new ParallelOptions() { CancellationToken = _cancellationTokenSource.Token };

var writeLock = new object();
Parallel.For(0, Files.Length, index =>
Parallel.For(0, Files.Length, parallelOptions, index =>
{
var file = Files[index];

Expand All @@ -88,7 +91,7 @@ public override bool Execute()
return;
}

var hash = ComputeHash(algorithmFactory, file.ItemSpec);
var hash = ComputeHash(algorithmFactory, file.ItemSpec, _cancellationTokenSource.Token);
var encodedHash = EncodeHash(encoding, hash);

lock (writeLock)
Expand All @@ -115,6 +118,13 @@ public override bool Execute()
return !Log.HasLoggedErrors;
}

private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
hickford marked this conversation as resolved.
Show resolved Hide resolved

public void Cancel()
{
_cancellationTokenSource.Cancel();
}

internal static string EncodeHash(HashEncoding encoding, byte[] hash)
{
return encoding switch
Expand All @@ -128,12 +138,16 @@ internal static string EncodeHash(HashEncoding encoding, byte[] hash)
internal static bool TryParseHashEncoding(string value, out HashEncoding encoding)
=> Enum.TryParse<HashEncoding>(value, /*ignoreCase:*/ true, out encoding);

internal static byte[] ComputeHash(Func<HashAlgorithm> algorithmFactory, string filePath)
internal static byte[] ComputeHash(Func<HashAlgorithm> algorithmFactory, string filePath, CancellationToken ct)
{
using (var stream = File.OpenRead(filePath))
using (var algorithm = algorithmFactory())
{
#if NET5_0_OR_GREATER
return algorithm.ComputeHashAsync(stream, ct).Result;
#else
return algorithm.ComputeHash(stream);
#endif
}
}
}
Expand Down
12 changes: 10 additions & 2 deletions src/Tasks/FileIO/VerifyFileHash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using Microsoft.Build.Framework;
using Microsoft.Build.Shared.FileSystem;
using System.Threading;

#nullable disable

Expand All @@ -12,7 +13,7 @@ namespace Microsoft.Build.Tasks
/// <summary>
/// Verifies that a file matches the expected file hash.
/// </summary>
public sealed class VerifyFileHash : TaskExtension
public sealed class VerifyFileHash : TaskExtension, ICancelableTask
{
/// <summary>
/// The file path.
Expand Down Expand Up @@ -56,7 +57,7 @@ public override bool Execute()
return false;
}

byte[] hash = GetFileHash.ComputeHash(algorithmFactory, File);
byte[] hash = GetFileHash.ComputeHash(algorithmFactory, File, _cancellationTokenSource.Token);
string actualHash = GetFileHash.EncodeHash(encoding, hash);
var comparison = encoding == Tasks.HashEncoding.Hex
? StringComparison.OrdinalIgnoreCase
Expand All @@ -69,5 +70,12 @@ public override bool Execute()

return !Log.HasLoggedErrors;
}

private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();

public void Cancel()
{
_cancellationTokenSource.Cancel();
}
}
}
Loading