Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
Improve SHA1 speed and checksum exception
Browse files Browse the repository at this point in the history
  • Loading branch information
caesay committed Dec 20, 2023
1 parent 8acbd96 commit 7ea1ea6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
15 changes: 14 additions & 1 deletion src/Squirrel/Compression/ChecksumFailedException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@ public class ChecksumFailedException : Exception
/// <summary>
/// The filename of the package which failed validation
/// </summary>
public string Filename { get; set; }
public string FilePath { get; }

/// <inheritdoc cref="ChecksumFailedException"/>
public ChecksumFailedException(string filePath)
: this(filePath, "Checksum failed")
{
}

/// <inheritdoc cref="ChecksumFailedException"/>
public ChecksumFailedException(string filePath, string message)
: base(message + $" ({filePath})")
{
FilePath = filePath;
}
}
}
4 changes: 2 additions & 2 deletions src/Squirrel/Compression/DeltaPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,12 @@ void verifyPatchedFile(string relativeFilePath, string inputFile, string tempTar

if (expectedReleaseEntry.Filesize != actualReleaseEntry.Filesize) {
_log.Warn($"Patched file {relativeFilePath} has incorrect size, expected {expectedReleaseEntry.Filesize}, got {actualReleaseEntry.Filesize}");
throw new ChecksumFailedException() { Filename = relativeFilePath };
throw new ChecksumFailedException(relativeFilePath);
}

if (expectedReleaseEntry.SHA1 != actualReleaseEntry.SHA1) {
_log.Warn($"Patched file {relativeFilePath} has incorrect SHA1, expected {expectedReleaseEntry.SHA1}, got {actualReleaseEntry.SHA1}");
throw new ChecksumFailedException() { Filename = relativeFilePath };
throw new ChecksumFailedException(relativeFilePath);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Squirrel/Internal/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ public static IEnumerable<string> GetAllFilePathsRecursively(string rootPath)
public static string CalculateFileSHA1(string filePath)
{
Contract.Requires(filePath != null);

using (var stream = File.OpenRead(filePath)) {
var bufferSize = 1000000; // 1mb
using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize)) {
return CalculateStreamSHA1(stream);
}
}
Expand Down

0 comments on commit 7ea1ea6

Please sign in to comment.