Skip to content

Conversation

@HumanEquivalentUnit
Copy link
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 iSazonov Apr 23, 2020
@msftclas
Copy link

msftclas commented Apr 23, 2020

CLA assistant check
All CLA requirements met.

Copy link
Collaborator

@vexx32 vexx32 left a comment

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
Collaborator

@vexx32 vexx32 left a comment

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
Collaborator

vexx32 commented Apr 24, 2020

@PoshChan please retry macos

@PoshChan
Copy link
Collaborator

@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
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 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
@iSazonov iSazonov added this to the 7.0.x-Servicing-Consider milestone 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
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
Copy link
Contributor Author

HumanEquivalentUnit commented Apr 24, 2020

@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
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 please retry macos

@vexx32
Copy link
Collaborator

vexx32 commented Apr 24, 2020

@PoshChan please retry macos

@PoshChan
Copy link
Collaborator

@vexx32, successfully started retry of PowerShell-CI-macOS

@iSazonov 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 iSazonov merged commit 9e3b340 into PowerShell:master Apr 30, 2020
@HumanEquivalentUnit
Copy link
Contributor Author

Thank you for reviewing and improving it, @iSazonov !

@ghost
Copy link

ghost commented May 19, 2020

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

Handy links:

@ghost ghost mentioned this pull request May 19, 2020
@ghost
Copy link

ghost commented Jun 11, 2020

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

Handy links:

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
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