Skip to content

Fix unblock read only file - #4395

Merged
Travis Plunk (TravisEz13) merged 4 commits into
PowerShell:masterfrom
iSazonov:fix-unblock-file-ro
Aug 14, 2017
Merged

Fix unblock read only file#4395
Travis Plunk (TravisEz13) merged 4 commits into
PowerShell:masterfrom
iSazonov:fix-unblock-file-ro

Conversation

@iSazonov

@iSazonov Ilya (iSazonov) commented Jul 31, 2017

Copy link
Copy Markdown
Collaborator

Adds a new non-terminating error

Fix #4390.

Problem

If a file is read only it is skipping silently.

Fix

Analize GetLastError() and throw if a file stream hasn't been removed.

First commit only refactor tests to use ShouldBeErrorId.

@SteveL-MSFT

Copy link
Copy Markdown
Member

Ilya (@iSazonov) what is the error message the user sees now?

@iSazonov

Copy link
Copy Markdown
Collaborator Author

MSDN says that GetLastError can return either "Access denied" or "File not found".

Unblock-File : Access is denied
At line:1 char:1
+ Unblock-File -Path $Testpath
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Unblock-File], Win32Exception
    + FullyQualifiedErrorId : System.ComponentModel.Win32Exception,Microsoft.PowerShell.Commands.UnblockFileCommand

@SteveL-MSFT Steve Lee (SteveL-MSFT) 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.

LGTM

if (!NativeMethods.DeleteFile(resultPath))
{
int error = Marshal.GetLastWin32Error();
throw new Win32Exception(error);

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.

Shouldn't this be a non-terminating error? The following should behave similarly:

dir | Unblock-File
dir | Remove-Item

In the Remove-Item case, a read only file reports an error, but all other files are deleted.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Good catch!
AlternateDataStreamUtilities uses the pattern. So maybe add try-catch in Unblock-File cmdlet file?

@iSazonov

Copy link
Copy Markdown
Collaborator Author

Jason Shirk (@lzybkr) Steve Lee (@SteveL-MSFT) Please continue the code review.

@TravisEz13
Travis Plunk (TravisEz13) dismissed Steve Lee (SteveL-MSFT)’s stale review August 11, 2017 17:27

A commit has been added to the review

@lzybkr Jason Shirk (lzybkr) 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.

Make sure the final commit message points out this is a new non-terminating error.

@TravisEz13
Travis Plunk (TravisEz13) merged commit 813e4a6 into PowerShell:master Aug 14, 2017
@iSazonov
Ilya (iSazonov) deleted the fix-unblock-file-ro branch August 15, 2017 09:22
Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj/PowerShell that referenced this pull request Aug 6, 2026
Adds a new non-terminating error when a file is read-only
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