-
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
Directory.EnumerateFiles on Linux recurses infinitely with bad symlinks #97123
Comments
Tagging subscribers to this area: @dotnet/area-system-io Issue DetailsDescriptionOn Linux, Reproduction Steps
Expected behaviorLinux builds should follow Windows builds and not follow the bad symlink. Actual behaviorHangs indefinitely (although I assume it actually recurses IntMax times) Regression?No response Known WorkaroundsNo response Configurationnet8.0, linux-x64 Other informationNo response
|
Linux doesn't allow to create links that have an empty target path. I think we may be seeing the expected behavior of the kernel when there is such a link in a filesystem. I assume the empty path is treated the same as a link where the target path is As we're recursing into the same directory again through the link, eventually If you use var fse = new FileSystemEnumerable<string>(
directory,
transform: (ref FileSystemEntry entry) => entry.ToFullPath(),
options: new EnumerationOptions()
{
RecurseSubdirectories = true
});
foreach (string path in fse)
{
Console.WriteLine(path);
} |
@Deterous can you run the above code on your directory? I imagine it may do something similar to the following, where the link target is using System.IO.Enumeration;
string directory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
Directory.CreateDirectory(directory);
Directory.CreateSymbolicLink(Path.Combine(directory, "self"), ".");
var fse = new FileSystemEnumerable<string>(
directory,
transform: (ref FileSystemEntry entry) => entry.ToFullPath(),
options: new EnumerationOptions()
{
RecurseSubdirectories = true
});
foreach (string path in fse)
{
Console.WriteLine(path);
} The output of this program on my system is:
And then it does stop (due to the Notice that the paths get longer. That's because .NET doesn't do anything that relies on the link target path, but it just appends the link name to the current directory path. It would be an issue if .NET were using the link target, and that would cause the same path to show up here continuously. That is not the case. |
@tmds Thank you, I also assumed the empty path was treated as current directory. Your output from is similar to the output I get, except in my case there are three files that are symlinked to For context, these files exist on a UDF filesystem on a DVD-ROM, I did not create these symlinks nor do I know how they were created. Is this then "expected" dotnet behaviour? Can anything be done to prevent this unnecessary recursion? |
For some use-cases you don't actually want to follow links. There is an API proposal for adding an option to control whether links should be followed: #52666. You can implement something yourself using To skip recursing into symlinks with I'll close the issue as there is nothing to be changed on the .NET side. |
Description
On Linux,
Directory.GetFiles(path, "*", SearchOption.AllDirectories)
andDirectory.EnumerateFiles(path, "*", SearchOption.AllDirectories)
both recurse infinitely when an unreadable symlink (File -> ''
) is present, seemingly seeing''
as the current directory and recursing into itself. This was confirmed by limiting the recursion with EnumerateOptions to 9, andEnumerateFiles
saw the symlink as a folder and recursed downwards 9 times.Windows dotnet does not follow the bad symlink and does not recurse infinitely.
Reproduction Steps
Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories)
where path is a path to a folder containing an unreadable symlink to''
Expected behavior
Linux builds should follow Windows builds and not follow the bad symlink.
Actual behavior
Hangs indefinitely on Linux (although I assume it actually recurses IntMax times)
Regression?
No response
Known Workarounds
No response
Configuration
net8.0, linux-x64
Other information
No response
The text was updated successfully, but these errors were encountered: