Skip to content

Add API to get actual file casing in path #14321

Open

Description

@ellismg

Per @jasonwilliams200OK in dotnet/coreclr#390

Based on this answer: http://stackoverflow.com/a/81493/1712065 (further redirected from: http://stackoverflow.com/a/326153).

Please add the ability to retrieve path with actual case via FileInfo and DirectoryInfo classes. The candidate member being FullPath and Name. Perhaps there is some sophisticated way of getting it from win32 file system API, but that seems to be a working solution.

Expected:

using System.IO.FileInfo;
// ..
// actual path was:
// C:\\SharePoint\\scripts\\MasterDeployment.ps1
FileInfo info = new FileInfo("c:\\sharepoint\\scriPts\\maSTerdeploymnet.PS1");
Console.WriteLine(info.FullPath);
// C:\\SharePoint\\scripts\\MasterDeployment.ps1
Console.WriteLine(info.Name);
// MasterDeployment.ps1

Actual result:

using System.IO.FileInfo;
// ..
// actual path was:
// C:\\SharePoint\\scripts\\MasterDeployment.ps1
FileInfo info = new FileInfo("c:\\sharepoint\\scriPts\\maSTerdeploymnet.PS1");
Console.WriteLine(info.FullPath);
// c:\\sharepoint\\scriPts\\maSTerdeploymnet.PS1
Console.WriteLine(info.Name);
// maSTerdeploymnet.PS1

