Skip to content

Change Get-FileHash to close file handles before writing output - #12474

Merged
Ilya (iSazonov) merged 16 commits into
PowerShell:masterfrom
HumanEquivalentUnit:master
Apr 30, 2020
Merged

Ilya (iSazonov) merged 16 commits into
PowerShell:masterfrom
HumanEquivalentUnit:master

Conversation

@HumanEquivalentUnit

Copy link
Copy Markdown
Contributor

PR Summary

Fix #12473 where Get-FileHash holds the hashed file handles open unnecessarily long.
Change is to hash the file contents, then dispose the file stream, then write the results, in that order.

PR Context

Get-ChildItem | Get-FileHash | Rename-Item -NewName {$_.Hash} throws because the file is still open when trying to be renamed, this PR tries to fix that by closing the file handle before Get-FileHash writes output.

PR Checklist

Fix the use case `Get-FileHash | Rename-Item` 
by changing to `using () {}` and closing the hashed file before writing the output result.
@ghost ghost assigned Ilya (iSazonov) Apr 23, 2020
@msftclas

Microsoft Contribution License Agreements (msftclas) commented Apr 23, 2020 •

Copy link
Copy Markdown

CLA assistant check
All CLA requirements met.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! Just a couple questions on the test. 😊

Copy the test document first, then remove the copy after, so it won't affect other tests.
Assumes the test drive is writable.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, nice work! 😊

@vexx32

Copy link
Copy Markdown
Collaborator

PoshChan-Bot (@PoshChan) please retry macos

@PoshChan

Copy link
Copy Markdown
Collaborator

Rain Sallow (/u/ta11ow) (@vexx32), successfully started retry of PowerShell-CI-macOS

@HumanEquivalentUnit
HumanEquivalentUnit marked this pull request as ready for review April 24, 2020 03:10
@HumanEquivalentUnit

Copy link
Copy Markdown
Contributor Author

NB. The failing PowerShell-CI-static-analysis test is about URLs in Markdown in the PowerShell 6 CHANGELOG, and is unrelated to these changes.

@iSazonov Ilya (iSazonov) added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Apr 24, 2020
Add more documentation comments.
Remove Algorithm argument from ComputeFileHash method.
Add param descriptions.
Move bytehash var into it.
Update how it returns success/failure state.
@HumanEquivalentUnit
HumanEquivalentUnit marked this pull request as draft April 24, 2020 06:00
@iSazonov

Copy link
Copy Markdown
Collaborator

HumanEquivalentUnit Please rebase to get a fix for CI-static.

Remove hashCompletedSuccessfully variable and replace with a return value calculated from hash variable.
Remove blank line before closing brace.
Change the test logic to try the rename, and then Test-Path the expected new name.
Make the context and description clearer.
@HumanEquivalentUnit

HumanEquivalentUnit commented Apr 24, 2020 •

Copy link
Copy Markdown
Contributor Author

HumanEquivalentUnit Please rebase to get a fix for CI-static.

OK, I followed https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/syncing-a-fork and think it has merged the updated markdown files. Fingers crossed.

Sync a fork of a repository to keep it up-to-date with the upstream repository.

@HumanEquivalentUnit
HumanEquivalentUnit marked this pull request as ready for review April 24, 2020 07:54
@HumanEquivalentUnit

Copy link
Copy Markdown
Contributor Author

PowerShell-CI-macos check failure is a traceroute one, and not related to this PR; could one of you re-run it? Not sure I can.

PoshChan-Bot (@PoshChan) please retry macos

@vexx32

Copy link
Copy Markdown
Collaborator

PoshChan-Bot (@PoshChan) please retry macos

@PoshChan

Copy link
Copy Markdown
Collaborator

Rain Sallow (/u/ta11ow) (@vexx32), successfully started retry of PowerShell-CI-macOS

@iSazonov Ilya (iSazonov) changed the title Change Get-FileHash to close file handles before writing output - fixes #12473 Change Get-FileHash to close file handles before writing output Apr 30, 2020
@iSazonov
Ilya (iSazonov) merged commit 9e3b340 into PowerShell:master Apr 30, 2020
@HumanEquivalentUnit

Copy link
Copy Markdown
Contributor Author

Thank you for reviewing and improving it, Ilya (@iSazonov) !

@ghost

Copy link
Copy Markdown

🎉v7.1.0-preview.3 has been released which incorporates this pull request.:tada:

Handy links:

@ghost

Copy link
Copy Markdown

🎉v7.0.2 has been released which incorporates this pull request.:tada:

Handy links:

John Dennis (silijon) pushed a commit to SkyKick/PowerShell that referenced this pull request Jul 2, 2020
…rShell#12474)

# Conflicts:
#	src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetHash.cs
Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj-contribs/PowerShell that referenced this pull request Aug 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Get-FileHash holds the file open

6 participants