-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Change PathInternal.IsCaseSensitive to a constant #54340
Changes from 5 commits
c604c47
7b8ba3e
6e7d107
4ac6f7d
731c90d
4476863
40ef8ec
ad2e8f2
84e7779
e1098a1
591af35
b971e4b
df04df4
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 |
---|---|---|
|
@@ -127,5 +127,33 @@ protected void ReadOnly_FileSystemHelper(Action<string> testAction, string subDi | |
Assert.Equal(0, AdminHelpers.RunAsSudo($"umount {readOnlyDirectory}")); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Determines whether the file system is case sensitive by creating a file in the specified folder and observing the result. | ||
/// </summary> | ||
/// <remarks> | ||
/// Ideally we'd use something like pathconf with _PC_CASE_SENSITIVE, but that is non-portable, | ||
/// not supported on Windows or Linux, etc. For now, this function creates a tmp file with capital letters | ||
/// and then tests for its existence with lower-case letters. This could return invalid results in corner | ||
/// cases where, for example, different file systems are mounted with differing sensitivities. | ||
/// </remarks> | ||
protected static bool GetIsCaseSensitiveByProbing(string probingDirectory) | ||
{ | ||
try | ||
{ | ||
string pathWithUpperCase = Path.Combine(probingDirectory, "CASESENSITIVETEST" + Guid.NewGuid().ToString("N")); | ||
using (new FileStream(pathWithUpperCase, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None, 0x1000, FileOptions.DeleteOnClose)) | ||
{ | ||
string lowerCased = pathWithUpperCase.ToLowerInvariant(); | ||
return !File.Exists(lowerCased); | ||
} | ||
} | ||
catch | ||
{ | ||
// In case something goes wrong (e.g. temp pointing to a privilieged directory), we don't | ||
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 |
||
// want to fail just because of a casing test, so we assume case-insensitive-but-preserving. | ||
return false; | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Xunit; | ||
|
||
namespace System.IO.Tests | ||
{ | ||
public class PathInternalTests : FileSystemTest | ||
{ | ||
[Fact] | ||
adamsitnik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public void PathInternalIsCaseSensitiveMatchesProbing() | ||
{ | ||
var probingDirectory = TestDirectory; | ||
lambdageek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Assert.Equal(GetIsCaseSensitiveByProbing(probingDirectory), PathInternal.IsCaseSensitive); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -957,11 +957,11 @@ private static string GetRelativePath(string relativeTo, string path, StringComp | |
return sb.ToString(); | ||
} | ||
|
||
/// <summary>Gets whether the system is case-sensitive.</summary> | ||
internal static bool IsCaseSensitive => PathInternal.IsCaseSensitive; | ||
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 would change the few places that use this to call |
||
|
||
/// <summary>Returns a comparison that can be used to compare file and directory names for equality.</summary> | ||
internal static StringComparison StringComparison => | ||
IsCaseSensitive ? | ||
StringComparison.Ordinal : | ||
StringComparison.OrdinalIgnoreCase; | ||
internal static StringComparison StringComparison => PathInternal.StringComparison; | ||
|
||
/// <summary> | ||
/// Trims one trailing directory separator beyond the root of the path. | ||
|
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.
This should be
internal static bool IsCaseSensitive
property ands_isCaseSensitive
should be deleted. It is not profitable to cache it in a static, and it prevents the linker constant propagation from kicking in.