Activity

  1. ellismg commented on Mar 5, 2015

    @ellismg
    ContributorAuthor

    @jasonwilliams200OK For .NET Core, the source for the lowest level path APIs are in the CoreFX repository. The implementation in mscorlib.dll in CoreCLR is not what we expose as part of .NET core and will be removed if possible.

    I don't fully understand your comment about CoreFX solution having worse performance than one in CoreCLR. In either case, we would likely implement the feature the same way, by PInvoking to the relevant OS APIs. The native portions of the runtime are not used for any of the File APIs that exposed to managed code.

  2. terrajobst commented on Sep 15, 2015

    @terrajobst
    Contributor

    @JeremyKuhne, it seems Roslyn had a similar request for a an API that gets the canonical path, i.e. resolves sym links, gets the correct casing etc. What's your take?

  3. stephentoub commented on Sep 15, 2015

    @stephentoub
    Member

    I'm not clear on what such an API would mean in a typical Unix context, where file AbC is unrelated to file ABc. It'd just return the original supplied string without modification?

    public static string GetActualCasing(string path) { return path; }

    ?

  4. JeremyKuhne commented on Sep 15, 2015

    @JeremyKuhne
    Member

    @jasonwilliams200OK, @terrajobst: We do need stuff along these lines, what I was going to suggest was Path.GetFinalPath to match the semantics we're already using (e.g. aligning with the File Management APIs in Windows). Essentially it would just be a call to GetFinalPathNameByHandle() on Windows and realpath() on Linux. Symbolic links will be resolved and the file will have to exist.

    @stephentoub I think this api/behavior would address what you're bringing up. I don't think there is a way to specify files with non canonical casing even when using case-insensitive file systems (NTFS) on Unix- but I'm certainly not positive. Anyone know?

    As far as not resolving sym links I'm not sure the best available APIs to make this happen and what we would call it. I suppose if we had a Path.GetCanonicalPath instead of GetFinalPath (matching Linux/Java semantics) we could add an overload to not resolve sym links?

    Note that on Windows you can find the right casing by walking DirectoryInfo/FileInfo objects. The file name that comes back from FindFirst/NextFile is always in the right case.

  5. JeremyKuhne commented on Sep 16, 2015

    @JeremyKuhne
    Member

    Hmm- I hadn't seen that behavior when I was playing with GetLongPathName- I'll take a closer look. The Windows file system folks mentioned that there was a way to get the correct cased name without following sym links- I'll ping them again to see if I can drag out details.

  6. JeremyKuhne commented on Sep 16, 2015

    @JeremyKuhne
    Member

    @jasonwilliams200OK - The thing I was missing was FILE_FLAG_OPEN_REPARSE_POINT. GetFinalPathNameByHandle will give you the real canonical name on whatever file you open. If you pass the flag above when calling CreateFile it won't follow the reparse points and will, instead, open the actual link.

  7. 7 remaining items

  8. JeremyKuhne commented on Jan 14, 2020

    @JeremyKuhne
    Member

    Triage: We want to do this, but there are questions around performance (as we have to walk the full path) and what to do for paths that don't exist or are not accessible. We also need to understand what to do with drive letters and validate casing requirements for device paths (e.g. \\.\Volume....). Effectively we need GetCanonicalCasingForExistingPath but with a better name.

  9. transferred this issue fromdotnet/corefxon Jan 31, 2020
  10. added this to the 5.0 milestone on Jan 31, 2020
  11. added
    untriaged
    on Feb 23, 2020
  12. removed
    untriaged
    on Mar 3, 2020
  13. modified the milestones: 5.0.0, Future on Jun 18, 2020
  14. iSazonov commented on Aug 19, 2020

    @iSazonov
    Contributor
  15. carlossanlop commented on Oct 2, 2020

    @carlossanlop
    Contributor

    If an OS allows using case-insensitive paths, why should it be considered a problem if the specified path string does not have the exact same casing as the actual path? It is allowed behavior. What would be the benefit of fixing this?

    This comment in the linked PowerShell issue offers a related explanation:

    Well, it's not strictly a functional problem, because it only applies to platforms with case-insensitive file systems, notably macOS and Windows - on Linux, with its case-sensitive file system you have to supply the case-accurate representation to begin with, otherwise you won't find the file / directory.

    Also, suggesting a cross-platform API to get the real path would not make sense in Unix, as it was pointed out here.

  16. PathogenDavid commented on Oct 3, 2020

    @PathogenDavid
    Contributor

    Also, suggesting a cross-platform API to get the real path would not make sense in Unix, as it was pointed out here.

    Beyond the extent of any APIs it can provide to help this issue, the OS should really not be a consideration here. Case sensitivity is an attribute of the file system, not the operating system. Apple's file systems have always supported both due to legacy Mac OS's case sensitivity. EXT4 supports toggling case insensitivity at the directory level. Linux (and notably
    WSL) has to deal with NTFS all the time. Heck, everything can mount FAT volumes which are case-insensitive.

    If an OS allows using case-insensitive paths, why should it be considered a problem if the specified path string does not have the exact same casing as the actual path? It is allowed behavior. What would be the benefit of fixing this?

    • Detecting when something is taking advantage of the case-insensitive file system when it shouldn't because it needs to be cross-platform.
    • Logging the canonical casing of a path when the user provided the wrong casing.
    • Scenarios when file paths come from more than one location.

    The scenario that prompted me to follow this issue in the first place is largely the third problem, but I've run into all three.

    A project I'm currently working on involves parsing C++ code using the Clang compiler. From a high level view, one aspect of that process looks like this:

    1. Ask the compiler to compile a bunch of files (which is done by compiling a single file which #includes them all for technical reasons)
    2. Get a big tree of syntax nodes back
    3. Work through those syntax nodes and if they correspond to one of the original input files, process them

    The "if they correspond" bit is the source of some whacky edge-case bugs right now. The main issue is that the paths I get back from Clang might not be the same paths I originally put in because files can be included more than once in C++. Currently we assume everything is case-insensitive, and this will do the right thing in 99.9% of cases. If you want to read about those 0.1% of cases I did a more detailed quick-and-dirty writeup of the issues we face here: MochiLibraries/Biohazrd#1 (comment)

    I definitely see this API as something for solving weird edge cases. You should not normally need it, but when you do it's annoying to get right. The processing of paths has always been a huge source of edge case bugs in software (see countless security issues caused by directory traversal attacks, the need for Path.Combine, or all the little edge cases Path.GetFullPath deals with), so I think this is something best provided by the BCL.

  17. iSazonov commented on Oct 3, 2020

    @iSazonov
    Contributor

    What would be the benefit of fixing this?

    In PowerShell repository main motivation was "users want nice output" like Windows Explorer does. It was approved and implemented by PowerShell MSFT team.

    Also, suggesting a cross-platform API to get the real path would not make sense in Unix

    As mentioned it is file system behavior. And we should consider both local scenario (mount NTFS on Unix and ext4 on Windows) and remote scenario (I mean PowerShell can connect to remote computer - what is an expected behavior in the case?).

    /cc @mklement0

  18. MichalPetryka commented on Mar 2, 2021

    @MichalPetryka
    Contributor

    A good usecase for this API (maybe even the most important) are interactions with tools like git, which are case sensitive even on case insensitive filesystems.
    And as said before, the API should be crossplatform as you can use case insenvitive filesystems like FAT even on Linux.

  19. DL444 commented on Sep 20, 2023

    @DL444

    I was in a situation where I needed to perform some operations on a bunch of files except those specified by the user. On case-insensitive file systems, exceptions were to be matched case-insensitively, i.e. "file" would match "file", "File", and "FILE". On case-sensitive file systems, exceptions needed to match exactly, i.e. "file" would only match "file" and not "File" or "FILE".

    I want to point out that the GetFileSystemInfos() trick referenced many times in the discussion is quite problematic. GetFileSystemInfos(string searchPattern) is an overload for GetFileSystemInfos(string searchPattern, EnumerationOptions enumerationOptions) with enumerationOptions.MatchCasing set to PlatformDefault. So it wouldn't work if the default case sensitivity for the OS does not match the case sensitivity for the specific file system used.

    For example, macOS is case-insensitive by default, and GetFileSystemInfos(string searchPattern) performs case-insensitive enumeration. But macOS also supports case-sensitive APFS volumes. Calling GetFileSystemInfos("file") on a case-sensitive volume that contains "File" and "FILE" will give you exactly those two files, neither of which is what you expected and which one comes first is undefined. This can easily cause data corruption if you are not careful.

    Windows NTFS also supports this, enabled per-directory with fsutil file setCaseSensitiveInfo. I assume this is mostly for WSL, but it being used outside of WSL is still a possibility to account for if your application runs everywhere.

    Most common Linux filesystems are case-sensitive by default. But there's a diverse ecosystem of file systems out there for Linux, I wouldn't be surprised if one of them is case-insensitive (take NTFS on Linux).

    Therefore I would say assuming case sensitivity of the OS is probably not a robust design. Depending on your use case, you might be able to get away with an explicitly case-insensitive GetFileSystemInfos() followed by a [File/Directory].Exists() and reason over the item's existence and how many matches you get to infer case sensitivity of the file system and act accordingly. But I imagine this wouldn't perform well, and someone could pull the rug from under you by modifying the directory in between those two method calls. Therefore I wouldn't call this a proper solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionarea-System.IO

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions