diff options
author | Jeremy Kuhne <jeremy.kuhne@microsoft.com> | 2018-02-22 06:32:31 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-22 06:32:31 +0300 |
commit | 62878f34d65f8bac6f7ebd91083fb9a02866de98 (patch) | |
tree | 9dab9892e6d21f4e90132e427fdd573726f6a6d4 | |
parent | ad34249a15a8d93f9517283b00c863fb6f8c152a (diff) |
Make final API review changes to file enumeration (#27318)
* Make final API review changes to file enumeration
Tweak stack array initialization to just zero the first element in matcher algorithm.
cc: @danmosemsft @terrajobst
* Don't null out the native name to track conversion
-rw-r--r-- | src/System.IO.FileSystem/ref/System.IO.FileSystem.cs | 7 | ||||
-rw-r--r-- | src/System.IO.FileSystem/src/System/IO/Enumeration/FileSystemEntry.Unix.cs | 4 | ||||
-rw-r--r-- | src/System.IO.FileSystem/src/System/IO/Enumeration/FileSystemEntry.Windows.cs | 5 | ||||
-rw-r--r-- | src/System.IO.FileSystem/src/System/IO/Enumeration/FileSystemEnumerableFactory.cs | 8 | ||||
-rw-r--r-- | src/System.IO.FileSystem/src/System/IO/Enumeration/FileSystemName.cs | 8 | ||||
-rw-r--r-- | src/System.IO.FileSystem/src/System/IO/EnumerationOptions.cs | 4 | ||||
-rw-r--r-- | src/System.IO.FileSystem/src/System/IO/MatchType.cs | 4 | ||||
-rw-r--r-- | src/System.IO.FileSystem/tests/Enumeration/AttributeTests.netcoreapp.cs | 26 | ||||
-rw-r--r-- | src/System.IO.FileSystem/tests/Enumeration/PatternTransformTests.netcoreapp.cs | 2 | ||||
-rw-r--r-- | src/System.IO.FileSystem/tests/Enumeration/Win32MatcherTests.netcoreapp.cs (renamed from src/System.IO.FileSystem/tests/Enumeration/DosMatcherTests.netcoreapp.cs) | 12 | ||||
-rw-r--r-- | src/System.IO.FileSystem/tests/System.IO.FileSystem.Tests.csproj | 2 |
11 files changed, 57 insertions, 25 deletions
diff --git a/src/System.IO.FileSystem/ref/System.IO.FileSystem.cs b/src/System.IO.FileSystem/ref/System.IO.FileSystem.cs index cd19c96ab5..8f0ee01b28 100644 --- a/src/System.IO.FileSystem/ref/System.IO.FileSystem.cs +++ b/src/System.IO.FileSystem/ref/System.IO.FileSystem.cs @@ -226,7 +226,7 @@ namespace System.IO public enum MatchType { Simple, - Dos + Win32 } public enum MatchCasing { @@ -260,6 +260,7 @@ namespace System.IO.Enumeration public DateTimeOffset LastAccessTimeUtc { get { throw null; } } public DateTimeOffset LastWriteTimeUtc { get { throw null; } } public bool IsDirectory { get { throw null; } } + public bool IsHidden { get { throw null; } } public FileSystemInfo ToFileSystemInfo() { throw null; } public string ToSpecifiedFullPath() { throw null; } public string ToFullPath() { throw null; } @@ -297,8 +298,8 @@ namespace System.IO.Enumeration } public static class FileSystemName { - public static string TranslateDosExpression(string expression) { throw null; } - public static bool MatchesDosExpression(ReadOnlySpan<char> expression, ReadOnlySpan<char> name, bool ignoreCase = true) { throw null; } + public static string TranslateWin32Expression(string expression) { throw null; } + public static bool MatchesWin32Expression(ReadOnlySpan<char> expression, ReadOnlySpan<char> name, bool ignoreCase = true) { throw null; } public static bool MatchesSimpleExpression(ReadOnlySpan<char> expression, ReadOnlySpan<char> name, bool ignoreCase = true) { throw null; } } } diff --git a/src/System.IO.FileSystem/src/System/IO/Enumeration/FileSystemEntry.Unix.cs b/src/System.IO.FileSystem/src/System/IO/Enumeration/FileSystemEntry.Unix.cs index f5339377b3..eb0bc1e68d 100644 --- a/src/System.IO.FileSystem/src/System/IO/Enumeration/FileSystemEntry.Unix.cs +++ b/src/System.IO.FileSystem/src/System/IO/Enumeration/FileSystemEntry.Unix.cs @@ -88,14 +88,13 @@ namespace System.IO.Enumeration { get { - if (_directoryEntry.Name != null) + if (_directoryEntry.NameLength != 0 && _fileName.Length == 0) { fixed (char* c = _fileNameBuffer) { Span<char> buffer = new Span<char>(c, FileNameBufferSize); _fileName = _directoryEntry.GetName(buffer); } - _directoryEntry.Name = null; } return _fileName; @@ -129,6 +128,7 @@ namespace System.IO.Enumeration public DateTimeOffset LastAccessTimeUtc => _status.GetLastAccessTime(FullPath, continueOnError: true); public DateTimeOffset LastWriteTimeUtc => _status.GetLastWriteTime(FullPath, continueOnError: true); public bool IsDirectory => _status.InitiallyDirectory; + public bool IsHidden => _directoryEntry.Name[0] == '.'; public FileSystemInfo ToFileSystemInfo() { diff --git a/src/System.IO.FileSystem/src/System/IO/Enumeration/FileSystemEntry.Windows.cs b/src/System.IO.FileSystem/src/System/IO/Enumeration/FileSystemEntry.Windows.cs index c7bedb181c..f3bdd4a852 100644 --- a/src/System.IO.FileSystem/src/System/IO/Enumeration/FileSystemEntry.Windows.cs +++ b/src/System.IO.FileSystem/src/System/IO/Enumeration/FileSystemEntry.Windows.cs @@ -67,6 +67,11 @@ namespace System.IO.Enumeration /// </summary> public bool IsDirectory => (Attributes & FileAttributes.Directory) != 0; + /// <summary> + /// Returns true if the file has the hidden attribute. + /// </summary> + public bool IsHidden => (Attributes & FileAttributes.Hidden) != 0; + public FileSystemInfo ToFileSystemInfo() => FileSystemInfo.Create(PathHelpers.CombineNoChecks(Directory, FileName), ref this); diff --git a/src/System.IO.FileSystem/src/System/IO/Enumeration/FileSystemEnumerableFactory.cs b/src/System.IO.FileSystem/src/System/IO/Enumeration/FileSystemEnumerableFactory.cs index 84e67fbffb..b8eae46ad3 100644 --- a/src/System.IO.FileSystem/src/System/IO/Enumeration/FileSystemEnumerableFactory.cs +++ b/src/System.IO.FileSystem/src/System/IO/Enumeration/FileSystemEnumerableFactory.cs @@ -37,7 +37,7 @@ namespace System.IO.Enumeration switch (options.MatchType) { - case MatchType.Dos: + case MatchType.Win32: if (string.IsNullOrEmpty(expression) || expression == "." || expression == "*.*") { // Historically we always treated "." as "*" @@ -57,7 +57,7 @@ namespace System.IO.Enumeration } // Need to convert the expression to match Win32 behavior - expression = FileSystemName.TranslateDosExpression(expression); + expression = FileSystemName.TranslateWin32Expression(expression); } break; case MatchType.Simple: @@ -76,8 +76,8 @@ namespace System.IO.Enumeration { case MatchType.Simple: return FileSystemName.MatchesSimpleExpression(expression, name, ignoreCase); - case MatchType.Dos: - return FileSystemName.MatchesDosExpression(expression, name, ignoreCase); + case MatchType.Win32: + return FileSystemName.MatchesWin32Expression(expression, name, ignoreCase); default: throw new ArgumentOutOfRangeException(nameof(options)); } diff --git a/src/System.IO.FileSystem/src/System/IO/Enumeration/FileSystemName.cs b/src/System.IO.FileSystem/src/System/IO/Enumeration/FileSystemName.cs index 10ef61cf83..2de8d2a4ad 100644 --- a/src/System.IO.FileSystem/src/System/IO/Enumeration/FileSystemName.cs +++ b/src/System.IO.FileSystem/src/System/IO/Enumeration/FileSystemName.cs @@ -28,7 +28,7 @@ namespace System.IO.Enumeration /// Change '*' and '?' to '<', '>' and '"' to match Win32 behavior. For compatibility, Windows /// changes some wildcards to provide a closer match to historical DOS 8.3 filename matching. /// </summary> - public static string TranslateDosExpression(string expression) + public static string TranslateWin32Expression(string expression) { if (string.IsNullOrEmpty(expression) || expression == "*" || expression == "*.*") return "*"; @@ -82,9 +82,9 @@ namespace System.IO.Enumeration /// of RtlIsNameInExpression, which defines the rules for matching DOS wildcards ('*', '?', '<', '>', '"'). /// /// Like PatternMatcher, matching will not line up with Win32 behavior unless you transform the expression - /// using <see cref="TranslateDosExpression(string)"/> + /// using <see cref="TranslateWin32Expression(string)"/> /// </remarks> - public static bool MatchesDosExpression(ReadOnlySpan<char> expression, ReadOnlySpan<char> name, bool ignoreCase = true) + public static bool MatchesWin32Expression(ReadOnlySpan<char> expression, ReadOnlySpan<char> name, bool ignoreCase = true) { return MatchPattern(expression, name, ignoreCase, useExtendedWildcards: true); } @@ -141,7 +141,7 @@ namespace System.IO.Enumeration Span<int> temp = stackalloc int[0]; Span<int> currentMatches = stackalloc int[16]; Span<int> priorMatches = stackalloc int[16]; - priorMatches.Clear(); + priorMatches[0] = 0; int maxState = expression.Length * 2; int currentState; diff --git a/src/System.IO.FileSystem/src/System/IO/EnumerationOptions.cs b/src/System.IO.FileSystem/src/System/IO/EnumerationOptions.cs index 3659ee7472..d78e8368e3 100644 --- a/src/System.IO.FileSystem/src/System/IO/EnumerationOptions.cs +++ b/src/System.IO.FileSystem/src/System/IO/EnumerationOptions.cs @@ -11,10 +11,10 @@ namespace System.IO /// explicitly specify EnumerationOptions. /// </summary> internal static EnumerationOptions Compatible { get; } = new EnumerationOptions - { MatchType = MatchType.Dos, AttributesToSkip = 0, IgnoreInaccessible = false }; + { MatchType = MatchType.Win32, AttributesToSkip = 0, IgnoreInaccessible = false }; private static EnumerationOptions CompatibleRecursive { get; } = new EnumerationOptions - { RecurseSubdirectories = true, MatchType = MatchType.Dos, AttributesToSkip = 0, IgnoreInaccessible = false }; + { RecurseSubdirectories = true, MatchType = MatchType.Win32, AttributesToSkip = 0, IgnoreInaccessible = false }; /// <summary> /// Internal singleton for default options. diff --git a/src/System.IO.FileSystem/src/System/IO/MatchType.cs b/src/System.IO.FileSystem/src/System/IO/MatchType.cs index 9789390d95..0bdf89b3c7 100644 --- a/src/System.IO.FileSystem/src/System/IO/MatchType.cs +++ b/src/System.IO.FileSystem/src/System/IO/MatchType.cs @@ -12,9 +12,9 @@ namespace System.IO Simple, /// <summary> - /// Match using DOS style matching semantics. '*', '?', '<', '>', and '"' + /// Match using Win32 DOS style matching semantics. '*', '?', '<', '>', and '"' /// are all considered wildcards. /// </summary> - Dos + Win32 } } diff --git a/src/System.IO.FileSystem/tests/Enumeration/AttributeTests.netcoreapp.cs b/src/System.IO.FileSystem/tests/Enumeration/AttributeTests.netcoreapp.cs index d35783713f..2ba62dfb42 100644 --- a/src/System.IO.FileSystem/tests/Enumeration/AttributeTests.netcoreapp.cs +++ b/src/System.IO.FileSystem/tests/Enumeration/AttributeTests.netcoreapp.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System.Collections.Generic; using System.IO.Enumeration; using Xunit; @@ -111,5 +112,30 @@ namespace System.IO.Tests.Enumeration Assert.False(enumerator.MoveNext()); } } + + [Fact] + public void IsHiddenAttribute() + { + DirectoryInfo testDirectory = Directory.CreateDirectory(GetTestFilePath()); + FileInfo fileOne = new FileInfo(Path.Combine(testDirectory.FullName, GetTestFileName())); + + // Put a period in front to make it hidden on Unix + FileInfo fileTwo = new FileInfo(Path.Combine(testDirectory.FullName, "." + GetTestFileName())); + + fileOne.Create().Dispose(); + fileTwo.Create().Dispose(); + if (PlatformDetection.IsWindows) + fileTwo.Attributes = fileTwo.Attributes | FileAttributes.Hidden; + + IEnumerable<string> enumerable = new FileSystemEnumerable<string>( + testDirectory.FullName, + (ref FileSystemEntry entry) => entry.ToFullPath(), + new EnumerationOptions() { AttributesToSkip = 0 }) + { + ShouldIncludePredicate = (ref FileSystemEntry entry) => entry.IsHidden + }; + + Assert.Equal(new string[] { fileTwo.FullName }, enumerable); + } } } diff --git a/src/System.IO.FileSystem/tests/Enumeration/PatternTransformTests.netcoreapp.cs b/src/System.IO.FileSystem/tests/Enumeration/PatternTransformTests.netcoreapp.cs index d9e8b5330d..9cb33e5853 100644 --- a/src/System.IO.FileSystem/tests/Enumeration/PatternTransformTests.netcoreapp.cs +++ b/src/System.IO.FileSystem/tests/Enumeration/PatternTransformTests.netcoreapp.cs @@ -32,7 +32,7 @@ namespace System.IO.Tests.Enumeration string[] results = GetFiles(testDirectory.FullName, pattern); FSAssert.EqualWhenOrdered(new string[] { fileOne.FullName, fileTwo.FullName }, results); - results = GetFiles(testDirectory.FullName, pattern, new EnumerationOptions { MatchType = MatchType.Dos }); + results = GetFiles(testDirectory.FullName, pattern, new EnumerationOptions { MatchType = MatchType.Win32 }); FSAssert.EqualWhenOrdered(new string[] { fileOne.FullName, fileTwo.FullName }, results); } diff --git a/src/System.IO.FileSystem/tests/Enumeration/DosMatcherTests.netcoreapp.cs b/src/System.IO.FileSystem/tests/Enumeration/Win32MatcherTests.netcoreapp.cs index 508f0f6bf9..bc1d529969 100644 --- a/src/System.IO.FileSystem/tests/Enumeration/DosMatcherTests.netcoreapp.cs +++ b/src/System.IO.FileSystem/tests/Enumeration/Win32MatcherTests.netcoreapp.cs @@ -7,15 +7,15 @@ using Xunit; namespace System.IO.Tests { - public class DosMatcherTests + public class Win32MatcherTests { - [Theory, MemberData(nameof(DosMatchData)), MemberData(nameof(EscapedDosMatchData))] - public static void DosMatch(string expression, string name, bool ignoreCase, bool expected) + [Theory, MemberData(nameof(Win32MatchData)), MemberData(nameof(EscapedWin32MatchData))] + public static void Win32Match(string expression, string name, bool ignoreCase, bool expected) { - Assert.Equal(expected, FileSystemName.MatchesDosExpression(expression, name.AsSpan(), ignoreCase)); + Assert.Equal(expected, FileSystemName.MatchesWin32Expression(expression, name.AsSpan(), ignoreCase)); } - public static TheoryData<string, string, bool, bool> EscapedDosMatchData => new TheoryData<string, string, bool, bool> + public static TheoryData<string, string, bool, bool> EscapedWin32MatchData => new TheoryData<string, string, bool, bool> { // Trailing escape matches as it is considered "invisible" { "\\", "\\", false, true }, @@ -43,7 +43,7 @@ namespace System.IO.Tests { "\\\"", "\"", true, true }, }; - public static TheoryData<string, string, bool, bool> DosMatchData => new TheoryData<string, string, bool, bool> + public static TheoryData<string, string, bool, bool> Win32MatchData => new TheoryData<string, string, bool, bool> { { null, "", false, false }, { null, "", true, false }, diff --git a/src/System.IO.FileSystem/tests/System.IO.FileSystem.Tests.csproj b/src/System.IO.FileSystem/tests/System.IO.FileSystem.Tests.csproj index 672d5642dc..5ff308577c 100644 --- a/src/System.IO.FileSystem/tests/System.IO.FileSystem.Tests.csproj +++ b/src/System.IO.FileSystem/tests/System.IO.FileSystem.Tests.csproj @@ -56,7 +56,7 @@ <Compile Include="Enumeration\ConstructionTests.netcoreapp.cs" /> <Compile Include="Enumeration\SpecialDirectoryTests.netcoreapp.cs" /> <Compile Include="Enumeration\SkipAttributeTests.netcoreapp.cs" /> - <Compile Include="Enumeration\DosMatcherTests.netcoreapp.cs" /> + <Compile Include="Enumeration\Win32MatcherTests.netcoreapp.cs" /> <Compile Include="Enumeration\MatchCasingTests.netcoreapp.cs" /> <Compile Include="Enumeration\TrimmedPaths.netcoreapp.cs" /> <Compile Include="Enumeration\ErrorHandlingTests.netcoreapp.cs" /> |