Skip to content

API Proposal: tell if a filesystem entry is any type of link #53577

@carlossanlop

Description

@carlossanlop

Background and Motivation

We recently got some APIs approved to provide the ability to create a symbolic link, and to return the target of a symbolic link.

The discussion continued after the approval to request an additional API that would tell if a file is a link or not.

There were a few particular things that were emphasized during the discussion:

  • The API can be opaque: it does not need to tell the type of link, only if it is any link.
  • There are 3 types of links that we should support: symbolic link, junction (NTFS) and AppExecLink (NTFS).
  • We can later decide if we want to expose all the reparse tags, for a more fine-grained control of reparse points. We have an issue open to discuss the design of such APIs.

Also, there are some platform-specific properties of these link types that we need to keep in mind:

  • Windows differentiates between a symbolic link to a file or to a directory. Unix does not.
  • NTFS supports Junctions and AppExecLinks. It's OS independent, so if a Unix machine has the proper NTFS driver installed, these link types should be detected there too.
  • Junctions only apply to directories.
  • AppExecLinks only apply to files.
  • When a FileInfo wraps a link that points to a directory, there is no exception thrown, but Exists returns false. Same if a DirectoryInfo wraps a link to a file. Our API should have a similar behavior.

Proposed API

namespace System.IO.FileSystem
{
    public abstract class FileSystemInfo
    {
+       public bool IsLink { get; }
    }
}

Usage Examples

FileInfo file = new FileInfo("/path/to/file-link");
Console.WriteLine(file.IsLink); // Prints true if file is a symlink to a file

DirectoryInfo directory = new DirectoryInfo("/path/to/dir-link");
Console.WriteLine(directory.IsLink); // Prints true if file is a symlink to a directory

FileInfo wrongfile = new FileInfo("/path/to/dir-link");
Console.WriteLine(wrongfile.IsLink); // Prints false because FileInfo is wrapping a directory link

FileInfo wrongdir = new FileInfo("/path/to/file-link");
Console.WriteLine(wrongdir.IsLink); // Prints false because DirectoryInfo is wrapping a file link

Optional additional designs

If desired during the review, we can also add static methods so users don't have to rely on FileSystemInfo instances:

namespace System.IO.FileSystem
{
    public static class File
    {
+       public static bool IsLink(string path);
    }
}
namespace System.IO.FileSystem
{
    public static class Directory
    {
+       public static bool IsLink(string path);
    }
}

cc @jozkee @mklement0 @tmds @iSazonov

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-System.IO

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions