Skip to content

Commit 4f242ef

Browse files
committed
(#747) Switch CryptoHashProvider to filestream for hashing files
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.
1 parent 18d7948 commit 4f242ef

File tree

4 files changed

+8
-118
lines changed

4 files changed

+8
-118
lines changed

src/chocolatey.tests/chocolatey.tests.csproj

-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@
121121
<Compile Include="infrastructure\commands\CommandExecutorSpecs.cs" />
122122
<Compile Include="infrastructure\commands\PowershellExecutorSpecs.cs" />
123123
<Compile Include="infrastructure\configuration\ConfigSpecs.cs" />
124-
<Compile Include="infrastructure\cryptography\CrytpoHashProviderSpecs.cs" />
125124
<Compile Include="infrastructure\events\context\FakeEvent.cs" />
126125
<Compile Include="infrastructure\events\context\FakeSubscriber.cs" />
127126
<Compile Include="infrastructure\events\EventSubscriptionManagerSpecs.cs" />

src/chocolatey.tests/infrastructure/cryptography/CrytpoHashProviderSpecs.cs

-110
This file was deleted.

src/chocolatey/infrastructure.app/ApplicationParameters.cs

-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ public static class Environment
136136
public static readonly string ConfigFileTransformExtension = ".install.xdt";
137137
public static readonly string[] ShimDirectorFileExtensions = new string[] {".gui",".ignore"};
138138

139-
public static readonly string HashProviderFileTooBig = "UnableToDetectChanges_FileTooBig";
140139
public static readonly string HashProviderFileLocked = "UnableToDetectChanges_FileLocked";
141140

142141
/// <summary>

src/chocolatey/infrastructure/cryptography/CryptoHashProvider.cs

+8-6
Original file line numberDiff line numberDiff line change
@@ -90,21 +90,23 @@ public string hash_file(string filePath)
9090

9191
try
9292
{
93-
var hash = _hashAlgorithm.ComputeHash(_fileSystem.read_file_bytes(filePath));
94-
95-
return BitConverter.ToString(hash).Replace("-", string.Empty);
93+
using (var fileStream = _fileSystem.open_file_readonly(filePath))
94+
{
95+
var hash = _hashAlgorithm.ComputeHash(fileStream);
96+
return BitConverter.ToString(hash).Replace("-", string.Empty);
97+
}
9698
}
9799
catch (IOException ex)
98100
{
99-
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}".format_with(filePath, Environment.NewLine, ex.Message));
101+
this.Log().Warn(() => "Error computing hash for '{0}'{1} Hash will be special code for locked file {1} Captured error:{1} {2}".format_with(filePath, Environment.NewLine, ex.Message));
100102

101103
if (file_is_locked(ex))
102104
{
103105
return ApplicationParameters.HashProviderFileLocked;
104106
}
105107

106-
//IO.IO_FileTooLong2GB (over Int32.MaxValue)
107-
return ApplicationParameters.HashProviderFileTooBig;
108+
//Rethrow if file is not locked
109+
throw;
108110
}
109111
}
110112

0 commit comments

Comments
 (0)