-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Improve CreateDirectory
on Windows
#66754
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
8ce1a96
de9e3a9
a0b5388
e43967a
d6b06b3
31961a4
12d7729
6faa0e1
bf0e715
7fc97aa
920bf49
8cfca86
b9e4754
8fb6ecb
5bc0806
c926642
378a6f7
61af4d3
056c9b9
cb6a732
ff116e4
bc22603
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 |
---|---|---|
|
@@ -29,6 +29,17 @@ private static bool DirectoryExists(string? path, out int lastError) | |
((data.dwFileAttributes & Interop.Kernel32.FileAttributes.FILE_ATTRIBUTE_DIRECTORY) != 0); | ||
} | ||
|
||
private static bool DirectoryExistsSlim(string? path, out int lastError) | ||
{ | ||
Interop.Kernel32.WIN32_FILE_ATTRIBUTE_DATA data = default; | ||
lastError = FillAttributeInfoSlim(path, ref data, returnErrorOnNotFound: true); | ||
|
||
return | ||
(lastError == 0) && | ||
(data.dwFileAttributes != -1) && | ||
((data.dwFileAttributes & Interop.Kernel32.FileAttributes.FILE_ATTRIBUTE_DIRECTORY) != 0); | ||
} | ||
|
||
public static bool FileExists(string fullPath) | ||
{ | ||
Interop.Kernel32.WIN32_FILE_ATTRIBUTE_DATA data = default; | ||
|
@@ -49,53 +60,64 @@ public static bool FileExists(string fullPath) | |
/// <param name="returnErrorOnNotFound">Return the error code for not found errors?</param> | ||
internal static int FillAttributeInfo(string? path, ref Interop.Kernel32.WIN32_FILE_ATTRIBUTE_DATA data, bool returnErrorOnNotFound) | ||
{ | ||
int errorCode = Interop.Errors.ERROR_SUCCESS; | ||
|
||
// Neither GetFileAttributes or FindFirstFile like trailing separators | ||
path = PathInternal.TrimEndingDirectorySeparator(path); | ||
|
||
using (DisableMediaInsertionPrompt.Create()) | ||
{ | ||
if (!Interop.Kernel32.GetFileAttributesEx(path, Interop.Kernel32.GET_FILEEX_INFO_LEVELS.GetFileExInfoStandard, ref data)) | ||
return FillAttributeInfoSlim(path, ref data, returnErrorOnNotFound); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Same as <see cref="FillAttributeInfo"/> without separator-trimming and media-insertion blocking | ||
/// </summary> | ||
/// <param name="path">The file path from which the file attribute information will be filled.</param> | ||
/// <param name="data">A struct that will contain the attribute information.</param> | ||
/// <param name="returnErrorOnNotFound">Return the error code for not found errors?</param> | ||
internal static int FillAttributeInfoSlim(string? path, ref Interop.Kernel32.WIN32_FILE_ATTRIBUTE_DATA data, bool returnErrorOnNotFound) | ||
{ | ||
int errorCode = Interop.Errors.ERROR_SUCCESS; | ||
string? prefixedString = PathInternal.EnsureExtendedPrefixIfNeeded(path); | ||
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. The FindFirstFilePrefixed path here is the rare case, right? (<1%) Where the file is locked. Given that, I don't think you're really saving anything by ensuring you only call EnsureExtendedPrefixIfNeeded once. I would reverse this and leave GetFileAttributesExPrivate private. That avoids creating a way for future code to accidentally call GetFileAttributesEx without prefixing. And it leaves this code a bit simpler. 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. This was marked resolved but I don't see the feedback addressed. Was it commented on somewhere else? |
||
|
||
if (!Interop.Kernel32.GetFileAttributesExPrivate(prefixedString, Interop.Kernel32.GET_FILEEX_INFO_LEVELS.GetFileExInfoStandard, ref data)) | ||
{ | ||
errorCode = Marshal.GetLastWin32Error(); | ||
|
||
Interop.Kernel32.WIN32_FIND_DATA findData = default; | ||
if (!IsPathUnreachableError(errorCode)) | ||
{ | ||
errorCode = Marshal.GetLastWin32Error(); | ||
// Assert so we can track down other cases (if any) to add to our test suite | ||
Debug.Assert(errorCode == Interop.Errors.ERROR_ACCESS_DENIED || errorCode == Interop.Errors.ERROR_SHARING_VIOLATION || errorCode == Interop.Errors.ERROR_SEM_TIMEOUT, | ||
$"Unexpected error code getting attributes {errorCode} from path {path}"); | ||
|
||
// Files that are marked for deletion will not let you GetFileAttributes, | ||
// ERROR_ACCESS_DENIED is given back without filling out the data struct. | ||
// FindFirstFile, however, will. Historically we always gave back attributes | ||
// for marked-for-deletion files. | ||
// | ||
// Another case where enumeration works is with special system files such as | ||
// pagefile.sys that give back ERROR_SHARING_VIOLATION on GetAttributes. | ||
// | ||
// Ideally we'd only try again for known cases due to the potential performance | ||
// hit. The last attempt to do so baked for nearly a year before we found the | ||
// pagefile.sys case. As such we're probably stuck filtering out specific | ||
// cases that we know we don't want to retry on. | ||
|
||
if (!IsPathUnreachableError(errorCode)) | ||
using SafeFindHandle handle = Interop.Kernel32.FindFirstFile(prefixedString!, ref findData); | ||
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. What's the purpose of this change? |
||
if (handle.IsInvalid) | ||
{ | ||
// Assert so we can track down other cases (if any) to add to our test suite | ||
Debug.Assert(errorCode == Interop.Errors.ERROR_ACCESS_DENIED || errorCode == Interop.Errors.ERROR_SHARING_VIOLATION || errorCode == Interop.Errors.ERROR_SEM_TIMEOUT, | ||
$"Unexpected error code getting attributes {errorCode} from path {path}"); | ||
|
||
// Files that are marked for deletion will not let you GetFileAttributes, | ||
// ERROR_ACCESS_DENIED is given back without filling out the data struct. | ||
// FindFirstFile, however, will. Historically we always gave back attributes | ||
// for marked-for-deletion files. | ||
// | ||
// Another case where enumeration works is with special system files such as | ||
// pagefile.sys that give back ERROR_SHARING_VIOLATION on GetAttributes. | ||
// | ||
// Ideally we'd only try again for known cases due to the potential performance | ||
// hit. The last attempt to do so baked for nearly a year before we found the | ||
// pagefile.sys case. As such we're probably stuck filtering out specific | ||
// cases that we know we don't want to retry on. | ||
|
||
Interop.Kernel32.WIN32_FIND_DATA findData = default; | ||
using (SafeFindHandle handle = Interop.Kernel32.FindFirstFile(path!, ref findData)) | ||
{ | ||
if (handle.IsInvalid) | ||
{ | ||
errorCode = Marshal.GetLastWin32Error(); | ||
} | ||
else | ||
{ | ||
errorCode = Interop.Errors.ERROR_SUCCESS; | ||
data.PopulateFrom(ref findData); | ||
} | ||
} | ||
errorCode = Marshal.GetLastWin32Error(); | ||
} | ||
else | ||
{ | ||
errorCode = Interop.Errors.ERROR_SUCCESS; | ||
data.PopulateFrom(ref findData); | ||
} | ||
} | ||
} | ||
|
||
|
||
if (errorCode != Interop.Errors.ERROR_SUCCESS && !returnErrorOnNotFound) | ||
{ | ||
switch (errorCode) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,11 +72,10 @@ internal static bool IsValidDriveChar(char value) | |
return (uint)((value | 0x20) - 'a') <= (uint)('z' - 'a'); | ||
} | ||
|
||
internal static bool EndsWithPeriodOrSpace(string? path) | ||
internal static bool EndsWithPeriodOrSpace(string path) | ||
{ | ||
if (string.IsNullOrEmpty(path)) | ||
return false; | ||
|
||
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. Why are the changes in this file necessary? |
||
char c = path[path.Length - 1]; | ||
return c == ' ' || c == '.'; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ internal static partial class PathInternal | |
internal static bool StartsWithDirectorySeparator(ReadOnlySpan<char> path) => path.Length > 0 && IsDirectorySeparator(path[0]); | ||
|
||
internal static string EnsureTrailingSeparator(string path) | ||
=> EndsInDirectorySeparator(path.AsSpan()) ? path : path + DirectorySeparatorCharAsString; | ||
=> path.Length > 0 && IsDirectorySeparator(path[path.Length - 1]) ? path : path + DirectorySeparatorCharAsString; | ||
|
||
internal static bool IsRoot(ReadOnlySpan<char> path) | ||
=> path.Length == GetRootLength(path); | ||
|
@@ -231,16 +231,10 @@ internal static bool EndsInDirectorySeparator(string? path) => | |
/// Trims one trailing directory separator beyond the root of the path. | ||
/// </summary> | ||
internal static ReadOnlySpan<char> TrimEndingDirectorySeparator(ReadOnlySpan<char> path) => | ||
EndsInDirectorySeparator(path) && !IsRoot(path) ? | ||
path.Length > 0 && IsDirectorySeparator(path[path.Length - 1]) && !IsRoot(path) ? | ||
path.Slice(0, path.Length - 1) : | ||
path; | ||
|
||
/// <summary> | ||
/// Returns true if the path ends in a directory separator. | ||
/// </summary> | ||
internal static bool EndsInDirectorySeparator(ReadOnlySpan<char> path) => | ||
path.Length > 0 && IsDirectorySeparator(path[path.Length - 1]); | ||
|
||
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. Why are the changes in this file necessary? |
||
internal static string GetLinkTargetFullPath(string path, string pathToTarget) | ||
=> IsPartiallyQualified(pathToTarget.AsSpan()) ? | ||
Path.Join(Path.GetDirectoryName(path.AsSpan()), pathToTarget.AsSpan()) : pathToTarget; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -986,7 +986,7 @@ private static string GetRelativePath(string relativeTo, string path, StringComp | |
/// <summary> | ||
/// Returns true if the path ends in a directory separator. | ||
/// </summary> | ||
public static bool EndsInDirectorySeparator(ReadOnlySpan<char> path) => PathInternal.EndsInDirectorySeparator(path); | ||
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. Why are the changes in this file necessary? |
||
public static bool EndsInDirectorySeparator(ReadOnlySpan<char> path) => path.Length > 0 && PathInternal.IsDirectorySeparator(path[path.Length - 1]); | ||
|
||
/// <summary> | ||
/// Returns true if the path ends in a directory separator. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding these "Slim" functions seems to be an attempt to improve throughput, but the perf results shared don't really show a throughput improvement. Seems like these changes should be reverted?