-
Notifications
You must be signed in to change notification settings - Fork 5k
FileSystem.Unix: Directory.Delete: remove per item syscall. #59520
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
Changes from all commits
d8451f1
ead0f47
599514a
63953d7
edb54b2
9014793
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
|
||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.IO.Enumeration; | ||
using System.Text; | ||
using Microsoft.Win32.SafeHandles; | ||
|
||
|
@@ -433,69 +434,66 @@ public static void MoveDirectory(string sourceFullPath, string destFullPath) | |
|
||
public static void RemoveDirectory(string fullPath, bool recursive) | ||
{ | ||
var di = new DirectoryInfo(fullPath); | ||
if (!di.Exists) | ||
// Delete the directory. | ||
// If we're recursing, don't throw when it is not empty, and perform a recursive remove. | ||
if (!RemoveEmptyDirectory(fullPath, topLevel: true, throwWhenNotEmpty: !recursive)) | ||
{ | ||
throw Interop.GetExceptionForIoErrno(Interop.Error.ENOENT.Info(), fullPath, isDirectory: true); | ||
Debug.Assert(recursive); | ||
|
||
RemoveDirectoryRecursive(fullPath); | ||
} | ||
RemoveDirectoryInternal(di, recursive, throwOnTopLevelDirectoryNotFound: true); | ||
} | ||
|
||
private static void RemoveDirectoryInternal(DirectoryInfo directory, bool recursive, bool throwOnTopLevelDirectoryNotFound) | ||
private static void RemoveDirectoryRecursive(string fullPath) | ||
{ | ||
Exception? firstException = null; | ||
|
||
if ((directory.Attributes & FileAttributes.ReparsePoint) != 0) | ||
try | ||
{ | ||
DeleteFile(directory.FullName); | ||
return; | ||
} | ||
var fse = new FileSystemEnumerable<(string, bool)>(fullPath, | ||
static (ref FileSystemEntry entry) => | ||
{ | ||
// Don't report symlinks to directories as directories. | ||
bool isRealDirectory = !entry.IsSymbolicLink && entry.IsDirectory; | ||
return (entry.ToFullPath(), isRealDirectory); | ||
}, | ||
EnumerationOptions.Compatible); | ||
|
||
if (recursive) | ||
{ | ||
try | ||
foreach ((string childPath, bool isDirectory) in fse) | ||
{ | ||
foreach (string item in Directory.EnumerateFileSystemEntries(directory.FullName)) | ||
try | ||
{ | ||
if (!ShouldIgnoreDirectory(Path.GetFileName(item))) | ||
if (isDirectory) | ||
{ | ||
try | ||
{ | ||
var childDirectory = new DirectoryInfo(item); | ||
if (childDirectory.Exists) | ||
{ | ||
RemoveDirectoryInternal(childDirectory, recursive, throwOnTopLevelDirectoryNotFound: false); | ||
} | ||
else | ||
{ | ||
DeleteFile(item); | ||
} | ||
} | ||
catch (Exception exc) | ||
{ | ||
if (firstException != null) | ||
{ | ||
firstException = exc; | ||
} | ||
} | ||
RemoveDirectoryRecursive(childPath); | ||
tmds marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
else | ||
{ | ||
DeleteFile(childPath); | ||
} | ||
} | ||
} | ||
catch (Exception exc) | ||
{ | ||
if (firstException != null) | ||
catch (Exception ex) | ||
{ | ||
firstException = exc; | ||
firstException ??= ex; | ||
} | ||
} | ||
} | ||
catch (Exception exc) | ||
{ | ||
firstException ??= exc; | ||
} | ||
|
||
if (firstException != null) | ||
{ | ||
throw firstException; | ||
} | ||
if (firstException != null) | ||
{ | ||
throw firstException; | ||
} | ||
|
||
if (Interop.Sys.RmDir(directory.FullName) < 0) | ||
RemoveEmptyDirectory(fullPath); | ||
} | ||
|
||
private static bool RemoveEmptyDirectory(string fullPath, bool topLevel = false, bool throwWhenNotEmpty = true) | ||
{ | ||
if (Interop.Sys.RmDir(fullPath) < 0) | ||
{ | ||
Interop.ErrorInfo errorInfo = Interop.Sys.GetLastErrorInfo(); | ||
switch (errorInfo.Error) | ||
|
@@ -504,17 +502,40 @@ private static void RemoveDirectoryInternal(DirectoryInfo directory, bool recurs | |
case Interop.Error.EPERM: | ||
case Interop.Error.EROFS: | ||
case Interop.Error.EISDIR: | ||
throw new IOException(SR.Format(SR.UnauthorizedAccess_IODenied_Path, directory.FullName)); // match Win32 exception | ||
throw new IOException(SR.Format(SR.UnauthorizedAccess_IODenied_Path, fullPath)); // match Win32 exception | ||
case Interop.Error.ENOENT: | ||
if (!throwOnTopLevelDirectoryNotFound) | ||
// When we're recursing, don't throw for items that go missing. | ||
if (!topLevel) | ||
{ | ||
return; | ||
return true; | ||
} | ||
tmds marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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)) | ||
tmds marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
throw Interop.GetExceptionForIoErrno(Interop.Error.ENOENT.Info(), fullPath, isDirectory: true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I kept There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @adamsitnik I would prefer to change this error code because I think the previous behavior was unintentional. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @adamsitnik can you share your opinion? |
||
} | ||
|
||
DeleteFile(fullPath); | ||
return true; | ||
} | ||
goto default; | ||
case Interop.Error.ENOTEMPTY: | ||
if (!throwWhenNotEmpty) | ||
{ | ||
return false; | ||
} | ||
goto default; | ||
default: | ||
throw Interop.GetExceptionForIoErrno(errorInfo, directory.FullName, isDirectory: true); | ||
throw Interop.GetExceptionForIoErrno(errorInfo, fullPath, isDirectory: true); | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/// <summary>Determines whether the specified directory name should be ignored.</summary> | ||
|
Uh oh!
There was an error while loading. Please reload this page.