Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -126,49 +126,10 @@ protected override void ProcessRecord()

foreach (string path in pathsToProcess)
{
byte[] bytehash = null;
string hash = null;
Stream openfilestream = null;

try
if (ComputeFileHash(path, out string hash))
{
openfilestream = File.OpenRead(path);
bytehash = hasher.ComputeHash(openfilestream);

hash = BitConverter.ToString(bytehash).Replace("-", string.Empty);
WriteHashResult(Algorithm, hash, path);
}
catch (FileNotFoundException ex)
{
var errorRecord = new ErrorRecord(
ex,
"FileNotFound",
ErrorCategory.ObjectNotFound,
path);
WriteError(errorRecord);
}
catch (UnauthorizedAccessException ex)
{
var errorRecord = new ErrorRecord(
ex,
"UnauthorizedAccessError",
ErrorCategory.InvalidData,
path);
WriteError(errorRecord);
}
catch (IOException ioException)
{
var errorRecord = new ErrorRecord(
ioException,
"FileReadError",
ErrorCategory.ReadError,
path);
WriteError(errorRecord);
}
finally
{
openfilestream?.Dispose();
}
}
}

Expand All @@ -190,6 +151,61 @@ protected override void EndProcessing()
}
}

/// <summary>
/// Read the file and calculate the hash.
/// </summary>
/// <param name="path">Path to file which will be hashed.</param>
/// <param name="hash">Will contain the hash of the file content.</param>
/// <returns>Boolean value indicating whether the hash calculation succeeded or failed.</returns>
private bool ComputeFileHash(string path, out string hash)
{
byte[] bytehash = null;
Stream openfilestream = null;

hash = null;

try
{
openfilestream = File.OpenRead(path);

bytehash = hasher.ComputeHash(openfilestream);
hash = BitConverter.ToString(bytehash).Replace("-", string.Empty);
}
catch (FileNotFoundException ex)
{
var errorRecord = new ErrorRecord(
ex,
"FileNotFound",
ErrorCategory.ObjectNotFound,
path);
WriteError(errorRecord);
}
catch (UnauthorizedAccessException ex)
{
var errorRecord = new ErrorRecord(
ex,
"UnauthorizedAccessError",
ErrorCategory.InvalidData,
path);
WriteError(errorRecord);
}
catch (IOException ioException)
{
var errorRecord = new ErrorRecord(
ioException,
"FileReadError",
ErrorCategory.ReadError,
path);
WriteError(errorRecord);
}
finally
{
openfilestream?.Dispose();
}

return hash != null;
}

/// <summary>
/// Create FileHashInfo object and output it.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,19 @@ Describe "Get-FileHash" -Tags "CI" {
$result.Path | Should -Be $testDocument
}
}

Context "File should be closed before Get-FileHash writes pipeline output" {
It "Should be able to edit the file without 'file is in use' exceptions" {
# This test runs against a copy of the document
# because it involves renaming it,
# and that might break tests added later on.
$testDocumentCopy = "${testDocument}-copy"
Copy-Item -Path $testdocument -Destination $testDocumentCopy

$newPath = Get-FileHash -Path $testDocumentCopy | Rename-Item -NewName {$_.Hash} -PassThru
$newPath.FullName | Should -Exist

Remove-Item -Path $testDocumentCopy -Force
}
}
}