Skip to content
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
Next Next commit
Directory.Delete: prefer DirectoryNotFoundException over Unauthorized…
…Access IOException.
  • Loading branch information
tmds committed Dec 4, 2021
commit 9fdb42b4b9f45588bbd938946cb95d2bcbf9f5a8
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.
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));
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
Contributor

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