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
Prev Previous commit
Refactor error handling.
  • Loading branch information
tmds committed Dec 12, 2021
commit 6a4ebc78f804ef52789c5fe9966dd4360daeb9e1
Original file line number Diff line number Diff line change
Expand Up @@ -492,49 +492,45 @@ private static bool RemoveEmptyDirectory(string fullPath, bool topLevel = false,
if (Interop.Sys.RmDir(fullPath) < 0)
{
Interop.ErrorInfo errorInfo = Interop.Sys.GetLastErrorInfo();
switch (errorInfo.Error)

if (errorInfo.Error == Interop.Error.ENOTEMPTY)
{
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));
case Interop.Error.ENOENT:
// When we're recursing, don't throw for items that go missing.
if (!topLevel)
{
return true;
}
goto default;
case Interop.Error.ENOTDIR:
// When the top-level path is a symlink to a directory, delete the link.
// In other cases, throw because we expect path to be a real directory.
if (topLevel)
{
if (!DirectoryExists(fullPath))
{
throw Interop.GetExceptionForIoErrno(Interop.Error.ENOENT.Info(), fullPath, isDirectory: true);
}
if (!throwWhenNotEmpty)
{
return false;
}
}
else if (errorInfo.Error == Interop.Error.ENOENT)
{
// When we're recursing, don't throw for items that go missing.
if (!topLevel)
{
return true;
}
}
else if (DirectoryExists(fullPath, out Interop.ErrorInfo existErr))
{
// Top-level path is a symlink to a directory, delete the link.
if (topLevel && errorInfo.Error == Interop.Error.ENOTDIR)
{
DeleteFile(fullPath);
return true;
}
}
else if (existErr.Error == Interop.Error.ENOENT)
{
// Prefer throwing DirectoryNotFoundException over other exceptions.
errorInfo = existErr;
}

DeleteFile(fullPath);
return true;
}
goto default;
case Interop.Error.ENOTEMPTY:
if (!throwWhenNotEmpty)
{
return false;
}
goto default;
default:
throw Interop.GetExceptionForIoErrno(errorInfo, fullPath, isDirectory: true);
if (errorInfo.Error == Interop.Error.EACCES ||
errorInfo.Error == Interop.Error.EPERM ||
errorInfo.Error == Interop.Error.EROFS)
{
throw new IOException(SR.Format(SR.UnauthorizedAccess_IODenied_Path, fullPath));
}

throw Interop.GetExceptionForIoErrno(errorInfo, fullPath, isDirectory: true);
}

return true;
Expand Down