Skip to content

Some filesystem enumeration cases may throw if a symbolic link cycle is detected (link to itself) #52746

@carlossanlop

Description

@carlossanlop

Problem

This recent PR introduced fixes for the ReadOnly and Hidden flags. While investigating the implementation of the recently approved symbolic link APIs, @jozkee and I discovered a potential regression for which we did not have any unit tests yet.

Repro steps

  • In Unix, create a folder, then add a symbolic link with a cycle (for example, make it point to itself with ln -s mylink mylink).
  • Enumerate the files with FileSystemEnumerable (making sure the transform predicate returns a string with the path), or call one of the Directory.Enumerate* or Directory.Get* methods that return a list of strings with the paths.

Expected

The enumeration invocation is performed to its completion without throwing.

Actual

The enumeration invocation throws due to encountering a symbolic link with a cycle to itself.

Root cause

Before the PR, the method FileSystemEntry.Initialize, which is a called by FileSystemEnumerator.MoveNext, would silently skip the retrieval of the directory information via stat or lstat:

&& Interop.Sys.Stat(entry.FullPath, out Interop.Sys.FileStatus targetStatus) >= 0)

&& (Interop.Sys.LStat(entry.FullPath, out Interop.Sys.FileStatus linkTargetStatus) >= 0))

Now, we are throwing because we are retrieving the directory information by calling the FileStatysm.IsDirectory method, which internally throws unless otherwise specified:

isDirectory = entry._status.IsDirectory(entry.FullPath);

isDirectory = entry._status.IsDirectory(entry.FullPath);

Fix

Pass the continueOnError: true argument to the IsDirectory calls.

Add unit tests to verify enumeration of directories containing cyclic symbolic links is performed without problems.

Notes

Metadata

Metadata

Assignees

Labels

area-System.IOuntriagedNew issue has not been triaged by the area owner

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions