Skip to content

dir -hidden -force c:\pagefile.sys returns an error (#2144) - #2273

Merged
Dongbo Wang (daxian-dbw) merged 1 commit into
PowerShell:masterfrom
charub:2144-getchilditem
Sep 27, 2016
Merged

Dongbo Wang (daxian-dbw) merged 1 commit into
PowerShell:masterfrom
charub:2144-getchilditem

Conversation

@charub

@charub Charu Bassi (charub) commented Sep 15, 2016 •

Copy link
Copy Markdown
Contributor

Resolving #2144

@msftclas

Hi Charu Bassi (@charub), I'm your friendly neighborhood Microsoft Pull Request Bot (You can call me MSBOT). Thanks for your contribution!


It looks like you're a Microsoft contributor (Charu Bassi). If you're full-time, we DON'T require a Contribution License Agreement. If you are a vendor, please DO sign the electronic Contribution License Agreement. It will take 2 minutes and there's no faxing! https://cla.microsoft.com.

TTYL, MSBOT;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

fwiw, this should be "Fall back" (verb+adverb ~ verb phrase) instead of "Fallback" (noun)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You can (and probably should) omit "or not"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done!

@jsoref Josh Soref (jsoref) Sep 20, 2016 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ditto

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done!

@jsoref Josh Soref (jsoref) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

As a user who has hit this rather annoying error, thanks for fixing it!

Sorry for the drive by grammatical notes. Hopefully, they're vaguely helpful to some readers. (I feel somewhat invested in this, as I've sent quite a few PRs and changes to improve the readability of PowerShell's source code.)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe this if ... else ... block can be changed to

isDirectory = (findData.dwFileAttributes & NativeMethods.FileAttributes.Directory) != 0;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Gud one! Made the change.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe it's not necessary to call NativeMethods.FindClose when findHandle is invalid. And NativeMethods.FindClose should be placed in a finally block in case of any unexpected exception.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Although FindClose() internally takes care if the handle is invalid. But in our case , we are anyways checking if the handle is valid, so calling FindClose() only in case it's valid.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

IntPtr findHandle;
findHandle = NativeMethods.FindFirstFile(path, out findData);

This should be put in one line: IntPtr findHandle = NativeMethods.FindFirstFile(path, out findData);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

can FindFirstFile get the handle of a file even if the file is being use by another process? If so, can you please add some comments explain your research on that?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Made the change suggested in the comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

GetFileAttributes can get the handle to a file even if it is shared by another process but it fails when the sharing flag for the file is set to 0x00000000. This happen because GetFileAttributes function tries to open the file with FILE_READ_ATTRIBUTES access right. It prevents us from opening a file for delete, read, or write access. In current scenario, C:\pagefile.sys is always opened by OS with sharing flag 0x00000000. But FindFirstFile is still able to get attributes as this api retrieves the required information using a find handle generated with FILE_LIST_DIRECTORY access.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice. Please add this to the code as comments.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nitpick: the order probably should be if (errorCode == 5) ... else if (32) ... else if (53) ...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

And another one -- not related to this fix -- it seems for any other error code we just silently sallow it without throw error. Probably it was by design. It might be a breaking change to fix that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Similar comments here as above.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Remove-Item should be in a finally block or in AfterEach block, so that it will be cleaned up even if any Should Be above failed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

There's no need to remove TestDrive. Pester internally takes care of that. So, removing this statement.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ditto

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe add > $null to New-Item.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

These 4 statements seems redundant for each test. Maybe you should put them in a BeforeAll or BeforeEach block.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Moved to BeforeAll block for this context.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ditto

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

($IsLinux -Or $IsOSX )

Maybe just (! $IsWindows)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

what if the system drive is not C:?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

using $env:SystemDrive instead of hard coded c:\

@charub
Charu Bassi (charub) force-pushed the 2144-getchilditem branch 2 times, most recently from 39b76ca to 0227b3a Compare September 27, 2016 17:27

@daxian-dbw Dongbo Wang (daxian-dbw) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's recommended to use more descriptive commit message other than the issue title. In this case, maybe we can use:
"Fix Get-ChildItem -Hidden to work on system hidden files like c:\hiberfil.sys c:\pagefile.sys and c:\swapfile.sys"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants