-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Fix: Avoid throwing if a symbolic link cycle to itself is detected while enumerating #52749
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f460cae
Fix: Avoid throwing on FileSystemEntry.Initialize
5528774
Add unit tests that verify cyclic symbolic links
65ddfc3
Fix Windows-specific unit test failures
carlossanlop 740cb18
Use OperatingSystem.IsWindows()
carlossanlop a3ba144
CHange 'Assert.Equal(1, ' to 'Assert.Single('
carlossanlop c0100c6
Rename helper method that creates self referencing symlink
carlossanlop d371bd8
Spacing
carlossanlop 9f0e595
ConditionalClass for CanCreateSymbolicLinks
carlossanlop 6ef2a60
ConditionalClass does not work in browser/ios/android, use Conditiona…
carlossanlop File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/libraries/System.IO.FileSystem/tests/Base/BaseSymbolicLinks.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// 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 abstract class BaseSymbolicLinks : FileSystemTest | ||
{ | ||
protected DirectoryInfo CreateDirectoryContainingSelfReferencingSymbolicLink() | ||
{ | ||
DirectoryInfo testDirectory = Directory.CreateDirectory(GetTestFilePath()); | ||
string pathToLink = Path.Join(testDirectory.FullName, GetTestFileName()); | ||
Assert.True(MountHelper.CreateSymbolicLink(pathToLink, pathToLink, isDirectory: true)); // Create a symlink cycle | ||
return testDirectory; | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/libraries/System.IO.FileSystem/tests/Directory/SymbolicLinks.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Xunit; | ||
|
||
namespace System.IO.Tests | ||
{ | ||
public class Directory_SymbolicLinks : BaseSymbolicLinks | ||
{ | ||
[ConditionalFact(nameof(CanCreateSymbolicLinks))] | ||
public void EnumerateDirectories_LinksWithCycles_ShouldNotThrow() | ||
{ | ||
DirectoryInfo testDirectory = CreateDirectoryContainingSelfReferencingSymbolicLink(); | ||
|
||
// Windows differentiates between dir symlinks and file symlinks | ||
int expected = OperatingSystem.IsWindows() ? 1 : 0; | ||
Assert.Equal(expected, Directory.EnumerateDirectories(testDirectory.FullName).Count()); | ||
} | ||
|
||
[ConditionalFact(nameof(CanCreateSymbolicLinks))] | ||
public void EnumerateFiles_LinksWithCycles_ShouldNotThrow() | ||
{ | ||
DirectoryInfo testDirectory = CreateDirectoryContainingSelfReferencingSymbolicLink(); | ||
|
||
// Windows differentiates between dir symlinks and file symlinks | ||
int expected = OperatingSystem.IsWindows() ? 0 : 1; | ||
Assert.Equal(expected, Directory.EnumerateFiles(testDirectory.FullName).Count()); | ||
} | ||
|
||
[ConditionalFact(nameof(CanCreateSymbolicLinks))] | ||
public void EnumerateFileSystemEntries_LinksWithCycles_ShouldNotThrow() | ||
{ | ||
DirectoryInfo testDirectory = CreateDirectoryContainingSelfReferencingSymbolicLink(); | ||
Assert.Single(Directory.EnumerateFileSystemEntries(testDirectory.FullName)); | ||
} | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
src/libraries/System.IO.FileSystem/tests/DirectoryInfo/SymbolicLinks.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Xunit; | ||
|
||
namespace System.IO.Tests | ||
{ | ||
public class DirectoryInfo_SymbolicLinks : BaseSymbolicLinks | ||
{ | ||
[ConditionalTheory(nameof(CanCreateSymbolicLinks))] | ||
[InlineData(false)] | ||
[InlineData(true)] | ||
public void EnumerateDirectories_LinksWithCycles_ThrowsTooManyLevelsOfSymbolicLinks(bool recurse) | ||
{ | ||
var options = new EnumerationOptions() { RecurseSubdirectories = recurse }; | ||
DirectoryInfo testDirectory = CreateDirectoryContainingSelfReferencingSymbolicLink(); | ||
|
||
// Windows avoids accessing the cyclic symlink if we do not recurse | ||
if (OperatingSystem.IsWindows() && !recurse) | ||
{ | ||
testDirectory.EnumerateDirectories("*", options).Count(); | ||
testDirectory.GetDirectories("*", options).Count(); | ||
Comment on lines
+23
to
+24
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. Assert how many entries this gets. |
||
} | ||
else | ||
{ | ||
// Internally transforms the FileSystemEntry to a DirectoryInfo, which performs a disk hit on the cyclic symlink | ||
Assert.Throws<IOException>(() => testDirectory.EnumerateDirectories("*", options).Count()); | ||
Assert.Throws<IOException>(() => testDirectory.GetDirectories("*", options).Count()); | ||
} | ||
} | ||
|
||
[ConditionalTheory(nameof(CanCreateSymbolicLinks))] | ||
[InlineData(false)] | ||
[InlineData(true)] | ||
public void EnumerateFiles_LinksWithCycles_ThrowsTooManyLevelsOfSymbolicLinks(bool recurse) | ||
{ | ||
var options = new EnumerationOptions() { RecurseSubdirectories = recurse }; | ||
DirectoryInfo testDirectory = CreateDirectoryContainingSelfReferencingSymbolicLink(); | ||
|
||
// Windows avoids accessing the cyclic symlink if we do not recurse | ||
if (OperatingSystem.IsWindows() && !recurse) | ||
{ | ||
testDirectory.EnumerateFiles("*", options).Count(); | ||
testDirectory.GetFiles("*", options).Count(); | ||
} | ||
else | ||
{ | ||
// Internally transforms the FileSystemEntry to a FileInfo, which performs a disk hit on the cyclic symlink | ||
Assert.Throws<IOException>(() => testDirectory.EnumerateFiles("*", options).Count()); | ||
Assert.Throws<IOException>(() => testDirectory.GetFiles("*", options).Count()); | ||
jozkee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
[ConditionalTheory(nameof(CanCreateSymbolicLinks))] | ||
[InlineData(false)] | ||
[InlineData(true)] | ||
public void EnumerateFileSystemInfos_LinksWithCycles_ThrowsTooManyLevelsOfSymbolicLinks(bool recurse) | ||
{ | ||
var options = new EnumerationOptions() { RecurseSubdirectories = recurse }; | ||
DirectoryInfo testDirectory = CreateDirectoryContainingSelfReferencingSymbolicLink(); | ||
|
||
// Windows avoids accessing the cyclic symlink if we do not recurse | ||
if (OperatingSystem.IsWindows() && !recurse) | ||
{ | ||
testDirectory.EnumerateFileSystemInfos("*", options).Count(); | ||
testDirectory.GetFileSystemInfos("*", options).Count(); | ||
} | ||
else | ||
{ | ||
// Internally transforms the FileSystemEntry to a FileSystemInfo, which performs a disk hit on the cyclic symlink | ||
Assert.Throws<IOException>(() => testDirectory.EnumerateFileSystemInfos("*", options).Count()); | ||
Assert.Throws<IOException>(() => testDirectory.GetFileSystemInfos("*", options).Count()); | ||
} | ||
} | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
src/libraries/System.IO.FileSystem/tests/Enumeration/SymbolicLinksTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Generic; | ||
using System.IO.Enumeration; | ||
using System.Linq; | ||
using Xunit; | ||
|
||
namespace System.IO.Tests.Enumeration | ||
{ | ||
public class Enumeration_SymbolicLinksTests : BaseSymbolicLinks | ||
{ | ||
[ConditionalFact(nameof(CanCreateSymbolicLinks))] | ||
public void EnumerateDirectories_LinksWithCycles_ShouldNotThrow() | ||
{ | ||
DirectoryInfo testDirectory = CreateDirectoryContainingSelfReferencingSymbolicLink(); | ||
|
||
IEnumerable<string> enumerable = new FileSystemEnumerable<string>( | ||
testDirectory.FullName, | ||
(ref FileSystemEntry entry) => entry.ToFullPath(), | ||
// Skipping attributes would force a disk hit which enters the cyclic symlink | ||
new EnumerationOptions(){ AttributesToSkip = 0 }) | ||
{ | ||
ShouldIncludePredicate = (ref FileSystemEntry entry) => entry.IsDirectory | ||
}; | ||
|
||
// Windows differentiates between dir symlinks and file symlinks | ||
int expected = OperatingSystem.IsWindows() ? 1 : 0; | ||
Assert.Equal(expected, enumerable.Count()); | ||
} | ||
|
||
[ConditionalFact(nameof(CanCreateSymbolicLinks))] | ||
public void EnumerateFiles_LinksWithCycles_ShouldNotThrow() | ||
{ | ||
DirectoryInfo testDirectory = CreateDirectoryContainingSelfReferencingSymbolicLink(); | ||
|
||
IEnumerable<string> enumerable = new FileSystemEnumerable<string>( | ||
testDirectory.FullName, | ||
(ref FileSystemEntry entry) => entry.ToFullPath(), | ||
// Skipping attributes would force a disk hit which enters the cyclic symlink | ||
new EnumerationOptions(){ AttributesToSkip = 0 }) | ||
{ | ||
ShouldIncludePredicate = (ref FileSystemEntry entry) => !entry.IsDirectory | ||
}; | ||
|
||
// Windows differentiates between dir symlinks and file symlinks | ||
int expected = OperatingSystem.IsWindows() ? 0 : 1; | ||
Assert.Equal(expected, enumerable.Count()); | ||
} | ||
|
||
[ConditionalFact(nameof(CanCreateSymbolicLinks))] | ||
public void EnumerateFileSystemEntries_LinksWithCycles_ShouldNotThrow() | ||
{ | ||
DirectoryInfo testDirectory = CreateDirectoryContainingSelfReferencingSymbolicLink(); | ||
|
||
IEnumerable<string> enumerable = new FileSystemEnumerable<string>( | ||
testDirectory.FullName, | ||
(ref FileSystemEntry entry) => entry.ToFullPath(), | ||
// Skipping attributes would force a disk hit which enters the cyclic symlink | ||
new EnumerationOptions(){ AttributesToSkip = 0 }); | ||
|
||
Assert.Single(enumerable); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I don't understand the comment... isn't all of this code about enumerating?
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.
Enumeration always goes through this path, yes. I do not know if in the future we will reach
FileSystemEntry.Initialize
another way.Next time I touch this code, I can change the comment to
// This call needs to fail silently
.