Skip to content

Commit

Permalink
(#747) Switch CryptoHashProvider to filestream for hashing files
Browse files Browse the repository at this point in the history
Previously, when getting the checksum of a file, this read the file
into a byte array to pass to ComputeHash. However, this was limited to
files of 2gb or less, and was not efficent memory wise. This changes
the method to using a filestream which is passed to ComputeHash, which
should allow arbitrary file sizes with better memory usage.

The unit test for this method had to be removed because the mock
filesystem does not allow opening a file stream.
  • Loading branch information
TheCakeIsNaOH committed Jun 15, 2023
1 parent 06acb28 commit 3292e4b
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 118 deletions.
1 change: 0 additions & 1 deletion src/chocolatey.tests/chocolatey.tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@
<Compile Include="infrastructure\commands\CommandExecutorSpecs.cs" />
<Compile Include="infrastructure\commands\PowershellExecutorSpecs.cs" />
<Compile Include="infrastructure\configuration\ConfigSpecs.cs" />
<Compile Include="infrastructure\cryptography\CryptoHashProviderSpecs.cs" />
<Compile Include="infrastructure\events\context\FakeEvent.cs" />
<Compile Include="infrastructure\events\context\FakeSubscriber.cs" />
<Compile Include="infrastructure\events\EventSubscriptionManagerSpecs.cs" />
Expand Down

This file was deleted.

1 change: 0 additions & 1 deletion src/chocolatey/infrastructure.app/ApplicationParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ public static class Environment
public static readonly string ConfigFileTransformExtension = ".install.xdt";
public static readonly string[] ShimDirectorFileExtensions = new string[] {".gui",".ignore"};

public static readonly string HashProviderFileTooBig = "UnableToDetectChanges_FileTooBig";
public static readonly string HashProviderFileLocked = "UnableToDetectChanges_FileLocked";

/// <summary>
Expand Down
14 changes: 8 additions & 6 deletions src/chocolatey/infrastructure/cryptography/CryptoHashProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,23 @@ public string ComputeFileHash(string filePath)

try
{
var hash = _hashAlgorithm.ComputeHash(_fileSystem.ReadFileBytes(filePath));

return BitConverter.ToString(hash).Replace("-", string.Empty);
using (var fileStream = _fileSystem.open_file_readonly(filePath))
{
var hash = _hashAlgorithm.ComputeHash(fileStream);
return BitConverter.ToString(hash).Replace("-", string.Empty);
}
}
catch (IOException ex)
{
this.Log().Warn(() => "Error computing hash for '{0}'{1} Hash will be special code for locked file or file too big instead.{1} Captured error:{1} {2}".FormatWith(filePath, Environment.NewLine, ex.Message));
this.Log().Warn(() => "Error computing hash for '{0}'{1} Hash will be special code for locked file {1} Captured error:{1} {2}".FormatWith(filePath, Environment.NewLine, ex.Message));

if (IsFileLocked(ex))
{
return ApplicationParameters.HashProviderFileLocked;
}

//IO.IO_FileTooLong2GB (over Int32.MaxValue)
return ApplicationParameters.HashProviderFileTooBig;
//Rethrow if file is not locked
throw;
}
}

Expand Down

0 comments on commit 3292e4b

Please sign in to comment.