Skip to content

Conversation

@iSazonov
Copy link
Collaborator

@iSazonov iSazonov commented Nov 19, 2019

PR Summary

Fix #11110

If Get-FileHash gets a folder as argument we should write non-terminate error.

PR Context

PR Checklist

@iSazonov iSazonov added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Nov 19, 2019
@iSazonov
Copy link
Collaborator Author

@SteveL-MSFT If the new test uses $error[0]:

        It "Should write non-terminating error if argument is a folder" {
            $result = $pshome, "${pshome}\pwsh.dll" | Get-FileHash
            $result.Count | Should -Be 1
            $error[0].FullyQualifiedErrorId | Should -BeExactly "UnauthorizedAccessError,Microsoft.PowerShell.Commands.GetFileHashCommand"
        }

it unexpectedly fail with an ConciseView error

MethodInvocationException:
Line |
 127 |                                                     $remainingMessage = $remainingMessage.Substring($substring.Length - $prefix.Length).Trim()

     |                                                     ^ Exception calling "Substring" with "1" argument(s): "StartIndex cannot be less than zero. (Parameter 'startIndex')"

I cannot find simple repo steps.

@iSazonov iSazonov self-assigned this Nov 19, 2019
iSazonov and others added 2 commits November 19, 2019 18:21
…Hash.cs

Co-Authored-By: Joel Sallow (/u/ta11ow) <32407840+vexx32@users.noreply.github.com>
…Hash.cs

Co-Authored-By: Joel Sallow (/u/ta11ow) <32407840+vexx32@users.noreply.github.com>
Comment on lines 141 to +158
catch (FileNotFoundException ex)
{
ErrorRecord errorRecord = new ErrorRecord(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);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you think it's worth doing a general catch or additional catches here as well? Looking at the exceptions that can be thrown from System.IO.File.OpenRead it seems likely we can run into the following exceptions somewhat commonly as well:

  • PathTooLongException
  • IOException

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

IOException combines FileNotFoundException, DirectoryNotFoundException, and PathTooLongException.

I guess PathTooLongException will be raised above at path resolving time.

So nothing to do. :-)

@iSazonov iSazonov merged commit 8d944fd into PowerShell:master Nov 21, 2019
@iSazonov iSazonov deleted the get-filehash-dir-error branch November 21, 2019 10:15
@iSazonov iSazonov added this to the GA-consider milestone Dec 20, 2019
@daxian-dbw daxian-dbw modified the milestones: GA-approved, 7.0.0-rc.2 Jan 11, 2020
@ghost
Copy link

ghost commented Jan 16, 2020

🎉v7.0.0-rc.2 has been released which incorporates this pull request.:tada:

Handy links:

@ghost ghost mentioned this pull request Jan 16, 2020
@JordanNash
Copy link

JordanNash commented Jun 8, 2021

It would be appreciated if this error could be changed from "Access Denied" to something more specific, i.e.: "SKIPPED: Invalid path: folder" or something.

I'm trying to recurse through my photo archive to try to find duplicates. For each file, I send FullName to get-filehash. I get a LOT of these errors, which implies a permissions issues, but after finding this pull request, I see that it is working as expected.
Error:
Get-FileHash: Access to the path 'E:\PHOTO\Lightroom CC Library\Lightroom CC\684f1da3ca494b3e854964d66fd6f341\originals\2011\2011-12-22' is denied.

Invocation:
Get-ChildItem -Recurse -path E:\PHOTO\ | Select-Object -ExpandProperty FullName | Get-FileHash | Export-Csv -Append h:\photo_hash_list3.csv

@vexx32
Copy link
Collaborator

vexx32 commented Jun 8, 2021

You can add the -File param to your Get-ChildItem call to avoid the issue entirely.

But yeah, that's a fair call. Would be a good idea to open a new issue for that. 🙂

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

6 participants