Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Directory.Delete: prefer DirectoryNotFoundException over UnauthorizedAccess IOException. #62396

Merged
merged 2 commits into from
Dec 16, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,14 @@ private static bool RemoveEmptyDirectory(string fullPath, bool topLevel = false,
case Interop.Error.EACCES:
case Interop.Error.EPERM:
case Interop.Error.EROFS:
// Prefer throwing DirectoryNotFoundException over UnauthorizedAccess IOException.
tmds marked this conversation as resolved.
Show resolved Hide resolved
if (topLevel && !DirectoryExists(fullPath, out Interop.ErrorInfo existErr) && existErr.Error == Interop.Error.ENOENT)
{
throw Interop.GetExceptionForIoErrno(Interop.Error.ENOENT.Info(), fullPath, isDirectory: true);
}
throw new IOException(SR.Format(SR.UnauthorizedAccess_IODenied_Path, fullPath));
tmds marked this conversation as resolved.
Show resolved Hide resolved
case Interop.Error.EISDIR:
throw new IOException(SR.Format(SR.UnauthorizedAccess_IODenied_Path, fullPath)); // match Win32 exception
throw new IOException(SR.Format(SR.UnauthorizedAccess_IODenied_Path, fullPath));
Copy link
Member

Choose a reason for hiding this comment

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

Interesting case. The fix looks good but I have a few questions.

So you found that when EROFS is caught, there is a special case where the actual problem was that the top level directory didn't exist. Correct? Do we know what is the actual reason why EROFS is being returned and not something else?

Do you mind changing that comment to something that would describe that we have two different possible cases here? That would be more meaningful.

In the original issue, Dan left a comment asking if the elevated test was not being executed in the PR CI. Can you please verify that the test was indeed executed in this PR?

Copy link
Member Author

Choose a reason for hiding this comment

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

So you found that when EROFS is caught, there is a special case where the actual problem was that the top level directory didn't exist. Correct? Do we know what is the actual reason why EROFS is being returned and not something else?

Previously we were always checking if the directory existed before trying to remove it.

One of the test verified that DirectoryNotFoundException was thrown in favor of the UnauthorizedAccess_IODenied_Path (on EROFS). And I regressed it in #59520.

In the original issue, Dan left a comment asking if the elevated test was not being executed in the PR CI. Can you please verify that the test was indeed executed in this PR?

I'll verify the test passes.

case Interop.Error.ENOENT:
// When we're recursing, don't throw for items that go missing.
if (!topLevel)
Expand Down